PageRenderTime 94ms CodeModel.GetById 46ms app.highlight 37ms RepoModel.GetById 7ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/options/SyntaxHiliteOptionPane.java

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