/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/jedit/pluginmgr/InstallPluginsDialog.java

# · Java · 311 lines · 252 code · 40 blank · 19 comment · 33 complexity · f9ff9fa922e711e7d770f3cbdbf74855 MD5 · raw file

  1. /*
  2. * InstallPluginsDialog.java - Plugin install dialog box
  3. * Copyright (C) 2000, 2001 Slava Pestov
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package org.gjt.sp.jedit.pluginmgr;
  20. import javax.swing.border.EmptyBorder;
  21. import javax.swing.event.*;
  22. import javax.swing.*;
  23. import java.awt.event.*;
  24. import java.awt.*;
  25. import java.util.Vector;
  26. import org.gjt.sp.jedit.gui.*;
  27. import org.gjt.sp.jedit.*;
  28. class InstallPluginsDialog extends EnhancedDialog
  29. {
  30. static final int INSTALL = 0;
  31. static final int UPDATE = 1;
  32. InstallPluginsDialog(JDialog dialog, Vector model, int mode)
  33. {
  34. super(JOptionPane.getFrameForComponent(dialog),
  35. (mode == INSTALL
  36. ? jEdit.getProperty("install-plugins.title")
  37. : jEdit.getProperty("update-plugins.title")),true);
  38. JPanel content = new JPanel(new BorderLayout());
  39. content.setBorder(new EmptyBorder(12,12,12,12));
  40. setContentPane(content);
  41. JLabel label = new JLabel(jEdit.getProperty("install-plugins.caption"));
  42. label.setBorder(new EmptyBorder(0,0,6,0));
  43. content.add(BorderLayout.NORTH,label);
  44. JPanel panel = new JPanel(new BorderLayout());
  45. panel.setBorder(new EmptyBorder(0,0,12,0));
  46. plugins = new JCheckBoxList(model);
  47. plugins.getSelectionModel().addListSelectionListener(new ListHandler());
  48. JScrollPane scroller = new JScrollPane(plugins);
  49. Dimension dim = scroller.getPreferredSize();
  50. dim.height = 120;
  51. scroller.setPreferredSize(dim);
  52. panel.add(BorderLayout.CENTER,scroller);
  53. JPanel panel2 = new JPanel(new BorderLayout());
  54. panel2.setBorder(new EmptyBorder(6,0,0,0));
  55. JPanel labelBox = new JPanel(new GridLayout(
  56. (mode == UPDATE ? 6 : 5),1,0,3));
  57. labelBox.setBorder(new EmptyBorder(0,0,3,12));
  58. labelBox.add(new JLabel(jEdit.getProperty("install-plugins"
  59. + ".info.name"),SwingConstants.RIGHT));
  60. labelBox.add(new JLabel(jEdit.getProperty("install-plugins"
  61. + ".info.author"),SwingConstants.RIGHT));
  62. labelBox.add(new JLabel(jEdit.getProperty("install-plugins"
  63. + ".info.latest-version"),SwingConstants.RIGHT));
  64. if(mode == UPDATE)
  65. {
  66. labelBox.add(new JLabel(jEdit.getProperty("install-plugins"
  67. + ".info.installed-version"),SwingConstants.RIGHT));
  68. }
  69. labelBox.add(new JLabel(jEdit.getProperty("install-plugins"
  70. + ".info.updated"),SwingConstants.RIGHT));
  71. labelBox.add(new JLabel(jEdit.getProperty("install-plugins"
  72. + ".info.description"),SwingConstants.RIGHT));
  73. panel2.add(BorderLayout.WEST,labelBox);
  74. JPanel valueBox = new JPanel(new GridLayout(
  75. (mode == UPDATE ? 6 : 5),1,0,3));
  76. valueBox.setBorder(new EmptyBorder(0,0,3,0));
  77. valueBox.add(name = new JLabel());
  78. valueBox.add(author = new JLabel());
  79. valueBox.add(latestVersion = new JLabel());
  80. if(mode == UPDATE)
  81. {
  82. valueBox.add(installedVersion = new JLabel());
  83. }
  84. valueBox.add(updated = new JLabel());
  85. valueBox.add(Box.createGlue());
  86. panel2.add(BorderLayout.CENTER,valueBox);
  87. JPanel panel3 = new JPanel(new BorderLayout(0,3));
  88. description = new JTextArea(6,30);
  89. description.setEditable(false);
  90. description.setLineWrap(true);
  91. description.setWrapStyleWord(true);
  92. panel3.add(BorderLayout.NORTH,new JScrollPane(description));
  93. if(mode == INSTALL)
  94. {
  95. JPanel panel4 = new JPanel(new BorderLayout(0,3));
  96. ButtonGroup grp = new ButtonGroup();
  97. installUser = new JRadioButton();
  98. String settings = jEdit.getSettingsDirectory();
  99. if(settings == null)
  100. {
  101. settings = jEdit.getProperty("install-plugins.none");
  102. installUser.setEnabled(false);
  103. }
  104. else
  105. {
  106. settings = MiscUtilities.constructPath(settings,"jars");
  107. installUser.setEnabled(true);
  108. }
  109. String[] args = { settings };
  110. installUser.setText(jEdit.getProperty("install-plugins.user",args));
  111. grp.add(installUser);
  112. panel4.add(BorderLayout.CENTER,installUser);
  113. installSystem = new JRadioButton();
  114. String jEditHome = jEdit.getJEditHome();
  115. if(jEditHome == null)
  116. {
  117. jEditHome = jEdit.getProperty("install-plugins.none");
  118. installSystem.setEnabled(false);
  119. }
  120. else
  121. {
  122. jEditHome = MiscUtilities.constructPath(jEditHome,"jars");
  123. installSystem.setEnabled(true);
  124. }
  125. args[0] = jEditHome;
  126. installSystem.setText(jEdit.getProperty("install-plugins.system",args));
  127. grp.add(installSystem);
  128. panel4.add(BorderLayout.SOUTH,installSystem);
  129. if(installUser.isEnabled())
  130. installUser.setSelected(true);
  131. else
  132. installSystem.setSelected(true);
  133. panel3.add(BorderLayout.CENTER,panel4);
  134. }
  135. panel3.add(BorderLayout.SOUTH,downloadSource = new JCheckBox(
  136. jEdit.getProperty("install-plugins.downloadSource")));
  137. downloadSource.setSelected(jEdit.getBooleanProperty("install-plugins"
  138. + ".downloadSource.value"));
  139. panel2.add(BorderLayout.SOUTH,panel3);
  140. panel.add(BorderLayout.SOUTH,panel2);
  141. content.add(BorderLayout.CENTER,panel);
  142. Box box = new Box(BoxLayout.X_AXIS);
  143. box.add(Box.createGlue());
  144. install = new JButton(jEdit.getProperty("install-plugins.install"));
  145. install.setEnabled(false);
  146. getRootPane().setDefaultButton(install);
  147. install.addActionListener(new ActionHandler());
  148. box.add(install);
  149. box.add(Box.createHorizontalStrut(6));
  150. cancel = new JButton(jEdit.getProperty("common.cancel"));
  151. cancel.addActionListener(new ActionHandler());
  152. box.add(cancel);
  153. box.add(Box.createHorizontalStrut(6));
  154. box.add(Box.createGlue());
  155. content.add(BorderLayout.SOUTH,box);
  156. pack();
  157. setLocationRelativeTo(dialog);
  158. show();
  159. }
  160. public void ok()
  161. {
  162. jEdit.setBooleanProperty("install-plugins.downloadSource.value",
  163. downloadSource.isSelected());
  164. dispose();
  165. }
  166. public void cancel()
  167. {
  168. cancelled = true;
  169. dispose();
  170. }
  171. void installPlugins(Roster roster)
  172. {
  173. if(cancelled)
  174. return;
  175. String installDirectory;
  176. if(installUser == null || installUser.isSelected())
  177. {
  178. installDirectory = MiscUtilities.constructPath(
  179. jEdit.getSettingsDirectory(),"jars");
  180. }
  181. else
  182. {
  183. installDirectory = MiscUtilities.constructPath(
  184. jEdit.getJEditHome(),"jars");
  185. }
  186. Object[] selected = plugins.getCheckedValues();
  187. for(int i = 0; i < selected.length; i++)
  188. {
  189. PluginList.Plugin plugin = (PluginList.Plugin)selected[i];
  190. plugin.install(roster,installDirectory,downloadSource.isSelected());
  191. }
  192. }
  193. // private members
  194. private JCheckBoxList plugins;
  195. private JLabel name;
  196. private JLabel author;
  197. private JLabel latestVersion;
  198. private JLabel installedVersion;
  199. private JLabel updated;
  200. private JTextArea description;
  201. private JRadioButton installUser;
  202. private JRadioButton installSystem;
  203. private JCheckBox downloadSource;
  204. private JButton install;
  205. private JButton cancel;
  206. private boolean cancelled;
  207. private Thread thread;
  208. class ActionHandler implements ActionListener
  209. {
  210. public void actionPerformed(ActionEvent evt)
  211. {
  212. if(evt.getSource() == install)
  213. ok();
  214. else
  215. cancel();
  216. }
  217. }
  218. class ListHandler implements ListSelectionListener
  219. {
  220. public void valueChanged(ListSelectionEvent evt)
  221. {
  222. Object selected = plugins.getSelectedValue();
  223. if(selected instanceof PluginList.Plugin)
  224. {
  225. install.setEnabled(true);
  226. PluginList.Plugin plugin = (PluginList.Plugin)selected;
  227. PluginList.Branch branch = plugin.getCompatibleBranch();
  228. name.setText(plugin.name);
  229. author.setText(plugin.author);
  230. if(branch.obsolete)
  231. latestVersion.setText(jEdit.getProperty(
  232. "install-plugins.info.obsolete"));
  233. else
  234. latestVersion.setText(branch.version);
  235. if(installedVersion != null)
  236. installedVersion.setText(plugin.installedVersion);
  237. updated.setText(branch.date);
  238. StringBuffer buf = new StringBuffer();
  239. for(int i = 0; i < branch.deps.size(); i++)
  240. {
  241. PluginList.Dependency dep = (PluginList.Dependency)
  242. branch.deps.elementAt(i);
  243. if(dep.what.equals("plugin")
  244. && !dep.isSatisfied())
  245. {
  246. if(buf.length() != 0)
  247. buf.append(", ");
  248. buf.append(dep.plugin);
  249. }
  250. }
  251. description.setText(plugin.description
  252. + (buf.length() == 0 ? ""
  253. : jEdit.getProperty("install-plugins.info"
  254. + ".also-install") + buf.toString()
  255. + (branch.obsolete ? jEdit.getProperty(
  256. "install-plugins.info.obsolete-text") : "")));
  257. }
  258. else
  259. {
  260. install.setEnabled(false);
  261. name.setText(null);
  262. author.setText(null);
  263. latestVersion.setText(null);
  264. if(installedVersion != null)
  265. installedVersion.setText(null);
  266. updated.setText(null);
  267. description.setText(null);
  268. }
  269. }
  270. }
  271. }