PageRenderTime 121ms CodeModel.GetById 90ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/options/PluginManagerOptionPane.java

#
Java | 395 lines | 287 code | 51 blank | 57 comment | 30 complexity | 2eacb24049423f174aec9167d4427315 MD5 | raw file
  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
 23package org.gjt.sp.jedit.options;
 24
 25import javax.swing.*;
 26import javax.swing.border.*;
 27import java.awt.*;
 28import java.awt.event.*;
 29import java.util.*;
 30import java.util.List;
 31import java.io.*;
 32
 33import org.gjt.sp.jedit.*;
 34import org.gjt.sp.jedit.pluginmgr.*;
 35import org.gjt.sp.util.*;
 36
 37/**
 38 * The plugin manager option pane.
 39 * 
 40 * @version $Id: PluginManagerOptionPane.java 17941 2010-06-01 14:22:58Z kpouer $
 41 */
 42public class PluginManagerOptionPane extends AbstractOptionPane
 43{
 44	//{{{ Constructor
 45	public PluginManagerOptionPane()
 46	{
 47		super("plugin-manager");
 48	} //}}}
 49
 50	//{{{ _init() method
 51	@Override
 52	protected void _init()
 53	{
 54		setLayout(new BorderLayout());
 55
 56
 57		mirrorLabel = new JLabel();
 58		updateMirrorLabel();
 59		JPanel buttonPanel = new JPanel();
 60		buttonPanel.setLayout(new BoxLayout(buttonPanel,BoxLayout.Y_AXIS));
 61		JPanel spinnerPanel = null;
 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			int delay = jEdit.getIntegerProperty("plugin-manager.list-cache.minutes", 10);
 69			spinnerModel = new SpinnerNumberModel(delay, 0, 240, 5);
 70			cacheForSpinner = new JSpinner(spinnerModel);
 71			spinnerPanel = new JPanel();
 72			spinnerPanel.setLayout(new BoxLayout(spinnerPanel, BoxLayout.X_AXIS));
 73			spinnerPanel.add(new JLabel("Cache plugin list for: (minutes)"));
 74			spinnerPanel.add(cacheForSpinner);
 75			spinnerPanel.add(Box.createGlue());
 76			
 77		}
 78		JRadioButton appDir = new JRadioButton(jEdit.getProperty(
 79				"options.plugin-manager.app-dir"));
 80		appDir.setToolTipText(MiscUtilities.constructPath(
 81			jEdit.getJEditHome(),"jars"));
 82
 83		miraList = new JList(miraModel = new MirrorModel());
 84		miraList.setSelectionModel(new SingleSelectionModel());
 85
 86		/* Download mirror */
 87		add(BorderLayout.NORTH,mirrorLabel);
 88		add(BorderLayout.CENTER,new JScrollPane(miraList));
 89
 90
 91		buttonPanel.add(Box.createVerticalStrut(6));
 92
 93		/* Update mirror list */
 94		updateMirrors = new JButton(jEdit.getProperty(
 95			"options.plugin-manager.updateMirrors"));
 96		updateMirrors.addActionListener(new ActionHandler());
 97		updateMirrors.setEnabled(false);
 98		ThreadUtilities.runInBackground(new UpdateMirrorsThread(false));
 99		JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
100		panel.add(updateMirrors);
101		if (spinnerPanel != null) panel.add(spinnerPanel);
102		panel.add(updateStatus);
103		panel.setAlignmentX(Component.LEFT_ALIGNMENT);
104		buttonPanel.add(panel);
105
106		buttonPanel.add(Box.createVerticalStrut(6));
107
108		/* Download source */
109		downloadSource = new JCheckBox(jEdit.getProperty(
110			"options.plugin-manager.downloadSource"));
111		downloadSource.setSelected(jEdit.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.getProperty(
119			"options.plugin-manager.deleteDownloads"));
120		deleteDownloads.setSelected(jEdit.getBooleanProperty("plugin-manager.deleteDownloads"));
121		deleteDownloads.setAlignmentX(Component.LEFT_ALIGNMENT);
122		buttonPanel.add(deleteDownloads);
123
124		buttonPanel.add(Box.createVerticalStrut(6));
125
126		/* Install location */
127		ButtonGroup locGrp = new ButtonGroup();
128		if(jEdit.getSettingsDirectory() != null)
129			locGrp.add(settingsDir);
130		locGrp.add(appDir);
131		JPanel locPanel = new JPanel();
132		locPanel.setLayout(new BoxLayout(locPanel,BoxLayout.Y_AXIS));
133		
134		if(jEdit.getSettingsDirectory() != null)
135		{
136			locPanel.add(settingsDir);
137			locPanel.add(Box.createVerticalStrut(3));
138		}
139		locPanel.setBorder(new TitledBorder(
140			jEdit.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	{
158		jEdit.setBooleanProperty("plugin-manager.installUser",
159			settingsDir != null && settingsDir.isSelected());
160		jEdit.setBooleanProperty("plugin-manager.downloadSource",downloadSource.isSelected());
161		jEdit.setBooleanProperty("plugin-manager.deleteDownloads",deleteDownloads.isSelected());
162		jEdit.setIntegerProperty("plugin-manager.list-cache.minutes", spinnerModel.getNumber().intValue());
163		if(miraList.getSelectedIndex() != -1)
164		{
165			String currentMirror = miraModel.getID(miraList.getSelectedIndex());
166			String previousMirror = jEdit.getProperty("plugin-manager.mirror.id");
167
168			if (!previousMirror.equals(currentMirror))
169			{
170				jEdit.setProperty("plugin-manager.mirror.id",currentMirror);
171				jEdit.setProperty("plugin-manager.mirror.name",(String) miraModel.getElementAt(miraList.getSelectedIndex()));
172				updateMirrorLabel();
173				// Insert code to update the plugin managers list here later
174			}
175		}
176	} //}}}
177
178	//{{{ Private members
179
180	//{{{ Instance variables
181	private JLabel mirrorLabel;
182
183	private JRadioButton settingsDir;
184	private JCheckBox downloadSource;
185	private JCheckBox deleteDownloads;
186	private JSpinner cacheForSpinner;
187	private SpinnerNumberModel spinnerModel;
188
189	private MirrorModel miraModel;
190	private JList miraList;
191	/** The button to update mirror list. */
192	private JButton updateMirrors;
193	/** A label telling if the mirror list is being updated. */
194	private final JLabel updateStatus = new JLabel();
195	//}}}
196
197	//{{{ updateMirrorLabel method
198	private void updateMirrorLabel()
199	{
200		String currentMirror = jEdit.getProperty("plugin-manager.mirror.id");
201		String mirrorName;
202		if (currentMirror.equals(MirrorList.Mirror.NONE))
203		{
204			mirrorName = "Plugin Central default";
205		}
206		else
207		{
208			mirrorName = jEdit.getProperty("plugin-manager.mirror.name");
209			if (mirrorName == null) mirrorName = currentMirror;
210		}
211		mirrorLabel.setText(jEdit.getProperty(
212			"options.plugin-manager.mirror") + ' ' + mirrorName);
213	} //}}}
214
215	//}}}
216
217	//{{{ MirrorModel class
218	static class MirrorModel extends AbstractListModel
219	{
220		private List<MirrorList.Mirror> mirrors;
221
222		MirrorModel()
223		{
224			mirrors = new ArrayList<MirrorList.Mirror>();
225		}
226
227		public String getID(int index)
228		{
229			return mirrors.get(index).id;
230		}
231
232		public int getSize()
233		{
234			return mirrors.size();
235		}
236
237		public Object getElementAt(int index)
238		{
239			MirrorList.Mirror mirror = mirrors.get(index);
240			if(mirror.id.equals(MirrorList.Mirror.NONE))
241				return jEdit.getProperty("options.plugin-manager.none");
242			else
243				return mirror.continent+": "+mirror.description+" ("+mirror.location+')';
244		}
245
246		public void setList(List<MirrorList.Mirror> mirrors)
247		{
248			this.mirrors = mirrors;
249			fireContentsChanged(this,0,mirrors.size() - 1);
250		}
251	} //}}}
252
253	//{{{ SingleSelectionModel class
254	static class SingleSelectionModel extends DefaultListSelectionModel
255	{
256		SingleSelectionModel()
257		{
258			setSelectionMode(SINGLE_SELECTION);
259		}
260
261		@Override
262		public void removeSelectionInterval(int index0, int index1) {}
263	} //}}}
264
265	//{{{ ActionHandler class
266	class ActionHandler implements ActionListener
267	{
268		public void actionPerformed(ActionEvent evt)
269		{
270			updateMirrors.setEnabled(false);
271			updateStatus.setText(jEdit.getProperty("options.plugin-manager.workthread"));
272			ThreadUtilities.runInBackground(new UpdateMirrorsThread(true));
273		}
274	} //}}}
275
276	//{{{ UpdateMirrorsThread class
277	/**
278	 * The thread that will update the mirror list.
279	 * It will read them from the cache or from the web.
280	 * It has 4 states :
281	 * 0 : started
282	 * 1 : xml downloaded
283	 * 2 : xml parsed
284	 * 3 : list updated
285	 */
286	class UpdateMirrorsThread extends Task
287	{
288		private final boolean download;
289
290		UpdateMirrorsThread(boolean download)
291		{
292			this.download = download;
293		}
294
295		//{{{ run() method
296		@Override
297		public void _run()
298		{
299			try
300			{
301				setStatus(jEdit.getProperty("options.plugin-manager.workthread"));
302				setMaximum(3L);
303				setValue(0L);
304
305				final List<MirrorList.Mirror> mirrors = new ArrayList<MirrorList.Mirror>();
306				try
307				{
308					MirrorList mirrorList = new MirrorList(download, this);
309					if (download)
310						saveMirrorList(mirrorList.getXml());
311
312					mirrors.addAll(mirrorList.getMirrors());
313				}
314				catch (final Exception ex)
315				{
316					if (download)
317					{
318						Log.log(Log.ERROR,this,ex);
319						ThreadUtilities.runInDispatchThread(new Runnable()
320						{
321							public void run()
322							{
323								GUIUtilities.error(PluginManagerOptionPane.this,
324								"ioerror",new String[] { ex.toString() });
325							}
326						});
327
328					}
329				}
330
331				ThreadUtilities.runInDispatchThread(new Runnable()
332				{
333					public void run()
334					{
335						miraModel.setList(mirrors);
336
337						String id = jEdit.getProperty("plugin-manager.mirror.id");
338						int size = miraModel.getSize();
339						for (int i=0; i < size; i++)
340						{
341							if (size == 1 || miraModel.getID(i).equals(id))
342							{
343								miraList.setSelectedIndex(i);
344								break;
345							}
346						}
347						if (size == 0)
348						{
349							miraList.clearSelection();
350						}
351					}
352				});
353
354				setValue(3L);
355			}
356			finally
357			{
358				ThreadUtilities.runInDispatchThread(new Runnable()
359				{
360					public void run()
361					{
362						updateMirrors.setEnabled(true);
363						updateStatus.setText(null);
364					}
365				});
366			}
367		} //}}}
368
369		//{{{ saveMirrorList() method
370		private void saveMirrorList(String xml)
371		{
372			String settingsDirectory = jEdit.getSettingsDirectory();
373			if(settingsDirectory == null)
374				return;
375
376			File mirrorList = new File(MiscUtilities.constructPath(
377				settingsDirectory,"mirrorList.xml"));
378			OutputStream out = null;
379
380			try
381			{
382				out = new BufferedOutputStream(new FileOutputStream(mirrorList));
383				IOUtilities.copyStream(null, new ByteArrayInputStream(xml.getBytes()), out, false);
384			}
385			catch (IOException e)
386			{
387				Log.log(Log.ERROR,this, "Unable to write cached mirror list : " + mirrorList);
388			}
389			finally
390			{
391				IOUtilities.closeQuietly(out);
392			}
393		} //}}}
394	} //}}}
395}