PageRenderTime 222ms CodeModel.GetById 188ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 362 lines | 268 code | 45 blank | 49 comment | 43 complexity | 5406bdaa5a33370624682942abd59cac 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, 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.table.*;
 27import javax.swing.*;
 28import java.awt.event.*;
 29import java.awt.*;
 30import java.util.Vector;
 31import org.gjt.sp.jedit.gui.*;
 32import org.gjt.sp.jedit.*;
 33//}}}
 34
 35//{{{ DockingOptionPane class
 36public class DockingOptionPane extends AbstractOptionPane
 37{
 38	//{{{ DockingOptionPane constructor
 39	public DockingOptionPane()
 40	{
 41		super("docking");
 42	} //}}}
 43
 44	//{{{ _init() method
 45	public void _init()
 46	{
 47		addSeparator("options.docking.viewLayout");
 48
 49		layoutIcon1 = GUIUtilities.loadIcon("dock_layout1.png");
 50		layoutIcon2 = GUIUtilities.loadIcon("dock_layout2.png");
 51		layoutIcon3 = GUIUtilities.loadIcon("dock_layout3.png");
 52		layoutIcon4 = GUIUtilities.loadIcon("dock_layout4.png");
 53
 54		JPanel layoutPanel = new JPanel(new VariableGridLayout(
 55			VariableGridLayout.FIXED_NUM_COLUMNS,1,
 56			6,6));
 57
 58		if(jEdit.getBooleanProperty("view.docking.alternateLayout"))
 59		{
 60			layout = new JLabel(jEdit.getBooleanProperty(
 61				"view.toolbar.alternateLayout")
 62				? layoutIcon4 : layoutIcon2);
 63		}
 64		else
 65		{
 66			layout = new JLabel(jEdit.getBooleanProperty(
 67				"view.toolbar.alternateLayout")
 68				? layoutIcon3 : layoutIcon1);
 69		}
 70
 71		layoutPanel.add(layout);
 72
 73		layoutPanel.add(alternateDockingLayout = new JButton(jEdit.getProperty(
 74			"options.docking.alternateDockingLayout")));
 75		alternateDockingLayout.addActionListener(new ActionHandler());
 76		layoutPanel.add(alternateToolBarLayout = new JButton(jEdit.getProperty(
 77			"options.docking.alternateToolBarLayout")));
 78		alternateToolBarLayout.addActionListener(new ActionHandler());
 79
 80		// center the layout panel
 81		GridBagConstraints cons = new GridBagConstraints();
 82		cons.gridy = y++;
 83		cons.gridwidth = GridBagConstraints.REMAINDER;
 84		cons.fill = GridBagConstraints.BOTH;
 85		cons.weightx = 1.0f;
 86		gridBag.setConstraints(layoutPanel,cons);
 87		add(layoutPanel);
 88
 89		addSeparator("options.docking.windowDocking");
 90
 91		cons = new GridBagConstraints();
 92		cons.gridy = y++;
 93		cons.gridwidth = cons.gridheight = GridBagConstraints.REMAINDER;
 94		cons.fill = GridBagConstraints.BOTH;
 95		cons.weightx = cons.weighty = 1.0f;
 96
 97		JScrollPane windowScroller = createWindowTableScroller();
 98		gridBag.setConstraints(windowScroller,cons);
 99		add(windowScroller);
100	} //}}}
101
102	//{{{ _save() method
103	public void _save()
104	{
105		jEdit.setBooleanProperty("view.docking.alternateLayout",
106			layout.getIcon() == layoutIcon2
107			|| layout.getIcon() == layoutIcon4);
108		jEdit.setBooleanProperty("view.toolbar.alternateLayout",
109			layout.getIcon() == layoutIcon3
110			|| layout.getIcon() == layoutIcon4);
111		windowModel.save();
112	} //}}}
113
114	//{{{ Private members
115
116	//{{{ Instance variables
117	private JLabel layout;
118	private Icon layoutIcon1, layoutIcon2, layoutIcon3, layoutIcon4;
119	private JButton alternateDockingLayout, alternateToolBarLayout;
120	private JTable windowTable;
121	private WindowTableModel windowModel;
122	//}}}
123
124	//{{{ createWindowTableScroller() method
125	private JScrollPane createWindowTableScroller()
126	{
127		windowModel = createWindowModel();
128		windowTable = new JTable(windowModel);
129		windowTable.getTableHeader().setReorderingAllowed(false);
130		windowTable.setColumnSelectionAllowed(false);
131		windowTable.setRowSelectionAllowed(false);
132		windowTable.setCellSelectionEnabled(false);
133
134		DockPositionCellRenderer comboBox = new DockPositionCellRenderer();
135		windowTable.setRowHeight(comboBox.getPreferredSize().height);
136		TableColumn column = windowTable.getColumnModel().getColumn(1);
137		column.setCellRenderer(comboBox);
138		column.setCellEditor(new DefaultCellEditor(new DockPositionCellRenderer()));
139
140		Dimension d = windowTable.getPreferredSize();
141		d.height = Math.min(d.height,50);
142		JScrollPane scroller = new JScrollPane(windowTable);
143		scroller.setPreferredSize(d);
144		return scroller;
145	} //}}}
146
147	//{{{ createWindowModel() method
148	private WindowTableModel createWindowModel()
149	{
150		return new WindowTableModel();
151	} //}}}
152
153	//}}}
154
155	//{{{ ActionHandler class
156	class ActionHandler implements ActionListener
157	{
158		public void actionPerformed(ActionEvent evt)
159		{
160			if(evt.getSource() == alternateDockingLayout)
161			{
162				if(layout.getIcon() == layoutIcon1)
163					layout.setIcon(layoutIcon2);
164				else if(layout.getIcon() == layoutIcon2)
165					layout.setIcon(layoutIcon1);
166				else if(layout.getIcon() == layoutIcon3)
167					layout.setIcon(layoutIcon4);
168				else if(layout.getIcon() == layoutIcon4)
169					layout.setIcon(layoutIcon3);
170			}
171			else if(evt.getSource() == alternateToolBarLayout)
172			{
173				if(layout.getIcon() == layoutIcon1)
174					layout.setIcon(layoutIcon3);
175				else if(layout.getIcon() == layoutIcon3)
176					layout.setIcon(layoutIcon1);
177				else if(layout.getIcon() == layoutIcon2)
178					layout.setIcon(layoutIcon4);
179				else if(layout.getIcon() == layoutIcon4)
180					layout.setIcon(layoutIcon2);
181			}
182		}
183	} //}}}
184
185	//{{{ DockPositionCellRenderer class
186	class DockPositionCellRenderer extends JComboBox
187		implements TableCellRenderer
188	{
189		DockPositionCellRenderer()
190		{
191			super(new String[] {
192				DockableWindowManager.FLOATING,
193				DockableWindowManager.TOP,
194				DockableWindowManager.LEFT,
195				DockableWindowManager.BOTTOM,
196				DockableWindowManager.RIGHT
197			});
198			DockPositionCellRenderer.this.setRequestFocusEnabled(false);
199		}
200
201		public Component getTableCellRendererComponent(JTable table,
202			Object value, boolean isSelected, boolean hasFocus,
203			int row, int column)
204		{
205			setSelectedItem(value);
206			return this;
207		}
208	} //}}}
209} //}}}
210
211//{{{ WindowTableModel class
212class WindowTableModel extends AbstractTableModel
213{
214	private Vector windows;
215
216	//{{{ WindowTableModel constructor
217	WindowTableModel()
218	{
219		windows = new Vector();
220
221		String[] dockables = DockableWindowManager.getRegisteredDockableWindows();
222		for(int i = 0; i < dockables.length; i++)
223		{
224			windows.addElement(new Entry(dockables[i]));
225		}
226
227		sort();
228	} //}}}
229
230	//{{{ sort() method
231	public void sort()
232	{
233		MiscUtilities.quicksort(windows,new WindowCompare());
234		fireTableDataChanged();
235	} //}}}
236
237	//{{{ getColumnCount() method
238	public int getColumnCount()
239	{
240		return 2;
241	} //}}}
242
243	//{{{ getRowCount() method
244	public int getRowCount()
245	{
246		return windows.size();
247	} //}}}
248
249	//{{{ getColumnClass() method
250	public Class getColumnClass(int col)
251	{
252		switch(col)
253		{
254		case 0:
255		case 1:
256			return String.class;
257		default:
258			throw new InternalError();
259		}
260	} //}}}
261
262	//{{{ getValueAt() method
263	public Object getValueAt(int row, int col)
264	{
265		Entry window = (Entry)windows.elementAt(row);
266		switch(col)
267		{
268		case 0:
269			return window.title;
270		case 1:
271			return window.dockPosition;
272		default:
273			throw new InternalError();
274		}
275	} //}}}
276
277	//{{{ isCellEditable() method
278	public boolean isCellEditable(int row, int col)
279	{
280		return (col != 0);
281	} //}}}
282
283	//{{{ setValueAt() method
284	public void setValueAt(Object value, int row, int col)
285	{
286		if(col == 0)
287			return;
288
289		Entry window = (Entry)windows.elementAt(row);
290		switch(col)
291		{
292		case 1:
293			window.dockPosition = (String)value;
294			break;
295		default:
296			throw new InternalError();
297		}
298
299		fireTableRowsUpdated(row,row);
300	} //}}}
301
302	//{{{ getColumnName() method
303	public String getColumnName(int index)
304	{
305		switch(index)
306		{
307		case 0:
308			return jEdit.getProperty("options.docking.title");
309		case 1:
310			return jEdit.getProperty("options.docking.dockPosition");
311		default:
312			throw new InternalError();
313		}
314	} //}}}
315
316	//{{{ save() method
317	public void save()
318	{
319		for(int i = 0; i < windows.size(); i++)
320		{
321			((Entry)windows.elementAt(i)).save();
322		}
323	} //}}}
324
325	//{{{ Entry class
326	class Entry
327	{
328		String name;
329		String title;
330		String dockPosition;
331
332		Entry(String name)
333		{
334			this.name = name;
335			title = jEdit.getProperty(name + ".title");
336			if(title == null)
337				title = name;
338
339			dockPosition = jEdit.getProperty(name + ".dock-position");
340			if(dockPosition == null)
341				dockPosition = DockableWindowManager.FLOATING;
342		}
343
344		void save()
345		{
346			jEdit.setProperty(name + ".dock-position",dockPosition);
347		}
348	} //}}}
349
350	//{{{ WindowCompare class
351	class WindowCompare implements MiscUtilities.Compare
352	{
353		public int compare(Object obj1, Object obj2)
354		{
355			Entry e1 = (Entry)obj1;
356			Entry e2 = (Entry)obj2;
357
358			return MiscUtilities.compareStrings(
359				e1.title,e2.title,true);
360		}
361	} //}}}
362} //}}}