PluginPanel.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.gui.plugin |
![]() |
![]() |
XNap 3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU 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 USA
*/
package org.xnap.gui.plugin;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import org.xnap.XNap;
import org.xnap.gui.component.ProgressDialog;
import org.xnap.gui.component.XNapButton;
import org.xnap.gui.event.DoubleClickListener;
import org.xnap.gui.util.GUIHelper;
import org.xnap.gui.util.GridBagHelper;
import org.xnap.pkg.PackageInfo;
import org.xnap.pkg.XNapPackageManager;
import org.xnap.plugin.PluginInfo;
import org.xnap.plugin.PluginManager;
public class PluginPanel extends JPanel
implements ActionListener, TreeSelectionListener {
//--- Constant(s) ---
//--- Data field(s) ---
private PluginTree pluginTree;
private JCheckBox installCheckBox;
private JCheckBox updateCheckBox;
private JCheckBox removeCheckBox;
private JLabel authorsLabel;
private JLabel descriptionLabel;
private JLabel longDescriptionLabel;
private JLabel nameLabel;
private JPanel infoPanel;
private JLabel statusLabel;
private PluginNode node;
private JPanel buttonsPanel;
private Action acDisable = new DisablePluginAction();
private Action acEnable = new EnablePluginAction();
private Action detailsAction = new DetailsAction();
//--- Constructor(s) ---
public PluginPanel(boolean showButtons)
{
// plugin list
pluginTree = new PluginTree();
pluginTree.setCellRenderer(new PluginTreeCellRenderer());
pluginTree.addTreeSelectionListener(this);
DoubleClickListener.install(pluginTree, new DoubleClickAction());
// info panel
infoPanel = new JPanel();
infoPanel.setLayout(new GridBagLayout());
nameLabel = GridBagHelper.addLabel(infoPanel, "", true);
nameLabel.setFont(nameLabel.getFont().deriveFont(Font.BOLD));
descriptionLabel = new JLabel();
GridBagHelper.add(infoPanel, descriptionLabel);
longDescriptionLabel = new JLabel();
GridBagHelper.add(infoPanel, longDescriptionLabel);
GridBagHelper.addVerticalSpacer(infoPanel);
// info panel actions
JPanel actionsPanel = new JPanel(new GridBagLayout());
actionsPanel.setBorder
(GUIHelper.createDefaultBorder(XNap.tr("Action")));
GridBagHelper.add(infoPanel, actionsPanel);
installCheckBox = new JCheckBox(XNap.tr("Install"));
installCheckBox.addActionListener(this);
GridBagHelper.add(actionsPanel, installCheckBox);
updateCheckBox = new JCheckBox(XNap.tr("Update"));
updateCheckBox.addActionListener(this);
GridBagHelper.add(actionsPanel, updateCheckBox);
removeCheckBox = new JCheckBox(XNap.tr("Remove"));
// FIX: GridBagHelper.add(actionsPanel, removeCheckBox);
// info panel buttons
buttonsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
GridBagHelper.add(infoPanel, buttonsPanel);
buttonsPanel.add(new XNapButton(detailsAction));
if (showButtons) {
buttonsPanel.add(new XNapButton(acEnable));
buttonsPanel.add(new XNapButton(acDisable));
}
// status
JLabel statusLabel = new JLabel("");
// splitter
JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
jsp.setDividerSize(2);
jsp.setDividerLocation(200);
jsp.setBorder(GUIHelper.createEmptyBorder());
jsp.add(new JScrollPane(pluginTree,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER),
JSplitPane.LEFT);
jsp.add(new JScrollPane(infoPanel), JSplitPane.RIGHT);
// content
setLayout(new BorderLayout());
add(jsp, BorderLayout.CENTER);
add(statusLabel, BorderLayout.SOUTH);
// initialize
valueChanged();
}
//--- Method(s) ---
/**
* Invoked when one of the action buttons was selected.
*/
public void actionPerformed(ActionEvent event)
{
if (installCheckBox.isSelected() || updateCheckBox.isSelected()) {
node.setAction("install");
}
else {
node.setAction("purge");
}
}
/**
* Sets the selected actions for all packages.
*/
public void apply()
{
Enumeration it = pluginTree.getRoot().depthFirstEnumeration();
while (it.hasMoreElements()) {
Object o = it.nextElement();
if (o instanceof PluginNode) {
PluginNode node = (PluginNode)o;
node.getPackageInfo().setAction(node.getAction());
}
}
}
public JPanel getButtonPanel()
{
return buttonsPanel;
}
public PluginTree getPluginTree()
{
return pluginTree;
}
/**
* Returns the currently selected plugin or null if no plugin is selected.
*/
public PluginInfo getSelectedPlugin()
{
PluginNode node = pluginTree.getSelectedNode();
return (node != null) ? node.getPluginInfo() : null;
}
/**
* Updates the info panel.
*/
public void setNode(PluginNode node)
{
this.node = node;
PackageInfo info = node.getPackageInfo();
nameLabel.setText(info.getName() + " " + info.getVersion());
descriptionLabel.setText(info.getDescription());
longDescriptionLabel.setText(GUIHelper.tt(info.getLongDescription()));
installCheckBox.setEnabled(!info.isInstalled());
// node.isUpdateAvailable() implies info.isInstalled()
updateCheckBox.setEnabled(node.isUpdateAvailable());
installCheckBox.setSelected(!info.isInstalled()
&& "install".equals(node.getAction()));
updateCheckBox.setSelected(node.isUpdateAvailable()
&& "install".equals(node.getAction()));
PluginInfo pluginInfo = node.getPluginInfo();
if (pluginInfo != null && pluginInfo.isPlugin()) {
acEnable.setEnabled(!pluginInfo.isEnabled());
acDisable.setEnabled(pluginInfo.isEnabled());
}
else {
acEnable.setEnabled(false);
acDisable.setEnabled(false);
}
}
public void setStatus(String status)
{
statusLabel.setText(status);
}
/**
* Updates the plugin tree.
*/
public void update()
{
PluginManager.getInstance().updateFromPackageManager();
pluginTree.update();
}
/**
* Updates the list of packages and invokes {@link #update()}.
*
* @see XNapPackageManager.update(ProgressMonitor)
*/
public void updatePackageManager()
{
ProgressDialog dialog
= new ProgressDialog(XNap.tr("Updating packages"));
XNapPackageManager.getInstance().update(dialog);
dialog.show(this);
update();
XNapPackageManager.getInstance().write();
}
/**
* Updates the plugin info panel.
*/
public void valueChanged(TreeSelectionEvent e)
{
PluginNode node = pluginTree.getSelectedNode();
if (node != null) {
setNode(node);
}
infoPanel.setVisible(node != null);
}
public void valueChanged()
{
valueChanged(null);
}
// --- Inner Class(es) ---
private class DoubleClickAction implements ActionListener {
public void actionPerformed(ActionEvent event)
{
if (acEnable.isEnabled()) {
acEnable.actionPerformed(event);
}
else if (acDisable.isEnabled()) {
acDisable.actionPerformed(event);
}
}
}
private class DetailsAction extends AbstractAction
{
public DetailsAction()
{
putValue(Action.NAME, XNap.tr("Details"));
putValue(Action.SHORT_DESCRIPTION,
XNap.tr("Shows a dialog with detailed information about the plugin."));
}
//--- Method(s) ---
public void actionPerformed(ActionEvent event)
{
PluginInfo info = getSelectedPlugin();
if (info != null) {
PluginDetailsDialog dialog = new PluginDetailsDialog(info);
dialog.setLocationRelativeTo(PluginPanel.this);
dialog.setVisible(true);
}
}
}
private class EnablePluginAction extends AbstractAction
{
public EnablePluginAction()
{
putValue(Action.NAME, XNap.tr("Enable"));
putValue(Action.SHORT_DESCRIPTION,
XNap.tr("Enables the plugin."));
}
//--- Method(s) ---
public void actionPerformed(ActionEvent event)
{
PluginInfo info = getSelectedPlugin();
if (info != null) {
if (PluginHelper.enablePlugin(PluginPanel.this, info)) {
valueChanged();
pluginTree.selectedNodeChanged();
}
}
}
}
private class DisablePluginAction extends AbstractAction
{
public DisablePluginAction()
{
putValue(Action.NAME, XNap.tr("Disable"));
putValue(Action.SHORT_DESCRIPTION,
XNap.tr("Disables the plugin."));
}
//--- Method(s) ---
public void actionPerformed(ActionEvent event)
{
PluginInfo info = getSelectedPlugin();
if (info != null) {
if (PluginHelper.disablePlugin(PluginPanel.this, info)) {
valueChanged();
pluginTree.selectedNodeChanged();
}
}
}
}
}
The table below shows all metrics for PluginPanel.java.




