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

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