PageRenderTime 115ms CodeModel.GetById 99ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 388 lines | 304 code | 37 blank | 47 comment | 14 complexity | 788694f70d7d6ccc2b5664b51593a3d0 MD5 | raw file
  1/*
  2 * DockingOptionPane.java - Dockable window options panel
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2000, 2003 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 java.awt.BorderLayout;
 27import java.awt.Component;
 28import java.awt.Dimension;
 29import java.awt.GridLayout;
 30import java.awt.event.ActionEvent;
 31import java.awt.event.ActionListener;
 32import java.awt.event.ItemEvent;
 33import java.awt.event.ItemListener;
 34import java.util.Collections;
 35import java.util.Comparator;
 36import java.util.HashMap;
 37import java.util.Vector;
 38
 39import javax.swing.Box;
 40import javax.swing.DefaultCellEditor;
 41import javax.swing.DefaultComboBoxModel;
 42import javax.swing.JCheckBox;
 43import javax.swing.JComboBox;
 44import javax.swing.JLabel;
 45import javax.swing.JPanel;
 46import javax.swing.JScrollPane;
 47import javax.swing.JTable;
 48import javax.swing.table.AbstractTableModel;
 49import javax.swing.table.TableCellRenderer;
 50import javax.swing.table.TableColumn;
 51
 52import org.gjt.sp.jedit.AbstractOptionPane;
 53import org.gjt.sp.jedit.jEdit;
 54import org.gjt.sp.jedit.gui.DockableWindowManager;
 55import org.gjt.sp.util.StandardUtilities;
 56//}}}
 57
 58//{{{ DockingOptionPane class
 59@SuppressWarnings("serial")
 60public class DockingOptionPane extends AbstractOptionPane
 61{
 62	//{{{ DockingOptionPane constructor
 63	public DockingOptionPane()
 64	{
 65		super("docking");
 66	} //}}}
 67
 68	//{{{ _init() method
 69	public void _init()
 70	{
 71		setLayout(new BorderLayout());
 72		add(BorderLayout.NORTH,createDockingOptionsPanel());
 73		add(BorderLayout.CENTER,createWindowTableScroller());
 74		dockableSetSelection.setModel(
 75			new DefaultComboBoxModel(windowModel.getDockableSets()));
 76	} //}}}
 77
 78	//{{{ _save() method
 79	public void _save()
 80	{
 81		jEdit.setBooleanProperty(AUTO_LOAD_MODE_LAYOUT_PROP, autoLoadModeLayout.isSelected());
 82		jEdit.setBooleanProperty(AUTO_SAVE_MODE_LAYOUT_PROP, autoSaveModeLayout.isSelected());
 83		windowModel.save();
 84	} //}}}
 85
 86	//{{{ Private members
 87
 88	//{{{ Instance variables
 89	private JTable windowTable;
 90	private WindowTableModel windowModel;
 91	private JCheckBox autoLoadModeLayout;
 92	private JCheckBox autoSaveModeLayout;
 93	private JComboBox dockableSetSelection;
 94	//}}}
 95
 96	private static final String DOCKING_OPTIONS_PREFIX = "options.docking.";
 97	public static final String AUTO_LOAD_MODE_LAYOUT_PROP = DOCKING_OPTIONS_PREFIX + "autoLoadModeLayout";
 98	private static final String AUTO_LOAD_MODE_LAYOUT_LABEL = AUTO_LOAD_MODE_LAYOUT_PROP + ".label";
 99	public static final String AUTO_SAVE_MODE_LAYOUT_PROP = DOCKING_OPTIONS_PREFIX + "autoSaveModeLayout";
100	private static final String AUTO_SAVE_MODE_LAYOUT_LABEL = AUTO_SAVE_MODE_LAYOUT_PROP + ".label";
101	
102	private JPanel createDockingOptionsPanel()
103	{
104		JPanel p = new JPanel();
105		p.setLayout(new GridLayout(0, 1));
106		boolean autoLoadModeLayoutProp = jEdit.getBooleanProperty(
107			AUTO_LOAD_MODE_LAYOUT_PROP, false);
108		autoLoadModeLayout = new JCheckBox(
109			jEdit.getProperty(AUTO_LOAD_MODE_LAYOUT_LABEL),
110			autoLoadModeLayoutProp);
111		p.add(autoLoadModeLayout);
112		autoSaveModeLayout = new JCheckBox(
113			jEdit.getProperty(AUTO_SAVE_MODE_LAYOUT_LABEL),
114			jEdit.getBooleanProperty(AUTO_SAVE_MODE_LAYOUT_PROP, false));
115		p.add(autoSaveModeLayout);
116		autoSaveModeLayout.setEnabled(autoLoadModeLayoutProp);
117		autoLoadModeLayout.addActionListener(new ActionListener()
118		{
119			public void actionPerformed(ActionEvent e)
120			{
121				autoSaveModeLayout.setEnabled(autoLoadModeLayout.isSelected());
122			}
123		});
124		Box vSetSelection = Box.createVerticalBox();
125		p.add(vSetSelection);
126		Box setSelection = Box.createHorizontalBox();
127		vSetSelection.add(setSelection);
128		setSelection.add(Box.createHorizontalStrut(6));
129		setSelection.add(new JLabel(jEdit.getProperty(
130			"options.docking.selectSet.label")));
131		setSelection.add(Box.createHorizontalStrut(6));
132		dockableSetSelection = new JComboBox();
133		setSelection.add(dockableSetSelection);
134		dockableSetSelection.addItemListener(new ItemListener()
135		{
136			public void itemStateChanged(ItemEvent e)
137			{
138				windowModel.showSet((String) dockableSetSelection.getSelectedItem());
139			}
140		});
141		setSelection.add(Box.createHorizontalStrut(6));
142		vSetSelection.add(Box.createVerticalStrut(6));
143		return p;
144	}
145	//{{{ createWindowTableScroller() method
146	private JScrollPane createWindowTableScroller()
147	{
148		windowModel = createWindowModel();
149		windowTable = new JTable(windowModel);
150		windowTable.getTableHeader().setReorderingAllowed(false);
151		windowTable.setColumnSelectionAllowed(false);
152		windowTable.setRowSelectionAllowed(false);
153		windowTable.setCellSelectionEnabled(false);
154
155		DockPositionCellRenderer comboBox = new DockPositionCellRenderer();
156		windowTable.setRowHeight(comboBox.getPreferredSize().height);
157		TableColumn column = windowTable.getColumnModel().getColumn(1);
158		column.setCellRenderer(comboBox);
159		column.setCellEditor(new DefaultCellEditor(new DockPositionCellRenderer()));
160
161		Dimension d = windowTable.getPreferredSize();
162		d.height = Math.min(d.height,50);
163		JScrollPane scroller = new JScrollPane(windowTable);
164		scroller.setPreferredSize(d);
165		return scroller;
166	} //}}}
167
168	//{{{ createWindowModel() method
169	private static WindowTableModel createWindowModel()
170	{
171		return new WindowTableModel();
172	} //}}}
173
174	//}}}
175
176	//{{{ DockPositionCellRenderer class
177	static class DockPositionCellRenderer extends JComboBox
178		implements TableCellRenderer
179	{
180		DockPositionCellRenderer()
181		{
182			super(new String[] {
183				DockableWindowManager.FLOATING,
184				DockableWindowManager.TOP,
185				DockableWindowManager.LEFT,
186				DockableWindowManager.BOTTOM,
187				DockableWindowManager.RIGHT
188			});
189			DockPositionCellRenderer.this.setRequestFocusEnabled(false);
190		}
191
192		public Component getTableCellRendererComponent(JTable table,
193			Object value, boolean isSelected, boolean hasFocus,
194			int row, int column)
195		{
196			setSelectedItem(value);
197			return this;
198		}
199	} //}}}
200} //}}}
201
202//{{{ WindowTableModel class
203@SuppressWarnings("serial")
204class WindowTableModel extends AbstractTableModel
205{
206	private static final String PLUGIN_SET_PREFIX = "Plugin: ";
207	private static final String CORE_DOCKABLE_SET = "Core";
208	private static final String ALL_DOCKABLE_SET = "All";
209	private HashMap<String, Vector<Entry>> dockableSets;
210	private Vector<Entry> windows;
211
212	//{{{ WindowTableModel constructor
213	WindowTableModel()
214	{
215		dockableSets = new HashMap<String, Vector<Entry>>();
216		Vector<Entry> all = new Vector<Entry>();
217		dockableSets.put(ALL_DOCKABLE_SET, all);
218		windows = new Vector<Entry>();
219		String[] dockables = DockableWindowManager.getRegisteredDockableWindows();
220		for (String dockable: dockables)
221		{
222			String plugin = DockableWindowManager.
223				getDockableWindowPluginName(dockable);
224			String set;
225			if (plugin != null)
226				set = PLUGIN_SET_PREFIX + plugin;
227			else
228				set = CORE_DOCKABLE_SET; 
229			Vector<Entry> currentSetDockables = dockableSets.get(set);
230			if (currentSetDockables == null)
231			{
232				currentSetDockables = new Vector<Entry>();
233				dockableSets.put(set, currentSetDockables);
234			}
235			Entry entry = new Entry(dockable);
236			currentSetDockables.add(entry);
237			all.add(entry);
238		}
239		showSet(ALL_DOCKABLE_SET);
240	} //}}}
241
242	public Vector<String> getDockableSets()
243	{
244		Vector<String> sets = new Vector<String>();
245		for (String set: dockableSets.keySet())
246			sets.add(set);
247		sets.remove(ALL_DOCKABLE_SET);
248		sets.remove(CORE_DOCKABLE_SET);
249		Collections.sort(sets);
250		sets.insertElementAt(CORE_DOCKABLE_SET, 0);
251		sets.insertElementAt(ALL_DOCKABLE_SET, 0);
252		return sets;
253	}
254
255	//{{{ showSet() method
256	public void showSet(String set)
257	{
258		windows = dockableSets.get(set);
259		Collections.sort(windows,new WindowCompare());
260		fireTableDataChanged();
261	} //}}}
262
263	//{{{ getColumnCount() method
264	public int getColumnCount()
265	{
266		return 2;
267	} //}}}
268
269	//{{{ getRowCount() method
270	public int getRowCount()
271	{
272		return windows.size();
273	} //}}}
274
275	//{{{ getColumnClass() method
276	public Class getColumnClass(int col)
277	{
278		switch(col)
279		{
280		case 0:
281		case 1:
282			return String.class;
283		default:
284			throw new InternalError();
285		}
286	} //}}}
287
288	//{{{ getValueAt() method
289	public Object getValueAt(int row, int col)
290	{
291		Entry window = (Entry)windows.elementAt(row);
292		switch(col)
293		{
294		case 0:
295			return window.title;
296		case 1:
297			return window.dockPosition;
298		default:
299			throw new InternalError();
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		if(col == 0)
313			return;
314
315		Entry window = (Entry)windows.elementAt(row);
316		switch(col)
317		{
318		case 1:
319			window.dockPosition = (String)value;
320			break;
321		default:
322			throw new InternalError();
323		}
324
325		fireTableRowsUpdated(row,row);
326	} //}}}
327
328	//{{{ getColumnName() method
329	public String getColumnName(int index)
330	{
331		switch(index)
332		{
333		case 0:
334			return jEdit.getProperty("options.docking.title");
335		case 1:
336			return jEdit.getProperty("options.docking.dockPosition");
337		default:
338			throw new InternalError();
339		}
340	} //}}}
341
342	//{{{ save() method
343	public void save()
344	{
345		for(int i = 0; i < windows.size(); i++)
346		{
347			((Entry)windows.elementAt(i)).save();
348		}
349	} //}}}
350
351	//{{{ Entry class
352	static class Entry
353	{
354		String name;
355		String title;
356		String dockPosition;
357
358		Entry(String name)
359		{
360			this.name = name;
361			title = jEdit.getProperty(name + ".title");
362			if(title == null)
363				title = name;
364
365			dockPosition = jEdit.getProperty(name + ".dock-position");
366			if(dockPosition == null)
367				dockPosition = DockableWindowManager.FLOATING;
368		}
369
370		void save()
371		{
372			jEdit.setProperty(name + ".dock-position",dockPosition);
373		}
374	} //}}}
375
376	//{{{ WindowCompare class
377	static class WindowCompare implements Comparator<Object>
378	{
379		public int compare(Object obj1, Object obj2)
380		{
381			Entry e1 = (Entry)obj1;
382			Entry e2 = (Entry)obj2;
383
384			return StandardUtilities.compareStrings(
385				e1.title,e2.title,true);
386		}
387	} //}}}
388} //}}}