PageRenderTime 145ms CodeModel.GetById 69ms app.highlight 52ms RepoModel.GetById 8ms app.codeStats 4ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/options/BrowserColorsOptionPane.java

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