PageRenderTime 30ms CodeModel.GetById 8ms RepoModel.GetById 0ms 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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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.pluginmgr.*;
  32. import org.gjt.sp.util.*;
  33. /**
  34. * The plugin manager option pane.
  35. *
  36. * @version $Id: PluginManagerOptionPane.java 17941 2010-06-01 14:22:58Z kpouer $
  37. */
  38. public class PluginManagerOptionPane extends AbstractOptionPane
  39. {
  40. //{{{ Constructor
  41. public PluginManagerOptionPane()
  42. {
  43. super("plugin-manager");
  44. } //}}}
  45. //{{{ _init() method
  46. @Override
  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. ThreadUtilities.runInBackground(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. @Override
  224. public void removeSelectionInterval(int index0, int index1) {}
  225. } //}}}
  226. //{{{ ActionHandler class
  227. class ActionHandler implements ActionListener
  228. {
  229. public void actionPerformed(ActionEvent evt)
  230. {
  231. updateMirrors.setEnabled(false);
  232. updateStatus.setText(jEdit.getProperty("options.plugin-manager.workthread"));
  233. ThreadUtilities.runInBackground(new UpdateMirrorsThread(true));
  234. }
  235. } //}}}
  236. //{{{ UpdateMirrorsThread class
  237. /**
  238. * The thread that will update the mirror list.
  239. * It will read them from the cache or from the web.
  240. * It has 4 states :
  241. * 0 : started
  242. * 1 : xml downloaded
  243. * 2 : xml parsed
  244. * 3 : list updated
  245. */
  246. class UpdateMirrorsThread extends Task
  247. {
  248. private final boolean download;
  249. UpdateMirrorsThread(boolean download)
  250. {
  251. this.download = download;
  252. }
  253. //{{{ run() method
  254. @Override
  255. public void _run()
  256. {
  257. try
  258. {
  259. setStatus(jEdit.getProperty("options.plugin-manager.workthread"));
  260. setMaximum(3L);
  261. setValue(0L);
  262. final List<MirrorList.Mirror> mirrors = new ArrayList<MirrorList.Mirror>();
  263. try
  264. {
  265. MirrorList mirrorList = new MirrorList(download, this);
  266. if (download)
  267. saveMirrorList(mirrorList.getXml());
  268. mirrors.addAll(mirrorList.getMirrors());
  269. }
  270. catch (final Exception ex)
  271. {
  272. if (download)
  273. {
  274. Log.log(Log.ERROR,this,ex);
  275. ThreadUtilities.runInDispatchThread(new Runnable()
  276. {
  277. public void run()
  278. {
  279. GUIUtilities.error(PluginManagerOptionPane.this,
  280. "ioerror",new String[] { ex.toString() });
  281. }
  282. });
  283. }
  284. }
  285. ThreadUtilities.runInDispatchThread(new Runnable()
  286. {
  287. public void run()
  288. {
  289. miraModel.setList(mirrors);
  290. String id = jEdit.getProperty("plugin-manager.mirror.id");
  291. int size = miraModel.getSize();
  292. for (int i=0; i < size; i++)
  293. {
  294. if (size == 1 || miraModel.getID(i).equals(id))
  295. {
  296. miraList.setSelectedIndex(i);
  297. break;
  298. }
  299. }
  300. if (size == 0)
  301. {
  302. miraList.clearSelection();
  303. }
  304. }
  305. });
  306. setValue(3L);
  307. }
  308. finally
  309. {
  310. ThreadUtilities.runInDispatchThread(new Runnable()
  311. {
  312. public void run()
  313. {
  314. updateMirrors.setEnabled(true);
  315. updateStatus.setText(null);
  316. }
  317. });
  318. }
  319. } //}}}
  320. //{{{ saveMirrorList() method
  321. private void saveMirrorList(String xml)
  322. {
  323. String settingsDirectory = jEdit.getSettingsDirectory();
  324. if(settingsDirectory == null)
  325. return;
  326. File mirrorList = new File(MiscUtilities.constructPath(
  327. settingsDirectory,"mirrorList.xml"));
  328. OutputStream out = null;
  329. try
  330. {
  331. out = new BufferedOutputStream(new FileOutputStream(mirrorList));
  332. IOUtilities.copyStream(null, new ByteArrayInputStream(xml.getBytes()), out, false);
  333. }
  334. catch (IOException e)
  335. {
  336. Log.log(Log.ERROR,this, "Unable to write cached mirror list : " + mirrorList);
  337. }
  338. finally
  339. {
  340. IOUtilities.closeQuietly(out);
  341. }
  342. } //}}}
  343. } //}}}
  344. }