KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > pluginmgr > PluginManager


1 /*
2  * PluginManager.java - Plugin manager window
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2002 Kris Kopicki
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit.pluginmgr;
24
25 //{{{ Imports
26
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 JavaDoc;
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 //}}}
39

40 /**
41  * @version $Id: PluginManager.java 8302 2007-01-02 21:37:07Z kpouer $
42  */

43 public class PluginManager extends JFrame implements EBComponent
44 {
45     //{{{ getInstance() method
46
/**
47      * Returns the currently visible plugin manager window, or null.
48      * @since jEdit 4.2pre2
49      */

50     public static PluginManager getInstance()
51     {
52         return instance;
53     } //}}}
54

55     //{{{ dispose() method
56
public void dispose()
57     {
58         instance = null;
59         EditBus.removeFromBus(this);
60         super.dispose();
61     } //}}}
62

63     //{{{ handleMessage() method
64
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 JavaDoc()
84                 {
85                     public void run()
86                     {
87                         queuedUpdate = false;
88                         manager.update();
89                     }
90                 });
91                 queuedUpdate = true;
92             }
93         }
94     } //}}}
95

96     //{{{ showPluginManager() method
97
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     } //}}}
107

108     //{{{ ok() method
109
public void ok()
110     {
111         dispose();
112     } //}}}
113

114     //{{{ cancel() method
115
public void cancel()
116     {
117         dispose();
118     } //}}}
119

120     //{{{ getPluginList() method
121
PluginList getPluginList()
122     {
123         return pluginList;
124     } //}}}
125

126     //{{{ Private members
127
private static PluginManager instance;
128
129     //{{{ Instance variables
130
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     //}}}
142

143     //{{{ PluginManager constructor
144

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         /* Setup panes */
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         /* Create the buttons */
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     } //}}}
210

211     //{{{ shouldUpdatePluginList()
212
/**
213     * Check if the plugin list should be updated.
214     * It will return <code>true</code> if the pluginList is <code>null</code>
215     * or if the mirror id of the current plugin list is not the current preffered mirror id
216     * and will return always false if the plugin list is currently downloading
217     *
218     * @return true if the plugin list should be updated
219     */

220     private boolean shouldUpdatePluginList()
221     {
222         return (pluginList == null ||
223             !pluginList.getMirrorId().equals(jEdit.getProperty("plugin-manager.mirror.id"))) &&
224             !downloadingPluginList;
225     } //}}}
226

227     //{{{ updatePluginList() method
228
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 JavaDoc[] exception = new Exception JavaDoc[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 JavaDoc e)
255                 {
256                     exception[0] = e;
257                 }
258                 finally
259                 {
260                     downloadingPluginList = false;
261                 }
262             }
263         });
264
265         VFSManager.runInAWTThread(new Runnable JavaDoc()
266         {
267             public void run()
268             {
269                 if(exception[0] instanceof SAXParseException JavaDoc)
270                 {
271                     SAXParseException JavaDoc se = (SAXParseException JavaDoc)
272                         exception[0];
273
274                     int line = se.getLineNumber();
275                     String JavaDoc path = jEdit.getProperty(
276                         "plugin-manager.export-url");
277                     String JavaDoc message = se.getMessage();
278                     Log.log(Log.ERROR,this,path + ":" + line
279                         + ": " + message);
280                     String JavaDoc[] 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 JavaDoc e = exception[0];
289
290                     Log.log(Log.ERROR,this,e);
291                     String JavaDoc[] pp = { e.toString() };
292
293                     String JavaDoc ok = jEdit.getProperty(
294                         "common.ok");
295                     String JavaDoc 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 JavaDoc[] {
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     } //}}}
321

322     //{{{ processKeyEvent() method
323
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     } //}}}
332

333     //}}}
334

335     //{{{ Inner classes
336

337     //{{{ ActionHandler class
338
class ActionHandler implements ActionListener
339     {
340         public void actionPerformed(ActionEvent evt)
341         {
342             Object JavaDoc 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     } //}}}
351

352     //{{{ ListUpdater class
353
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     } //}}}
368

369     //}}}
370
}
371
Popular Tags