/plugins/MacroManager/tags/MacroManager1_2_rc/macroManager/InstallMacrosDialog.java

# · Java · 531 lines · 404 code · 69 blank · 58 comment · 52 complexity · ae3d943a83a0046b5a72e2ba38b1a944 MD5 · raw file

  1. /*
  2. * InstallMacrosDialog.java - macro install dialog box
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2002 Carmine Lucarelli
  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 macroManager;
  23. //{{{ Imports
  24. import javax.swing.border.*;
  25. import javax.swing.event.*;
  26. import javax.swing.*;
  27. import java.awt.event.*;
  28. import java.awt.*;
  29. import java.util.ArrayList;
  30. import java.util.Vector;
  31. import org.gjt.sp.jedit.gui.*;
  32. import org.gjt.sp.jedit.*;
  33. import org.gjt.sp.util.Log;
  34. //}}}
  35. public class InstallMacrosDialog extends EnhancedDialog
  36. {
  37. static final int INSTALL = 0;
  38. static final int UPDATE = 1;
  39. //{{{ InstallPluginsDialog constructor
  40. /* InstallPluginsDialog(JDialog dialog, Vector model, int mode)
  41. {
  42. super(JOptionPane.getFrameForComponent(dialog),
  43. (mode == INSTALL
  44. ? jEdit.getProperty("install-macros.title")
  45. : jEdit.getProperty("update-macros.title")),true); */
  46. public InstallMacrosDialog(Frame frame)
  47. {
  48. super(frame, jEdit.getProperty("install-macros.title"), true);
  49. JPanel content = new JPanel(new BorderLayout(12,12));
  50. content.setBorder(new EmptyBorder(12,12,12,12));
  51. setContentPane(content);
  52. try
  53. {
  54. list = new MacroListDownloadProgress(InstallMacrosDialog.this, false)
  55. .getMacroList();
  56. list.sortMacroList(MacroList.SORT_BY_NAME);
  57. }
  58. catch(Exception e)
  59. {
  60. Log.log(Log.ERROR, this, "An error occurred " + e.getMessage());
  61. }
  62. if(list == null)
  63. return;
  64. JPanel infoPanel = new JPanel(new GridLayout(3,1,0,3));
  65. infoPanel.setBorder(new EmptyBorder(0,0,3,0));
  66. JLabel label = new JLabel(jEdit.getProperty("install-macros.caption"));
  67. infoPanel.add(label);
  68. dateLabel = new JLabel(jEdit.getProperty("install-macros.refresh") + " " + MacroList.timestamp);
  69. infoPanel.add(dateLabel);
  70. refreshList = new JButton(jEdit.getProperty("install-macros.refreshList"));
  71. refreshList.addActionListener(new ActionHandler());
  72. infoPanel.add(refreshList);
  73. content.add(BorderLayout.NORTH,infoPanel);
  74. macros = new JCheckBoxList(list.macros);
  75. macros.getSelectionModel().addListSelectionListener(new ListHandler());
  76. macros.getModel().addTableModelListener(new TableModelHandler());
  77. JScrollPane scroller = new JScrollPane(macros);
  78. scroller.setPreferredSize(new Dimension(200,0));
  79. content.add(BorderLayout.WEST,scroller);
  80. JPanel panel = new JPanel(new BorderLayout());
  81. panel.setBorder(new TitledBorder(jEdit.getProperty("install-macros"
  82. + ".macro-info")));
  83. JPanel labelAndValueBox = new JPanel(new BorderLayout());
  84. JPanel labelBox = new JPanel(new GridLayout(6,1,0,3));
  85. labelBox.setBorder(new EmptyBorder(0,0,3,12));
  86. labelBox.add(new JLabel(jEdit.getProperty("install-macros"
  87. + ".info.name"),SwingConstants.RIGHT));
  88. labelBox.add(new JLabel(jEdit.getProperty("install-macros"
  89. + ".info.author"),SwingConstants.RIGHT));
  90. labelBox.add(new JLabel(jEdit.getProperty("install-macros"
  91. + ".info.size"),SwingConstants.RIGHT));
  92. labelBox.add(new JLabel(jEdit.getProperty("install-macros"
  93. + ".info.latest-version"),SwingConstants.RIGHT));
  94. labelBox.add(new JLabel(jEdit.getProperty("install-macros"
  95. + ".info.number-of-downloads"),SwingConstants.RIGHT));
  96. // labelBox.add(new JLabel(jEdit.getProperty("install-macros"
  97. // + ".info.description"),SwingConstants.RIGHT));
  98. labelAndValueBox.add(BorderLayout.WEST,labelBox);
  99. JPanel valueBox = new JPanel(new GridLayout(6,1,0,3));
  100. valueBox.setBorder(new EmptyBorder(0,0,3,0));
  101. valueBox.add(name = new JLabel());
  102. valueBox.add(author = new JLabel());
  103. valueBox.add(size = new JLabel());
  104. valueBox.add(latestVersion = new JLabel());
  105. valueBox.add(numberOfDownloads = new JLabel());
  106. valueBox.add(Box.createGlue());
  107. labelAndValueBox.add(BorderLayout.CENTER,valueBox);
  108. panel.add(BorderLayout.NORTH,labelAndValueBox);
  109. description = new JTextArea(6,50);
  110. description.setEditable(false);
  111. description.setLineWrap(true);
  112. description.setWrapStyleWord(true);
  113. panel.add(BorderLayout.CENTER,new JScrollPane(description));
  114. content.add(BorderLayout.CENTER,panel);
  115. panel = new JPanel(new BorderLayout(12,0));
  116. JPanel panel2 = new JPanel(new GridLayout(7,1));
  117. Box totalSizeBox = new Box(BoxLayout.X_AXIS);
  118. totalSizeBox.add(new JLabel(jEdit.getProperty("install-macros.totalSize")));
  119. totalSizeBox.add(Box.createHorizontalStrut(12));
  120. totalSizeBox.add(totalSize = new JLabel());
  121. panel2.add(totalSizeBox);
  122. ButtonGroup grp = new ButtonGroup();
  123. installUser = new JRadioButton();
  124. installUser.addActionListener(new ActionHandler());
  125. String settings = jEdit.getSettingsDirectory();
  126. if(settings == null)
  127. {
  128. settings = jEdit.getProperty("install-macros.none");
  129. installUser.setEnabled(false);
  130. }
  131. else
  132. {
  133. settings = MiscUtilities.constructPath(settings,"macros");
  134. installUser.setEnabled(true);
  135. }
  136. String[] args = { settings };
  137. installUser.setText(jEdit.getProperty("install-macros.user",args));
  138. grp.add(installUser);
  139. panel2.add(installUser);
  140. installSystem = new JRadioButton();
  141. installSystem.addActionListener(new ActionHandler());
  142. String jEditHome = jEdit.getJEditHome();
  143. if(jEditHome == null)
  144. {
  145. jEditHome = jEdit.getProperty("install-macros.none");
  146. installSystem.setEnabled(false);
  147. }
  148. else
  149. {
  150. jEditHome = MiscUtilities.constructPath(jEditHome,"macros");
  151. installSystem.setEnabled(true);
  152. }
  153. args[0] = jEditHome;
  154. installSystem.setText(jEdit.getProperty("install-macros.system",args));
  155. grp.add(installSystem);
  156. panel2.add(installSystem);
  157. installCustom = new JRadioButton();
  158. installCustom.addActionListener(new ActionHandler());
  159. installCustom.setText(jEdit.getProperty("install-macros.custom"));
  160. grp.add(installCustom);
  161. panel2.add(installCustom);
  162. customDir = new JTextField();
  163. customDir.setEnabled(false);
  164. panel2.add(customDir);
  165. String work = jEdit.getProperty("intall-macors.custom.directory");
  166. if(work != null)
  167. {
  168. installCustom.setSelected(true);
  169. customDir.setEnabled(true);
  170. customDir.setText(work);
  171. }
  172. else if(installUser.isEnabled())
  173. installUser.setSelected(true);
  174. else
  175. installSystem.setSelected(true);
  176. panel.add(BorderLayout.NORTH,panel2);
  177. Box box = new Box(BoxLayout.X_AXIS);
  178. box.add(Box.createGlue());
  179. install = new JButton(jEdit.getProperty("install-macros.install"));
  180. install.setEnabled(false);
  181. getRootPane().setDefaultButton(install);
  182. install.addActionListener(new ActionHandler());
  183. box.add(install);
  184. box.add(Box.createHorizontalStrut(6));
  185. cancel = new JButton(jEdit.getProperty("common.cancel"));
  186. cancel.addActionListener(new ActionHandler());
  187. box.add(cancel);
  188. box.add(Box.createHorizontalStrut(6));
  189. box.add(Box.createGlue());
  190. sort = new JButton(jEdit.getProperty("install-macros.sort-by-date"));
  191. sort.addActionListener(new ActionHandler());
  192. box.add(sort);
  193. box.add(Box.createHorizontalStrut(6));
  194. box.add(Box.createGlue());
  195. searchField = new JTextField(jEdit.getProperty("install-macros.searchField"));
  196. searchField.addKeyListener(new KeyHandler());
  197. searchField.addFocusListener(new FocusAdapter()
  198. {
  199. public void focusGained(FocusEvent evt)
  200. {
  201. searchField.selectAll();
  202. }
  203. });
  204. searchField.addMouseListener(new MouseAdapter()
  205. {
  206. public void mouseClicked(MouseEvent evt)
  207. {
  208. searchField.selectAll();
  209. }
  210. });
  211. searchField.setColumns(20);
  212. box.add(searchField);
  213. box.add(Box.createHorizontalStrut(6));
  214. box.add(Box.createGlue());
  215. /* search = new JButton(jEdit.getProperty("install-macros.search"));
  216. search.addActionListener(new ActionHandler());
  217. box.add(search);
  218. box.add(Box.createHorizontalStrut(6));
  219. box.add(Box.createGlue()); */
  220. panel.add(BorderLayout.SOUTH,box);
  221. content.add(BorderLayout.SOUTH,panel);
  222. updateTotalSize();
  223. pack();
  224. setLocationRelativeTo(frame);
  225. show();
  226. } //}}}
  227. //{{{ ok() method
  228. public void ok()
  229. {
  230. if(installCustom.isSelected())
  231. jEdit.setProperty("intall-macors.custom.directory", customDir.getText());
  232. else
  233. jEdit.unsetProperty("intall-macors.custom.directory");
  234. macroManager.Roster roster = new macroManager.Roster();
  235. installMacros(roster);
  236. if(roster.isEmpty())
  237. return;
  238. new MacroManagerProgress(InstallMacrosDialog.this, roster);
  239. // rescan the macros to update the menu
  240. Macros.loadMacros();
  241. dispose();
  242. } //}}}
  243. //{{{ cancel() method
  244. public void cancel()
  245. {
  246. cancelled = true;
  247. dispose();
  248. } //}}}
  249. //{{{ installPlugins() method
  250. void installMacros(macroManager.Roster roster)
  251. {
  252. if(cancelled)
  253. return;
  254. String installDirectory;
  255. if(installUser == null || installUser.isSelected())
  256. {
  257. installDirectory = MiscUtilities.constructPath(
  258. jEdit.getSettingsDirectory(),"macros");
  259. }
  260. else if(installSystem.isSelected())
  261. {
  262. installDirectory = MiscUtilities.constructPath(
  263. jEdit.getJEditHome(),"macros");
  264. }
  265. else
  266. {
  267. installDirectory = customDir.getText();
  268. }
  269. Object[] selected = macros.getCheckedValues();
  270. for(int i = 0; i < selected.length; i++)
  271. {
  272. ((MacroList.Macro)selected[i]).install(roster, installDirectory);
  273. }
  274. } //}}}
  275. //{{{ Private members
  276. //{{{ Instance variables
  277. private JCheckBoxList macros;
  278. private JLabel name;
  279. private JLabel author;
  280. private JLabel size;
  281. private JLabel latestVersion;
  282. private JLabel numberOfDownloads;
  283. private JTextArea description;
  284. private JLabel totalSize;
  285. private JLabel dateLabel;
  286. private JRadioButton installUser;
  287. private JRadioButton installSystem;
  288. private JRadioButton installCustom;
  289. private JTextField customDir;
  290. private JButton install;
  291. private JButton cancel;
  292. private JButton sort;
  293. private JButton refreshList;
  294. private JTextField searchField;
  295. // private JButton search;
  296. private MacroList list;
  297. private boolean cancelled;
  298. private boolean sortedByDate = false;
  299. private Thread thread;
  300. //}}}
  301. //{{{ updateInfo() method
  302. private void updateInfo()
  303. {
  304. Object selected = macros.getSelectedValue();
  305. if(selected instanceof MacroList.Macro)
  306. {
  307. MacroList.Macro macro = (MacroList.Macro)selected;
  308. name.setText(macro.name);
  309. author.setText(macro.author);
  310. size.setText(String.valueOf((macro.size / 1024)) + " Kb");
  311. latestVersion.setText(macro.version);
  312. numberOfDownloads.setText(macro.hits);
  313. description.setText(macro.description);
  314. description.setCaretPosition(0);
  315. }
  316. else
  317. {
  318. name.setText(null);
  319. author.setText(null);
  320. size.setText(null);
  321. latestVersion.setText(null);
  322. description.setText(null);
  323. }
  324. } //}}}
  325. //{{{ updateTotalSize() method
  326. private void updateTotalSize()
  327. {
  328. ArrayList selectedMacros = new ArrayList();
  329. Object[] selected = macros.getCheckedValues();
  330. install.setEnabled(selected.length != 0);
  331. for(int i = 0; i < selected.length; i++)
  332. {
  333. MacroList.Macro macro = (MacroList.Macro)selected[i];
  334. if(!selectedMacros.contains(macro))
  335. selectedMacros.add(macro);
  336. }
  337. int _totalSize = 0;
  338. for(int i = 0; i < selectedMacros.size(); i++)
  339. {
  340. _totalSize += ((MacroList.Macro)selectedMacros.get(i)).size;
  341. }
  342. totalSize.setText(String.valueOf(_totalSize / 1024) + " Kb");
  343. } //}}}
  344. //}}}
  345. //{{{ sortByDate method
  346. void sort()
  347. {
  348. String sortLabel = jEdit.getProperty("install-macros.sort-by-date");
  349. int constraint;
  350. if(sort.getText().equals(sortLabel))
  351. {
  352. constraint = MacroList.SORT_BY_DATE;
  353. sort.setText(jEdit.getProperty("install-macros.sort-by-name"));
  354. }
  355. else
  356. {
  357. constraint = MacroList.SORT_BY_NAME;
  358. sort.setText(sortLabel);
  359. }
  360. list.sortMacroList(constraint);
  361. macros.setModel(list.macros);
  362. macros.getSelectionModel().addListSelectionListener(new ListHandler());
  363. macros.getModel().addTableModelListener(new TableModelHandler());
  364. } //}}}
  365. //{{{ sortByDate method
  366. void search()
  367. {
  368. String srch = searchField.getText();
  369. Log.log(Log.DEBUG, this, "searching for " + srch);
  370. if(srch == null || srch.length() == 0)
  371. {
  372. macros.setModel(list.macros);
  373. macros.getSelectionModel().addListSelectionListener(new ListHandler());
  374. macros.getModel().addTableModelListener(new TableModelHandler());
  375. }
  376. Vector results = list.searchMacroList(srch);
  377. if(results.size() > 0)
  378. {
  379. macros.setModel(results);
  380. macros.getSelectionModel().addListSelectionListener(new ListHandler());
  381. macros.getModel().addTableModelListener(new TableModelHandler());
  382. searchField.selectAll();
  383. }
  384. else
  385. {
  386. searchField.setText("No matches");
  387. searchField.selectAll();
  388. }
  389. } //}}}
  390. //{{{ refreshList method
  391. void refreshList()
  392. {
  393. try
  394. {
  395. list = new MacroListDownloadProgress(InstallMacrosDialog.this, true)
  396. .getMacroList();
  397. }
  398. catch(Exception e)
  399. {
  400. Log.log(Log.ERROR, this, "An error occurred " + e.getMessage());
  401. }
  402. macros.setModel(list.macros);
  403. macros.getSelectionModel().addListSelectionListener(new ListHandler());
  404. macros.getModel().addTableModelListener(new TableModelHandler());
  405. dateLabel.setText(jEdit.getProperty("install-macros.refresh") + " " + MacroList.timestamp);
  406. } //}}}
  407. //{{{ updateButtons method
  408. private void updateButtons()
  409. {
  410. if(installUser.isSelected() || installSystem.isSelected())
  411. customDir.setEnabled(false);
  412. else
  413. customDir.setEnabled(true);
  414. }
  415. //{{{ ActionHandler class
  416. class ActionHandler implements ActionListener
  417. {
  418. public void actionPerformed(ActionEvent evt)
  419. {
  420. Object source = evt.getSource();
  421. if(source == install)
  422. ok();
  423. else if(source == cancel)
  424. cancel();
  425. else if(source == sort)
  426. sort();
  427. else if(source == refreshList)
  428. refreshList();
  429. else if(source == installUser || source == installSystem || source == installCustom)
  430. updateButtons();
  431. // else if(search == sort)
  432. // search();
  433. }
  434. } //}}}
  435. //{{{ KeyHandler class
  436. class KeyHandler extends KeyAdapter
  437. {
  438. public void keyReleased(KeyEvent evt)
  439. {
  440. if(evt.getKeyCode() == KeyEvent.VK_ENTER)
  441. {
  442. search();
  443. }
  444. }
  445. } //}}}
  446. //{{{ ListHandler class
  447. class ListHandler implements ListSelectionListener
  448. {
  449. public void valueChanged(ListSelectionEvent evt)
  450. {
  451. updateInfo();
  452. }
  453. } //}}}
  454. //{{{ TableModelHandler class
  455. class TableModelHandler implements TableModelListener
  456. {
  457. public void tableChanged(TableModelEvent e)
  458. {
  459. updateTotalSize();
  460. }
  461. } //}}}
  462. }