001: /*
002: * Created on Aug 1, 2004
003: *
004: */
005: package com.pk;
006:
007: import java.awt.BorderLayout;
008: import java.awt.ScrollPane;
009: import java.awt.event.ActionEvent;
010: import java.awt.event.ActionListener;
011: import java.awt.event.KeyEvent;
012:
013: import java.util.List;
014:
015: import javax.swing.AbstractAction;
016: import javax.swing.JButton;
017: import javax.swing.JDialog;
018: import javax.swing.JPanel;
019: import javax.swing.JScrollPane;
020: import javax.swing.JSplitPane;
021: import javax.swing.JTree;
022: import javax.swing.KeyStroke;
023: import javax.swing.LookAndFeel;
024: import javax.swing.event.TreeSelectionEvent;
025: import javax.swing.event.TreeSelectionListener;
026: import javax.swing.tree.DefaultMutableTreeNode;
027:
028: import com.pk.preferences.ExportPanel;
029: import com.pk.preferences.GeneralPanel;
030: import com.pk.preferences.PropertiesPanel;
031:
032: /**
033: * @author Chris Taylor
034: *
035: */
036: public class Preferences extends JPanel implements
037: TreeSelectionListener, ActionListener {
038: /**
039: *
040: */
041: private static final long serialVersionUID = 7690494394227269192L;
042: public static int WIDTH = 340;
043: public static int HEIGHT = 570;
044:
045: private JSplitPane jSplitPane = null;
046: private JTree jTree = null;
047: private Config config = null;
048: private PropertiesPanel propertiesPanel = null;
049: private ExportPanel exportPanel = null;
050: private GeneralPanel generalPanel = null;
051: private DefaultMutableTreeNode topDatasourceNode = null;
052: private DefaultMutableTreeNode topNode = null;
053: private DefaultMutableTreeNode exportPrefencesNode = null;
054: private DefaultMutableTreeNode generalPrefencesNode = null;
055: private JDialog jDialog = null;
056: private ConnectionConfig selConnectionConfig;
057: private JButton saveButton;
058: private JButton closeButton;
059:
060: private class CloseAction extends AbstractAction {
061:
062: /**
063: *
064: */
065: private static final long serialVersionUID = 8735012374912583312L;
066:
067: /* (non-Javadoc)
068: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
069: */
070: public void actionPerformed(ActionEvent arg0) {
071: jDialog.dispose();
072: }
073: }
074:
075: /**
076: * This is the default constructor
077: */
078: public Preferences() {
079: super ();
080: CloseAction closeAction = new CloseAction();
081: getInputMap()
082: .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
083: "ESCAPE");
084: getActionMap().put("ESCAPE", closeAction);
085:
086: }
087:
088: /**
089: * This method initializes this
090: *
091: * @return void
092: */
093: public void initialize() {
094: this .setSize(Preferences.HEIGHT, Preferences.WIDTH);
095: this .setLayout(new java.awt.BorderLayout());
096: this .add(getJSplitPane(), java.awt.BorderLayout.CENTER);
097: JPanel tmp = new JPanel();
098: saveButton = new JButton("Save");
099: saveButton.addActionListener(this );
100: closeButton = new JButton("Close");
101: closeButton.addActionListener(this );
102: tmp.add(saveButton);
103: tmp.add(closeButton);
104: this .add(tmp, BorderLayout.SOUTH);
105:
106: }
107:
108: /**
109: * This method initializes jContentPane
110: *
111: * @return javax.swing.JPanel
112: */
113: // private javax.swing.JPanel getJContentPane() {
114: // if(jContentPane == null) {
115: // jContentPane = new javax.swing.JPanel();
116: // jContentPane.setLayout(new java.awt.BorderLayout());
117: // jContentPane.add(getJSplitPane(), java.awt.BorderLayout.CENTER);
118: // }
119: // return jContentPane;
120: // }
121: /**
122: * This method initializes jSplitPane
123: *
124: * @return javax.swing.JSplitPane
125: */
126: private JSplitPane getJSplitPane() {
127: if (jSplitPane == null) {
128: jSplitPane = new JSplitPane();
129: ScrollPane tmp = new ScrollPane();
130: tmp.add(getJTree());
131: jSplitPane.setLeftComponent(tmp);
132: propertiesPanel = new PropertiesPanel(this );
133: jSplitPane.setRightComponent(propertiesPanel);
134: jSplitPane.setDividerLocation(165);
135: }
136: return jSplitPane;
137: }
138:
139: /**
140: * This method initializes jTree
141: *
142: * @return javax.swing.JTree
143: */
144: private JTree getJTree() {
145: if (jTree == null) {
146: topNode = new DefaultMutableTreeNode("Config");
147: generalPrefencesNode = new DefaultMutableTreeNode("General");
148: topNode.add(generalPrefencesNode);
149: exportPrefencesNode = new DefaultMutableTreeNode("Export");
150: topNode.add(exportPrefencesNode);
151: topDatasourceNode = new DefaultMutableTreeNode(
152: "Datasources");
153: topNode.add(topDatasourceNode);
154:
155: DefaultMutableTreeNode tmp2 = null;
156: List configs = config.getConnections();
157: int size = configs.size();
158: for (int x = 0; x < size; x++) {
159: tmp2 = new DefaultMutableTreeNode(configs.get(x));
160: topDatasourceNode.add(tmp2);
161: }
162: jTree = new JTree(topNode);
163: jTree.addTreeSelectionListener(this );
164: }
165: return jTree;
166: }
167:
168: /**
169: * @return Returns the configs.
170: */
171: public Config getConfig() {
172: return config;
173: }
174:
175: /**
176: * @param configs The configs to set.
177: */
178: public void setConfig(Config argConfig) {
179: this .config = argConfig;
180: }
181:
182: /* (non-Javadoc)
183: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
184: */
185: public void actionPerformed(ActionEvent actionEvent) {
186: Object source = actionEvent.getSource();
187: if (source == saveButton) {
188: if (jSplitPane.getRightComponent() == propertiesPanel) {
189:
190: if (selConnectionConfig != null) {
191: selConnectionConfig
192: .setDatabaseDialectName((String) propertiesPanel
193: .getTxtDialect().getSelectedItem());
194: selConnectionConfig.setDriver(propertiesPanel
195: .getTxtDriver().getText());
196: selConnectionConfig.setName(propertiesPanel
197: .getTxtName().getText());
198: selConnectionConfig.setUrl(propertiesPanel
199: .getTxtURL().getText());
200:
201: }
202:
203: config.writeFile();
204:
205: config.orderConnections();
206: jTree = null;
207: jTree = getJTree();
208: jTree.expandPath(jTree.getPathForRow(3));
209: JScrollPane scrollPane = new JScrollPane(jTree);
210: getJSplitPane().setLeftComponent(scrollPane);
211:
212: } else if (jSplitPane.getRightComponent() == exportPanel) {
213: config.setExportDelimiterChar(exportPanel
214: .getTxtExportDelimiter().getText());
215: config.setEncloseByChar(exportPanel.getTxtEncloseBy()
216: .getText());
217: config.setBorderColor(exportPanel.getBorderColor()
218: .getText());
219: config.setHeaderColor(exportPanel.getHeaderColor()
220: .getText());
221: config.setAltRowColor(exportPanel.getAltRowColor()
222: .getText());
223: config.setUseCdata(exportPanel.getUseCdata()
224: .isSelected());
225: config.writeFile();
226: } else if (jSplitPane.getRightComponent() == generalPanel) {
227: config.setSelectedLookAndFeel((LookAndFeel) config
228: .getLookandFeels().get(
229: generalPanel.getLookAndFeelComboBox()
230: .getSelectedIndex()));
231: config.setHistoryMode(generalPanel
232: .getSelectedHistoryMode());
233: config.writeFile();
234: }
235: } else if (source == propertiesPanel.getNewButton()) {
236: selConnectionConfig = new ConnectionConfig();
237: config.addConnectionConfig(selConnectionConfig);
238: selConnectionConfig.setName("NewConfig");
239: topDatasourceNode.add(new DefaultMutableTreeNode(
240: selConnectionConfig));
241: propertiesPanel.getTxtDialect().setSelectedItem(
242: selConnectionConfig.getDatabaseDialectName());
243: propertiesPanel.getTxtDriver().setText(
244: selConnectionConfig.getDriver());
245: propertiesPanel.getTxtName().setText(
246: selConnectionConfig.getName());
247: propertiesPanel.getTxtURL().setText(
248: selConnectionConfig.getUrl());
249: } else if (source == propertiesPanel.getRemoveButton()) {
250: config.removeConnection(selConnectionConfig);
251: config.writeFile();
252: propertiesPanel.getTxtDialect().setSelectedItem("");
253: propertiesPanel.getTxtDriver().setText("");
254: propertiesPanel.getTxtName().setText("");
255: propertiesPanel.getTxtURL().setText("");
256: jTree = null;
257: jTree = getJTree();
258: JScrollPane scrollPane = new JScrollPane(jTree);
259: jTree.expandPath(jTree.getPathForRow(3));
260: getJSplitPane().setLeftComponent(scrollPane);
261: } else if (source == closeButton) {
262: jDialog.setVisible(false);
263: }
264: }
265:
266: /* (non-Javadoc)
267: * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent)
268: */
269: public void valueChanged(TreeSelectionEvent arg0) {
270: DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree
271: .getLastSelectedPathComponent();
272: Object tmp = node.getUserObject();
273: if (tmp instanceof ConnectionConfig) {
274: if (jSplitPane.getRightComponent() != propertiesPanel) {
275: jSplitPane.setRightComponent(propertiesPanel);
276: }
277: selConnectionConfig = (ConnectionConfig) tmp;
278: propertiesPanel.getTxtDialect().setSelectedItem(
279: selConnectionConfig.getDatabaseDialectName());
280: propertiesPanel.getTxtDriver().setText(
281: selConnectionConfig.getDriver());
282: propertiesPanel.getTxtName().setText(
283: selConnectionConfig.getName());
284: propertiesPanel.getTxtURL().setText(
285: selConnectionConfig.getUrl());
286: } else if (node == exportPrefencesNode) {
287: if (exportPanel == null) {
288: exportPanel = new ExportPanel(this );
289: }
290: if (jSplitPane.getRightComponent() != exportPanel) {
291: jSplitPane.setRightComponent(exportPanel);
292: exportPanel.getTxtExportDelimiter().setText(
293: config.getExportDelimiterChar());
294: exportPanel.getTxtEncloseBy().setText(
295: config.getEncloseByChar());
296: exportPanel.getBorderColor().setText(
297: config.getBorderColor());
298: exportPanel.getHeaderColor().setText(
299: config.getHeaderColor());
300: exportPanel.getAltRowColor().setText(
301: config.getAltRowColor());
302: exportPanel.getUseCdata().setSelected(
303: config.isUseCdata());
304: }
305:
306: } else if (node == generalPrefencesNode) {
307: if (generalPanel == null) {
308: generalPanel = new GeneralPanel(this );
309: generalPanel.setLookandFeels(config);
310:
311: }
312: generalPanel
313: .setSelectedHistoryMode(config.getHistoryMode());
314: generalPanel.getLookAndFeelComboBox().setSelectedItem(
315: config.getSelectedLookAndFeel().getName());
316: jSplitPane.setRightComponent(generalPanel);
317: }
318:
319: }
320:
321: /**
322: * @param dialog The jDialog to set.
323: */
324: public void setJDialog(JDialog dialog) {
325: jDialog = dialog;
326: }
327: } // @jve:decl-index=0:visual-constraint="65,28"
|