PageRenderTime 234ms CodeModel.GetById 199ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 405 lines | 303 code | 43 blank | 59 comment | 32 complexity | ca5b24c52cf6d6a60c1c12b7bd1ee990 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 4892 2003-10-11 02:21:37Z 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("common.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("common.remove"));
 84		remove.addActionListener(actionHandler);
 85		buttons.add(remove);
 86		buttons.add(Box.createHorizontalStrut(6));
 87		moveUp = new RolloverButton(GUIUtilities.loadIcon("ArrowU.png"));
 88		moveUp.setToolTipText(jEdit.getProperty("common.moveUp"));
 89		moveUp.addActionListener(actionHandler);
 90		buttons.add(moveUp);
 91		buttons.add(Box.createHorizontalStrut(6));
 92		moveDown = new RolloverButton(GUIUtilities.loadIcon("ArrowD.png"));
 93		moveDown.setToolTipText(jEdit.getProperty("common.moveDown"));
 94		moveDown.addActionListener(actionHandler);
 95		buttons.add(moveDown);
 96		buttons.add(Box.createGlue());
 97
 98		add(BorderLayout.SOUTH,buttons);
 99
100		updateEnabled();
101	} //}}}
102
103	//{{{ _save() method
104	protected void _save()
105	{
106		colorsModel.save();
107	} //}}}
108
109	//}}}
110
111	//{{{ Private members
112	private BrowserColorsModel colorsModel;
113	private JTable colorsTable;
114	private JButton add;
115	private JButton remove;
116	private JButton moveUp;
117	private JButton moveDown;
118
119	//{{{ updateEnabled() method
120	private void updateEnabled()
121	{
122		int selectedRow = colorsTable.getSelectedRow();
123		remove.setEnabled(selectedRow != -1);
124		moveUp.setEnabled(selectedRow > 0);
125		moveUp.setEnabled(selectedRow != -1 && selectedRow !=
126			colorsModel.getRowCount());
127	} //}}}
128
129	//{{{ setSelectedRow() method
130	private void setSelectedRow(int row)
131	{
132		colorsTable.getSelectionModel().setSelectionInterval(row,row);
133		colorsTable.scrollRectToVisible(colorsTable.getCellRect(row,0,true));
134	} //}}}
135
136	//}}}
137
138	//{{{ SelectionHandler class
139	class SelectionHandler implements ListSelectionListener
140	{
141		public void valueChanged(ListSelectionEvent evt)
142		{
143			updateEnabled();
144		}
145	} //}}}
146
147	//{{{ ActionHandler class
148	class ActionHandler implements ActionListener
149	{
150		public void actionPerformed(ActionEvent evt)
151		{
152			Object source = evt.getSource();
153			if(source == add)
154			{
155				colorsModel.add();
156			}
157			else if(source == remove)
158			{
159				int selectedRow = colorsTable.getSelectedRow();
160				colorsModel.remove(selectedRow);
161				updateEnabled();
162			}
163			else if(source == moveUp)
164			{
165				int selectedRow = colorsTable.getSelectedRow();
166				if(selectedRow != 0)
167				{
168					colorsModel.moveUp(selectedRow);
169					setSelectedRow(selectedRow - 1);
170				}
171				updateEnabled();
172			}
173			else if(source == moveDown)
174			{
175				int selectedRow = colorsTable.getSelectedRow();
176				if(selectedRow != colorsTable.getRowCount() - 1)
177				{
178					colorsModel.moveDown(selectedRow);
179					setSelectedRow(selectedRow + 1);
180				}
181				updateEnabled();
182			}
183		}
184	} //}}}
185
186	//{{{ MouseHandler class
187	class MouseHandler extends MouseAdapter
188	{
189		public void mouseClicked(MouseEvent evt)
190		{
191			Point p = evt.getPoint();
192			int row = colorsTable.rowAtPoint(p);
193			int column = colorsTable.columnAtPoint(p);
194			if(row == -1 || column != 1)
195				return;
196
197			Color color = JColorChooser.showDialog(
198				BrowserColorsOptionPane.this,
199				jEdit.getProperty("colorChooser.title"),
200				(Color)colorsModel.getValueAt(row,1));
201			if(color != null)
202				colorsModel.setValueAt(color,row,1);
203		}
204	} //}}}
205} //}}}
206
207//{{{ BrowserColorsModel class
208class BrowserColorsModel extends AbstractTableModel
209{
210	//{{{ BrowserColorsModel constructor
211	BrowserColorsModel()
212	{
213		entries = new ArrayList();
214
215		int i = 0;
216		String glob;
217		while((glob = jEdit.getProperty("vfs.browser.colors." + i + ".glob")) != null)
218		{
219			entries.add(new Entry(glob,
220				jEdit.getColorProperty(
221				"vfs.browser.colors." + i + ".color",
222				Color.black)));
223			i++;
224		}
225	} //}}}
226
227	//{{{ add() method
228	void add()
229	{
230		entries.add(new Entry("",UIManager.getColor("Tree.foreground")));
231		fireTableRowsInserted(entries.size() - 1,entries.size() - 1);
232	} //}}}
233
234	//{{{ remove() method
235	void remove(int index)
236	{
237		entries.remove(index);
238		fireTableRowsDeleted(entries.size(),entries.size());
239	} //}}}
240
241	//{{{ moveUp() method
242	public void moveUp(int index)
243	{
244		Object obj = entries.get(index);
245		entries.remove(index);
246		entries.add(index - 1,obj);
247		fireTableRowsUpdated(index - 1,index);
248	} //}}}
249
250	//{{{ moveDown() method
251	public void moveDown(int index)
252	{
253		Object obj = entries.get(index);
254		entries.remove(index);
255		entries.add(index + 1,obj);
256		fireTableRowsUpdated(index,index + 1);
257	} //}}}
258
259	//{{{ save() method
260	void save()
261	{
262		int i;
263		for(i = 0; i < entries.size(); i++)
264		{
265			Entry entry = (Entry)entries.get(i);
266			jEdit.setProperty("vfs.browser.colors." + i + ".glob",
267				entry.glob);
268			jEdit.setColorProperty("vfs.browser.colors." + i + ".color",
269				entry.color);
270		}
271		jEdit.unsetProperty("vfs.browser.colors." + i + ".glob");
272		jEdit.unsetProperty("vfs.browser.colors." + i + ".color");
273	} //}}}
274
275	//{{{ getColumnCount() method
276	public int getColumnCount()
277	{
278		return 2;
279	} //}}}
280
281	//{{{ getRowCount() method
282	public int getRowCount()
283	{
284		return entries.size();
285	} //}}}
286
287	//{{{ getValueAt() method
288	public Object getValueAt(int row, int col)
289	{
290		Entry entry = (Entry)entries.get(row);
291
292		switch(col)
293		{
294		case 0:
295			return entry.glob;
296		case 1:
297			return entry.color;
298		default:
299			return null;
300		}
301	} //}}}
302
303	//{{{ isCellEditable() method
304	public boolean isCellEditable(int row, int col)
305	{
306		return (col == 0);
307	} //}}}
308
309	//{{{ setValueAt() method
310	public void setValueAt(Object value, int row, int col)
311	{
312		Entry entry = (Entry)entries.get(row);
313
314		if(col == 0)
315			entry.glob = (String)value;
316		else
317			entry.color = (Color)value;
318
319		fireTableRowsUpdated(row,row);
320	} //}}}
321
322	//{{{ getColumnName() method
323	public String getColumnName(int index)
324	{
325		switch(index)
326		{
327		case 0:
328			return jEdit.getProperty("options.browser.colors.glob");
329		case 1:
330			return jEdit.getProperty("options.browser.colors.color");
331		default:
332			return null;
333		}
334	} //}}}
335
336	//{{{ getColumnClass() method
337	public Class getColumnClass(int col)
338	{
339		switch(col)
340		{
341		case 0:
342			return String.class;
343		case 1:
344			return Color.class;
345		default:
346			throw new InternalError();
347		}
348	} //}}}
349
350	private ArrayList entries;
351
352	//{{{ Entry class
353	static class Entry
354	{
355		String glob;
356		Color color;
357
358		Entry(String glob, Color color)
359		{
360			this.glob = glob;
361			this.color = color;
362		}
363	} //}}}
364
365	//{{{ ColorRenderer class
366	static class ColorRenderer extends JLabel
367		implements TableCellRenderer
368	{
369		//{{{ ColorRenderer constructor
370		public ColorRenderer()
371		{
372			setOpaque(true);
373			setBorder(SyntaxHiliteOptionPane.noFocusBorder);
374		} //}}}
375
376		//{{{ getTableCellRendererComponent() method
377		public Component getTableCellRendererComponent(
378			JTable table,
379			Object value,
380			boolean isSelected,
381			boolean cellHasFocus,
382			int row,
383			int col)
384		{
385			if (isSelected)
386			{
387				setBackground(table.getSelectionBackground());
388				setForeground(table.getSelectionForeground());
389			}
390			else
391			{
392				setBackground(table.getBackground());
393				setForeground(table.getForeground());
394			}
395
396			if (value != null)
397				setBackground((Color)value);
398
399			setBorder((cellHasFocus) ? UIManager.getBorder(
400				"Table.focusCellHighlightBorder")
401				: SyntaxHiliteOptionPane.noFocusBorder);
402			return this;
403		} //}}}
404	} //}}}
405} //}}}