KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > options > DockingOptionPane


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
23 package org.gjt.sp.jedit.options;
24
25 //{{{ Imports
26
import javax.swing.table.*;
27 import javax.swing.*;
28 import java.awt.*;
29 import java.util.Vector JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.Comparator JavaDoc;
32
33 import org.gjt.sp.jedit.gui.*;
34 import org.gjt.sp.jedit.*;
35 import org.gjt.sp.util.StandardUtilities;
36 //}}}
37

38 //{{{ DockingOptionPane class
39
public class DockingOptionPane extends AbstractOptionPane
40 {
41     //{{{ DockingOptionPane constructor
42
public DockingOptionPane()
43     {
44         super("docking");
45     } //}}}
46

47     //{{{ _init() method
48
public void _init()
49     {
50         setLayout(new BorderLayout());
51         add(BorderLayout.CENTER,createWindowTableScroller());
52     } //}}}
53

54     //{{{ _save() method
55
public void _save()
56     {
57         windowModel.save();
58     } //}}}
59

60     //{{{ Private members
61

62     //{{{ Instance variables
63
private JTable windowTable;
64     private WindowTableModel windowModel;
65     //}}}
66

67     //{{{ createWindowTableScroller() method
68
private JScrollPane createWindowTableScroller()
69     {
70         windowModel = createWindowModel();
71         windowTable = new JTable(windowModel);
72         windowTable.getTableHeader().setReorderingAllowed(false);
73         windowTable.setColumnSelectionAllowed(false);
74         windowTable.setRowSelectionAllowed(false);
75         windowTable.setCellSelectionEnabled(false);
76
77         DockPositionCellRenderer comboBox = new DockPositionCellRenderer();
78         windowTable.setRowHeight(comboBox.getPreferredSize().height);
79         TableColumn column = windowTable.getColumnModel().getColumn(1);
80         column.setCellRenderer(comboBox);
81         column.setCellEditor(new DefaultCellEditor(new DockPositionCellRenderer()));
82
83         Dimension d = windowTable.getPreferredSize();
84         d.height = Math.min(d.height,50);
85         JScrollPane scroller = new JScrollPane(windowTable);
86         scroller.setPreferredSize(d);
87         return scroller;
88     } //}}}
89

90     //{{{ createWindowModel() method
91
private static WindowTableModel createWindowModel()
92     {
93         return new WindowTableModel();
94     } //}}}
95

96     //}}}
97

98     //{{{ DockPositionCellRenderer class
99
static class DockPositionCellRenderer extends JComboBox
100         implements TableCellRenderer
101     {
102         DockPositionCellRenderer()
103         {
104             super(new String JavaDoc[] {
105                 DockableWindowManager.FLOATING,
106                 DockableWindowManager.TOP,
107                 DockableWindowManager.LEFT,
108                 DockableWindowManager.BOTTOM,
109                 DockableWindowManager.RIGHT
110             });
111             DockPositionCellRenderer.this.setRequestFocusEnabled(false);
112         }
113
114         public Component getTableCellRendererComponent(JTable table,
115             Object JavaDoc value, boolean isSelected, boolean hasFocus,
116             int row, int column)
117         {
118             setSelectedItem(value);
119             return this;
120         }
121     } //}}}
122
} //}}}
123

124 //{{{ WindowTableModel class
125
class WindowTableModel extends AbstractTableModel
126 {
127     private Vector JavaDoc windows;
128
129     //{{{ WindowTableModel constructor
130
WindowTableModel()
131     {
132         windows = new Vector JavaDoc();
133
134         String JavaDoc[] dockables = DockableWindowManager
135             .getRegisteredDockableWindows();
136         for(int i = 0; i < dockables.length; i++)
137         {
138             windows.addElement(new Entry(dockables[i]));
139         }
140
141         sort();
142     } //}}}
143

144     //{{{ sort() method
145
public void sort()
146     {
147         Collections.sort(windows,new WindowCompare());
148         fireTableDataChanged();
149     } //}}}
150

151     //{{{ getColumnCount() method
152
public int getColumnCount()
153     {
154         return 2;
155     } //}}}
156

157     //{{{ getRowCount() method
158
public int getRowCount()
159     {
160         return windows.size();
161     } //}}}
162

163     //{{{ getColumnClass() method
164
public Class JavaDoc getColumnClass(int col)
165     {
166         switch(col)
167         {
168         case 0:
169         case 1:
170             return String JavaDoc.class;
171         default:
172             throw new InternalError JavaDoc();
173         }
174     } //}}}
175

176     //{{{ getValueAt() method
177
public Object JavaDoc getValueAt(int row, int col)
178     {
179         Entry window = (Entry)windows.elementAt(row);
180         switch(col)
181         {
182         case 0:
183             return window.title;
184         case 1:
185             return window.dockPosition;
186         default:
187             throw new InternalError JavaDoc();
188         }
189     } //}}}
190

191     //{{{ isCellEditable() method
192
public boolean isCellEditable(int row, int col)
193     {
194         return col != 0;
195     } //}}}
196

197     //{{{ setValueAt() method
198
public void setValueAt(Object JavaDoc value, int row, int col)
199     {
200         if(col == 0)
201             return;
202
203         Entry window = (Entry)windows.elementAt(row);
204         switch(col)
205         {
206         case 1:
207             window.dockPosition = (String JavaDoc)value;
208             break;
209         default:
210             throw new InternalError JavaDoc();
211         }
212
213         fireTableRowsUpdated(row,row);
214     } //}}}
215

216     //{{{ getColumnName() method
217
public String JavaDoc getColumnName(int index)
218     {
219         switch(index)
220         {
221         case 0:
222             return jEdit.getProperty("options.docking.title");
223         case 1:
224             return jEdit.getProperty("options.docking.dockPosition");
225         default:
226             throw new InternalError JavaDoc();
227         }
228     } //}}}
229

230     //{{{ save() method
231
public void save()
232     {
233         for(int i = 0; i < windows.size(); i++)
234         {
235             ((Entry)windows.elementAt(i)).save();
236         }
237     } //}}}
238

239     //{{{ Entry class
240
static class Entry
241     {
242         String JavaDoc name;
243         String JavaDoc title;
244         String JavaDoc dockPosition;
245
246         Entry(String JavaDoc name)
247         {
248             this.name = name;
249             title = jEdit.getProperty(name + ".title");
250             if(title == null)
251                 title = name;
252
253             dockPosition = jEdit.getProperty(name + ".dock-position");
254             if(dockPosition == null)
255                 dockPosition = DockableWindowManager.FLOATING;
256         }
257
258         void save()
259         {
260             jEdit.setProperty(name + ".dock-position",dockPosition);
261         }
262     } //}}}
263

264     //{{{ WindowCompare class
265
static class WindowCompare implements Comparator JavaDoc
266     {
267         public int compare(Object JavaDoc obj1, Object JavaDoc obj2)
268         {
269             Entry e1 = (Entry)obj1;
270             Entry e2 = (Entry)obj2;
271
272             return StandardUtilities.compareStrings(
273                 e1.title,e2.title,true);
274         }
275     } //}}}
276
} //}}}
277
Popular Tags