001: /**
002: *
003: */package org.geotools.demo.postgis;
004:
005: import java.awt.Color;
006: import java.awt.Dimension;
007: import java.awt.GridLayout;
008: import java.awt.event.ActionEvent;
009: import java.awt.event.ActionListener;
010: import java.awt.event.KeyAdapter;
011: import java.awt.event.KeyEvent;
012: import java.util.Collections;
013: import java.util.HashMap;
014: import java.util.Map;
015:
016: import javax.swing.JButton;
017: import javax.swing.JDialog;
018: import javax.swing.JLabel;
019: import javax.swing.JPanel;
020: import javax.swing.JPasswordField;
021: import javax.swing.JTextField;
022:
023: import org.geotools.data.DataStoreFactorySpi.Param;
024: import org.geotools.data.postgis.PostgisDataStoreFactory;
025:
026: /**
027: * A dialog that displays the PostGIS connection parameters.
028: *
029: * This class is not important to our main story of connecting to PostGIS
030: * it does serve as a nice example of how to use the Param information
031: * published by the PostgidDataStoreFactory class.
032: * <p>
033: * If you want to write a generic DatastoreDialog you can make use of
034: * the getParametersInfo() method at runtime.
035: * </p>
036: * @author Jody Garnett
037: */
038: public class PostGISDialog extends JDialog implements ActionListener {
039: private static final long serialVersionUID = -4773502007868922959L;
040: //private PostgisDataStoreFactory factory;
041:
042: private JParamField database;
043: private JParamField dbtype;
044: private JParamField host;
045: private JParamField port;
046: private JParamField schema;
047: private JParamField user;
048: private JPasswordField password;
049: private JButton okay;
050: private JButton cancel;
051:
052: boolean connect = false;
053:
054: public PostGISDialog() {
055: this (Collections.EMPTY_MAP);
056: }
057:
058: public PostGISDialog(Map config) {
059: setTitle("Connection Parameters");
060: setModal(true);
061: dbtype = new JParamField(PostgisDataStoreFactory.DBTYPE, config);
062: host = new JParamField(PostgisDataStoreFactory.HOST, config);
063: port = new JParamField(PostgisDataStoreFactory.PORT, config);
064:
065: schema = new JParamField(PostgisDataStoreFactory.SCHEMA, config);
066: database = new JParamField(PostgisDataStoreFactory.DATABASE,
067: config);
068: user = new JParamField(PostgisDataStoreFactory.USER, config);
069: password = new JPasswordField((String) config
070: .get(PostgisDataStoreFactory.USER.key));
071: password
072: .setToolTipText(PostgisDataStoreFactory.PASSWD.description);
073:
074: okay = new JButton("OK");
075: cancel = new JButton("Cancel");
076:
077: okay.addActionListener(this );
078: cancel.addActionListener(this );
079:
080: // layout dialog
081: setLayout(new GridLayout(0, 2));
082:
083: add(new JLabel("DBType"));
084: add(dbtype);
085: add(new JLabel("Host"));
086: add(host);
087: add(new JLabel("Port"));
088: add(port);
089: add(new JLabel("Schema"));
090: add(schema);
091: add(new JLabel("Database"));
092: add(database);
093: add(new JLabel("user"));
094: add(user);
095: add(new JLabel("password"));
096: add(password);
097:
098: add(new JLabel(""));
099: JPanel buttons = new JPanel();
100: add(buttons);
101: buttons.add(okay);
102: buttons.add(cancel);
103:
104: setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
105: Dimension preferredSize = getPreferredSize();
106: preferredSize.height += 30;
107: setSize(preferredSize);
108: }
109:
110: public Map getProperties() {
111: if (!connect) {
112: return null;
113: }
114: Map config = new HashMap();
115: config.put(PostgisDataStoreFactory.DBTYPE.key, dbtype
116: .getValue());
117:
118: config.put(PostgisDataStoreFactory.HOST.key, host.getValue());
119: config.put(PostgisDataStoreFactory.PORT.key, port.getValue());
120:
121: config.put(PostgisDataStoreFactory.SCHEMA.key, schema
122: .getValue());
123: config.put(PostgisDataStoreFactory.DATABASE.key, database
124: .getValue());
125: config.put(PostgisDataStoreFactory.USER.key, user.getValue());
126: config.put(PostgisDataStoreFactory.PASSWD.key, new String(
127: password.getPassword()));
128:
129: return config;
130: }
131:
132: public void actionPerformed(ActionEvent e) {
133: String action = e.getActionCommand();
134: if ("OK".equals(action)) {
135: connect = true;
136: }
137: setVisible(false);
138: }
139:
140: class JParamField extends JTextField {
141: Param param;
142: Object value;
143:
144: JParamField(Param param) {
145: this (param, Collections.EMPTY_MAP);
146: }
147:
148: JParamField(Param param, Map map) {
149: super (14);
150: this .param = param;
151: setValue(map.get(param.key));
152: addKeyListener(new KeyAdapter() {
153: public void keyReleased(KeyEvent e) {
154: refresh();
155: }
156: });
157: setToolTipText(param.description);
158: }
159:
160: public void refresh() {
161: try {
162: JParamField.this .value = param.parse(getText());
163: setToolTipText(param.description);
164: setForeground(Color.BLACK);
165: } catch (Throwable e) {
166: setToolTipText(e.getLocalizedMessage());
167: setForeground(Color.RED);
168: JParamField.this .value = null;
169: }
170: }
171:
172: public void setValue(Object value) {
173: if (value == null) {
174: value = param.sample;
175: }
176: this .value = value;
177: if (value == null) {
178: setText("");
179: } else {
180: setText(param.text(value));
181: }
182: }
183:
184: public Object getValue() {
185: return value;
186: }
187: }
188: }
|