KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > options > PluginManagerOptionPane


1 /*
2  * PluginManagerOptionPane.java - Plugin options panel
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2003 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.options;
24
25 import javax.swing.*;
26 import javax.swing.border.*;
27 import java.awt.*;
28 import java.awt.event.*;
29 import java.util.*;
30 import java.util.List JavaDoc;
31 import java.io.*;
32
33 import org.gjt.sp.jedit.*;
34 import org.gjt.sp.jedit.io.VFSManager;
35 import org.gjt.sp.jedit.pluginmgr.*;
36 import org.gjt.sp.util.*;
37
38 /**
39  * The plugin manager option pane.
40  *
41  * @version $Id: PluginManagerOptionPane.java 7062 2006-09-18 09:55:39Z kpouer $
42  */

43 public class PluginManagerOptionPane extends AbstractOptionPane
44 {
45     //{{{ Constructor
46
public PluginManagerOptionPane()
47     {
48         super("plugin-manager");
49     } //}}}
50

51     //{{{ _init() method
52
protected void _init()
53     {
54         setLayout(new BorderLayout());
55
56         JLabel locationLabel = new JLabel(jEdit.getProperty(
57             "options.plugin-manager.location"));
58
59         mirrorLabel = new JLabel();
60         updateMirrorLabel();
61
62         if(jEdit.getSettingsDirectory() != null)
63         {
64             settingsDir = new JRadioButton(jEdit.getProperty(
65                 "options.plugin-manager.settings-dir"));
66             settingsDir.setToolTipText(MiscUtilities.constructPath(
67                 jEdit.getSettingsDirectory(),"jars"));
68         }
69         JRadioButton appDir = new JRadioButton(jEdit.getProperty(
70                 "options.plugin-manager.app-dir"));
71         appDir.setToolTipText(MiscUtilities.constructPath(
72             jEdit.getJEditHome(),"jars"));
73
74         miraList = new JList(miraModel = new MirrorModel());
75         miraList.setSelectionModel(new SingleSelectionModel());
76
77         /* Download mirror */
78         add(BorderLayout.NORTH,mirrorLabel);
79         add(BorderLayout.CENTER,new JScrollPane(miraList));
80
81         JPanel buttonPanel = new JPanel();
82         buttonPanel.setLayout(new BoxLayout(buttonPanel,BoxLayout.Y_AXIS));
83
84         buttonPanel.add(Box.createVerticalStrut(6));
85
86         /* Update mirror list */
87         updateMirrors = new JButton(jEdit.getProperty(
88             "options.plugin-manager.updateMirrors"));
89         updateMirrors.addActionListener(new ActionHandler());
90         updateMirrors.setEnabled(false);
91         VFSManager.runInWorkThread(new UpdateMirrorsThread(false));
92         JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
93         panel.add(updateMirrors);
94         panel.add(updateStatus);
95         panel.setAlignmentX(Component.LEFT_ALIGNMENT);
96         buttonPanel.add(panel);
97
98         buttonPanel.add(Box.createVerticalStrut(6));
99
100         /* Download source */
101         downloadSource = new JCheckBox(jEdit.getProperty(
102             "options.plugin-manager.downloadSource"));
103         downloadSource.setSelected(jEdit.getBooleanProperty("plugin-manager.downloadSource"));
104         downloadSource.setAlignmentX(Component.LEFT_ALIGNMENT);
105         buttonPanel.add(downloadSource);
106
107         buttonPanel.add(Box.createVerticalStrut(6));
108
109         /* Delete downloaded files */
110         deleteDownloads = new JCheckBox(jEdit.getProperty(
111             "options.plugin-manager.deleteDownloads"));
112         deleteDownloads.setSelected(jEdit.getBooleanProperty("plugin-manager.deleteDownloads"));
113         deleteDownloads.setAlignmentX(Component.LEFT_ALIGNMENT);
114         buttonPanel.add(deleteDownloads);
115
116         buttonPanel.add(Box.createVerticalStrut(6));
117
118         /* Install location */
119         ButtonGroup locGrp = new ButtonGroup();
120         if(jEdit.getSettingsDirectory() != null)
121             locGrp.add(settingsDir);
122         locGrp.add(appDir);
123         JPanel locPanel = new JPanel();
124         locPanel.setBorder(new EmptyBorder(3,12,0,0));
125         locPanel.setLayout(new BoxLayout(locPanel,BoxLayout.Y_AXIS));
126         if(jEdit.getSettingsDirectory() != null)
127         {
128             locPanel.add(settingsDir);
129             locPanel.add(Box.createVerticalStrut(3));
130         }
131         locPanel.add(appDir);
132         locationLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
133         locPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
134         buttonPanel.add(locationLabel);
135         buttonPanel.add(locPanel);
136
137         buttonPanel.add(Box.createGlue());
138         add(BorderLayout.SOUTH,buttonPanel);
139
140         if (jEdit.getBooleanProperty("plugin-manager.installUser")
141             && jEdit.getSettingsDirectory() != null)
142             settingsDir.setSelected(true);
143         else
144             appDir.setSelected(true);
145     } //}}}
146

147     //{{{ _save() method
148
protected void _save()
149     {
150         jEdit.setBooleanProperty("plugin-manager.installUser",
151             settingsDir != null && settingsDir.isSelected());
152         jEdit.setBooleanProperty("plugin-manager.downloadSource",downloadSource.isSelected());
153         jEdit.setBooleanProperty("plugin-manager.deleteDownloads",deleteDownloads.isSelected());
154
155         if(miraList.getSelectedIndex() != -1)
156         {
157             String JavaDoc currentMirror = miraModel.getID(miraList.getSelectedIndex());
158             String JavaDoc previousMirror = jEdit.getProperty("plugin-manager.mirror.id");
159
160             if (!previousMirror.equals(currentMirror))
161             {
162                 jEdit.setProperty("plugin-manager.mirror.id",currentMirror);
163                 jEdit.setProperty("plugin-manager.mirror.name",(String JavaDoc) miraModel.getElementAt(miraList.getSelectedIndex()));
164                 updateMirrorLabel();
165                 // Insert code to update the plugin managers list here later
166
}
167         }
168     } //}}}
169

170     //{{{ Private members
171

172     //{{{ Instance variables
173
private JLabel mirrorLabel;
174
175     private JRadioButton settingsDir;
176     private JCheckBox downloadSource;
177     private JCheckBox deleteDownloads;
178
179     private MirrorModel miraModel;
180     private JList miraList;
181     /** The button to update mirror list. */
182     private JButton updateMirrors;
183     /** A label telling if the mirror list is being updated. */
184     private final JLabel updateStatus = new JLabel();
185     //}}}
186

187     //{{{ updateMirrorLabel method
188
private void updateMirrorLabel()
189     {
190         String JavaDoc currentMirror = jEdit.getProperty("plugin-manager.mirror.id");
191         String JavaDoc mirrorName;
192         if (currentMirror.equals(MirrorList.Mirror.NONE))
193         {
194             mirrorName = "Plugin Central default";
195         }
196         else
197         {
198             mirrorName = jEdit.getProperty("plugin-manager.mirror.name");
199             if (mirrorName == null) mirrorName = currentMirror;
200         }
201         mirrorLabel.setText(jEdit.getProperty(
202             "options.plugin-manager.mirror") + ' ' + mirrorName);
203     } //}}}
204

205     //}}}
206

207     //{{{ MirrorModel class
208
static class MirrorModel extends AbstractListModel
209     {
210         private List JavaDoc<MirrorList.Mirror> mirrors;
211
212         MirrorModel()
213         {
214             mirrors = new ArrayList<MirrorList.Mirror>();
215         }
216
217         public String JavaDoc getID(int index)
218         {
219             return mirrors.get(index).id;
220         }
221
222         public int getSize()
223         {
224             return mirrors.size();
225         }
226
227         public Object JavaDoc getElementAt(int index)
228         {
229             MirrorList.Mirror mirror = mirrors.get(index);
230             if(mirror.id.equals(MirrorList.Mirror.NONE))
231                 return jEdit.getProperty("options.plugin-manager.none");
232             else
233                 return mirror.continent+": "+mirror.description+" ("+mirror.location+')';
234         }
235
236         public void setList(List JavaDoc<MirrorList.Mirror> mirrors)
237         {
238             this.mirrors = mirrors;
239             fireContentsChanged(this,0,mirrors.size() - 1);
240         }
241     } //}}}
242

243     //{{{ SingleSelectionModel class
244
static class SingleSelectionModel extends DefaultListSelectionModel
245     {
246         SingleSelectionModel()
247         {
248             setSelectionMode(SINGLE_SELECTION);
249         }
250
251         public void removeSelectionInterval(int index0, int index1) {}
252     } //}}}
253

254     //{{{ ActionHandler class
255
class ActionHandler implements ActionListener
256     {
257         public void actionPerformed(ActionEvent evt)
258         {
259             updateMirrors.setEnabled(false);
260             updateStatus.setText(jEdit.getProperty("options.plugin-manager.workthread"));
261             VFSManager.runInWorkThread(new UpdateMirrorsThread(true));
262         }
263     } //}}}
264

265     //{{{ UpdateMirrorsThread class
266
/**
267      * The thread that will update the mirror list.
268      * It will read them from the cache or from the web.
269      * It has 4 states :
270      * 0 : started
271      * 1 : xml downloaded
272      * 2 : xml parsed
273      * 3 : list updated
274      */

275     class UpdateMirrorsThread extends WorkRequest
276     {
277         private boolean download;
278
279         UpdateMirrorsThread(boolean download)
280         {
281             this.download = download;
282         }
283
284         //{{{ run() method
285
public void run()
286         {
287             try
288             {
289                 setStatus(jEdit.getProperty("options.plugin-manager.workthread"));
290                 setMaximum(3);
291                 setValue(0);
292
293                 final List JavaDoc<MirrorList.Mirror> mirrors = new ArrayList<MirrorList.Mirror>();
294                 try
295                 {
296                     MirrorList mirrorList = new MirrorList(download, this);
297                     if (download)
298                         saveMirrorList(mirrorList.xml);
299
300                     mirrors.addAll(mirrorList.mirrors);
301                 }
302                 catch (Exception JavaDoc ex)
303                 {
304                     if (download)
305                     {
306                         Log.log(Log.ERROR,this,ex);
307                         GUIUtilities.error(PluginManagerOptionPane.this,
308                                 "ioerror",new String JavaDoc[] { ex.toString() });
309                     }
310                 }
311
312                 SwingUtilities.invokeLater(new Runnable JavaDoc()
313                 {
314                     public void run()
315                     {
316                         miraModel.setList(mirrors);
317
318                         String JavaDoc id = jEdit.getProperty("plugin-manager.mirror.id");
319                         int size = miraModel.getSize();
320                         for (int i=0; i < size; i++)
321                         {
322                             if (size == 1 || miraModel.getID(i).equals(id))
323                             {
324                                 miraList.setSelectedIndex(i);
325                                 break;
326                             }
327                         }
328                     }
329                 });
330
331                 setValue(3);
332             }
333             finally
334             {
335                 updateMirrors.setEnabled(true);
336                 updateStatus.setText(null);
337             }
338         } //}}}
339

340         //{{{ saveMirrorList() method
341
private void saveMirrorList(String JavaDoc xml)
342         {
343             String JavaDoc settingsDirectory = jEdit.getSettingsDirectory();
344             if(settingsDirectory == null)
345                 return;
346
347             File mirrorList = new File(MiscUtilities.constructPath(
348                 settingsDirectory,"mirrorList.xml"));
349             OutputStream out = null;
350
351             try
352             {
353                 out = new BufferedOutputStream(new FileOutputStream(mirrorList));
354                 IOUtilities.copyStream(null, new ByteArrayInputStream(xml.getBytes()), out, false);
355             }
356             catch (IOException e)
357             {
358                 Log.log(Log.ERROR,this, "Unable to write cached mirror list : " + mirrorList);
359             }
360             finally
361             {
362                 IOUtilities.closeQuietly(out);
363             }
364         } //}}}
365
} //}}}
366
}
367
Popular Tags