001: package org.geotools.demo.mappane;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Container;
005: import java.awt.Dimension;
006: import java.awt.Frame;
007: import java.awt.GraphicsConfiguration;
008: import java.awt.HeadlessException;
009: import java.awt.event.ActionEvent;
010: import java.awt.event.ActionListener;
011: import java.awt.event.MouseAdapter;
012: import java.awt.event.MouseEvent;
013: import java.io.IOException;
014: import java.net.MalformedURLException;
015: import java.net.URL;
016: import java.util.ArrayList;
017: import java.util.Iterator;
018:
019: import javax.swing.BorderFactory;
020: import javax.swing.Box;
021: import javax.swing.BoxLayout;
022: import javax.swing.JButton;
023: import javax.swing.JDialog;
024: import javax.swing.JLabel;
025: import javax.swing.JList;
026: import javax.swing.JPanel;
027: import javax.swing.JScrollPane;
028: import javax.swing.ListSelectionModel;
029:
030: import org.geotools.data.ows.Layer;
031: import org.geotools.data.ows.WMSCapabilities;
032: import org.geotools.data.wms.WebMapServer;
033: import org.geotools.ows.ServiceException;
034:
035: public class WMSChooser extends JDialog implements ActionListener {
036: private String server = "http://localhost:8080/geoserver/wms";
037: private String request = "?REQUEST=GetCapabilities";
038: URL url;
039: WebMapServer wms;
040: WMSCapabilities caps;
041: JList list;
042: ArrayList layers = new ArrayList();
043: ArrayList layerNames = new ArrayList();
044:
045: public WMSChooser() throws HeadlessException {
046: super ();
047: init();
048: }
049:
050: public WMSChooser(Frame owner, boolean modal)
051: throws HeadlessException {
052: super (owner, modal);
053: init();
054: }
055:
056: public WMSChooser(Frame owner, String title, boolean modal,
057: GraphicsConfiguration gc) {
058: super (owner, title, modal, gc);
059: init();
060: }
061:
062: public WMSChooser(Frame owner, String title, boolean modal)
063: throws HeadlessException {
064: super (owner, title, modal);
065: init();
066: }
067:
068: public WMSChooser(Frame owner, String title)
069: throws HeadlessException {
070: super (owner, title);
071: init();
072: }
073:
074: public WMSChooser(Frame owner) throws HeadlessException {
075: super (owner);
076: init();
077: }
078:
079: private void init() {
080: try {
081: this .setSize(400, 200);
082: url = new URL(server + request);
083: wms = new WebMapServer(url);
084:
085: setupLayersList();
086: // Create and initialize the buttons.
087: JButton cancelButton = new JButton("Cancel");
088: cancelButton.addActionListener(this );
089: //
090: final JButton setButton = new JButton("Set");
091: setButton.setActionCommand("Set");
092: setButton.addActionListener(this );
093: getRootPane().setDefaultButton(setButton);
094: list = new JList(layerNames.toArray());
095: list
096: .setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
097: list.setLayoutOrientation(JList.VERTICAL);
098: list.setVisibleRowCount(-1);
099: list.addMouseListener(new MouseAdapter() {
100: public void mouseClicked(MouseEvent e) {
101: if (e.getClickCount() == 2) {
102: setButton.doClick(); //emulate button click
103: }
104: }
105: });
106: JScrollPane listScroller = new JScrollPane(list);
107: listScroller.setPreferredSize(new Dimension(400, 280));
108: JPanel listPane = new JPanel();
109: listPane.setLayout(new BoxLayout(listPane,
110: BoxLayout.PAGE_AXIS));
111: JLabel label = new JLabel("Layers");
112: label.setLabelFor(list);
113: listPane.add(label);
114: listPane.add(Box.createRigidArea(new Dimension(0, 5)));
115: listPane.add(listScroller);
116: listPane.setBorder(BorderFactory.createEmptyBorder(10, 10,
117: 10, 10));
118:
119: //Lay out the buttons from left to right.
120: JPanel buttonPane = new JPanel();
121: buttonPane.setLayout(new BoxLayout(buttonPane,
122: BoxLayout.LINE_AXIS));
123: buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10,
124: 10, 10));
125: buttonPane.add(Box.createHorizontalGlue());
126: buttonPane.add(cancelButton);
127: buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
128: buttonPane.add(setButton);
129:
130: //Put everything together, using the content pane's BorderLayout.
131: Container contentPane = getContentPane();
132: contentPane.add(listPane, BorderLayout.CENTER);
133: contentPane.add(buttonPane, BorderLayout.PAGE_END);
134:
135: } catch (MalformedURLException e) {
136: // TODO Auto-generated catch block
137: e.printStackTrace();
138: } catch (ServiceException e) {
139: // TODO Auto-generated catch block
140: e.printStackTrace();
141: } catch (IOException e) {
142: // TODO Auto-generated catch block
143: e.printStackTrace();
144: }
145:
146: }
147:
148: private void setupLayersList() {
149: caps = wms.getCapabilities();
150: layers.clear();
151: layerNames.clear();
152: for (Iterator i = caps.getLayerList().iterator(); i.hasNext();) {
153: Layer layer = (Layer) i.next();
154: layerNames.add(layer.getTitle());
155: layers.add(layer);
156:
157: }
158: }
159:
160: public int getLayer() {
161: // TODO Auto-generated method stub
162: return list.getSelectedIndex();
163: }
164:
165: public void actionPerformed(ActionEvent e) {
166: if (e.getActionCommand().equalsIgnoreCase("cancel")) {
167: list.clearSelection();
168: }
169: this .setVisible(false);
170: }
171:
172: public ArrayList getLayers() {
173: return layers;
174: }
175:
176: public WebMapServer getWms() {
177: return wms;
178: }
179:
180: public void setWms(WebMapServer wms) {
181: this .wms = wms;
182: setupLayersList();
183: }
184:
185: public String getServer() {
186: return server;
187: }
188:
189: public void setServer(String server) {
190: this .server = server;
191: System.out.println("setting server to " + server);
192: try {
193: url = new URL(server + request);
194: System.out.println("url " + url.toString());
195: wms = new WebMapServer(url);
196: } catch (MalformedURLException e) {
197: // TODO Auto-generated catch block
198: e.printStackTrace();
199: } catch (ServiceException e) {
200: // TODO Auto-generated catch block
201: e.printStackTrace();
202: } catch (IOException e) {
203: // TODO Auto-generated catch block
204: e.printStackTrace();
205: }
206:
207: setupLayersList();
208:
209: }
210: }
|