PageRenderTime 235ms CodeModel.GetById 211ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 301 lines | 235 code | 44 blank | 22 comment | 13 complexity | 6fdc122fd0103408bf831b59b411f34b MD5 | raw file
  1/*
  2 * DockingOptionPane.java - Dockable window options panel
  3 * Copyright (C) 2000, 2001 Slava Pestov
  4 *
  5 * This program is free software; you can redistribute it and/or
  6 * modify it under the terms of the GNU General Public License
  7 * as published by the Free Software Foundation; either version 2
  8 * of the License, or any later version.
  9 *
 10 * This program is distributed in the hope that it will be useful,
 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13 * GNU General Public License for more details.
 14 *
 15 * You should have received a copy of the GNU General Public License
 16 * along with this program; if not, write to the Free Software
 17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 18 */
 19
 20package org.gjt.sp.jedit.options;
 21
 22import javax.swing.table.*;
 23import javax.swing.*;
 24import java.awt.*;
 25import java.util.Vector;
 26import org.gjt.sp.jedit.gui.*;
 27import org.gjt.sp.jedit.*;
 28
 29public class DockingOptionPane extends AbstractOptionPane
 30{
 31	public DockingOptionPane()
 32	{
 33		super("docking");
 34	}
 35
 36	public void _init()
 37	{
 38		Box box = new Box(BoxLayout.X_AXIS);
 39		ButtonGroup grp = new ButtonGroup();
 40
 41		layout1 = new JToggleButton(GUIUtilities.loadIcon("dock_layout1.gif"));
 42		grp.add(layout1);
 43		box.add(layout1);
 44
 45		box.add(Box.createHorizontalStrut(6));
 46
 47		layout2 = new JToggleButton(GUIUtilities.loadIcon("dock_layout2.gif"));
 48		grp.add(layout2);
 49		box.add(layout2);
 50
 51		if(jEdit.getBooleanProperty("view.docking.alternateLayout"))
 52			layout2.setSelected(true);
 53		else
 54			layout1.setSelected(true);
 55
 56		addComponent(jEdit.getProperty("options.docking.layout"),box);
 57
 58		// reuse properties defined by the general option pane
 59		String[] positions = {
 60			jEdit.getProperty("options.docking.top"),
 61			jEdit.getProperty("options.docking.bottom"),
 62		};
 63
 64		tabsPos = new JComboBox(positions);
 65		tabsPos.setSelectedIndex(jEdit.getIntegerProperty(
 66			"view.docking.tabsPos",0));
 67		addComponent(jEdit.getProperty("options.docking.tabsPos"),tabsPos);
 68
 69		addComponent(Box.createVerticalStrut(6));
 70
 71		GridBagConstraints cons = new GridBagConstraints();
 72		cons.gridy = 3;
 73		cons.gridwidth = cons.gridheight = GridBagConstraints.REMAINDER;
 74		cons.fill = GridBagConstraints.BOTH;
 75		cons.weightx = cons.weighty = 1.0f;
 76
 77		JScrollPane windowScroller = createWindowTableScroller();
 78		gridBag.setConstraints(windowScroller,cons);
 79		add(windowScroller);
 80	}
 81
 82	public void _save()
 83	{
 84		jEdit.setBooleanProperty("view.docking.alternateLayout",
 85			layout2.isSelected());
 86		jEdit.setIntegerProperty("view.docking.tabsPos",
 87			tabsPos.getSelectedIndex());
 88		windowModel.save();
 89	}
 90
 91	// private members
 92	private JToggleButton layout1;
 93	private JToggleButton layout2;
 94	private JComboBox tabsPos;
 95	private JTable windowTable;
 96	private WindowTableModel windowModel;
 97
 98	private JScrollPane createWindowTableScroller()
 99	{
100		windowModel = createWindowModel();
101		windowTable = new JTable(windowModel);
102		windowTable.getTableHeader().setReorderingAllowed(false);
103		windowTable.setColumnSelectionAllowed(false);
104		windowTable.setRowSelectionAllowed(false);
105		windowTable.setCellSelectionEnabled(false);
106
107		DockPositionCellRenderer comboBox = new DockPositionCellRenderer();
108		comboBox.setRequestFocusEnabled(false);
109		windowTable.setRowHeight(comboBox.getPreferredSize().height);
110		TableColumn column = windowTable.getColumnModel().getColumn(1);
111		column.setCellRenderer(comboBox);
112		comboBox = new DockPositionCellRenderer();
113		comboBox.setRequestFocusEnabled(false);
114		column.setCellEditor(new DefaultCellEditor(comboBox));
115
116		Dimension d = windowTable.getPreferredSize();
117		d.height = Math.min(d.height,200);
118		JScrollPane scroller = new JScrollPane(windowTable);
119		scroller.setPreferredSize(d);
120		return scroller;
121	}
122
123	private WindowTableModel createWindowModel()
124	{
125		return new WindowTableModel();
126	}
127
128	class DockPositionCellRenderer extends JComboBox
129		implements TableCellRenderer
130	{
131		DockPositionCellRenderer()
132		{
133			super(new String[] {
134				DockableWindowManager.FLOATING,
135				DockableWindowManager.TOP,
136				DockableWindowManager.LEFT,
137				DockableWindowManager.BOTTOM,
138				DockableWindowManager.RIGHT
139			});
140		}
141
142		public Component getTableCellRendererComponent(JTable table,
143			Object value, boolean isSelected, boolean hasFocus,
144			int row, int column)
145		{
146			setSelectedItem(value);
147			return this;
148		}
149	}
150}
151
152class WindowTableModel extends AbstractTableModel
153{
154	private Vector windows;
155
156	WindowTableModel()
157	{
158		windows = new Vector();
159
160		// old dockable window API compatibility code
161		Object[] list = EditBus.getNamedList(DockableWindow.DOCKABLE_WINDOW_LIST);
162		if(list != null)
163		{
164			for(int i = 0; i < list.length; i++)
165			{
166				windows.addElement(new Entry((String)list[i]));
167			}
168		}
169		// end compatibility code
170
171		String[] dockables = DockableWindowManager.getRegisteredDockableWindows();
172		for(int i = 0; i < dockables.length; i++)
173		{
174			windows.addElement(new Entry(dockables[i]));
175		}
176
177		sort();
178	}
179
180	public void sort()
181	{
182		MiscUtilities.quicksort(windows,new WindowCompare());
183		fireTableDataChanged();
184	}
185
186	public int getColumnCount()
187	{
188		return 2;
189	}
190
191	public int getRowCount()
192	{
193		return windows.size();
194	}
195
196	public Class getColumnClass(int col)
197	{
198		switch(col)
199		{
200		case 0:
201		case 1:
202			return String.class;
203		default:
204			throw new InternalError();
205		}
206	}
207
208	public Object getValueAt(int row, int col)
209	{
210		Entry window = (Entry)windows.elementAt(row);
211		switch(col)
212		{
213		case 0:
214			return window.title;
215		case 1:
216			return window.dockPosition;
217		default:
218			throw new InternalError();
219		}
220	}
221
222	public boolean isCellEditable(int row, int col)
223	{
224		return (col != 0);
225	}
226
227	public void setValueAt(Object value, int row, int col)
228	{
229		if(col == 0)
230			return;
231
232		Entry window = (Entry)windows.elementAt(row);
233		switch(col)
234		{
235		case 1:
236			window.dockPosition = (String)value;
237			break;
238		default:
239			throw new InternalError();
240		}
241
242		fireTableRowsUpdated(row,row);
243	}
244
245	public String getColumnName(int index)
246	{
247		switch(index)
248		{
249		case 0:
250			return jEdit.getProperty("options.docking.title");
251		case 1:
252			return jEdit.getProperty("options.docking.dockPosition");
253		default:
254			throw new InternalError();
255		}
256	}
257
258	public void save()
259	{
260		for(int i = 0; i < windows.size(); i++)
261		{
262			((Entry)windows.elementAt(i)).save();
263		}
264	}
265
266	class Entry
267	{
268		String name;
269		String title;
270		String dockPosition;
271
272		Entry(String name)
273		{
274			this.name = name;
275			title = jEdit.getProperty(name + ".title");
276			if(title == null)
277				title = name;
278
279			dockPosition = jEdit.getProperty(name + ".dock-position");
280			if(dockPosition == null)
281				dockPosition = DockableWindowManager.FLOATING;
282		}
283
284		void save()
285		{
286			jEdit.setProperty(name + ".dock-position",dockPosition);
287		}
288	}
289
290	class WindowCompare implements MiscUtilities.Compare
291	{
292		public int compare(Object obj1, Object obj2)
293		{
294			Entry e1 = (Entry)obj1;
295			Entry e2 = (Entry)obj2;
296
297			return MiscUtilities.compareStrings(
298				e1.title,e2.title,true);
299		}
300	}
301}