PageRenderTime 169ms CodeModel.GetById 140ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/options/ToolBarOptionPane.java

#
Java | 702 lines | 557 code | 82 blank | 63 comment | 77 complexity | d9dc4653a891b3375a778be7894c3172 MD5 | raw file
  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
 23package org.gjt.sp.jedit.options;
 24
 25//{{{ Imports
 26import javax.swing.border.*;
 27import javax.swing.event.*;
 28import javax.swing.*;
 29import java.awt.event.*;
 30import java.awt.*;
 31import java.net.*;
 32import java.util.*;
 33import org.gjt.sp.jedit.browser.VFSBrowser;
 34import org.gjt.sp.jedit.gui.*;
 35import org.gjt.sp.jedit.*;
 36import org.gjt.sp.util.Log;
 37//}}}
 38
 39//{{{ ToolBarOptionPane class
 40/**
 41 * Tool bar editor.
 42 * @author Slava Pestov
 43 * @version $Id: ToolBarOptionPane.java 4622 2003-04-14 22:43:17Z spestov $
 44 */
 45public class ToolBarOptionPane extends AbstractOptionPane
 46{
 47	//{{{ ToolBarOptionPane constructor
 48	public ToolBarOptionPane()
 49	{
 50		super("toolbar");
 51	} //}}}
 52
 53	//{{{ _init() method
 54	protected void _init()
 55	{
 56		setLayout(new BorderLayout());
 57
 58		JPanel panel = new JPanel(new GridLayout(2,1));
 59
 60		/* Show toolbar */
 61		showToolbar = new JCheckBox(jEdit.getProperty(
 62			"options.toolbar.showToolbar"));
 63		showToolbar.setSelected(jEdit.getBooleanProperty("view.showToolbar"));
 64		panel.add(showToolbar);
 65
 66		panel.add(new JLabel(jEdit.getProperty(
 67			"options.toolbar.caption")));
 68
 69		add(BorderLayout.NORTH,panel);
 70
 71		String toolbar = jEdit.getProperty("view.toolbar");
 72		StringTokenizer st = new StringTokenizer(toolbar);
 73		listModel = new DefaultListModel();
 74		while(st.hasMoreTokens())
 75		{
 76			String actionName = (String)st.nextToken();
 77			if(actionName.equals("-"))
 78				listModel.addElement(new ToolBarOptionPane.Button("-",null,null,"-"));
 79			else
 80			{
 81				EditAction action = jEdit.getAction(actionName);
 82				if(action == null)
 83					continue;
 84				String label = action.getLabel();
 85				if(label == null)
 86					continue;
 87
 88				Icon icon;
 89				String iconName;
 90				if(actionName.equals("-"))
 91				{
 92					iconName = null;
 93					icon = null;
 94				}
 95				else
 96				{
 97					iconName = jEdit.getProperty(actionName + ".icon");
 98					if(iconName == null)
 99						icon = GUIUtilities.loadIcon("BrokenImage.png");
100					else
101					{
102						icon = GUIUtilities.loadIcon(iconName);
103						if(icon == null)
104							icon = GUIUtilities.loadIcon("BrokenImage.png");
105					}
106				}
107				listModel.addElement(new Button(actionName,iconName,icon,label));
108			}
109		}
110
111		list = new JList(listModel);
112		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
113		list.addListSelectionListener(new ListHandler());
114		list.setCellRenderer(new ButtonCellRenderer());
115
116		add(BorderLayout.CENTER,new JScrollPane(list));
117
118		//{{{ Create buttons
119		JPanel buttons = new JPanel();
120		buttons.setBorder(new EmptyBorder(3,0,0,0));
121		buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
122		ActionHandler actionHandler = new ActionHandler();
123		add = new RolloverButton(GUIUtilities.loadIcon("Plus.png"));
124		add.setToolTipText(jEdit.getProperty("options.toolbar.add"));
125		add.addActionListener(actionHandler);
126		buttons.add(add);
127		buttons.add(Box.createHorizontalStrut(6));
128		remove = new RolloverButton(GUIUtilities.loadIcon("Minus.png"));
129		remove.setToolTipText(jEdit.getProperty("options.toolbar.remove"));
130		remove.addActionListener(actionHandler);
131		buttons.add(remove);
132		buttons.add(Box.createHorizontalStrut(6));
133		moveUp = new RolloverButton(GUIUtilities.loadIcon("ArrowU.png"));
134		moveUp.setToolTipText(jEdit.getProperty("options.toolbar.moveUp"));
135		moveUp.addActionListener(actionHandler);
136		buttons.add(moveUp);
137		buttons.add(Box.createHorizontalStrut(6));
138		moveDown = new RolloverButton(GUIUtilities.loadIcon("ArrowD.png"));
139		moveDown.setToolTipText(jEdit.getProperty("options.toolbar.moveDown"));
140		moveDown.addActionListener(actionHandler);
141		buttons.add(moveDown);
142		buttons.add(Box.createHorizontalStrut(6));
143		edit = new RolloverButton(GUIUtilities.loadIcon("ButtonProperties.png"));
144		edit.setToolTipText(jEdit.getProperty("options.toolbar.edit"));
145		edit.addActionListener(actionHandler);
146		buttons.add(edit);
147		buttons.add(Box.createGlue());
148		//}}}
149
150		updateButtons();
151		add(BorderLayout.SOUTH,buttons);
152
153		//{{{ Ceate icons list
154		iconList = new DefaultComboBoxModel();
155		st = new StringTokenizer(jEdit.getProperty("icons"));
156		while(st.hasMoreElements())
157		{
158			String icon = st.nextToken();
159			iconList.addElement(new IconListEntry(
160				GUIUtilities.loadIcon(icon),icon));
161		} //}}}
162	} ///}}}
163
164	//{{{ _save() method
165	protected void _save()
166	{
167		jEdit.setBooleanProperty("view.showToolbar",showToolbar
168			.isSelected());
169
170		StringBuffer buf = new StringBuffer();
171		for(int i = 0; i < listModel.getSize(); i++)
172		{
173			if(i != 0)
174				buf.append(' ');
175			Button button = (Button)listModel.elementAt(i);
176			buf.append(button.actionName);
177			jEdit.setProperty(button.actionName + ".icon",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	//{{{ updateButtons() method
197	private void updateButtons()
198	{
199		int index = list.getSelectedIndex();
200		remove.setEnabled(index != -1 && listModel.getSize() != 0);
201		moveUp.setEnabled(index > 0);
202		moveDown.setEnabled(index != -1 && index != listModel.getSize() - 1);
203		edit.setEnabled(index != -1);
204	} //}}}
205
206	//}}}
207
208	//{{{ Inner classes
209
210	//{{{ ButtonCompare class
211	static class ButtonCompare implements MiscUtilities.Compare
212	{
213		public int compare(Object obj1, Object obj2)
214		{
215			return MiscUtilities.compareStrings(
216				((Button)obj1).label,
217				((Button)obj2).label,
218				true);
219		}
220	} //}}}
221
222	//{{{ Button class
223	static class Button
224	{
225		String actionName;
226		String iconName;
227		Icon icon;
228		String label;
229
230		Button(String actionName, String iconName, Icon icon, String label)
231		{
232			this.actionName = actionName;
233			this.iconName = iconName;
234			this.icon = icon;
235			this.label = GUIUtilities.prettifyMenuLabel(label);
236		}
237
238		public String toString()
239		{
240			return label;
241		}
242
243		public boolean equals(Object o)
244		{
245			if(o instanceof Button)
246				return ((Button)o).actionName.equals(actionName);
247			else
248				return false;
249		}
250	} //}}}
251
252	//{{{ IconListEntry class
253	static class IconListEntry
254	{
255		Icon icon;
256		String name;
257
258		IconListEntry(Icon icon, String name)
259		{
260			this.icon = icon;
261			this.name = name;
262		}
263
264		public String toString()
265		{
266			return name;
267		}
268	} //}}}
269
270	//{{{ ButtonCellRenderer class
271	static class ButtonCellRenderer extends DefaultListCellRenderer
272	{
273		public Component getListCellRendererComponent(JList list,
274			Object value, int index, boolean isSelected,
275			boolean cellHasFocus)
276		{
277			super.getListCellRendererComponent(list,value,index,
278				isSelected,cellHasFocus);
279
280			Button button = (Button)value;
281			setIcon(button.icon);
282
283			return this;
284		}
285	} //}}}
286
287	//{{{ IconCellRenderer class
288	static class IconCellRenderer extends DefaultListCellRenderer
289	{
290		public Component getListCellRendererComponent(JList list,
291			Object value, int index, boolean isSelected,
292			boolean cellHasFocus)
293		{
294			super.getListCellRendererComponent(list,value,index,
295				isSelected,cellHasFocus);
296
297			IconListEntry icon = (IconListEntry)value;
298			setIcon(icon.icon);
299
300			return this;
301		}
302	} //}}}
303
304	//{{{ ActionHandler class
305	class ActionHandler implements ActionListener
306	{
307		public void actionPerformed(ActionEvent evt)
308		{
309			Object source = evt.getSource();
310
311			if(source == add)
312			{
313				ToolBarEditDialog dialog = new ToolBarEditDialog(
314					ToolBarOptionPane.this,iconList,null);
315				Button selection = dialog.getSelection();
316				if(selection == null)
317					return;
318
319				int index = list.getSelectedIndex();
320				if(index == -1)
321					index = listModel.getSize();
322				else
323					index++;
324
325				listModel.insertElementAt(selection,index);
326				list.setSelectedIndex(index);
327				list.ensureIndexIsVisible(index);
328			}
329			else if(source == remove)
330			{
331				int index = list.getSelectedIndex();
332				listModel.removeElementAt(index);
333				updateButtons();
334			}
335			else if(source == moveUp)
336			{
337				int index = list.getSelectedIndex();
338				Object selected = list.getSelectedValue();
339				listModel.removeElementAt(index);
340				listModel.insertElementAt(selected,index-1);
341				list.setSelectedIndex(index-1);
342				list.ensureIndexIsVisible(index-1);
343			}
344			else if(source == moveDown)
345			{
346				int index = list.getSelectedIndex();
347				Object selected = list.getSelectedValue();
348				listModel.removeElementAt(index);
349				listModel.insertElementAt(selected,index+1);
350				list.setSelectedIndex(index+1);
351				list.ensureIndexIsVisible(index+1);
352			}
353			else if(source == edit)
354			{
355				ToolBarEditDialog dialog = new ToolBarEditDialog(
356					ToolBarOptionPane.this,iconList,
357					(Button)list.getSelectedValue());
358				Button selection = dialog.getSelection();
359				if(selection == null)
360					return;
361
362				int index = list.getSelectedIndex();
363
364				listModel.setElementAt(selection,index);
365				list.setSelectedIndex(index);
366				list.ensureIndexIsVisible(index);
367			}
368		}
369	} //}}}
370
371	//{{{ ListHandler class
372	class ListHandler implements ListSelectionListener
373	{
374		public void valueChanged(ListSelectionEvent evt)
375		{
376			updateButtons();
377		}
378	} //}}}
379
380	//}}}
381} //}}}
382
383//{{{ ToolBarEditDialog class
384class ToolBarEditDialog extends EnhancedDialog
385{
386	//{{{ ToolBarEditDialog constructor
387	public ToolBarEditDialog(Component comp,
388		DefaultComboBoxModel iconListModel,
389		ToolBarOptionPane.Button current)
390	{
391		super(GUIUtilities.getParentDialog(comp),
392			jEdit.getProperty("options.toolbar.edit.title"),
393			true);
394
395		JPanel content = new JPanel(new BorderLayout());
396		content.setBorder(new EmptyBorder(12,12,12,12));
397		setContentPane(content);
398
399		ActionHandler actionHandler = new ActionHandler();
400		ButtonGroup grp = new ButtonGroup();
401
402		JPanel typePanel = new JPanel(new GridLayout(3,1,6,6));
403		typePanel.setBorder(new EmptyBorder(0,0,6,0));
404		typePanel.add(new JLabel(
405			jEdit.getProperty("options.toolbar.edit.caption")));
406
407		separator = new JRadioButton(jEdit.getProperty("options.toolbar"
408			+ ".edit.separator"));
409		separator.addActionListener(actionHandler);
410		grp.add(separator);
411		typePanel.add(separator);
412
413		action = new JRadioButton(jEdit.getProperty("options.toolbar"
414			+ ".edit.action"));
415		action.addActionListener(actionHandler);
416		grp.add(action);
417		typePanel.add(action);
418
419		content.add(BorderLayout.NORTH,typePanel);
420
421		JPanel actionPanel = new JPanel(new BorderLayout(6,6));
422
423		ActionSet[] actionsList = jEdit.getActionSets();
424		Vector vec = new Vector(actionsList.length);
425		for(int i = 0; i < actionsList.length; i++)
426		{
427			ActionSet actionSet = actionsList[i];
428			if(actionSet.getActionCount() != 0)
429				vec.addElement(actionSet);
430		}
431		combo = new JComboBox(vec);
432		combo.addActionListener(actionHandler);
433		actionPanel.add(BorderLayout.NORTH,combo);
434
435		list = new JList();
436		list.setVisibleRowCount(8);
437		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
438		actionPanel.add(BorderLayout.CENTER,new JScrollPane(list));
439
440		// Icon selection
441		JPanel iconPanel = new JPanel(new BorderLayout(0,3));
442		JPanel labelPanel = new JPanel(new GridLayout(2,1));
443		labelPanel.setBorder(new EmptyBorder(0,0,0,12));
444		JPanel compPanel = new JPanel(new GridLayout(2,1));
445		grp = new ButtonGroup();
446		labelPanel.add(builtin = new JRadioButton(jEdit.getProperty(
447			"options.toolbar.edit.builtin")));
448		builtin.addActionListener(actionHandler);
449		grp.add(builtin);
450		labelPanel.add(file = new JRadioButton(jEdit.getProperty(
451			"options.toolbar.edit.file")));
452		grp.add(file);
453		file.addActionListener(actionHandler);
454		iconPanel.add(BorderLayout.WEST,labelPanel);
455		builtinCombo = new JComboBox(iconListModel);
456		builtinCombo.setRenderer(new ToolBarOptionPane.IconCellRenderer());
457		compPanel.add(builtinCombo);
458
459		fileButton = new JButton(jEdit.getProperty("options.toolbar.edit.no-icon"));
460		fileButton.setMargin(new Insets(1,1,1,1));
461		fileButton.setIcon(GUIUtilities.loadIcon("Blank24.gif"));
462		fileButton.setHorizontalAlignment(SwingConstants.LEFT);
463		fileButton.addActionListener(actionHandler);
464		compPanel.add(fileButton);
465		iconPanel.add(BorderLayout.CENTER,compPanel);
466		actionPanel.add(BorderLayout.SOUTH,iconPanel);
467
468		content.add(BorderLayout.CENTER,actionPanel);
469
470		JPanel southPanel = new JPanel();
471		southPanel.setLayout(new BoxLayout(southPanel,BoxLayout.X_AXIS));
472		southPanel.setBorder(new EmptyBorder(12,0,0,0));
473		southPanel.add(Box.createGlue());
474		ok = new JButton(jEdit.getProperty("common.ok"));
475		ok.addActionListener(actionHandler);
476		getRootPane().setDefaultButton(ok);
477		southPanel.add(ok);
478		southPanel.add(Box.createHorizontalStrut(6));
479		cancel = new JButton(jEdit.getProperty("common.cancel"));
480		cancel.addActionListener(actionHandler);
481		southPanel.add(cancel);
482		southPanel.add(Box.createGlue());
483
484		content.add(BorderLayout.SOUTH,southPanel);
485
486		if(current == null)
487		{
488			action.setSelected(true);
489			builtin.setSelected(true);
490			updateList();
491		}
492		else
493		{
494			if(current.actionName.equals("-"))
495			{
496				separator.setSelected(true);
497				builtin.setSelected(true);
498			}
499			else
500			{
501				action.setSelected(true);
502				ActionSet set = jEdit.getActionSetForAction(
503					current.actionName);
504				combo.setSelectedItem(set);
505				updateList();
506				list.setSelectedValue(current,true);
507
508				if(MiscUtilities.isURL(current.iconName))
509				{
510					file.setSelected(true);
511					fileIcon = current.iconName;
512					try
513					{
514						fileButton.setIcon(new ImageIcon(new URL(
515							fileIcon)));
516					}
517					catch(MalformedURLException mf)
518					{
519						Log.log(Log.ERROR,this,mf);
520					}
521					fileButton.setText(MiscUtilities.getFileName(fileIcon));
522				}
523				else
524				{
525					String iconName = MiscUtilities.getFileName(current.iconName);
526					builtin.setSelected(true);
527					ListModel model = builtinCombo.getModel();
528					for(int i = 0; i < model.getSize(); i++)
529					{
530						ToolBarOptionPane.IconListEntry entry
531							= (ToolBarOptionPane.IconListEntry)
532							model.getElementAt(i);
533						if(entry.name.equals(iconName))
534						{
535							builtinCombo.setSelectedIndex(i);
536							break;
537						}
538					}
539				}
540			}
541		}
542
543		updateEnabled();
544
545		pack();
546		setLocationRelativeTo(GUIUtilities.getParentDialog(comp));
547		show();
548	} //}}}
549
550	//{{{ ok() method
551	public void ok()
552	{
553		isOK = true;
554		dispose();
555	} //}}}
556
557	//{{{ cancel() method
558	public void cancel()
559	{
560		dispose();
561	} //}}}
562
563	//{{{ getSelection() method
564	public ToolBarOptionPane.Button getSelection()
565	{
566		if(!isOK)
567			return null;
568
569		if(separator.isSelected())
570			return new ToolBarOptionPane.Button("-",null,null,"-");
571		else
572		{
573			Icon icon;
574			String iconName;
575			if(builtin.isSelected())
576			{
577				ToolBarOptionPane.IconListEntry selectedIcon =
578					(ToolBarOptionPane.IconListEntry)
579					builtinCombo.getSelectedItem();
580				icon = selectedIcon.icon;
581				iconName = selectedIcon.name;
582			}
583			else
584			{
585				icon = fileButton.getIcon();
586				iconName = fileIcon;
587				if(iconName == null)
588					iconName = "Blank24.gif";
589			}
590
591			String label;
592			String actionName;
593			if(action.isSelected())
594			{
595				ToolBarOptionPane.Button button =
596					(ToolBarOptionPane.Button)list
597					.getSelectedValue();
598				label = button.label;
599				actionName = button.actionName;
600			}
601			else
602				throw new InternalError();
603
604			return new ToolBarOptionPane.Button(actionName,
605				iconName,icon,label);
606		}
607	} //}}}
608
609	//{{{ Private members
610
611	//{{{ Instance variables
612	private boolean isOK;
613	private JRadioButton separator, action;
614	private JComboBox combo;
615	private JList list;
616	private JRadioButton builtin;
617	private JComboBox builtinCombo;
618	private JRadioButton file;
619	private JButton fileButton;
620	private String fileIcon;
621	private JButton ok, cancel;
622	//}}}
623
624	//{{{ updateEnabled() method
625	private void updateEnabled()
626	{
627		combo.setEnabled(action.isSelected());
628		list.setEnabled(action.isSelected());
629
630		boolean iconControlsEnabled = !separator.isSelected();
631		builtin.setEnabled(iconControlsEnabled);
632		file.setEnabled(iconControlsEnabled);
633		builtinCombo.setEnabled(iconControlsEnabled && builtin.isSelected());
634		fileButton.setEnabled(iconControlsEnabled && file.isSelected());
635	} //}}}
636
637	//{{{ updateList() method
638	private void updateList()
639	{
640		ActionSet actionSet = (ActionSet)combo.getSelectedItem();
641		EditAction[] actions = actionSet.getActions();
642		Vector listModel = new Vector(actions.length);
643
644		for(int i = 0; i < actions.length; i++)
645		{
646			EditAction action = actions[i];
647			String label = action.getLabel();
648			if(label == null)
649				continue;
650
651			listModel.addElement(new ToolBarOptionPane.Button(
652				action.getName(),null,null,label));
653		}
654
655		MiscUtilities.quicksort(listModel,new ToolBarOptionPane.ButtonCompare());
656		list.setListData(listModel);
657	} //}}}
658
659	//}}}
660
661	//{{{ ActionHandler class
662	class ActionHandler implements ActionListener
663	{
664		public void actionPerformed(ActionEvent evt)
665		{
666			Object source = evt.getSource();
667			if(source instanceof JRadioButton)
668				updateEnabled();
669			if(source == ok)
670				ok();
671			else if(source == cancel)
672				cancel();
673			else if(source == combo)
674				updateList();
675			else if(source == fileButton)
676			{
677				String directory;
678				if(fileIcon == null)
679					directory = null;
680				else
681					directory = MiscUtilities.getParentOfPath(fileIcon);
682				String paths[] = GUIUtilities.showVFSFileDialog(null,directory,
683					VFSBrowser.OPEN_DIALOG,false);
684				if(paths == null)
685					return;
686
687				fileIcon = "file:" + paths[0];
688
689				try
690				{
691					fileButton.setIcon(new ImageIcon(new URL(
692						fileIcon)));
693				}
694				catch(MalformedURLException mf)
695				{
696					Log.log(Log.ERROR,this,mf);
697				}
698				fileButton.setText(MiscUtilities.getFileName(fileIcon));
699			}
700		}
701	} //}}}
702} //}}}