KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ToolBarOptionPane.java - Tool bar options panel
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2000, 2001, 2002 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit.options;
24
25 //{{{ Imports
26
import javax.swing.border.*;
27 import javax.swing.event.*;
28 import javax.swing.*;
29 import java.awt.event.*;
30 import java.awt.*;
31 import java.net.*;
32 import java.util.*;
33 import org.gjt.sp.jedit.browser.VFSBrowser;
34 import org.gjt.sp.jedit.gui.*;
35 import org.gjt.sp.jedit.*;
36 import org.gjt.sp.util.Log;
37 import org.gjt.sp.util.StandardUtilities;
38 //}}}
39

40 //{{{ ToolBarOptionPane class
41
/**
42  * Tool bar editor.
43  * @author Slava Pestov
44  * @version $Id: ToolBarOptionPane.java 5570 2006-07-11 09:27:07Z kpouer $
45  */

46 public class ToolBarOptionPane extends AbstractOptionPane
47 {
48     //{{{ ToolBarOptionPane constructor
49
public ToolBarOptionPane()
50     {
51         super("toolbar");
52     } //}}}
53

54     //{{{ _init() method
55
protected void _init()
56     {
57         setLayout(new BorderLayout());
58
59         JPanel panel = new JPanel(new GridLayout(2,1));
60
61         /* Show toolbar */
62         showToolbar = new JCheckBox(jEdit.getProperty(
63             "options.toolbar.showToolbar"));
64         showToolbar.setSelected(jEdit.getBooleanProperty("view.showToolbar"));
65         panel.add(showToolbar);
66
67         panel.add(new JLabel(jEdit.getProperty(
68             "options.toolbar.caption")));
69
70         add(BorderLayout.NORTH,panel);
71
72         String JavaDoc toolbar = jEdit.getProperty("view.toolbar");
73         StringTokenizer st = new StringTokenizer(toolbar);
74         listModel = new DefaultListModel();
75         while(st.hasMoreTokens())
76         {
77             String JavaDoc actionName = st.nextToken();
78             if(actionName.equals("-"))
79                 listModel.addElement(new ToolBarOptionPane.Button("-",null,null,"-"));
80             else
81             {
82                 EditAction action = jEdit.getAction(actionName);
83                 if(action == null)
84                     continue;
85                 String JavaDoc label = action.getLabel();
86                 if(label == null)
87                     continue;
88
89                 Icon icon;
90                 String JavaDoc iconName;
91                 if(actionName.equals("-"))
92                 {
93                     iconName = null;
94                     icon = null;
95                 }
96                 else
97                 {
98                     iconName = jEdit.getProperty(actionName + ".icon");
99                     if(iconName == null)
100                         icon = GUIUtilities.loadIcon("BrokenImage.png");
101                     else
102                     {
103                         icon = GUIUtilities.loadIcon(iconName);
104                         if(icon == null)
105                             icon = GUIUtilities.loadIcon("BrokenImage.png");
106                     }
107                 }
108                 listModel.addElement(new Button(actionName,iconName,icon,label));
109             }
110         }
111
112         list = new JList(listModel);
113         list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
114         list.addListSelectionListener(new ListHandler());
115         list.setCellRenderer(new ButtonCellRenderer());
116
117         add(BorderLayout.CENTER,new JScrollPane(list));
118
119         //{{{ Create buttons
120
JPanel buttons = new JPanel();
121         buttons.setBorder(new EmptyBorder(3,0,0,0));
122         buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
123         ActionHandler actionHandler = new ActionHandler();
124         add = new RolloverButton(GUIUtilities.loadIcon("Plus.png"));
125         add.setToolTipText(jEdit.getProperty("options.toolbar.add"));
126         add.addActionListener(actionHandler);
127         buttons.add(add);
128         buttons.add(Box.createHorizontalStrut(6));
129         remove = new RolloverButton(GUIUtilities.loadIcon("Minus.png"));
130         remove.setToolTipText(jEdit.getProperty("options.toolbar.remove"));
131         remove.addActionListener(actionHandler);
132         buttons.add(remove);
133         buttons.add(Box.createHorizontalStrut(6));
134         moveUp = new RolloverButton(GUIUtilities.loadIcon("ArrowU.png"));
135         moveUp.setToolTipText(jEdit.getProperty("options.toolbar.moveUp"));
136         moveUp.addActionListener(actionHandler);
137         buttons.add(moveUp);
138         buttons.add(Box.createHorizontalStrut(6));
139         moveDown = new RolloverButton(GUIUtilities.loadIcon("ArrowD.png"));
140         moveDown.setToolTipText(jEdit.getProperty("options.toolbar.moveDown"));
141         moveDown.addActionListener(actionHandler);
142         buttons.add(moveDown);
143         buttons.add(Box.createHorizontalStrut(6));
144         edit = new RolloverButton(GUIUtilities.loadIcon("ButtonProperties.png"));
145         edit.setToolTipText(jEdit.getProperty("options.toolbar.edit"));
146         edit.addActionListener(actionHandler);
147         buttons.add(edit);
148         buttons.add(Box.createGlue());
149         //}}}
150

151         updateButtons();
152         add(BorderLayout.SOUTH,buttons);
153
154         //{{{ Ceate icons list
155
iconList = new DefaultComboBoxModel();
156         st = new StringTokenizer(jEdit.getProperty("icons"));
157         while(st.hasMoreElements())
158         {
159             String JavaDoc icon = st.nextToken();
160             iconList.addElement(new IconListEntry(
161                 GUIUtilities.loadIcon(icon),icon));
162         } //}}}
163
} ///}}}
164

165     //{{{ _save() method
166
protected void _save()
167     {
168         jEdit.setBooleanProperty("view.showToolbar",showToolbar
169             .isSelected());
170
171         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
172         for(int i = 0; i < listModel.getSize(); i++)
173         {
174             if(i != 0)
175                 buf.append(' ');
176             Button button = (Button)listModel.elementAt(i);
177             buf.append(button.actionName);
178             jEdit.setProperty(button.actionName + ".icon",button.iconName);
179         }
180         jEdit.setProperty("view.toolbar",buf.toString());
181     } //}}}
182

183     //{{{ Private members
184

185     //{{{ Instance variables
186
private JCheckBox showToolbar;
187     private DefaultListModel listModel;
188     private JList list;
189     private RolloverButton add;
190     private RolloverButton remove;
191     private RolloverButton moveUp, moveDown;
192     private RolloverButton edit;
193
194     private DefaultComboBoxModel iconList;
195     //}}}
196

197     //{{{ updateButtons() method
198
private void updateButtons()
199     {
200         int index = list.getSelectedIndex();
201         remove.setEnabled(index != -1 && listModel.getSize() != 0);
202         moveUp.setEnabled(index > 0);
203         moveDown.setEnabled(index != -1 && 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
213     {
214         public int compare(Object JavaDoc obj1, Object JavaDoc obj2)
215         {
216             return StandardUtilities.compareStrings(
217                 ((Button)obj1).label,
218                 ((Button)obj2).label,
219                 true);
220         }
221     } //}}}
222

223     //{{{ Button class
224
static class Button
225     {
226         String JavaDoc actionName;
227         String JavaDoc iconName;
228         Icon icon;
229         String JavaDoc label;
230
231         Button(String JavaDoc actionName, String JavaDoc iconName, Icon icon, String JavaDoc label)
232         {
233             this.actionName = actionName;
234             this.iconName = iconName;
235             this.icon = icon;
236             this.label = GUIUtilities.prettifyMenuLabel(label);
237         }
238
239         public String JavaDoc toString()
240         {
241             return label;
242         }
243
244         public boolean equals(Object JavaDoc o)
245         {
246             if(o instanceof Button)
247                 return ((Button)o).actionName.equals(actionName);
248             else
249                 return false;
250         }
251     } //}}}
252

253     //{{{ IconListEntry class
254
static class IconListEntry
255     {
256         Icon icon;
257         String JavaDoc name;
258
259         IconListEntry(Icon icon, String JavaDoc name)
260         {
261             this.icon = icon;
262             this.name = name;
263         }
264
265         public String JavaDoc toString()
266         {
267             return name;
268         }
269     } //}}}
270

271     //{{{ ButtonCellRenderer class
272
static class ButtonCellRenderer extends DefaultListCellRenderer
273     {
274         public Component getListCellRendererComponent(JList list,
275             Object JavaDoc value, int index, boolean isSelected,
276             boolean cellHasFocus)
277         {
278             super.getListCellRendererComponent(list,value,index,
279                 isSelected,cellHasFocus);
280
281             Button button = (Button)value;
282             setIcon(button.icon);
283
284             return this;
285         }
286     } //}}}
287

288     //{{{ IconCellRenderer class
289
static class IconCellRenderer extends DefaultListCellRenderer
290     {
291         public Component getListCellRendererComponent(JList list,
292             Object JavaDoc value, int index, boolean isSelected,
293             boolean cellHasFocus)
294         {
295             super.getListCellRendererComponent(list,value,index,
296                 isSelected,cellHasFocus);
297
298             IconListEntry icon = (IconListEntry)value;
299             setIcon(icon.icon);
300
301             return this;
302         }
303     } //}}}
304

305     //{{{ ActionHandler class
306
class ActionHandler implements ActionListener
307     {
308         public void actionPerformed(ActionEvent evt)
309         {
310             Object JavaDoc source = evt.getSource();
311
312             if(source == add)
313             {
314                 ToolBarEditDialog dialog = new ToolBarEditDialog(
315                     ToolBarOptionPane.this,iconList,null);
316                 Button selection = dialog.getSelection();
317                 if(selection == null)
318                     return;
319
320                 int index = list.getSelectedIndex();
321                 if(index == -1)
322                     index = listModel.getSize();
323                 else
324                     index++;
325
326                 listModel.insertElementAt(selection,index);
327                 list.setSelectedIndex(index);
328                 list.ensureIndexIsVisible(index);
329             }
330             else if(source == remove)
331             {
332                 int index = list.getSelectedIndex();
333                 listModel.removeElementAt(index);
334                 if(listModel.getSize() != 0)
335                 {
336                     if(listModel.getSize() == index)
337                         list.setSelectedIndex(index-1);
338                     else
339                         list.setSelectedIndex(index);
340                 }
341                 updateButtons();
342             }
343             else if(source == moveUp)
344             {
345                 int index = list.getSelectedIndex();
346                 Object JavaDoc selected = list.getSelectedValue();
347                 listModel.removeElementAt(index);
348                 listModel.insertElementAt(selected,index-1);
349                 list.setSelectedIndex(index-1);
350                 list.ensureIndexIsVisible(index-1);
351             }
352             else if(source == moveDown)
353             {
354                 int index = list.getSelectedIndex();
355                 Object JavaDoc selected = list.getSelectedValue();
356                 listModel.removeElementAt(index);
357                 listModel.insertElementAt(selected,index+1);
358                 list.setSelectedIndex(index+1);
359                 list.ensureIndexIsVisible(index+1);
360             }
361             else if(source == edit)
362             {
363                 ToolBarEditDialog dialog = new ToolBarEditDialog(
364                     ToolBarOptionPane.this,iconList,
365                     (Button)list.getSelectedValue());
366                 Button selection = dialog.getSelection();
367                 if(selection == null)
368                     return;
369
370                 int index = list.getSelectedIndex();
371
372                 listModel.setElementAt(selection,index);
373                 list.setSelectedIndex(index);
374                 list.ensureIndexIsVisible(index);
375             }
376         }
377     } //}}}
378

379     //{{{ ListHandler class
380
class ListHandler implements ListSelectionListener
381     {
382         public void valueChanged(ListSelectionEvent evt)
383         {
384             updateButtons();
385         }
386     } //}}}
387

388     //}}}
389
} //}}}
390

391 //{{{ ToolBarEditDialog class
392
class ToolBarEditDialog extends EnhancedDialog
393 {
394     //{{{ ToolBarEditDialog constructor
395
public ToolBarEditDialog(Component comp,
396         DefaultComboBoxModel iconListModel,
397         ToolBarOptionPane.Button current)
398     {
399         super(GUIUtilities.getParentDialog(comp),
400             jEdit.getProperty("options.toolbar.edit.title"),
401             true);
402
403         JPanel content = new JPanel(new BorderLayout());
404         content.setBorder(new EmptyBorder(12,12,12,12));
405         setContentPane(content);
406
407         ActionHandler actionHandler = new ActionHandler();
408         ButtonGroup grp = new ButtonGroup();
409
410         JPanel typePanel = new JPanel(new GridLayout(3,1,6,6));
411         typePanel.setBorder(new EmptyBorder(0,0,6,0));
412         typePanel.add(new JLabel(
413             jEdit.getProperty("options.toolbar.edit.caption")));
414
415         separator = new JRadioButton(jEdit.getProperty("options.toolbar"
416             + ".edit.separator"));
417         separator.addActionListener(actionHandler);
418         grp.add(separator);
419         typePanel.add(separator);
420
421         action = new JRadioButton(jEdit.getProperty("options.toolbar"
422             + ".edit.action"));
423         action.addActionListener(actionHandler);
424         grp.add(action);
425         typePanel.add(action);
426
427         content.add(BorderLayout.NORTH,typePanel);
428
429         JPanel actionPanel = new JPanel(new BorderLayout(6,6));
430
431         ActionSet[] actionsList = jEdit.getActionSets();
432         Vector vec = new Vector(actionsList.length);
433         for(int i = 0; i < actionsList.length; i++)
434         {
435             ActionSet actionSet = actionsList[i];
436             if(actionSet.getActionCount() != 0)
437                 vec.addElement(actionSet);
438         }
439         combo = new JComboBox(vec);
440         combo.addActionListener(actionHandler);
441         actionPanel.add(BorderLayout.NORTH,combo);
442
443         list = new JList();
444         list.setVisibleRowCount(8);
445         list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
446         actionPanel.add(BorderLayout.CENTER,new JScrollPane(list));
447
448         // Icon selection
449
JPanel iconPanel = new JPanel(new BorderLayout(0,3));
450         JPanel labelPanel = new JPanel(new GridLayout(2,1));
451         labelPanel.setBorder(new EmptyBorder(0,0,0,12));
452         JPanel compPanel = new JPanel(new GridLayout(2,1));
453         grp = new ButtonGroup();
454         labelPanel.add(builtin = new JRadioButton(jEdit.getProperty(
455             "options.toolbar.edit.builtin")));
456         builtin.addActionListener(actionHandler);
457         grp.add(builtin);
458         labelPanel.add(file = new JRadioButton(jEdit.getProperty(
459             "options.toolbar.edit.file")));
460         grp.add(file);
461         file.addActionListener(actionHandler);
462         iconPanel.add(BorderLayout.WEST,labelPanel);
463         builtinCombo = new JComboBox(iconListModel);
464         builtinCombo.setRenderer(new ToolBarOptionPane.IconCellRenderer());
465         compPanel.add(builtinCombo);
466
467         fileButton = new JButton(jEdit.getProperty("options.toolbar.edit.no-icon"));
468         fileButton.setMargin(new Insets(1,1,1,1));
469         fileButton.setIcon(GUIUtilities.loadIcon("Blank24.gif"));
470         fileButton.setHorizontalAlignment(SwingConstants.LEFT);
471         fileButton.addActionListener(actionHandler);
472         compPanel.add(fileButton);
473         iconPanel.add(BorderLayout.CENTER,compPanel);
474         actionPanel.add(BorderLayout.SOUTH,iconPanel);
475
476         content.add(BorderLayout.CENTER,actionPanel);
477
478         JPanel southPanel = new JPanel();
479         southPanel.setLayout(new BoxLayout(southPanel,BoxLayout.X_AXIS));
480         southPanel.setBorder(new EmptyBorder(12,0,0,0));
481         southPanel.add(Box.createGlue());
482         ok = new JButton(jEdit.getProperty("common.ok"));
483         ok.addActionListener(actionHandler);
484         getRootPane().setDefaultButton(ok);
485         southPanel.add(ok);
486         southPanel.add(Box.createHorizontalStrut(6));
487         cancel = new JButton(jEdit.getProperty("common.cancel"));
488         cancel.addActionListener(actionHandler);
489         southPanel.add(cancel);
490         southPanel.add(Box.createGlue());
491
492         content.add(BorderLayout.SOUTH,southPanel);
493
494         if(current == null)
495         {
496             action.setSelected(true);
497             builtin.setSelected(true);
498             updateList();
499         }
500         else
501         {
502             if(current.actionName.equals("-"))
503             {
504                 separator.setSelected(true);
505                 builtin.setSelected(true);
506             }
507             else
508             {
509                 action.setSelected(true);
510                 ActionSet set = jEdit.getActionSetForAction(
511                     current.actionName);
512                 combo.setSelectedItem(set);
513                 updateList();
514                 list.setSelectedValue(current,true);
515
516                 if(MiscUtilities.isURL(current.iconName))
517                 {
518                     file.setSelected(true);
519                     fileIcon = current.iconName;
520                     try
521                     {
522                         fileButton.setIcon(new ImageIcon(new URL(
523                             fileIcon)));
524                     }
525                     catch(MalformedURLException mf)
526                     {
527                         Log.log(Log.ERROR,this,mf);
528                     }
529                     fileButton.setText(MiscUtilities.getFileName(fileIcon));
530                 }
531                 else
532                 {
533                     String JavaDoc iconName = MiscUtilities.getFileName(current.iconName);
534                     builtin.setSelected(true);
535                     ListModel model = builtinCombo.getModel();
536                     for(int i = 0; i < model.getSize(); i++)
537                     {
538                         ToolBarOptionPane.IconListEntry entry
539                             = (ToolBarOptionPane.IconListEntry)
540                             model.getElementAt(i);
541                         if(entry.name.equals(iconName))
542                         {
543                             builtinCombo.setSelectedIndex(i);
544                             break;
545                         }
546                     }
547                 }
548             }
549         }
550
551         updateEnabled();
552
553         pack();
554         setLocationRelativeTo(GUIUtilities.getParentDialog(comp));
555         setVisible(true);
556     } //}}}
557

558     //{{{ ok() method
559
public void ok()
560     {
561         isOK = true;
562         dispose();
563     } //}}}
564

565     //{{{ cancel() method
566
public void cancel()
567     {
568         dispose();
569     } //}}}
570

571     //{{{ getSelection() method
572
public ToolBarOptionPane.Button getSelection()
573     {
574         if(!isOK)
575             return null;
576
577         if(separator.isSelected())
578             return new ToolBarOptionPane.Button("-",null,null,"-");
579         else
580         {
581             Icon icon;
582             String JavaDoc iconName;
583             if(builtin.isSelected())
584             {
585                 ToolBarOptionPane.IconListEntry selectedIcon =
586                     (ToolBarOptionPane.IconListEntry)
587                     builtinCombo.getSelectedItem();
588                 icon = selectedIcon.icon;
589                 iconName = selectedIcon.name;
590             }
591             else
592             {
593                 icon = fileButton.getIcon();
594                 iconName = fileIcon;
595                 if(iconName == null)
596                     iconName = "Blank24.gif";
597             }
598
599             String JavaDoc label;
600             String JavaDoc actionName;
601             if(action.isSelected())
602             {
603                 ToolBarOptionPane.Button button =
604                     (ToolBarOptionPane.Button)list
605                     .getSelectedValue();
606                 label = button.label;
607                 actionName = button.actionName;
608             }
609             else
610                 throw new InternalError JavaDoc();
611
612             return new ToolBarOptionPane.Button(actionName,
613                 iconName,icon,label);
614         }
615     } //}}}
616

617     //{{{ Private members
618

619     //{{{ Instance variables
620
private boolean isOK;
621     private JRadioButton separator, action;
622     private JComboBox combo;
623     private JList list;
624     private JRadioButton builtin;
625     private JComboBox builtinCombo;
626     private JRadioButton file;
627     private JButton fileButton;
628     private String JavaDoc fileIcon;
629     private JButton ok, cancel;
630     //}}}
631

632     //{{{ updateEnabled() method
633
private void updateEnabled()
634     {
635         combo.setEnabled(action.isSelected());
636         list.setEnabled(action.isSelected());
637
638         boolean iconControlsEnabled = !separator.isSelected();
639         builtin.setEnabled(iconControlsEnabled);
640         file.setEnabled(iconControlsEnabled);
641         builtinCombo.setEnabled(iconControlsEnabled && builtin.isSelected());
642         fileButton.setEnabled(iconControlsEnabled && file.isSelected());
643     } //}}}
644

645     //{{{ updateList() method
646
private void updateList()
647     {
648         ActionSet actionSet = (ActionSet)combo.getSelectedItem();
649         EditAction[] actions = actionSet.getActions();
650         Vector listModel = new Vector(actions.length);
651
652         for(int i = 0; i < actions.length; i++)
653         {
654             EditAction action = actions[i];
655             String JavaDoc label = action.getLabel();
656             if(label == null)
657                 continue;
658
659             listModel.addElement(new ToolBarOptionPane.Button(
660                 action.getName(),null,null,label));
661         }
662
663         Collections.sort(listModel,new ToolBarOptionPane.ButtonCompare());
664         list.setListData(listModel);
665     } //}}}
666

667     //}}}
668

669     //{{{ ActionHandler class
670
class ActionHandler implements ActionListener
671     {
672         public void actionPerformed(ActionEvent evt)
673         {
674             Object JavaDoc source = evt.getSource();
675             if(source instanceof JRadioButton)
676                 updateEnabled();
677             if(source == ok)
678                 ok();
679             else if(source == cancel)
680                 cancel();
681             else if(source == combo)
682                 updateList();
683             else if(source == fileButton)
684             {
685                 String JavaDoc directory;
686                 if(fileIcon == null)
687                     directory = null;
688                 else
689                     directory = MiscUtilities.getParentOfPath(fileIcon);
690                 String JavaDoc[] paths = GUIUtilities.showVFSFileDialog(null,directory,
691                     VFSBrowser.OPEN_DIALOG,false);
692                 if(paths == null)
693                     return;
694
695                 fileIcon = "file:" + paths[0];
696
697                 try
698                 {
699                     fileButton.setIcon(new ImageIcon(new URL(
700                         fileIcon)));
701                 }
702                 catch(MalformedURLException mf)
703                 {
704                     Log.log(Log.ERROR,this,mf);
705                 }
706                 fileButton.setText(MiscUtilities.getFileName(fileIcon));
707             }
708         }
709     } //}}}
710
} //}}}
711
Popular Tags