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.RepositoryPreferences.RepositoryInfo;
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.WorkspaceRepositoryLoader;
028import icy.workspace.WorkspaceRepositoryLoader.WorkspaceRepositoryLoaderListener;
029
030import java.util.ArrayList;
031
032/**
033 * @author Stephane
034 */
035public class WorkspaceOnlinePreferencePanel extends WorkspaceListPreferencePanel implements
036        WorkspaceRepositoryLoaderListener, WorkspaceInstallerListener
037{
038    private enum WorkspaceOnlineState
039    {
040        NULL, INSTALLING, DELETING, HAS_INSTALL, INSTALLED
041    }
042
043    /**
044     * 
045     */
046    private static final long serialVersionUID = 5061606004171912231L;
047
048    public static final String NODE_NAME = "Online Workspace";
049
050    WorkspaceOnlinePreferencePanel(PreferenceFrame parent)
051    {
052        super(parent, NODE_NAME);
053
054        WorkspaceRepositoryLoader.addListener(this);
055        WorkspaceInstaller.addListener(this);
056
057        repositoryPanel.setVisible(true);
058        action1Button.setText("Install");
059        action1Button.setVisible(true);
060
061        updateButtonsState();
062        updateRepositories();
063    }
064
065    @Override
066    protected void closed()
067    {
068        super.closed();
069
070        WorkspaceRepositoryLoader.removeListener(this);
071        WorkspaceInstaller.removeListener(this);
072    }
073
074    private WorkspaceOnlineState getWorkspaceOnlineState(Workspace workspace)
075    {
076        if (workspace == null)
077            return WorkspaceOnlineState.NULL;
078
079        if ((WorkspaceInstaller.isInstallingWorkspace(workspace)))
080            return WorkspaceOnlineState.INSTALLING;
081
082        if ((WorkspaceInstaller.isDesinstallingWorkspace(workspace)))
083            return WorkspaceOnlineState.DELETING;
084
085        if (WorkspaceLoader.isLoaded(workspace))
086            return WorkspaceOnlineState.INSTALLED;
087
088        return WorkspaceOnlineState.HAS_INSTALL;
089    }
090
091    @Override
092    protected int getColumnCount()
093    {
094        return 3;
095    }
096
097    @Override
098    protected void doAction1(Workspace workspace)
099    {
100        switch (getWorkspaceOnlineState(workspace))
101        {
102            case HAS_INSTALL:
103                // install workspace
104                WorkspaceInstaller.install(workspace, true);
105                // refresh state
106                refreshTableData();
107                updateButtonsState();
108                break;
109
110            case INSTALLED:
111                // desinstall workspace
112                WorkspaceInstaller.desinstall(workspace, true);
113                // refresh state
114                refreshTableData();
115                updateButtonsState();
116                break;
117        }
118    }
119
120    @Override
121    protected void repositoryChanged()
122    {
123        refreshWorkspaces();
124    }
125
126    @Override
127    protected void reloadWorkspaces()
128    {
129        WorkspaceRepositoryLoader.reload();
130        refreshWorkspaces();
131    }
132
133    @Override
134    protected String getStateValue(Workspace workspace)
135    {
136        switch (getWorkspaceOnlineState(workspace))
137        {
138            case INSTALLING:
139                return "installing...";
140
141            case DELETING:
142                return "deleting...";
143
144            case INSTALLED:
145                return "installed";
146        }
147
148        return "";
149    }
150
151    @Override
152    protected ArrayList<Workspace> getWorkspaces()
153    {
154        // get selected repository
155        final Object selectedItem = repository.getSelectedItem();
156
157        // load workspaces from selected repository
158        if (selectedItem != null)
159            return WorkspaceRepositoryLoader.getWorkspaces((RepositoryInfo) selectedItem);
160
161        return WorkspaceRepositoryLoader.getWorkspaces();
162    }
163
164    @Override
165    protected void updateButtonsStateInternal()
166    {
167        if (WorkspaceRepositoryLoader.isLoading())
168        {
169            refreshButton.setText("Reloading...");
170            refreshButton.setEnabled(false);
171            repository.setEnabled(false);
172        }
173        else
174        {
175            refreshButton.setText("Reload list");
176            refreshButton.setEnabled(true);
177            repository.setEnabled(true);
178        }
179
180        final Workspace workspace = getSelectedWorkspace();
181
182        if (workspace == null)
183        {
184            action1Button.setEnabled(false);
185            return;
186        }
187
188        switch (getWorkspaceOnlineState(workspace))
189        {
190            case INSTALLING:
191                action1Button.setText("Installing...");
192                action1Button.setEnabled(false);
193                break;
194
195            case DELETING:
196                action1Button.setText("Deleting...");
197                action1Button.setEnabled(false);
198                break;
199
200            case HAS_INSTALL:
201                action1Button.setText("Install");
202                action1Button.setEnabled(true);
203                break;
204
205            case INSTALLED:
206                action1Button.setText("Delete");
207                action1Button.setEnabled(true);
208                break;
209
210            case NULL:
211                action1Button.setEnabled(false);
212                break;
213        }
214    }
215
216    @Override
217    public void workspaceRepositeryLoaderChanged()
218    {
219        workspacesChanged();
220    }
221
222    @Override
223    public void workspaceInstalled(WorkspaceInstallerEvent e)
224    {
225        refreshTableData();
226        updateButtonsState();
227    }
228
229    @Override
230    public void workspaceRemoved(WorkspaceInstallerEvent e)
231    {
232        refreshTableData();
233        updateButtonsState();
234    }
235
236}