001: /*
002: * DockingOptionPane.java - Dockable window options panel
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2000, 2003 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.options;
024:
025: //{{{ Imports
026: import javax.swing.table.*;
027: import javax.swing.*;
028: import java.awt.*;
029: import java.util.Vector;
030: import java.util.Collections;
031: import java.util.Comparator;
032:
033: import org.gjt.sp.jedit.gui.*;
034: import org.gjt.sp.jedit.*;
035: import org.gjt.sp.util.StandardUtilities;
036:
037: //}}}
038:
039: //{{{ DockingOptionPane class
040: public class DockingOptionPane extends AbstractOptionPane {
041: //{{{ DockingOptionPane constructor
042: public DockingOptionPane() {
043: super ("docking");
044: } //}}}
045:
046: //{{{ _init() method
047: public void _init() {
048: setLayout(new BorderLayout());
049: add(BorderLayout.CENTER, createWindowTableScroller());
050: } //}}}
051:
052: //{{{ _save() method
053: public void _save() {
054: windowModel.save();
055: } //}}}
056:
057: //{{{ Private members
058:
059: //{{{ Instance variables
060: private JTable windowTable;
061: private WindowTableModel windowModel;
062:
063: //}}}
064:
065: //{{{ createWindowTableScroller() method
066: private JScrollPane createWindowTableScroller() {
067: windowModel = createWindowModel();
068: windowTable = new JTable(windowModel);
069: windowTable.getTableHeader().setReorderingAllowed(false);
070: windowTable.setColumnSelectionAllowed(false);
071: windowTable.setRowSelectionAllowed(false);
072: windowTable.setCellSelectionEnabled(false);
073:
074: DockPositionCellRenderer comboBox = new DockPositionCellRenderer();
075: windowTable.setRowHeight(comboBox.getPreferredSize().height);
076: TableColumn column = windowTable.getColumnModel().getColumn(1);
077: column.setCellRenderer(comboBox);
078: column.setCellEditor(new DefaultCellEditor(
079: new DockPositionCellRenderer()));
080:
081: Dimension d = windowTable.getPreferredSize();
082: d.height = Math.min(d.height, 50);
083: JScrollPane scroller = new JScrollPane(windowTable);
084: scroller.setPreferredSize(d);
085: return scroller;
086: } //}}}
087:
088: //{{{ createWindowModel() method
089: private static WindowTableModel createWindowModel() {
090: return new WindowTableModel();
091: } //}}}
092:
093: //}}}
094:
095: //{{{ DockPositionCellRenderer class
096: static class DockPositionCellRenderer extends JComboBox implements
097: TableCellRenderer {
098: DockPositionCellRenderer() {
099: super (new String[] { DockableWindowManager.FLOATING,
100: DockableWindowManager.TOP,
101: DockableWindowManager.LEFT,
102: DockableWindowManager.BOTTOM,
103: DockableWindowManager.RIGHT });
104: DockPositionCellRenderer.this .setRequestFocusEnabled(false);
105: }
106:
107: public Component getTableCellRendererComponent(JTable table,
108: Object value, boolean isSelected, boolean hasFocus,
109: int row, int column) {
110: setSelectedItem(value);
111: return this ;
112: }
113: } //}}}
114: } //}}}
115:
116: //{{{ WindowTableModel class
117: class WindowTableModel extends AbstractTableModel {
118: private Vector windows;
119:
120: //{{{ WindowTableModel constructor
121: WindowTableModel() {
122: windows = new Vector();
123:
124: String[] dockables = DockableWindowManager
125: .getRegisteredDockableWindows();
126: for (int i = 0; i < dockables.length; i++) {
127: windows.addElement(new Entry(dockables[i]));
128: }
129:
130: sort();
131: } //}}}
132:
133: //{{{ sort() method
134: public void sort() {
135: Collections.sort(windows, new WindowCompare());
136: fireTableDataChanged();
137: } //}}}
138:
139: //{{{ getColumnCount() method
140: public int getColumnCount() {
141: return 2;
142: } //}}}
143:
144: //{{{ getRowCount() method
145: public int getRowCount() {
146: return windows.size();
147: } //}}}
148:
149: //{{{ getColumnClass() method
150: public Class getColumnClass(int col) {
151: switch (col) {
152: case 0:
153: case 1:
154: return String.class;
155: default:
156: throw new InternalError();
157: }
158: } //}}}
159:
160: //{{{ getValueAt() method
161: public Object getValueAt(int row, int col) {
162: Entry window = (Entry) windows.elementAt(row);
163: switch (col) {
164: case 0:
165: return window.title;
166: case 1:
167: return window.dockPosition;
168: default:
169: throw new InternalError();
170: }
171: } //}}}
172:
173: //{{{ isCellEditable() method
174: public boolean isCellEditable(int row, int col) {
175: return col != 0;
176: } //}}}
177:
178: //{{{ setValueAt() method
179: public void setValueAt(Object value, int row, int col) {
180: if (col == 0)
181: return;
182:
183: Entry window = (Entry) windows.elementAt(row);
184: switch (col) {
185: case 1:
186: window.dockPosition = (String) value;
187: break;
188: default:
189: throw new InternalError();
190: }
191:
192: fireTableRowsUpdated(row, row);
193: } //}}}
194:
195: //{{{ getColumnName() method
196: public String getColumnName(int index) {
197: switch (index) {
198: case 0:
199: return jEdit.getProperty("options.docking.title");
200: case 1:
201: return jEdit.getProperty("options.docking.dockPosition");
202: default:
203: throw new InternalError();
204: }
205: } //}}}
206:
207: //{{{ save() method
208: public void save() {
209: for (int i = 0; i < windows.size(); i++) {
210: ((Entry) windows.elementAt(i)).save();
211: }
212: } //}}}
213:
214: //{{{ Entry class
215: static class Entry {
216: String name;
217: String title;
218: String dockPosition;
219:
220: Entry(String name) {
221: this .name = name;
222: title = jEdit.getProperty(name + ".title");
223: if (title == null)
224: title = name;
225:
226: dockPosition = jEdit.getProperty(name + ".dock-position");
227: if (dockPosition == null)
228: dockPosition = DockableWindowManager.FLOATING;
229: }
230:
231: void save() {
232: jEdit.setProperty(name + ".dock-position", dockPosition);
233: }
234: } //}}}
235:
236: //{{{ WindowCompare class
237: static class WindowCompare implements Comparator {
238: public int compare(Object obj1, Object obj2) {
239: Entry e1 = (Entry) obj1;
240: Entry e2 = (Entry) obj2;
241:
242: return StandardUtilities.compareStrings(e1.title, e2.title,
243: true);
244: }
245: } //}}}
246: } //}}}
|