Changeset 22677 in osm for applications/editors/josm/plugins/wmsplugin
- Timestamp:
- 2010-08-17T23:25:20+02:00 (14 years ago)
- Location:
- applications/editors/josm/plugins/wmsplugin
- Files:
-
- 1 added
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/wmsplugin/.classpath
r22550 r22677 5 5 <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> 6 6 <classpathentry combineaccessrules="false" kind="src" path="/JOSM"/> 7 <classpathentry combineaccessrules="false" kind="src" path="/remotecontrol"/> 7 8 <classpathentry kind="output" path="build"/> 8 9 </classpath> -
applications/editors/josm/plugins/wmsplugin/build.xml
r22550 r22677 33 33 34 34 <property name="josm" location="../../core/dist/josm-custom.jar" /> 35 <property name="remotecontrol" location="../../dist/remotecontrol.jar" /> 35 36 <property name="plugin.dist.dir" value="../../dist" /> 36 37 <property name="plugin.build.dir" value="build" /> … … 43 44 <target name="compile" depends="init"> 44 45 <echo message="creating ${plugin.jar}" /> 45 <javac srcdir="src" classpath="${josm} " debug="true" destdir="${plugin.build.dir}">46 <javac srcdir="src" classpath="${josm};${remotecontrol}" debug="true" destdir="${plugin.build.dir}"> 46 47 <compilerarg value="-Xlint:deprecation" /> 47 48 <compilerarg value="-Xlint:unchecked" /> -
applications/editors/josm/plugins/wmsplugin/src/wmsplugin/Map_Rectifier_WMSmenuAction.java
r19417 r22677 29 29 30 30 public class Map_Rectifier_WMSmenuAction extends JosmAction { 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 31 /** 32 * Class that bundles all required information of a rectifier service 33 */ 34 public static class RectifierService { 35 private final String name; 36 private final String url; 37 private final String wmsUrl; 38 private final Pattern urlRegEx; 39 private final Pattern idValidator; 40 public JRadioButton btn; 41 /** 42 * @param name: Name of the rectifing service 43 * @param url: URL to the service where users can register, upload, etc. 44 * @param wmsUrl: URL to the WMS server where JOSM will grab the images. Insert __s__ where the ID should be placed 45 * @param urlRegEx: a regular expression that determines if a given URL is one of the service and returns the WMS id if so 46 * @param idValidator: regular expression that checks if a given ID is syntactically valid 47 */ 48 public RectifierService(String name, String url, String wmsUrl, String urlRegEx, String idValidator) { 49 this.name = name; 50 this.url = url; 51 this.wmsUrl = wmsUrl; 52 this.urlRegEx = Pattern.compile(urlRegEx); 53 this.idValidator = Pattern.compile(idValidator); 54 } 55 56 public boolean isSelected() { 57 return btn.isSelected(); 58 } 59 } 60 61 /** 62 * List of available rectifier services. May be extended from the outside 63 */ 64 public ArrayList<RectifierService> services = new ArrayList<RectifierService>(); 65 66 public Map_Rectifier_WMSmenuAction() { 67 super(tr("Rectified Image..."), 68 "OLmarker", 69 tr("Download Rectified Images From Various Services"), 70 Shortcut.registerShortcut("wms:rectimg", 71 tr("WMS: {0}", tr("Rectified Image...")), 72 KeyEvent.VK_R, 73 Shortcut.GROUP_NONE), 74 true 75 ); 76 77 // Add default services 78 services.add( 79 new RectifierService("Metacarta Map Rectifier", 80 "http://labs.metacarta.com/rectifier/", 81 "http://labs.metacarta.com/rectifier/wms.cgi?id=__s__&srs=EPSG:4326" 82 + "&Service=WMS&Version=1.1.0&Request=GetMap&format=image/png&", 83 // This matches more than the "classic" WMS link, so users can pretty much 84 // copy any link as long as it includes the ID 85 "labs\\.metacarta\\.com/(?:.*?)(?:/|=)([0-9]+)(?:\\?|/|\\.|$)", 86 "^[0-9]+$") 87 ); 88 services.add( 89 // TODO: Change all links to mapwarper.net once the project has moved. 90 // The RegEx already matches the new URL and old URLs will be forwarded 91 // to make the transition as smooth as possible for the users 92 new RectifierService("Geothings Map Warper", 93 "http://warper.geothings.net/", 94 "http://warper.geothings.net/maps/wms/__s__?request=GetMap&version=1.1.1" 95 + "&styles=&format=image/png&srs=epsg:4326&exceptions=application/vnd.ogc.se_inimage&", 96 // This matches more than the "classic" WMS link, so users can pretty much 97 // copy any link as long as it includes the ID 98 "(?:mapwarper\\.net|warper\\.geothings\\.net)/(?:.*?)/([0-9]+)(?:\\?|/|\\.|$)", 99 "^[0-9]+$") 100 ); 101 102 // This service serves the purpose of "just this once" without forcing the user 103 // to commit the link to the preferences 104 105 // Clipboard content gets trimmed, so matching whitespace only ensures that this 106 // service will never be selected automatically. 107 services.add(new RectifierService(tr("Custom WMS Link"), "", "", "^\\s+$", "")); 108 } 109 110 public void actionPerformed(ActionEvent e) { 111 JPanel panel = new JPanel(new GridBagLayout()); 112 panel.add(new JLabel(tr("Supported Rectifier Services:")), GBC.eol()); 113 114 JTextField tfWmsUrl = new JTextField(30); 115 116 String clip = getClipboardContents(); 117 ButtonGroup group = new ButtonGroup(); 118 119 JRadioButton firstBtn = null; 120 for(RectifierService s : services) { 121 JRadioButton serviceBtn = new JRadioButton(s.name); 122 if(firstBtn == null) 123 firstBtn = serviceBtn; 124 // Checks clipboard contents against current service if no match has been found yet. 125 // If the contents match, they will be inserted into the text field and the corresponding 126 // service will be pre-selected. 127 if(!clip.equals("") && tfWmsUrl.getText().equals("") 128 && (s.urlRegEx.matcher(clip).find() || s.idValidator.matcher(clip).matches())) { 129 serviceBtn.setSelected(true); 130 tfWmsUrl.setText(clip); 131 } 132 s.btn = serviceBtn; 133 group.add(serviceBtn); 134 if(!s.url.equals("")) { 135 panel.add(serviceBtn, GBC.std()); 136 panel.add(new UrlLabel(s.url, tr("Visit Homepage")), GBC.eol().anchor(GridBagConstraints.EAST)); 137 } else 138 panel.add(serviceBtn, GBC.eol().anchor(GridBagConstraints.WEST)); 139 } 140 141 // Fallback in case no match was found 142 if(tfWmsUrl.getText().equals("") && firstBtn != null) 143 firstBtn.setSelected(true); 144 145 panel.add(new JLabel(tr("WMS URL or Image ID:")), GBC.eol()); 146 panel.add(tfWmsUrl, GBC.eol().fill(GridBagConstraints.HORIZONTAL)); 147 148 ExtendedDialog diag = new ExtendedDialog(Main.parent, 149 tr("Add Rectified Image"), 150 151 new String[] {tr("Add Rectified Image"), tr("Cancel")}); 152 diag.setContent(panel); 153 diag.setButtonIcons(new String[] {"OLmarker.png", "cancel.png"}); 154 155 // This repeatedly shows the dialog in case there has been an error. 156 // The loop is break;-ed if the users cancels 157 outer: while(true) { 158 diag.showDialog(); 159 int answer = diag.getValue(); 160 // Break loop when the user cancels 161 if(answer != 1) 162 break; 163 164 String text = tfWmsUrl.getText().trim(); 165 // Loop all services until we find the selected one 166 for(RectifierService s : services) { 167 if(!s.isSelected()) 168 continue; 169 170 // We've reached the custom WMS URL service 171 // Just set the URL and hope everything works out 172 if(s.wmsUrl.equals("")) { 173 addWMSLayer(s.name + " (" + text + ")", text); 174 break outer; 175 } 176 177 // First try to match if the entered string as an URL 178 Matcher m = s.urlRegEx.matcher(text); 179 if(m.find()) { 180 String id = m.group(1); 181 String newURL = s.wmsUrl.replaceAll("__s__", id); 182 String title = s.name + " (" + id + ")"; 183 addWMSLayer(title, newURL); 184 break outer; 185 } 186 // If not, look if it's a valid ID for the selected service 187 if(s.idValidator.matcher(text).matches()) { 188 String newURL = s.wmsUrl.replaceAll("__s__", text); 189 String title = s.name + " (" + text + ")"; 190 addWMSLayer(title, newURL); 191 break outer; 192 } 193 194 // We've found the selected service, but the entered string isn't suitable for 195 // it. So quit checking the other radio buttons 196 break; 197 } 198 199 // and display an error message. The while(true) ensures that the dialog pops up again 200 JOptionPane.showMessageDialog(Main.parent, 201 tr("Couldn't match the entered link or id to the selected service. Please try again."), 202 tr("No valid WMS URL or id"), 203 JOptionPane.ERROR_MESSAGE); 204 diag.setVisible(true); 205 } 206 } 207 208 /** 209 * Adds a WMS Layer with given title and URL 210 * @param title: Name of the layer as it will shop up in the layer manager 211 * @param url: URL to the WMS server 212 * @param cookies: Cookies to send with each image request (Format: josm=is; very=cool) 213 */ 214 private void addWMSLayer(String title, String url, String cookies) { 215 WMSLayer wmsLayer = new WMSLayer(title, url, cookies); 216 Main.main.addLayer(wmsLayer); 217 } 218 219 /** 220 * Adds a WMS Layer with given title and URL 221 * @param title: Name of the layer as it will shop up in the layer manager 222 * @param url: URL to the WMS server 223 */ 224 private void addWMSLayer(String title, String url) { 225 addWMSLayer(title, url, ""); 226 } 227 228 /** 229 * Helper function that extracts a String from the Clipboard if available. 230 * Returns an empty String otherwise 231 * @return String Clipboard contents if available 232 */ 233 private String getClipboardContents() { 234 String result = ""; 235 Transferable contents = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); 236 237 if(contents == null || !contents.isDataFlavorSupported(DataFlavor.stringFlavor)) 238 return ""; 239 240 try { 241 result = (String)contents.getTransferData(DataFlavor.stringFlavor); 242 } catch(Exception ex) { 243 return ""; 244 } 245 return result.trim(); 246 } 247 247 } -
applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSAdjustAction.java
r19417 r22677 31 31 32 32 public class WMSAdjustAction extends MapMode implements MouseListener, MouseMotionListener{ 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 33 //static private final Logger logger = Logger.getLogger(WMSAdjustAction.class.getName()); 34 35 GeorefImage selectedImage; 36 boolean mouseDown; 37 EastNorth prevEastNorth; 38 private WMSLayer adjustingLayer; 39 40 public WMSAdjustAction(MapFrame mapFrame) { 41 super(tr("Adjust WMS"), "adjustwms", 42 tr("Adjust the position of the selected WMS layer"), mapFrame, 43 ImageProvider.getCursor("normal", "move")); 44 } 45 46 47 48 @Override public void enterMode() { 49 super.enterMode(); 50 if (!hasWMSLayersToAdjust()) { 51 warnNoWMSLayers(); 52 return; 53 } 54 List<WMSLayer> wmsLayers = Main.map.mapView.getLayersOfType(WMSLayer.class); 55 if (wmsLayers.size() == 1) { 56 adjustingLayer = wmsLayers.get(0); 57 } else { 58 adjustingLayer = (WMSLayer)askAdjustLayer(Main.map.mapView.getLayersOfType(WMSLayer.class)); 59 } 60 if (adjustingLayer == null) 61 return; 62 if (!adjustingLayer.isVisible()) { 63 adjustingLayer.setVisible(true); 64 } 65 Main.map.mapView.addMouseListener(this); 66 Main.map.mapView.addMouseMotionListener(this); 67 } 68 69 @Override public void exitMode() { 70 super.exitMode(); 71 Main.map.mapView.removeMouseListener(this); 72 Main.map.mapView.removeMouseMotionListener(this); 73 adjustingLayer = null; 74 } 75 76 @Override public void mousePressed(MouseEvent e) { 77 if (e.getButton() != MouseEvent.BUTTON1) 78 return; 79 80 if (adjustingLayer.isVisible()) { 81 prevEastNorth=Main.map.mapView.getEastNorth(e.getX(),e.getY()); 82 selectedImage = adjustingLayer.findImage(prevEastNorth); 83 if(selectedImage!=null) { 84 Main.map.mapView.setCursor 85 (Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); 86 } 87 } 88 } 89 90 @Override public void mouseDragged(MouseEvent e) { 91 if(selectedImage!=null) { 92 EastNorth eastNorth= 93 Main.map.mapView.getEastNorth(e.getX(),e.getY()); 94 adjustingLayer.displace( 95 eastNorth.east()-prevEastNorth.east(), 96 eastNorth.north()-prevEastNorth.north() 97 ); 98 prevEastNorth = eastNorth; 99 Main.map.mapView.repaint(); 100 } 101 } 102 103 @Override public void mouseReleased(MouseEvent e) { 104 Main.map.mapView.repaint(); 105 Main.map.mapView.setCursor(Cursor.getDefaultCursor()); 106 selectedImage = null; 107 prevEastNorth = null; 108 } 109 110 @Override 111 public void mouseEntered(MouseEvent e) { 112 } 113 114 @Override 115 public void mouseExited(MouseEvent e) { 116 } 117 118 @Override 119 public void mouseMoved(MouseEvent e) { 120 } 121 122 @Override public void mouseClicked(MouseEvent e) { 123 } 124 125 // This only makes the buttons look disabled, but since no keyboard shortcut is 126 // provided there aren't any other means to activate this tool 127 @Override public boolean layerIsSupported(Layer l) { 128 return (l instanceof WMSLayer) && l.isVisible(); 129 } 130 131 /** 132 * the list cell renderer used to render layer list entries 133 * 134 */ 135 static public class LayerListCellRenderer extends DefaultListCellRenderer { 136 137 protected boolean isActiveLayer(Layer layer) { 138 if (Main.map == null) 139 return false; 140 if (Main.map.mapView == null) 141 return false; 142 return Main.map.mapView.getActiveLayer() == layer; 143 } 144 145 @Override 146 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, 147 boolean cellHasFocus) { 148 Layer layer = (Layer) value; 149 JLabel label = (JLabel) super.getListCellRendererComponent(list, layer.getName(), index, isSelected, 150 cellHasFocus); 151 Icon icon = layer.getIcon(); 152 label.setIcon(icon); 153 label.setToolTipText(layer.getToolTipText()); 154 return label; 155 } 156 } 157 158 /** 159 * Prompts the user with a list of WMS layers which can be adjusted 160 * 161 * @param adjustableLayers the list of adjustable layers 162 * @return the selected layer; null, if no layer was selected 163 */ 164 protected Layer askAdjustLayer(List<? extends Layer> adjustableLayers) { 165 JComboBox layerList = new JComboBox(); 166 layerList.setRenderer(new LayerListCellRenderer()); 167 layerList.setModel(new DefaultComboBoxModel(adjustableLayers.toArray())); 168 layerList.setSelectedIndex(0); 169 170 JPanel pnl = new JPanel(); 171 pnl.setLayout(new GridBagLayout()); 172 pnl.add(new JLabel(tr("Please select the WMS layer to adjust.")), GBC.eol()); 173 pnl.add(layerList, GBC.eol()); 174 175 ExtendedDialog diag = new ExtendedDialog( 176 Main.parent, 177 tr("Select WMS layer"), 178 new String[] { tr("Start adjusting"),tr("Cancel") } 179 ); 180 diag.setContent(pnl); 181 diag.setButtonIcons(new String[] { "mapmode/adjustwms", "cancel" }); 182 diag.showDialog(); 183 int decision = diag.getValue(); 184 if (decision != 1) 185 return null; 186 Layer adjustLayer = (Layer) layerList.getSelectedItem(); 187 return adjustLayer; 188 } 189 190 /** 191 * Displays a warning message if there are no WMS layers to adjust 192 * 193 */ 194 protected void warnNoWMSLayers() { 195 JOptionPane.showMessageDialog( 196 Main.parent, 197 tr("There are currently no WMS layer to adjust."), 198 tr("No layers to adjust"), 199 JOptionPane.WARNING_MESSAGE 200 ); 201 } 202 203 /** 204 * Replies true if there is at least one WMS layer 205 * 206 * @return true if there is at least one WMS layer 207 */ 208 protected boolean hasWMSLayersToAdjust() { 209 if (Main.map == null) return false; 210 if (Main.map.mapView == null) return false; 211 return ! Main.map.mapView.getLayersOfType(WMSLayer.class).isEmpty(); 212 } 213 214 @Override 215 protected void updateEnabledState() { 216 setEnabled(hasWMSLayersToAdjust()); 217 } 218 218 } -
applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSDownloadAction.java
r16308 r22677 10 10 public class WMSDownloadAction extends JosmAction { 11 11 12 12 private final WMSInfo info; 13 13 14 15 16 17 18 14 public WMSDownloadAction(WMSInfo info) { 15 super(info.name, "wmsmenu", tr("Download WMS tile from {0}",info.name), null, false); 16 putValue("toolbar", "wms_" + info.name); 17 this.info = info; 18 } 19 19 20 21 20 public void actionPerformed(ActionEvent e) { 21 //System.out.println(info.url); 22 22 23 24 25 23 WMSLayer wmsLayer = new WMSLayer(info.name, info.url, info.cookies); 24 Main.main.addLayer(wmsLayer); 25 } 26 26 27 28 29 30 31 32 27 public static WMSLayer getLayer(WMSInfo info) { 28 // FIXME: move this to WMSPlugin/WMSInfo/preferences. 29 WMSLayer wmsLayer = new WMSLayer(info.name, info.url, info.cookies); 30 Main.main.addLayer(wmsLayer); 31 return wmsLayer; 32 } 33 33 }; -
applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSLayer.java
r22581 r22677 29 29 import org.openstreetmap.josm.actions.SaveActionBase; 30 30 import org.openstreetmap.josm.data.Bounds; 31 import org.openstreetmap.josm.data.ProjectionBounds;32 31 import org.openstreetmap.josm.data.Preferences.PreferenceChangeEvent; 33 32 import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener; 33 import org.openstreetmap.josm.data.ProjectionBounds; 34 34 import org.openstreetmap.josm.data.coor.EastNorth; 35 35 import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor; … … 310 310 311 311 public class DownloadAction extends AbstractAction { 312 private static final long serialVersionUID = -7183852461015284020L; 312 313 public DownloadAction() { 313 314 super(tr("Download visible tiles")); -
applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSPlugin.java
r21796 r22677 36 36 import org.openstreetmap.josm.io.MirroredInputStream; 37 37 import org.openstreetmap.josm.plugins.Plugin; 38 import org.openstreetmap.josm.plugins.PluginHandler; 38 39 import org.openstreetmap.josm.plugins.PluginInformation; 40 import org.openstreetmap.josm.plugins.PluginProxy; 41 import org.openstreetmap.josm.plugins.remotecontrol.RemoteControlPlugin; 39 42 40 43 import wmsplugin.io.WMSLayerExporter; … … 42 45 43 46 public class WMSPlugin extends Plugin { 44 static CacheFiles cache = new CacheFiles("wmsplugin"); 45 46 WMSLayer wmsLayer; 47 static JMenu wmsJMenu; 48 49 static ArrayList<WMSInfo> wmsList = new ArrayList<WMSInfo>(); 50 static TreeMap<String,String> wmsListDefault = new TreeMap<String,String>(); 51 52 static boolean doOverlap = false; 53 static int overlapEast = 14; 54 static int overlapNorth = 4; 55 static int simultaneousConnections = 3; 56 57 // remember state of menu item to restore on changed preferences 58 static private boolean menuEnabled = false; 59 60 protected void initExporterAndImporter() { 61 ExtensionFileFilter.exporters.add(new WMSLayerExporter()); 62 ExtensionFileFilter.importers.add(new WMSLayerImporter()); 63 } 64 65 public WMSPlugin(PluginInformation info) { 66 super(info); 67 refreshMenu(); 68 cache.setExpire(CacheFiles.EXPIRE_MONTHLY, false); 69 cache.setMaxSize(70, false); 70 initExporterAndImporter(); 71 } 72 73 // this parses the preferences settings. preferences for the wms plugin have to 74 // look like this: 75 // wmsplugin.1.name=Landsat 76 // wmsplugin.1.url=http://and.so.on/ 77 78 @Override 79 public void copy(String from, String to) throws FileNotFoundException, IOException 80 { 81 File pluginDir = new File(getPrefsPath()); 82 if (!pluginDir.exists()) 83 pluginDir.mkdirs(); 84 FileOutputStream out = new FileOutputStream(getPrefsPath() + to); 85 InputStream in = WMSPlugin.class.getResourceAsStream(from); 86 byte[] buffer = new byte[8192]; 87 for(int len = in.read(buffer); len > 0; len = in.read(buffer)) 88 out.write(buffer, 0, len); 89 in.close(); 90 out.close(); 91 } 92 93 94 public static void refreshMenu() { 95 wmsList.clear(); 96 Map<String,String> prefs = Main.pref.getAllPrefix("wmsplugin.url."); 97 98 TreeSet<String> keys = new TreeSet<String>(prefs.keySet()); 99 100 // Here we load the settings for "overlap" checkbox and spinboxes. 101 102 try { 103 doOverlap = Boolean.valueOf(prefs.get("wmsplugin.url.overlap")); 104 } catch (Exception e) {} // If sth fails, we drop to default settings. 105 106 try { 107 overlapEast = Integer.valueOf(prefs.get("wmsplugin.url.overlapEast")); 108 } catch (Exception e) {} // If sth fails, we drop to default settings. 109 110 try { 111 overlapNorth = Integer.valueOf(prefs.get("wmsplugin.url.overlapNorth")); 112 } catch (Exception e) {} // If sth fails, we drop to default settings. 113 114 // Load the settings for number of simultaneous connections 115 try { 116 simultaneousConnections = Integer.valueOf(prefs.get("wmsplugin.simultanousConnections")); 117 } catch (Exception e) {} // If sth fails, we drop to default settings. 118 119 // And then the names+urls of WMS servers 120 int prefid = 0; 121 String name = null; 122 String url = null; 123 String cookies = ""; 124 int lastid = -1; 125 for (String key : keys) { 126 String[] elements = key.split("\\."); 127 if (elements.length != 4) continue; 128 try { 129 prefid = Integer.parseInt(elements[2]); 130 } catch(NumberFormatException e) { 131 continue; 132 } 133 if (prefid != lastid) { 134 name = url = null; lastid = prefid; 135 } 136 if (elements[3].equals("name")) 137 name = prefs.get(key); 138 else if (elements[3].equals("url")) 139 { 140 /* FIXME: Remove the if clause after some time */ 141 if(!prefs.get(key).startsWith("yahoo:")) /* legacy stuff */ 142 url = prefs.get(key); 143 } 144 else if (elements[3].equals("cookies")) 145 cookies = prefs.get(key); 146 if (name != null && url != null) 147 wmsList.add(new WMSInfo(name, url, cookies, prefid)); 148 } 149 String source = "http://svn.openstreetmap.org/applications/editors/josm/plugins/wmsplugin/sources.cfg"; 150 try 151 { 152 MirroredInputStream s = new MirroredInputStream(source, 153 Main.pref.getPreferencesDir() + "plugins/wmsplugin/", -1); 154 InputStreamReader r; 155 try 156 { 157 r = new InputStreamReader(s, "UTF-8"); 158 } 159 catch (UnsupportedEncodingException e) 160 { 161 r = new InputStreamReader(s); 162 } 163 BufferedReader reader = new BufferedReader(r); 164 String line; 165 while((line = reader.readLine()) != null) 166 { 167 String val[] = line.split(";"); 168 if(!line.startsWith("#") && val.length == 3) 169 setDefault("true".equals(val[0]), tr(val[1]), val[2]); 170 } 171 } 172 catch (IOException e) 173 { 174 } 175 176 Collections.sort(wmsList); 177 MainMenu menu = Main.main.menu; 178 179 if (wmsJMenu == null) 180 wmsJMenu = menu.addMenu(marktr("WMS"), KeyEvent.VK_W, menu.defaultMenuPos, ht("/Plugin/WMS")); 181 else 182 wmsJMenu.removeAll(); 183 184 // for each configured WMSInfo, add a menu entry. 185 for (final WMSInfo u : wmsList) { 186 wmsJMenu.add(new JMenuItem(new WMSDownloadAction(u))); 187 } 188 wmsJMenu.addSeparator(); 189 wmsJMenu.add(new JMenuItem(new Map_Rectifier_WMSmenuAction())); 190 191 wmsJMenu.addSeparator(); 192 wmsJMenu.add(new JMenuItem(new 193 JosmAction(tr("Blank Layer"), "blankmenu", tr("Open a blank WMS layer to load data from a file"), null, false) { 194 public void actionPerformed(ActionEvent ev) { 195 Main.main.addLayer(new WMSLayer()); 196 } 197 })); 198 setEnabledAll(menuEnabled); 199 } 200 201 /* add a default entry in case the URL does not yet exist */ 202 private static void setDefault(Boolean force, String name, String url) 203 { 204 String testurl = url.replaceAll("=", "_"); 205 wmsListDefault.put(name, url); 206 207 if(force && !Main.pref.getBoolean("wmsplugin.default."+testurl)) 208 { 209 Main.pref.put("wmsplugin.default."+testurl, true); 210 int id = -1; 211 for(WMSInfo i : wmsList) 212 { 213 if(url.equals(i.url)) 214 return; 215 if(i.prefid > id) 216 id = i.prefid; 217 } 218 WMSInfo newinfo = new WMSInfo(name, url, id+1); 219 newinfo.save(); 220 wmsList.add(newinfo); 221 } 222 } 223 224 public static Grabber getGrabber(ProjectionBounds bounds, GeorefImage img, MapView mv, WMSLayer layer){ 225 if(layer.baseURL.startsWith("html:")) 226 return new HTMLGrabber(bounds, img, mv, layer, cache); 227 else 228 return new WMSGrabber(bounds, img, mv, layer, cache); 229 } 230 231 private static void setEnabledAll(boolean isEnabled) { 232 for(int i=0; i < wmsJMenu.getItemCount(); i++) { 233 JMenuItem item = wmsJMenu.getItem(i); 234 235 if(item != null) item.setEnabled(isEnabled); 236 } 237 menuEnabled = isEnabled; 238 } 239 240 @Override 241 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) { 242 if (oldFrame==null && newFrame!=null) { 243 setEnabledAll(true); 244 Main.map.addMapMode(new IconToggleButton 245 (new WMSAdjustAction(Main.map))); 246 } else if (oldFrame!=null && newFrame==null ) { 247 setEnabledAll(false); 248 } 249 } 250 251 @Override 252 public PreferenceSetting getPreferenceSetting() { 253 return new WMSPreferenceEditor(); 254 } 255 256 static public String getPrefsPath() 257 { 258 return Main.pref.getPluginsDirectory().getPath() + "/wmsplugin/"; 259 } 47 static CacheFiles cache = new CacheFiles("wmsplugin"); 48 49 WMSLayer wmsLayer; 50 static JMenu wmsJMenu; 51 52 static ArrayList<WMSInfo> wmsList = new ArrayList<WMSInfo>(); 53 static TreeMap<String,String> wmsListDefault = new TreeMap<String,String>(); 54 55 static boolean doOverlap = false; 56 static int overlapEast = 14; 57 static int overlapNorth = 4; 58 static int simultaneousConnections = 3; 59 // remember state of menu item to restore on changed preferences 60 static private boolean menuEnabled = false; 61 62 static boolean remoteControlAvailable = false; 63 64 protected void initExporterAndImporter() { 65 ExtensionFileFilter.exporters.add(new WMSLayerExporter()); 66 ExtensionFileFilter.importers.add(new WMSLayerImporter()); 67 } 68 69 public WMSPlugin(PluginInformation info) { 70 super(info); 71 /* 72 System.out.println("constructor " + this.getClass().getName() + " (" + info.name + 73 " v " + info.version + " stage " + info.stage + ")"); 74 */ 75 refreshMenu(); 76 cache.setExpire(CacheFiles.EXPIRE_MONTHLY, false); 77 cache.setMaxSize(70, false); 78 initExporterAndImporter(); 79 initRemoteControl(); 80 } 81 82 /** 83 * Check if remotecontrol plug-in is available and if its version is 84 * high enough and add handler for "wms" remote control command "wms". 85 */ 86 private void initRemoteControl() { 87 final String remotecontrolName = "remotecontrol"; 88 final String remotecontrolVersion = "22675"; 89 for(PluginProxy pp: PluginHandler.pluginList) 90 { 91 PluginInformation info = pp.getPluginInformation(); 92 if(remotecontrolName.equals(info.name)) 93 { 94 if(remotecontrolVersion.compareTo(info.version) <= 0) 95 { 96 remoteControlAvailable = true; 97 } 98 else 99 { 100 System.out.println("wmsplugin: remote control plugin version is " + 101 info.version + ", need " + remotecontrolVersion + " or newer"); 102 } 103 break; 104 } 105 } 106 107 if(remoteControlAvailable) 108 { 109 System.out.println("wmsplugin: initializing remote control"); 110 RemoteControlPlugin plugin = 111 (RemoteControlPlugin) PluginHandler.getPlugin(remotecontrolName); 112 plugin.addRequestHandler(WMSRemoteHandler.command, WMSRemoteHandler.class); 113 } 114 else 115 { 116 System.out.println("wmsplugin: cannot use remote control"); 117 } 118 } 119 120 // this parses the preferences settings. preferences for the wms plugin have to 121 // look like this: 122 // wmsplugin.1.name=Landsat 123 // wmsplugin.1.url=http://and.so.on/ 124 125 @Override 126 public void copy(String from, String to) throws FileNotFoundException, IOException 127 { 128 File pluginDir = new File(getPrefsPath()); 129 if (!pluginDir.exists()) 130 pluginDir.mkdirs(); 131 FileOutputStream out = new FileOutputStream(getPrefsPath() + to); 132 InputStream in = WMSPlugin.class.getResourceAsStream(from); 133 byte[] buffer = new byte[8192]; 134 for(int len = in.read(buffer); len > 0; len = in.read(buffer)) 135 out.write(buffer, 0, len); 136 in.close(); 137 out.close(); 138 } 139 140 141 public static void refreshMenu() { 142 wmsList.clear(); 143 Map<String,String> prefs = Main.pref.getAllPrefix("wmsplugin.url."); 144 145 TreeSet<String> keys = new TreeSet<String>(prefs.keySet()); 146 147 // Here we load the settings for "overlap" checkbox and spinboxes. 148 149 try { 150 doOverlap = Boolean.valueOf(prefs.get("wmsplugin.url.overlap")); 151 } catch (Exception e) {} // If sth fails, we drop to default settings. 152 153 try { 154 overlapEast = Integer.valueOf(prefs.get("wmsplugin.url.overlapEast")); 155 } catch (Exception e) {} // If sth fails, we drop to default settings. 156 157 try { 158 overlapNorth = Integer.valueOf(prefs.get("wmsplugin.url.overlapNorth")); 159 } catch (Exception e) {} // If sth fails, we drop to default settings. 160 161 // Load the settings for number of simultaneous connections 162 try { 163 simultaneousConnections = Integer.valueOf(Main.pref.get("wmsplugin.simultanousConnections")); 164 } catch (Exception e) {} // If sth fails, we drop to default settings. 165 166 // And then the names+urls of WMS servers 167 int prefid = 0; 168 String name = null; 169 String url = null; 170 String cookies = ""; 171 int lastid = -1; 172 for (String key : keys) { 173 String[] elements = key.split("\\."); 174 if (elements.length != 4) continue; 175 try { 176 prefid = Integer.parseInt(elements[2]); 177 } catch(NumberFormatException e) { 178 continue; 179 } 180 if (prefid != lastid) { 181 name = url = null; lastid = prefid; 182 } 183 if (elements[3].equals("name")) 184 name = prefs.get(key); 185 else if (elements[3].equals("url")) 186 { 187 /* FIXME: Remove the if clause after some time */ 188 if(!prefs.get(key).startsWith("yahoo:")) /* legacy stuff */ 189 url = prefs.get(key); 190 } 191 else if (elements[3].equals("cookies")) 192 cookies = prefs.get(key); 193 if (name != null && url != null) 194 wmsList.add(new WMSInfo(name, url, cookies, prefid)); 195 } 196 String source = "http://svn.openstreetmap.org/applications/editors/josm/plugins/wmsplugin/sources.cfg"; 197 try 198 { 199 MirroredInputStream s = new MirroredInputStream(source, 200 Main.pref.getPreferencesDir() + "plugins/wmsplugin/", -1); 201 InputStreamReader r; 202 try 203 { 204 r = new InputStreamReader(s, "UTF-8"); 205 } 206 catch (UnsupportedEncodingException e) 207 { 208 r = new InputStreamReader(s); 209 } 210 BufferedReader reader = new BufferedReader(r); 211 String line; 212 while((line = reader.readLine()) != null) 213 { 214 String val[] = line.split(";"); 215 if(!line.startsWith("#") && val.length == 3) 216 setDefault("true".equals(val[0]), tr(val[1]), val[2]); 217 } 218 } 219 catch (IOException e) 220 { 221 } 222 223 Collections.sort(wmsList); 224 MainMenu menu = Main.main.menu; 225 226 if (wmsJMenu == null) 227 wmsJMenu = menu.addMenu(marktr("WMS"), KeyEvent.VK_W, menu.defaultMenuPos, ht("/Plugin/WMS")); 228 else 229 wmsJMenu.removeAll(); 230 231 // for each configured WMSInfo, add a menu entry. 232 for (final WMSInfo u : wmsList) { 233 wmsJMenu.add(new JMenuItem(new WMSDownloadAction(u))); 234 } 235 wmsJMenu.addSeparator(); 236 wmsJMenu.add(new JMenuItem(new Map_Rectifier_WMSmenuAction())); 237 238 wmsJMenu.addSeparator(); 239 wmsJMenu.add(new JMenuItem(new 240 JosmAction(tr("Blank Layer"), "blankmenu", tr("Open a blank WMS layer to load data from a file"), null, false) { 241 public void actionPerformed(ActionEvent ev) { 242 Main.main.addLayer(new WMSLayer()); 243 } 244 })); 245 setEnabledAll(menuEnabled); 246 } 247 248 /* add a default entry in case the URL does not yet exist */ 249 private static void setDefault(Boolean force, String name, String url) 250 { 251 String testurl = url.replaceAll("=", "_"); 252 wmsListDefault.put(name, url); 253 254 if(force && !Main.pref.getBoolean("wmsplugin.default."+testurl)) 255 { 256 Main.pref.put("wmsplugin.default."+testurl, true); 257 int id = -1; 258 for(WMSInfo i : wmsList) 259 { 260 if(url.equals(i.url)) 261 return; 262 if(i.prefid > id) 263 id = i.prefid; 264 } 265 WMSInfo newinfo = new WMSInfo(name, url, id+1); 266 newinfo.save(); 267 wmsList.add(newinfo); 268 } 269 } 270 271 public static Grabber getGrabber(ProjectionBounds bounds, GeorefImage img, MapView mv, WMSLayer layer){ 272 if(layer.baseURL.startsWith("html:")) 273 return new HTMLGrabber(bounds, img, mv, layer, cache); 274 else 275 return new WMSGrabber(bounds, img, mv, layer, cache); 276 } 277 278 private static void setEnabledAll(boolean isEnabled) { 279 for(int i=0; i < wmsJMenu.getItemCount(); i++) { 280 JMenuItem item = wmsJMenu.getItem(i); 281 282 if(item != null) item.setEnabled(isEnabled); 283 } 284 menuEnabled = isEnabled; 285 } 286 287 @Override 288 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) { 289 if (oldFrame==null && newFrame!=null) { 290 setEnabledAll(true); 291 Main.map.addMapMode(new IconToggleButton 292 (new WMSAdjustAction(Main.map))); 293 } else if (oldFrame!=null && newFrame==null ) { 294 setEnabledAll(false); 295 } 296 } 297 298 @Override 299 public PreferenceSetting getPreferenceSetting() { 300 return new WMSPreferenceEditor(); 301 } 302 303 static public String getPrefsPath() 304 { 305 return Main.pref.getPluginsDirectory().getPath() + "/wmsplugin/"; 306 } 260 307 } -
applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSPreferenceEditor.java
r22008 r22677 32 32 33 33 public class WMSPreferenceEditor implements PreferenceSetting { 34 private DefaultTableModel model; 35 private JComboBox browser; 36 private HashMap<Integer, WMSInfo> oldValues = new HashMap<Integer, WMSInfo>(); 37 38 JCheckBox overlapCheckBox; 39 JSpinner spinEast; 40 JSpinner spinNorth; 41 JSpinner spinSimConn; 42 43 public void addGui(final PreferenceTabbedPane gui) { 44 JPanel p = gui.createPreferenceTab("wms", tr("WMS Plugin Preferences"), tr("Modify list of WMS servers displayed in the WMS plugin menu")); 45 46 model = new DefaultTableModel(new String[]{tr("Menu Name"), tr("WMS URL")}, 0); 47 final JTable list = new JTable(model); 48 JScrollPane scroll = new JScrollPane(list); 49 p.add(scroll, GBC.eol().fill(GridBagConstraints.BOTH)); 50 scroll.setPreferredSize(new Dimension(200,200)); 51 52 for (WMSInfo i : WMSPlugin.wmsList) { 53 oldValues.put(i.prefid, i); 54 model.addRow(new String[]{i.name, i.url}); 55 } 56 57 final DefaultTableModel modeldef = new DefaultTableModel( 58 new String[]{tr("Menu Name (Default)"), tr("WMS URL (Default)")}, 0); 59 final JTable listdef = new JTable(modeldef){ 60 @Override 61 public boolean isCellEditable(int row,int column){return false;} 62 }; 63 JScrollPane scrolldef = new JScrollPane(listdef); 64 // scrolldef is added after the buttons so it's clearer the buttons 65 // control the top list and not the default one 66 scrolldef.setPreferredSize(new Dimension(200,200)); 67 68 for (Map.Entry<String,String> i : WMSPlugin.wmsListDefault.entrySet()) { 69 modeldef.addRow(new String[]{i.getKey(), i.getValue()}); 70 } 71 72 JPanel buttonPanel = new JPanel(new FlowLayout()); 73 74 JButton add = new JButton(tr("Add")); 75 buttonPanel.add(add, GBC.std().insets(0,5,0,0)); 76 add.addActionListener(new ActionListener(){ 77 public void actionPerformed(ActionEvent e) { 78 JPanel p = new JPanel(new GridBagLayout()); 79 p.add(new JLabel(tr("Menu Name")), GBC.std().insets(0,0,5,0)); 80 JTextField key = new JTextField(40); 81 JTextField value = new JTextField(40); 82 p.add(key, GBC.eop().insets(5,0,0,0).fill(GridBagConstraints.HORIZONTAL)); 83 p.add(new JLabel(tr("WMS URL")), GBC.std().insets(0,0,5,0)); 84 p.add(value, GBC.eol().insets(5,0,0,0).fill(GridBagConstraints.HORIZONTAL)); 85 int answer = JOptionPane.showConfirmDialog( 86 gui, p, 87 tr("Enter a menu name and WMS URL"), 88 JOptionPane.OK_CANCEL_OPTION, 89 JOptionPane.QUESTION_MESSAGE); 90 if (answer == JOptionPane.OK_OPTION) { 91 model.addRow(new String[]{key.getText(), value.getText()}); 92 } 93 } 94 }); 95 96 JButton delete = new JButton(tr("Delete")); 97 buttonPanel.add(delete, GBC.std().insets(0,5,0,0)); 98 delete.addActionListener(new ActionListener(){ 99 public void actionPerformed(ActionEvent e) { 100 if (list.getSelectedRow() == -1) 101 JOptionPane.showMessageDialog(gui, tr("Please select the row to delete.")); 102 else 103 { 104 Integer i; 105 while ((i = list.getSelectedRow()) != -1) 106 model.removeRow(i); 107 } 108 } 109 }); 110 111 JButton copy = new JButton(tr("Copy Selected Default(s)")); 112 buttonPanel.add(copy, GBC.std().insets(0,5,0,0)); 113 copy.addActionListener(new ActionListener(){ 114 public void actionPerformed(ActionEvent e) { 115 int[] lines = listdef.getSelectedRows(); 116 if (lines.length == 0) { 117 JOptionPane.showMessageDialog( 118 gui, 119 tr("Please select at least one row to copy."), 120 tr("Information"), 121 JOptionPane.INFORMATION_MESSAGE 122 ); 123 return; 124 } 125 126 outer: for(int i = 0; i < lines.length; i++) { 127 String c1 = modeldef.getValueAt(lines[i], 0).toString(); 128 String c2 = modeldef.getValueAt(lines[i], 1).toString(); 129 130 // Check if an entry with exactly the same values already 131 // exists 132 for(int j = 0; j < model.getRowCount(); j++) { 133 if(c1.equals(model.getValueAt(j, 0).toString()) 134 && c2.equals(model.getValueAt(j, 1).toString())) { 135 // Select the already existing row so the user has 136 // some feedback in case an entry exists 137 list.getSelectionModel().setSelectionInterval(j, j); 138 list.scrollRectToVisible(list.getCellRect(j, 0, true)); 139 continue outer; 140 } 141 } 142 143 model.addRow(new String[] {c1, c2}); 144 int lastLine = model.getRowCount() - 1; 145 list.getSelectionModel().setSelectionInterval(lastLine, lastLine); 146 list.scrollRectToVisible(list.getCellRect(lastLine, 0, true)); 147 } 148 } 149 }); 150 151 p.add(buttonPanel); 152 p.add(Box.createHorizontalGlue(), GBC.eol().fill(GridBagConstraints.HORIZONTAL)); 153 // Add default item list 154 p.add(scrolldef, GBC.eol().insets(0,5,0,0).fill(GridBagConstraints.BOTH)); 155 156 browser = new JComboBox(new String[]{ 157 "webkit-image {0}", 158 "gnome-web-photo --mode=photo --format=png {0} /dev/stdout", 159 "gnome-web-photo-fixed {0}", 160 "webkit-image-gtk {0}"}); 161 browser.setEditable(true); 162 browser.setSelectedItem(Main.pref.get("wmsplugin.browser", "webkit-image {0}")); 163 p.add(new JLabel(tr("Downloader:")), GBC.eol().fill(GridBagConstraints.HORIZONTAL)); 164 p.add(browser); 165 166 //Overlap 167 p.add(Box.createHorizontalGlue(), GBC.eol().fill(GridBagConstraints.HORIZONTAL)); 168 169 overlapCheckBox = new JCheckBox(tr("Overlap tiles"), WMSPlugin.doOverlap ); 170 JLabel labelEast = new JLabel(tr("% of east:")); 171 JLabel labelNorth = new JLabel(tr("% of north:")); 172 spinEast = new JSpinner(new SpinnerNumberModel(WMSPlugin.overlapEast, 1, 50, 1)); 173 spinNorth = new JSpinner(new SpinnerNumberModel(WMSPlugin.overlapNorth, 1, 50, 1)); 174 175 JPanel overlapPanel = new JPanel(new FlowLayout()); 176 overlapPanel.add(overlapCheckBox); 177 overlapPanel.add(labelEast); 178 overlapPanel.add(spinEast); 179 overlapPanel.add(labelNorth); 180 overlapPanel.add(spinNorth); 181 182 p.add(overlapPanel); 183 184 // Simultaneous connections 185 p.add(Box.createHorizontalGlue(), GBC.eol().fill(GridBagConstraints.HORIZONTAL)); 186 JLabel labelSimConn = new JLabel(tr("Simultaneous connections")); 187 spinSimConn = new JSpinner(new SpinnerNumberModel(WMSPlugin.simultaneousConnections, 1, 30, 1)); 188 JPanel overlapPanelSimConn = new JPanel(new FlowLayout()); 189 overlapPanelSimConn.add(labelSimConn); 190 overlapPanelSimConn.add(spinSimConn); 191 p.add(overlapPanelSimConn); 192 } 193 194 public boolean ok() { 195 boolean change = false; 196 for (int i = 0; i < model.getRowCount(); ++i) { 197 String name = model.getValueAt(i,0).toString(); 198 String url = model.getValueAt(i,1).toString(); 199 200 WMSInfo origValue = oldValues.get(i); 201 if (origValue == null) 202 { 203 new WMSInfo(name, url, i).save(); 204 change = true; 205 } 206 else 207 { 208 if (!origValue.name.equals(name) || !origValue.url.equals(url)) 209 { 210 origValue.name = name; 211 origValue.url = url; 212 origValue.save(); 213 change = true; 214 } 215 oldValues.remove(i); 216 } 217 } 218 219 // using null values instead of empty string really deletes 220 // the preferences entry 221 for (WMSInfo i : oldValues.values()) 222 { 223 i.url = null; 224 i.name = null; 225 i.save(); 226 change = true; 227 } 228 229 if (change) WMSPlugin.refreshMenu(); 230 231 WMSPlugin.doOverlap = overlapCheckBox.getModel().isSelected(); 232 WMSPlugin.overlapEast = (Integer) spinEast.getModel().getValue(); 233 WMSPlugin.overlapNorth = (Integer) spinNorth.getModel().getValue(); 234 WMSPlugin.simultaneousConnections = (Integer) spinSimConn.getModel().getValue(); 235 236 Main.pref.put("wmsplugin.url.overlap", String.valueOf(WMSPlugin.doOverlap)); 237 Main.pref.put("wmsplugin.url.overlapEast", String.valueOf(WMSPlugin.overlapEast)); 238 Main.pref.put("wmsplugin.url.overlapNorth", String.valueOf(WMSPlugin.overlapNorth)); 239 240 Main.pref.put("wmsplugin.browser", browser.getEditor().getItem().toString()); 241 Main.pref.put("wmsplugin.simultaneousConnections", String.valueOf(WMSPlugin.simultaneousConnections)); 242 return false; 243 } 244 245 /** 246 * Updates a server URL in the preferences dialog. Used by other plugins. 247 * 248 * @param server The server name 249 * @param url The server URL 250 */ 251 public void setServerUrl(String server, String url) 252 { 253 for (int i = 0; i < model.getRowCount(); i++) 254 { 255 if( server.equals(model.getValueAt(i,0).toString()) ) 256 { 257 model.setValueAt(url, i, 1); 258 return; 259 } 260 } 261 model.addRow(new String[]{server, url}); 262 } 263 264 /** 265 * Gets a server URL in the preferences dialog. Used by other plugins. 266 * 267 * @param server The server name 268 * @return The server URL 269 */ 270 public String getServerUrl(String server) 271 { 272 for (int i = 0; i < model.getRowCount(); i++) 273 { 274 if( server.equals(model.getValueAt(i,0).toString()) ) 275 { 276 return model.getValueAt(i,1).toString(); 277 } 278 } 279 return null; 280 } 34 private DefaultTableModel model; 35 private JComboBox browser; 36 private HashMap<Integer, WMSInfo> oldValues = new HashMap<Integer, WMSInfo>(); 37 38 JCheckBox overlapCheckBox; 39 JSpinner spinEast; 40 JSpinner spinNorth; 41 JSpinner spinSimConn; 42 JCheckBox remoteCheckBox; 43 boolean allowRemoteControl = true; 44 45 public void addGui(final PreferenceTabbedPane gui) { 46 JPanel p = gui.createPreferenceTab("wms", tr("WMS Plugin Preferences"), tr("Modify list of WMS servers displayed in the WMS plugin menu")); 47 48 model = new DefaultTableModel(new String[]{tr("Menu Name"), tr("WMS URL")}, 0); 49 final JTable list = new JTable(model); 50 JScrollPane scroll = new JScrollPane(list); 51 p.add(scroll, GBC.eol().fill(GridBagConstraints.BOTH)); 52 scroll.setPreferredSize(new Dimension(200,200)); 53 54 for (WMSInfo i : WMSPlugin.wmsList) { 55 oldValues.put(i.prefid, i); 56 model.addRow(new String[]{i.name, i.url}); 57 } 58 59 final DefaultTableModel modeldef = new DefaultTableModel( 60 new String[]{tr("Menu Name (Default)"), tr("WMS URL (Default)")}, 0); 61 final JTable listdef = new JTable(modeldef){ 62 @Override 63 public boolean isCellEditable(int row,int column){return false;} 64 }; 65 JScrollPane scrolldef = new JScrollPane(listdef); 66 // scrolldef is added after the buttons so it's clearer the buttons 67 // control the top list and not the default one 68 scrolldef.setPreferredSize(new Dimension(200,200)); 69 70 for (Map.Entry<String,String> i : WMSPlugin.wmsListDefault.entrySet()) { 71 modeldef.addRow(new String[]{i.getKey(), i.getValue()}); 72 } 73 74 JPanel buttonPanel = new JPanel(new FlowLayout()); 75 76 JButton add = new JButton(tr("Add")); 77 buttonPanel.add(add, GBC.std().insets(0,5,0,0)); 78 add.addActionListener(new ActionListener(){ 79 public void actionPerformed(ActionEvent e) { 80 JPanel p = new JPanel(new GridBagLayout()); 81 p.add(new JLabel(tr("Menu Name")), GBC.std().insets(0,0,5,0)); 82 JTextField key = new JTextField(40); 83 JTextField value = new JTextField(40); 84 p.add(key, GBC.eop().insets(5,0,0,0).fill(GridBagConstraints.HORIZONTAL)); 85 p.add(new JLabel(tr("WMS URL")), GBC.std().insets(0,0,5,0)); 86 p.add(value, GBC.eol().insets(5,0,0,0).fill(GridBagConstraints.HORIZONTAL)); 87 int answer = JOptionPane.showConfirmDialog( 88 gui, p, 89 tr("Enter a menu name and WMS URL"), 90 JOptionPane.OK_CANCEL_OPTION, 91 JOptionPane.QUESTION_MESSAGE); 92 if (answer == JOptionPane.OK_OPTION) { 93 model.addRow(new String[]{key.getText(), value.getText()}); 94 } 95 } 96 }); 97 98 JButton delete = new JButton(tr("Delete")); 99 buttonPanel.add(delete, GBC.std().insets(0,5,0,0)); 100 delete.addActionListener(new ActionListener(){ 101 public void actionPerformed(ActionEvent e) { 102 if (list.getSelectedRow() == -1) 103 JOptionPane.showMessageDialog(gui, tr("Please select the row to delete.")); 104 else 105 { 106 Integer i; 107 while ((i = list.getSelectedRow()) != -1) 108 model.removeRow(i); 109 } 110 } 111 }); 112 113 JButton copy = new JButton(tr("Copy Selected Default(s)")); 114 buttonPanel.add(copy, GBC.std().insets(0,5,0,0)); 115 copy.addActionListener(new ActionListener(){ 116 public void actionPerformed(ActionEvent e) { 117 int[] lines = listdef.getSelectedRows(); 118 if (lines.length == 0) { 119 JOptionPane.showMessageDialog( 120 gui, 121 tr("Please select at least one row to copy."), 122 tr("Information"), 123 JOptionPane.INFORMATION_MESSAGE 124 ); 125 return; 126 } 127 128 outer: for(int i = 0; i < lines.length; i++) { 129 String c1 = modeldef.getValueAt(lines[i], 0).toString(); 130 String c2 = modeldef.getValueAt(lines[i], 1).toString(); 131 132 // Check if an entry with exactly the same values already 133 // exists 134 for(int j = 0; j < model.getRowCount(); j++) { 135 if(c1.equals(model.getValueAt(j, 0).toString()) 136 && c2.equals(model.getValueAt(j, 1).toString())) { 137 // Select the already existing row so the user has 138 // some feedback in case an entry exists 139 list.getSelectionModel().setSelectionInterval(j, j); 140 list.scrollRectToVisible(list.getCellRect(j, 0, true)); 141 continue outer; 142 } 143 } 144 145 model.addRow(new String[] {c1, c2}); 146 int lastLine = model.getRowCount() - 1; 147 list.getSelectionModel().setSelectionInterval(lastLine, lastLine); 148 list.scrollRectToVisible(list.getCellRect(lastLine, 0, true)); 149 } 150 } 151 }); 152 153 p.add(buttonPanel); 154 p.add(Box.createHorizontalGlue(), GBC.eol().fill(GridBagConstraints.HORIZONTAL)); 155 // Add default item list 156 p.add(scrolldef, GBC.eol().insets(0,5,0,0).fill(GridBagConstraints.BOTH)); 157 158 browser = new JComboBox(new String[]{ 159 "webkit-image {0}", 160 "gnome-web-photo --mode=photo --format=png {0} /dev/stdout", 161 "gnome-web-photo-fixed {0}", 162 "webkit-image-gtk {0}"}); 163 browser.setEditable(true); 164 browser.setSelectedItem(Main.pref.get("wmsplugin.browser", "webkit-image {0}")); 165 p.add(new JLabel(tr("Downloader:")), GBC.eol().fill(GridBagConstraints.HORIZONTAL)); 166 p.add(browser); 167 168 //Overlap 169 p.add(Box.createHorizontalGlue(), GBC.eol().fill(GridBagConstraints.HORIZONTAL)); 170 171 overlapCheckBox = new JCheckBox(tr("Overlap tiles"), WMSPlugin.doOverlap ); 172 JLabel labelEast = new JLabel(tr("% of east:")); 173 JLabel labelNorth = new JLabel(tr("% of north:")); 174 spinEast = new JSpinner(new SpinnerNumberModel(WMSPlugin.overlapEast, 1, 50, 1)); 175 spinNorth = new JSpinner(new SpinnerNumberModel(WMSPlugin.overlapNorth, 1, 50, 1)); 176 177 JPanel overlapPanel = new JPanel(new FlowLayout()); 178 overlapPanel.add(overlapCheckBox); 179 overlapPanel.add(labelEast); 180 overlapPanel.add(spinEast); 181 overlapPanel.add(labelNorth); 182 overlapPanel.add(spinNorth); 183 184 p.add(overlapPanel); 185 186 // Simultaneous connections 187 p.add(Box.createHorizontalGlue(), GBC.eol().fill(GridBagConstraints.HORIZONTAL)); 188 JLabel labelSimConn = new JLabel(tr("Simultaneous connections")); 189 spinSimConn = new JSpinner(new SpinnerNumberModel(WMSPlugin.simultaneousConnections, 1, 30, 1)); 190 JPanel overlapPanelSimConn = new JPanel(new FlowLayout()); 191 overlapPanelSimConn.add(labelSimConn); 192 overlapPanelSimConn.add(spinSimConn); 193 p.add(overlapPanelSimConn, GBC.eol().fill(GridBagConstraints.HORIZONTAL)); 194 195 196 allowRemoteControl = Main.pref.getBoolean("wmsplugin.remotecontrol", true); 197 remoteCheckBox = new JCheckBox(tr("Allow remote control (reqires remotecontrol plugin)"), allowRemoteControl ); 198 JPanel remotePanel = new JPanel(new FlowLayout()); 199 remotePanel.add(remoteCheckBox); 200 201 p.add(remotePanel); 202 203 } 204 205 public boolean ok() { 206 boolean change = false; 207 for (int i = 0; i < model.getRowCount(); ++i) { 208 String name = model.getValueAt(i,0).toString(); 209 String url = model.getValueAt(i,1).toString(); 210 211 WMSInfo origValue = oldValues.get(i); 212 if (origValue == null) 213 { 214 new WMSInfo(name, url, i).save(); 215 change = true; 216 } 217 else 218 { 219 if (!origValue.name.equals(name) || !origValue.url.equals(url)) 220 { 221 origValue.name = name; 222 origValue.url = url; 223 origValue.save(); 224 change = true; 225 } 226 oldValues.remove(i); 227 } 228 } 229 230 // using null values instead of empty string really deletes 231 // the preferences entry 232 for (WMSInfo i : oldValues.values()) 233 { 234 i.url = null; 235 i.name = null; 236 i.save(); 237 change = true; 238 } 239 240 if (change) WMSPlugin.refreshMenu(); 241 242 WMSPlugin.doOverlap = overlapCheckBox.getModel().isSelected(); 243 WMSPlugin.overlapEast = (Integer) spinEast.getModel().getValue(); 244 WMSPlugin.overlapNorth = (Integer) spinNorth.getModel().getValue(); 245 WMSPlugin.simultaneousConnections = (Integer) spinSimConn.getModel().getValue(); 246 allowRemoteControl = remoteCheckBox.getModel().isSelected(); 247 248 Main.pref.put("wmsplugin.url.overlap", String.valueOf(WMSPlugin.doOverlap)); 249 Main.pref.put("wmsplugin.url.overlapEast", String.valueOf(WMSPlugin.overlapEast)); 250 Main.pref.put("wmsplugin.url.overlapNorth", String.valueOf(WMSPlugin.overlapNorth)); 251 252 Main.pref.put("wmsplugin.browser", browser.getEditor().getItem().toString()); 253 Main.pref.put("wmsplugin.simultaneousConnections", String.valueOf(WMSPlugin.simultaneousConnections)); 254 255 Main.pref.put("wmsplugin.remotecontrol", String.valueOf(allowRemoteControl)); 256 return false; 257 } 258 259 /** 260 * Updates a server URL in the preferences dialog. Used by other plugins. 261 * 262 * @param server The server name 263 * @param url The server URL 264 */ 265 public void setServerUrl(String server, String url) 266 { 267 for (int i = 0; i < model.getRowCount(); i++) 268 { 269 if( server.equals(model.getValueAt(i,0).toString()) ) 270 { 271 model.setValueAt(url, i, 1); 272 return; 273 } 274 } 275 model.addRow(new String[]{server, url}); 276 } 277 278 /** 279 * Gets a server URL in the preferences dialog. Used by other plugins. 280 * 281 * @param server The server name 282 * @return The server URL 283 */ 284 public String getServerUrl(String server) 285 { 286 for (int i = 0; i < model.getRowCount(); i++) 287 { 288 if( server.equals(model.getValueAt(i,0).toString()) ) 289 { 290 return model.getValueAt(i,1).toString(); 291 } 292 } 293 return null; 294 } 281 295 } 282 296
Note:
See TracChangeset
for help on using the changeset viewer.