001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.workbench.ui.plugin.wms;
034:
035: import java.awt.Component;
036: import java.awt.Dimension;
037: import java.awt.GridBagConstraints;
038: import java.awt.GridBagLayout;
039: import java.awt.Insets;
040: import java.awt.event.ActionEvent;
041: import java.awt.event.ActionListener;
042: import java.awt.event.MouseAdapter;
043: import java.awt.event.MouseEvent;
044: import java.io.IOException;
045: import java.util.LinkedList;
046: import java.util.Map;
047:
048: import javax.swing.ButtonGroup;
049: import javax.swing.JCheckBox;
050: import javax.swing.JComboBox;
051: import javax.swing.JLabel;
052: import javax.swing.JPanel;
053: import javax.swing.JRadioButton;
054: import javax.swing.SwingUtilities;
055:
056: import com.vividsolutions.jump.I18N;
057: import com.vividsolutions.jump.workbench.WorkbenchException;
058: import com.vividsolutions.jump.workbench.ui.InputChangedFirer;
059: import com.vividsolutions.jump.workbench.ui.InputChangedListener;
060: import com.vividsolutions.jump.workbench.ui.wizard.WizardPanel;
061: import com.vividsolutions.wms.MapImageFormatChooser;
062: import com.vividsolutions.wms.WMService;
063:
064: public class URLWizardPanel extends JPanel implements WizardPanel {
065: public static final String SERVICE_KEY = "SERVICE";
066: public static final String FORMAT_KEY = "FORMAT";
067: public static final String URL_KEY = "URL";
068: private InputChangedFirer inputChangedFirer = new InputChangedFirer();
069: private Map dataMap;
070: private GridBagLayout gridBagLayout1 = new GridBagLayout();
071: private JLabel urlLabel = new JLabel();
072: private JComboBox urls;
073: private JPanel fillerPanel = new JPanel();
074: // [UT]
075: public static final String VERSION_KEY = "WMS_VERSION";
076: private String wmsVersion = WMService.WMS_1_1_1;
077: private boolean lossyPreferred = true;
078:
079: public URLWizardPanel(String[] initialURLs, String wmsVersion) {
080: try {
081: this .wmsVersion = wmsVersion;
082: urls = new JComboBox(initialURLs);
083: urls.setEditable(true);
084: urls.getEditor().selectAll();
085: jbInit();
086: } catch (Exception ex) {
087: ex.printStackTrace();
088: }
089: }
090:
091: public void add(InputChangedListener listener) {
092: inputChangedFirer.add(listener);
093: }
094:
095: public void remove(InputChangedListener listener) {
096: inputChangedFirer.remove(listener);
097: }
098:
099: void jbInit() throws Exception {
100: urlLabel.setText("URL:");
101: this .setLayout(gridBagLayout1);
102: urls.setPreferredSize(new Dimension(300, 21));
103: urlLabel.addMouseListener(new MouseAdapter() {
104: public void mouseClicked(MouseEvent e) {
105: if (SwingUtilities.isRightMouseButton(e)
106: && e.getClickCount() == 3) {
107: urls
108: .setSelectedItem("http://libcwms.gov.bc.ca/wmsconnector/com.esri.wsit.WMSServlet/ogc_layer_service");
109: }
110: super .mouseClicked(e);
111: }
112: });
113: this .add(urlLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
114: GridBagConstraints.CENTER, GridBagConstraints.NONE,
115: new Insets(0, 0, 0, 4), 0, 0));
116: this .add(urls, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
117: GridBagConstraints.CENTER,
118: GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 4),
119: 0, 0));
120: this .add(fillerPanel, new GridBagConstraints(2, 10, 1, 1, 1.0,
121: 1.0, GridBagConstraints.CENTER,
122: GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
123: // [UT]
124: this .add(createVersionButtons(new String[] {
125: WMService.WMS_1_0_0, WMService.WMS_1_1_0,
126: WMService.WMS_1_1_1 }), new GridBagConstraints(1, 3, 1,
127: 1, 0.0, 0.0, GridBagConstraints.CENTER,
128: GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
129:
130: //[UT] 20.10.2005 not added yet; need more testing
131: /*
132: this.add(createLossyCheckBox(),
133: new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0,
134: GridBagConstraints.CENTER, GridBagConstraints.BOTH,
135: new Insets(0, 0, 0, 0), 0, 0));
136: */
137:
138: }
139:
140: public String getInstructions() {
141: return I18N
142: .get("ui.plugin.wms.URLWizardPanel.please-enter-the-url-of-the-wms-server");
143: }
144:
145: //
146: // The WMService appends other parameters to the end of the URL
147: //
148: private String fixUrlForWMService(String url) {
149: String fixedURL = url.trim();
150:
151: if (fixedURL.indexOf("?") == -1) {
152: fixedURL = fixedURL + "?";
153: } else {
154: if (fixedURL.endsWith("?")) {
155: // ok
156: } else {
157: // it must have other parameters
158: if (!fixedURL.endsWith("&")) {
159: fixedURL = fixedURL + "&";
160: }
161: }
162: }
163:
164: return fixedURL;
165: }
166:
167: public void exitingToRight() throws IOException, WorkbenchException {
168: LinkedList<String> list = new LinkedList<String>();
169: String url = urls.getSelectedIndex() == -1 ? urls.getEditor()
170: .getItem().toString() : urls.getItemAt(
171: urls.getSelectedIndex()).toString();
172:
173: list.add(url);
174:
175: for (int i = 0; i < urls.getItemCount(); ++i)
176: if (i != urls.getSelectedIndex())
177: list.add(urls.getItemAt(i).toString());
178:
179: dataMap.put(URL_KEY, list.toArray(new String[list.size()]));
180: // [UT]
181: //String ver = (String)dataMap.get(VERSION_KEY);
182:
183: url = fixUrlForWMService(url);
184: //[UT] 20.04.2005
185: WMService service = new WMService(url, wmsVersion);
186: //WMService service = new WMService( url );
187:
188: service.initialize();
189: dataMap.put(SERVICE_KEY, service);
190: //[UT] 20.04.2005 added version
191: MapImageFormatChooser formatChooser = new MapImageFormatChooser(
192: wmsVersion);
193:
194: formatChooser.setPreferLossyCompression(false);
195: formatChooser.setTransparencyRequired(true);
196:
197: String format = formatChooser.chooseFormat(service
198: .getCapabilities().getMapFormats());
199:
200: if (format == null) {
201: throw new WorkbenchException(
202: I18N
203: .get("ui.plugin.wms.URLWizardPanel.the-server-does-not-support-gif-png-or-jpeg-format"));
204: }
205:
206: dataMap.put(FORMAT_KEY, format);
207: dataMap.put(MapLayerWizardPanel.INITIAL_LAYER_NAMES_KEY, null);
208: dataMap.put(VERSION_KEY, wmsVersion);
209:
210: }
211:
212: public void enteredFromLeft(Map dataMap) {
213: this .dataMap = dataMap;
214: urls.getEditor().selectAll();
215: }
216:
217: public String getTitle() {
218: return I18N
219: .get("ui.plugin.wms.URLWizardPanel.select-uniform-resource-locator-url");
220: }
221:
222: public String getID() {
223: return getClass().getName();
224: }
225:
226: public boolean isInputValid() {
227: return true;
228: }
229:
230: public String getNextID() {
231: return MapLayerWizardPanel.class.getName();
232: }
233:
234: //[UT] 10.01.2005
235: private Component createVersionButtons(String[] versions) {
236: JPanel p = new JPanel();
237:
238: ActionListener al = new ActionListener() {
239: public void actionPerformed(ActionEvent e) {
240: JRadioButton jb = (JRadioButton) e.getSource();
241: wmsVersion = jb.getText();
242: }
243: };
244:
245: ButtonGroup group = new ButtonGroup();
246: JRadioButton[] buttons = new JRadioButton[versions.length];
247: for (int i = 0; i < buttons.length; i++) {
248: buttons[i] = new JRadioButton(versions[i]);
249: buttons[i].addActionListener(al);
250: group.add(buttons[i]);
251: p.add(buttons[i]);
252: //click the last one
253: if (versions[i].equals(wmsVersion)) {
254: buttons[i].setSelected(true);
255: }
256: }
257:
258: return p;
259: }
260:
261: //[UT] 20.10.2005
262: private Component createLossyCheckBox() {
263: JPanel p = new JPanel();
264: JCheckBox checkBox = new JCheckBox("Preferr Lossy images", true);//I18N.get("ui.plugin.wms.URLWizardPanel.select-uniform-resource-locator-url") );
265: checkBox
266: .setToolTipText("This will try to load JPEG images, if the WMS allows it.");
267: checkBox.addActionListener(new ActionListener() {
268: public void actionPerformed(ActionEvent e) {
269: lossyPreferred = ((JCheckBox) e.getSource())
270: .isSelected();
271: }
272: });
273: p.add(checkBox);
274: return p;
275: }
276:
277: }
|