PageRenderTime 133ms CodeModel.GetById 107ms app.highlight 22ms RepoModel.GetById 0ms app.codeStats 1ms

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

#
Java | 343 lines | 246 code | 41 blank | 56 comment | 20 complexity | 92820a63f01ac78191275c048e7276e9 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 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.*;
 34//}}}
 35
 36//{{{ BrowserColorsOptionPane class
 37/**
 38 * Browser color editor.
 39 * @author Slava Pestov
 40 * @version $Id: BrowserColorsOptionPane.java 4288 2002-08-02 17:31:58Z spestov $
 41 */
 42public class BrowserColorsOptionPane extends AbstractOptionPane
 43{
 44	//{{{ BrowserColorsOptionPane constructor
 45	public BrowserColorsOptionPane()
 46	{
 47		super("browser.colors");
 48	} //}}}
 49
 50	//{{{ Protected members
 51
 52	//{{{ _init() method
 53	protected void _init()
 54	{
 55		setLayout(new BorderLayout());
 56
 57		colorsModel = new BrowserColorsModel();
 58		colorsTable = new JTable(colorsModel);
 59		colorsTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
 60		colorsTable.getTableHeader().setReorderingAllowed(false);
 61		colorsTable.addMouseListener(new MouseHandler());
 62		colorsTable.getSelectionModel().addListSelectionListener(
 63			new SelectionHandler());
 64		TableColumnModel tcm = colorsTable.getColumnModel();
 65		tcm.getColumn(1).setCellRenderer(new BrowserColorsModel.ColorRenderer());
 66		Dimension d = colorsTable.getPreferredSize();
 67		d.height = Math.min(d.height,200);
 68		JScrollPane scroller = new JScrollPane(colorsTable);
 69		scroller.setPreferredSize(d);
 70		add(BorderLayout.CENTER,scroller);
 71
 72		JPanel buttons = new JPanel();
 73		buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
 74		buttons.setBorder(new EmptyBorder(6,0,0,0));
 75
 76		buttons.add(Box.createGlue());
 77		add = new JButton(jEdit.getProperty("options.browser.colors.add"));
 78		add.addActionListener(new ActionHandler());
 79		buttons.add(add);
 80		buttons.add(Box.createHorizontalStrut(6));
 81		remove = new JButton(jEdit.getProperty("options.browser.colors.remove"));
 82		remove.addActionListener(new ActionHandler());
 83		buttons.add(remove);
 84		buttons.add(Box.createGlue());
 85
 86		add(BorderLayout.SOUTH,buttons);
 87
 88		updateEnabled();
 89	} //}}}
 90
 91	//{{{ _save() method
 92	protected void _save()
 93	{
 94		colorsModel.save();
 95	} //}}}
 96
 97	//}}}
 98
 99	//{{{ Private members
100	private BrowserColorsModel colorsModel;
101	private JTable colorsTable;
102	private JButton add;
103	private JButton remove;
104
105	//{{{ updateEnabled() method
106	private void updateEnabled()
107	{
108		int selectedRow = colorsTable.getSelectedRow();
109		remove.setEnabled(selectedRow != -1);
110	} //}}}
111
112	//}}}
113
114	//{{{ SelectionHandler class
115	class SelectionHandler implements ListSelectionListener
116	{
117		public void valueChanged(ListSelectionEvent evt)
118		{
119			updateEnabled();
120		}
121	} //}}}
122
123	//{{{ ActionHandler class
124	class ActionHandler implements ActionListener
125	{
126		public void actionPerformed(ActionEvent evt)
127		{
128			Object source = evt.getSource();
129			if(source == add)
130			{
131				colorsModel.add();
132			}
133			else if(source == remove)
134			{
135				int selectedRow = colorsTable.getSelectedRow();
136				colorsModel.remove(selectedRow);
137				updateEnabled();
138			}
139		}
140	} //}}}
141
142	//{{{ MouseHandler class
143	class MouseHandler extends MouseAdapter
144	{
145		public void mouseClicked(MouseEvent evt)
146		{
147			Point p = evt.getPoint();
148			int row = colorsTable.rowAtPoint(p);
149			int column = colorsTable.columnAtPoint(p);
150			if(row == -1 || column != 1)
151				return;
152
153			Color color = JColorChooser.showDialog(
154				BrowserColorsOptionPane.this,
155				jEdit.getProperty("colorChooser.title"),
156				(Color)colorsModel.getValueAt(row,1));
157			if(color != null)
158				colorsModel.setValueAt(color,row,1);
159		}
160	} //}}}
161} //}}}
162
163//{{{ BrowserColorsModel class
164class BrowserColorsModel extends AbstractTableModel
165{
166	Vector entries;
167
168	//{{{ BrowserColorsModel constructor
169	BrowserColorsModel()
170	{
171		entries = new Vector();
172
173		int i = 0;
174		String glob;
175		while((glob = jEdit.getProperty("vfs.browser.colors." + i + ".glob")) != null)
176		{
177			entries.addElement(new Entry(glob,
178				jEdit.getColorProperty(
179				"vfs.browser.colors." + i + ".color",
180				Color.black)));
181			i++;
182		}
183	} //}}}
184
185	//{{{ add() method
186	void add()
187	{
188		entries.addElement(new Entry("",UIManager.getColor("Tree.foreground")));
189		fireTableRowsInserted(entries.size() - 1,entries.size() - 1);
190	} //}}}
191
192	//{{{ remove() method
193	void remove(int index)
194	{
195		entries.removeElementAt(index);
196		fireTableRowsDeleted(entries.size(),entries.size());
197	} //}}}
198
199	//{{{ save() method
200	void save()
201	{
202		int i;
203		for(i = 0; i < entries.size(); i++)
204		{
205			Entry entry = (Entry)entries.elementAt(i);
206			jEdit.setProperty("vfs.browser.colors." + i + ".glob",
207				entry.glob);
208			jEdit.setColorProperty("vfs.browser.colors." + i + ".color",
209				entry.color);
210		}
211		jEdit.unsetProperty("vfs.browser.colors." + i + ".glob");
212		jEdit.unsetProperty("vfs.browser.colors." + i + ".color");
213	} //}}}
214
215	//{{{ getColumnCount() method
216	public int getColumnCount()
217	{
218		return 2;
219	} //}}}
220
221	//{{{ getRowCount() method
222	public int getRowCount()
223	{
224		return entries.size();
225	} //}}}
226
227	//{{{ getValueAt() method
228	public Object getValueAt(int row, int col)
229	{
230		Entry entry = (Entry)entries.elementAt(row);
231
232		switch(col)
233		{
234		case 0:
235			return entry.glob;
236		case 1:
237			return entry.color;
238		default:
239			return null;
240		}
241	} //}}}
242
243	//{{{ isCellEditable() method
244	public boolean isCellEditable(int row, int col)
245	{
246		return (col == 0);
247	} //}}}
248
249	//{{{ setValueAt() method
250	public void setValueAt(Object value, int row, int col)
251	{
252		Entry entry = (Entry)entries.elementAt(row);
253
254		if(col == 0)
255			entry.glob = (String)value;
256		else
257			entry.color = (Color)value;
258
259		fireTableRowsUpdated(row,row);
260	} //}}}
261
262	//{{{ getColumnName() method
263	public String getColumnName(int index)
264	{
265		switch(index)
266		{
267		case 0:
268			return jEdit.getProperty("options.browser.colors.glob");
269		case 1:
270			return jEdit.getProperty("options.browser.colors.color");
271		default:
272			return null;
273		}
274	} //}}}
275
276	//{{{ getColumnClass() method
277	public Class getColumnClass(int col)
278	{
279		switch(col)
280		{
281		case 0:
282			return String.class;
283		case 1:
284			return Color.class;
285		default:
286			throw new InternalError();
287		}
288	} //}}}
289
290	//{{{ Entry class
291	static class Entry
292	{
293		String glob;
294		Color color;
295
296		Entry(String glob, Color color)
297		{
298			this.glob = glob;
299			this.color = color;
300		}
301	} //}}}
302
303	//{{{ ColorRenderer class
304	static class ColorRenderer extends JLabel
305		implements TableCellRenderer
306	{
307		//{{{ ColorRenderer constructor
308		public ColorRenderer()
309		{
310			setOpaque(true);
311			setBorder(SyntaxHiliteOptionPane.noFocusBorder);
312		} //}}}
313
314		//{{{ getTableCellRendererComponent() method
315		public Component getTableCellRendererComponent(
316			JTable table,
317			Object value,
318			boolean isSelected,
319			boolean cellHasFocus,
320			int row,
321			int col)
322		{
323			if (isSelected)
324			{
325				setBackground(table.getSelectionBackground());
326				setForeground(table.getSelectionForeground());
327			}
328			else
329			{
330				setBackground(table.getBackground());
331				setForeground(table.getForeground());
332			}
333
334			if (value != null)
335				setBackground((Color)value);
336
337			setBorder((cellHasFocus) ? UIManager.getBorder(
338				"Table.focusCellHighlightBorder")
339				: SyntaxHiliteOptionPane.noFocusBorder);
340			return this;
341		} //}}}
342	} //}}}
343} //}}}