1 17 package org.columba.core.gui.plugin; 18 19 import java.awt.BorderLayout ; 20 import java.awt.Color ; 21 import java.awt.Component ; 22 import java.awt.Dimension ; 23 import java.awt.GridBagConstraints ; 24 import java.awt.GridBagLayout ; 25 import java.awt.GridLayout ; 26 import java.awt.event.ActionEvent ; 27 import java.awt.event.ActionListener ; 28 import java.awt.event.KeyEvent ; 29 import java.io.File ; 30 import java.io.IOException ; 31 import java.net.URL ; 32 import java.util.logging.Logger ; 33 34 import javax.swing.BorderFactory ; 35 import javax.swing.Box ; 36 import javax.swing.BoxLayout ; 37 import javax.swing.JButton ; 38 import javax.swing.JComponent ; 39 import javax.swing.JDialog ; 40 import javax.swing.JFileChooser ; 41 import javax.swing.JFrame ; 42 import javax.swing.JLabel ; 43 import javax.swing.JOptionPane ; 44 import javax.swing.JPanel ; 45 import javax.swing.JScrollPane ; 46 import javax.swing.KeyStroke ; 47 import javax.swing.SwingConstants ; 48 import javax.swing.event.TreeSelectionEvent ; 49 import javax.swing.event.TreeSelectionListener ; 50 import javax.swing.filechooser.FileFilter ; 51 52 import org.columba.api.plugin.IExtensionHandler; 53 import org.columba.api.plugin.IExtensionHandlerKeys; 54 import org.columba.api.plugin.PluginHandlerNotFoundException; 55 import org.columba.api.plugin.PluginLoadingFailedException; 56 import org.columba.api.plugin.PluginMetadata; 57 import org.columba.core.config.DefaultConfigDirectory; 58 import org.columba.core.gui.base.ButtonWithMnemonic; 59 import org.columba.core.gui.base.InfoViewerDialog; 60 import org.columba.core.gui.base.SingleSideEtchedBorder; 61 import org.columba.core.help.HelpManager; 62 import org.columba.core.io.DirectoryIO; 63 import org.columba.core.io.ZipFileIO; 64 import org.columba.core.plugin.PluginManager; 65 import org.columba.core.resourceloader.GlobalResourceLoader; 66 67 75 public class PluginManagerDialog extends JDialog implements ActionListener , 76 TreeSelectionListener { 77 private static final String RESOURCE_PATH = "org.columba.core.i18n.dialog"; 78 79 private static final Logger LOG = Logger 80 .getLogger("org.columba.core.gui.plugin"); 81 82 protected JButton installButton; 83 84 protected JButton removeButton; 85 86 protected JButton optionsButton; 87 88 protected JButton infoButton; 89 90 protected JButton helpButton; 91 92 protected JButton closeButton; 93 94 protected PluginTree table; 95 96 protected IExtensionHandler configHandler; 97 98 protected PluginNode selectedNode; 99 100 private static PluginManagerDialog instance; 101 102 private PluginManagerDialog() { 103 super((JFrame ) null, GlobalResourceLoader.getString(RESOURCE_PATH, 105 "pluginmanager", "title"), true); 106 107 try { 108 configHandler = PluginManager.getInstance() 109 .getExtensionHandler(IExtensionHandlerKeys.ORG_COLUMBA_CORE_CONFIG); 110 } catch (Exception ex) { 111 ex.printStackTrace(); 112 } 113 114 initComponents(); 115 pack(); 116 setLocationRelativeTo(null); 117 setVisible(true); 118 } 119 120 public static PluginManagerDialog getInstance() { 121 if ( instance == null) { 122 instance = new PluginManagerDialog(); 123 } 124 125 return instance; 126 } 127 128 protected void initComponents() { 129 JPanel mainPanel = new JPanel (); 130 mainPanel.setLayout(new BorderLayout ()); 131 mainPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12)); 132 getContentPane().add(mainPanel); 133 134 installButton = new ButtonWithMnemonic(GlobalResourceLoader.getString( 135 RESOURCE_PATH, "pluginmanager", "install")); 136 installButton.setActionCommand("INSTALL"); 137 installButton.addActionListener(this); 138 139 removeButton = new ButtonWithMnemonic(GlobalResourceLoader.getString( 140 RESOURCE_PATH, "pluginmanager", "remove")); 141 removeButton.setActionCommand("REMOVE"); 142 removeButton.setEnabled(false); 143 removeButton.addActionListener(this); 144 145 optionsButton = new ButtonWithMnemonic(GlobalResourceLoader.getString( 146 RESOURCE_PATH, "pluginmanager", "options")); 147 optionsButton.setActionCommand("OPTIONS"); 148 optionsButton.setEnabled(false); 149 optionsButton.addActionListener(this); 150 151 infoButton = new ButtonWithMnemonic(GlobalResourceLoader.getString( 152 RESOURCE_PATH, "pluginmanager", "info")); 153 infoButton.setActionCommand("INFO"); 154 infoButton.setEnabled(false); 155 infoButton.addActionListener(this); 156 157 JPanel topPanel = new JPanel (); 159 topPanel.setLayout(new BoxLayout (topPanel, BoxLayout.X_AXIS)); 160 161 GridBagLayout gridBagLayout = new GridBagLayout (); 162 GridBagConstraints c = new GridBagConstraints (); 163 164 JPanel topBorderPanel = new JPanel (); 166 topBorderPanel.setLayout(new BorderLayout ()); 167 168 topBorderPanel.add(topPanel); 171 172 JLabel nameLabel = new JLabel ("name"); 174 nameLabel.setEnabled(false); 175 topPanel.add(nameLabel); 176 177 topPanel.add(Box.createRigidArea(new Dimension (10, 0))); 178 topPanel.add(Box.createHorizontalGlue()); 179 180 Component glue = Box.createVerticalGlue(); 181 c.anchor = GridBagConstraints.EAST; 182 c.gridwidth = GridBagConstraints.REMAINDER; 183 184 gridBagLayout.setConstraints(glue, c); 186 187 gridBagLayout = new GridBagLayout (); 188 c = new GridBagConstraints (); 189 190 JPanel eastPanel = new JPanel (gridBagLayout); 191 mainPanel.add(eastPanel, BorderLayout.EAST); 192 193 c.fill = GridBagConstraints.HORIZONTAL; 194 c.weightx = 1.0; 195 c.gridwidth = GridBagConstraints.REMAINDER; 196 gridBagLayout.setConstraints(installButton, c); 197 eastPanel.add(installButton); 198 199 Component strut1 = Box.createRigidArea(new Dimension (30, 5)); 200 gridBagLayout.setConstraints(strut1, c); 201 eastPanel.add(strut1); 202 203 gridBagLayout.setConstraints(removeButton, c); 204 eastPanel.add(removeButton); 205 206 Component strut = Box.createRigidArea(new Dimension (30, 5)); 207 gridBagLayout.setConstraints(strut, c); 208 eastPanel.add(strut); 209 210 gridBagLayout.setConstraints(optionsButton, c); 211 eastPanel.add(optionsButton); 212 213 Component strut3 = Box.createRigidArea(new Dimension (30, 5)); 214 gridBagLayout.setConstraints(strut3, c); 215 eastPanel.add(strut3); 216 217 gridBagLayout.setConstraints(infoButton, c); 218 eastPanel.add(infoButton); 219 220 strut = Box.createRigidArea(new Dimension (30, 20)); 221 gridBagLayout.setConstraints(strut, c); 222 eastPanel.add(strut); 223 224 glue = Box.createVerticalGlue(); 225 c.fill = GridBagConstraints.BOTH; 226 c.weighty = 1.0; 227 gridBagLayout.setConstraints(glue, c); 228 eastPanel.add(glue); 229 230 JPanel centerPanel = new JPanel (new BorderLayout ()); 232 centerPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5)); 233 234 242 table = new PluginTree(); 243 table.getTree().addTreeSelectionListener(this); 244 245 JScrollPane scrollPane = new JScrollPane (table); 246 scrollPane.setPreferredSize(new Dimension (350, 300)); 247 scrollPane.getViewport().setBackground(Color.white); 248 centerPanel.add(scrollPane); 249 250 mainPanel.add(centerPanel); 251 252 JPanel bottomPanel = new JPanel (new BorderLayout ()); 253 bottomPanel.setBorder(new SingleSideEtchedBorder(SwingConstants.TOP)); 254 255 JPanel buttonPanel = new JPanel (new GridLayout (1, 3, 6, 0)); 256 buttonPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12)); 257 258 JButton closeButton = new ButtonWithMnemonic(GlobalResourceLoader 259 .getString("global", "global", "close")); 260 closeButton.setActionCommand("CLOSE"); 261 closeButton.addActionListener(this); 262 buttonPanel.add(closeButton); 263 264 ButtonWithMnemonic helpButton = new ButtonWithMnemonic( 265 GlobalResourceLoader.getString("global", "global", "help")); 266 buttonPanel.add(helpButton); 267 bottomPanel.add(buttonPanel, BorderLayout.EAST); 268 getContentPane().add(bottomPanel, BorderLayout.SOUTH); 269 getRootPane().setDefaultButton(closeButton); 270 getRootPane().registerKeyboardAction(this, "CLOSE", 271 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), 272 JComponent.WHEN_IN_FOCUSED_WINDOW); 273 274 HelpManager.getInstance().enableHelpOnButton(helpButton, 276 "extending_columba_1"); 277 HelpManager.getInstance().enableHelpKey(getRootPane(), 278 "extending_columba_1"); 279 } 280 281 public void actionPerformed(ActionEvent e) { 282 String action = e.getActionCommand(); 283 284 if (action.equals("CLOSE")) { 285 setVisible(false); 286 } else if (action.equals("INFO")) { 287 String id = selectedNode.getId(); 288 289 URL url = PluginManager.getInstance().getInfoURL(id); 290 if (url != null) { 291 try { 292 new InfoViewerDialog(url); 293 } catch (IOException ioe) { 294 } 295 } 296 } else if (action.equals("OPTIONS")) { 297 String id = selectedNode.getId(); 298 id = id.substring(id.lastIndexOf(".") + 1, id.length()); 299 300 try { 301 ConfigurationDialog dialog = new ConfigurationDialog(id); 302 dialog.setVisible(true); 303 } catch (PluginHandlerNotFoundException phnfe) { 304 } catch (PluginLoadingFailedException plfe) { 305 } 306 } else if (action.equals("REMOVE")) { 307 File directory = PluginManager.getInstance().getPluginMetadata( 309 selectedNode.getId()).getDirectory(); 310 311 DirectoryIO.delete(directory); 313 314 table.removePluginNode(selectedNode); 316 } else if (action.equals("INSTALL")) { 317 JFileChooser chooser = new JFileChooser (); 318 chooser.addChoosableFileFilter(new FileFilter () { 319 public boolean accept(File file) { 320 return file.isDirectory() 321 || file.getName().toLowerCase().endsWith(".zip"); 322 } 323 324 public String getDescription() { 325 return GlobalResourceLoader.getString(RESOURCE_PATH, 326 "pluginmanager", "filefilter"); 327 } 328 }); 329 chooser.setAcceptAllFileFilterUsed(false); 330 331 int result = chooser.showOpenDialog(this); 332 333 if (result == JFileChooser.APPROVE_OPTION) { 334 File file = chooser.getSelectedFile(); 335 336 installPlugin(file); 337 } 338 } 339 } 340 341 public void valueChanged(TreeSelectionEvent arg0) { 342 selectedNode = (PluginNode) arg0.getPath().getLastPathComponent(); 343 344 if (selectedNode == null) { 345 return; 346 } 347 348 boolean isCategoryFolder = selectedNode.isCategory(); 349 350 if (isCategoryFolder) { 351 removeButton.setEnabled(false); 354 infoButton.setEnabled(false); 355 356 optionsButton.setEnabled(false); 357 } else { 358 removeButton.setEnabled(true); 359 infoButton.setEnabled(selectedNode.hasInfo()); 360 361 if (selectedNode == null) { 362 return; 363 } 364 365 String id = selectedNode.getId(); 367 id = id.substring(id.lastIndexOf(".") + 1, id.length()); 368 optionsButton.setEnabled(configHandler.exists(id)); 369 } 370 } 371 372 375 public PluginNode getSelectedNode() { 376 return selectedNode; 377 } 378 379 382 protected void installPlugin(File file) { 383 File destination = new File (DefaultConfigDirectory.getInstance().getCurrentPath(), 386 "plugins"); 387 388 LOG.info("extract "+file.getName()+" to "+ destination.getAbsolutePath()); 389 390 File pluginDirectory; 391 try { 392 ZipFileIO.extract(file, destination); 394 395 pluginDirectory = ZipFileIO.getFirstFile(file); 396 } catch (IOException ioe) { 397 JOptionPane.showMessageDialog(this, GlobalResourceLoader.getString( 398 RESOURCE_PATH, "pluginmanager", "errExtract.msg"), 399 GlobalResourceLoader.getString(RESOURCE_PATH, 400 "pluginmanager", "errExtract.title"), 401 JOptionPane.ERROR_MESSAGE); 402 return; 403 } 404 405 if (pluginDirectory != null) { 406 pluginDirectory = new File (destination, pluginDirectory.getName()); 408 LOG.info("plugin directory="+pluginDirectory.getAbsolutePath()); 409 410 pluginDirectory = new File (pluginDirectory, "plugin.xml"); 413 414 String id = PluginManager.getInstance().addPlugin(pluginDirectory); 415 PluginMetadata metadata = PluginManager.getInstance() 416 .getPluginMetadata(id); 417 418 table.addPlugin(metadata); 419 420 JOptionPane.showMessageDialog(this, GlobalResourceLoader.getString( 421 RESOURCE_PATH, "pluginmanager", "installSuccess.msg"), 422 GlobalResourceLoader.getString(RESOURCE_PATH, 423 "pluginmanager", "installSuccess.title"), 424 JOptionPane.INFORMATION_MESSAGE); 425 } 426 } 427 } 428 | Popular Tags |