PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre5/org/gjt/sp/jedit/gui/OptionsDialog.java

#
Java | 554 lines | 419 code | 94 blank | 41 comment | 58 complexity | 646acbf017ca7ffdea1b1f76cc2b3829 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. * OptionsDialog.java - Global options dialog
  3. * Copyright (C) 1998, 1999, 2000, 2001 Slava Pestov
  4. * Portions copyright (C) 1999 mike dillon
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. package org.gjt.sp.jedit.gui;
  21. import javax.swing.*;
  22. import javax.swing.border.*;
  23. import javax.swing.event.*;
  24. import javax.swing.tree.*;
  25. import java.awt.*;
  26. import java.awt.event.*;
  27. import java.util.*;
  28. import org.gjt.sp.jedit.*;
  29. import org.gjt.sp.jedit.options.*;
  30. import org.gjt.sp.util.Log;
  31. /**
  32. * An abstract tabbed options dialog box.
  33. * @author Slava Pestov
  34. * @version $Id: OptionsDialog.java 3972 2002-01-17 10:37:55Z spestov $
  35. */
  36. public class OptionsDialog extends EnhancedDialog
  37. implements ActionListener, TreeSelectionListener
  38. {
  39. public OptionsDialog(View view)
  40. {
  41. super(view, jEdit.getProperty("options.title"), true);
  42. view.showWaitCursor();
  43. JPanel content = new JPanel(new BorderLayout());
  44. content.setBorder(new EmptyBorder(12,12,12,12));
  45. setContentPane(content);
  46. content.setLayout(new BorderLayout());
  47. JPanel stage = new JPanel(new BorderLayout());
  48. stage.setBorder(new EmptyBorder(0,6,0,0));
  49. content.add(stage, BorderLayout.CENTER);
  50. // currentLabel displays the path of the currently selected
  51. // OptionPane at the top of the stage area
  52. currentLabel = new JLabel();
  53. currentLabel.setHorizontalAlignment(JLabel.LEFT);
  54. currentLabel.setBorder(BorderFactory.createMatteBorder(0, 0, 1,
  55. 0, Color.black));
  56. stage.add(currentLabel, BorderLayout.NORTH);
  57. cardPanel = new JPanel(new CardLayout());
  58. cardPanel.setBorder(new EmptyBorder(5,0,0,0));
  59. stage.add(cardPanel, BorderLayout.CENTER);
  60. paneTree = new JTree(createOptionTreeModel());
  61. paneTree.setCellRenderer(new PaneNameRenderer());
  62. paneTree.putClientProperty("JTree.lineStyle", "Angled");
  63. paneTree.setShowsRootHandles(true);
  64. paneTree.setRootVisible(false);
  65. content.add(new JScrollPane(paneTree,
  66. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
  67. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),
  68. BorderLayout.WEST);
  69. JPanel buttons = new JPanel();
  70. buttons.setBorder(new EmptyBorder(12,0,0,0));
  71. buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
  72. buttons.add(Box.createGlue());
  73. ok = new JButton(jEdit.getProperty("common.ok"));
  74. ok.addActionListener(this);
  75. buttons.add(ok);
  76. buttons.add(Box.createHorizontalStrut(6));
  77. getRootPane().setDefaultButton(ok);
  78. cancel = new JButton(jEdit.getProperty("common.cancel"));
  79. cancel.addActionListener(this);
  80. buttons.add(cancel);
  81. buttons.add(Box.createHorizontalStrut(6));
  82. apply = new JButton(jEdit.getProperty("common.apply"));
  83. apply.addActionListener(this);
  84. buttons.add(apply);
  85. buttons.add(Box.createGlue());
  86. content.add(buttons, BorderLayout.SOUTH);
  87. // register the Options dialog as a TreeSelectionListener.
  88. // this is done before the initial selection to ensure that the
  89. // first selected OptionPane is displayed on startup.
  90. paneTree.getSelectionModel().addTreeSelectionListener(this);
  91. paneTree.expandPath(new TreePath(
  92. new Object[] { paneTree.getModel().getRoot(), jEditGroup }));
  93. paneTree.setSelectionRow(0);
  94. view.hideWaitCursor();
  95. pack();
  96. setLocationRelativeTo(view);
  97. show();
  98. }
  99. public void addOptionGroup(OptionGroup group)
  100. {
  101. addOptionGroup(group, pluginsGroup);
  102. }
  103. public void addOptionPane(OptionPane pane)
  104. {
  105. addOptionPane(pane, pluginsGroup);
  106. }
  107. // EnhancedDialog implementation
  108. public void ok()
  109. {
  110. ok(true);
  111. }
  112. public void cancel()
  113. {
  114. dispose();
  115. }
  116. // end EnhancedDialog implementation
  117. public void ok(boolean dispose)
  118. {
  119. OptionTreeModel m = (OptionTreeModel) paneTree
  120. .getModel();
  121. ((OptionGroup) m.getRoot()).save();
  122. /* This will fire the PROPERTIES_CHANGED event */
  123. jEdit.propertiesChanged();
  124. // Save settings to disk
  125. jEdit.saveSettings();
  126. // get rid of this dialog if necessary
  127. if(dispose)
  128. dispose();
  129. }
  130. public void actionPerformed(ActionEvent evt)
  131. {
  132. Object source = evt.getSource();
  133. if(source == ok)
  134. {
  135. ok();
  136. }
  137. else if(source == cancel)
  138. {
  139. cancel();
  140. }
  141. else if(source == apply)
  142. {
  143. ok(false);
  144. }
  145. }
  146. public void valueChanged(TreeSelectionEvent evt)
  147. {
  148. TreePath path = evt.getPath();
  149. if (path == null || !(path.getLastPathComponent() instanceof
  150. OptionPane)) return;
  151. Object[] nodes = path.getPath();
  152. StringBuffer buf = new StringBuffer();
  153. OptionPane optionPane = null;
  154. String name = null;
  155. int lastIdx = nodes.length - 1;
  156. for (int i = paneTree.isRootVisible() ? 0 : 1;
  157. i <= lastIdx; i++)
  158. {
  159. if (nodes[i] instanceof OptionPane)
  160. {
  161. optionPane = (OptionPane)nodes[i];
  162. name = optionPane.getName();
  163. }
  164. else if (nodes[i] instanceof OptionGroup)
  165. {
  166. name = ((OptionGroup)nodes[i]).getName();
  167. }
  168. else
  169. {
  170. continue;
  171. }
  172. if (name != null)
  173. {
  174. String label = jEdit.getProperty("options." +
  175. name + ".label");
  176. if (label == null)
  177. {
  178. buf.append(name);
  179. }
  180. else
  181. {
  182. buf.append(label);
  183. }
  184. }
  185. if (i != lastIdx) buf.append(": ");
  186. }
  187. currentLabel.setText(buf.toString());
  188. optionPane.init();
  189. pack();
  190. ((CardLayout)cardPanel.getLayout()).show(cardPanel, name);
  191. }
  192. // private members
  193. private Hashtable panes;
  194. private JTree paneTree;
  195. private JPanel cardPanel;
  196. private JLabel currentLabel;
  197. private JButton ok;
  198. private JButton cancel;
  199. private JButton apply;
  200. private OptionGroup jEditGroup;
  201. private OptionGroup pluginsGroup;
  202. private OptionTreeModel createOptionTreeModel()
  203. {
  204. OptionTreeModel paneTreeModel = new OptionTreeModel();
  205. OptionGroup rootGroup = (OptionGroup) paneTreeModel.getRoot();
  206. addOptionPane(new OverviewOptionPane(), rootGroup);
  207. // initialize the jEdit branch of the options tree
  208. jEditGroup = new OptionGroup("jedit");
  209. addOptionPane(new GeneralOptionPane(), jEditGroup);
  210. addOptionPane(new AppearanceOptionPane(), jEditGroup);
  211. addOptionPane(new TextAreaOptionPane(), jEditGroup);
  212. addOptionPane(new GutterOptionPane(), jEditGroup);
  213. addOptionPane(new ColorOptionPane(), jEditGroup);
  214. addOptionPane(new StyleOptionPane(), jEditGroup);
  215. addOptionPane(new LoadSaveOptionPane(), jEditGroup);
  216. addOptionPane(new EditingOptionPane(), jEditGroup);
  217. addOptionPane(new ModeOptionPane(), jEditGroup);
  218. addOptionPane(new ShortcutsOptionPane(), jEditGroup);
  219. addOptionPane(new DockingOptionPane(), jEditGroup);
  220. addOptionPane(new ContextOptionPane(), jEditGroup);
  221. addOptionPane(new ToolBarOptionPane(), jEditGroup);
  222. addOptionPane(new AbbrevsOptionPane(), jEditGroup);
  223. addOptionPane(new PrintOptionPane(), jEditGroup);
  224. addOptionPane(new FirewallOptionPane(), jEditGroup);
  225. OptionGroup browserGroup = new OptionGroup("browser");
  226. addOptionPane(new BrowserOptionPane(), browserGroup);
  227. addOptionPane(new BrowserColorsOptionPane(), browserGroup);
  228. addOptionGroup(browserGroup, jEditGroup);
  229. addOptionGroup(jEditGroup, rootGroup);
  230. // initialize the Plugins branch of the options tree
  231. pluginsGroup = new OptionGroup("plugins");
  232. // Query plugins for option panes
  233. EditPlugin[] plugins = jEdit.getPlugins();
  234. for(int i = 0; i < plugins.length; i++)
  235. {
  236. EditPlugin ep = plugins[i];
  237. try
  238. {
  239. ep.createOptionPanes(this);
  240. }
  241. catch(Throwable t)
  242. {
  243. Log.log(Log.ERROR, ep,
  244. "Error creating option pane");
  245. Log.log(Log.ERROR, ep, t);
  246. }
  247. }
  248. // only add the Plugins branch if there are OptionPanes
  249. if (pluginsGroup.getMemberCount() > 0)
  250. {
  251. addOptionGroup(pluginsGroup, rootGroup);
  252. }
  253. return paneTreeModel;
  254. }
  255. private void addOptionGroup(OptionGroup child, OptionGroup parent)
  256. {
  257. Enumeration enum = child.getMembers();
  258. while (enum.hasMoreElements())
  259. {
  260. Object elem = enum.nextElement();
  261. if (elem instanceof OptionPane)
  262. {
  263. addOptionPane((OptionPane) elem, child);
  264. }
  265. else if (elem instanceof OptionGroup)
  266. {
  267. addOptionGroup((OptionGroup) elem, child);
  268. }
  269. }
  270. parent.addOptionGroup(child);
  271. }
  272. private void addOptionPane(OptionPane pane, OptionGroup parent)
  273. {
  274. String name = pane.getName();
  275. cardPanel.add(pane.getComponent(), name);
  276. parent.addOptionPane(pane);
  277. }
  278. class PaneNameRenderer extends DefaultTreeCellRenderer
  279. {
  280. public PaneNameRenderer()
  281. {
  282. paneFont = UIManager.getFont("Tree.font");
  283. groupFont = paneFont.deriveFont(Font.BOLD);
  284. }
  285. public Component getTreeCellRendererComponent(JTree tree,
  286. Object value, boolean selected, boolean expanded,
  287. boolean leaf, int row, boolean hasFocus)
  288. {
  289. super.getTreeCellRendererComponent(tree,value,
  290. selected,expanded,leaf,row,hasFocus);
  291. String name = null;
  292. if (value instanceof OptionGroup)
  293. {
  294. name = ((OptionGroup)value).getName();
  295. this.setFont(groupFont);
  296. }
  297. else if (value instanceof OptionPane)
  298. {
  299. name = ((OptionPane)value).getName();
  300. this.setFont(paneFont);
  301. }
  302. if (name == null)
  303. {
  304. setText(null);
  305. }
  306. else
  307. {
  308. String label = jEdit.getProperty("options." +
  309. name + ".label");
  310. if (label == null)
  311. {
  312. // hahaha, suckers!!!
  313. Log.log(Log.WARNING,this,
  314. "options." + name + ".label"
  315. + " property not defined");
  316. setText(name);
  317. }
  318. else
  319. {
  320. setText(label);
  321. }
  322. }
  323. setIcon(null);
  324. return this;
  325. }
  326. private Font paneFont;
  327. private Font groupFont;
  328. }
  329. class OptionTreeModel implements TreeModel
  330. {
  331. public void addTreeModelListener(TreeModelListener l)
  332. {
  333. listenerList.add(TreeModelListener.class, l);
  334. }
  335. public void removeTreeModelListener(TreeModelListener l)
  336. {
  337. listenerList.remove(TreeModelListener.class, l);
  338. }
  339. public Object getChild(Object parent, int index)
  340. {
  341. if (parent instanceof OptionGroup)
  342. {
  343. return ((OptionGroup)parent).getMember(index);
  344. }
  345. else
  346. {
  347. return null;
  348. }
  349. }
  350. public int getChildCount(Object parent)
  351. {
  352. if (parent instanceof OptionGroup)
  353. {
  354. return ((OptionGroup)parent).getMemberCount();
  355. }
  356. else
  357. {
  358. return 0;
  359. }
  360. }
  361. public int getIndexOfChild(Object parent, Object child)
  362. {
  363. if (parent instanceof OptionGroup)
  364. {
  365. return ((OptionGroup)parent)
  366. .getMemberIndex(child);
  367. }
  368. else
  369. {
  370. return -1;
  371. }
  372. }
  373. public Object getRoot()
  374. {
  375. return root;
  376. }
  377. public boolean isLeaf(Object node)
  378. {
  379. return node instanceof OptionPane;
  380. }
  381. public void valueForPathChanged(TreePath path, Object newValue)
  382. {
  383. // this model may not be changed by the TableCellEditor
  384. }
  385. protected void fireNodesChanged(Object source, Object[] path,
  386. int[] childIndices, Object[] children)
  387. {
  388. Object[] listeners = listenerList.getListenerList();
  389. TreeModelEvent modelEvent = null;
  390. for (int i = listeners.length - 2; i >= 0; i -= 2)
  391. {
  392. if (listeners[i] != TreeModelListener.class)
  393. continue;
  394. if (modelEvent == null)
  395. {
  396. modelEvent = new TreeModelEvent(source,
  397. path, childIndices, children);
  398. }
  399. ((TreeModelListener)listeners[i + 1])
  400. .treeNodesChanged(modelEvent);
  401. }
  402. }
  403. protected void fireNodesInserted(Object source, Object[] path,
  404. int[] childIndices, Object[] children)
  405. {
  406. Object[] listeners = listenerList.getListenerList();
  407. TreeModelEvent modelEvent = null;
  408. for (int i = listeners.length - 2; i >= 0; i -= 2)
  409. {
  410. if (listeners[i] != TreeModelListener.class)
  411. continue;
  412. if (modelEvent == null)
  413. {
  414. modelEvent = new TreeModelEvent(source,
  415. path, childIndices, children);
  416. }
  417. ((TreeModelListener)listeners[i + 1])
  418. .treeNodesInserted(modelEvent);
  419. }
  420. }
  421. protected void fireNodesRemoved(Object source, Object[] path,
  422. int[] childIndices, Object[] children)
  423. {
  424. Object[] listeners = listenerList.getListenerList();
  425. TreeModelEvent modelEvent = null;
  426. for (int i = listeners.length - 2; i >= 0; i -= 2)
  427. {
  428. if (listeners[i] != TreeModelListener.class)
  429. continue;
  430. if (modelEvent == null)
  431. {
  432. modelEvent = new TreeModelEvent(source,
  433. path, childIndices, children);
  434. }
  435. ((TreeModelListener)listeners[i + 1])
  436. .treeNodesRemoved(modelEvent);
  437. }
  438. }
  439. protected void fireTreeStructureChanged(Object source,
  440. Object[] path, int[] childIndices, Object[] children)
  441. {
  442. Object[] listeners = listenerList.getListenerList();
  443. TreeModelEvent modelEvent = null;
  444. for (int i = listeners.length - 2; i >= 0; i -= 2)
  445. {
  446. if (listeners[i] != TreeModelListener.class)
  447. continue;
  448. if (modelEvent == null)
  449. {
  450. modelEvent = new TreeModelEvent(source,
  451. path, childIndices, children);
  452. }
  453. ((TreeModelListener)listeners[i + 1])
  454. .treeStructureChanged(modelEvent);
  455. }
  456. }
  457. private OptionGroup root = new OptionGroup(null);
  458. private EventListenerList listenerList = new EventListenerList();
  459. }
  460. }