PageRenderTime 76ms CodeModel.GetById 60ms app.highlight 13ms RepoModel.GetById 0ms app.codeStats 1ms

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

#
Java | 415 lines | 295 code | 55 blank | 65 comment | 28 complexity | 81326f9840978a6943eea68d3310fcf0 MD5 | raw file
  1/*
  2 * SyntaxHiliteOptionPane.java - Syntax highlighting option pane
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 1999, 2000, 2001 Slava Pestov
  7 * Portions copyright (C) 1999 mike dillon
  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.EmptyBorder;
 28import javax.swing.table.*;
 29import javax.swing.*;
 30import java.awt.event.*;
 31import java.awt.*;
 32import java.util.Vector;
 33import org.gjt.sp.jedit.syntax.*;
 34import org.gjt.sp.jedit.gui.ColorWellButton;
 35import org.gjt.sp.jedit.gui.EnhancedDialog;
 36import org.gjt.sp.jedit.*;
 37//}}}
 38
 39//{{{ SyntaxHiliteOptionPane class
 40/**
 41 * Style option pane.
 42 * @author Slava Pestov
 43 * @version $Id: SyntaxHiliteOptionPane.java 4795 2003-06-19 01:28:08Z spestov $
 44 */
 45public class SyntaxHiliteOptionPane extends AbstractOptionPane
 46{
 47	public static final EmptyBorder noFocusBorder = new EmptyBorder(1,1,1,1);
 48
 49	//{{{ StyleOptionPane constructor
 50	public SyntaxHiliteOptionPane()
 51	{
 52		super("syntax");
 53	}
 54	//}}}
 55
 56	//{{{ Protected members
 57
 58	//{{{ _init() method
 59	protected void _init()
 60	{
 61		setLayout(new BorderLayout(6,6));
 62
 63		add(BorderLayout.CENTER,createStyleTableScroller());
 64	} //}}}
 65
 66	//{{{ _save() method
 67	protected void _save()
 68	{
 69		styleModel.save();
 70	} //}}}
 71
 72	//}}}
 73
 74	//{{{ Private members
 75	private StyleTableModel styleModel;
 76	private JTable styleTable;
 77
 78	//{{{ createStyleTableScroller() method
 79	private JScrollPane createStyleTableScroller()
 80	{
 81		styleModel = createStyleTableModel();
 82		styleTable = new JTable(styleModel);
 83		styleTable.setRowSelectionAllowed(false);
 84		styleTable.setColumnSelectionAllowed(false);
 85		styleTable.setCellSelectionEnabled(false);
 86		styleTable.getTableHeader().setReorderingAllowed(false);
 87		styleTable.addMouseListener(new MouseHandler());
 88		TableColumnModel tcm = styleTable.getColumnModel();
 89 		TableColumn styleColumn = tcm.getColumn(1);
 90		styleColumn.setCellRenderer(new StyleTableModel.StyleRenderer());
 91		Dimension d = styleTable.getPreferredSize();
 92		d.height = Math.min(d.height,100);
 93		JScrollPane scroller = new JScrollPane(styleTable);
 94		scroller.setPreferredSize(d);
 95		return scroller;
 96	} //}}}
 97
 98	//{{{ createStyleTableModel() method
 99	private StyleTableModel createStyleTableModel()
100	{
101		return new StyleTableModel();
102	} //}}}
103
104	//}}}
105
106	//{{{ MouseHandler class
107	class MouseHandler extends MouseAdapter
108	{
109		public void mouseClicked(MouseEvent evt)
110		{
111			int row = styleTable.rowAtPoint(evt.getPoint());
112			if(row == -1)
113				return;
114
115			SyntaxStyle style = new StyleEditor(
116				SyntaxHiliteOptionPane.this,
117				(SyntaxStyle)styleModel.getValueAt(
118				row,1)).getStyle();
119			if(style != null)
120				styleModel.setValueAt(style,row,1);
121		}
122	} //}}}
123} //}}}
124
125//{{{ StyleTableModel class
126class StyleTableModel extends AbstractTableModel
127{
128	private Vector styleChoices;
129
130	//{{{ StyleTableModel constructor
131	StyleTableModel()
132	{
133		styleChoices = new Vector(Token.ID_COUNT + 4);
134		// start at 1 not 0 to skip Token.NULL
135		for(int i = 1; i < Token.ID_COUNT; i++)
136		{
137			String tokenName = Token.tokenToString((byte)i);
138			addStyleChoice(tokenName,"view.style." + tokenName.toLowerCase());
139		}
140
141		addStyleChoice(jEdit.getProperty("options.syntax.foldLine.1"),
142			"view.style.foldLine.1");
143		addStyleChoice(jEdit.getProperty("options.syntax.foldLine.2"),
144			"view.style.foldLine.2");
145		addStyleChoice(jEdit.getProperty("options.syntax.foldLine.3"),
146			"view.style.foldLine.3");
147		addStyleChoice(jEdit.getProperty("options.syntax.foldLine.0"),
148			"view.style.foldLine.0");
149
150		MiscUtilities.quicksort(styleChoices,new MiscUtilities.StringICaseCompare());
151	} //}}}
152
153	//{{{ getColumnCount() method
154	public int getColumnCount()
155	{
156		return 2;
157	} //}}}
158
159	//{{{ getRowCount() method
160	public int getRowCount()
161	{
162		return styleChoices.size();
163	} //}}}
164
165	//{{{ getValueAt() method
166	public Object getValueAt(int row, int col)
167	{
168		StyleChoice ch = (StyleChoice)styleChoices.elementAt(row);
169		switch(col)
170		{
171		case 0:
172			return ch.label;
173		case 1:
174			return ch.style;
175		default:
176			return null;
177		}
178	} //}}}
179
180	//{{{ setValueAt() method
181	public void setValueAt(Object value, int row, int col)
182	{
183		StyleChoice ch = (StyleChoice)styleChoices.elementAt(row);
184		if(col == 1)
185			ch.style = (SyntaxStyle)value;
186		fireTableRowsUpdated(row,row);
187	} //}}}
188
189	//{{{ getColumnName() method
190	public String getColumnName(int index)
191	{
192		switch(index)
193		{
194		case 0:
195			return jEdit.getProperty("options.syntax.object");
196		case 1:
197			return jEdit.getProperty("options.syntax.style");
198		default:
199			return null;
200		}
201	} //}}}
202
203	//{{{ save() method
204	public void save()
205	{
206		for(int i = 0; i < styleChoices.size(); i++)
207		{
208			StyleChoice ch = (StyleChoice)styleChoices
209				.elementAt(i);
210			jEdit.setProperty(ch.property,
211				GUIUtilities.getStyleString(ch.style));
212		}
213	} //}}}
214
215	//{{{ addStyleChoice() method
216	private void addStyleChoice(String label, String property)
217	{
218		styleChoices.addElement(new StyleChoice(label,
219			property,
220			GUIUtilities.parseStyle(jEdit.getProperty(property),
221			"Dialog",12)));
222	} //}}}
223
224	//{{{ StyleChoice class
225	static class StyleChoice
226	{
227		String label;
228		String property;
229		SyntaxStyle style;
230
231		StyleChoice(String label, String property, SyntaxStyle style)
232		{
233			this.label = label;
234			this.property = property;
235			this.style = style;
236		}
237
238		// for sorting
239		public String toString()
240		{
241			return label;
242		}
243	} //}}}
244
245	//{{{ StyleRenderer class
246	static class StyleRenderer extends JLabel
247		implements TableCellRenderer
248	{
249		//{{{ StyleRenderer constructor
250		public StyleRenderer()
251		{
252			setOpaque(true);
253			setBorder(SyntaxHiliteOptionPane.noFocusBorder);
254			setText("Hello World");
255		} //}}}
256
257		//{{{ getTableCellRendererComponent() method
258		public Component getTableCellRendererComponent(
259			JTable table,
260			Object value,
261			boolean isSelected,
262			boolean cellHasFocus,
263			int row,
264			int col)
265		{
266			if (value != null)
267			{
268				SyntaxStyle style = (SyntaxStyle)value;
269				setForeground(style.getForegroundColor());
270				if (style.getBackgroundColor() != null) 
271					setBackground(style.getBackgroundColor());
272				else
273				{
274					// this part sucks
275					setBackground(jEdit.getColorProperty(
276						"view.bgColor"));
277				}
278				setFont(style.getFont());
279			}
280
281			setBorder((cellHasFocus) ? UIManager.getBorder(
282				"Table.focusCellHighlightBorder")
283				: SyntaxHiliteOptionPane.noFocusBorder);
284			return this;
285		} //}}}
286	} //}}}
287} //}}}
288
289//{{{ StyleEditor class
290class StyleEditor extends EnhancedDialog implements ActionListener
291{
292	//{{{ StyleEditor constructor
293	StyleEditor(Component comp, SyntaxStyle style)
294	{
295		super(GUIUtilities.getParentDialog(comp),
296			jEdit.getProperty("style-editor.title"),true);
297
298		JPanel content = new JPanel(new BorderLayout(12,12));
299		content.setBorder(new EmptyBorder(12,12,12,12));
300		setContentPane(content);
301
302		JPanel panel = new JPanel(new GridLayout(4,2,12,12));
303
304		italics = new JCheckBox(jEdit.getProperty("style-editor.italics"));
305		italics.setSelected(style.getFont().isItalic());
306		panel.add(italics);
307		panel.add(new JLabel());
308
309		bold = new JCheckBox(jEdit.getProperty("style-editor.bold"));
310		bold.setSelected(style.getFont().isBold());
311		panel.add(bold);
312		panel.add(new JLabel());
313
314		Color fg = style.getForegroundColor();
315
316		fgColorCheckBox = new JCheckBox(jEdit.getProperty("style-editor.fgColor"));
317		fgColorCheckBox.setSelected(fg != null);
318		fgColorCheckBox.addActionListener(this);
319		panel.add(fgColorCheckBox);
320
321		fgColor = new ColorWellButton(fg);
322		fgColor.setEnabled(fg != null);
323		panel.add(fgColor);
324
325		Color bg = style.getBackgroundColor();
326		bgColorCheckBox = new JCheckBox(jEdit.getProperty("style-editor.bgColor"));
327		bgColorCheckBox.setSelected(bg != null);
328		bgColorCheckBox.addActionListener(this);
329		panel.add(bgColorCheckBox);
330
331		bgColor = new ColorWellButton(bg);
332		bgColor.setEnabled(bg != null);
333		panel.add(bgColor);
334
335		content.add(BorderLayout.CENTER,panel);
336
337		Box box = new Box(BoxLayout.X_AXIS);
338		box.add(Box.createGlue());
339		box.add(ok = new JButton(jEdit.getProperty("common.ok")));
340		getRootPane().setDefaultButton(ok);
341		ok.addActionListener(this);
342		box.add(Box.createHorizontalStrut(6));
343		box.add(cancel = new JButton(jEdit.getProperty("common.cancel")));
344		cancel.addActionListener(this);
345		box.add(Box.createGlue());
346
347		content.add(BorderLayout.SOUTH,box);
348
349		pack();
350		setLocationRelativeTo(GUIUtilities.getParentDialog(comp));
351
352		setResizable(false);
353		show();
354	} //}}}
355
356	//{{{ actionPerformed() method
357	public void actionPerformed(ActionEvent evt)
358	{
359		Object source = evt.getSource();
360		if(source == ok)
361			ok();
362		else if(source == cancel)
363			cancel();
364		else if(source == fgColorCheckBox)
365			fgColor.setEnabled(fgColorCheckBox.isSelected());
366		else if(source == bgColorCheckBox)
367			bgColor.setEnabled(bgColorCheckBox.isSelected());
368	} //}}}
369
370	//{{{ ok() method
371	public void ok()
372	{
373		okClicked = true;
374		dispose();
375	} //}}}
376
377	//{{{ cancel() method
378	public void cancel()
379	{
380		dispose();
381	} //}}}
382
383	//{{{ getStyle() method
384	public SyntaxStyle getStyle()
385	{
386		if(!okClicked)
387			return null;
388
389		Color foreground = (fgColorCheckBox.isSelected()
390			? fgColor.getSelectedColor()
391			: null);
392
393		Color background = (bgColorCheckBox.isSelected()
394			? bgColor.getSelectedColor()
395			: null);
396
397		return new SyntaxStyle(foreground,background,
398				new Font("Dialog",
399				(italics.isSelected() ? Font.ITALIC : 0)
400				| (bold.isSelected() ? Font.BOLD : 0),
401				12));
402	} //}}}
403
404	//{{{ Private members
405	private JCheckBox italics;
406	private JCheckBox bold;
407	private JCheckBox fgColorCheckBox;
408	private ColorWellButton fgColor;
409	private JCheckBox bgColorCheckBox;
410	private ColorWellButton bgColor;
411	private JButton ok;
412	private JButton cancel;
413	private boolean okClicked;
414	//}}}
415} //}}}