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.plugin.PluginDescriptor;
022import icy.plugin.PluginLoader;
023import icy.plugin.PluginLoader.PluginLoaderEvent;
024import icy.plugin.PluginLoader.PluginLoaderListener;
025import icy.preferences.PluginPreferences;
026
027import java.awt.BorderLayout;
028import java.util.ArrayList;
029import java.util.HashSet;
030
031import javax.swing.Box;
032import javax.swing.BoxLayout;
033import javax.swing.JLabel;
034import javax.swing.JPanel;
035
036/**
037 * @author Stephane
038 */
039public class PluginStartupPreferencePanel extends PluginListPreferencePanel implements PluginLoaderListener
040{
041    /**
042     * 
043     */
044    private static final long serialVersionUID = -3485972129754541852L;
045
046    public static final String NODE_NAME = "Startup Plugin";
047
048    final HashSet<String> inactives;
049
050    public PluginStartupPreferencePanel(PreferenceFrame parent)
051    {
052        super(parent, NODE_NAME, PluginPreferencePanel.NODE_NAME);
053
054        inactives = new HashSet<String>();
055
056        PluginLoader.addListener(this);
057
058        // remove columns 3 (not used here)
059        table.removeColumn(table.getColumn(columnIds[3]));
060
061        // filter.setVisible(false);
062        action1Button.setVisible(false);
063        action2Button.setVisible(false);
064        refreshButton.setVisible(false);
065
066        final JPanel topPanel = new JPanel();
067        topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.PAGE_AXIS));
068
069        topPanel.add(Box.createVerticalStrut(4));
070        topPanel.add(new JLabel("Setup plugin which should be loaded at startup (daemon plugin only)."));
071        topPanel.add(Box.createVerticalStrut(4));
072
073        mainPanel.add(topPanel, BorderLayout.NORTH);
074
075        load();
076        pluginsChanged();
077    }
078
079    @Override
080    protected void closed()
081    {
082        super.closed();
083
084        PluginLoader.removeListener(this);
085    }
086
087    @Override
088    protected boolean isActive(PluginDescriptor plugin)
089    {
090        return !inactives.contains(plugin.getClassName());
091    }
092
093    @Override
094    protected void setActive(PluginDescriptor plugin, boolean value)
095    {
096        final String className = plugin.getClassName();
097
098        if (value)
099            inactives.remove(className);
100        else
101        {
102            if (!inactives.contains(className))
103                inactives.add(className);
104        }
105    }
106
107    @Override
108    protected void load()
109    {
110        inactives.clear();
111        inactives.addAll(PluginPreferences.getInactiveDaemons());
112    }
113
114    @Override
115    protected void save()
116    {
117        // save preferences
118        PluginPreferences.setInactiveDaemons(new ArrayList<String>(inactives));
119        // restart daemon plugins
120        PluginLoader.resetDaemons();
121    }
122
123    @Override
124    protected void doAction1()
125    {
126    }
127
128    @Override
129    protected void doAction2()
130    {
131    }
132
133    @Override
134    protected void repositoryChanged()
135    {
136        // do nothing here
137    }
138
139    @Override
140    protected void reloadPlugins()
141    {
142        // do nothing here
143    }
144
145    @Override
146    protected String getStateValue(PluginDescriptor plugin)
147    {
148        return "";
149    }
150
151    @Override
152    protected ArrayList<PluginDescriptor> getPlugins()
153    {
154        return PluginLoader.getDaemonPlugins();
155    }
156
157    @Override
158    protected void updateButtonsStateInternal()
159    {
160        super.updateButtonsStateInternal();
161
162        if (PluginLoader.isLoading())
163        {
164            refreshButton.setText("Reloading...");
165            refreshButton.setEnabled(false);
166        }
167        else
168        {
169            refreshButton.setText("Reload list");
170            refreshButton.setEnabled(true);
171        }
172
173        action1Button.setEnabled(false);
174        action2Button.setEnabled(false);
175    }
176
177    @Override
178    public void pluginLoaderChanged(PluginLoaderEvent e)
179    {
180        pluginsChanged();
181    }
182}