1 22 23 package org.gjt.sp.jedit.pluginmgr; 24 25 import javax.swing.border.*; 27 import javax.swing.event.*; 28 import javax.swing.*; 29 import java.awt.event.*; 30 import java.awt.*; 31 import org.xml.sax.SAXParseException ; 32 import org.gjt.sp.jedit.io.VFSManager; 33 import org.gjt.sp.jedit.msg.*; 34 import org.gjt.sp.jedit.options.*; 35 import org.gjt.sp.jedit.*; 36 import org.gjt.sp.util.Log; 37 import org.gjt.sp.util.WorkRequest; 38 40 43 public class PluginManager extends JFrame implements EBComponent 44 { 45 50 public static PluginManager getInstance() 51 { 52 return instance; 53 } 55 public void dispose() 57 { 58 instance = null; 59 EditBus.removeFromBus(this); 60 super.dispose(); 61 } 63 public void handleMessage(EBMessage message) 65 { 66 if (message instanceof PropertiesChanged) 67 { 68 if (shouldUpdatePluginList()) 69 { 70 pluginList = null; 71 updatePluginList(); 72 if(tabPane.getSelectedIndex() != 0) 73 { 74 installer.updateModel(); 75 updater.updateModel(); 76 } 77 } 78 } 79 else if (message instanceof PluginUpdate) 80 { 81 if(!queuedUpdate) 82 { 83 SwingUtilities.invokeLater(new Runnable () 84 { 85 public void run() 86 { 87 queuedUpdate = false; 88 manager.update(); 89 } 90 }); 91 queuedUpdate = true; 92 } 93 } 94 } 96 public static void showPluginManager(Frame parent) 98 { 99 if (instance == null) 100 instance = new PluginManager(parent); 101 else 102 { 103 instance.toFront(); 104 return; 105 } 106 } 108 public void ok() 110 { 111 dispose(); 112 } 114 public void cancel() 116 { 117 dispose(); 118 } 120 PluginList getPluginList() 122 { 123 return pluginList; 124 } 126 private static PluginManager instance; 128 129 private JTabbedPane tabPane; 131 private JButton done; 132 private JButton mgrOptions; 133 private JButton pluginOptions; 134 private InstallPanel installer; 135 private InstallPanel updater; 136 private ManagePanel manager; 137 private PluginList pluginList; 138 private boolean queuedUpdate; 139 private boolean downloadingPluginList; 140 private Frame parent = null; 141 143 145 private PluginManager(Frame parent) 146 { 147 super(jEdit.getProperty("plugin-manager.title")); 148 this.parent = parent; 149 init(); 150 } 151 152 private PluginManager() 153 { 154 super(jEdit.getProperty("plugin-manager.title")); 155 init(); 156 } 157 158 private void init() { 159 EditBus.addToBus(this); 160 161 162 JPanel content = new JPanel(new BorderLayout(12,12)); 163 content.setBorder(new EmptyBorder(12,12,12,12)); 164 setContentPane(content); 165 166 tabPane = new JTabbedPane(); 167 tabPane.addTab(jEdit.getProperty("manage-plugins.title"), 168 manager = new ManagePanel(this)); 169 tabPane.addTab(jEdit.getProperty("update-plugins.title"), 170 updater = new InstallPanel(this,true)); 171 tabPane.addTab(jEdit.getProperty("install-plugins.title"), 172 installer = new InstallPanel(this,false)); 173 174 content.add(BorderLayout.CENTER,tabPane); 175 176 tabPane.addChangeListener(new ListUpdater()); 177 178 179 Box buttons = new Box(BoxLayout.X_AXIS); 180 181 ActionListener al = new ActionHandler(); 182 mgrOptions = new JButton(jEdit.getProperty("plugin-manager.mgr-options")); 183 mgrOptions.addActionListener(al); 184 pluginOptions = new JButton(jEdit.getProperty("plugin-manager.plugin-options")); 185 pluginOptions.addActionListener(al); 186 done = new JButton(jEdit.getProperty("plugin-manager.done")); 187 done.addActionListener(al); 188 189 buttons.add(Box.createGlue()); 190 buttons.add(mgrOptions); 191 buttons.add(Box.createHorizontalStrut(6)); 192 buttons.add(pluginOptions); 193 buttons.add(Box.createHorizontalStrut(6)); 194 buttons.add(done); 195 buttons.add(Box.createGlue()); 196 197 getRootPane().setDefaultButton(done); 198 199 content.add(BorderLayout.SOUTH,buttons); 200 201 setDefaultCloseOperation(DISPOSE_ON_CLOSE); 202 203 setIconImage(GUIUtilities.getPluginIcon()); 204 205 pack(); 206 GUIUtilities.loadGeometry(this, parent, "plugin-manager"); 207 GUIUtilities.addSizeSaver(this, parent, "plugin-manager"); 208 setVisible(true); 209 } 211 220 private boolean shouldUpdatePluginList() 221 { 222 return (pluginList == null || 223 !pluginList.getMirrorId().equals(jEdit.getProperty("plugin-manager.mirror.id"))) && 224 !downloadingPluginList; 225 } 227 private void updatePluginList() 229 { 230 if(jEdit.getSettingsDirectory() == null 231 && jEdit.getJEditHome() == null) 232 { 233 GUIUtilities.error(this,"no-settings",null); 234 return; 235 } 236 if (!shouldUpdatePluginList()) 237 { 238 return; 239 } 240 241 final Exception [] exception = new Exception [1]; 242 243 VFSManager.runInWorkThread(new WorkRequest() 244 { 245 public void run() 246 { 247 try 248 { 249 downloadingPluginList = true; 250 setStatus(jEdit.getProperty( 251 "plugin-manager.list-download-connect")); 252 pluginList = new PluginList(this); 253 } 254 catch(Exception e) 255 { 256 exception[0] = e; 257 } 258 finally 259 { 260 downloadingPluginList = false; 261 } 262 } 263 }); 264 265 VFSManager.runInAWTThread(new Runnable () 266 { 267 public void run() 268 { 269 if(exception[0] instanceof SAXParseException ) 270 { 271 SAXParseException se = (SAXParseException ) 272 exception[0]; 273 274 int line = se.getLineNumber(); 275 String path = jEdit.getProperty( 276 "plugin-manager.export-url"); 277 String message = se.getMessage(); 278 Log.log(Log.ERROR,this,path + ":" + line 279 + ": " + message); 280 String [] pp = { path, 281 String.valueOf(line), 282 message }; 283 GUIUtilities.error(PluginManager.this, 284 "plugin-list.xmlerror",pp); 285 } 286 else if(exception[0] != null) 287 { 288 Exception e = exception[0]; 289 290 Log.log(Log.ERROR,this,e); 291 String [] pp = { e.toString() }; 292 293 String ok = jEdit.getProperty( 294 "common.ok"); 295 String proxyButton = jEdit.getProperty( 296 "plugin-list.ioerror.proxy-servers"); 297 int retVal = 298 JOptionPane.showOptionDialog( 299 PluginManager.this, 300 jEdit.getProperty("plugin-list.ioerror.message",pp), 301 jEdit.getProperty("plugin-list.ioerror.title"), 302 JOptionPane.YES_NO_OPTION, 303 JOptionPane.ERROR_MESSAGE, 304 null, 305 new Object [] { 306 proxyButton, 307 ok 308 }, 309 ok); 310 311 if(retVal == 0) 312 { 313 new GlobalOptions( 314 PluginManager.this, 315 "firewall"); 316 } 317 } 318 } 319 }); 320 } 322 public void processKeyEvents(KeyEvent ke) 324 { 325 if ((KeyEvent.KEY_PRESSED == ke.getID()) && 326 (KeyEvent.VK_ESCAPE == ke.getKeyCode())) 327 { 328 cancel(); 329 ke.consume(); 330 } 331 } 333 335 337 class ActionHandler implements ActionListener 339 { 340 public void actionPerformed(ActionEvent evt) 341 { 342 Object source = evt.getSource(); 343 if(source == done) 344 ok(); 345 else if (source == mgrOptions) 346 new GlobalOptions(PluginManager.this,"plugin-manager"); 347 else if (source == pluginOptions) 348 new PluginOptions(PluginManager.this); 349 } 350 } 352 class ListUpdater implements ChangeListener 354 { 355 public void stateChanged(ChangeEvent e) 356 { 357 final Component selected = tabPane.getSelectedComponent(); 358 if(selected == installer || selected == updater) 359 { 360 updatePluginList(); 361 installer.updateModel(); 362 updater.updateModel(); 363 } 364 else if(selected == manager) 365 manager.update(); 366 } 367 } 369 } 371 | Popular Tags |