001: package org.geotools.demo;
002:
003: import java.awt.Color;
004: import java.awt.GridBagConstraints;
005: import java.awt.GridBagLayout;
006: import java.awt.Insets;
007: import java.awt.event.ActionEvent;
008: import java.awt.event.ActionListener;
009: import java.io.File;
010: import java.io.FileInputStream;
011: import java.io.FileNotFoundException;
012: import java.io.IOException;
013: import java.io.InputStream;
014: import java.util.Map;
015: import java.util.Properties;
016: import java.util.Vector;
017:
018: import javax.swing.JButton;
019: import javax.swing.JComboBox;
020: import javax.swing.JDialog;
021: import javax.swing.JOptionPane;
022: import javax.swing.JPanel;
023: import javax.swing.JScrollPane;
024: import javax.swing.JTextArea;
025:
026: import org.geotools.data.DataStore;
027: import org.geotools.data.DataStoreFinder;
028: import org.geotools.data.DataUtilities;
029: import org.geotools.data.DefaultQuery;
030: import org.geotools.data.FeatureSource;
031: import org.geotools.demo.postgis.PostGISDialog;
032: import org.geotools.feature.AttributeType;
033: import org.geotools.feature.Feature;
034: import org.geotools.feature.FeatureCollection;
035: import org.geotools.feature.FeatureType;
036: import org.geotools.feature.GeometryAttributeType;
037: import org.geotools.feature.visitor.FeatureVisitor;
038: import org.geotools.filter.FilterTransformer;
039: import org.geotools.filter.text.cql2.CQL;
040: import org.opengis.filter.Filter;
041:
042: public class PostGISLab {
043: /**
044: * Tip: When running from eclipse include the ${file_prompt} as an argument!
045: * @param args
046: * @throws Exception
047: */
048: public static void main(String[] args) throws Exception {
049: Map connectionProperties = getConnectionProperties(args);
050: DataStore dataStore = DataStoreFinder
051: .getDataStore(connectionProperties);
052: String[] typeNames = dataStore.getTypeNames();
053: if (typeNames == null) {
054: JOptionPane.showConfirmDialog(null, "Could not conntect");
055: System.exit(0);
056: }
057: JQuery dialog = new JQuery(dataStore);
058: dialog.setVisible(true);
059: dialog.dispose();
060: System.exit(0);
061: }
062:
063: private static Map getConnectionProperties(String[] args)
064: throws IOException {
065: PostGISDialog dialog;
066:
067: if (args.length == 0) {
068: dialog = new PostGISDialog();
069: } else {
070: File file = new File(args[0]);
071: if (!file.exists()) {
072: throw new FileNotFoundException(file.getAbsolutePath());
073: }
074: InputStream input = new FileInputStream(file);
075: Properties config = new Properties();
076: config.load(input);
077:
078: dialog = new PostGISDialog(config);
079: }
080: dialog.setVisible(true);
081: Map properties = dialog.getProperties();
082: dialog.dispose();
083:
084: if (properties == null) {
085: System.exit(0);
086: }
087: return properties;
088: }
089:
090: static class JQuery extends JDialog {
091: final DataStore dataStore;
092:
093: JTextArea query;
094: JTextArea show;
095: JButton selectButton;
096: JButton closeButton;
097: JComboBox typeNameSelect;
098: JButton schemaButton;
099:
100: private JButton filterButton;
101:
102: JQuery(DataStore database) throws IOException {
103: this .dataStore = database;
104: setTitle("Query");
105: setModal(true);
106: setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
107:
108: JPanel panel = new JPanel();
109: panel.setLayout(new GridBagLayout());
110: GridBagConstraints c = new GridBagConstraints();
111: c.fill = GridBagConstraints.NONE;
112: c.anchor = GridBagConstraints.WEST;
113: c.insets = new Insets(2, 2, 2, 2);
114: c.weightx = 1.0;
115:
116: Vector options = new Vector();
117: String typeNames[] = dataStore.getTypeNames();
118: for (int i = 0; i < typeNames.length; i++) {
119: String typeName = typeNames[i];
120: options.add(typeName);
121: }
122: typeNameSelect = new JComboBox(options);
123: panel.add(typeNameSelect, c);
124:
125: schemaButton = new JButton("Describe Schema");
126: schemaButton.addActionListener(new ActionListener() {
127: public void actionPerformed(ActionEvent e) {
128: displaySchema();
129: }
130: });
131: c.gridwidth = GridBagConstraints.REMAINDER;
132: panel.add(schemaButton, c);
133:
134: c.weightx = 0.0;
135: c.weighty = 0.0;
136: query = new JTextArea(4, 80);
137: c.fill = GridBagConstraints.BOTH;
138: JScrollPane scrollPane1 = new JScrollPane(query);
139: scrollPane1.setPreferredSize(query
140: .getPreferredScrollableViewportSize());
141: scrollPane1.setMinimumSize(query
142: .getPreferredScrollableViewportSize());
143: panel.add(scrollPane1, c);
144:
145: c.fill = GridBagConstraints.NONE;
146: c.weightx = 0.0;
147: c.weighty = 0.0;
148: c.gridwidth = GridBagConstraints.RELATIVE;
149: selectButton = new JButton("Select Features");
150: selectButton.addActionListener(new ActionListener() {
151: public void actionPerformed(ActionEvent e) {
152: selectFeatures();
153: }
154: });
155: panel.add(selectButton, c);
156:
157: c.gridwidth = GridBagConstraints.REMAINDER;
158: filterButton = new JButton("CQL to Filter 1.0");
159: filterButton.addActionListener(new ActionListener() {
160: public void actionPerformed(ActionEvent e) {
161: cqlToFilter();
162: }
163: });
164: panel.add(filterButton, c);
165:
166: c.fill = GridBagConstraints.BOTH;
167: c.weightx = 1.0;
168: c.weighty = 1.0;
169: show = new JTextArea(24, 80);
170: show.setTabSize(2);
171: JScrollPane scrollPane2 = new JScrollPane(show);
172: scrollPane2
173: .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
174: scrollPane2.setPreferredSize(show
175: .getPreferredScrollableViewportSize());
176: // scrollPane2.setMinimumSize(show.getMinimumSize() );
177: panel.add(scrollPane2, c);
178: add(panel);
179:
180: c.weighty = 0.0;
181: c.weightx = 0.0;
182: c.fill = GridBagConstraints.NONE;
183: c.anchor = GridBagConstraints.CENTER;
184: c.gridheight = GridBagConstraints.REMAINDER;
185: c.gridwidth = GridBagConstraints.REMAINDER;
186: closeButton = new JButton("Close");
187: closeButton.addActionListener(new ActionListener() {
188: public void actionPerformed(ActionEvent e) {
189: setVisible(false);
190: }
191: });
192: panel.add(closeButton, c);
193:
194: this .setSize(panel.getSize());
195: this .pack();
196: }
197:
198: private void displaySchema() {
199: try {
200: String typeName = (String) typeNameSelect
201: .getSelectedItem();
202: FeatureType schema = dataStore.getSchema(typeName);
203: display(schema);
204: } catch (Throwable t) {
205: display(t);
206: }
207: }
208:
209: protected void display(Filter filter) throws Exception {
210: FilterTransformer transform = new FilterTransformer();
211: transform.setIndentation(2);
212: String xml = transform.transform(filter);
213:
214: show.setText(xml);
215: }
216:
217: public void display(FeatureType schema) {
218: if (schema == null) {
219: show.setText("");
220: return;
221: }
222: StringBuffer buf = new StringBuffer();
223: buf.append("typeName=");
224: buf.append(schema.getTypeName());
225:
226: buf.append(" namespace=");
227: buf.append(schema.getNamespace());
228: buf.append("attributes = ([\n");
229:
230: for (int index = 0; index < schema.getAttributeCount(); index++) {
231: AttributeType type = schema.getAttributeType(index);
232: buf.append(type.getLocalName());
233: buf.append(" [\n");
234:
235: buf.append("\t binding=");
236: buf.append(type.getBinding());
237: buf.append("\n");
238:
239: buf.append("\t minOccurs=");
240: buf.append(type.getMinOccurs());
241: buf.append(" maxOccurs=");
242: buf.append(type.getMaxOccurs());
243: buf.append(" nillable=");
244: buf.append(type.isNillable());
245: buf.append("\n");
246:
247: buf.append("\t restrictions=");
248: buf.append(type.getRestriction());
249: buf.append("\n");
250:
251: if (type instanceof GeometryAttributeType) {
252: GeometryAttributeType geomType = (GeometryAttributeType) type;
253: buf.append("\t crs=");
254: if (geomType.getCoordinateSystem() == null) {
255: buf.append("null");
256: } else {
257: buf.append(geomType.getCoordinateSystem()
258: .getName());
259: }
260: buf.append("\n");
261: }
262: buf.append("]\n");
263: }
264: buf.append(")");
265: show.setText(buf.toString());
266: }
267:
268: public FeatureCollection filter(String text) throws Exception {
269: Filter filter;
270: filter = CQL.toFilter(text);
271:
272: String typeName = (String) typeNameSelect.getSelectedItem();
273: DefaultQuery query = new DefaultQuery();
274: query.setTypeName(typeName);
275: query.setFilter(filter);
276: query.setMaxFeatures(1000);
277:
278: FeatureSource table = dataStore.getFeatureSource(typeName);
279: return table.getFeatures(query);
280: }
281:
282: protected void display(FeatureCollection features)
283: throws Exception {
284: if (features == null) {
285: show.setText("empty");
286: return;
287: }
288: final FeatureType schema = features.getSchema();
289: final StringBuffer buf = new StringBuffer();
290:
291: buf.append(DataUtilities.spec(schema));
292: buf.append("\n");
293:
294: features.accepts(new FeatureVisitor() {
295: public void visit(Feature feature) {
296: buf.append(feature.getID());
297: buf.append(" [\n");
298: for (int index = 0; index < schema
299: .getAttributeCount(); index++) {
300: AttributeType type = schema
301: .getAttributeType(index);
302: String name = type.getLocalName();
303: buf.append("\t");
304: buf.append(name);
305: buf.append("=");
306: buf.append(feature.getAttribute(name));
307: }
308: buf.append("]");
309: }
310: }, null);
311: show.setText(buf.toString());
312: }
313:
314: public void display(Throwable t) {
315: show.setText(t.getLocalizedMessage());
316: show.setForeground(Color.RED);
317: }
318:
319: private void selectFeatures() {
320: try {
321: String text = query.getText();
322: FeatureCollection features = filter(text);
323: display(features);
324: } catch (Throwable t) {
325: display(t);
326: }
327: }
328:
329: private void cqlToFilter() {
330: try {
331: String text = query.getText();
332: Filter filter = CQL.toFilter(text);
333:
334: display(filter);
335: } catch (Throwable t) {
336: display(t);
337: }
338: }
339: }
340: }
|