001: /*
002: * Copyright (C) 2004 Nicky BRAMANTE
003: *
004: * This file is part of FreeQueryBuilder
005: *
006: * FreeQueryBuilder 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 (at your option) 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: * Send questions or suggestions to nickyb@interfree.it
021: */
022:
023: package it.frb.admin;
024:
025: import java.awt.BorderLayout;
026: import java.awt.CardLayout;
027: import java.awt.Dimension;
028:
029: import java.awt.event.ActionEvent;
030:
031: import java.util.Enumeration;
032: import java.util.StringTokenizer;
033:
034: import javax.swing.Box;
035: import javax.swing.BoxLayout;
036: import javax.swing.JButton;
037: import javax.swing.JFrame;
038: import javax.swing.JPanel;
039: import javax.swing.JScrollPane;
040: import javax.swing.JTree;
041:
042: import javax.swing.border.CompoundBorder;
043: import javax.swing.border.EmptyBorder;
044: import javax.swing.border.EtchedBorder;
045: import javax.swing.event.TreeSelectionEvent;
046: import javax.swing.event.TreeSelectionListener;
047:
048: import javax.swing.tree.DefaultMutableTreeNode;
049: import javax.swing.tree.DefaultTreeModel;
050: import javax.swing.tree.TreePath;
051:
052: public class DialogAdministrator extends ModalDialog implements
053: TreeSelectionListener {
054: private final static int LEVEL_ROOT = 1;
055: private final static int LEVEL_DRV = 2;
056: private final static int LEVEL_PROF = 3;
057:
058: private CardLayout card;
059: private JPanel detail;
060: private JTree tree;
061:
062: protected InfoDriverPane dinfopane;
063: protected InfoProfilePane pinfopane;
064: protected InfoClasspathPane cinfopane;
065:
066: private JButton btnAdd;
067: private JButton btnRemove;
068:
069: public DialogAdministrator(JFrame syswin) {
070: super (syswin, "JDBC administrator", 640, 480);
071:
072: tree = new JTree(new DefaultMutableTreeNode("JDBC", true));
073: tree.putClientProperty("JTree.lineStyle", "Angled");
074: tree.addTreeSelectionListener(this );
075:
076: Box bar = new Box(BoxLayout.X_AXIS);
077: bar.add(btnAdd = UIUtilities.createCustomButton("add", this ));
078: bar.add(btnRemove = UIUtilities.createCustomButton("remove",
079: this ));
080: bar.add(Box.createHorizontalGlue());
081: btnRemove.setEnabled(false);
082:
083: JScrollPane scroll = new JScrollPane(tree);
084: scroll.setMaximumSize(new Dimension(220, 100));
085: scroll.setMinimumSize(new Dimension(220, 100));
086: scroll.setPreferredSize(new Dimension(220, 100));
087:
088: DefaultPanel side = new DefaultPanel(2, 2);
089: side.setCenterComponent(scroll);
090: side.setSouthComponent(bar);
091:
092: detail = new JPanel(card = new CardLayout());
093: detail.add("level1", cinfopane = new InfoClasspathPane(this ));
094: detail.add("level2", dinfopane = new InfoDriverPane(this ));
095: detail.add("level3", pinfopane = new InfoProfilePane(this ));
096:
097: detail.setBorder(new CompoundBorder(new EtchedBorder(
098: EtchedBorder.RAISED), new EmptyBorder(5, 5, 5, 5)));
099: side.setBorder(new CompoundBorder(new EtchedBorder(
100: EtchedBorder.RAISED), new EmptyBorder(5, 5, 5, 5)));
101:
102: getContentPane().add(detail, BorderLayout.CENTER);
103: getContentPane().add(side, BorderLayout.WEST);
104: }
105:
106: protected void onRunning() {
107: DefaultMutableTreeNode root = new DefaultMutableTreeNode(
108: "JDBC", true);
109: DefaultTreeModel model = new DefaultTreeModel(root, true);
110:
111: String declared_drivers = "";
112: String declared_profiles = "";
113:
114: try {
115: declared_drivers = UserSession.jdbc.get(
116: UserSession.JDBC_DRIVERS).toString();
117: declared_profiles = UserSession.jdbc.get(
118: UserSession.JDBC_PROFILES).toString();
119:
120: } catch (NullPointerException npe) {
121: UserSession.load();
122: declared_drivers = UserSession.jdbc.get(
123: UserSession.JDBC_DRIVERS).toString();
124: declared_profiles = UserSession.jdbc.get(
125: UserSession.JDBC_PROFILES).toString();
126: }
127:
128: StringTokenizer k = new StringTokenizer(declared_drivers, ";");
129: while (k.hasMoreTokens()) {
130: InfoDriverPane.InfoElement dinfo = new InfoDriverPane.InfoElement(
131: k.nextToken());
132: dinfo.file = UserSession.jdbc.get(dinfo.name + ".file")
133: .toString();
134: if (!dinfo.file.startsWith("$"))
135: cinfopane.addArchive(dinfo.file);
136: dinfo.driver = UserSession.jdbc.get(dinfo.name + ".driver")
137: .toString();
138: dinfo.example = UserSession.jdbc.get(
139: dinfo.name + ".example").toString();
140:
141: root.add(new DefaultMutableTreeNode(dinfo, true));
142: }
143:
144: k = new StringTokenizer(declared_profiles, ";");
145: while (k.hasMoreTokens()) {
146: String name = k.nextToken();
147: String drv = UserSession.jdbc.get(name + ".drv").toString();
148: if (drv.startsWith("$")) {
149: for (Enumeration e = root.children(); e
150: .hasMoreElements();) {
151: DefaultMutableTreeNode node = (DefaultMutableTreeNode) e
152: .nextElement();
153: InfoDriverPane.InfoElement dinfo = (InfoDriverPane.InfoElement) node
154: .getUserObject();
155: if (dinfo.name.equals(drv.substring(1))) {
156: InfoProfilePane.InfoElement pinfo = new InfoProfilePane.InfoElement(
157: dinfo, name);
158: pinfo.url = UserSession.jdbc.get(
159: pinfo.name + ".url").toString();
160: pinfo.uid = UserSession.jdbc.get(
161: pinfo.name + ".uid").toString();
162:
163: node.add(new DefaultMutableTreeNode(pinfo,
164: false));
165: }
166: }
167: }
168: }
169:
170: tree.setModel(model);
171: tree.setSelectionPath(new TreePath(root));
172: }
173:
174: protected void save() {
175: DefaultMutableTreeNode needreload = (DefaultMutableTreeNode) tree
176: .getSelectionPath().getLastPathComponent();
177: ((DefaultTreeModel) tree.getModel()).reload(needreload);
178:
179: StringBuffer drivers = new StringBuffer();
180: StringBuffer profiles = new StringBuffer();
181:
182: DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree
183: .getModel().getRoot();
184: for (int i = 0; i < root.getChildCount(); i++) {
185: DefaultMutableTreeNode node = (DefaultMutableTreeNode) root
186: .getChildAt(i);
187: drivers.append(node + ";");
188:
189: InfoDriverPane.InfoElement dinfo = (InfoDriverPane.InfoElement) node
190: .getUserObject();
191: UserSession.jdbc.put(dinfo.name + ".file", dinfo.file);
192: UserSession.jdbc.put(dinfo.name + ".driver", dinfo.driver);
193: UserSession.jdbc
194: .put(dinfo.name + ".example", dinfo.example);
195:
196: for (int j = 0; j < node.getChildCount(); j++) {
197: DefaultMutableTreeNode child = (DefaultMutableTreeNode) node
198: .getChildAt(j);
199: InfoProfilePane.InfoElement pinfo = (InfoProfilePane.InfoElement) child
200: .getUserObject();
201:
202: profiles.append(pinfo.name + ";");
203:
204: UserSession.jdbc.put(pinfo.name + ".drv", "$"
205: + dinfo.name);
206: UserSession.jdbc.put(pinfo.name + ".url", pinfo.url);
207: UserSession.jdbc.put(pinfo.name + ".uid", pinfo.uid);
208: }
209: }
210: UserSession.jdbc.put(UserSession.JDBC_DRIVERS, drivers
211: .toString());
212: UserSession.jdbc.put(UserSession.JDBC_PROFILES, profiles
213: .toString());
214: UserSession.save();
215: }
216:
217: public void valueChanged(TreeSelectionEvent e) {
218: if (tree.getSelectionCount() == 1) {
219: int level = e.getPath().getPathCount();
220: DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree
221: .getSelectionPath().getLastPathComponent();
222:
223: card.show(detail, "level" + level);
224:
225: btnRemove.setEnabled(level != LEVEL_ROOT);
226:
227: if (level == LEVEL_DRV)
228: dinfopane.setInfo((InfoDriverPane.InfoElement) node
229: .getUserObject());
230: else if (level == LEVEL_PROF)
231: pinfopane.setInfo((InfoProfilePane.InfoElement) node
232: .getUserObject());
233: }
234: }
235:
236: public void actionPerformed(ActionEvent ae) {
237: int level = -1;
238:
239: DefaultTreeModel model = null;
240: DefaultMutableTreeNode node = null;
241:
242: if (tree.getSelectionCount() == 1) {
243: level = tree.getSelectionPath().getPathCount();
244: node = (DefaultMutableTreeNode) tree.getSelectionPath()
245: .getLastPathComponent();
246: model = (DefaultTreeModel) tree.getModel();
247: }
248:
249: if (ae.getSource() == btnAdd) {
250: if (level == -1)
251: return;
252:
253: DefaultMutableTreeNode child;
254: if (level == LEVEL_ROOT) {
255: node.add(child = new DefaultMutableTreeNode(
256: new InfoDriverPane.InfoElement(), true));
257: } else {
258: if (level == LEVEL_PROF) {
259: node = (DefaultMutableTreeNode) node.getParent();
260: tree.setSelectionPath(new TreePath(node.getPath()));
261: }
262:
263: InfoProfilePane.InfoElement pinfo = new InfoProfilePane.InfoElement(
264: (InfoDriverPane.InfoElement) node
265: .getUserObject());
266: node.add(child = new DefaultMutableTreeNode(pinfo,
267: false));
268: }
269:
270: model.reload(node);
271: tree.setSelectionPath(new TreePath(child.getPath()));
272: } else if (ae.getSource() == btnRemove) {
273: if (level == -1 || level == LEVEL_ROOT)
274: return;
275:
276: tree.setSelectionPath(new TreePath(node.getPreviousNode()
277: .getPath()));
278:
279: if (level == LEVEL_DRV) {
280: for (int i = 0; i < node.getChildCount(); i++) {
281: DefaultMutableTreeNode child = (DefaultMutableTreeNode) node
282: .getChildAt(i);
283: InfoProfilePane.InfoElement pinfo = (InfoProfilePane.InfoElement) child
284: .getUserObject();
285:
286: UserSession.jdbc.remove(pinfo.name + ".drv");
287: UserSession.jdbc.remove(pinfo.name + ".url");
288: UserSession.jdbc.remove(pinfo.name + ".uid");
289: }
290:
291: InfoDriverPane.InfoElement dinfo = (InfoDriverPane.InfoElement) node
292: .getUserObject();
293: UserSession.jdbc.remove(dinfo.name + ".file");
294: UserSession.jdbc.remove(dinfo.name + ".driver");
295: UserSession.jdbc.remove(dinfo.name + ".example");
296: } else {
297: InfoProfilePane.InfoElement pinfo = (InfoProfilePane.InfoElement) node
298: .getUserObject();
299:
300: UserSession.jdbc.remove(pinfo.name + ".drv");
301: UserSession.jdbc.remove(pinfo.name + ".url");
302: UserSession.jdbc.remove(pinfo.name + ".uid");
303: }
304:
305: model.removeNodeFromParent(node);
306: DialogAdministrator.this.save();
307: } else {
308: super.actionPerformed(ae);
309: }
310: }
311: }
|