PageRenderTime 107ms CodeModel.GetById 53ms app.highlight 49ms RepoModel.GetById 1ms app.codeStats 0ms

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

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