PageRenderTime 218ms CodeModel.GetById 184ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

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

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