PageRenderTime 386ms CodeModel.GetById 353ms app.highlight 18ms RepoModel.GetById 12ms app.codeStats 0ms

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

#
Java | 298 lines | 214 code | 30 blank | 54 comment | 10 complexity | 3268cec68f9fc5b4c0d49fc4dc85903f MD5 | raw file
  1/*
  2 * ColorOptionPane.java - Color option pane
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 1999, 2000 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.plaf.metal.MetalLookAndFeel;
 30import javax.swing.table.*;
 31import javax.swing.*;
 32import java.awt.event.*;
 33import java.awt.*;
 34import java.util.Vector;
 35import org.gjt.sp.jedit.syntax.SyntaxStyle;
 36import org.gjt.sp.jedit.gui.EnhancedDialog;
 37import org.gjt.sp.jedit.*;
 38//}}}
 39
 40//{{{ ColorOptionPane class
 41/**
 42 * Color option pane.
 43 * @author Slava Pestov
 44 * @version $Id: ColorOptionPane.java 3905 2001-11-23 09:08:49Z spestov $
 45 */
 46public class ColorOptionPane extends AbstractOptionPane
 47{
 48	public static final EmptyBorder noFocusBorder = new EmptyBorder(1,1,1,1);
 49
 50	//{{{ ColorOptionPane constructor
 51	public ColorOptionPane()
 52	{
 53		super("color");
 54	} //}}}
 55
 56	//{{{ Protected members
 57
 58	//{{{ _init() method
 59	protected void _init()
 60	{
 61		setLayout(new BorderLayout());
 62		add(BorderLayout.CENTER,createColorTableScroller());
 63	} //}}}
 64
 65	//{{{ _save() method
 66	protected void _save()
 67	{
 68		colorModel.save();
 69	} //}}}
 70
 71	//}}}
 72
 73	//{{{ Private members
 74	private ColorTableModel colorModel;
 75	private JTable colorTable;
 76
 77	//{{{ createColorTableScroller() method
 78	private JScrollPane createColorTableScroller()
 79	{
 80		colorModel = createColorTableModel();
 81		colorTable = new JTable(colorModel);
 82		colorTable.setRowSelectionAllowed(false);
 83		colorTable.setColumnSelectionAllowed(false);
 84		colorTable.setCellSelectionEnabled(false);
 85		colorTable.getTableHeader().setReorderingAllowed(false);
 86		colorTable.addMouseListener(new MouseHandler());
 87		TableColumnModel tcm = colorTable.getColumnModel();
 88 		TableColumn colorColumn = tcm.getColumn(1);
 89		colorColumn.setCellRenderer(new ColorTableModel.ColorRenderer());
 90		Dimension d = colorTable.getPreferredSize();
 91		d.height = Math.min(d.height,100);
 92		JScrollPane scroller = new JScrollPane(colorTable);
 93		scroller.setPreferredSize(d);
 94		return scroller;
 95	} //}}}
 96
 97	//{{{ createColorTableModel() method
 98	private ColorTableModel createColorTableModel()
 99	{
100		return new ColorTableModel();
101	} //}}}
102
103	//}}}
104
105	//{{{ MouseHandler class
106	class MouseHandler extends MouseAdapter
107	{
108		public void mouseClicked(MouseEvent evt)
109		{
110			int row = colorTable.rowAtPoint(evt.getPoint());
111			if(row == -1)
112				return;
113
114			Color color = JColorChooser.showDialog(
115				ColorOptionPane.this,
116				jEdit.getProperty("colorChooser.title"),
117				(Color)colorModel.getValueAt(row,1));
118			if(color != null)
119				colorModel.setValueAt(color,row,1);
120		}
121	} //}}}
122} //}}}
123
124//{{{ ColorTableModel class
125class ColorTableModel extends AbstractTableModel
126{
127	private Vector colorChoices;
128
129	//{{{ ColorTableModel constructor
130	ColorTableModel()
131	{
132		colorChoices = new Vector(16);
133		addColorChoice("options.color.bgColor","view.bgColor");
134		addColorChoice("options.color.fgColor","view.fgColor");
135		addColorChoice("options.color.caretColor","view.caretColor");
136		addColorChoice("options.color.selectionColor",
137			"view.selectionColor");
138		addColorChoice("options.color.foldedLineColor",
139			"view.foldedLineColor");
140		addColorChoice("options.color.lineHighlightColor",
141			"view.lineHighlightColor");
142		addColorChoice("options.color.bracketHighlightColor",
143			"view.bracketHighlightColor");
144		addColorChoice("options.color.eolMarkerColor",
145			"view.eolMarkerColor");
146		addColorChoice("options.color.wrapGuideColor",
147			"view.wrapGuideColor");
148		addColorChoice("options.color.gutterBgColor",
149			"view.gutter.bgColor");
150		addColorChoice("options.color.gutterFgColor",
151			"view.gutter.fgColor");
152		addColorChoice("options.color.gutterHighlightColor",
153			"view.gutter.highlightColor");
154		addColorChoice("options.color.gutterCurrentLineColor",
155			"view.gutter.currentLineColor");
156		addColorChoice("options.color.gutterBracketHighlightColor",
157			"view.bracketHighlightColor");
158		addColorChoice("options.color.gutterMarkerColor",
159			"view.gutter.markerColor");
160		addColorChoice("options.color.gutterFoldColor",
161			"view.gutter.foldColor");
162		addColorChoice("options.color.gutterFocusBorderColor",
163			"view.gutter.focusBorderColor");
164		addColorChoice("options.color.gutterNoFocusBorderColor",
165			"view.gutter.noFocusBorderColor");
166		MiscUtilities.quicksort(colorChoices,new MiscUtilities.StringCompare());
167	} //}}}
168
169	//{{{ getColumnCount() method
170	public int getColumnCount()
171	{
172		return 2;
173	} //}}}
174
175	//{{{ getRowCount() method
176	public int getRowCount()
177	{
178		return colorChoices.size();
179	} //}}}
180
181	//{{{ getValueAt() method
182	public Object getValueAt(int row, int col)
183	{
184		ColorChoice ch = (ColorChoice)colorChoices.elementAt(row);
185		switch(col)
186		{
187		case 0:
188			return ch.label;
189		case 1:
190			return ch.color;
191		default:
192			return null;
193		}
194	} //}}}
195
196	//{{{ setValueAt() method
197	public void setValueAt(Object value, int row, int col)
198	{
199		ColorChoice ch = (ColorChoice)colorChoices.elementAt(row);
200		if(col == 1)
201			ch.color = (Color)value;
202		fireTableRowsUpdated(row,row);
203	} //}}}
204
205	//{{{ getColumnName() method
206	public String getColumnName(int index)
207	{
208		switch(index)
209		{
210		case 0:
211			return jEdit.getProperty("options.color.object");
212		case 1:
213			return jEdit.getProperty("options.color.color");
214		default:
215			return null;
216		}
217	} //}}}
218
219	//{{{ save() method
220	public void save()
221	{
222		for(int i = 0; i < colorChoices.size(); i++)
223		{
224			ColorChoice ch = (ColorChoice)colorChoices
225				.elementAt(i);
226			jEdit.setColorProperty(ch.property,ch.color);
227		}
228	} //}}}
229
230	//{{{ addColorChoice() method
231	private void addColorChoice(String label, String property)
232	{
233		colorChoices.addElement(new ColorChoice(jEdit.getProperty(label),
234			property,jEdit.getColorProperty(property)));
235	} //}}}
236
237	//{{{ ColorChoice class
238	static class ColorChoice
239	{
240		String label;
241		String property;
242		Color color;
243
244		ColorChoice(String label, String property, Color color)
245		{
246			this.label = label;
247			this.property = property;
248			this.color = color;
249		}
250
251		// for sorting
252		public String toString()
253		{
254			return label;
255		}
256	} //}}}
257
258	//{{{ ColorRenderer class
259	static class ColorRenderer extends JLabel
260		implements TableCellRenderer
261	{
262		//{{{ ColorRenderer constructor
263		public ColorRenderer()
264		{
265			setOpaque(true);
266			setBorder(StyleOptionPane.noFocusBorder);
267		} //}}}
268
269		//{{{ getTableCellRendererComponent() method
270		public Component getTableCellRendererComponent(
271			JTable table,
272			Object value,
273			boolean isSelected,
274			boolean cellHasFocus,
275			int row,
276			int col)
277		{
278			if (isSelected)
279			{
280				setBackground(table.getSelectionBackground());
281				setForeground(table.getSelectionForeground());
282			}
283			else
284			{
285				setBackground(table.getBackground());
286				setForeground(table.getForeground());
287			}
288
289			if (value != null)
290				setBackground((Color)value);
291
292			setBorder((cellHasFocus) ? UIManager.getBorder(
293				"Table.focusCellHighlightBorder")
294				: StyleOptionPane.noFocusBorder);
295			return this;
296		} //}}}
297	} //}}}
298} //}}}