001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2006, Adrian Custer, assigned to the PMC.
006: *
007: * This file is hereby placed into the Public Domain. This means anyone is
008: * free to do whatever they wish with this file. Use it well and enjoy!
009: */
010:
011: package org.geotools.demo.introduction;
012:
013: import java.awt.BorderLayout;
014: import java.awt.Color;
015: import java.awt.Container;
016: import java.awt.Dimension;
017: import java.awt.Panel;
018: import java.awt.ScrollPane;
019: import java.awt.event.ActionEvent;
020: import java.awt.event.ActionListener;
021:
022: import javax.swing.Action;
023: import javax.swing.Box;
024: import javax.swing.BoxLayout;
025: import javax.swing.ImageIcon;
026: import javax.swing.JButton;
027: import javax.swing.JFrame;
028: import javax.swing.JLabel;
029: import javax.swing.JOptionPane;
030: import javax.swing.JPanel;
031: import javax.swing.JTextArea;
032: import javax.swing.JToolBar;
033: import javax.swing.WindowConstants;
034:
035: import org.geotools.gui.swing.JMapPane;
036: import org.geotools.gui.swing.PanAction;
037: import org.geotools.gui.swing.ResetAction;
038: import org.geotools.gui.swing.SelectAction;
039: import org.geotools.gui.swing.ZoomInAction;
040: import org.geotools.gui.swing.ZoomOutAction;
041: import org.geotools.map.DefaultMapContext;
042: import org.geotools.map.MapContext;
043: import org.geotools.referencing.CRS;
044: import org.geotools.referencing.crs.DefaultGeographicCRS;
045: import org.geotools.renderer.GTRenderer;
046: import org.geotools.renderer.lite.StreamingRenderer;
047: import org.opengis.referencing.FactoryException;
048: import org.opengis.referencing.crs.CoordinateReferenceSystem;
049:
050: /**
051: * The DemoGUI class which produces the top level JFrame for the introductory
052: * demo application.
053: *
054: * @author Adrian Custer
055: *
056: * @version 0.03
057: * @since 2.3-M0
058: *
059: */
060: public class DemoGUI {
061:
062: /* A link to the DemoBase instance, to pass control back to the 'button*'
063: * methods in that instance. */
064: final DemoBase demoBase;
065:
066: /* GUI frame, pane and extras */
067: final JFrame frame;
068: JPanel visPanel;
069: ScrollPane infoSP;
070: JToolBar jtb;
071: JLabel text;
072: JButton quitButton;
073: JButton createButton;
074: JButton styleButton;
075: JButton renderButton;
076: JButton projectButton;
077: JButton filterButton;
078: JButton captureButton;
079: JButton saveButton;
080: JButton commitButton;
081: JButton analyzeButton;
082: JTextArea textArea;
083:
084: /* Display elements */
085: JMapPane jmp;
086: MapContext context;
087: GTRenderer renderer;
088:
089: com.vividsolutions.jts.geom.Envelope worldbounds;
090:
091: /*
092: * Create the Demo's GUI.
093: *
094: * Geotools users can skip this swing code, go to create_FeatureSource...
095: *
096: */
097: public DemoGUI(DemoBase db) {
098:
099: this .demoBase = db;
100:
101: frame = new JFrame("Geotools Demo");
102: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
103: frame.setBounds(20, 20, 800, 500);
104: // frame.setBackground(Color.cyan);
105:
106: Container contentPane = frame.getContentPane();
107: BoxLayout layout = new BoxLayout(contentPane, BoxLayout.X_AXIS);
108: contentPane.setLayout(layout);
109: // contentPane.setBackground(Color.red);
110:
111: JPanel buttonPanel = new JPanel();
112: // buttonPanel.setBackground(Color.blue);
113: buttonPanel.setVisible(true);
114: visPanel = new JPanel();
115: // visPanel.setBackground(Color.gray);
116: visPanel.setVisible(true);
117:
118: contentPane.add(Box.createRigidArea(new Dimension(10, 0)));
119: contentPane.add(buttonPanel);
120: contentPane.add(Box.createRigidArea(new Dimension(10, 0)));
121: contentPane.add(visPanel);
122: contentPane.add(Box.createRigidArea(new Dimension(10, 0)));
123:
124: /* The action button Panel */
125: JPanel actionButtonPanel = new JPanel();
126: // actionButtonPanel.setBackground(Color.green);
127: actionButtonPanel.setVisible(true);
128:
129: BoxLayout aBPlayout = new BoxLayout(actionButtonPanel,
130: BoxLayout.Y_AXIS);
131: actionButtonPanel.setLayout(aBPlayout);
132:
133: int BUTTON_WIDTH = 100;
134: createButton = new JButton("1. Create Features");
135: createButton.setMinimumSize(new Dimension(BUTTON_WIDTH, 1));
136: actionButtonPanel.add(createButton);
137: actionButtonPanel.add(Box.createRigidArea(new Dimension(0, 6)));
138: styleButton = new JButton("2. Style Features");
139: styleButton.setEnabled(false);
140: styleButton.setMinimumSize(new Dimension(BUTTON_WIDTH, 1));
141: actionButtonPanel.add(styleButton);
142: actionButtonPanel.add(Box.createRigidArea(new Dimension(0, 6)));
143: renderButton = new JButton("3. Render Map");
144: renderButton.setEnabled(false);
145: renderButton.setMinimumSize(new Dimension(BUTTON_WIDTH, 1));
146: actionButtonPanel.add(renderButton);
147: actionButtonPanel.add(Box.createRigidArea(new Dimension(0, 6)));
148: projectButton = new JButton("4. Project Map");
149: projectButton.setEnabled(false);
150: projectButton.setMinimumSize(new Dimension(BUTTON_WIDTH, 1));
151: actionButtonPanel.add(projectButton);
152: actionButtonPanel.add(Box.createRigidArea(new Dimension(0, 6)));
153: filterButton = new JButton("5. Filter Features");
154: filterButton.setEnabled(false);
155: filterButton.setMinimumSize(new Dimension(BUTTON_WIDTH, 1));
156: actionButtonPanel.add(filterButton);
157: actionButtonPanel.add(Box.createRigidArea(new Dimension(0, 6)));
158: captureButton = new JButton("6. Capture Image");
159: captureButton.setEnabled(false);
160: captureButton.setMinimumSize(new Dimension(BUTTON_WIDTH, 1));
161: actionButtonPanel.add(captureButton);
162: actionButtonPanel.add(Box.createRigidArea(new Dimension(0, 6)));
163: saveButton = new JButton("7. Save to file");
164: saveButton.setEnabled(false);
165: saveButton.setMinimumSize(new Dimension(BUTTON_WIDTH, 1));
166: actionButtonPanel.add(saveButton);
167: actionButtonPanel.add(Box.createRigidArea(new Dimension(0, 6)));
168: commitButton = new JButton("8. Commit to WFS");
169: commitButton.setEnabled(false);
170: commitButton.setMinimumSize(new Dimension(BUTTON_WIDTH, 1));
171: actionButtonPanel.add(commitButton);
172: actionButtonPanel.add(Box.createRigidArea(new Dimension(0, 6)));
173: analyzeButton = new JButton("9. Analyze network");
174: analyzeButton.setEnabled(false);
175: analyzeButton.setMinimumSize(new Dimension(BUTTON_WIDTH, 1));
176: actionButtonPanel.add(analyzeButton);
177:
178: /* The button Panel */
179: BoxLayout buttonPanelBoxLayout = new BoxLayout(buttonPanel,
180: BoxLayout.Y_AXIS);
181: buttonPanel.setLayout(buttonPanelBoxLayout);
182: buttonPanel.add(Box.createRigidArea(new Dimension(0, 10)));
183: //TODO: verify the file can be found
184: java.net.URL imgURL = DemoGUI.class
185: .getResource("/GeotoolsBoxLogo.png");
186: // System.out.println(imgURL);
187: ImageIcon icon = new ImageIcon(imgURL, "The Geotools Logo");
188: JLabel iconLabel = new JLabel(icon);
189: buttonPanel.add(iconLabel);
190: buttonPanel.add(Box.createRigidArea(new Dimension(0, 20)));
191: buttonPanel.add(actionButtonPanel);
192: buttonPanel.add(Box.createVerticalGlue());
193: JButton quitButton = new JButton("QUIT");
194: buttonPanel.add(quitButton);
195: buttonPanel.add(Box.createRigidArea(new Dimension(0, 10)));
196:
197: quitButton.addActionListener(new ActionListener() {
198: public void actionPerformed(ActionEvent e) {
199:
200: frame.dispose();
201:
202: }
203: });
204:
205: createButton.addActionListener(new ActionListener() {
206: public void actionPerformed(ActionEvent e) {
207:
208: createButton.setEnabled(false);
209: demoBase.buttonCreateFeatures();
210: styleButton.setEnabled(true);
211: }
212: });
213: styleButton.addActionListener(new ActionListener() {
214: public void actionPerformed(ActionEvent e) {
215:
216: styleButton.setEnabled(false);
217: demoBase.buttonCreateStyles();
218: renderButton.setEnabled(true);
219: }
220: });
221: renderButton.addActionListener(new ActionListener() {
222: public void actionPerformed(ActionEvent e) {
223:
224: renderButton.setEnabled(false);
225: demoBase.buttonCreateMap();
226: projectButton.setEnabled(true);
227: }
228: });
229: projectButton.addActionListener(new ActionListener() {
230: public void actionPerformed(ActionEvent e) {
231:
232: projectButton.setEnabled(false);
233: demoBase.buttonProjectMap();
234: //DemoApp.filterButton.setEnabled(true);
235:
236: }
237: });
238: filterButton.addActionListener(new ActionListener() {
239: public void actionPerformed(ActionEvent e) {
240:
241: filterButton.setEnabled(false);
242:
243: captureButton.setEnabled(true);
244: }
245: });
246: captureButton.addActionListener(new ActionListener() {
247: public void actionPerformed(ActionEvent e) {
248:
249: captureButton.setEnabled(false);
250: // DemoBase.captureImage();
251: saveButton.setEnabled(true);
252: }
253: });
254: saveButton.addActionListener(new ActionListener() {
255: public void actionPerformed(ActionEvent e) {
256:
257: saveButton.setEnabled(false);
258:
259: commitButton.setEnabled(true);
260:
261: }
262: });
263: commitButton.addActionListener(new ActionListener() {
264: public void actionPerformed(ActionEvent e) {
265:
266: commitButton.setEnabled(false);
267:
268: analyzeButton.setEnabled(true);
269:
270: }
271: });
272: analyzeButton.addActionListener(new ActionListener() {
273: public void actionPerformed(ActionEvent e) {
274:
275: analyzeButton.setEnabled(false);
276:
277: }
278: });
279:
280: /* The info Text Area */
281: textArea = new JTextArea();
282: textArea.append("Welcome to the Geotools Demo.\n\n");
283: textArea.append("Click on the \"Create\" button to start.\n\n");
284: infoSP = new ScrollPane();
285: infoSP.add(textArea);
286:
287: //TOOD: use a Logger to output to the textArea. Not sure how to do this
288: //without using classes: annonymous inner class?
289: // OutputStream os = new anOutputStream() extends OutputStream {
290: // public void write( int b ) throws IOException {
291: // // append the data as characters to the JTextArea control
292: // DemoApp.textArea.append( String.valueOf( ( char )b ) );
293: // }
294: // };
295: // StreamHandler sh = new StreamHandler(os , new Formatter());
296:
297: /* The visuals Panel */
298: BoxLayout visPanelBoxLayout = new BoxLayout(visPanel,
299: BoxLayout.Y_AXIS);
300: visPanel.setLayout(visPanelBoxLayout);
301: visPanel.add(Box.createRigidArea(new Dimension(0, 10)));
302: visPanel.add(infoSP);
303: visPanel.add(Box.createRigidArea(new Dimension(0, 10)));
304:
305: contentPane.setVisible(true);
306: contentPane.doLayout();
307: frame.doLayout();
308: frame.setVisible(true);
309: frame.repaint();
310:
311: }
312:
313: /*
314: * Create a GUI map displayer.
315: *
316: * This is all Swing stuff for the JMapPane.
317: *
318: */
319: public void initialize_JMapPane() {
320: textArea.append("Start: Initialize the GUI.\n");
321:
322: Panel mapGUI = new Panel();
323: mapGUI.setLayout(new BorderLayout());
324: jmp = new JMapPane();
325: jmp.setBackground(Color.white);
326:
327: /* Renderer */
328: renderer = new StreamingRenderer();
329:
330: /* Context */
331: context = new DefaultMapContext(DefaultGeographicCRS.WGS84);
332: context.setAreaOfInterest(demoBase.envlp_NoEdges2);
333:
334: /* Add to JMapPane */
335: jmp.setRenderer(renderer);
336: jmp.setContext(context);
337:
338: /* The toolbar */
339: jtb = new JToolBar();
340: Action zoomIn = new ZoomInAction(jmp);
341: Action zoomOut = new ZoomOutAction(jmp);
342: Action pan = new PanAction(jmp);
343: Action select = new SelectAction(jmp);
344: Action reset = new ResetAction(jmp);
345: jtb.add(zoomIn);
346: jtb.add(zoomOut);
347: jtb.add(pan);
348: jtb.addSeparator();
349: jtb.add(reset);
350: jtb.addSeparator();
351: jtb.add(select);
352: final JButton button = new JButton();
353: button.setText("CRS");
354: button.setToolTipText("Change map prjection");
355: button.addActionListener(new ActionListener() {
356: public void actionPerformed(ActionEvent e) {
357:
358: String code = JOptionPane.showInputDialog(button,
359: "Coordinate Reference System:", "EPSG:4326");
360: try {
361: CoordinateReferenceSystem crs = CRS.decode(code);
362: jmp.getContext().setAreaOfInterest(
363: jmp.getContext().getAreaOfInterest(), crs);
364: jmp.setReset(true);
365: jmp.repaint();
366:
367: } catch (FactoryException fe) {
368: JOptionPane.showMessageDialog(button, fe
369: .getMessage(), fe.getClass().toString(),
370: JOptionPane.ERROR_MESSAGE);
371: return;
372: }
373: }
374: });
375: jtb.add(button);
376: mapGUI.add(jtb, BorderLayout.NORTH);
377: mapGUI.add(jmp);
378:
379: infoSP.setSize(new Dimension(300, 60));
380: BoxLayout visPanelBoxLayout = new BoxLayout(visPanel,
381: BoxLayout.Y_AXIS);
382: visPanel.setLayout(visPanelBoxLayout);
383: visPanel.add(Box.createRigidArea(new Dimension(0, 10)));
384: visPanel.add(infoSP);
385: visPanel.add(Box.createRigidArea(new Dimension(0, 10)));
386: visPanel.add(mapGUI);
387: visPanel.add(Box.createRigidArea(new Dimension(0, 10)));
388: frame.getContentPane().doLayout();
389: infoSP.setSize(new Dimension(3, 3));
390: frame.getContentPane().doLayout();
391: frame.setVisible(true);
392:
393: textArea.append(" End: Initialized the GUI.\n");
394:
395: }
396:
397: }
|