KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > options > ContextOptionPane


1 /*
2  * ContextOptionPane.java - Context menu 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
20 package org.gjt.sp.jedit.options;
21
22 import javax.swing.border.*;
23 import javax.swing.event.*;
24 import javax.swing.*;
25 import java.awt.event.*;
26 import java.awt.*;
27 import java.util.*;
28 import org.gjt.sp.jedit.gui.*;
29 import org.gjt.sp.jedit.*;
30 import org.gjt.sp.util.StandardUtilities;
31
32 /**
33  * Right-click context menu editor.
34  * @author Slava Pestov
35  * @version $Id: ContextOptionPane.java 5570 2006-07-11 09:27:07Z kpouer $
36  */

37 public class ContextOptionPane extends AbstractOptionPane
38 {
39     public ContextOptionPane()
40     {
41         super("context");
42     }
43
44     // protected members
45
protected void _init()
46     {
47         setLayout(new BorderLayout());
48
49         JLabel caption = new JLabel(jEdit.getProperty(
50             "options.context.caption"));
51         add(BorderLayout.NORTH,caption);
52
53         String JavaDoc contextMenu = jEdit.getProperty("view.context");
54         StringTokenizer st = new StringTokenizer(contextMenu);
55         listModel = new DefaultListModel();
56         while(st.hasMoreTokens())
57         {
58             String JavaDoc actionName = st.nextToken();
59             if(actionName.equals("-"))
60                 listModel.addElement(new ContextOptionPane.MenuItem("-","-"));
61             else
62             {
63                 EditAction action = jEdit.getAction(actionName);
64                 if(action == null)
65                     continue;
66                 String JavaDoc label = action.getLabel();
67                 if(label == null)
68                     continue;
69                 listModel.addElement(new ContextOptionPane.MenuItem(actionName,label));
70             }
71         }
72         list = new JList(listModel);
73         list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
74         list.addListSelectionListener(new ListHandler());
75
76         add(BorderLayout.CENTER,new JScrollPane(list));
77
78         JPanel buttons = new JPanel();
79         buttons.setBorder(new EmptyBorder(3,0,0,0));
80         buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
81         ActionHandler actionHandler = new ActionHandler();
82         add = new RolloverButton(GUIUtilities.loadIcon("Plus.png"));
83         add.setToolTipText(jEdit.getProperty("common.add"));
84         add.addActionListener(actionHandler);
85         buttons.add(add);
86         buttons.add(Box.createHorizontalStrut(6));
87         remove = new RolloverButton(GUIUtilities.loadIcon("Minus.png"));
88         remove.setToolTipText(jEdit.getProperty("common.remove"));
89         remove.addActionListener(actionHandler);
90         buttons.add(remove);
91         buttons.add(Box.createHorizontalStrut(6));
92         moveUp = new RolloverButton(GUIUtilities.loadIcon("ArrowU.png"));
93         moveUp.setToolTipText(jEdit.getProperty("common.moveUp"));
94         moveUp.addActionListener(actionHandler);
95         buttons.add(moveUp);
96         buttons.add(Box.createHorizontalStrut(6));
97         moveDown = new RolloverButton(GUIUtilities.loadIcon("ArrowD.png"));
98         moveDown.setToolTipText(jEdit.getProperty("common.moveDown"));
99         moveDown.addActionListener(actionHandler);
100         buttons.add(moveDown);
101         buttons.add(Box.createGlue());
102
103         updateButtons();
104         add(BorderLayout.SOUTH,buttons);
105     }
106
107     static class MenuItemCompare implements Comparator
108     {
109         public int compare(Object JavaDoc obj1, Object JavaDoc obj2)
110         {
111             return StandardUtilities.compareStrings(
112                 ((MenuItem)obj1).label,
113                 ((MenuItem)obj2).label,
114                 true);
115         }
116     }
117
118     protected void _save()
119     {
120         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
121         for(int i = 0; i < listModel.getSize(); i++)
122         {
123             if(i != 0)
124                 buf.append(' ');
125             buf.append(((MenuItem)listModel.elementAt(i)).actionName);
126         }
127         jEdit.setProperty("view.context",buf.toString());
128     }
129
130     // private members
131
private DefaultListModel listModel;
132     private JList list;
133     private JButton add;
134     private JButton remove;
135     private JButton moveUp, moveDown;
136
137     private void updateButtons()
138     {
139         int index = list.getSelectedIndex();
140         remove.setEnabled(index != -1 && listModel.getSize() != 0);
141         moveUp.setEnabled(index > 0);
142         moveDown.setEnabled(index != -1 && index != listModel.getSize() - 1);
143     }
144
145     static class MenuItem
146     {
147         String JavaDoc actionName;
148         String JavaDoc label;
149
150         MenuItem(String JavaDoc actionName, String JavaDoc label)
151         {
152             this.actionName = actionName;
153             this.label = GUIUtilities.prettifyMenuLabel(label);
154         }
155
156         public String JavaDoc toString()
157         {
158             return label;
159         }
160     }
161
162     class ActionHandler implements ActionListener
163     {
164         public void actionPerformed(ActionEvent evt)
165         {
166             Object JavaDoc source = evt.getSource();
167
168             if(source == add)
169             {
170                 ContextAddDialog dialog = new ContextAddDialog(
171                     ContextOptionPane.this);
172                 String JavaDoc selection = dialog.getSelection();
173                 if(selection == null)
174                     return;
175
176                 int index = list.getSelectedIndex();
177                 if(index == -1)
178                     index = listModel.getSize();
179                 else
180                     index++;
181
182                 MenuItem menuItem;
183                 if(selection.equals("-"))
184                     menuItem = new ContextOptionPane.MenuItem("-","-");
185                 else
186                 {
187                     menuItem = new ContextOptionPane.MenuItem(selection,
188                         jEdit.getAction(selection)
189                         .getLabel());
190                 }
191
192                 listModel.insertElementAt(menuItem,index);
193                 list.setSelectedIndex(index);
194                 list.ensureIndexIsVisible(index);
195             }
196             else if(source == remove)
197             {
198                 int index = list.getSelectedIndex();
199                 listModel.removeElementAt(index);
200                 if(listModel.getSize() != 0)
201                 {
202                     list.setSelectedIndex(
203                         Math.min(listModel.getSize()-1,
204                         index));
205                 }
206                 updateButtons();
207             }
208             else if(source == moveUp)
209             {
210                 int index = list.getSelectedIndex();
211                 Object JavaDoc selected = list.getSelectedValue();
212                 listModel.removeElementAt(index);
213                 listModel.insertElementAt(selected,index-1);
214                 list.setSelectedIndex(index-1);
215                 list.ensureIndexIsVisible(index - 1);
216             }
217             else if(source == moveDown)
218             {
219                 int index = list.getSelectedIndex();
220                 Object JavaDoc selected = list.getSelectedValue();
221                 listModel.removeElementAt(index);
222                 listModel.insertElementAt(selected,index+1);
223                 list.setSelectedIndex(index+1);
224                 list.ensureIndexIsVisible(index+1);
225             }
226         }
227     }
228
229     class ListHandler implements ListSelectionListener
230     {
231         public void valueChanged(ListSelectionEvent evt)
232         {
233             updateButtons();
234         }
235     }
236 }
237
238 class ContextAddDialog extends EnhancedDialog
239 {
240     public ContextAddDialog(Component comp)
241     {
242         super(GUIUtilities.getParentDialog(comp),
243             jEdit.getProperty("options.context.add.title"),
244             true);
245
246         JPanel content = new JPanel(new BorderLayout());
247         content.setBorder(new EmptyBorder(12,12,12,12));
248         setContentPane(content);
249
250         ActionHandler actionHandler = new ActionHandler();
251         ButtonGroup grp = new ButtonGroup();
252
253         JPanel typePanel = new JPanel(new GridLayout(3,1,6,6));
254         typePanel.setBorder(new EmptyBorder(0,0,6,0));
255         typePanel.add(new JLabel(
256             jEdit.getProperty("options.context.add.caption")));
257
258         separator = new JRadioButton(jEdit.getProperty("options.context"
259             + ".add.separator"));
260         separator.addActionListener(actionHandler);
261         grp.add(separator);
262         typePanel.add(separator);
263
264         action = new JRadioButton(jEdit.getProperty("options.context"
265             + ".add.action"));
266         action.addActionListener(actionHandler);
267         grp.add(action);
268         action.setSelected(true);
269         typePanel.add(action);
270
271         content.add(BorderLayout.NORTH,typePanel);
272
273         JPanel actionPanel = new JPanel(new BorderLayout(6,6));
274
275         ActionSet[] actionsList = jEdit.getActionSets();
276         Vector vec = new Vector(actionsList.length);
277         for(int i = 0; i < actionsList.length; i++)
278         {
279             ActionSet actionSet = actionsList[i];
280             if(actionSet.getActionCount() != 0)
281                 vec.addElement(actionSet);
282         }
283         combo = new JComboBox(vec);
284         combo.addActionListener(actionHandler);
285         actionPanel.add(BorderLayout.NORTH,combo);
286
287         list = new JList();
288         list.setVisibleRowCount(8);
289         list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
290         actionPanel.add(BorderLayout.CENTER,new JScrollPane(list));
291
292         content.add(BorderLayout.CENTER,actionPanel);
293
294         JPanel southPanel = new JPanel();
295         southPanel.setLayout(new BoxLayout(southPanel,BoxLayout.X_AXIS));
296         southPanel.setBorder(new EmptyBorder(12,0,0,0));
297         southPanel.add(Box.createGlue());
298         ok = new JButton(jEdit.getProperty("common.ok"));
299         ok.addActionListener(actionHandler);
300         getRootPane().setDefaultButton(ok);
301         southPanel.add(ok);
302         southPanel.add(Box.createHorizontalStrut(6));
303         cancel = new JButton(jEdit.getProperty("common.cancel"));
304         cancel.addActionListener(actionHandler);
305         southPanel.add(cancel);
306         southPanel.add(Box.createGlue());
307
308         content.add(BorderLayout.SOUTH,southPanel);
309
310         updateList();
311
312         pack();
313         setLocationRelativeTo(GUIUtilities.getParentDialog(comp));
314         setVisible(true);
315     }
316
317     public void ok()
318     {
319         isOK = true;
320         dispose();
321     }
322
323     public void cancel()
324     {
325         dispose();
326     }
327
328     public String JavaDoc getSelection()
329     {
330         if(!isOK)
331             return null;
332
333         if(separator.isSelected())
334             return "-";
335         else if(action.isSelected())
336         {
337             return ((ContextOptionPane.MenuItem)list.getSelectedValue())
338                 .actionName;
339         }
340         else
341             throw new InternalError JavaDoc();
342     }
343
344     // private members
345
private boolean isOK;
346     private JRadioButton separator, action;
347     private JComboBox combo;
348     private JList list;
349     private JButton ok, cancel;
350
351     private void updateList()
352     {
353         ActionSet actionSet = (ActionSet)combo.getSelectedItem();
354         EditAction[] actions = actionSet.getActions();
355         Vector listModel = new Vector(actions.length);
356
357         for(int i = 0; i < actions.length; i++)
358         {
359             EditAction action = actions[i];
360             String JavaDoc label = action.getLabel();
361             if(label == null)
362                 continue;
363
364             listModel.addElement(new ContextOptionPane.MenuItem(
365                 action.getName(),label));
366         }
367
368         Collections.sort(listModel,new ContextOptionPane.MenuItemCompare());
369
370         list.setListData(listModel);
371     }
372
373     class ActionHandler implements ActionListener
374     {
375         public void actionPerformed(ActionEvent evt)
376         {
377             Object JavaDoc source = evt.getSource();
378             if(source instanceof JRadioButton)
379             {
380                 combo.setEnabled(action.isSelected());
381                 list.setEnabled(action.isSelected());
382             }
383             if(source == ok)
384                 ok();
385             else if(source == cancel)
386                 cancel();
387             else if(source == combo)
388                 updateList();
389         }
390     }
391 }
392
Popular Tags