/jEdit/tags/before_bsh-2-0b4/org/gjt/sp/jedit/options/DockingOptionPane.java

# · Java · 275 lines · 192 code · 36 blank · 47 comment · 9 complexity · 1af066e49d2332b6fc165e9e90d0abd5 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. package org.gjt.sp.jedit.options;
  23. //{{{ Imports
  24. import javax.swing.table.*;
  25. import javax.swing.*;
  26. import java.awt.*;
  27. import java.util.Vector;
  28. import java.util.Collections;
  29. import java.util.Comparator;
  30. import org.gjt.sp.jedit.gui.*;
  31. import org.gjt.sp.jedit.*;
  32. //}}}
  33. //{{{ DockingOptionPane class
  34. public class DockingOptionPane extends AbstractOptionPane
  35. {
  36. //{{{ DockingOptionPane constructor
  37. public DockingOptionPane()
  38. {
  39. super("docking");
  40. } //}}}
  41. //{{{ _init() method
  42. public void _init()
  43. {
  44. setLayout(new BorderLayout());
  45. add(BorderLayout.CENTER,createWindowTableScroller());
  46. } //}}}
  47. //{{{ _save() method
  48. public void _save()
  49. {
  50. windowModel.save();
  51. } //}}}
  52. //{{{ Private members
  53. //{{{ Instance variables
  54. private JTable windowTable;
  55. private WindowTableModel windowModel;
  56. //}}}
  57. //{{{ createWindowTableScroller() method
  58. private JScrollPane createWindowTableScroller()
  59. {
  60. windowModel = createWindowModel();
  61. windowTable = new JTable(windowModel);
  62. windowTable.getTableHeader().setReorderingAllowed(false);
  63. windowTable.setColumnSelectionAllowed(false);
  64. windowTable.setRowSelectionAllowed(false);
  65. windowTable.setCellSelectionEnabled(false);
  66. DockPositionCellRenderer comboBox = new DockPositionCellRenderer();
  67. windowTable.setRowHeight(comboBox.getPreferredSize().height);
  68. TableColumn column = windowTable.getColumnModel().getColumn(1);
  69. column.setCellRenderer(comboBox);
  70. column.setCellEditor(new DefaultCellEditor(new DockPositionCellRenderer()));
  71. Dimension d = windowTable.getPreferredSize();
  72. d.height = Math.min(d.height,50);
  73. JScrollPane scroller = new JScrollPane(windowTable);
  74. scroller.setPreferredSize(d);
  75. return scroller;
  76. } //}}}
  77. //{{{ createWindowModel() method
  78. private static WindowTableModel createWindowModel()
  79. {
  80. return new WindowTableModel();
  81. } //}}}
  82. //}}}
  83. //{{{ DockPositionCellRenderer class
  84. static class DockPositionCellRenderer extends JComboBox
  85. implements TableCellRenderer
  86. {
  87. DockPositionCellRenderer()
  88. {
  89. super(new String[] {
  90. DockableWindowManager.FLOATING,
  91. DockableWindowManager.TOP,
  92. DockableWindowManager.LEFT,
  93. DockableWindowManager.BOTTOM,
  94. DockableWindowManager.RIGHT
  95. });
  96. DockPositionCellRenderer.this.setRequestFocusEnabled(false);
  97. }
  98. public Component getTableCellRendererComponent(JTable table,
  99. Object value, boolean isSelected, boolean hasFocus,
  100. int row, int column)
  101. {
  102. setSelectedItem(value);
  103. return this;
  104. }
  105. } //}}}
  106. } //}}}
  107. //{{{ WindowTableModel class
  108. class WindowTableModel extends AbstractTableModel
  109. {
  110. private Vector windows;
  111. //{{{ WindowTableModel constructor
  112. WindowTableModel()
  113. {
  114. windows = new Vector();
  115. String[] dockables = DockableWindowManager
  116. .getRegisteredDockableWindows();
  117. for(int i = 0; i < dockables.length; i++)
  118. {
  119. windows.addElement(new Entry(dockables[i]));
  120. }
  121. sort();
  122. } //}}}
  123. //{{{ sort() method
  124. public void sort()
  125. {
  126. Collections.sort(windows,new WindowCompare());
  127. fireTableDataChanged();
  128. } //}}}
  129. //{{{ getColumnCount() method
  130. public int getColumnCount()
  131. {
  132. return 2;
  133. } //}}}
  134. //{{{ getRowCount() method
  135. public int getRowCount()
  136. {
  137. return windows.size();
  138. } //}}}
  139. //{{{ getColumnClass() method
  140. public Class getColumnClass(int col)
  141. {
  142. switch(col)
  143. {
  144. case 0:
  145. case 1:
  146. return String.class;
  147. default:
  148. throw new InternalError();
  149. }
  150. } //}}}
  151. //{{{ getValueAt() method
  152. public Object getValueAt(int row, int col)
  153. {
  154. Entry window = (Entry)windows.elementAt(row);
  155. switch(col)
  156. {
  157. case 0:
  158. return window.title;
  159. case 1:
  160. return window.dockPosition;
  161. default:
  162. throw new InternalError();
  163. }
  164. } //}}}
  165. //{{{ isCellEditable() method
  166. public boolean isCellEditable(int row, int col)
  167. {
  168. return (col != 0);
  169. } //}}}
  170. //{{{ setValueAt() method
  171. public void setValueAt(Object value, int row, int col)
  172. {
  173. if(col == 0)
  174. return;
  175. Entry window = (Entry)windows.elementAt(row);
  176. switch(col)
  177. {
  178. case 1:
  179. window.dockPosition = (String)value;
  180. break;
  181. default:
  182. throw new InternalError();
  183. }
  184. fireTableRowsUpdated(row,row);
  185. } //}}}
  186. //{{{ getColumnName() method
  187. public String getColumnName(int index)
  188. {
  189. switch(index)
  190. {
  191. case 0:
  192. return jEdit.getProperty("options.docking.title");
  193. case 1:
  194. return jEdit.getProperty("options.docking.dockPosition");
  195. default:
  196. throw new InternalError();
  197. }
  198. } //}}}
  199. //{{{ save() method
  200. public void save()
  201. {
  202. for(int i = 0; i < windows.size(); i++)
  203. {
  204. ((Entry)windows.elementAt(i)).save();
  205. }
  206. } //}}}
  207. //{{{ Entry class
  208. static class Entry
  209. {
  210. String name;
  211. String title;
  212. String dockPosition;
  213. Entry(String name)
  214. {
  215. this.name = name;
  216. title = jEdit.getProperty(name + ".title");
  217. if(title == null)
  218. title = name;
  219. dockPosition = jEdit.getProperty(name + ".dock-position");
  220. if(dockPosition == null)
  221. dockPosition = DockableWindowManager.FLOATING;
  222. }
  223. void save()
  224. {
  225. jEdit.setProperty(name + ".dock-position",dockPosition);
  226. }
  227. } //}}}
  228. //{{{ WindowCompare class
  229. static class WindowCompare implements Comparator
  230. {
  231. public int compare(Object obj1, Object obj2)
  232. {
  233. Entry e1 = (Entry)obj1;
  234. Entry e2 = (Entry)obj2;
  235. return MiscUtilities.compareStrings(
  236. e1.title,e2.title,true);
  237. }
  238. } //}}}
  239. } //}}}