PageRenderTime 85ms CodeModel.GetById 52ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/options/ToolBarOptionPane.java

#
Java | 703 lines | 558 code | 82 blank | 63 comment | 77 complexity | bdb0b6b242054440a1e5d7f0e739efec 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.io.File;
 32import java.net.*;
 33import java.util.*;
 34import org.gjt.sp.jedit.browser.VFSBrowser;
 35import org.gjt.sp.jedit.gui.*;
 36import org.gjt.sp.jedit.*;
 37import org.gjt.sp.util.Log;
 38//}}}
 39
 40//{{{ ToolBarOptionPane class
 41/**
 42 * Tool bar editor.
 43 * @author Slava Pestov
 44 * @version $Id: ToolBarOptionPane.java 4336 2002-09-08 21:03:11Z spestov $
 45 */
 46public 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 toolbar = jEdit.getProperty("view.toolbar");
 73		StringTokenizer st = new StringTokenizer(toolbar);
 74		listModel = new DefaultListModel();
 75		while(st.hasMoreTokens())
 76		{
 77			String actionName = (String)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 label = action.getLabel();
 86				if(label == null)
 87					continue;
 88
 89				Icon icon;
 90				String 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 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 buf = new StringBuffer();
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 MiscUtilities.Compare
213	{
214		public int compare(Object obj1, Object obj2)
215		{
216			return MiscUtilities.compareStrings(
217				((Button)obj1).label,
218				((Button)obj2).label,
219				true);
220		}
221	} //}}}
222
223	//{{{ Button class
224	static class Button
225	{
226		String actionName;
227		String iconName;
228		Icon icon;
229		String label;
230
231		Button(String actionName, String iconName, Icon icon, String 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 toString()
240		{
241			return label;
242		}
243
244		public boolean equals(Object 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 name;
258
259		IconListEntry(Icon icon, String name)
260		{
261			this.icon = icon;
262			this.name = name;
263		}
264
265		public String 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 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 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 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				updateButtons();
335			}
336			else if(source == moveUp)
337			{
338				int index = list.getSelectedIndex();
339				Object selected = list.getSelectedValue();
340				listModel.removeElementAt(index);
341				listModel.insertElementAt(selected,index-1);
342				list.setSelectedIndex(index-1);
343				list.ensureIndexIsVisible(index-1);
344			}
345			else if(source == moveDown)
346			{
347				int index = list.getSelectedIndex();
348				Object selected = list.getSelectedValue();
349				listModel.removeElementAt(index);
350				listModel.insertElementAt(selected,index+1);
351				list.setSelectedIndex(index+1);
352				list.ensureIndexIsVisible(index+1);
353			}
354			else if(source == edit)
355			{
356				ToolBarEditDialog dialog = new ToolBarEditDialog(
357					ToolBarOptionPane.this,iconList,
358					(Button)list.getSelectedValue());
359				Button selection = dialog.getSelection();
360				if(selection == null)
361					return;
362
363				int index = list.getSelectedIndex();
364
365				listModel.setElementAt(selection,index);
366				list.setSelectedIndex(index);
367				list.ensureIndexIsVisible(index);
368			}
369		}
370	} //}}}
371
372	//{{{ ListHandler class
373	class ListHandler implements ListSelectionListener
374	{
375		public void valueChanged(ListSelectionEvent evt)
376		{
377			updateButtons();
378		}
379	} //}}}
380
381	//}}}
382} //}}}
383
384//{{{ ToolBarEditDialog class
385class ToolBarEditDialog extends EnhancedDialog
386{
387	//{{{ ToolBarEditDialog constructor
388	public ToolBarEditDialog(Component comp,
389		DefaultComboBoxModel iconListModel,
390		ToolBarOptionPane.Button current)
391	{
392		super(JOptionPane.getFrameForComponent(comp),
393			jEdit.getProperty("options.toolbar.edit.title"),
394			true);
395
396		JPanel content = new JPanel(new BorderLayout());
397		content.setBorder(new EmptyBorder(12,12,12,12));
398		setContentPane(content);
399
400		ActionHandler actionHandler = new ActionHandler();
401		ButtonGroup grp = new ButtonGroup();
402
403		JPanel typePanel = new JPanel(new GridLayout(3,1,6,6));
404		typePanel.setBorder(new EmptyBorder(0,0,6,0));
405		typePanel.add(new JLabel(
406			jEdit.getProperty("options.toolbar.edit.caption")));
407
408		separator = new JRadioButton(jEdit.getProperty("options.toolbar"
409			+ ".edit.separator"));
410		separator.addActionListener(actionHandler);
411		grp.add(separator);
412		typePanel.add(separator);
413
414		action = new JRadioButton(jEdit.getProperty("options.toolbar"
415			+ ".edit.action"));
416		action.addActionListener(actionHandler);
417		grp.add(action);
418		typePanel.add(action);
419
420		content.add(BorderLayout.NORTH,typePanel);
421
422		JPanel actionPanel = new JPanel(new BorderLayout(6,6));
423
424		ActionSet[] actionsList = jEdit.getActionSets();
425		Vector vec = new Vector(actionsList.length);
426		for(int i = 0; i < actionsList.length; i++)
427		{
428			ActionSet actionSet = actionsList[i];
429			if(actionSet.getActionCount() != 0)
430				vec.addElement(actionSet);
431		}
432		combo = new JComboBox(vec);
433		combo.addActionListener(actionHandler);
434		actionPanel.add(BorderLayout.NORTH,combo);
435
436		list = new JList();
437		list.setVisibleRowCount(8);
438		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
439		actionPanel.add(BorderLayout.CENTER,new JScrollPane(list));
440
441		// Icon selection
442		JPanel iconPanel = new JPanel(new BorderLayout(0,3));
443		JPanel labelPanel = new JPanel(new GridLayout(2,1));
444		labelPanel.setBorder(new EmptyBorder(0,0,0,12));
445		JPanel compPanel = new JPanel(new GridLayout(2,1));
446		grp = new ButtonGroup();
447		labelPanel.add(builtin = new JRadioButton(jEdit.getProperty(
448			"options.toolbar.edit.builtin")));
449		builtin.addActionListener(actionHandler);
450		grp.add(builtin);
451		labelPanel.add(file = new JRadioButton(jEdit.getProperty(
452			"options.toolbar.edit.file")));
453		grp.add(file);
454		file.addActionListener(actionHandler);
455		iconPanel.add(BorderLayout.WEST,labelPanel);
456		builtinCombo = new JComboBox(iconListModel);
457		builtinCombo.setRenderer(new ToolBarOptionPane.IconCellRenderer());
458		compPanel.add(builtinCombo);
459
460		fileButton = new JButton(jEdit.getProperty("options.toolbar.edit.no-icon"));
461		fileButton.setMargin(new Insets(1,1,1,1));
462		fileButton.setIcon(GUIUtilities.loadIcon("Blank24.gif"));
463		fileButton.setHorizontalAlignment(SwingConstants.LEFT);
464		fileButton.addActionListener(actionHandler);
465		compPanel.add(fileButton);
466		iconPanel.add(BorderLayout.CENTER,compPanel);
467		actionPanel.add(BorderLayout.SOUTH,iconPanel);
468
469		content.add(BorderLayout.CENTER,actionPanel);
470
471		JPanel southPanel = new JPanel();
472		southPanel.setLayout(new BoxLayout(southPanel,BoxLayout.X_AXIS));
473		southPanel.setBorder(new EmptyBorder(12,0,0,0));
474		southPanel.add(Box.createGlue());
475		ok = new JButton(jEdit.getProperty("common.ok"));
476		ok.addActionListener(actionHandler);
477		getRootPane().setDefaultButton(ok);
478		southPanel.add(ok);
479		southPanel.add(Box.createHorizontalStrut(6));
480		cancel = new JButton(jEdit.getProperty("common.cancel"));
481		cancel.addActionListener(actionHandler);
482		southPanel.add(cancel);
483		southPanel.add(Box.createGlue());
484
485		content.add(BorderLayout.SOUTH,southPanel);
486
487		if(current == null)
488		{
489			action.setSelected(true);
490			builtin.setSelected(true);
491			updateList();
492		}
493		else
494		{
495			if(current.actionName.equals("-"))
496			{
497				separator.setSelected(true);
498				builtin.setSelected(true);
499			}
500			else
501			{
502				action.setSelected(true);
503				ActionSet set = jEdit.getActionSetForAction(
504					jEdit.getAction(current.actionName));
505				combo.setSelectedItem(set);
506				updateList();
507				list.setSelectedValue(current,true);
508
509				if(MiscUtilities.isURL(current.iconName))
510				{
511					file.setSelected(true);
512					fileIcon = current.iconName;
513					try
514					{
515						fileButton.setIcon(new ImageIcon(new URL(
516							fileIcon)));
517					}
518					catch(MalformedURLException mf)
519					{
520						Log.log(Log.ERROR,this,mf);
521					}
522					fileButton.setText(MiscUtilities.getFileName(fileIcon));
523				}
524				else
525				{
526					String iconName = MiscUtilities.getFileName(current.iconName);
527					builtin.setSelected(true);
528					ListModel model = builtinCombo.getModel();
529					for(int i = 0; i < model.getSize(); i++)
530					{
531						ToolBarOptionPane.IconListEntry entry
532							= (ToolBarOptionPane.IconListEntry)
533							model.getElementAt(i);
534						if(entry.name.equals(iconName))
535						{
536							builtinCombo.setSelectedIndex(i);
537							break;
538						}
539					}
540				}
541			}
542		}
543
544		updateEnabled();
545
546		pack();
547		setLocationRelativeTo(JOptionPane.getFrameForComponent(comp));
548		show();
549	} //}}}
550
551	//{{{ ok() method
552	public void ok()
553	{
554		isOK = true;
555		dispose();
556	} //}}}
557
558	//{{{ cancel() method
559	public void cancel()
560	{
561		dispose();
562	} //}}}
563
564	//{{{ getSelection() method
565	public ToolBarOptionPane.Button getSelection()
566	{
567		if(!isOK)
568			return null;
569
570		if(separator.isSelected())
571			return new ToolBarOptionPane.Button("-",null,null,"-");
572		else
573		{
574			Icon icon;
575			String iconName;
576			if(builtin.isSelected())
577			{
578				ToolBarOptionPane.IconListEntry selectedIcon =
579					(ToolBarOptionPane.IconListEntry)
580					builtinCombo.getSelectedItem();
581				icon = selectedIcon.icon;
582				iconName = selectedIcon.name;
583			}
584			else
585			{
586				icon = fileButton.getIcon();
587				iconName = fileIcon;
588				if(iconName == null)
589					iconName = "Blank24.gif";
590			}
591
592			String label;
593			String actionName;
594			if(action.isSelected())
595			{
596				ToolBarOptionPane.Button button =
597					(ToolBarOptionPane.Button)list
598					.getSelectedValue();
599				label = button.label;
600				actionName = button.actionName;
601			}
602			else
603				throw new InternalError();
604
605			return new ToolBarOptionPane.Button(actionName,
606				iconName,icon,label);
607		}
608	} //}}}
609
610	//{{{ Private members
611
612	//{{{ Instance variables
613	private boolean isOK;
614	private JRadioButton separator, action;
615	private JComboBox combo;
616	private JList list;
617	private JRadioButton builtin;
618	private JComboBox builtinCombo;
619	private JRadioButton file;
620	private JButton fileButton;
621	private String fileIcon;
622	private JButton ok, cancel;
623	//}}}
624
625	//{{{ updateEnabled() method
626	private void updateEnabled()
627	{
628		combo.setEnabled(action.isSelected());
629		list.setEnabled(action.isSelected());
630
631		boolean iconControlsEnabled = !separator.isSelected();
632		builtin.setEnabled(iconControlsEnabled);
633		file.setEnabled(iconControlsEnabled);
634		builtinCombo.setEnabled(iconControlsEnabled && builtin.isSelected());
635		fileButton.setEnabled(iconControlsEnabled && file.isSelected());
636	} //}}}
637
638	//{{{ updateList() method
639	private void updateList()
640	{
641		ActionSet actionSet = (ActionSet)combo.getSelectedItem();
642		EditAction[] actions = actionSet.getActions();
643		Vector listModel = new Vector(actions.length);
644
645		for(int i = 0; i < actions.length; i++)
646		{
647			EditAction action = actions[i];
648			String label = action.getLabel();
649			if(label == null)
650				continue;
651
652			listModel.addElement(new ToolBarOptionPane.Button(
653				action.getName(),null,null,label));
654		}
655
656		MiscUtilities.quicksort(listModel,new ToolBarOptionPane.ButtonCompare());
657		list.setListData(listModel);
658	} //}}}
659
660	//}}}
661
662	//{{{ ActionHandler class
663	class ActionHandler implements ActionListener
664	{
665		public void actionPerformed(ActionEvent evt)
666		{
667			Object source = evt.getSource();
668			if(source instanceof JRadioButton)
669				updateEnabled();
670			if(source == ok)
671				ok();
672			else if(source == cancel)
673				cancel();
674			else if(source == combo)
675				updateList();
676			else if(source == fileButton)
677			{
678				String directory;
679				if(fileIcon == null)
680					directory = null;
681				else
682					directory = MiscUtilities.getParentOfPath(fileIcon);
683				String paths[] = GUIUtilities.showVFSFileDialog(null,directory,
684					VFSBrowser.OPEN_DIALOG,false);
685				if(paths == null)
686					return;
687
688				fileIcon = "file:" + paths[0];
689
690				try
691				{
692					fileButton.setIcon(new ImageIcon(new URL(
693						fileIcon)));
694				}
695				catch(MalformedURLException mf)
696				{
697					Log.log(Log.ERROR,this,mf);
698				}
699				fileButton.setText(MiscUtilities.getFileName(fileIcon));
700			}
701		}
702	} //}}}
703} //}}}