PageRenderTime 26ms CodeModel.GetById 1ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 345 lines | 249 code | 40 blank | 56 comment | 20 complexity | 28e4b7153a4d12d36994159dce093957 MD5 | raw file
  1/*
  2 * BrowserColorsOptionPane.java - Browser colors options panel
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2001, 2002 Slava Pestov
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22
 23package org.gjt.sp.jedit.options;
 24
 25//{{{ Imports
 26import javax.swing.border.EmptyBorder;
 27import javax.swing.event.*;
 28import javax.swing.table.*;
 29import javax.swing.*;
 30import java.awt.event.*;
 31import java.awt.*;
 32import java.util.*;
 33import org.gjt.sp.jedit.gui.RolloverButton;
 34import org.gjt.sp.jedit.*;
 35//}}}
 36
 37//{{{ BrowserColorsOptionPane class
 38/**
 39 * Browser color editor.
 40 * @author Slava Pestov
 41 * @version $Id: BrowserColorsOptionPane.java 4393 2002-12-24 17:35:24Z spestov $
 42 */
 43public class BrowserColorsOptionPane extends AbstractOptionPane
 44{
 45	//{{{ BrowserColorsOptionPane constructor
 46	public BrowserColorsOptionPane()
 47	{
 48		super("browser.colors");
 49	} //}}}
 50
 51	//{{{ Protected members
 52
 53	//{{{ _init() method
 54	protected void _init()
 55	{
 56		setLayout(new BorderLayout());
 57
 58		colorsModel = new BrowserColorsModel();
 59		colorsTable = new JTable(colorsModel);
 60		colorsTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
 61		colorsTable.getTableHeader().setReorderingAllowed(false);
 62		colorsTable.addMouseListener(new MouseHandler());
 63		colorsTable.getSelectionModel().addListSelectionListener(
 64			new SelectionHandler());
 65		TableColumnModel tcm = colorsTable.getColumnModel();
 66		tcm.getColumn(1).setCellRenderer(new BrowserColorsModel.ColorRenderer());
 67		Dimension d = colorsTable.getPreferredSize();
 68		d.height = Math.min(d.height,200);
 69		JScrollPane scroller = new JScrollPane(colorsTable);
 70		scroller.setPreferredSize(d);
 71		add(BorderLayout.CENTER,scroller);
 72
 73		JPanel buttons = new JPanel();
 74		buttons.setBorder(new EmptyBorder(3,0,0,0));
 75		buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
 76		ActionHandler actionHandler = new ActionHandler();
 77		add = new RolloverButton(GUIUtilities.loadIcon("Plus.png"));
 78		add.setToolTipText(jEdit.getProperty("options.browser.colors.add"));
 79		add.addActionListener(actionHandler);
 80		buttons.add(add);
 81		buttons.add(Box.createHorizontalStrut(6));
 82		remove = new RolloverButton(GUIUtilities.loadIcon("Minus.png"));
 83		remove.setToolTipText(jEdit.getProperty("options.browser.colors.remove"));
 84		remove.addActionListener(actionHandler);
 85		buttons.add(remove);
 86		buttons.add(Box.createGlue());
 87
 88		add(BorderLayout.SOUTH,buttons);
 89
 90		updateEnabled();
 91	} //}}}
 92
 93	//{{{ _save() method
 94	protected void _save()
 95	{
 96		colorsModel.save();
 97	} //}}}
 98
 99	//}}}
100
101	//{{{ Private members
102	private BrowserColorsModel colorsModel;
103	private JTable colorsTable;
104	private JButton add;
105	private JButton remove;
106
107	//{{{ updateEnabled() method
108	private void updateEnabled()
109	{
110		int selectedRow = colorsTable.getSelectedRow();
111		remove.setEnabled(selectedRow != -1);
112	} //}}}
113
114	//}}}
115
116	//{{{ SelectionHandler class
117	class SelectionHandler implements ListSelectionListener
118	{
119		public void valueChanged(ListSelectionEvent evt)
120		{
121			updateEnabled();
122		}
123	} //}}}
124
125	//{{{ ActionHandler class
126	class ActionHandler implements ActionListener
127	{
128		public void actionPerformed(ActionEvent evt)
129		{
130			Object source = evt.getSource();
131			if(source == add)
132			{
133				colorsModel.add();
134			}
135			else if(source == remove)
136			{
137				int selectedRow = colorsTable.getSelectedRow();
138				colorsModel.remove(selectedRow);
139				updateEnabled();
140			}
141		}
142	} //}}}
143
144	//{{{ MouseHandler class
145	class MouseHandler extends MouseAdapter
146	{
147		public void mouseClicked(MouseEvent evt)
148		{
149			Point p = evt.getPoint();
150			int row = colorsTable.rowAtPoint(p);
151			int column = colorsTable.columnAtPoint(p);
152			if(row == -1 || column != 1)
153				return;
154
155			Color color = JColorChooser.showDialog(
156				BrowserColorsOptionPane.this,
157				jEdit.getProperty("colorChooser.title"),
158				(Color)colorsModel.getValueAt(row,1));
159			if(color != null)
160				colorsModel.setValueAt(color,row,1);
161		}
162	} //}}}
163} //}}}
164
165//{{{ BrowserColorsModel class
166class BrowserColorsModel extends AbstractTableModel
167{
168	Vector entries;
169
170	//{{{ BrowserColorsModel constructor
171	BrowserColorsModel()
172	{
173		entries = new Vector();
174
175		int i = 0;
176		String glob;
177		while((glob = jEdit.getProperty("vfs.browser.colors." + i + ".glob")) != null)
178		{
179			entries.addElement(new Entry(glob,
180				jEdit.getColorProperty(
181				"vfs.browser.colors." + i + ".color",
182				Color.black)));
183			i++;
184		}
185	} //}}}
186
187	//{{{ add() method
188	void add()
189	{
190		entries.addElement(new Entry("",UIManager.getColor("Tree.foreground")));
191		fireTableRowsInserted(entries.size() - 1,entries.size() - 1);
192	} //}}}
193
194	//{{{ remove() method
195	void remove(int index)
196	{
197		entries.removeElementAt(index);
198		fireTableRowsDeleted(entries.size(),entries.size());
199	} //}}}
200
201	//{{{ save() method
202	void save()
203	{
204		int i;
205		for(i = 0; i < entries.size(); i++)
206		{
207			Entry entry = (Entry)entries.elementAt(i);
208			jEdit.setProperty("vfs.browser.colors." + i + ".glob",
209				entry.glob);
210			jEdit.setColorProperty("vfs.browser.colors." + i + ".color",
211				entry.color);
212		}
213		jEdit.unsetProperty("vfs.browser.colors." + i + ".glob");
214		jEdit.unsetProperty("vfs.browser.colors." + i + ".color");
215	} //}}}
216
217	//{{{ getColumnCount() method
218	public int getColumnCount()
219	{
220		return 2;
221	} //}}}
222
223	//{{{ getRowCount() method
224	public int getRowCount()
225	{
226		return entries.size();
227	} //}}}
228
229	//{{{ getValueAt() method
230	public Object getValueAt(int row, int col)
231	{
232		Entry entry = (Entry)entries.elementAt(row);
233
234		switch(col)
235		{
236		case 0:
237			return entry.glob;
238		case 1:
239			return entry.color;
240		default:
241			return null;
242		}
243	} //}}}
244
245	//{{{ isCellEditable() method
246	public boolean isCellEditable(int row, int col)
247	{
248		return (col == 0);
249	} //}}}
250
251	//{{{ setValueAt() method
252	public void setValueAt(Object value, int row, int col)
253	{
254		Entry entry = (Entry)entries.elementAt(row);
255
256		if(col == 0)
257			entry.glob = (String)value;
258		else
259			entry.color = (Color)value;
260
261		fireTableRowsUpdated(row,row);
262	} //}}}
263
264	//{{{ getColumnName() method
265	public String getColumnName(int index)
266	{
267		switch(index)
268		{
269		case 0:
270			return jEdit.getProperty("options.browser.colors.glob");
271		case 1:
272			return jEdit.getProperty("options.browser.colors.color");
273		default:
274			return null;
275		}
276	} //}}}
277
278	//{{{ getColumnClass() method
279	public Class getColumnClass(int col)
280	{
281		switch(col)
282		{
283		case 0:
284			return String.class;
285		case 1:
286			return Color.class;
287		default:
288			throw new InternalError();
289		}
290	} //}}}
291
292	//{{{ Entry class
293	static class Entry
294	{
295		String glob;
296		Color color;
297
298		Entry(String glob, Color color)
299		{
300			this.glob = glob;
301			this.color = color;
302		}
303	} //}}}
304
305	//{{{ ColorRenderer class
306	static class ColorRenderer extends JLabel
307		implements TableCellRenderer
308	{
309		//{{{ ColorRenderer constructor
310		public ColorRenderer()
311		{
312			setOpaque(true);
313			setBorder(SyntaxHiliteOptionPane.noFocusBorder);
314		} //}}}
315
316		//{{{ getTableCellRendererComponent() method
317		public Component getTableCellRendererComponent(
318			JTable table,
319			Object value,
320			boolean isSelected,
321			boolean cellHasFocus,
322			int row,
323			int col)
324		{
325			if (isSelected)
326			{
327				setBackground(table.getSelectionBackground());
328				setForeground(table.getSelectionForeground());
329			}
330			else
331			{
332				setBackground(table.getBackground());
333				setForeground(table.getForeground());
334			}
335
336			if (value != null)
337				setBackground((Color)value);
338
339			setBorder((cellHasFocus) ? UIManager.getBorder(
340				"Table.focusCellHighlightBorder")
341				: SyntaxHiliteOptionPane.noFocusBorder);
342			return this;
343		} //}}}
344	} //}}}
345} //}}}