001: /*
002: * ContextOptionPane.java - Context menu options panel
003: * Copyright (C) 2000, 2001 Slava Pestov
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */
019:
020: package org.gjt.sp.jedit.options;
021:
022: import javax.swing.border.*;
023: import javax.swing.event.*;
024: import javax.swing.*;
025: import java.awt.event.*;
026: import java.awt.*;
027: import java.util.*;
028: import org.gjt.sp.jedit.gui.*;
029: import org.gjt.sp.jedit.*;
030: import org.gjt.sp.util.StandardUtilities;
031:
032: /**
033: * Right-click context menu editor.
034: * @author Slava Pestov
035: * @version $Id: ContextOptionPane.java 5570 2006-07-11 09:27:07Z kpouer $
036: */
037: public class ContextOptionPane extends AbstractOptionPane {
038: public ContextOptionPane() {
039: super ("context");
040: }
041:
042: // protected members
043: protected void _init() {
044: setLayout(new BorderLayout());
045:
046: JLabel caption = new JLabel(jEdit
047: .getProperty("options.context.caption"));
048: add(BorderLayout.NORTH, caption);
049:
050: String contextMenu = jEdit.getProperty("view.context");
051: StringTokenizer st = new StringTokenizer(contextMenu);
052: listModel = new DefaultListModel();
053: while (st.hasMoreTokens()) {
054: String actionName = st.nextToken();
055: if (actionName.equals("-"))
056: listModel.addElement(new ContextOptionPane.MenuItem(
057: "-", "-"));
058: else {
059: EditAction action = jEdit.getAction(actionName);
060: if (action == null)
061: continue;
062: String label = action.getLabel();
063: if (label == null)
064: continue;
065: listModel.addElement(new ContextOptionPane.MenuItem(
066: actionName, label));
067: }
068: }
069: list = new JList(listModel);
070: list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
071: list.addListSelectionListener(new ListHandler());
072:
073: add(BorderLayout.CENTER, new JScrollPane(list));
074:
075: JPanel buttons = new JPanel();
076: buttons.setBorder(new EmptyBorder(3, 0, 0, 0));
077: buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
078: ActionHandler actionHandler = new ActionHandler();
079: add = new RolloverButton(GUIUtilities.loadIcon("Plus.png"));
080: add.setToolTipText(jEdit.getProperty("common.add"));
081: add.addActionListener(actionHandler);
082: buttons.add(add);
083: buttons.add(Box.createHorizontalStrut(6));
084: remove = new RolloverButton(GUIUtilities.loadIcon("Minus.png"));
085: remove.setToolTipText(jEdit.getProperty("common.remove"));
086: remove.addActionListener(actionHandler);
087: buttons.add(remove);
088: buttons.add(Box.createHorizontalStrut(6));
089: moveUp = new RolloverButton(GUIUtilities.loadIcon("ArrowU.png"));
090: moveUp.setToolTipText(jEdit.getProperty("common.moveUp"));
091: moveUp.addActionListener(actionHandler);
092: buttons.add(moveUp);
093: buttons.add(Box.createHorizontalStrut(6));
094: moveDown = new RolloverButton(GUIUtilities
095: .loadIcon("ArrowD.png"));
096: moveDown.setToolTipText(jEdit.getProperty("common.moveDown"));
097: moveDown.addActionListener(actionHandler);
098: buttons.add(moveDown);
099: buttons.add(Box.createGlue());
100:
101: updateButtons();
102: add(BorderLayout.SOUTH, buttons);
103: }
104:
105: static class MenuItemCompare implements Comparator {
106: public int compare(Object obj1, Object obj2) {
107: return StandardUtilities.compareStrings(
108: ((MenuItem) obj1).label, ((MenuItem) obj2).label,
109: true);
110: }
111: }
112:
113: protected void _save() {
114: StringBuffer buf = new StringBuffer();
115: for (int i = 0; i < listModel.getSize(); i++) {
116: if (i != 0)
117: buf.append(' ');
118: buf.append(((MenuItem) listModel.elementAt(i)).actionName);
119: }
120: jEdit.setProperty("view.context", buf.toString());
121: }
122:
123: // private members
124: private DefaultListModel listModel;
125: private JList list;
126: private JButton add;
127: private JButton remove;
128: private JButton moveUp, moveDown;
129:
130: private void updateButtons() {
131: int index = list.getSelectedIndex();
132: remove.setEnabled(index != -1 && listModel.getSize() != 0);
133: moveUp.setEnabled(index > 0);
134: moveDown.setEnabled(index != -1
135: && index != listModel.getSize() - 1);
136: }
137:
138: static class MenuItem {
139: String actionName;
140: String label;
141:
142: MenuItem(String actionName, String label) {
143: this .actionName = actionName;
144: this .label = GUIUtilities.prettifyMenuLabel(label);
145: }
146:
147: public String toString() {
148: return label;
149: }
150: }
151:
152: class ActionHandler implements ActionListener {
153: public void actionPerformed(ActionEvent evt) {
154: Object source = evt.getSource();
155:
156: if (source == add) {
157: ContextAddDialog dialog = new ContextAddDialog(
158: ContextOptionPane.this );
159: String selection = dialog.getSelection();
160: if (selection == null)
161: return;
162:
163: int index = list.getSelectedIndex();
164: if (index == -1)
165: index = listModel.getSize();
166: else
167: index++;
168:
169: MenuItem menuItem;
170: if (selection.equals("-"))
171: menuItem = new ContextOptionPane.MenuItem("-", "-");
172: else {
173: menuItem = new ContextOptionPane.MenuItem(
174: selection, jEdit.getAction(selection)
175: .getLabel());
176: }
177:
178: listModel.insertElementAt(menuItem, index);
179: list.setSelectedIndex(index);
180: list.ensureIndexIsVisible(index);
181: } else if (source == remove) {
182: int index = list.getSelectedIndex();
183: listModel.removeElementAt(index);
184: if (listModel.getSize() != 0) {
185: list.setSelectedIndex(Math.min(
186: listModel.getSize() - 1, index));
187: }
188: updateButtons();
189: } else if (source == moveUp) {
190: int index = list.getSelectedIndex();
191: Object selected = list.getSelectedValue();
192: listModel.removeElementAt(index);
193: listModel.insertElementAt(selected, index - 1);
194: list.setSelectedIndex(index - 1);
195: list.ensureIndexIsVisible(index - 1);
196: } else if (source == moveDown) {
197: int index = list.getSelectedIndex();
198: Object selected = list.getSelectedValue();
199: listModel.removeElementAt(index);
200: listModel.insertElementAt(selected, index + 1);
201: list.setSelectedIndex(index + 1);
202: list.ensureIndexIsVisible(index + 1);
203: }
204: }
205: }
206:
207: class ListHandler implements ListSelectionListener {
208: public void valueChanged(ListSelectionEvent evt) {
209: updateButtons();
210: }
211: }
212: }
213:
214: class ContextAddDialog extends EnhancedDialog {
215: public ContextAddDialog(Component comp) {
216: super (GUIUtilities.getParentDialog(comp), jEdit
217: .getProperty("options.context.add.title"), true);
218:
219: JPanel content = new JPanel(new BorderLayout());
220: content.setBorder(new EmptyBorder(12, 12, 12, 12));
221: setContentPane(content);
222:
223: ActionHandler actionHandler = new ActionHandler();
224: ButtonGroup grp = new ButtonGroup();
225:
226: JPanel typePanel = new JPanel(new GridLayout(3, 1, 6, 6));
227: typePanel.setBorder(new EmptyBorder(0, 0, 6, 0));
228: typePanel.add(new JLabel(jEdit
229: .getProperty("options.context.add.caption")));
230:
231: separator = new JRadioButton(jEdit
232: .getProperty("options.context" + ".add.separator"));
233: separator.addActionListener(actionHandler);
234: grp.add(separator);
235: typePanel.add(separator);
236:
237: action = new JRadioButton(jEdit.getProperty("options.context"
238: + ".add.action"));
239: action.addActionListener(actionHandler);
240: grp.add(action);
241: action.setSelected(true);
242: typePanel.add(action);
243:
244: content.add(BorderLayout.NORTH, typePanel);
245:
246: JPanel actionPanel = new JPanel(new BorderLayout(6, 6));
247:
248: ActionSet[] actionsList = jEdit.getActionSets();
249: Vector vec = new Vector(actionsList.length);
250: for (int i = 0; i < actionsList.length; i++) {
251: ActionSet actionSet = actionsList[i];
252: if (actionSet.getActionCount() != 0)
253: vec.addElement(actionSet);
254: }
255: combo = new JComboBox(vec);
256: combo.addActionListener(actionHandler);
257: actionPanel.add(BorderLayout.NORTH, combo);
258:
259: list = new JList();
260: list.setVisibleRowCount(8);
261: list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
262: actionPanel.add(BorderLayout.CENTER, new JScrollPane(list));
263:
264: content.add(BorderLayout.CENTER, actionPanel);
265:
266: JPanel southPanel = new JPanel();
267: southPanel
268: .setLayout(new BoxLayout(southPanel, BoxLayout.X_AXIS));
269: southPanel.setBorder(new EmptyBorder(12, 0, 0, 0));
270: southPanel.add(Box.createGlue());
271: ok = new JButton(jEdit.getProperty("common.ok"));
272: ok.addActionListener(actionHandler);
273: getRootPane().setDefaultButton(ok);
274: southPanel.add(ok);
275: southPanel.add(Box.createHorizontalStrut(6));
276: cancel = new JButton(jEdit.getProperty("common.cancel"));
277: cancel.addActionListener(actionHandler);
278: southPanel.add(cancel);
279: southPanel.add(Box.createGlue());
280:
281: content.add(BorderLayout.SOUTH, southPanel);
282:
283: updateList();
284:
285: pack();
286: setLocationRelativeTo(GUIUtilities.getParentDialog(comp));
287: setVisible(true);
288: }
289:
290: public void ok() {
291: isOK = true;
292: dispose();
293: }
294:
295: public void cancel() {
296: dispose();
297: }
298:
299: public String getSelection() {
300: if (!isOK)
301: return null;
302:
303: if (separator.isSelected())
304: return "-";
305: else if (action.isSelected()) {
306: return ((ContextOptionPane.MenuItem) list
307: .getSelectedValue()).actionName;
308: } else
309: throw new InternalError();
310: }
311:
312: // private members
313: private boolean isOK;
314: private JRadioButton separator, action;
315: private JComboBox combo;
316: private JList list;
317: private JButton ok, cancel;
318:
319: private void updateList() {
320: ActionSet actionSet = (ActionSet) combo.getSelectedItem();
321: EditAction[] actions = actionSet.getActions();
322: Vector listModel = new Vector(actions.length);
323:
324: for (int i = 0; i < actions.length; i++) {
325: EditAction action = actions[i];
326: String label = action.getLabel();
327: if (label == null)
328: continue;
329:
330: listModel.addElement(new ContextOptionPane.MenuItem(action
331: .getName(), label));
332: }
333:
334: Collections.sort(listModel,
335: new ContextOptionPane.MenuItemCompare());
336:
337: list.setListData(listModel);
338: }
339:
340: class ActionHandler implements ActionListener {
341: public void actionPerformed(ActionEvent evt) {
342: Object source = evt.getSource();
343: if (source instanceof JRadioButton) {
344: combo.setEnabled(action.isSelected());
345: list.setEnabled(action.isSelected());
346: }
347: if (source == ok)
348: ok();
349: else if (source == cancel)
350: cancel();
351: else if (source == combo)
352: updateList();
353: }
354: }
355: }
|