001:
002: /*
003: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
004: * for visualizing and manipulating spatial features with geometry and attributes.
005: *
006: * Copyright (C) 2003 Vivid Solutions
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: *
022: * For more information, contact:
023: *
024: * Vivid Solutions
025: * Suite #1A
026: * 2328 Government Street
027: * Victoria BC V8T 5G5
028: * Canada
029: *
030: * (250)385-6040
031: * www.vividsolutions.com
032: */
033:
034: package com.vividsolutions.wms.ui;
035:
036: import java.awt.*;
037: import java.awt.event.*;
038: import java.io.IOException;
039: import java.util.Arrays;
040: import java.util.Iterator;
041:
042: import javax.swing.*;
043:
044: import com.vividsolutions.jump.I18N;
045: import com.vividsolutions.wms.*;
046:
047: /**
048: * The executable WMS Viewer.
049: */
050: public class WMSViewer extends JFrame implements ActionListener,
051: MouseListener {
052:
053: private JTextField serverUrlField;
054: private JButton connectButton;
055: private JButton disconnectButton;
056: private JList layerList;
057: private JTextField xMinField;
058: private JTextField yMinField;
059: private JTextField xMaxField;
060: private JTextField yMaxField;
061: private JComboBox srsCombo;
062: private JButton getImageButton;
063: private JComboBox formatCombo;
064: private ImageCanvas canvas;
065: private JLabel mapLabel;
066: private String zoomMode;
067: private WMService service;
068: private boolean connected;
069:
070: /**
071: * Constructs a WMSViewer object.
072: */
073: public WMSViewer() {
074: super (I18N
075: .get("com.vividsolutions.wms.ui.WMSViewer.wms-viewer"));
076: this .addWindowListener(new WindowAdapter() {
077: public void windowClosing(WindowEvent e) {
078: System.exit(0);
079: }
080: });
081:
082: getContentPane().setLayout(new BorderLayout());
083:
084: // create and layout the top panel (server connect)
085: JPanel topPanel = new JPanel();
086: topPanel.setLayout(new FlowLayout());
087: topPanel
088: .add(new JLabel(
089: I18N
090: .get("com.vividsolutions.wms.ui.WMSViewer.server-string")));
091: serverUrlField = new JTextField(I18N
092: .get("com.vividsolutions.wms.ui.WMSViewer.wms-url"), 30);
093: topPanel.add(serverUrlField);
094: connectButton = new JButton(I18N
095: .get("com.vividsolutions.wms.ui.WMSViewer.connect"));
096: connectButton.setActionCommand("connect");
097: connectButton.addActionListener(this );
098: topPanel.add(connectButton);
099: disconnectButton = new JButton(I18N
100: .get("com.vividsolutions.wms.ui.WMSViewer.disconnect"));
101: disconnectButton.setActionCommand("disconnect");
102: disconnectButton.addActionListener(this );
103: disconnectButton.setEnabled(false);
104: topPanel.add(disconnectButton);
105: getContentPane().add(topPanel, BorderLayout.NORTH);
106:
107: // create and layout the left side panel (layer list)
108: layerList = new JList();
109: layerList
110: .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
111: getContentPane().add(new JScrollPane(layerList),
112: BorderLayout.WEST);
113:
114: // create and layout the right side panel (bounding box, pan and zoom controls)
115: JPanel rightPanel = new JPanel();
116: rightPanel.setLayout(new BorderLayout());
117: JPanel thePanel = new JPanel();
118: thePanel.setLayout(new GridLayout(5, 2));
119: thePanel.add(new JLabel("X-Min:"));
120: xMinField = new JTextField("1375470", 7);
121: thePanel.add(xMinField);
122: thePanel.add(new JLabel("Y-Min:"));
123: yMinField = new JTextField("701069", 7);
124: thePanel.add(yMinField);
125: thePanel.add(new JLabel("X-Max:"));
126: xMaxField = new JTextField("1401720", 7);
127: thePanel.add(xMaxField);
128: thePanel.add(new JLabel("Y-Max:"));
129: yMaxField = new JTextField("714194", 7);
130: thePanel.add(yMaxField);
131: thePanel.add(new JLabel("SRS:"));
132: srsCombo = new JComboBox();
133: thePanel.add(srsCombo);
134: rightPanel.add(thePanel, BorderLayout.NORTH);
135: thePanel = new JPanel();
136: thePanel.setLayout(new GridLayout(3, 1));
137: ButtonGroup bg = new ButtonGroup();
138: JRadioButton rb = new JRadioButton(I18N
139: .get("com.vividsolutions.wms.ui.WMSViewer.pan"));
140: rb.setActionCommand("pan");
141: rb.addActionListener(this );
142: rb.setSelected(true);
143: zoomMode = "pan";
144: thePanel.add(rb);
145: bg.add(rb);
146: rb = new JRadioButton(I18N
147: .get("com.vividsolutions.wms.ui.WMSViewer.zoom-in"));
148: rb.setActionCommand("zoomIn");
149: rb.addActionListener(this );
150: thePanel.add(rb);
151: bg.add(rb);
152: rb = new JRadioButton(I18N
153: .get("com.vividsolutions.wms.ui.WMSViewer.zoom-out"));
154: rb.setActionCommand("zoomOut");
155: rb.addActionListener(this );
156: thePanel.add(rb);
157: bg.add(rb);
158: rightPanel.add(thePanel, BorderLayout.SOUTH);
159: getContentPane().add(rightPanel, BorderLayout.EAST);
160:
161: // create and layout the bottom panel (get image control)
162: JPanel bottomPanel = new JPanel();
163: bottomPanel.setLayout(new FlowLayout());
164: formatCombo = new JComboBox();
165: bottomPanel.add(formatCombo);
166: getImageButton = new JButton(I18N
167: .get("com.vividsolutions.wms.ui.WMSViewer.get-image"));
168: getImageButton.setActionCommand("getImage");
169: getImageButton.addActionListener(this );
170: getImageButton.setEnabled(false);
171: bottomPanel.add(getImageButton);
172: getContentPane().add(bottomPanel, BorderLayout.SOUTH);
173:
174: // create the center panel
175: canvas = new ImageCanvas();
176: canvas.addMouseListener(this );
177: getContentPane().add(canvas, BorderLayout.CENTER);
178:
179: this .pack();
180: this .setVisible(true);
181: }
182:
183: /**
184: * Processes actions from the buttons on the interface
185: * @param actionEvent the event to process
186: */
187: public void actionPerformed(java.awt.event.ActionEvent actionEvent) {
188: String command = actionEvent.getActionCommand();
189: if (command.equals("connect")) {
190: try {
191: // service = new WMService( "http://hydra/mapserv/mapserv?map=/home/chodgson/www/demo/demo.map&" );
192: // service = new WMService( "http://slkapps2.env.gov.bc.ca/servlet/com.esri.wms.Esrimap?" );
193: service = new WMService(serverUrlField.getText());
194: service.initialize();
195: // DEBUG: System.out.print( serv.getTopLayer().toString() );
196:
197: // setup the layerList
198: Iterator it = service.getCapabilities().getTopLayer()
199: .getLayerList().iterator();
200: DefaultListModel lm = new DefaultListModel();
201: while (it.hasNext()) {
202: lm.addElement(((MapLayer) it.next()).getName());
203: }
204: layerList.setModel(lm);
205:
206: // setup the srsCombo
207: it = service.getCapabilities().getTopLayer()
208: .getSRSList().iterator();
209: DefaultComboBoxModel cm = new DefaultComboBoxModel();
210: while (it.hasNext()) {
211: cm.addElement(it.next());
212: }
213: srsCombo.setModel(cm);
214:
215: // setup the formatCombo
216: String[] formats = service.getCapabilities()
217: .getMapFormats();
218: cm = new DefaultComboBoxModel();
219: for (int i = 0; i < formats.length; i++) {
220: cm.addElement(formats[i]);
221: }
222: formatCombo.setModel(cm);
223:
224: connectButton.setEnabled(false);
225: disconnectButton.setEnabled(true);
226: getImageButton.setEnabled(true);
227: connected = true;
228: } catch (IOException ioe) {
229: // failed to connect and retrieve capabilities
230:
231: }
232: } else if (command.equals("disconnect")) {
233: layerList.setModel(new DefaultListModel());
234: srsCombo.setModel(new DefaultComboBoxModel());
235: formatCombo.setModel(new DefaultComboBoxModel());
236: getImageButton.setEnabled(false);
237: disconnectButton.setEnabled(false);
238: connectButton.setEnabled(true);
239: canvas.setImage(null);
240: } else if (command.equals("getImage")) {
241: MapRequest req = service.createMapRequest();
242: req.setImageSize(canvas.getWidth(), canvas.getHeight());
243: req.setFormat((String) formatCombo.getSelectedItem());
244: req.setLayers(Arrays.asList(layerList.getSelectedValues()));
245: // req.setBoundingBox( new BoundingBox( "EPSG:42102", 1100000, 430000, 1120000, 450000 ) );
246: req.setBoundingBox(new BoundingBox((String) srsCombo
247: .getSelectedItem(), Float.parseFloat(xMinField
248: .getText()), Float.parseFloat(yMinField.getText()),
249: Float.parseFloat(xMaxField.getText()), Float
250: .parseFloat(yMaxField.getText())));
251: Image mapImage;
252: try {
253: System.out.println("Url: " + req.getURL());
254: mapImage = req.getImage();
255: } catch (Exception e) {
256: e.printStackTrace(System.err);
257: return;
258: }
259: canvas.setImage(mapImage);
260: canvas.repaint();
261: } else if (command.equals("pan")) {
262: zoomMode = "pan";
263: } else if (command.equals("zoomIn")) {
264: zoomMode = "zoomIn";
265: } else if (command.equals("zoomOut")) {
266: zoomMode = "zoomOut";
267: }
268: }
269:
270: public void mouseClicked(java.awt.event.MouseEvent mouseEvent) {
271: if (mouseEvent.getComponent() == canvas) {
272: System.out.println("click");
273: int x = mouseEvent.getX();
274: int y = mouseEvent.getY();
275: float xMin = Float.parseFloat(xMinField.getText());
276: float yMin = Float.parseFloat(yMinField.getText());
277: float xMax = Float.parseFloat(xMaxField.getText());
278: float yMax = Float.parseFloat(yMaxField.getText());
279: float mapWidth = xMax - xMin;
280: float mapHeight = yMax - yMin;
281: int imgWidth = canvas.getWidth();
282: int imgHeight = canvas.getHeight();
283: float xCenter = xMin
284: + ((((float) x) / (float) imgWidth) * mapWidth);
285: float yCenter = yMax
286: - ((((float) y) / (float) imgHeight) * mapHeight);
287: if (zoomMode.equals("zoomIn")) {
288: mapWidth /= 2;
289: mapHeight /= 2;
290: } else if (zoomMode.equals("zoomOut")) {
291: mapWidth *= 2;
292: mapHeight *= 2;
293: }
294: xMin = xCenter - mapWidth / 2;
295: yMin = yCenter - mapHeight / 2;
296: xMax = xCenter + mapWidth / 2;
297: yMax = yCenter + mapHeight / 2;
298: xMinField.setText("" + xMin);
299: yMinField.setText("" + yMin);
300: xMaxField.setText("" + xMax);
301: yMaxField.setText("" + yMax);
302:
303: getImageButton.doClick();
304: }
305: }
306:
307: public void mouseEntered(java.awt.event.MouseEvent mouseEvent) {
308: }
309:
310: public void mouseExited(java.awt.event.MouseEvent mouseEvent) {
311: }
312:
313: public void mousePressed(java.awt.event.MouseEvent mouseEvent) {
314: }
315:
316: public void mouseReleased(java.awt.event.MouseEvent mouseEvent) {
317: }
318:
319: /**
320: * Runs the viewer aplication
321: */
322: public static void main(String args[]) {
323: WMSViewer viewer = new WMSViewer();
324: }
325:
326: }
|