PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/options/ToolBarOptionPane.java

#
Java | 702 lines | 557 code | 82 blank | 63 comment | 77 complexity | d9dc4653a891b3375a778be7894c3172 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. * ToolBarOptionPane.java - Tool bar options panel
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2000, 2001, 2002 Slava Pestov
  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. //{{{ 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.net.*;
  30. import java.util.*;
  31. import org.gjt.sp.jedit.browser.VFSBrowser;
  32. import org.gjt.sp.jedit.gui.*;
  33. import org.gjt.sp.jedit.*;
  34. import org.gjt.sp.util.Log;
  35. //}}}
  36. //{{{ ToolBarOptionPane class
  37. /**
  38. * Tool bar editor.
  39. * @author Slava Pestov
  40. * @version $Id: ToolBarOptionPane.java 4622 2003-04-14 22:43:17Z spestov $
  41. */
  42. public class ToolBarOptionPane extends AbstractOptionPane
  43. {
  44. //{{{ ToolBarOptionPane constructor
  45. public ToolBarOptionPane()
  46. {
  47. super("toolbar");
  48. } //}}}
  49. //{{{ _init() method
  50. protected void _init()
  51. {
  52. setLayout(new BorderLayout());
  53. JPanel panel = new JPanel(new GridLayout(2,1));
  54. /* Show toolbar */
  55. showToolbar = new JCheckBox(jEdit.getProperty(
  56. "options.toolbar.showToolbar"));
  57. showToolbar.setSelected(jEdit.getBooleanProperty("view.showToolbar"));
  58. panel.add(showToolbar);
  59. panel.add(new JLabel(jEdit.getProperty(
  60. "options.toolbar.caption")));
  61. add(BorderLayout.NORTH,panel);
  62. String toolbar = jEdit.getProperty("view.toolbar");
  63. StringTokenizer st = new StringTokenizer(toolbar);
  64. listModel = new DefaultListModel();
  65. while(st.hasMoreTokens())
  66. {
  67. String actionName = (String)st.nextToken();
  68. if(actionName.equals("-"))
  69. listModel.addElement(new ToolBarOptionPane.Button("-",null,null,"-"));
  70. else
  71. {
  72. EditAction action = jEdit.getAction(actionName);
  73. if(action == null)
  74. continue;
  75. String label = action.getLabel();
  76. if(label == null)
  77. continue;
  78. Icon icon;
  79. String iconName;
  80. if(actionName.equals("-"))
  81. {
  82. iconName = null;
  83. icon = null;
  84. }
  85. else
  86. {
  87. iconName = jEdit.getProperty(actionName + ".icon");
  88. if(iconName == null)
  89. icon = GUIUtilities.loadIcon("BrokenImage.png");
  90. else
  91. {
  92. icon = GUIUtilities.loadIcon(iconName);
  93. if(icon == null)
  94. icon = GUIUtilities.loadIcon("BrokenImage.png");
  95. }
  96. }
  97. listModel.addElement(new Button(actionName,iconName,icon,label));
  98. }
  99. }
  100. list = new JList(listModel);
  101. list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  102. list.addListSelectionListener(new ListHandler());
  103. list.setCellRenderer(new ButtonCellRenderer());
  104. add(BorderLayout.CENTER,new JScrollPane(list));
  105. //{{{ Create buttons
  106. JPanel buttons = new JPanel();
  107. buttons.setBorder(new EmptyBorder(3,0,0,0));
  108. buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
  109. ActionHandler actionHandler = new ActionHandler();
  110. add = new RolloverButton(GUIUtilities.loadIcon("Plus.png"));
  111. add.setToolTipText(jEdit.getProperty("options.toolbar.add"));
  112. add.addActionListener(actionHandler);
  113. buttons.add(add);
  114. buttons.add(Box.createHorizontalStrut(6));
  115. remove = new RolloverButton(GUIUtilities.loadIcon("Minus.png"));
  116. remove.setToolTipText(jEdit.getProperty("options.toolbar.remove"));
  117. remove.addActionListener(actionHandler);
  118. buttons.add(remove);
  119. buttons.add(Box.createHorizontalStrut(6));
  120. moveUp = new RolloverButton(GUIUtilities.loadIcon("ArrowU.png"));
  121. moveUp.setToolTipText(jEdit.getProperty("options.toolbar.moveUp"));
  122. moveUp.addActionListener(actionHandler);
  123. buttons.add(moveUp);
  124. buttons.add(Box.createHorizontalStrut(6));
  125. moveDown = new RolloverButton(GUIUtilities.loadIcon("ArrowD.png"));
  126. moveDown.setToolTipText(jEdit.getProperty("options.toolbar.moveDown"));
  127. moveDown.addActionListener(actionHandler);
  128. buttons.add(moveDown);
  129. buttons.add(Box.createHorizontalStrut(6));
  130. edit = new RolloverButton(GUIUtilities.loadIcon("ButtonProperties.png"));
  131. edit.setToolTipText(jEdit.getProperty("options.toolbar.edit"));
  132. edit.addActionListener(actionHandler);
  133. buttons.add(edit);
  134. buttons.add(Box.createGlue());
  135. //}}}
  136. updateButtons();
  137. add(BorderLayout.SOUTH,buttons);
  138. //{{{ Ceate icons list
  139. iconList = new DefaultComboBoxModel();
  140. st = new StringTokenizer(jEdit.getProperty("icons"));
  141. while(st.hasMoreElements())
  142. {
  143. String icon = st.nextToken();
  144. iconList.addElement(new IconListEntry(
  145. GUIUtilities.loadIcon(icon),icon));
  146. } //}}}
  147. } ///}}}
  148. //{{{ _save() method
  149. protected void _save()
  150. {
  151. jEdit.setBooleanProperty("view.showToolbar",showToolbar
  152. .isSelected());
  153. StringBuffer buf = new StringBuffer();
  154. for(int i = 0; i < listModel.getSize(); i++)
  155. {
  156. if(i != 0)
  157. buf.append(' ');
  158. Button button = (Button)listModel.elementAt(i);
  159. buf.append(button.actionName);
  160. jEdit.setProperty(button.actionName + ".icon",button.iconName);
  161. }
  162. jEdit.setProperty("view.toolbar",buf.toString());
  163. } //}}}
  164. //{{{ Private members
  165. //{{{ Instance variables
  166. private JCheckBox showToolbar;
  167. private DefaultListModel listModel;
  168. private JList list;
  169. private RolloverButton add;
  170. private RolloverButton remove;
  171. private RolloverButton moveUp, moveDown;
  172. private RolloverButton edit;
  173. private DefaultComboBoxModel iconList;
  174. //}}}
  175. //{{{ updateButtons() method
  176. private void updateButtons()
  177. {
  178. int index = list.getSelectedIndex();
  179. remove.setEnabled(index != -1 && listModel.getSize() != 0);
  180. moveUp.setEnabled(index > 0);
  181. moveDown.setEnabled(index != -1 && index != listModel.getSize() - 1);
  182. edit.setEnabled(index != -1);
  183. } //}}}
  184. //}}}
  185. //{{{ Inner classes
  186. //{{{ ButtonCompare class
  187. static class ButtonCompare implements MiscUtilities.Compare
  188. {
  189. public int compare(Object obj1, Object obj2)
  190. {
  191. return MiscUtilities.compareStrings(
  192. ((Button)obj1).label,
  193. ((Button)obj2).label,
  194. true);
  195. }
  196. } //}}}
  197. //{{{ Button class
  198. static class Button
  199. {
  200. String actionName;
  201. String iconName;
  202. Icon icon;
  203. String label;
  204. Button(String actionName, String iconName, Icon icon, String label)
  205. {
  206. this.actionName = actionName;
  207. this.iconName = iconName;
  208. this.icon = icon;
  209. this.label = GUIUtilities.prettifyMenuLabel(label);
  210. }
  211. public String toString()
  212. {
  213. return label;
  214. }
  215. public boolean equals(Object o)
  216. {
  217. if(o instanceof Button)
  218. return ((Button)o).actionName.equals(actionName);
  219. else
  220. return false;
  221. }
  222. } //}}}
  223. //{{{ IconListEntry class
  224. static class IconListEntry
  225. {
  226. Icon icon;
  227. String name;
  228. IconListEntry(Icon icon, String name)
  229. {
  230. this.icon = icon;
  231. this.name = name;
  232. }
  233. public String toString()
  234. {
  235. return name;
  236. }
  237. } //}}}
  238. //{{{ ButtonCellRenderer class
  239. static class ButtonCellRenderer extends DefaultListCellRenderer
  240. {
  241. public Component getListCellRendererComponent(JList list,
  242. Object value, int index, boolean isSelected,
  243. boolean cellHasFocus)
  244. {
  245. super.getListCellRendererComponent(list,value,index,
  246. isSelected,cellHasFocus);
  247. Button button = (Button)value;
  248. setIcon(button.icon);
  249. return this;
  250. }
  251. } //}}}
  252. //{{{ IconCellRenderer class
  253. static class IconCellRenderer extends DefaultListCellRenderer
  254. {
  255. public Component getListCellRendererComponent(JList list,
  256. Object value, int index, boolean isSelected,
  257. boolean cellHasFocus)
  258. {
  259. super.getListCellRendererComponent(list,value,index,
  260. isSelected,cellHasFocus);
  261. IconListEntry icon = (IconListEntry)value;
  262. setIcon(icon.icon);
  263. return this;
  264. }
  265. } //}}}
  266. //{{{ ActionHandler class
  267. class ActionHandler implements ActionListener
  268. {
  269. public void actionPerformed(ActionEvent evt)
  270. {
  271. Object source = evt.getSource();
  272. if(source == add)
  273. {
  274. ToolBarEditDialog dialog = new ToolBarEditDialog(
  275. ToolBarOptionPane.this,iconList,null);
  276. Button selection = dialog.getSelection();
  277. if(selection == null)
  278. return;
  279. int index = list.getSelectedIndex();
  280. if(index == -1)
  281. index = listModel.getSize();
  282. else
  283. index++;
  284. listModel.insertElementAt(selection,index);
  285. list.setSelectedIndex(index);
  286. list.ensureIndexIsVisible(index);
  287. }
  288. else if(source == remove)
  289. {
  290. int index = list.getSelectedIndex();
  291. listModel.removeElementAt(index);
  292. updateButtons();
  293. }
  294. else if(source == moveUp)
  295. {
  296. int index = list.getSelectedIndex();
  297. Object selected = list.getSelectedValue();
  298. listModel.removeElementAt(index);
  299. listModel.insertElementAt(selected,index-1);
  300. list.setSelectedIndex(index-1);
  301. list.ensureIndexIsVisible(index-1);
  302. }
  303. else if(source == moveDown)
  304. {
  305. int index = list.getSelectedIndex();
  306. Object selected = list.getSelectedValue();
  307. listModel.removeElementAt(index);
  308. listModel.insertElementAt(selected,index+1);
  309. list.setSelectedIndex(index+1);
  310. list.ensureIndexIsVisible(index+1);
  311. }
  312. else if(source == edit)
  313. {
  314. ToolBarEditDialog dialog = new ToolBarEditDialog(
  315. ToolBarOptionPane.this,iconList,
  316. (Button)list.getSelectedValue());
  317. Button selection = dialog.getSelection();
  318. if(selection == null)
  319. return;
  320. int index = list.getSelectedIndex();
  321. listModel.setElementAt(selection,index);
  322. list.setSelectedIndex(index);
  323. list.ensureIndexIsVisible(index);
  324. }
  325. }
  326. } //}}}
  327. //{{{ ListHandler class
  328. class ListHandler implements ListSelectionListener
  329. {
  330. public void valueChanged(ListSelectionEvent evt)
  331. {
  332. updateButtons();
  333. }
  334. } //}}}
  335. //}}}
  336. } //}}}
  337. //{{{ ToolBarEditDialog class
  338. class ToolBarEditDialog extends EnhancedDialog
  339. {
  340. //{{{ ToolBarEditDialog constructor
  341. public ToolBarEditDialog(Component comp,
  342. DefaultComboBoxModel iconListModel,
  343. ToolBarOptionPane.Button current)
  344. {
  345. super(GUIUtilities.getParentDialog(comp),
  346. jEdit.getProperty("options.toolbar.edit.title"),
  347. true);
  348. JPanel content = new JPanel(new BorderLayout());
  349. content.setBorder(new EmptyBorder(12,12,12,12));
  350. setContentPane(content);
  351. ActionHandler actionHandler = new ActionHandler();
  352. ButtonGroup grp = new ButtonGroup();
  353. JPanel typePanel = new JPanel(new GridLayout(3,1,6,6));
  354. typePanel.setBorder(new EmptyBorder(0,0,6,0));
  355. typePanel.add(new JLabel(
  356. jEdit.getProperty("options.toolbar.edit.caption")));
  357. separator = new JRadioButton(jEdit.getProperty("options.toolbar"
  358. + ".edit.separator"));
  359. separator.addActionListener(actionHandler);
  360. grp.add(separator);
  361. typePanel.add(separator);
  362. action = new JRadioButton(jEdit.getProperty("options.toolbar"
  363. + ".edit.action"));
  364. action.addActionListener(actionHandler);
  365. grp.add(action);
  366. typePanel.add(action);
  367. content.add(BorderLayout.NORTH,typePanel);
  368. JPanel actionPanel = new JPanel(new BorderLayout(6,6));
  369. ActionSet[] actionsList = jEdit.getActionSets();
  370. Vector vec = new Vector(actionsList.length);
  371. for(int i = 0; i < actionsList.length; i++)
  372. {
  373. ActionSet actionSet = actionsList[i];
  374. if(actionSet.getActionCount() != 0)
  375. vec.addElement(actionSet);
  376. }
  377. combo = new JComboBox(vec);
  378. combo.addActionListener(actionHandler);
  379. actionPanel.add(BorderLayout.NORTH,combo);
  380. list = new JList();
  381. list.setVisibleRowCount(8);
  382. list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  383. actionPanel.add(BorderLayout.CENTER,new JScrollPane(list));
  384. // Icon selection
  385. JPanel iconPanel = new JPanel(new BorderLayout(0,3));
  386. JPanel labelPanel = new JPanel(new GridLayout(2,1));
  387. labelPanel.setBorder(new EmptyBorder(0,0,0,12));
  388. JPanel compPanel = new JPanel(new GridLayout(2,1));
  389. grp = new ButtonGroup();
  390. labelPanel.add(builtin = new JRadioButton(jEdit.getProperty(
  391. "options.toolbar.edit.builtin")));
  392. builtin.addActionListener(actionHandler);
  393. grp.add(builtin);
  394. labelPanel.add(file = new JRadioButton(jEdit.getProperty(
  395. "options.toolbar.edit.file")));
  396. grp.add(file);
  397. file.addActionListener(actionHandler);
  398. iconPanel.add(BorderLayout.WEST,labelPanel);
  399. builtinCombo = new JComboBox(iconListModel);
  400. builtinCombo.setRenderer(new ToolBarOptionPane.IconCellRenderer());
  401. compPanel.add(builtinCombo);
  402. fileButton = new JButton(jEdit.getProperty("options.toolbar.edit.no-icon"));
  403. fileButton.setMargin(new Insets(1,1,1,1));
  404. fileButton.setIcon(GUIUtilities.loadIcon("Blank24.gif"));
  405. fileButton.setHorizontalAlignment(SwingConstants.LEFT);
  406. fileButton.addActionListener(actionHandler);
  407. compPanel.add(fileButton);
  408. iconPanel.add(BorderLayout.CENTER,compPanel);
  409. actionPanel.add(BorderLayout.SOUTH,iconPanel);
  410. content.add(BorderLayout.CENTER,actionPanel);
  411. JPanel southPanel = new JPanel();
  412. southPanel.setLayout(new BoxLayout(southPanel,BoxLayout.X_AXIS));
  413. southPanel.setBorder(new EmptyBorder(12,0,0,0));
  414. southPanel.add(Box.createGlue());
  415. ok = new JButton(jEdit.getProperty("common.ok"));
  416. ok.addActionListener(actionHandler);
  417. getRootPane().setDefaultButton(ok);
  418. southPanel.add(ok);
  419. southPanel.add(Box.createHorizontalStrut(6));
  420. cancel = new JButton(jEdit.getProperty("common.cancel"));
  421. cancel.addActionListener(actionHandler);
  422. southPanel.add(cancel);
  423. southPanel.add(Box.createGlue());
  424. content.add(BorderLayout.SOUTH,southPanel);
  425. if(current == null)
  426. {
  427. action.setSelected(true);
  428. builtin.setSelected(true);
  429. updateList();
  430. }
  431. else
  432. {
  433. if(current.actionName.equals("-"))
  434. {
  435. separator.setSelected(true);
  436. builtin.setSelected(true);
  437. }
  438. else
  439. {
  440. action.setSelected(true);
  441. ActionSet set = jEdit.getActionSetForAction(
  442. current.actionName);
  443. combo.setSelectedItem(set);
  444. updateList();
  445. list.setSelectedValue(current,true);
  446. if(MiscUtilities.isURL(current.iconName))
  447. {
  448. file.setSelected(true);
  449. fileIcon = current.iconName;
  450. try
  451. {
  452. fileButton.setIcon(new ImageIcon(new URL(
  453. fileIcon)));
  454. }
  455. catch(MalformedURLException mf)
  456. {
  457. Log.log(Log.ERROR,this,mf);
  458. }
  459. fileButton.setText(MiscUtilities.getFileName(fileIcon));
  460. }
  461. else
  462. {
  463. String iconName = MiscUtilities.getFileName(current.iconName);
  464. builtin.setSelected(true);
  465. ListModel model = builtinCombo.getModel();
  466. for(int i = 0; i < model.getSize(); i++)
  467. {
  468. ToolBarOptionPane.IconListEntry entry
  469. = (ToolBarOptionPane.IconListEntry)
  470. model.getElementAt(i);
  471. if(entry.name.equals(iconName))
  472. {
  473. builtinCombo.setSelectedIndex(i);
  474. break;
  475. }
  476. }
  477. }
  478. }
  479. }
  480. updateEnabled();
  481. pack();
  482. setLocationRelativeTo(GUIUtilities.getParentDialog(comp));
  483. show();
  484. } //}}}
  485. //{{{ ok() method
  486. public void ok()
  487. {
  488. isOK = true;
  489. dispose();
  490. } //}}}
  491. //{{{ cancel() method
  492. public void cancel()
  493. {
  494. dispose();
  495. } //}}}
  496. //{{{ getSelection() method
  497. public ToolBarOptionPane.Button getSelection()
  498. {
  499. if(!isOK)
  500. return null;
  501. if(separator.isSelected())
  502. return new ToolBarOptionPane.Button("-",null,null,"-");
  503. else
  504. {
  505. Icon icon;
  506. String iconName;
  507. if(builtin.isSelected())
  508. {
  509. ToolBarOptionPane.IconListEntry selectedIcon =
  510. (ToolBarOptionPane.IconListEntry)
  511. builtinCombo.getSelectedItem();
  512. icon = selectedIcon.icon;
  513. iconName = selectedIcon.name;
  514. }
  515. else
  516. {
  517. icon = fileButton.getIcon();
  518. iconName = fileIcon;
  519. if(iconName == null)
  520. iconName = "Blank24.gif";
  521. }
  522. String label;
  523. String actionName;
  524. if(action.isSelected())
  525. {
  526. ToolBarOptionPane.Button button =
  527. (ToolBarOptionPane.Button)list
  528. .getSelectedValue();
  529. label = button.label;
  530. actionName = button.actionName;
  531. }
  532. else
  533. throw new InternalError();
  534. return new ToolBarOptionPane.Button(actionName,
  535. iconName,icon,label);
  536. }
  537. } //}}}
  538. //{{{ Private members
  539. //{{{ Instance variables
  540. private boolean isOK;
  541. private JRadioButton separator, action;
  542. private JComboBox combo;
  543. private JList list;
  544. private JRadioButton builtin;
  545. private JComboBox builtinCombo;
  546. private JRadioButton file;
  547. private JButton fileButton;
  548. private String fileIcon;
  549. private JButton ok, cancel;
  550. //}}}
  551. //{{{ updateEnabled() method
  552. private void updateEnabled()
  553. {
  554. combo.setEnabled(action.isSelected());
  555. list.setEnabled(action.isSelected());
  556. boolean iconControlsEnabled = !separator.isSelected();
  557. builtin.setEnabled(iconControlsEnabled);
  558. file.setEnabled(iconControlsEnabled);
  559. builtinCombo.setEnabled(iconControlsEnabled && builtin.isSelected());
  560. fileButton.setEnabled(iconControlsEnabled && file.isSelected());
  561. } //}}}
  562. //{{{ updateList() method
  563. private void updateList()
  564. {
  565. ActionSet actionSet = (ActionSet)combo.getSelectedItem();
  566. EditAction[] actions = actionSet.getActions();
  567. Vector listModel = new Vector(actions.length);
  568. for(int i = 0; i < actions.length; i++)
  569. {
  570. EditAction action = actions[i];
  571. String label = action.getLabel();
  572. if(label == null)
  573. continue;
  574. listModel.addElement(new ToolBarOptionPane.Button(
  575. action.getName(),null,null,label));
  576. }
  577. MiscUtilities.quicksort(listModel,new ToolBarOptionPane.ButtonCompare());
  578. list.setListData(listModel);
  579. } //}}}
  580. //}}}
  581. //{{{ ActionHandler class
  582. class ActionHandler implements ActionListener
  583. {
  584. public void actionPerformed(ActionEvent evt)
  585. {
  586. Object source = evt.getSource();
  587. if(source instanceof JRadioButton)
  588. updateEnabled();
  589. if(source == ok)
  590. ok();
  591. else if(source == cancel)
  592. cancel();
  593. else if(source == combo)
  594. updateList();
  595. else if(source == fileButton)
  596. {
  597. String directory;
  598. if(fileIcon == null)
  599. directory = null;
  600. else
  601. directory = MiscUtilities.getParentOfPath(fileIcon);
  602. String paths[] = GUIUtilities.showVFSFileDialog(null,directory,
  603. VFSBrowser.OPEN_DIALOG,false);
  604. if(paths == null)
  605. return;
  606. fileIcon = "file:" + paths[0];
  607. try
  608. {
  609. fileButton.setIcon(new ImageIcon(new URL(
  610. fileIcon)));
  611. }
  612. catch(MalformedURLException mf)
  613. {
  614. Log.log(Log.ERROR,this,mf);
  615. }
  616. fileButton.setText(MiscUtilities.getFileName(fileIcon));
  617. }
  618. }
  619. } //}}}
  620. } //}}}