001: /*
002: * ToolBarOptionPane.java - Tool bar options panel
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2000, 2001, 2002 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.options;
024:
025: //{{{ Imports
026: import javax.swing.border.*;
027: import javax.swing.event.*;
028: import javax.swing.*;
029: import java.awt.event.*;
030: import java.awt.*;
031: import java.net.*;
032: import java.util.*;
033: import org.gjt.sp.jedit.browser.VFSBrowser;
034: import org.gjt.sp.jedit.gui.*;
035: import org.gjt.sp.jedit.*;
036: import org.gjt.sp.util.Log;
037: import org.gjt.sp.util.StandardUtilities;
038:
039: //}}}
040:
041: //{{{ ToolBarOptionPane class
042: /**
043: * Tool bar editor.
044: * @author Slava Pestov
045: * @version $Id: ToolBarOptionPane.java 10702 2007-09-21 10:29:55Z kpouer $
046: */
047: public class ToolBarOptionPane extends AbstractOptionPane {
048: //{{{ ToolBarOptionPane constructor
049: public ToolBarOptionPane() {
050: super ("toolbar");
051: } //}}}
052:
053: //{{{ _init() method
054: protected void _init() {
055: setLayout(new BorderLayout());
056:
057: JPanel panel = new JPanel(new GridLayout(2, 1));
058:
059: /* Show toolbar */
060: showToolbar = new JCheckBox(jEdit
061: .getProperty("options.toolbar.showToolbar"));
062: showToolbar.setSelected(jEdit
063: .getBooleanProperty("view.showToolbar"));
064: panel.add(showToolbar);
065:
066: panel.add(new JLabel(jEdit
067: .getProperty("options.toolbar.caption")));
068:
069: add(BorderLayout.NORTH, panel);
070:
071: String toolbar = jEdit.getProperty("view.toolbar");
072: StringTokenizer st = new StringTokenizer(toolbar);
073: listModel = new DefaultListModel();
074: while (st.hasMoreTokens()) {
075: String actionName = st.nextToken();
076: if (actionName.equals("-"))
077: listModel.addElement(new ToolBarOptionPane.Button("-",
078: null, null, "-"));
079: else {
080: EditAction action = jEdit.getAction(actionName);
081: if (action == null)
082: continue;
083: String label = action.getLabel();
084: if (label == null)
085: continue;
086:
087: Icon icon;
088: String iconName;
089: if (actionName.equals("-")) {
090: iconName = null;
091: icon = null;
092: } else {
093: iconName = jEdit.getProperty(actionName + ".icon");
094: if (iconName == null)
095: icon = GUIUtilities.loadIcon("BrokenImage.png");
096: else {
097: icon = GUIUtilities.loadIcon(iconName);
098: if (icon == null)
099: icon = GUIUtilities
100: .loadIcon("BrokenImage.png");
101: }
102: }
103: listModel.addElement(new Button(actionName, iconName,
104: icon, label));
105: }
106: }
107:
108: list = new JList(listModel);
109: list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
110: list.addListSelectionListener(new ListHandler());
111: list.setCellRenderer(new ButtonCellRenderer());
112:
113: add(BorderLayout.CENTER, new JScrollPane(list));
114:
115: //{{{ Create buttons
116: JPanel buttons = new JPanel();
117: buttons.setBorder(new EmptyBorder(3, 0, 0, 0));
118: buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
119: ActionHandler actionHandler = new ActionHandler();
120: add = new RolloverButton(GUIUtilities.loadIcon("Plus.png"));
121: add.setToolTipText(jEdit.getProperty("options.toolbar.add"));
122: add.addActionListener(actionHandler);
123: buttons.add(add);
124: buttons.add(Box.createHorizontalStrut(6));
125: remove = new RolloverButton(GUIUtilities.loadIcon("Minus.png"));
126: remove.setToolTipText(jEdit
127: .getProperty("options.toolbar.remove"));
128: remove.addActionListener(actionHandler);
129: buttons.add(remove);
130: buttons.add(Box.createHorizontalStrut(6));
131: moveUp = new RolloverButton(GUIUtilities.loadIcon("ArrowU.png"));
132: moveUp.setToolTipText(jEdit
133: .getProperty("options.toolbar.moveUp"));
134: moveUp.addActionListener(actionHandler);
135: buttons.add(moveUp);
136: buttons.add(Box.createHorizontalStrut(6));
137: moveDown = new RolloverButton(GUIUtilities
138: .loadIcon("ArrowD.png"));
139: moveDown.setToolTipText(jEdit
140: .getProperty("options.toolbar.moveDown"));
141: moveDown.addActionListener(actionHandler);
142: buttons.add(moveDown);
143: buttons.add(Box.createHorizontalStrut(6));
144: edit = new RolloverButton(GUIUtilities
145: .loadIcon("ButtonProperties.png"));
146: edit.setToolTipText(jEdit.getProperty("options.toolbar.edit"));
147: edit.addActionListener(actionHandler);
148: buttons.add(edit);
149: buttons.add(Box.createGlue());
150: //}}}
151:
152: updateButtons();
153: add(BorderLayout.SOUTH, buttons);
154:
155: //{{{ Ceate icons list
156: iconList = new DefaultComboBoxModel();
157: st = new StringTokenizer(jEdit.getProperty("icons"));
158: while (st.hasMoreElements()) {
159: String icon = st.nextToken();
160: iconList.addElement(new IconListEntry(GUIUtilities
161: .loadIcon(icon), icon));
162: } //}}}
163: } ///}}}
164:
165: //{{{ _save() method
166: protected void _save() {
167: jEdit.setBooleanProperty("view.showToolbar", showToolbar
168: .isSelected());
169:
170: StringBuilder buf = new StringBuilder();
171: for (int i = 0; i < listModel.getSize(); i++) {
172: if (i != 0)
173: buf.append(' ');
174: Button button = (Button) listModel.elementAt(i);
175: buf.append(button.actionName);
176: jEdit.setProperty(button.actionName + ".icon",
177: button.iconName);
178: }
179: jEdit.setProperty("view.toolbar", buf.toString());
180: } //}}}
181:
182: //{{{ Private members
183:
184: //{{{ Instance variables
185: private JCheckBox showToolbar;
186: private DefaultListModel listModel;
187: private JList list;
188: private RolloverButton add;
189: private RolloverButton remove;
190: private RolloverButton moveUp, moveDown;
191: private RolloverButton edit;
192:
193: private DefaultComboBoxModel iconList;
194:
195: //}}}
196:
197: //{{{ updateButtons() method
198: private void updateButtons() {
199: int index = list.getSelectedIndex();
200: remove.setEnabled(index != -1 && listModel.getSize() != 0);
201: moveUp.setEnabled(index > 0);
202: moveDown.setEnabled(index != -1
203: && index != listModel.getSize() - 1);
204: edit.setEnabled(index != -1);
205: } //}}}
206:
207: //}}}
208:
209: //{{{ Inner classes
210:
211: //{{{ ButtonCompare class
212: static class ButtonCompare implements Comparator<Button> {
213: public int compare(Button button1, Button button2) {
214: return StandardUtilities.compareStrings(button1.label,
215: button2.label, true);
216: }
217: } //}}}
218:
219: //{{{ Button class
220: static class Button {
221: String actionName;
222: String iconName;
223: Icon icon;
224: String label;
225:
226: Button(String actionName, String iconName, Icon icon,
227: String label) {
228: this .actionName = actionName;
229: this .iconName = iconName;
230: this .icon = icon;
231: this .label = GUIUtilities.prettifyMenuLabel(label);
232: }
233:
234: public String toString() {
235: return label;
236: }
237:
238: public boolean equals(Object o) {
239: if (o instanceof Button)
240: return ((Button) o).actionName.equals(actionName);
241: else
242: return false;
243: }
244: } //}}}
245:
246: //{{{ IconListEntry class
247: static class IconListEntry {
248: Icon icon;
249: String name;
250:
251: IconListEntry(Icon icon, String name) {
252: this .icon = icon;
253: this .name = name;
254: }
255:
256: public String toString() {
257: return name;
258: }
259: } //}}}
260:
261: //{{{ ButtonCellRenderer class
262: static class ButtonCellRenderer extends DefaultListCellRenderer {
263: public Component getListCellRendererComponent(JList list,
264: Object value, int index, boolean isSelected,
265: boolean cellHasFocus) {
266: super .getListCellRendererComponent(list, value, index,
267: isSelected, cellHasFocus);
268:
269: Button button = (Button) value;
270: setIcon(button.icon);
271:
272: return this ;
273: }
274: } //}}}
275:
276: //{{{ IconCellRenderer class
277: static class IconCellRenderer extends DefaultListCellRenderer {
278: public Component getListCellRendererComponent(JList list,
279: Object value, int index, boolean isSelected,
280: boolean cellHasFocus) {
281: super .getListCellRendererComponent(list, value, index,
282: isSelected, cellHasFocus);
283:
284: IconListEntry icon = (IconListEntry) value;
285: setIcon(icon.icon);
286:
287: return this ;
288: }
289: } //}}}
290:
291: //{{{ ActionHandler class
292: class ActionHandler implements ActionListener {
293: public void actionPerformed(ActionEvent evt) {
294: Object source = evt.getSource();
295:
296: if (source == add) {
297: ToolBarEditDialog dialog = new ToolBarEditDialog(
298: ToolBarOptionPane.this , iconList, null);
299: Button selection = dialog.getSelection();
300: if (selection == null)
301: return;
302:
303: int index = list.getSelectedIndex();
304: if (index == -1)
305: index = listModel.getSize();
306: else
307: index++;
308:
309: listModel.insertElementAt(selection, index);
310: list.setSelectedIndex(index);
311: list.ensureIndexIsVisible(index);
312: } else if (source == remove) {
313: int index = list.getSelectedIndex();
314: listModel.removeElementAt(index);
315: if (listModel.getSize() != 0) {
316: if (listModel.getSize() == index)
317: list.setSelectedIndex(index - 1);
318: else
319: list.setSelectedIndex(index);
320: }
321: updateButtons();
322: } else if (source == moveUp) {
323: int index = list.getSelectedIndex();
324: Object selected = list.getSelectedValue();
325: listModel.removeElementAt(index);
326: listModel.insertElementAt(selected, index - 1);
327: list.setSelectedIndex(index - 1);
328: list.ensureIndexIsVisible(index - 1);
329: } else if (source == moveDown) {
330: int index = list.getSelectedIndex();
331: Object selected = list.getSelectedValue();
332: listModel.removeElementAt(index);
333: listModel.insertElementAt(selected, index + 1);
334: list.setSelectedIndex(index + 1);
335: list.ensureIndexIsVisible(index + 1);
336: } else if (source == edit) {
337: ToolBarEditDialog dialog = new ToolBarEditDialog(
338: ToolBarOptionPane.this , iconList, (Button) list
339: .getSelectedValue());
340: Button selection = dialog.getSelection();
341: if (selection == null)
342: return;
343:
344: int index = list.getSelectedIndex();
345:
346: listModel.setElementAt(selection, index);
347: list.setSelectedIndex(index);
348: list.ensureIndexIsVisible(index);
349: }
350: }
351: } //}}}
352:
353: //{{{ ListHandler class
354: class ListHandler implements ListSelectionListener {
355: public void valueChanged(ListSelectionEvent evt) {
356: updateButtons();
357: }
358: } //}}}
359:
360: //}}}
361: } //}}}
362:
363: //{{{ ToolBarEditDialog class
364: class ToolBarEditDialog extends EnhancedDialog {
365: //{{{ ToolBarEditDialog constructor
366: public ToolBarEditDialog(Component comp,
367: DefaultComboBoxModel iconListModel,
368: ToolBarOptionPane.Button current) {
369: super (GUIUtilities.getParentDialog(comp), jEdit
370: .getProperty("options.toolbar.edit.title"), true);
371:
372: JPanel content = new JPanel(new BorderLayout());
373: content.setBorder(new EmptyBorder(12, 12, 12, 12));
374: setContentPane(content);
375:
376: ActionHandler actionHandler = new ActionHandler();
377: ButtonGroup grp = new ButtonGroup();
378:
379: JPanel typePanel = new JPanel(new GridLayout(3, 1, 6, 6));
380: typePanel.setBorder(new EmptyBorder(0, 0, 6, 0));
381: typePanel.add(new JLabel(jEdit
382: .getProperty("options.toolbar.edit.caption")));
383:
384: separator = new JRadioButton(jEdit
385: .getProperty("options.toolbar" + ".edit.separator"));
386: separator.addActionListener(actionHandler);
387: grp.add(separator);
388: typePanel.add(separator);
389:
390: action = new JRadioButton(jEdit.getProperty("options.toolbar"
391: + ".edit.action"));
392: action.addActionListener(actionHandler);
393: grp.add(action);
394: typePanel.add(action);
395:
396: content.add(BorderLayout.NORTH, typePanel);
397:
398: JPanel actionPanel = new JPanel(new BorderLayout(6, 6));
399:
400: ActionSet[] actionsList = jEdit.getActionSets();
401: String selectedActionSet = jEdit
402: .getProperty("options.toolbar.selectedActionSet");
403: ActionSet selectedItem = null;
404: Vector<ActionSet> vec = new Vector<ActionSet>(
405: actionsList.length);
406: for (int i = 0; i < actionsList.length; i++) {
407: ActionSet actionSet = actionsList[i];
408: if (actionSet.getActionCount() != 0) {
409: vec.add(actionSet);
410: if (actionSet.getLabel().equals(selectedActionSet)) {
411: selectedItem = actionSet;
412: }
413: }
414: }
415: combo = new JComboBox(vec);
416: if (selectedItem != null)
417: combo.setSelectedItem(selectedItem);
418: else
419: jEdit.unsetProperty("options.toolbar.selectedActionSet");
420: combo.addActionListener(actionHandler);
421:
422: actionPanel.add(BorderLayout.NORTH, combo);
423:
424: list = new JList();
425: list.setVisibleRowCount(8);
426: list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
427: actionPanel.add(BorderLayout.CENTER, new JScrollPane(list));
428:
429: // Icon selection
430: JPanel iconPanel = new JPanel(new BorderLayout(0, 3));
431: JPanel labelPanel = new JPanel(new GridLayout(2, 1));
432: labelPanel.setBorder(new EmptyBorder(0, 0, 0, 12));
433: JPanel compPanel = new JPanel(new GridLayout(2, 1));
434: grp = new ButtonGroup();
435: labelPanel.add(builtin = new JRadioButton(jEdit
436: .getProperty("options.toolbar.edit.builtin")));
437: builtin.addActionListener(actionHandler);
438: grp.add(builtin);
439: labelPanel.add(file = new JRadioButton(jEdit
440: .getProperty("options.toolbar.edit.file")));
441: grp.add(file);
442: file.addActionListener(actionHandler);
443: iconPanel.add(BorderLayout.WEST, labelPanel);
444: builtinCombo = new JComboBox(iconListModel);
445: builtinCombo
446: .setRenderer(new ToolBarOptionPane.IconCellRenderer());
447: compPanel.add(builtinCombo);
448:
449: fileButton = new JButton(jEdit
450: .getProperty("options.toolbar.edit.no-icon"));
451: fileButton.setMargin(new Insets(1, 1, 1, 1));
452: fileButton.setIcon(GUIUtilities.loadIcon("Blank24.gif"));
453: fileButton.setHorizontalAlignment(SwingConstants.LEFT);
454: fileButton.addActionListener(actionHandler);
455: compPanel.add(fileButton);
456: iconPanel.add(BorderLayout.CENTER, compPanel);
457: actionPanel.add(BorderLayout.SOUTH, iconPanel);
458:
459: content.add(BorderLayout.CENTER, actionPanel);
460:
461: JPanel southPanel = new JPanel();
462: southPanel
463: .setLayout(new BoxLayout(southPanel, BoxLayout.X_AXIS));
464: southPanel.setBorder(new EmptyBorder(12, 0, 0, 0));
465: southPanel.add(Box.createGlue());
466: ok = new JButton(jEdit.getProperty("common.ok"));
467: ok.addActionListener(actionHandler);
468: getRootPane().setDefaultButton(ok);
469: southPanel.add(ok);
470: southPanel.add(Box.createHorizontalStrut(6));
471: cancel = new JButton(jEdit.getProperty("common.cancel"));
472: cancel.addActionListener(actionHandler);
473: southPanel.add(cancel);
474: southPanel.add(Box.createGlue());
475:
476: content.add(BorderLayout.SOUTH, southPanel);
477:
478: if (current == null) {
479: action.setSelected(true);
480: builtin.setSelected(true);
481: updateList();
482: } else {
483: if (current.actionName.equals("-")) {
484: separator.setSelected(true);
485: builtin.setSelected(true);
486: } else {
487: action.setSelected(true);
488: ActionSet set = jEdit
489: .getActionSetForAction(current.actionName);
490: combo.setSelectedItem(set);
491: updateList();
492: list.setSelectedValue(current, true);
493:
494: if (MiscUtilities.isURL(current.iconName)) {
495: file.setSelected(true);
496: fileIcon = current.iconName;
497: try {
498: fileButton.setIcon(new ImageIcon(new URL(
499: fileIcon)));
500: } catch (MalformedURLException mf) {
501: Log.log(Log.ERROR, this , mf);
502: }
503: fileButton.setText(MiscUtilities
504: .getFileName(fileIcon));
505: } else {
506: String iconName = MiscUtilities
507: .getFileName(current.iconName);
508: builtin.setSelected(true);
509: ListModel model = builtinCombo.getModel();
510: for (int i = 0; i < model.getSize(); i++) {
511: ToolBarOptionPane.IconListEntry entry = (ToolBarOptionPane.IconListEntry) model
512: .getElementAt(i);
513: if (entry.name.equals(iconName)) {
514: builtinCombo.setSelectedIndex(i);
515: break;
516: }
517: }
518: }
519: }
520: }
521:
522: updateEnabled();
523:
524: pack();
525: setLocationRelativeTo(GUIUtilities.getParentDialog(comp));
526: setVisible(true);
527: } //}}}
528:
529: //{{{ ok() method
530: public void ok() {
531: isOK = true;
532: dispose();
533: } //}}}
534:
535: //{{{ cancel() method
536: public void cancel() {
537: dispose();
538: } //}}}
539:
540: //{{{ getSelection() method
541: public ToolBarOptionPane.Button getSelection() {
542: if (!isOK)
543: return null;
544:
545: if (separator.isSelected())
546: return new ToolBarOptionPane.Button("-", null, null, "-");
547: else {
548: Icon icon;
549: String iconName;
550: if (builtin.isSelected()) {
551: ToolBarOptionPane.IconListEntry selectedIcon = (ToolBarOptionPane.IconListEntry) builtinCombo
552: .getSelectedItem();
553: icon = selectedIcon.icon;
554: iconName = selectedIcon.name;
555: } else {
556: icon = fileButton.getIcon();
557: iconName = fileIcon;
558: if (iconName == null)
559: iconName = "Blank24.gif";
560: }
561:
562: String label;
563: String actionName;
564: if (action.isSelected()) {
565: ToolBarOptionPane.Button button = (ToolBarOptionPane.Button) list
566: .getSelectedValue();
567: label = button.label;
568: actionName = button.actionName;
569: } else
570: throw new InternalError();
571:
572: return new ToolBarOptionPane.Button(actionName, iconName,
573: icon, label);
574: }
575: } //}}}
576:
577: //{{{ Private members
578:
579: //{{{ Instance variables
580: private boolean isOK;
581: private JRadioButton separator, action;
582: private JComboBox combo;
583: private JList list;
584: private JRadioButton builtin;
585: private JComboBox builtinCombo;
586: private JRadioButton file;
587: private JButton fileButton;
588: private String fileIcon;
589: private JButton ok, cancel;
590:
591: //}}}
592:
593: //{{{ updateEnabled() method
594: private void updateEnabled() {
595: combo.setEnabled(action.isSelected());
596: list.setEnabled(action.isSelected());
597:
598: boolean iconControlsEnabled = !separator.isSelected();
599: builtin.setEnabled(iconControlsEnabled);
600: file.setEnabled(iconControlsEnabled);
601: builtinCombo.setEnabled(iconControlsEnabled
602: && builtin.isSelected());
603: fileButton.setEnabled(iconControlsEnabled && file.isSelected());
604: } //}}}
605:
606: //{{{ updateList() method
607: private void updateList() {
608: ActionSet actionSet = (ActionSet) combo.getSelectedItem();
609: String actionSetLabel = actionSet.getLabel();
610: jEdit.setProperty("options.toolbar.selectedActionSet",
611: actionSetLabel);
612: EditAction[] actions = actionSet.getActions();
613: Vector<ToolBarOptionPane.Button> listModel = new Vector<ToolBarOptionPane.Button>(
614: actions.length);
615:
616: for (int i = 0; i < actions.length; i++) {
617: EditAction action = actions[i];
618: String label = action.getLabel();
619: if (label == null)
620: continue;
621:
622: listModel.add(new ToolBarOptionPane.Button(
623: action.getName(), null, null, label));
624: }
625:
626: Collections.sort(listModel,
627: new ToolBarOptionPane.ButtonCompare());
628: list.setListData(listModel);
629: } //}}}
630:
631: //}}}
632:
633: //{{{ ActionHandler class
634: class ActionHandler implements ActionListener {
635: public void actionPerformed(ActionEvent evt) {
636: Object source = evt.getSource();
637: if (source instanceof JRadioButton)
638: updateEnabled();
639: if (source == ok)
640: ok();
641: else if (source == cancel)
642: cancel();
643: else if (source == combo)
644: updateList();
645: else if (source == fileButton) {
646: String directory;
647: if (fileIcon == null)
648: directory = null;
649: else
650: directory = MiscUtilities.getParentOfPath(fileIcon);
651: String[] paths = GUIUtilities.showVFSFileDialog(null,
652: directory, VFSBrowser.OPEN_DIALOG, false);
653: if (paths == null)
654: return;
655:
656: fileIcon = "file:" + paths[0];
657:
658: try {
659: fileButton
660: .setIcon(new ImageIcon(new URL(fileIcon)));
661: } catch (MalformedURLException mf) {
662: Log.log(Log.ERROR, this , mf);
663: }
664: fileButton.setText(MiscUtilities.getFileName(fileIcon));
665: }
666: }
667: } //}}}
668: } //}}}
|