PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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