001: /*
002: * PluginManagerOptionPane.java - Plugin options panel
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2003 Kris Kopicki
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.options;
024:
025: import javax.swing.*;
026: import javax.swing.border.*;
027: import java.awt.*;
028: import java.awt.event.*;
029: import java.util.*;
030: import java.util.List;
031: import java.io.*;
032:
033: import org.gjt.sp.jedit.*;
034: import org.gjt.sp.jedit.io.VFSManager;
035: import org.gjt.sp.jedit.pluginmgr.*;
036: import org.gjt.sp.util.*;
037:
038: /**
039: * The plugin manager option pane.
040: *
041: * @version $Id: PluginManagerOptionPane.java 9942 2007-07-07 23:25:42Z ezust $
042: */
043: public class PluginManagerOptionPane extends AbstractOptionPane {
044: //{{{ Constructor
045: public PluginManagerOptionPane() {
046: super ("plugin-manager");
047: } //}}}
048:
049: //{{{ _init() method
050: protected void _init() {
051: setLayout(new BorderLayout());
052:
053: mirrorLabel = new JLabel();
054: updateMirrorLabel();
055: JPanel buttonPanel = new JPanel();
056: buttonPanel.setLayout(new BoxLayout(buttonPanel,
057: BoxLayout.Y_AXIS));
058: JPanel spinnerPanel = null;
059: if (jEdit.getSettingsDirectory() != null) {
060: settingsDir = new JRadioButton(jEdit
061: .getProperty("options.plugin-manager.settings-dir"));
062: settingsDir.setToolTipText(MiscUtilities.constructPath(
063: jEdit.getSettingsDirectory(), "jars"));
064: int delay = jEdit.getIntegerProperty(
065: "plugin-manager.list-cache.minutes", 10);
066: spinnerModel = new SpinnerNumberModel(delay, 0, 240, 5);
067: cacheForSpinner = new JSpinner(spinnerModel);
068: spinnerPanel = new JPanel();
069: spinnerPanel.setLayout(new BoxLayout(spinnerPanel,
070: BoxLayout.X_AXIS));
071: spinnerPanel.add(new JLabel(
072: "Cache plugin list for: (minutes)"));
073: spinnerPanel.add(cacheForSpinner);
074: spinnerPanel.add(Box.createGlue());
075:
076: }
077: JRadioButton appDir = new JRadioButton(jEdit
078: .getProperty("options.plugin-manager.app-dir"));
079: appDir.setToolTipText(MiscUtilities.constructPath(jEdit
080: .getJEditHome(), "jars"));
081:
082: miraList = new JList(miraModel = new MirrorModel());
083: miraList.setSelectionModel(new SingleSelectionModel());
084:
085: /* Download mirror */
086: add(BorderLayout.NORTH, mirrorLabel);
087: add(BorderLayout.CENTER, new JScrollPane(miraList));
088:
089: buttonPanel.add(Box.createVerticalStrut(6));
090:
091: /* Update mirror list */
092: updateMirrors = new JButton(jEdit
093: .getProperty("options.plugin-manager.updateMirrors"));
094: updateMirrors.addActionListener(new ActionHandler());
095: updateMirrors.setEnabled(false);
096: VFSManager.runInWorkThread(new UpdateMirrorsThread(false));
097: JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
098: panel.add(updateMirrors);
099: if (spinnerPanel != null)
100: panel.add(spinnerPanel);
101: panel.add(updateStatus);
102: panel.setAlignmentX(Component.LEFT_ALIGNMENT);
103: buttonPanel.add(panel);
104:
105: buttonPanel.add(Box.createVerticalStrut(6));
106:
107: /* Download source */
108: downloadSource = new JCheckBox(jEdit
109: .getProperty("options.plugin-manager.downloadSource"));
110: downloadSource.setSelected(jEdit
111: .getBooleanProperty("plugin-manager.downloadSource"));
112: downloadSource.setAlignmentX(Component.LEFT_ALIGNMENT);
113: buttonPanel.add(downloadSource);
114:
115: buttonPanel.add(Box.createVerticalStrut(6));
116:
117: /* Delete downloaded files */
118: deleteDownloads = new JCheckBox(jEdit
119: .getProperty("options.plugin-manager.deleteDownloads"));
120: deleteDownloads.setSelected(jEdit
121: .getBooleanProperty("plugin-manager.deleteDownloads"));
122: deleteDownloads.setAlignmentX(Component.LEFT_ALIGNMENT);
123: buttonPanel.add(deleteDownloads);
124:
125: buttonPanel.add(Box.createVerticalStrut(6));
126:
127: /* Install location */
128: ButtonGroup locGrp = new ButtonGroup();
129: if (jEdit.getSettingsDirectory() != null)
130: locGrp.add(settingsDir);
131: locGrp.add(appDir);
132: JPanel locPanel = new JPanel();
133: locPanel.setLayout(new BoxLayout(locPanel, BoxLayout.Y_AXIS));
134:
135: if (jEdit.getSettingsDirectory() != null) {
136: locPanel.add(settingsDir);
137: locPanel.add(Box.createVerticalStrut(3));
138: }
139: locPanel.setBorder(new TitledBorder(jEdit
140: .getProperty("options.plugin-manager.location")));
141: locPanel.add(appDir);
142: locPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
143: buttonPanel.add(locPanel);
144:
145: buttonPanel.add(Box.createGlue());
146: add(BorderLayout.SOUTH, buttonPanel);
147:
148: if (jEdit.getBooleanProperty("plugin-manager.installUser")
149: && jEdit.getSettingsDirectory() != null)
150: settingsDir.setSelected(true);
151: else
152: appDir.setSelected(true);
153: } //}}}
154:
155: //{{{ _save() method
156: protected void _save() {
157: jEdit.setBooleanProperty("plugin-manager.installUser",
158: settingsDir != null && settingsDir.isSelected());
159: jEdit.setBooleanProperty("plugin-manager.downloadSource",
160: downloadSource.isSelected());
161: jEdit.setBooleanProperty("plugin-manager.deleteDownloads",
162: deleteDownloads.isSelected());
163: jEdit.setIntegerProperty("plugin-manager.list-cache.minutes",
164: spinnerModel.getNumber().intValue());
165: if (miraList.getSelectedIndex() != -1) {
166: String currentMirror = miraModel.getID(miraList
167: .getSelectedIndex());
168: String previousMirror = jEdit
169: .getProperty("plugin-manager.mirror.id");
170:
171: if (!previousMirror.equals(currentMirror)) {
172: jEdit.setProperty("plugin-manager.mirror.id",
173: currentMirror);
174: jEdit.setProperty("plugin-manager.mirror.name",
175: (String) miraModel.getElementAt(miraList
176: .getSelectedIndex()));
177: updateMirrorLabel();
178: // Insert code to update the plugin managers list here later
179: }
180: }
181: } //}}}
182:
183: //{{{ Private members
184:
185: //{{{ Instance variables
186: private JLabel mirrorLabel;
187:
188: private JRadioButton settingsDir;
189: private JCheckBox downloadSource;
190: private JCheckBox deleteDownloads;
191: private JSpinner cacheForSpinner;
192: private SpinnerNumberModel spinnerModel;
193:
194: private MirrorModel miraModel;
195: private JList miraList;
196: /** The button to update mirror list. */
197: private JButton updateMirrors;
198: /** A label telling if the mirror list is being updated. */
199: private final JLabel updateStatus = new JLabel();
200:
201: //}}}
202:
203: //{{{ updateMirrorLabel method
204: private void updateMirrorLabel() {
205: String currentMirror = jEdit
206: .getProperty("plugin-manager.mirror.id");
207: String mirrorName;
208: if (currentMirror.equals(MirrorList.Mirror.NONE)) {
209: mirrorName = "Plugin Central default";
210: } else {
211: mirrorName = jEdit
212: .getProperty("plugin-manager.mirror.name");
213: if (mirrorName == null)
214: mirrorName = currentMirror;
215: }
216: mirrorLabel.setText(jEdit
217: .getProperty("options.plugin-manager.mirror")
218: + ' ' + mirrorName);
219: } //}}}
220:
221: //}}}
222:
223: //{{{ MirrorModel class
224: static class MirrorModel extends AbstractListModel {
225: private List<MirrorList.Mirror> mirrors;
226:
227: MirrorModel() {
228: mirrors = new ArrayList<MirrorList.Mirror>();
229: }
230:
231: public String getID(int index) {
232: return mirrors.get(index).id;
233: }
234:
235: public int getSize() {
236: return mirrors.size();
237: }
238:
239: public Object getElementAt(int index) {
240: MirrorList.Mirror mirror = mirrors.get(index);
241: if (mirror.id.equals(MirrorList.Mirror.NONE))
242: return jEdit.getProperty("options.plugin-manager.none");
243: else
244: return mirror.continent + ": " + mirror.description
245: + " (" + mirror.location + ')';
246: }
247:
248: public void setList(List<MirrorList.Mirror> mirrors) {
249: this .mirrors = mirrors;
250: fireContentsChanged(this , 0, mirrors.size() - 1);
251: }
252: } //}}}
253:
254: //{{{ SingleSelectionModel class
255: static class SingleSelectionModel extends DefaultListSelectionModel {
256: SingleSelectionModel() {
257: setSelectionMode(SINGLE_SELECTION);
258: }
259:
260: public void removeSelectionInterval(int index0, int index1) {
261: }
262: } //}}}
263:
264: //{{{ ActionHandler class
265: class ActionHandler implements ActionListener {
266: public void actionPerformed(ActionEvent evt) {
267: updateMirrors.setEnabled(false);
268: updateStatus.setText(jEdit
269: .getProperty("options.plugin-manager.workthread"));
270: VFSManager.runInWorkThread(new UpdateMirrorsThread(true));
271: }
272: } //}}}
273:
274: //{{{ UpdateMirrorsThread class
275: /**
276: * The thread that will update the mirror list.
277: * It will read them from the cache or from the web.
278: * It has 4 states :
279: * 0 : started
280: * 1 : xml downloaded
281: * 2 : xml parsed
282: * 3 : list updated
283: */
284: class UpdateMirrorsThread extends WorkRequest {
285: private boolean download;
286:
287: UpdateMirrorsThread(boolean download) {
288: this .download = download;
289: }
290:
291: //{{{ run() method
292: public void run() {
293: try {
294: setStatus(jEdit
295: .getProperty("options.plugin-manager.workthread"));
296: setMaximum(3);
297: setValue(0);
298:
299: final List<MirrorList.Mirror> mirrors = new ArrayList<MirrorList.Mirror>();
300: try {
301: MirrorList mirrorList = new MirrorList(download,
302: this );
303: if (download)
304: saveMirrorList(mirrorList.xml);
305:
306: mirrors.addAll(mirrorList.mirrors);
307: } catch (Exception ex) {
308: if (download) {
309: Log.log(Log.ERROR, this , ex);
310: GUIUtilities.error(
311: PluginManagerOptionPane.this ,
312: "ioerror",
313: new String[] { ex.toString() });
314: }
315: }
316:
317: SwingUtilities.invokeLater(new Runnable() {
318: public void run() {
319: miraModel.setList(mirrors);
320:
321: String id = jEdit
322: .getProperty("plugin-manager.mirror.id");
323: int size = miraModel.getSize();
324: for (int i = 0; i < size; i++) {
325: if (size == 1
326: || miraModel.getID(i).equals(id)) {
327: miraList.setSelectedIndex(i);
328: break;
329: }
330: }
331: }
332: });
333:
334: setValue(3);
335: } finally {
336: updateMirrors.setEnabled(true);
337: updateStatus.setText(null);
338: }
339: } //}}}
340:
341: //{{{ saveMirrorList() method
342: private void saveMirrorList(String xml) {
343: String 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: out = new BufferedOutputStream(new FileOutputStream(
353: mirrorList));
354: IOUtilities.copyStream(null, new ByteArrayInputStream(
355: xml.getBytes()), out, false);
356: } catch (IOException e) {
357: Log.log(Log.ERROR, this ,
358: "Unable to write cached mirror list : "
359: + mirrorList);
360: } finally {
361: IOUtilities.closeQuietly(out);
362: }
363: } //}}}
364: } //}}}
365: }
|