source: josm/trunk/src/org/openstreetmap/josm/actions/AddImageryLayerAction.java@ 6589

Last change on this file since 6589 was 6248, checked in by Don-vip, 11 years ago

Rework console output:

  • new log level "error"
  • Replace nearly all calls to system.out and system.err to Main.(error|warn|info|debug)
  • Remove some unnecessary debug output
  • Some messages are modified (removal of "Info", "Warning", "Error" from the message itself -> notable i18n impact but limited to console error messages not seen by the majority of users, so that's ok)
  • Property svn:eol-style set to native
File size: 5.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Dimension;
7import java.awt.GridBagLayout;
8import java.awt.event.ActionEvent;
9import java.io.IOException;
10import java.net.MalformedURLException;
11
12import javax.swing.Action;
13import javax.swing.ImageIcon;
14import javax.swing.JComboBox;
15import javax.swing.JOptionPane;
16import javax.swing.JPanel;
17import javax.swing.JScrollPane;
18
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.data.imagery.ImageryInfo;
21import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
22import org.openstreetmap.josm.gui.ExtendedDialog;
23import org.openstreetmap.josm.gui.actionsupport.AlignImageryPanel;
24import org.openstreetmap.josm.gui.layer.ImageryLayer;
25import org.openstreetmap.josm.gui.preferences.imagery.WMSLayerTree;
26import org.openstreetmap.josm.io.imagery.WMSImagery;
27import org.openstreetmap.josm.tools.GBC;
28import org.openstreetmap.josm.tools.ImageProvider;
29
30public class AddImageryLayerAction extends JosmAction implements AdaptableAction {
31
32 private static final int MAX_ICON_SIZE = 24;
33 private final ImageryInfo info;
34
35 public AddImageryLayerAction(ImageryInfo info) {
36 super(info.getMenuName(), /* ICON */"imagery_menu", tr("Add imagery layer {0}",info.getName()), null, false, false);
37 putValue("toolbar", "imagery_" + info.getToolbarName());
38 this.info = info;
39 installAdapters();
40
41 // change toolbar icon from if specified
42 try {
43 if (info.getIcon() != null) {
44 ImageIcon i = new ImageProvider(info.getIcon()).setOptional(true).
45 setMaxHeight(MAX_ICON_SIZE).setMaxWidth(MAX_ICON_SIZE).get();
46 if (i != null) {
47 putValue(Action.SMALL_ICON, i);
48 }
49 }
50 } catch (Exception ex) {
51 throw new RuntimeException(ex.getMessage(), ex);
52 }
53 }
54
55 @Override
56 public void actionPerformed(ActionEvent e) {
57 if (!isEnabled()) return;
58 try {
59 final ImageryInfo infoToAdd = ImageryType.WMS_ENDPOINT.equals(info.getImageryType())
60 ? getWMSLayerInfo() : info;
61 if (infoToAdd != null) {
62 Main.main.addLayer(ImageryLayer.create(infoToAdd));
63 AlignImageryPanel.addNagPanelIfNeeded();
64 }
65 } catch (IllegalArgumentException ex) {
66 if (ex.getMessage() == null || ex.getMessage().isEmpty()) {
67 throw ex;
68 } else {
69 JOptionPane.showMessageDialog(Main.parent,
70 ex.getMessage(), tr("Error"),
71 JOptionPane.ERROR_MESSAGE);
72 }
73 }
74 }
75
76 protected ImageryInfo getWMSLayerInfo() {
77 try {
78 assert (ImageryType.WMS_ENDPOINT.equals(info.getImageryType()));
79 final WMSImagery wms = new WMSImagery();
80 wms.attemptGetCapabilities(info.getUrl());
81
82 final WMSLayerTree tree = new WMSLayerTree();
83 tree.updateTree(wms);
84 final JComboBox formats = new JComboBox(wms.getFormats().toArray());
85 formats.setToolTipText(tr("Select image format for WMS layer"));
86
87 if (1 != new ExtendedDialog(Main.parent, tr("Select WMS layers"), new String[]{tr("Add layers"), tr("Cancel")}) {{
88 final JScrollPane scrollPane = new JScrollPane(tree.getLayerTree());
89 scrollPane.setPreferredSize(new Dimension(400, 400));
90 final JPanel panel = new JPanel(new GridBagLayout());
91 panel.add(scrollPane, GBC.eol().fill());
92 panel.add(formats, GBC.eol().fill(GBC.HORIZONTAL));
93 setContent(panel);
94 }}.showDialog().getValue()) {
95 return null;
96 }
97
98 final String url = wms.buildGetMapUrl(
99 tree.getSelectedLayers(), (String) formats.getSelectedItem());
100 return new ImageryInfo(info.getName(), url, "wms", info.getEulaAcceptanceRequired(), info.getCookies());
101 } // exception handling from AddWMSLayerPanel.java
102 catch (MalformedURLException ex) {
103 JOptionPane.showMessageDialog(Main.parent, tr("Invalid service URL."),
104 tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
105 } catch (IOException ex) {
106 JOptionPane.showMessageDialog(Main.parent, tr("Could not retrieve WMS layer list."),
107 tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
108 } catch (WMSImagery.WMSGetCapabilitiesException ex) {
109 JOptionPane.showMessageDialog(Main.parent, tr("Could not parse WMS layer list."),
110 tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
111 Main.error("Could not parse WMS layer list. Incoming data:\n"+ex.getIncomingData());
112 }
113 return null;
114 }
115
116 protected boolean isLayerAlreadyPresent() {
117 if (Main.isDisplayingMapView()) {
118 for (ImageryLayer layer : Main.map.mapView.getLayersOfType(ImageryLayer.class)) {
119 if (info.equals(layer.getInfo())) {
120 return true;
121 }
122 }
123 }
124 return false;
125 }
126
127 @Override
128 protected void updateEnabledState() {
129 // never enable blacklisted entries. Do not add same imagery layer twice (fix #2519)
130 if (info.isBlacklisted() /*|| isLayerAlreadyPresent()*/) { // FIXME check disabled to allow several instances with different settings (see #7981)
131 setEnabled(false);
132 } else if (info.getImageryType() == ImageryType.TMS || info.getImageryType() == ImageryType.BING || info.getImageryType() == ImageryType.SCANEX) {
133 setEnabled(true);
134 } else if (Main.isDisplayingMapView() && !Main.map.mapView.getAllLayers().isEmpty()) {
135 setEnabled(true);
136 } else {
137 setEnabled(false);
138 }
139 }
140}
Note: See TracBrowser for help on using the repository browser.