001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.gui.preferences;
020
021import icy.preferences.WorkspaceLocalPreferences;
022import icy.workspace.Workspace;
023import icy.workspace.WorkspaceInstaller;
024import icy.workspace.WorkspaceInstaller.WorkspaceInstallerEvent;
025import icy.workspace.WorkspaceInstaller.WorkspaceInstallerListener;
026import icy.workspace.WorkspaceLoader;
027import icy.workspace.WorkspaceLoader.WorkspaceLoaderEvent;
028import icy.workspace.WorkspaceLoader.WorkspaceLoaderListener;
029
030import java.util.ArrayList;
031import java.util.List;
032
033/**
034 * @author Stephane
035 */
036public class WorkspaceLocalPreferencePanel extends WorkspaceListPreferencePanel implements WorkspaceLoaderListener,
037        WorkspaceInstallerListener
038{
039    /**
040     * 
041     */
042    private static final long serialVersionUID = -7178443947421949836L;
043
044    public static final String NODE_NAME = "Local Workspace";
045
046    private ArrayList<String> activesWorkspace;
047
048    WorkspaceLocalPreferencePanel(PreferenceFrame parent)
049    {
050        super(parent, NODE_NAME);
051
052        action1Button.setText("Delete");
053        action1Button.setVisible(true);
054
055        refreshWorkspaces();
056
057        WorkspaceLoader.addListener(this);
058        WorkspaceInstaller.addListener(this);
059
060    }
061
062    @Override
063    protected void closed()
064    {
065        super.closed();
066
067        WorkspaceLoader.removeListener(this);
068        WorkspaceInstaller.removeListener(this);
069    }
070
071    @Override
072    protected int getColumnCount()
073    {
074        return 4;
075    }
076
077    @Override
078    protected void doAction1(Workspace workspace)
079    {
080        // desinstall workspace
081        WorkspaceInstaller.desinstall(workspace, true);
082        // refresh state
083        refreshTableData();
084        updateButtonsState();
085    }
086
087    @Override
088    protected Boolean isWorkspaceEnable(Workspace workspace)
089    {
090        return Boolean.valueOf(activesWorkspace.contains(workspace.getName()));
091    }
092
093    @Override
094    protected void setWorkspaceEnable(Workspace workspace, Boolean value)
095    {
096        final String name = workspace.getName();
097
098        if (value.booleanValue())
099        {
100            if (!activesWorkspace.contains(name))
101                activesWorkspace.add(name);
102        }
103        else
104            activesWorkspace.remove(name);
105    }
106
107    private void cleanActivesWorkspace(List<Workspace> workspaces)
108    {
109        // clean active workspace list
110        for (int i = activesWorkspace.size() - 1; i >= 0; i--)
111        {
112            final String name = activesWorkspace.get(i);
113
114            if (Workspace.getWorkspace(workspaces, name) == null)
115                activesWorkspace.remove(i);
116        }
117    }
118
119    @Override
120    protected void load()
121    {
122        super.load();
123
124        activesWorkspace = WorkspaceLocalPreferences.getActivesWorkspace();
125    }
126
127    @Override
128    protected void save()
129    {
130        WorkspaceLocalPreferences.setActivesWorkspace(activesWorkspace);
131
132        super.save();
133    }
134
135    @Override
136    protected void repositoryChanged()
137    {
138        // do nothing here
139    }
140
141    @Override
142    protected void reloadWorkspaces()
143    {
144        WorkspaceLoader.reloadAsynch();
145        refreshWorkspaces();
146    }
147
148    @Override
149    protected String getStateValue(Workspace workspace)
150    {
151        if (workspace == null)
152            return "";
153
154        if (WorkspaceInstaller.isDesinstallingWorkspace(workspace))
155            return "Deleting...";
156
157        return "";
158    }
159
160    @Override
161    protected List<Workspace> getWorkspaces()
162    {
163        final List<Workspace> result;
164
165        if (WorkspaceLoader.isLoading())
166            result = new ArrayList<Workspace>();
167        else
168        {
169            result = WorkspaceLoader.getWorkspaces();
170
171            // we don't want to see here WORKSPACE_SYSTEM
172            Workspace.removeWorkspace(result, Workspace.WORKSPACE_SYSTEM_NAME);
173            cleanActivesWorkspace(result);
174        }
175
176        return result;
177    }
178
179    @Override
180    protected void updateButtonsStateInternal()
181    {
182        if (WorkspaceLoader.isLoading())
183        {
184            refreshButton.setText("Reloading...");
185            refreshButton.setEnabled(false);
186        }
187        else
188        {
189            refreshButton.setText("Reload list");
190            refreshButton.setEnabled(true);
191        }
192
193        final Workspace workspace = getSelectedWorkspace();
194
195        if (workspace == null)
196        {
197            action1Button.setText("Delete");
198            action1Button.setEnabled(false);
199            return;
200        }
201
202        // special case where workspace is currently begin removed
203        if (WorkspaceInstaller.isDesinstallingWorkspace(workspace))
204        {
205            action1Button.setText("Deleting...");
206            action1Button.setEnabled(false);
207        }
208        else
209        {
210            action1Button.setText("Delete");
211            action1Button.setEnabled(true);
212        }
213    }
214
215    @Override
216    public void workspaceLoaderChanged(WorkspaceLoaderEvent e)
217    {
218        workspacesChanged();
219    }
220
221    @Override
222    public void workspaceInstalled(WorkspaceInstallerEvent e)
223    {
224        final Workspace workspace = e.getWorkspace();
225
226        // enable the installed workspace by default
227        setWorkspaceEnable(workspace, Boolean.TRUE);
228        // workspace setting changed, restart needed
229        getPreferenceFrame().setNeedRestart();
230        updateButtonsState();
231    }
232
233    @Override
234    public void workspaceRemoved(WorkspaceInstallerEvent e)
235    {
236        // workspace setting changed, restart needed
237        getPreferenceFrame().setNeedRestart();
238        updateButtonsState();
239    }
240}