/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/options/ToolBarOptionPane.java

# · Java · 703 lines · 558 code · 82 blank · 63 comment · 77 complexity · bdb0b6b242054440a1e5d7f0e739efec MD5 · raw file

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