001: package com.vividsolutions.jump.workbench.ui.plugin.datastore;
002:
003: import java.awt.Component;
004: import java.awt.Dialog;
005: import java.awt.Dimension;
006: import java.awt.Frame;
007: import java.awt.GridBagConstraints;
008: import java.awt.GridBagLayout;
009: import java.awt.GridLayout;
010: import java.awt.Window;
011: import java.awt.event.ActionEvent;
012: import java.awt.event.ActionListener;
013: import java.util.ArrayList;
014: import java.util.Arrays;
015: import java.util.Collection;
016: import java.util.Collections;
017: import java.util.Comparator;
018: import java.util.Iterator;
019: import java.util.List;
020:
021: import javax.swing.DefaultListCellRenderer;
022: import javax.swing.DefaultListModel;
023: import javax.swing.Icon;
024: import javax.swing.ImageIcon;
025: import javax.swing.JButton;
026: import javax.swing.JLabel;
027: import javax.swing.JList;
028: import javax.swing.JOptionPane;
029: import javax.swing.JPanel;
030: import javax.swing.JScrollPane;
031: import javax.swing.ListModel;
032: import javax.swing.SwingUtilities;
033: import javax.swing.event.ListSelectionEvent;
034: import javax.swing.event.ListSelectionListener;
035:
036: import com.vividsolutions.jump.I18N;
037: import com.vividsolutions.jump.datastore.DataStoreConnection;
038: import com.vividsolutions.jump.datastore.DataStoreDriver;
039: import com.vividsolutions.jump.datastore.DataStoreException;
040: import com.vividsolutions.jump.util.Block;
041: import com.vividsolutions.jump.workbench.WorkbenchContext;
042: import com.vividsolutions.jump.workbench.datastore.ConnectionDescriptor;
043: import com.vividsolutions.jump.workbench.datastore.ConnectionManager;
044: import com.vividsolutions.jump.workbench.registry.Registry;
045: import com.vividsolutions.jump.workbench.ui.ErrorHandler;
046: import com.vividsolutions.jump.workbench.ui.OKCancelDialog;
047:
048: public class ConnectionManagerPanel extends JPanel {
049: private Icon CONNECTED_ICON = new ImageIcon(
050: ConnectionManagerPanel.class
051: .getResource("green_circle.png"));
052: private Icon DISCONNECTED_ICON = new ImageIcon(
053: ConnectionManagerPanel.class.getResource("small_red_x.png"));
054: private Icon DBS_ICON = new ImageIcon(ConnectionManagerPanel.class
055: .getResource("databases.gif"));
056: private Icon NEW_DB_ICON = new ImageIcon(
057: ConnectionManagerPanel.class.getResource("newDatabase.gif"));
058: private Icon DELETE_DB_ICON = new ImageIcon(
059: ConnectionManagerPanel.class
060: .getResource("deleteDatabase.gif"));
061:
062: // Partially generated using Eclipse Visual Editor [Jon Aquino 2005-03-08]
063:
064: private JScrollPane scrollPane = null;
065: private JList connectionJList = null;
066: private JPanel buttonPanel = null;
067: private JButton addButton = null;
068: private JButton copyButton = null;
069: private JButton deleteButton = null;
070: private JButton connectButton = null;
071: private JPanel fillerPanel = null;
072: private JButton disconnectButton = null;
073: private ConnectionManager connectionManager;
074: private ErrorHandler errorHandler;
075: private Registry registry;
076: private WorkbenchContext context;
077:
078: public ConnectionManagerPanel(ConnectionManager connectionManager,
079: Registry registry, ErrorHandler errorHandler,
080: WorkbenchContext context) {
081: super ();
082: initialize();
083: this .connectionManager = connectionManager;
084: this .registry = registry;
085: this .errorHandler = errorHandler;
086: this .context = context;
087: initializeConnectionJList();
088: updateButtons();
089: connectionJList.getSelectionModel().addListSelectionListener(
090: new ListSelectionListener() {
091: public void valueChanged(ListSelectionEvent e) {
092: updateButtons();
093: }
094: });
095: }
096:
097: private void initializeConnectionJList() {
098: connectionJList.setModel(createListModel());
099: }
100:
101: private void updateButtons() {
102: boolean hasDrivers = !registry.getEntries(
103: DataStoreDriver.REGISTRY_CLASSIFICATION).isEmpty();
104: boolean hasSelected = !getSelectedConnectionDescriptors()
105: .isEmpty();
106: addButton.setEnabled(hasDrivers);
107: copyButton.setEnabled(hasDrivers && hasSelected);
108: deleteButton.setEnabled(!getSelectedConnectionDescriptors()
109: .isEmpty());
110: connectButton.setEnabled(findSelectedConnection(new Block() {
111: public Object yield(Object connection) {
112: try {
113: return Boolean
114: .valueOf(((DataStoreConnection) connection)
115: .isClosed());
116: } catch (DataStoreException e) {
117: errorHandler.handleThrowable(e);
118: return Boolean.FALSE;
119: }
120: }
121: }));
122: disconnectButton.setEnabled(findSelectedConnection(new Block() {
123: public Object yield(Object connection) {
124: try {
125: return Boolean
126: .valueOf(!((DataStoreConnection) connection)
127: .isClosed());
128: } catch (DataStoreException e) {
129: errorHandler.handleThrowable(e);
130: return Boolean.FALSE;
131: }
132: }
133: }));
134: }
135:
136: private boolean findSelectedConnection(Block criterion) {
137: for (Iterator i = getSelectedConnectionDescriptors().iterator(); i
138: .hasNext();) {
139: ConnectionDescriptor connectionDescriptor = (ConnectionDescriptor) i
140: .next();
141: if (criterion.yield(connectionManager
142: .getConnection(connectionDescriptor)) == Boolean.TRUE) {
143: return true;
144: }
145: }
146: return false;
147: }
148:
149: private ListModel createListModel() {
150: DefaultListModel listModel = new DefaultListModel();
151: for (Iterator i = sort(
152: new ArrayList(connectionManager
153: .getConnectionDescriptors()), new Comparator() {
154: public int compare(Object a, Object b) {
155: return ((ConnectionDescriptor) a).toString()
156: .compareTo(
157: ((ConnectionDescriptor) b)
158: .toString());
159: }
160: }).iterator(); i.hasNext();) {
161: ConnectionDescriptor connectionDescriptor = (ConnectionDescriptor) i
162: .next();
163: listModel.addElement(connectionDescriptor);
164: }
165: return listModel;
166: }
167:
168: private List sort(List collection, Comparator comparator) {
169: Collections.sort(collection, comparator);
170: return collection;
171: }
172:
173: private void initialize() {
174: GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
175: GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
176: GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
177: this .setLayout(new GridBagLayout());
178: // Use #setPreferredSize rather than #setSize; otherwise if the
179: // connection list is pre-populated with items, the preferred
180: // size will be based on the preferred sizes of the items,
181: // and will probably be too narrow. [Jon Aquino 2005-03-11]
182: this .setPreferredSize(new Dimension(400, 300));
183: gridBagConstraints2.gridx = 0;
184: gridBagConstraints2.gridy = 0;
185: gridBagConstraints2.weightx = 1.0;
186: gridBagConstraints2.weighty = 1.0;
187: gridBagConstraints2.fill = java.awt.GridBagConstraints.BOTH;
188: gridBagConstraints2.gridheight = 2;
189: gridBagConstraints2.insets = new java.awt.Insets(5, 5, 5, 5);
190: gridBagConstraints4.gridx = 1;
191: gridBagConstraints4.gridy = 1;
192: gridBagConstraints4.weighty = 1.0D;
193: gridBagConstraints4.fill = java.awt.GridBagConstraints.BOTH;
194: gridBagConstraints1.insets = new java.awt.Insets(5, 0, 0, 5);
195: this .add(getScrollPane(), gridBagConstraints2);
196: this .add(getButtonPanel(), gridBagConstraints1);
197: this .add(getFillerPanel(), gridBagConstraints4);
198: }
199:
200: private JScrollPane getScrollPane() {
201: if (scrollPane == null) {
202: scrollPane = new JScrollPane();
203: scrollPane.setViewportView(getConnectionJList());
204: }
205: return scrollPane;
206: }
207:
208: private JList getConnectionJList() {
209: if (connectionJList == null) {
210: connectionJList = new JList();
211: connectionJList
212: .setCellRenderer(new DefaultListCellRenderer() {
213:
214: public Component getListCellRendererComponent(
215: JList list, Object value, int index,
216: boolean isSelected, boolean cellHasFocus) {
217: ConnectionDescriptor connectionDescriptor = (ConnectionDescriptor) value;
218: super .getListCellRendererComponent(list,
219: connectionDescriptor, index,
220: isSelected, cellHasFocus);
221: try {
222: setIcon(connectionManager
223: .getConnection(
224: connectionDescriptor)
225: .isClosed() ? DISCONNECTED_ICON
226: : CONNECTED_ICON);
227: } catch (DataStoreException e) {
228: errorHandler.handleThrowable(e);
229: }
230: return this ;
231: }
232: });
233: }
234: return connectionJList;
235: }
236:
237: private JPanel getButtonPanel() {
238: if (buttonPanel == null) {
239: GridLayout gridLayout3 = new GridLayout();
240: gridLayout3.setRows(6);
241: gridLayout3.setVgap(5);
242: gridLayout3.setColumns(1);
243: gridLayout3.setHgap(0);
244:
245: buttonPanel = new JPanel();
246: buttonPanel.setLayout(gridLayout3);
247: buttonPanel.add(getAddButton(), null);
248: buttonPanel.add(getCopyButton(), null);
249: buttonPanel.add(getDeleteButton(), null);
250: buttonPanel.add(new JLabel());
251: buttonPanel.add(getConnectButton(), null);
252: buttonPanel.add(getDisconnectButton(), null);
253: }
254: return buttonPanel;
255: }
256:
257: private JButton getAddButton() {
258: if (addButton == null) {
259: addButton = new JButton();
260: addButton.setIcon(NEW_DB_ICON);
261: addButton
262: .setText(I18N
263: .get("jump.workbench.ui.plugin.datastore.ConnectionManagerPanel.Add"));
264: addButton
265: .addActionListener(new java.awt.event.ActionListener() {
266: public void actionPerformed(
267: java.awt.event.ActionEvent e) {
268: ConnectionDescriptor connectionDescriptor = addConnection();
269: initializeConnectionJList();
270: if (connectionDescriptor != null) {
271: getConnectionJList().setSelectedValue(
272: connectionDescriptor, true);
273: }
274: updateButtons();
275: }
276: });
277: }
278: return addButton;
279: }
280:
281: private JButton getCopyButton() {
282: if (copyButton == null) {
283: copyButton = new JButton();
284: copyButton.setIcon(DBS_ICON);
285: copyButton
286: .setText(I18N
287: .get("jump.workbench.ui.plugin.datastore.ConnectionManagerPanel.Copy"));
288: copyButton
289: .addActionListener(new java.awt.event.ActionListener() {
290: public void actionPerformed(
291: java.awt.event.ActionEvent e) {
292: ConnectionDescriptor connectionDescriptor = copyConnection();
293: initializeConnectionJList();
294: if (connectionDescriptor != null) {
295: getConnectionJList().setSelectedValue(
296: connectionDescriptor, true);
297: }
298: updateButtons();
299: }
300: });
301: }
302: return copyButton;
303: }
304:
305: private ConnectionDescriptor copyConnection() {
306: return addOrCopyConnection(
307: I18N
308: .get("jump.workbench.ui.plugin.datastore.ConnectionManagerPanel.Copy-Connection"),
309: getSelectedConnection());
310: }
311:
312: private ConnectionDescriptor addConnection() {
313: // MD - this behavior no longer needed?
314: if (registry
315: .getEntries(DataStoreDriver.REGISTRY_CLASSIFICATION)
316: .isEmpty()) {
317: JOptionPane
318: .showMessageDialog(
319: SwingUtilities.windowForComponent(this ),
320: I18N
321: .get("jump.workbench.ui.plugin.datastore.ConnectionManagerPanel.No-datastore-drivers-are-loaded"));
322: return null;
323: }
324: return addOrCopyConnection(
325: I18N
326: .get("jump.workbench.ui.plugin.datastore.ConnectionManagerPanel.Add-Connection"),
327: null);
328: }
329:
330: private ConnectionDescriptor addOrCopyConnection(String title,
331: ConnectionDescriptor connDesc) {
332: Window window = SwingUtilities
333: .windowForComponent(ConnectionManagerPanel.this );
334: OKCancelDialog.Validator validator = new OKCancelDialog.Validator() {
335: public String validateInput(Component component) {
336: return ((ConnectionDescriptorPanel) component)
337: .validateInput();
338: }
339: };
340: final ConnectionDescriptorPanel connectionDescriptorPanel = new ConnectionDescriptorPanel(
341: registry, context);
342: if (connDesc != null)
343: connectionDescriptorPanel.setParameters(connDesc);
344: final OKCancelDialog dialog = window instanceof Dialog ? new OKCancelDialog(
345: (Dialog) window, title, true,
346: connectionDescriptorPanel, validator)
347: : new OKCancelDialog((Frame) window, title, true,
348: connectionDescriptorPanel, validator);
349: dialog.setVisible(true);
350: if (!dialog.wasOKPressed()) {
351: return null;
352: }
353: try {
354: // Don't use PasswordPrompter dialog, as the user has already
355: // entered a password in the ConnectionDescriptorPanel
356: // [Jon Aquino 2005-03-15]
357: connectionManager
358: .getOpenConnection(connectionDescriptorPanel
359: .getConnectionDescriptor());
360: } catch (Exception e) {
361: e.printStackTrace(System.err);
362: // At least make sure a closed connection exists, so
363: // a connection appears in the list box. [Jon Aquino 2005-03-09]
364: connectionManager.getConnection(connectionDescriptorPanel
365: .getConnectionDescriptor());
366: }
367: return connectionDescriptorPanel.getConnectionDescriptor();
368: }
369:
370: private JButton getDeleteButton() {
371: if (deleteButton == null) {
372: deleteButton = new JButton();
373: deleteButton.setIcon(DELETE_DB_ICON);
374: deleteButton
375: .setText(I18N
376: .get("jump.workbench.ui.plugin.datastore.ConnectionManagerPanel.Delete"));
377: deleteButton
378: .addActionListener(new java.awt.event.ActionListener() {
379: public void actionPerformed(
380: java.awt.event.ActionEvent e) {
381: try {
382: deleteSelectedConnections();
383: } catch (DataStoreException x) {
384: errorHandler.handleThrowable(x);
385: }
386: initializeConnectionJList();
387: updateButtons();
388: }
389: });
390: }
391: return deleteButton;
392: }
393:
394: private JButton getConnectButton() {
395: if (connectButton == null) {
396: connectButton = new JButton();
397: connectButton.setIcon(CONNECTED_ICON);
398: connectButton
399: .setText(I18N
400: .get("jump.workbench.ui.plugin.datastore.ConnectionManagerPanel.Connect"));
401: connectButton.addActionListener(new ActionListener() {
402: public void actionPerformed(ActionEvent e) {
403: try {
404: openSelectedConnections();
405: } catch (Exception x) {
406: errorHandler.handleThrowable(x);
407: }
408: updateButtons();
409: repaintConnectionJList();
410: }
411: });
412: }
413: return connectButton;
414: }
415:
416: private JPanel getFillerPanel() {
417: if (fillerPanel == null) {
418: fillerPanel = new JPanel();
419: fillerPanel.setLayout(new GridBagLayout());
420: }
421: return fillerPanel;
422: }
423:
424: private JButton getDisconnectButton() {
425: if (disconnectButton == null) {
426: disconnectButton = new JButton();
427: disconnectButton.setIcon(DISCONNECTED_ICON);
428: disconnectButton
429: .setText(I18N
430: .get("jump.workbench.ui.plugin.datastore.ConnectionManagerPanel.Disconnect"));
431: disconnectButton
432: .addActionListener(new java.awt.event.ActionListener() {
433: public void actionPerformed(
434: java.awt.event.ActionEvent e) {
435: try {
436: closeSelectedConnections();
437: } catch (DataStoreException x) {
438: errorHandler.handleThrowable(x);
439: }
440: updateButtons();
441: repaintConnectionJList();
442: }
443: });
444: }
445: return disconnectButton;
446: }
447:
448: private void repaintConnectionJList() {
449: connectionJList.repaint();
450: }
451:
452: /**
453: * Gets the first selected connection, or null if none.
454: *
455: * @return the first selected connection
456: * @return null if none selected
457: */
458: private ConnectionDescriptor getSelectedConnection() {
459: for (Iterator i = getSelectedConnectionDescriptors().iterator(); i
460: .hasNext();) {
461: ConnectionDescriptor connectionDescriptor = (ConnectionDescriptor) i
462: .next();
463: return connectionDescriptor;
464: }
465: return null;
466: }
467:
468: private void deleteSelectedConnections() throws DataStoreException {
469: for (Iterator i = getSelectedConnectionDescriptors().iterator(); i
470: .hasNext();) {
471: ConnectionDescriptor connectionDescriptor = (ConnectionDescriptor) i
472: .next();
473: connectionManager
474: .deleteConnectionDescriptor(connectionDescriptor);
475: }
476: }
477:
478: private void openSelectedConnections() throws Exception {
479: for (Iterator i = getSelectedConnectionDescriptors().iterator(); i
480: .hasNext();) {
481: ConnectionDescriptor connectionDescriptor = (ConnectionDescriptor) i
482: .next();
483: if (connectionManager.getConnection(connectionDescriptor)
484: .isClosed()) {
485: new PasswordPrompter().getOpenConnection(
486: connectionManager, connectionDescriptor, this );
487: }
488: }
489: }
490:
491: private void closeSelectedConnections() throws DataStoreException {
492: for (Iterator i = getSelectedConnectionDescriptors().iterator(); i
493: .hasNext();) {
494: ConnectionDescriptor connectionDescriptor = (ConnectionDescriptor) i
495: .next();
496: if (!connectionManager.getConnection(connectionDescriptor)
497: .isClosed()) {
498: connectionManager.getConnection(connectionDescriptor)
499: .close();
500: }
501: }
502: }
503:
504: public Collection getSelectedConnectionDescriptors() {
505: return Arrays.asList(connectionJList.getSelectedValues());
506: }
507:
508: /*
509: public static void main(String[] args) throws Exception {
510: UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
511: JFrame frame = new JFrame("Connection Manager");
512: frame.getContentPane().add(
513: new ConnectionManagerPanel(ConnectionManager
514: .instance(new Blackboard()), new Registry()
515: .createEntry(DataStoreDriver.REGISTRY_CLASSIFICATION,
516: new OracleDataStoreDriver()),
517: new ErrorHandler() {
518: public void handleThrowable(Throwable t) {
519: System.out.println("Handling error: ");
520: t.printStackTrace(System.out);
521: }
522: },null));
523: frame.addWindowListener(new WindowAdapter() {
524: public void windowClosing(WindowEvent e) {
525: System.exit(0);
526: }
527: });
528: frame.pack();
529: GUIUtil.centreOnScreen(frame);
530: frame.setVisible(true);
531: }
532: */
533:
534: }
|