001: /*
002: * PropertiesPanel.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.executequery.gui.prefs;
023:
024: import java.awt.BorderLayout;
025: import java.awt.CardLayout;
026: import java.awt.Color;
027: import java.awt.Dimension;
028: import java.awt.event.ActionEvent;
029: import java.awt.event.ActionListener;
030: import java.util.ArrayList;
031: import java.util.HashMap;
032: import java.util.Iterator;
033: import java.util.List;
034: import java.util.Set;
035: import javax.swing.JPanel;
036: import javax.swing.JScrollPane;
037: import javax.swing.JSplitPane;
038: import javax.swing.JTree;
039: import javax.swing.SwingUtilities;
040: import javax.swing.event.TreeSelectionEvent;
041: import javax.swing.event.TreeSelectionListener;
042: import javax.swing.tree.DefaultMutableTreeNode;
043: import javax.swing.tree.TreePath;
044: import org.executequery.Constants;
045: import org.executequery.GUIUtilities;
046: import org.executequery.ActiveComponent;
047: import org.executequery.gui.*;
048: import org.executequery.components.table.PropsTreeCellRenderer;
049: import org.executequery.components.BottomButtonPanel;
050: import org.underworldlabs.swing.FlatSplitPane;
051: import org.underworldlabs.swing.tree.DynamicTree;
052:
053: /* ----------------------------------------------------------
054: * CVS NOTE: Changes to the CVS repository prior to the
055: * release of version 3.0.0beta1 has meant a
056: * resetting of CVS revision numbers.
057: * ----------------------------------------------------------
058: */
059:
060: /**
061: * Main system preferences panel.
062: *
063: * @author Takis Diakoumis
064: * @version $Revision: 1.6 $
065: * @date $Date: 2006/07/15 13:37:32 $
066: */
067: public class PropertiesPanel extends JPanel implements ActiveComponent,
068: ActionListener, TreeSelectionListener {
069:
070: public static final String TITLE = "Preferences";
071: public static final String FRAME_ICON = "Preferences16.gif";
072:
073: /** the property selection tree */
074: private JTree tree;
075:
076: /** the right-hand property display panel */
077: private JPanel rightPanel;
078:
079: /** the base panel layout */
080: private CardLayout cardLayout;
081:
082: /** map of panels within the layout */
083: private HashMap panelMap;
084:
085: /** the parent container */
086: private ActionContainer parent;
087:
088: /** Constructs a new instance. */
089: public PropertiesPanel(ActionContainer parent) {
090: this (parent, -1);
091: }
092:
093: /**
094: * Constructs a new instance seleting the specified node.
095: *
096: * @param the node to select
097: */
098: public PropertiesPanel(ActionContainer parent, int openRow) {
099: super (new BorderLayout());
100: this .parent = parent;
101: try {
102: jbInit();
103: } catch (Exception e) {
104: e.printStackTrace();
105: }
106:
107: if (openRow != -1) {
108: tree.setSelectionRow(6);
109: }
110: }
111:
112: /** <p>Initializes the state of this instance. */
113: private void jbInit() throws Exception {
114: JSplitPane splitPane = null;
115: if (GUIUtilities.getLookAndFeel() < Constants.GTK_LAF) {
116: splitPane = new FlatSplitPane(JSplitPane.HORIZONTAL_SPLIT);
117: } else {
118: splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
119: }
120:
121: splitPane.setDividerSize(6);
122: setPreferredSize(new Dimension(625, 450));
123:
124: JPanel mainPanel = new JPanel(new BorderLayout());
125: mainPanel.setPreferredSize(new Dimension(625, 400));
126:
127: cardLayout = new CardLayout();
128: rightPanel = new JPanel(cardLayout);
129: splitPane.setRightComponent(rightPanel);
130:
131: // ----------------------------------
132: // initialise branches
133:
134: ArrayList branches = new ArrayList();
135: PropertyNode node = new PropertyNode(PropertyTypes.GENERAL,
136: "General");
137: branches.add(node);
138: node = new PropertyNode(PropertyTypes.LOCALE, "Locale");
139: branches.add(node);
140: // node = new PropertyNode(PropertyTypes.VIEW, "View");
141: // branches.add(node);
142: node = new PropertyNode(PropertyTypes.APPEARANCE, "Display");
143: branches.add(node);
144: node = new PropertyNode(PropertyTypes.SHORTCUTS, "Shortcuts");
145: branches.add(node);
146: node = new PropertyNode(PropertyTypes.LOOK_PLUGIN,
147: "Look & Feel Plugins");
148: branches.add(node);
149:
150: node = new PropertyNode(PropertyTypes.TOOLBAR_GENERAL,
151: "Tool Bar");
152: node.addChild(new PropertyNode(PropertyTypes.TOOLBAR_FILE,
153: "File Tools"));
154: node.addChild(new PropertyNode(PropertyTypes.TOOLBAR_EDIT,
155: "Edit Tools"));
156: node.addChild(new PropertyNode(PropertyTypes.TOOLBAR_DATABASE,
157: "Database Tools"));
158: node.addChild(new PropertyNode(PropertyTypes.TOOLBAR_BROWSER,
159: "Browser Tools"));
160: node.addChild(new PropertyNode(
161: PropertyTypes.TOOLBAR_IMPORT_EXPORT,
162: "Import/Export Tools"));
163: node.addChild(new PropertyNode(PropertyTypes.TOOLBAR_SEARCH,
164: "Search Tools"));
165: node.addChild(new PropertyNode(PropertyTypes.TOOLBAR_SYSTEM,
166: "System Tools"));
167: branches.add(node);
168:
169: node = new PropertyNode(PropertyTypes.EDITOR_GENERAL, "Editor");
170: node.addChild(new PropertyNode(PropertyTypes.EDITOR_BACKGROUND,
171: "Colours"));
172: node.addChild(new PropertyNode(PropertyTypes.EDITOR_FONTS,
173: "Fonts"));
174: node.addChild(new PropertyNode(PropertyTypes.EDITOR_SYNTAX,
175: "Syntax Colours"));
176: branches.add(node);
177:
178: node = new PropertyNode(PropertyTypes.RESULTS, "Results Table");
179: branches.add(node);
180: node = new PropertyNode(PropertyTypes.CONNECTIONS, "Connection");
181: branches.add(node);
182: node = new PropertyNode(PropertyTypes.BROWSER_GENERAL,
183: "Database Browser");
184: branches.add(node);
185:
186: DefaultMutableTreeNode root = new DefaultMutableTreeNode(
187: new PropertyNode(PropertyTypes.SYSTEM, "Preferences"));
188:
189: List children = null;
190: DefaultMutableTreeNode treeNode = null;
191:
192: for (int i = 0, k = branches.size(); i < k; i++) {
193: node = (PropertyNode) branches.get(i);
194: treeNode = new DefaultMutableTreeNode(node);
195: root.add(treeNode);
196:
197: if (node.hasChildren()) {
198: children = node.getChildren();
199: int count = children.size();
200:
201: for (int j = 0; j < count; j++) {
202: treeNode.add(new DefaultMutableTreeNode(children
203: .get(j)));
204: }
205:
206: }
207:
208: }
209:
210: tree = new DynamicTree(root);
211: tree.putClientProperty("JTree.lineStyle", "Angled");
212: tree.setCellRenderer(new PropsTreeCellRenderer());
213:
214: // expand all rows
215: for (int i = 0; i < tree.getRowCount(); i++) {
216: tree.expandRow(i);
217: }
218:
219: Dimension leftPanelDim = new Dimension(150, 350);
220: JScrollPane js = new JScrollPane(tree);
221: js.setPreferredSize(leftPanelDim);
222:
223: JPanel leftPanel = new JPanel(new BorderLayout());
224: leftPanel.setBackground(Color.white);
225: leftPanel.setMinimumSize(leftPanelDim);
226: leftPanel.setMaximumSize(leftPanelDim);
227: leftPanel.add(js, BorderLayout.CENTER);
228: splitPane.setLeftComponent(leftPanel);
229:
230: mainPanel.add(splitPane, BorderLayout.CENTER);
231: mainPanel.add(new BottomButtonPanel(this , null, "prefs", parent
232: .isDialog()), BorderLayout.SOUTH);
233:
234: add(mainPanel, BorderLayout.CENTER);
235:
236: /*
237: add(mainPanel, new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0,
238: GridBagConstraints.SOUTHEAST,
239: GridBagConstraints.BOTH,
240: new Insets(5, 5, 5, 5), 0, 0));
241: */
242: panelMap = new HashMap();
243: tree.addTreeSelectionListener(this );
244:
245: // setup the first panel
246: JPanel panel = new PropertiesRootPanel();
247: PropertyNode rootObject = (PropertyNode) root.getUserObject();
248: String label = rootObject.getLabel();
249: panelMap.put(label, panel);
250: rightPanel.add(panel, label);
251: cardLayout.show(rightPanel, label);
252:
253: tree.setSelectionRow(0);
254: }
255:
256: public void valueChanged(TreeSelectionEvent e) {
257: final TreePath path = e.getPath();
258: SwingUtilities.invokeLater(new Runnable() {
259: public void run() {
260: getProperties(path.getPath());
261: }
262: });
263: }
264:
265: private void getProperties(Object[] selection) {
266: DefaultMutableTreeNode n = (DefaultMutableTreeNode) selection[selection.length - 1];
267: PropertyNode node = (PropertyNode) n.getUserObject();
268:
269: JPanel panel = null;
270: int id = node.getNodeId();
271: String label = node.getLabel();
272:
273: if (panelMap.containsKey(label)) {
274: cardLayout.show(rightPanel, label);
275: return;
276: }
277:
278: switch (id) {
279: case PropertyTypes.SYSTEM:
280: panel = new PropertiesRootPanel();
281: break;
282: case PropertyTypes.GENERAL:
283: panel = new PropertiesGeneral();
284: break;
285: case PropertyTypes.LOCALE:
286: panel = new PropertiesLocales();
287: break;
288: /*
289: case PropertyTypes.VIEW:
290: panel = new PropertiesView();
291: break;
292: */
293: case PropertyTypes.SHORTCUTS:
294: panel = new PropertiesKeyShortcuts();
295: break;
296: case PropertyTypes.APPEARANCE:
297: panel = new PropertiesAppearance();
298: break;
299: case PropertyTypes.TOOLBAR_GENERAL:
300: panel = new PropertiesToolBarGeneral();
301: break;
302: case PropertyTypes.TOOLBAR_FILE:
303: panel = new PropertiesToolBar("File Tools");
304: break;
305: case PropertyTypes.TOOLBAR_EDIT:
306: panel = new PropertiesToolBar("Edit Tools");
307: break;
308: case PropertyTypes.TOOLBAR_SEARCH:
309: panel = new PropertiesToolBar("Search Tools");
310: break;
311: case PropertyTypes.TOOLBAR_DATABASE:
312: panel = new PropertiesToolBar("Database Tools");
313: break;
314: case PropertyTypes.TOOLBAR_BROWSER:
315: panel = new PropertiesToolBar("Browser Tools");
316: break;
317: case PropertyTypes.TOOLBAR_IMPORT_EXPORT:
318: panel = new PropertiesToolBar("Import/Export Tools");
319: break;
320: case PropertyTypes.TOOLBAR_SYSTEM:
321: panel = new PropertiesToolBar("System Tools");
322: break;
323: case PropertyTypes.LOOK_PLUGIN:
324: panel = new PropertiesLookPlugins();
325: break;
326: case PropertyTypes.EDITOR_GENERAL:
327: panel = new PropertiesEditorGeneral();
328: break;
329: case PropertyTypes.EDITOR_BACKGROUND:
330: panel = new PropertiesEditorBackground();
331: break;
332: /*
333: case PropertyTypes.EDITOR_DISPLAY:
334: panel = new PropertiesEditorDisplay();
335: break;
336: */
337: case PropertyTypes.EDITOR_FONTS:
338: panel = new PropertiesEditorFonts();
339: break;
340: case PropertyTypes.EDITOR_SYNTAX:
341: panel = new PropertiesEditorSyntax();
342: break;
343: case PropertyTypes.RESULTS:
344: panel = new PropertiesResults();
345: break;
346: case PropertyTypes.CONNECTIONS:
347: panel = new PropertiesConns();
348: break;
349: case PropertyTypes.BROWSER_GENERAL:
350: panel = new PropertiesBrowserGeneral();
351: break;
352: }
353:
354: panelMap.put(label, panel);
355: rightPanel.add(panel, label);
356: cardLayout.show(rightPanel, label);
357:
358: }
359:
360: public void actionPerformed(ActionEvent e) {
361: GUIUtilities.showWaitCursor(); // ????
362: Set keys = panelMap.keySet();
363:
364: for (Iterator i = keys.iterator(); i.hasNext();) {
365: UserPreferenceFunction panel = (UserPreferenceFunction) panelMap
366: .get(i.next().toString());
367: panel.save();
368: }
369:
370: GUIUtilities.resetToolBar(true);
371: // TODO: CHECK THIS
372: GUIUtilities.preferencesChanged();
373:
374: parent.finished();
375: }
376:
377: public void cleanup() {
378: if (panelMap.containsKey("Colours")) {
379: PropertiesEditorBackground panel = (PropertiesEditorBackground) panelMap
380: .get("Colours");
381: panel.stopCaretDisplayTimer();
382: }
383: }
384:
385: }
|