PageRenderTime 137ms CodeModel.GetById 87ms app.highlight 43ms RepoModel.GetById 1ms app.codeStats 1ms

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

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