001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032: package com.vividsolutions.jump.workbench.datasource;
033:
034: import com.vividsolutions.jump.I18N;
035: import com.vividsolutions.jump.workbench.ui.OKCancelPanel;
036:
037: import java.awt.*;
038: import java.awt.event.*;
039:
040: import java.util.Collection;
041: import java.util.HashMap;
042: import java.util.HashSet;
043: import java.util.Iterator;
044:
045: import javax.swing.*;
046:
047: import org.openjump.core.CheckOS;
048:
049: /**
050: * Contains the various DataSourceQueryChooser panels, regardless of whether
051: * they are for files, databases, web services, or other kinds of DataSources.
052: * <p>
053: * A bit confusing for files, as there are two "format" comboboxes for the user
054: * to choose from: one for the DataSource type, and another for the file extension.
055: * In the future, file DataSources may have their own dialog, eliminating the
056: * first combobox.
057: */
058: public class DataSourceQueryChooserDialog extends JDialog {
059: private CardLayout cardLayout = new CardLayout();
060: private BorderLayout borderLayout2 = new BorderLayout();
061: private JPanel mainPanel = new JPanel(cardLayout);
062: private JPanel formatPanel = new JPanel();
063: private GridBagLayout gridBagLayout1 = new GridBagLayout();
064: private JComboBox formatComboBox = new JComboBox();
065: private JLabel formatLabel = new JLabel() {
066: {
067: setDisplayedMnemonic('F');
068: setLabelFor(formatComboBox);
069: }
070: };
071:
072: private HashMap componentToNameMap = new HashMap();
073: private OKCancelPanel okCancelPanel = new OKCancelPanel();
074:
075: public DataSourceQueryChooserDialog(
076: Collection dataSourceQueryChoosers, Frame frame,
077: String title, boolean modal) {
078: super (frame, title, modal);
079: init(dataSourceQueryChoosers);
080: try {
081: jbInit();
082: pack();
083: } catch (Exception ex) {
084: ex.printStackTrace();
085: }
086: addComponentListener(new ComponentAdapter() {
087: public void componentShown(ComponentEvent e) {
088: okCancelPanel.setOKPressed(false);
089: }
090: });
091: addWindowListener(new WindowAdapter() {
092: public void windowClosing(WindowEvent e) {
093: //User may have hit OK, got a validation-error dialog, then hit the
094: //X button. [Jon Aquino]
095: okCancelPanel.setOKPressed(false);
096: }
097: });
098:
099: //Set the selected item to trigger the event that sets the panel. [Jon Aquino]
100: formatComboBox.setSelectedItem(formatComboBox.getItemAt(0));
101: }
102:
103: private void init(Collection dataSourceQueryChoosers) {
104: //Some components may be shared, so use a Set. [Jon Aquino]
105: HashSet components = new HashSet();
106: for (Iterator i = dataSourceQueryChoosers.iterator(); i
107: .hasNext();) {
108: DataSourceQueryChooser chooser = (DataSourceQueryChooser) i
109: .next();
110: formatComboBox.addItem(chooser);
111: components.add(chooser.getComponent());
112: }
113:
114: int j = 0;
115: for (Iterator i = components.iterator(); i.hasNext();) {
116: Component component = (Component) i.next();
117:
118: //Can't use DataSourceQueryChooser name because several DataSourceQueryChoosers may
119: //share a component (e.g. FileDataSourceQueryChooser). [Jon Aquino]
120: j++;
121: componentToNameMap
122: .put(
123: component,
124: I18N
125: .get("datasource.DataSourceQueryChooserDialog.card")
126: + " " + j);
127: mainPanel.add(component, name(component));
128: }
129: }
130:
131: private String name(Component component) {
132: return (String) componentToNameMap.get(component);
133: }
134:
135: private void jbInit() throws Exception {
136: this .getContentPane().setLayout(borderLayout2);
137: formatPanel.setLayout(gridBagLayout1);
138: formatPanel.setBorder(BorderFactory.createEtchedBorder());
139: formatLabel.setText(I18N
140: .get("datasource.DataSourceQueryChooserDialog.format"));
141: formatComboBox
142: .addActionListener(new java.awt.event.ActionListener() {
143: public void actionPerformed(ActionEvent e) {
144: formatComboBox_actionPerformed(e);
145: }
146: });
147: okCancelPanel
148: .addActionListener(new java.awt.event.ActionListener() {
149: public void actionPerformed(ActionEvent e) {
150: okCancelPanel_actionPerformed(e);
151: }
152: });
153:
154: this .getContentPane().add(mainPanel, BorderLayout.NORTH);
155: this .getContentPane().add(formatPanel, BorderLayout.CENTER);
156: this .getContentPane().add(okCancelPanel, BorderLayout.SOUTH);
157: formatPanel.add(formatComboBox,
158: new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
159: GridBagConstraints.WEST,
160: GridBagConstraints.NONE, new Insets(16, 4, 16,
161: 4), 0, 0));
162: formatPanel.add(formatLabel,
163: new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
164: GridBagConstraints.EAST,
165: GridBagConstraints.NONE, new Insets(16, 4, 16,
166: 4), 0, 0));
167: }
168:
169: /**
170: * @return true if the user hit OK; false if the user hit Cancel or the Close Window button.
171: */
172: public boolean wasOKPressed() {
173: return okCancelPanel.wasOKPressed();
174: }
175:
176: public void setOKPressed() {
177: //
178: // It is important to call setOKPressed before calling isInputValid
179: // otherwise we run into an infinite loop of actionPerformed calls.
180: //
181: okCancelPanel.setOKPressed(true);
182:
183: if (getCurrentChooser().isInputValid()) {
184: setVisible(false);
185: }
186: }
187:
188: void formatComboBox_actionPerformed(ActionEvent e) {
189: cardLayout.show(mainPanel, name(getCurrentChooser()
190: .getComponent()));
191: }
192:
193: public DataSourceQueryChooser getCurrentChooser() {
194: return (DataSourceQueryChooser) formatComboBox
195: .getSelectedItem();
196: }
197:
198: void okCancelPanel_actionPerformed(ActionEvent e) {
199: if (!okCancelPanel.wasOKPressed()
200: || getCurrentChooser().isInputValid()) {
201: setVisible(false);
202: }
203: // sstein: added else-if to fix MAC-OSX-bug
204: else if ((okCancelPanel.wasOKPressed()) && (CheckOS.isMacOsx())) {
205: //--sstein: leave out validation - because it returns always "false" on Mac-OSX ?
206: //System.out.println("validate input:" + getCurrentChooser().isInputValid());
207: //System.out.println("i am inside");
208: okCancelPanel.setOKPressed(true);
209: setVisible(false);
210: }
211: }
212:
213: public String getSelectedFormat() {
214: return formatComboBox.getSelectedItem().toString();
215: }
216:
217: public void setSelectedFormat(String format) {
218: for (int i = 0; i < formatComboBox.getItemCount(); i++) {
219: DataSourceQueryChooser chooser = (DataSourceQueryChooser) formatComboBox
220: .getItemAt(i);
221: if (chooser.toString().equals(format)) {
222: formatComboBox.setSelectedIndex(i);
223:
224: return;
225: }
226: }
227: }
228: }
|