PageRenderTime 65ms CodeModel.GetById 40ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/options/StatusBarOptionPane.java

#
Java | 571 lines | 429 code | 68 blank | 74 comment | 60 complexity | 44db72ededa5a9bf5ef57a4d4a04a094 MD5 | raw file
  1/*
  2 * StatusBarOptionPane.java - Tool bar options panel
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2008-2011 Matthieu Casanova
  7 * Portions Copyright (C) 2000-2002 Slava Pestov
  8 *
  9 * This program is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU General Public License
 11 * as published by the Free Software Foundation; either version 2
 12 * of the License, or any later version.
 13 *
 14 * This program is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17 * GNU General Public License for more details.
 18 *
 19 * You should have received a copy of the GNU General Public License
 20 * along with this program; if not, write to the Free Software
 21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 22 */
 23
 24package org.gjt.sp.jedit.options;
 25
 26//{{{ Imports
 27import javax.swing.border.*;
 28import javax.swing.event.*;
 29import javax.swing.*;
 30import java.awt.event.*;
 31import java.awt.*;
 32import java.util.*;
 33import org.gjt.sp.jedit.gui.*;
 34import org.gjt.sp.jedit.*;
 35//}}}
 36
 37/**
 38 * Status bar editor.
 39 * @author Matthieu Casanova
 40 * @version $Id: StatusBarOptionPane.java 19444 2011-03-12 16:15:16Z kpouer $
 41 */
 42public class StatusBarOptionPane extends AbstractOptionPane
 43{
 44	//{{{ StatusBarOptionPane constructor
 45	public StatusBarOptionPane()
 46	{
 47		super("status");
 48	} //}}}
 49
 50	//{{{ _init() method
 51	@Override
 52	protected void _init()
 53	{
 54		setLayout(new BorderLayout());
 55
 56		//{{{ North
 57		JPanel checkboxPanel = new JPanel(new GridLayout(2,1));
 58		showStatusbar = new JCheckBox(jEdit.getProperty(
 59			"options.status.visible"));
 60		showStatusbar.setSelected(jEdit.getBooleanProperty("view.status.visible"));
 61		checkboxPanel.add(showStatusbar);
 62		showStatusbarPlain = new JCheckBox(jEdit.getProperty(
 63			"options.status.plainview.visible"));
 64		showStatusbarPlain.setSelected(jEdit.getBooleanProperty("view.status.plainview.visible"));
 65		checkboxPanel.add(showStatusbarPlain);
 66		checkboxPanel.add(new JLabel(jEdit.getProperty(
 67			"options.status.caption")));
 68
 69		JPanel previewPanel = new JPanel();
 70		previewStatusBar = new JLabel();
 71		previewPanel.add(previewStatusBar);
 72		JPanel north = new JPanel(new GridLayout(2,1));
 73		north.add(checkboxPanel);
 74		north.add(previewPanel);
 75		add(north, BorderLayout.NORTH);
 76		//}}}
 77
 78		//{{{ Options panel
 79		AbstractOptionPane optionsPanel = new AbstractOptionPane("Status Options");
 80		/* Foreground color */
 81		optionsPanel.addComponent(jEdit.getProperty("options.status.foreground"),
 82			foregroundColor = new ColorWellButton(
 83			jEdit.getColorProperty("view.status.foreground")),
 84			GridBagConstraints.VERTICAL);
 85
 86		/* Background color */
 87		optionsPanel.addComponent(jEdit.getProperty("options.status.background"),
 88			backgroundColor = new ColorWellButton(
 89			jEdit.getColorProperty("view.status.background")),
 90			GridBagConstraints.VERTICAL);
 91
 92		/* Memory foreground color */
 93		optionsPanel.addComponent(jEdit.getProperty("options.status.memory.foreground"),
 94			memForegroundColor = new ColorWellButton(
 95			jEdit.getColorProperty("view.status.memory.foreground")),
 96			GridBagConstraints.VERTICAL);
 97
 98		/* Memory background color */
 99		optionsPanel.addComponent(jEdit.getProperty("options.status.memory.background"),
100			memBackgroundColor = new ColorWellButton(
101			jEdit.getColorProperty("view.status.memory.background")),
102			GridBagConstraints.VERTICAL);
103
104		optionsPanel.addSeparator();
105		optionsPanel.addComponent(new JLabel(jEdit.getProperty("options.status.caret.title", "Caret position display options:")));
106
107		/*
108		Caret position format: lineno,dot-virtual (caretpos/bufferlength)
109		view.status.show-caret-linenumber -- true shows line number for caret (lineno)
110		view.status.show-caret-dot -- true shows offset in line for caret (dot)
111		view.status.show-caret-virtual -- true shows virtual offset in line for caret (virtual)
112		view.status.show-caret-offset -- true shows caret offset from start of buffer (caretpos)
113		view.status.show-caret-bufferlength -- true shows length of buffer (bufferlength)
114		*/
115		showCaretLineNumber = new JCheckBox(jEdit.getProperty("options.status.caret.linenumber", "Show caret line number"),
116			jEdit.getBooleanProperty("view.status.show-caret-linenumber", true));
117		showCaretLineNumber.setName("showCaretLineNumber");
118		showCaretDot = new JCheckBox(jEdit.getProperty("options.status.caret.dot", "Show caret offset from start of line"),
119			jEdit.getBooleanProperty("view.status.show-caret-dot", true));
120		showCaretDot.setName("showCaretDot");
121		showCaretVirtual = new JCheckBox(jEdit.getProperty("options.status.caret.virtual", "Show caret virtual offset from start of line"),
122			jEdit.getBooleanProperty("view.status.show-caret-virtual", true));
123		showCaretVirtual.setName("showCaretVirtual");
124		showCaretOffset = new JCheckBox(jEdit.getProperty("options.status.caret.offset", "Show caret offset from start of file"),
125			jEdit.getBooleanProperty("view.status.show-caret-offset", true));
126		showCaretOffset.setName("showCaretOffset");
127		showCaretBufferLength = new JCheckBox(jEdit.getProperty("options.status.caret.bufferlength", "Show length of file"),
128			jEdit.getBooleanProperty("view.status.show-caret-bufferlength", true));
129		showCaretBufferLength.setName("showCaretBufferLength");
130		optionsPanel.addComponent(showCaretLineNumber);
131		optionsPanel.addComponent(showCaretDot);
132		optionsPanel.addComponent(showCaretVirtual);
133		optionsPanel.addComponent(showCaretOffset);
134		optionsPanel.addComponent(showCaretBufferLength);
135
136		//}}}
137
138
139		//{{{ widgets panel
140		String statusbar = jEdit.getProperty("view.status");
141		StringTokenizer st = new StringTokenizer(statusbar);
142		listModel = new DefaultListModel();
143		while (st.hasMoreTokens())
144		{
145			String token = st.nextToken();
146			listModel.addElement(token);
147		}
148
149
150		list = new JList(listModel);
151		list.setCellRenderer(new WidgetListCellRenderer());
152		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
153		list.addListSelectionListener(new ListHandler());
154
155		JPanel widgetsPanel = new JPanel(new BorderLayout());
156		widgetsPanel.add(new JScrollPane(list), BorderLayout.CENTER);
157		//}}}
158
159		//{{{ Create buttons
160		JPanel buttons = new JPanel();
161		buttons.setBorder(new EmptyBorder(3,0,0,0));
162		buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
163		ActionHandler actionHandler = new ActionHandler();
164		add = new RolloverButton(GUIUtilities.loadIcon("Plus.png"));
165		add.setToolTipText(jEdit.getProperty("options.status.add"));
166		add.addActionListener(actionHandler);
167		buttons.add(add);
168		buttons.add(Box.createHorizontalStrut(6));
169		remove = new RolloverButton(GUIUtilities.loadIcon("Minus.png"));
170		remove.setToolTipText(jEdit.getProperty("options.status.remove"));
171		remove.addActionListener(actionHandler);
172		buttons.add(remove);
173		buttons.add(Box.createHorizontalStrut(6));
174		moveUp = new RolloverButton(GUIUtilities.loadIcon("ArrowU.png"));
175		moveUp.setToolTipText(jEdit.getProperty("options.status.moveUp"));
176		moveUp.addActionListener(actionHandler);
177		buttons.add(moveUp);
178		buttons.add(Box.createHorizontalStrut(6));
179		moveDown = new RolloverButton(GUIUtilities.loadIcon("ArrowD.png"));
180		moveDown.setToolTipText(jEdit.getProperty("options.status.moveDown"));
181		moveDown.addActionListener(actionHandler);
182		buttons.add(moveDown);
183		buttons.add(Box.createHorizontalStrut(6));
184		edit = new RolloverButton(GUIUtilities.loadIcon("ButtonProperties.png"));
185		edit.setToolTipText(jEdit.getProperty("options.status.edit"));
186		edit.addActionListener(actionHandler);
187		buttons.add(edit);
188		buttons.add(Box.createGlue());
189		//}}}
190
191		updateButtons();
192		widgetsPanel.add(buttons, BorderLayout.SOUTH);
193
194
195		JTabbedPane tabs = new JTabbedPane();
196		tabs.addTab("Options",optionsPanel);
197		tabs.add("Widgets", widgetsPanel);
198
199		add(tabs, BorderLayout.CENTER);
200		updatePreview();
201	} ///}}}
202
203	//{{{ _save() method
204	@Override
205	protected void _save()
206	{
207		jEdit.setColorProperty("view.status.foreground",foregroundColor
208			.getSelectedColor());
209		jEdit.setColorProperty("view.status.background",backgroundColor
210			.getSelectedColor());
211		jEdit.setColorProperty("view.status.memory.foreground",memForegroundColor
212			.getSelectedColor());
213		jEdit.setColorProperty("view.status.memory.background",memBackgroundColor
214			.getSelectedColor());
215
216		jEdit.setBooleanProperty("view.status.visible",showStatusbar
217			.isSelected());
218
219		jEdit.setBooleanProperty("view.status.plainview.visible",showStatusbarPlain
220			.isSelected());
221
222		StringBuilder buf = new StringBuilder();
223		for(int i = 0; i < listModel.getSize(); i++)
224		{
225			if(i != 0)
226				buf.append(' ');
227
228			String widgetName = (String) listModel.elementAt(i);
229			buf.append(widgetName);
230		}
231		jEdit.setProperty("view.status",buf.toString());
232
233		jEdit.setBooleanProperty("view.status.show-caret-linenumber", showCaretLineNumber.isSelected());
234		jEdit.setBooleanProperty("view.status.show-caret-dot", showCaretDot.isSelected());
235		jEdit.setBooleanProperty("view.status.show-caret-virtual", showCaretVirtual.isSelected());
236		jEdit.setBooleanProperty("view.status.show-caret-offset", showCaretOffset.isSelected());
237		jEdit.setBooleanProperty("view.status.show-caret-bufferlength", showCaretBufferLength.isSelected());
238
239	} //}}}
240
241	//{{{ Private members
242
243	//{{{ Instance variables
244	private ColorWellButton foregroundColor;
245	private ColorWellButton backgroundColor;
246	private ColorWellButton memForegroundColor;
247	private ColorWellButton memBackgroundColor;
248	private JCheckBox showStatusbar;
249	private JCheckBox showStatusbarPlain;
250	private DefaultListModel listModel;
251	private JLabel previewStatusBar;
252	private JList list;
253	private RolloverButton add;
254	private RolloverButton remove;
255	private RolloverButton moveUp, moveDown;
256	private RolloverButton edit;
257
258	private JCheckBox showCaretLineNumber;
259	private JCheckBox showCaretDot;
260	private JCheckBox showCaretVirtual;
261	private JCheckBox showCaretOffset;
262	private JCheckBox showCaretBufferLength;
263	//}}}
264
265	//{{{ updateButtons() method
266	private void updateButtons()
267	{
268		int index = list.getSelectedIndex();
269		remove.setEnabled(index != -1 && listModel.getSize() != 0);
270		moveUp.setEnabled(index > 0);
271		moveDown.setEnabled(index != -1 && index != listModel.getSize() - 1);
272		edit.setEnabled(index != -1);
273	} //}}}
274
275	//{{{ updateButtons() method
276	/**
277	 * Update the preview
278	 */
279	private void updatePreview()
280	{
281		StringBuilder buf = new StringBuilder();
282		for(int i = 0; i < listModel.getSize(); i++)
283		{
284			if (i != 0)
285				buf.append(' ');
286			String widgetName = (String) listModel.elementAt(i);
287			String sample = jEdit.getProperty("statusbar."+widgetName+".sample",widgetName);
288			buf.append(sample);
289		}
290		previewStatusBar.setText(buf.toString());
291	} //}}}
292
293	//}}}
294
295	//{{{ Inner classes
296
297	//{{{ ActionHandler class
298	private class ActionHandler implements ActionListener
299	{
300		public void actionPerformed(ActionEvent evt)
301		{
302			Object source = evt.getSource();
303
304			if(source == add)
305			{
306				String value = selectWidget();
307				if (value == null)
308					return;
309
310
311				int index = list.getSelectedIndex();
312				if(index == -1)
313					index = listModel.getSize();
314				else
315					index++;
316
317				listModel.insertElementAt(value,index);
318				list.setSelectedIndex(index);
319				list.ensureIndexIsVisible(index);
320				updatePreview();
321			}
322			else if(source == remove)
323			{
324				int index = list.getSelectedIndex();
325				listModel.removeElementAt(index);
326				if(listModel.getSize() != 0)
327				{
328					if(listModel.getSize() == index)
329						list.setSelectedIndex(index-1);
330					else
331						list.setSelectedIndex(index);
332				}
333				updateButtons();
334				updatePreview();
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				updatePreview();
345			}
346			else if(source == moveDown)
347			{
348				int index = list.getSelectedIndex();
349				Object selected = list.getSelectedValue();
350				listModel.removeElementAt(index);
351				listModel.insertElementAt(selected,index+1);
352				list.setSelectedIndex(index+1);
353				list.ensureIndexIsVisible(index+1);
354				updatePreview();
355			}
356			else if(source == edit)
357			{
358				String value = selectWidget();
359				if (value == null)
360					return;
361
362				int index = list.getSelectedIndex();
363
364				listModel.insertElementAt(value,index);
365				list.setSelectedIndex(index);
366				list.ensureIndexIsVisible(index);
367				updatePreview();
368			}
369		}
370
371		private String selectWidget()
372		{
373			WidgetSelectionDialog dialog = new WidgetSelectionDialog(StatusBarOptionPane.this);
374			String value = dialog.getValue();
375			if (value != null && value.length() == 0)
376				value = null;
377			return value;
378		}
379	} //}}}
380
381	//{{{ ListHandler class
382	private class ListHandler implements ListSelectionListener
383	{
384		public void valueChanged(ListSelectionEvent evt)
385		{
386			updateButtons();
387		}
388	} //}}}
389
390	private class WidgetListCellRenderer extends DefaultListCellRenderer
391	{
392		@Override
393		public Component getListCellRendererComponent(JList list, Object value, int index,
394							      boolean isSelected, boolean cellHasFocus)
395		{
396			super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
397			if (value == null)
398				return this;
399			String widget = String.valueOf(value);
400			String label = jEdit.getProperty("statusbar."+widget+".label", widget);
401			setText(label);
402			return this;
403		}
404	}
405
406	//}}}
407
408	//{{{ WidgetSelectionDialog class
409	private class WidgetSelectionDialog extends EnhancedDialog
410	{
411		private JButton ok;
412		private JButton cancel;
413		private JTextField labelField;
414		private JLabel labelLabel;
415		private JRadioButton labelRadio;
416		private JComboBox widgetCombo;
417		private JLabel widgetLabel;
418		private JRadioButton widgetRadio;
419		private String value;
420
421		//{{{ WidgetSelectionDialog constructor
422		WidgetSelectionDialog(Component comp)
423		{
424			super(GUIUtilities.getParentDialog(comp),
425			      jEdit.getProperty("options.status.edit.title"),
426			      true);
427			ButtonGroup buttonGroup = new ButtonGroup();
428			labelRadio = new JRadioButton(jEdit.getProperty("options.status.edit.labelRadioButton"));
429			widgetRadio = new JRadioButton(jEdit.getProperty("options.status.edit.widgetRadioButton"));
430			buttonGroup.add(labelRadio);
431			buttonGroup.add(widgetRadio);
432
433			labelLabel = new JLabel(jEdit.getProperty("options.status.edit.labelLabel"));
434			labelField = new JTextField();
435
436			widgetLabel = new JLabel(jEdit.getProperty("options.status.edit.widgetLabel"));
437
438			String[] allWidgets = ServiceManager.getServiceNames("org.gjt.sp.jedit.gui.statusbar.StatusWidget");
439			Vector<String> widgets = new Vector<String>(allWidgets.length);
440			Set<String> usedWidget = new HashSet<String>(listModel.getSize());
441			for (int i = 0; i < listModel.getSize(); i++)
442			{
443				usedWidget.add((String) listModel.get(i));
444			}
445			for (String widget : allWidgets)
446			{
447				if (!usedWidget.contains(widget))
448					widgets.add(widget);
449			}
450			widgetCombo = new JComboBox(widgets);
451			widgetCombo.setRenderer(new WidgetListCellRenderer());
452			ActionHandler actionHandler = new ActionHandler();
453			labelRadio.addActionListener(actionHandler);
454			widgetRadio.addActionListener(actionHandler);
455			//{{{ south panel
456			JPanel southPanel = new JPanel();
457			southPanel.setLayout(new BoxLayout(southPanel,BoxLayout.X_AXIS));
458			southPanel.setBorder(new EmptyBorder(12,0,0,0));
459			southPanel.add(Box.createGlue());
460			ok = new JButton(jEdit.getProperty("common.ok"));
461			ok.addActionListener(actionHandler);
462			getRootPane().setDefaultButton(ok);
463			southPanel.add(ok);
464			southPanel.add(Box.createHorizontalStrut(6));
465			cancel = new JButton(jEdit.getProperty("common.cancel"));
466			cancel.addActionListener(actionHandler);
467			southPanel.add(cancel);
468			southPanel.add(Box.createGlue());
469			//}}}
470
471			labelField.setEnabled(false);
472			widgetRadio.setSelected(true);
473
474
475			JPanel content = new JPanel(new BorderLayout());
476			content.setBorder(new EmptyBorder(12,12,12,12));
477			setContentPane(content);
478			JPanel center = new JPanel();
479			center.setLayout(new BoxLayout(center,BoxLayout.Y_AXIS));
480
481			center.add(labelRadio);
482			JPanel p = new JPanel(new BorderLayout());
483			p.add(labelLabel, BorderLayout.WEST);
484			p.add(labelField);
485			center.add(p);
486			center.add(widgetRadio);
487			p = new JPanel(new BorderLayout());
488			p.add(widgetLabel, BorderLayout.WEST);
489			p.add(widgetCombo);
490			if (widgets.isEmpty())
491			{
492				labelRadio.setSelected(true);
493				widgetRadio.setEnabled(false);
494				widgetLabel.setEnabled(false);
495				widgetCombo.setEnabled(false);
496			}
497			center.add(p);
498
499
500
501			getContentPane().add(center, BorderLayout.CENTER);
502			getContentPane().add(southPanel, BorderLayout.SOUTH);
503			pack();
504			setLocationRelativeTo(GUIUtilities.getParentDialog(comp));
505			setVisible(true);
506		} //}}}
507
508		//{{{ ok() method
509		@Override
510		public void ok()
511		{
512			if (widgetRadio.isSelected())
513			{
514				value = (String) widgetCombo.getSelectedItem();
515			}
516			else
517			{
518				value = labelField.getText().trim();
519			}
520			dispose();
521		} //}}}
522
523		//{{{ cancel() method
524		@Override
525		public void cancel()
526		{
527			value = null;
528			dispose();
529		} //}}}
530
531		//{{{ getValue() method
532		public String getValue()
533		{
534			return value;
535		} //}}}
536
537		//{{{ ActionHandler class
538		private class ActionHandler implements ActionListener
539		{
540			//{{{ actionPerformed() method
541			public void actionPerformed(ActionEvent evt)
542			{
543				Object source = evt.getSource();
544				if (source == ok)
545				{
546					ok();
547				}
548				else if (source == cancel)
549				{
550					cancel();
551				}
552				else if (source == labelRadio)
553				{
554					labelField.setEnabled(true);
555					widgetCombo.setEnabled(false);
556					validate();
557				}
558				else if (source == widgetRadio)
559				{
560					labelField.setEnabled(false);
561					widgetCombo.setEnabled(true);
562					validate();
563				}
564			} //}}}
565
566		} //}}}
567
568	} //}}}
569
570}
571