blob: dfef2fe36373d89050b10088e49725f94615be66 [file] [log] [blame]
/*******************************************************************************
* Copyright (C) 2013 The University of Manchester
*
* Modifications to the initial code base are copyright of their
* respective authors, or their employers as appropriate.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
******************************************************************************/
package net.sf.taverna.t2.workbench.plugin.impl;
import static java.awt.GridBagConstraints.BOTH;
import static java.awt.GridBagConstraints.EAST;
import static java.awt.GridBagConstraints.NONE;
import static java.awt.GridBagConstraints.WEST;
import static java.lang.Math.max;
import static javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER;
import static javax.swing.SwingConstants.CENTER;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.LayoutManager;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import org.apache.log4j.Logger;
import uk.org.taverna.commons.plugin.Plugin;
import uk.org.taverna.commons.plugin.PluginException;
import uk.org.taverna.commons.plugin.PluginManager;
import uk.org.taverna.commons.plugin.xml.jaxb.PluginVersions;
/**
* @author David Withers
*/
@SuppressWarnings("serial")
public class PluginManagerPanel extends JPanel {
private static final Logger logger = Logger
.getLogger(PluginManagerPanel.class);
private PluginManager pluginManager;
private JLabel message = new JLabel("");
public PluginManagerPanel(PluginManager pluginManager) {
this.pluginManager = pluginManager;
initialize();
}
public void initialize() {
removeAll();
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = WEST;
gbc.gridy = 0;
gbc.insets.left = 5;
gbc.insets.right = 5;
gbc.insets.top = 5;
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Available", createAvailablePluginsPanel());
tabbedPane.addTab("Installed", createInstalledPluginsPanel());
tabbedPane.addTab("Updates", createUpdatePluginsPanel());
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridy = 1;
gbc.fill = BOTH;
add(tabbedPane, gbc);
gbc.anchor = EAST;
gbc.fill = NONE;
gbc.weightx = 0;
gbc.weighty = 0;
gbc.gridy = 2;
gbc.insets.bottom = 5;
add(message, gbc);
}
public void checkForUpdates() {
message.setText("Checking for updates");
try {
pluginManager.checkForUpdates();
} catch (PluginException e) {
logger.info("Error checking for plugin updates", e);
} finally {
message.setText("");
}
}
private static Component scrolled(Component view) {
JScrollPane scrollPane = new JScrollPane(view);
scrollPane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setBorder(null);
return scrollPane;
}
private Component createAvailablePluginsPanel() {
try {
List<PluginVersions> availablePlugins = pluginManager
.getAvailablePlugins();
if (availablePlugins.size() == 0)
return new JLabel("No new plugins available", CENTER);
JPanel panel = new JPanel(new ListLayout());
for (PluginVersions plugin : availablePlugins)
panel.add(new AvailablePluginPanel(plugin, pluginManager));
return scrolled(panel);
} catch (PluginException e) {
logger.info("Error looking for new plugins", e);
return new JLabel("No new plugins available", CENTER);
}
}
private Component createInstalledPluginsPanel() {
try {
List<Plugin> installedPlugins = pluginManager.getInstalledPlugins();
if (installedPlugins.size() == 0)
return new JLabel("No installed plugins", CENTER);
JPanel panel = new JPanel(new ListLayout());
for (Plugin plugin : installedPlugins)
panel.add(new InstalledPluginPanel(plugin));
return scrolled(panel);
} catch (PluginException e) {
return new JLabel("No installed plugins", CENTER);
}
}
private Component createUpdatePluginsPanel() {
try {
List<PluginVersions> pluginUpdates = pluginManager
.getPluginUpdates();
if (pluginUpdates.size() == 0)
return new JLabel("All plugins are up to date", CENTER);
JPanel panel = new JPanel(new ListLayout());
for (PluginVersions plugin : pluginUpdates)
panel.add(new UpdatePluginPanel(plugin, pluginManager));
return scrolled(panel);
} catch (PluginException e) {
return new JLabel("All plugins are up to date", CENTER);
}
}
private final class ListLayout implements LayoutManager {
@Override
public void addLayoutComponent(String name, Component comp) {
}
@Override
public void removeLayoutComponent(Component comp) {
}
@Override
public Dimension preferredLayoutSize(Container parent) {
Dimension preferredLayoutSize = new Dimension(0, 1);
for (Component component : parent.getComponents()) {
Dimension preferredSize = component.getPreferredSize();
preferredLayoutSize.width = max(preferredSize.width,
preferredLayoutSize.width);
preferredLayoutSize.height = preferredSize.height
+ preferredLayoutSize.height - 1;
}
return preferredLayoutSize;
}
@Override
public Dimension minimumLayoutSize(Container parent) {
Dimension minimumLayoutSize = new Dimension(0, 1);
for (Component component : parent.getComponents()) {
Dimension minimumSize = component.getMinimumSize();
minimumLayoutSize.width = max(minimumSize.width,
minimumLayoutSize.width);
minimumLayoutSize.height = minimumSize.height
+ minimumLayoutSize.height - 1;
}
return minimumLayoutSize;
}
@Override
public void layoutContainer(Container parent) {
int y = 0;
for (Component component : parent.getComponents()) {
component.setLocation(0, y);
component.setSize(parent.getSize().width,
component.getPreferredSize().height);
y += component.getHeight() - 1;
}
}
}
}