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

# · Java · 571 lines · 472 code · 70 blank · 29 comment · 64 complexity · bc1ba658fc7eabd0cc5b6fede5fc6bb8 MD5 · raw file

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