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.GridBagConstraints;
007: import java.awt.GridBagLayout;
008: import java.awt.Insets;
009: import java.awt.event.ActionEvent;
010: import java.awt.event.ActionListener;
011: import java.util.Arrays;
012: import java.util.Collection;
013: import java.util.Comparator;
014:
015: import javax.swing.DefaultComboBoxModel;
016: import javax.swing.ImageIcon;
017: import javax.swing.JButton;
018: import javax.swing.JComboBox;
019: import javax.swing.JLabel;
020: import javax.swing.JPanel;
021: import javax.swing.SwingUtilities;
022:
023: import com.vividsolutions.jump.I18N;
024:
025: import com.vividsolutions.jump.workbench.WorkbenchContext;
026: import com.vividsolutions.jump.workbench.datastore.ConnectionDescriptor;
027: import com.vividsolutions.jump.workbench.datastore.ConnectionManager;
028: import com.vividsolutions.jump.workbench.ui.OKCancelDialog;
029:
030: /**
031: * Base class for panels with a Connection combobox.
032: */
033: public class ConnectionPanel extends JPanel {
034:
035: protected final static int MAIN_COLUMN_WIDTH = 400;
036:
037: protected final static Insets INSETS = new Insets(2, 2, 2, 2);
038:
039: private WorkbenchContext context;
040:
041: private JComboBox connectionComboBox = null;
042:
043: private int nextRow = 0;
044:
045: private JButton chooseConnectionButton = null;
046:
047: public ConnectionPanel(WorkbenchContext context) {
048: this .context = context;
049: initialize();
050: populateConnectionComboBox();
051: }
052:
053: public ConnectionDescriptor getConnectionDescriptor() {
054: return (ConnectionDescriptor) connectionComboBox
055: .getSelectedItem();
056: }
057:
058: public WorkbenchContext getContext() {
059: return context;
060: }
061:
062: public void populateConnectionComboBox() {
063: ConnectionDescriptor selectedConnectionDescriptor = getConnectionDescriptor();
064: connectionComboBox.setModel(new DefaultComboBoxModel(
065: sortByString(connectionDescriptors().toArray())));
066: // Note that the selectedConnectionDescriptor may no longer exist,
067: // in which case #setSelectedItem will have no effect.
068: // [Jon Aquino 2005-03-10]
069: connectionComboBox
070: .setSelectedItem(selectedConnectionDescriptor);
071: }
072:
073: public String validateInput() {
074: if (getConnectionDescriptor() == null) {
075: return I18N
076: .get("jump.workbench.ui.plugin.datastore.ConnectionPanel.Required-field-missing-Connection");
077: }
078: return null;
079: }
080:
081: protected JComboBox getConnectionComboBox() {
082: if (connectionComboBox == null) {
083: connectionComboBox = new JComboBox();
084: connectionComboBox.setPreferredSize(new Dimension(
085: MAIN_COLUMN_WIDTH, (int) connectionComboBox
086: .getPreferredSize().getHeight()));
087: }
088: return connectionComboBox;
089: }
090:
091: protected void addRow(String caption, Component a, Component b,
092: boolean aStretchesVertically) {
093: add(new JLabel(caption), new GridBagConstraints(0, nextRow, 1,
094: 1, 0, 0, GridBagConstraints.NORTHWEST,
095: GridBagConstraints.NONE, INSETS, 0, 0));
096:
097: if (aStretchesVertically) {
098: add(a, new GridBagConstraints(1, nextRow, 1, 1, 1, 1,
099: GridBagConstraints.NORTHWEST,
100: GridBagConstraints.BOTH, INSETS, 0, 0));
101: } else {
102: add(a, new GridBagConstraints(1, nextRow, 1, 1, 1, 0,
103: GridBagConstraints.NORTHWEST,
104: GridBagConstraints.HORIZONTAL, INSETS, 0, 0));
105: }
106:
107: if (b != null) {
108: add(b, new GridBagConstraints(2, nextRow, 1, 1, 0, 0,
109: GridBagConstraints.NORTHWEST,
110: GridBagConstraints.NONE, INSETS, 0, 0));
111: }
112:
113: nextRow++;
114: }
115:
116: protected Collection connectionDescriptors() {
117: return connectionManager().getConnectionDescriptors();
118: }
119:
120: protected ConnectionManager connectionManager() {
121: return ConnectionManager.instance(context);
122: }
123:
124: protected Object[] sortByString(Object[] objects) {
125: Arrays.sort(objects, new Comparator() {
126: public int compare(Object o1, Object o2) {
127: return o1.toString().compareTo(o2.toString());
128: }
129: });
130: return objects;
131: }
132:
133: private JButton getChooseConnectionButton() {
134: if (chooseConnectionButton == null) {
135: chooseConnectionButton = new JButton();
136: ImageIcon icon = new ImageIcon(ConnectionManagerPanel.class
137: .getResource("databases.gif"));
138: chooseConnectionButton.setIcon(icon);
139: chooseConnectionButton
140: .setToolTipText(I18N
141: .get("jump.workbench.ui.plugin.datastore.ConnectionPanel.Connection-Manager"));
142: chooseConnectionButton.setMargin(new Insets(0, 0, 0, 0));
143: chooseConnectionButton
144: .addActionListener(new ActionListener() {
145: public void actionPerformed(ActionEvent e) {
146: chooseConnection();
147: }
148: });
149: }
150: return chooseConnectionButton;
151: }
152:
153: private void initialize() {
154: setLayout(new GridBagLayout());
155: addRow(
156: I18N
157: .get("jump.workbench.ui.plugin.datastore.ConnectionPanel.Connection"),
158: getConnectionComboBox(), getChooseConnectionButton(),
159: false);
160: }
161:
162: private void chooseConnection() {
163: ConnectionManagerPanel panel = new ConnectionManagerPanel(
164: ConnectionManager.instance(getContext()), getContext()
165: .getRegistry(), getContext().getErrorHandler(),
166: context);
167: OKCancelDialog dialog = new OKCancelDialog(
168: (Dialog) SwingUtilities
169: .windowForComponent(ConnectionPanel.this ),
170: I18N
171: .get("jump.workbench.ui.plugin.datastore.ConnectionPanel.Connection-Manager"),
172: true, panel, new OKCancelDialog.Validator() {
173: public String validateInput(Component component) {
174: return null;
175: }
176: });
177: dialog.setVisible(true);
178: // Even if OK was not pressed, refresh the combobox.
179: // [Jon Aquino 2005-03-16]
180: populateConnectionComboBox();
181: if (!dialog.wasOKPressed()) {
182: return;
183: }
184: if (panel.getSelectedConnectionDescriptors().isEmpty()) {
185: return;
186: }
187: getConnectionComboBox().setSelectedItem(
188: panel.getSelectedConnectionDescriptors().iterator()
189: .next());
190: }
191: }
|