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.plugin;
020
021import icy.gui.frame.ActionFrame;
022import icy.gui.util.GuiUtil;
023import icy.plugin.PluginDescriptor;
024import icy.plugin.PluginUpdater;
025import icy.system.thread.ThreadUtil;
026import icy.util.StringUtil;
027
028import java.awt.BorderLayout;
029import java.awt.Dimension;
030import java.awt.event.ActionEvent;
031import java.awt.event.ActionListener;
032import java.util.ArrayList;
033import java.util.List;
034
035import javax.swing.BorderFactory;
036import javax.swing.Box;
037import javax.swing.DefaultListModel;
038import javax.swing.JLabel;
039import javax.swing.JList;
040import javax.swing.JPanel;
041import javax.swing.JScrollPane;
042import javax.swing.JSplitPane;
043import javax.swing.JTextArea;
044import javax.swing.ListSelectionModel;
045import javax.swing.event.ListSelectionEvent;
046import javax.swing.event.ListSelectionListener;
047
048/**
049 * @author Stephane
050 */
051public class PluginUpdateFrame extends ActionFrame
052{
053    JList pluginList;
054    private DefaultListModel listModel;
055
056    public PluginUpdateFrame(final List<PluginDescriptor> toInstallPlugins)
057    {
058        super("Plugin Update", true);
059
060        setPreferredSize(new Dimension(640, 500));
061
062        final JPanel titlePanel = GuiUtil.createCenteredBoldLabel("Select the plugin(s) to update in the list");
063        titlePanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
064
065        final JTextArea changeLogArea = new JTextArea();
066        changeLogArea.setEditable(false);
067        final JLabel changeLogTitleLabel = GuiUtil.createBoldLabel("Change log :");
068
069        listModel = new DefaultListModel();
070        pluginList = new JList(listModel);
071        for (PluginDescriptor plugin : toInstallPlugins)
072            listModel.addElement(plugin);
073
074        pluginList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
075        pluginList.getSelectionModel().addListSelectionListener(new ListSelectionListener()
076        {
077            @Override
078            public void valueChanged(ListSelectionEvent e)
079            {
080                final PluginDescriptor plugin = (PluginDescriptor) pluginList.getSelectedValue();
081
082                if (plugin != null)
083                {
084                    // plugin.loadChangeLog() can take lot of time, better to do that in background...
085                    ThreadUtil.bgRun(new Runnable()
086                    {
087                        @Override
088                        public void run()
089                        {
090                            plugin.loadChangeLog();
091                            final String changeLog = plugin.getChangeLog();
092
093                            if (StringUtil.isEmpty(changeLog))
094                                changeLogArea.setText("no change log");
095                            else
096                                changeLogArea.setText(changeLog);
097                            changeLogArea.setCaretPosition(0);
098                            changeLogTitleLabel.setText(plugin.getName() + " change log");
099                        }
100                    });
101                }
102            }
103        });
104        pluginList.setSelectionInterval(0, toInstallPlugins.size() - 1);
105
106        getOkBtn().setText("Update");
107        getCancelBtn().setText("Close");
108        setCloseAfterAction(false);
109        setOkAction(new ActionListener()
110        {
111            @Override
112            public void actionPerformed(ActionEvent e)
113            {
114                // launch update
115                doUpdate();
116            }
117        });
118
119        final JScrollPane medScrollPane = new JScrollPane(pluginList);
120        final JScrollPane changeLogScrollPane = new JScrollPane(GuiUtil.createTabArea(changeLogArea, 4));
121        final JPanel bottomPanel = GuiUtil.createPageBoxPanel(Box.createVerticalStrut(4),
122                GuiUtil.createCenteredLabel(changeLogTitleLabel), Box.createVerticalStrut(4), changeLogScrollPane);
123
124        final JPanel mainPanel = getMainPanel();
125
126        final JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, medScrollPane, bottomPanel);
127
128        mainPanel.add(titlePanel, BorderLayout.NORTH);
129        mainPanel.add(splitPane, BorderLayout.CENTER);
130
131        pack();
132        addToDesktopPane();
133        setVisible(true);
134        center();
135        requestFocus();
136
137        // set splitter to middle
138        splitPane.setDividerLocation(0.5d);
139    }
140
141    /**
142     * update selected plugins
143     */
144    protected void doUpdate()
145    {
146        final ArrayList<PluginDescriptor> plugins = new ArrayList<PluginDescriptor>();
147
148        for (Object value : pluginList.getSelectedValues())
149            plugins.add((PluginDescriptor) value);
150
151        for (PluginDescriptor plugin : plugins)
152            listModel.removeElement(plugin);
153
154        // no more plugin to update ? close frame
155        if (listModel.isEmpty())
156            close();
157
158        // process plugins update in background
159        ThreadUtil.bgRun(new Runnable()
160        {
161            @Override
162            public void run()
163            {
164                if (!plugins.isEmpty())
165                    PluginUpdater.updatePlugins(plugins, true);
166            }
167        });
168    }
169}