PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/options/DockingOptionPane.java

#
Java | 388 lines | 304 code | 37 blank | 47 comment | 14 complexity | 788694f70d7d6ccc2b5664b51593a3d0 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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 java.awt.BorderLayout;
  25. import java.awt.Component;
  26. import java.awt.Dimension;
  27. import java.awt.GridLayout;
  28. import java.awt.event.ActionEvent;
  29. import java.awt.event.ActionListener;
  30. import java.awt.event.ItemEvent;
  31. import java.awt.event.ItemListener;
  32. import java.util.Collections;
  33. import java.util.Comparator;
  34. import java.util.HashMap;
  35. import java.util.Vector;
  36. import javax.swing.Box;
  37. import javax.swing.DefaultCellEditor;
  38. import javax.swing.DefaultComboBoxModel;
  39. import javax.swing.JCheckBox;
  40. import javax.swing.JComboBox;
  41. import javax.swing.JLabel;
  42. import javax.swing.JPanel;
  43. import javax.swing.JScrollPane;
  44. import javax.swing.JTable;
  45. import javax.swing.table.AbstractTableModel;
  46. import javax.swing.table.TableCellRenderer;
  47. import javax.swing.table.TableColumn;
  48. import org.gjt.sp.jedit.AbstractOptionPane;
  49. import org.gjt.sp.jedit.jEdit;
  50. import org.gjt.sp.jedit.gui.DockableWindowManager;
  51. import org.gjt.sp.util.StandardUtilities;
  52. //}}}
  53. //{{{ DockingOptionPane class
  54. @SuppressWarnings("serial")
  55. public class DockingOptionPane extends AbstractOptionPane
  56. {
  57. //{{{ DockingOptionPane constructor
  58. public DockingOptionPane()
  59. {
  60. super("docking");
  61. } //}}}
  62. //{{{ _init() method
  63. public void _init()
  64. {
  65. setLayout(new BorderLayout());
  66. add(BorderLayout.NORTH,createDockingOptionsPanel());
  67. add(BorderLayout.CENTER,createWindowTableScroller());
  68. dockableSetSelection.setModel(
  69. new DefaultComboBoxModel(windowModel.getDockableSets()));
  70. } //}}}
  71. //{{{ _save() method
  72. public void _save()
  73. {
  74. jEdit.setBooleanProperty(AUTO_LOAD_MODE_LAYOUT_PROP, autoLoadModeLayout.isSelected());
  75. jEdit.setBooleanProperty(AUTO_SAVE_MODE_LAYOUT_PROP, autoSaveModeLayout.isSelected());
  76. windowModel.save();
  77. } //}}}
  78. //{{{ Private members
  79. //{{{ Instance variables
  80. private JTable windowTable;
  81. private WindowTableModel windowModel;
  82. private JCheckBox autoLoadModeLayout;
  83. private JCheckBox autoSaveModeLayout;
  84. private JComboBox dockableSetSelection;
  85. //}}}
  86. private static final String DOCKING_OPTIONS_PREFIX = "options.docking.";
  87. public static final String AUTO_LOAD_MODE_LAYOUT_PROP = DOCKING_OPTIONS_PREFIX + "autoLoadModeLayout";
  88. private static final String AUTO_LOAD_MODE_LAYOUT_LABEL = AUTO_LOAD_MODE_LAYOUT_PROP + ".label";
  89. public static final String AUTO_SAVE_MODE_LAYOUT_PROP = DOCKING_OPTIONS_PREFIX + "autoSaveModeLayout";
  90. private static final String AUTO_SAVE_MODE_LAYOUT_LABEL = AUTO_SAVE_MODE_LAYOUT_PROP + ".label";
  91. private JPanel createDockingOptionsPanel()
  92. {
  93. JPanel p = new JPanel();
  94. p.setLayout(new GridLayout(0, 1));
  95. boolean autoLoadModeLayoutProp = jEdit.getBooleanProperty(
  96. AUTO_LOAD_MODE_LAYOUT_PROP, false);
  97. autoLoadModeLayout = new JCheckBox(
  98. jEdit.getProperty(AUTO_LOAD_MODE_LAYOUT_LABEL),
  99. autoLoadModeLayoutProp);
  100. p.add(autoLoadModeLayout);
  101. autoSaveModeLayout = new JCheckBox(
  102. jEdit.getProperty(AUTO_SAVE_MODE_LAYOUT_LABEL),
  103. jEdit.getBooleanProperty(AUTO_SAVE_MODE_LAYOUT_PROP, false));
  104. p.add(autoSaveModeLayout);
  105. autoSaveModeLayout.setEnabled(autoLoadModeLayoutProp);
  106. autoLoadModeLayout.addActionListener(new ActionListener()
  107. {
  108. public void actionPerformed(ActionEvent e)
  109. {
  110. autoSaveModeLayout.setEnabled(autoLoadModeLayout.isSelected());
  111. }
  112. });
  113. Box vSetSelection = Box.createVerticalBox();
  114. p.add(vSetSelection);
  115. Box setSelection = Box.createHorizontalBox();
  116. vSetSelection.add(setSelection);
  117. setSelection.add(Box.createHorizontalStrut(6));
  118. setSelection.add(new JLabel(jEdit.getProperty(
  119. "options.docking.selectSet.label")));
  120. setSelection.add(Box.createHorizontalStrut(6));
  121. dockableSetSelection = new JComboBox();
  122. setSelection.add(dockableSetSelection);
  123. dockableSetSelection.addItemListener(new ItemListener()
  124. {
  125. public void itemStateChanged(ItemEvent e)
  126. {
  127. windowModel.showSet((String) dockableSetSelection.getSelectedItem());
  128. }
  129. });
  130. setSelection.add(Box.createHorizontalStrut(6));
  131. vSetSelection.add(Box.createVerticalStrut(6));
  132. return p;
  133. }
  134. //{{{ createWindowTableScroller() method
  135. private JScrollPane createWindowTableScroller()
  136. {
  137. windowModel = createWindowModel();
  138. windowTable = new JTable(windowModel);
  139. windowTable.getTableHeader().setReorderingAllowed(false);
  140. windowTable.setColumnSelectionAllowed(false);
  141. windowTable.setRowSelectionAllowed(false);
  142. windowTable.setCellSelectionEnabled(false);
  143. DockPositionCellRenderer comboBox = new DockPositionCellRenderer();
  144. windowTable.setRowHeight(comboBox.getPreferredSize().height);
  145. TableColumn column = windowTable.getColumnModel().getColumn(1);
  146. column.setCellRenderer(comboBox);
  147. column.setCellEditor(new DefaultCellEditor(new DockPositionCellRenderer()));
  148. Dimension d = windowTable.getPreferredSize();
  149. d.height = Math.min(d.height,50);
  150. JScrollPane scroller = new JScrollPane(windowTable);
  151. scroller.setPreferredSize(d);
  152. return scroller;
  153. } //}}}
  154. //{{{ createWindowModel() method
  155. private static WindowTableModel createWindowModel()
  156. {
  157. return new WindowTableModel();
  158. } //}}}
  159. //}}}
  160. //{{{ DockPositionCellRenderer class
  161. static class DockPositionCellRenderer extends JComboBox
  162. implements TableCellRenderer
  163. {
  164. DockPositionCellRenderer()
  165. {
  166. super(new String[] {
  167. DockableWindowManager.FLOATING,
  168. DockableWindowManager.TOP,
  169. DockableWindowManager.LEFT,
  170. DockableWindowManager.BOTTOM,
  171. DockableWindowManager.RIGHT
  172. });
  173. DockPositionCellRenderer.this.setRequestFocusEnabled(false);
  174. }
  175. public Component getTableCellRendererComponent(JTable table,
  176. Object value, boolean isSelected, boolean hasFocus,
  177. int row, int column)
  178. {
  179. setSelectedItem(value);
  180. return this;
  181. }
  182. } //}}}
  183. } //}}}
  184. //{{{ WindowTableModel class
  185. @SuppressWarnings("serial")
  186. class WindowTableModel extends AbstractTableModel
  187. {
  188. private static final String PLUGIN_SET_PREFIX = "Plugin: ";
  189. private static final String CORE_DOCKABLE_SET = "Core";
  190. private static final String ALL_DOCKABLE_SET = "All";
  191. private HashMap<String, Vector<Entry>> dockableSets;
  192. private Vector<Entry> windows;
  193. //{{{ WindowTableModel constructor
  194. WindowTableModel()
  195. {
  196. dockableSets = new HashMap<String, Vector<Entry>>();
  197. Vector<Entry> all = new Vector<Entry>();
  198. dockableSets.put(ALL_DOCKABLE_SET, all);
  199. windows = new Vector<Entry>();
  200. String[] dockables = DockableWindowManager.getRegisteredDockableWindows();
  201. for (String dockable: dockables)
  202. {
  203. String plugin = DockableWindowManager.
  204. getDockableWindowPluginName(dockable);
  205. String set;
  206. if (plugin != null)
  207. set = PLUGIN_SET_PREFIX + plugin;
  208. else
  209. set = CORE_DOCKABLE_SET;
  210. Vector<Entry> currentSetDockables = dockableSets.get(set);
  211. if (currentSetDockables == null)
  212. {
  213. currentSetDockables = new Vector<Entry>();
  214. dockableSets.put(set, currentSetDockables);
  215. }
  216. Entry entry = new Entry(dockable);
  217. currentSetDockables.add(entry);
  218. all.add(entry);
  219. }
  220. showSet(ALL_DOCKABLE_SET);
  221. } //}}}
  222. public Vector<String> getDockableSets()
  223. {
  224. Vector<String> sets = new Vector<String>();
  225. for (String set: dockableSets.keySet())
  226. sets.add(set);
  227. sets.remove(ALL_DOCKABLE_SET);
  228. sets.remove(CORE_DOCKABLE_SET);
  229. Collections.sort(sets);
  230. sets.insertElementAt(CORE_DOCKABLE_SET, 0);
  231. sets.insertElementAt(ALL_DOCKABLE_SET, 0);
  232. return sets;
  233. }
  234. //{{{ showSet() method
  235. public void showSet(String set)
  236. {
  237. windows = dockableSets.get(set);
  238. Collections.sort(windows,new WindowCompare());
  239. fireTableDataChanged();
  240. } //}}}
  241. //{{{ getColumnCount() method
  242. public int getColumnCount()
  243. {
  244. return 2;
  245. } //}}}
  246. //{{{ getRowCount() method
  247. public int getRowCount()
  248. {
  249. return windows.size();
  250. } //}}}
  251. //{{{ getColumnClass() method
  252. public Class getColumnClass(int col)
  253. {
  254. switch(col)
  255. {
  256. case 0:
  257. case 1:
  258. return String.class;
  259. default:
  260. throw new InternalError();
  261. }
  262. } //}}}
  263. //{{{ getValueAt() method
  264. public Object getValueAt(int row, int col)
  265. {
  266. Entry window = (Entry)windows.elementAt(row);
  267. switch(col)
  268. {
  269. case 0:
  270. return window.title;
  271. case 1:
  272. return window.dockPosition;
  273. default:
  274. throw new InternalError();
  275. }
  276. } //}}}
  277. //{{{ isCellEditable() method
  278. public boolean isCellEditable(int row, int col)
  279. {
  280. return col != 0;
  281. } //}}}
  282. //{{{ setValueAt() method
  283. public void setValueAt(Object value, int row, int col)
  284. {
  285. if(col == 0)
  286. return;
  287. Entry window = (Entry)windows.elementAt(row);
  288. switch(col)
  289. {
  290. case 1:
  291. window.dockPosition = (String)value;
  292. break;
  293. default:
  294. throw new InternalError();
  295. }
  296. fireTableRowsUpdated(row,row);
  297. } //}}}
  298. //{{{ getColumnName() method
  299. public String getColumnName(int index)
  300. {
  301. switch(index)
  302. {
  303. case 0:
  304. return jEdit.getProperty("options.docking.title");
  305. case 1:
  306. return jEdit.getProperty("options.docking.dockPosition");
  307. default:
  308. throw new InternalError();
  309. }
  310. } //}}}
  311. //{{{ save() method
  312. public void save()
  313. {
  314. for(int i = 0; i < windows.size(); i++)
  315. {
  316. ((Entry)windows.elementAt(i)).save();
  317. }
  318. } //}}}
  319. //{{{ Entry class
  320. static class Entry
  321. {
  322. String name;
  323. String title;
  324. String dockPosition;
  325. Entry(String name)
  326. {
  327. this.name = name;
  328. title = jEdit.getProperty(name + ".title");
  329. if(title == null)
  330. title = name;
  331. dockPosition = jEdit.getProperty(name + ".dock-position");
  332. if(dockPosition == null)
  333. dockPosition = DockableWindowManager.FLOATING;
  334. }
  335. void save()
  336. {
  337. jEdit.setProperty(name + ".dock-position",dockPosition);
  338. }
  339. } //}}}
  340. //{{{ WindowCompare class
  341. static class WindowCompare implements Comparator<Object>
  342. {
  343. public int compare(Object obj1, Object obj2)
  344. {
  345. Entry e1 = (Entry)obj1;
  346. Entry e2 = (Entry)obj2;
  347. return StandardUtilities.compareStrings(
  348. e1.title,e2.title,true);
  349. }
  350. } //}}}
  351. } //}}}