Changeset 22733 in osm for applications
- Timestamp:
- 2010-08-22T20:18:44+02:00 (14 years ago)
- Location:
- applications/editors/josm/plugins/remotecontrol/src/org/openstreetmap/josm/plugins/remotecontrol
- Files:
-
- 1 added
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/remotecontrol/src/org/openstreetmap/josm/plugins/remotecontrol/PermissionPref.java
r22675 r22733 5 5 * implemented by the RequestHandler, and an error message to be displayed 6 6 * if not permitted. 7 * 8 * Use @see PermissionPrefWithDefault instead of this class. 9 * 10 * @author Bodo Meissner 7 11 */ 12 @Deprecated 8 13 public class PermissionPref { 14 /** name of the preference setting to permit the remote operation */ 9 15 String pref; 16 /** message to be displayed if operation is not permitted */ 10 17 String message; 18 11 19 public PermissionPref(String pref, String message) 12 20 { -
applications/editors/josm/plugins/remotecontrol/src/org/openstreetmap/josm/plugins/remotecontrol/RemoteControlPlugin.java
r22675 r22733 8 8 9 9 /** 10 10 * Base plugin for remote control operations. 11 * This plugin contains operations that use JOSM core only. 12 * 13 * Other plugins can register additional operations by calling 14 * @see addRequestHandler(). 15 * To allow API changes this plugin contains a @see getVersion() method. 16 * 17 * IMPORTANT! increment the minor version on compatible API extensions 18 * and increment the major version and set minor to 0 on incompatible changes. 11 19 */ 12 20 public class RemoteControlPlugin extends Plugin 13 21 { 22 /** API version 23 * IMPORTANT! update the version number on API changes. 24 */ 25 static final int apiMajorVersion = 1; 26 static final int apiMinorVersion = 0; 27 28 /** 29 * RemoteControl HTTP protocol version. Change minor number for compatible 30 * interface extensions. Change major number in case of incompatible 31 * changes. 32 */ 33 static final int protocolMajorVersion = 1; 34 static final int protocolMinorVersion = 2; 35 14 36 /** The HTTP server this plugin launches */ 15 37 static HttpServer server; 16 38 39 /** 40 * Returns an array of int values with major and minor API version 41 * and major and minor HTTP protocol version. 42 * 43 * The function returns an int[4] instead of an object with fields 44 * to avoid ClassNotFound errors with old versions of remotecontrol. 45 * 46 * @return array of integer version numbers: 47 * apiMajorVersion, apiMinorVersion, protocolMajorVersion, protocolMajorVersion 48 */ 49 public int[] getVersion() 50 { 51 int versions[] = {apiMajorVersion, apiMinorVersion, protocolMajorVersion, protocolMajorVersion}; 52 return versions; 53 } 54 17 55 /** 18 56 * Creates the plugin, and starts the HTTP server -
applications/editors/josm/plugins/remotecontrol/src/org/openstreetmap/josm/plugins/remotecontrol/RemoteControlPreferences.java
r22702 r22733 15 15 import org.openstreetmap.josm.gui.preferences.PreferenceSetting; 16 16 import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane; 17 import org.openstreetmap.josm.plugins.remotecontrol.handler.AddNodeHandler; 18 import org.openstreetmap.josm.plugins.remotecontrol.handler.ImportHandler; 19 import org.openstreetmap.josm.plugins.remotecontrol.handler.LoadAndZoomHandler; 20 import org.openstreetmap.josm.plugins.remotecontrol.handler.VersionHandler; 17 21 import org.openstreetmap.josm.tools.GBC; 18 22 … … 25 29 { 26 30 private JCheckBox permissionLoadData = new JCheckBox(tr("load data from API")); 31 private JCheckBox permissionImportData = new JCheckBox(tr("import data from URL")); 27 32 private JCheckBox permissionCreateObjects = new JCheckBox(tr("create new objects")); 28 33 private JCheckBox permissionChangeSelection = new JCheckBox(tr("change the selection")); … … 42 47 perms.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.gray), tr("Permitted actions"))); 43 48 perms.add(permissionLoadData, GBC.eol().insets(0,5,0,0).fill(GBC.HORIZONTAL)); 49 perms.add(permissionImportData, GBC.eol().insets(0,5,0,0).fill(GBC.HORIZONTAL)); 44 50 perms.add(permissionChangeSelection, GBC.eol().insets(0,5,0,0).fill(GBC.HORIZONTAL)); 45 51 perms.add(permissionChangeViewport, GBC.eol().insets(0,5,0,0).fill(GBC.HORIZONTAL)); … … 51 57 remote.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.VERTICAL)); 52 58 53 permissionLoadData.setSelected(Main.pref.getBoolean("remotecontrol.permission.load-data", true)); 54 permissionChangeSelection.setSelected(Main.pref.getBoolean("remotecontrol.permission.change-selection", true)); 55 permissionChangeViewport.setSelected(Main.pref.getBoolean("remotecontrol.permission.change-viewport", true)); 56 permissionCreateObjects.setSelected(Main.pref.getBoolean("remotecontrol.permission.create-objects", false)); 57 permissionReadProtocolversion.setSelected(Main.pref.getBoolean("remotecontrol.permission.read-protocolversion", true)); 58 alwaysAskUserConfirm.setSelected(Main.pref.getBoolean("remotecontrol.always-confirm", false)); 59 permissionLoadData.setSelected(Main.pref.getBoolean(LoadAndZoomHandler.loadDataPermissionKey, LoadAndZoomHandler.loadDataPermissionDefault)); 60 permissionImportData.setSelected(Main.pref.getBoolean(ImportHandler.permissionKey, ImportHandler.permissionDefault)); 61 permissionChangeSelection.setSelected(Main.pref.getBoolean(LoadAndZoomHandler.changeSelectionPermissionKey, LoadAndZoomHandler.changeSelectionPermissionDefault)); 62 permissionChangeViewport.setSelected(Main.pref.getBoolean(LoadAndZoomHandler.changeViewportPermissionKey, LoadAndZoomHandler.changeViewportPermissionDefault)); 63 permissionCreateObjects.setSelected(Main.pref.getBoolean(AddNodeHandler.permissionKey, AddNodeHandler.permissionDefault)); 64 permissionReadProtocolversion.setSelected(Main.pref.getBoolean(VersionHandler.permissionKey, VersionHandler.permissionDefault)); 65 alwaysAskUserConfirm.setSelected(Main.pref.getBoolean(RequestHandler.globalConfirmationKey, RequestHandler.globalConfirmationDefault)); 59 66 60 67 } 61 68 62 69 public boolean ok() { 63 Main.pref.put("remotecontrol.permission.load-data", permissionLoadData.isSelected()); 64 Main.pref.put("remotecontrol.permission.change-selection", permissionChangeSelection.isSelected()); 65 Main.pref.put("remotecontrol.permission.change-viewport", permissionChangeViewport.isSelected()); 66 Main.pref.put("remotecontrol.permission.create-objects", permissionCreateObjects.isSelected()); 67 Main.pref.put("remotecontrol.permission.read-protocolversion", permissionReadProtocolversion.isSelected()); 68 Main.pref.put("remotecontrol.always-confirm", alwaysAskUserConfirm.isSelected()); 70 Main.pref.put(LoadAndZoomHandler.loadDataPermissionKey, permissionLoadData.isSelected()); 71 Main.pref.put(ImportHandler.permissionKey, permissionImportData.isSelected()); 72 Main.pref.put(LoadAndZoomHandler.changeSelectionPermissionKey, permissionChangeSelection.isSelected()); 73 Main.pref.put(LoadAndZoomHandler.changeViewportPermissionKey, permissionChangeViewport.isSelected()); 74 Main.pref.put(AddNodeHandler.permissionKey, permissionCreateObjects.isSelected()); 75 Main.pref.put(VersionHandler.permissionKey, permissionReadProtocolversion.isSelected()); 76 Main.pref.put(RequestHandler.globalConfirmationKey, alwaysAskUserConfirm.isSelected()); 69 77 // FIXME confirm return value - really no restart needed? 70 78 return false /* no restart needed */; -
applications/editors/josm/plugins/remotecontrol/src/org/openstreetmap/josm/plugins/remotecontrol/RequestHandler.java
r22675 r22733 18 18 public abstract class RequestHandler 19 19 { 20 public static final String globalConfirmationKey = "remotecontrol.always-confirm"; 21 public static final boolean globalConfirmationDefault = false; 22 20 23 /** The GET request arguments */ 21 24 protected HashMap<String,String> args; … … 30 33 31 34 /** will be filled with the command assigned to the subclass */ 32 pr ivateString myCommand;35 protected String myCommand; 33 36 34 37 /** … … 77 80 * @return the preference name and error message or null 78 81 */ 79 public PermissionPref getPermissionPref() 82 @SuppressWarnings("deprecation") 83 public PermissionPref getPermissionPref() 80 84 { 81 85 /* Example: 82 return new PermissionPref("fooobar.remotecontrol", 86 return new PermissionPrefWithDefault("fooobar.remotecontrol", 87 true 83 88 "RemoteControl: foobar forbidden by preferences"); 84 89 */ … … 97 102 * @throws RequestHandlerForbiddenException 98 103 */ 99 final public void checkPermission() throws RequestHandlerForbiddenException 104 @SuppressWarnings("deprecation") 105 final public void checkPermission() throws RequestHandlerForbiddenException 100 106 { 101 107 /* 102 108 * If the subclass defines a specific preference and if this is set 103 109 * to false, abort with an error message. 110 * 111 * Note: we use the deprecated class here for compatibility with 112 * older versions of WMSPlugin. 104 113 */ 105 114 PermissionPref permissionPref = getPermissionPref(); 106 115 if((permissionPref != null) && (permissionPref.pref != null)) 107 116 { 108 if (!Main.pref.getBoolean(permissionPref.pref, true)) { 109 System.out.println(permissionPref.message); 117 PermissionPrefWithDefault permissionPrefWithDefault; 118 if(permissionPref instanceof PermissionPrefWithDefault) 119 { 120 permissionPrefWithDefault = (PermissionPrefWithDefault) permissionPref; 121 } 122 else 123 { 124 permissionPrefWithDefault = new PermissionPrefWithDefault(permissionPref); 125 } 126 if (!Main.pref.getBoolean(permissionPrefWithDefault.pref, 127 permissionPrefWithDefault.defaultVal)) { 128 System.out.println(permissionPrefWithDefault.message); 110 129 throw new RequestHandlerForbiddenException(); 111 130 } … … 115 134 * If yes, display specific confirmation message. 116 135 */ 117 if (Main.pref.getBoolean( "remotecontrol.always-confirm", false)) {136 if (Main.pref.getBoolean(globalConfirmationKey, globalConfirmationDefault)) { 118 137 if (JOptionPane.showConfirmDialog(Main.parent, 119 138 "<html>" + getPermissionMessage() + -
applications/editors/josm/plugins/remotecontrol/src/org/openstreetmap/josm/plugins/remotecontrol/RequestProcessor.java
r22675 r22733 28 28 * changes. 29 29 */ 30 public static final String PROTOCOLVERSION = "{\"protocolversion\": {\"major\": 1, \"minor\": 0}, \"application\": \"JOSM RemoteControl\"}"; 30 public static final String PROTOCOLVERSION = "{\"protocolversion\": {\"major\": " + 31 RemoteControlPlugin.protocolMajorVersion + ", \"minor\": " + 32 RemoteControlPlugin.protocolMinorVersion + 33 "}, \"application\": \"JOSM RemoteControl\"}"; 31 34 32 35 /** The socket this processor listens on */ … … 82 85 private static void addRequestHandlerClass(String command, 83 86 Class<? extends RequestHandler> handler, boolean silent) { 84 if (handlers.get(command) != null) { 85 System.out.println("RemoteControl: duplicate command " + command 87 if(command.charAt(0) == '/') 88 { 89 command = command.substring(1); 90 } 91 if (handlers.get("/" + command) != null) { 92 System.out.println("RemoteControl: ignoring duplicate command " + command 86 93 + " with handler " + handler.getName()); 87 94 } else { 88 if(!silent) System.out.println("RemoteControl: adding command command" +89 command + " (handled by " + handler.getName() + ")");95 if(!silent) System.out.println("RemoteControl: adding command \"" + 96 command + "\" (handled by " + handler.getSimpleName() + ")"); 90 97 handlers.put(command, handler); 91 98 } … … 95 102 static { 96 103 addRequestHandlerClass(LoadAndZoomHandler.command, 104 LoadAndZoomHandler.class, true); 105 addRequestHandlerClass(LoadAndZoomHandler.command2, 97 106 LoadAndZoomHandler.class, true); 98 107 addRequestHandlerClass(AddNodeHandler.command, AddNodeHandler.class, true); -
applications/editors/josm/plugins/remotecontrol/src/org/openstreetmap/josm/plugins/remotecontrol/handler/AddNodeHandler.java
r22675 r22733 9 9 import org.openstreetmap.josm.data.coor.LatLon; 10 10 import org.openstreetmap.josm.data.osm.Node; 11 import org.openstreetmap.josm.plugins.remotecontrol.PermissionPref ;11 import org.openstreetmap.josm.plugins.remotecontrol.PermissionPrefWithDefault; 12 12 import org.openstreetmap.josm.plugins.remotecontrol.RequestHandler; 13 import org.openstreetmap.josm.plugins.remotecontrol.RequestHandlerBadRequestException;14 13 15 14 /** … … 18 17 public class AddNodeHandler extends RequestHandler { 19 18 20 public static final String command = "/add_node"; 19 public static final String command = "add_node"; 20 public static final String permissionKey = "remotecontrol.permission.create-objects"; 21 public static final boolean permissionDefault = false; 21 22 22 23 @Override … … 37 38 38 39 @Override 39 public PermissionPref getPermissionPref()40 public PermissionPrefWithDefault getPermissionPref() 40 41 { 41 return new PermissionPref ("remotecontrol.permission.create-objects",42 return new PermissionPrefWithDefault(permissionKey, permissionDefault, 42 43 "RemoteControl: creating objects forbidden by preferences"); 43 44 } -
applications/editors/josm/plugins/remotecontrol/src/org/openstreetmap/josm/plugins/remotecontrol/handler/ImportHandler.java
r22675 r22733 7 7 import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask; 8 8 import org.openstreetmap.josm.actions.downloadtasks.DownloadTask; 9 import org.openstreetmap.josm.plugins.remotecontrol.PermissionPref ;9 import org.openstreetmap.josm.plugins.remotecontrol.PermissionPrefWithDefault; 10 10 import org.openstreetmap.josm.plugins.remotecontrol.RequestHandler; 11 import org.openstreetmap.josm.plugins.remotecontrol.RequestHandlerBadRequestException;12 11 import org.openstreetmap.josm.plugins.remotecontrol.RequestHandlerErrorException; 13 12 … … 17 16 public class ImportHandler extends RequestHandler { 18 17 19 public static final String command = "/import"; 18 public static final String command = "import"; 19 public static final String permissionKey = "remotecontrol.permission.import"; 20 public static final boolean permissionDefault = true; 20 21 21 22 @Override … … 44 45 45 46 @Override 46 public PermissionPref getPermissionPref()47 public PermissionPrefWithDefault getPermissionPref() 47 48 { 48 return new PermissionPref ("remotecontrol.permission.import",49 return new PermissionPrefWithDefault(permissionKey, permissionDefault, 49 50 "RemoteControl: import forbidden by preferences"); 50 51 } -
applications/editors/josm/plugins/remotecontrol/src/org/openstreetmap/josm/plugins/remotecontrol/handler/LoadAndZoomHandler.java
r22675 r22733 22 22 import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor; 23 23 import org.openstreetmap.josm.plugins.remotecontrol.RequestHandler; 24 import org.openstreetmap.josm.plugins.remotecontrol.RequestHandlerBadRequestException;25 24 import org.openstreetmap.josm.plugins.remotecontrol.RequestHandlerErrorException; 26 25 … … 30 29 public class LoadAndZoomHandler extends RequestHandler 31 30 { 32 public static final String command = "/load_and_zoom"; 33 31 public static final String command = "load_and_zoom"; 32 public static final String command2 = "zoom"; 33 34 public static final String loadDataPermissionKey = "remotecontrol.permission.load-data"; 35 public static final boolean loadDataPermissionDefault = true; 36 public static final String changeSelectionPermissionKey = "remotecontrol.permission.change-selection"; 37 public static final boolean changeSelectionPermissionDefault = true; 38 public static final String changeViewportPermissionKey = "remotecontrol.permission.change-viewport"; 39 public static final boolean changeViewportPermissionDefault = true; 40 34 41 @Override 35 42 public String getPermissionMessage() … … 59 66 maxlon = Double.parseDouble(args.get("right")); 60 67 61 if (!Main.pref.getBoolean("remotecontrol.permission.load-data", true))68 if(command.equals(myCommand)) 62 69 { 63 System.out.println("RemoteControl: download forbidden by preferences"); 64 } 65 else 66 { 67 68 // find out whether some data has already been downloaded 69 Area present = null; 70 Area toDownload = null; 71 DataSet ds = Main.main.getCurrentDataSet(); 72 if (ds != null) 73 present = ds.getDataSourceArea(); 74 if (present != null && !present.isEmpty()) { 75 toDownload = new Area(new Rectangle2D.Double(minlon,minlat,maxlon-minlon,maxlat-minlat)); 76 toDownload.subtract(present); 77 if (!toDownload.isEmpty()) 78 { 79 // the result might not be a rectangle (L shaped etc) 80 Rectangle2D downloadBounds = toDownload.getBounds2D(); 81 minlat = downloadBounds.getMinY(); 82 minlon = downloadBounds.getMinX(); 83 maxlat = downloadBounds.getMaxY(); 84 maxlon = downloadBounds.getMaxX(); 85 } 86 } 87 if((toDownload != null) && toDownload.isEmpty()) 70 if (!Main.pref.getBoolean(loadDataPermissionKey, loadDataPermissionDefault)) 88 71 { 89 System.out.println("RemoteControl: no download necessary");72 System.out.println("RemoteControl: download forbidden by preferences"); 90 73 } 91 74 else 92 75 { 93 Future<?> future = osmTask.download(false /*no new layer*/, new Bounds(minlat,minlon,maxlat,maxlon), null /* let the task manage the progress monitor */); 94 Main.worker.submit(new PostDownloadHandler(osmTask, future)); 76 77 // find out whether some data has already been downloaded 78 Area present = null; 79 Area toDownload = null; 80 DataSet ds = Main.main.getCurrentDataSet(); 81 if (ds != null) 82 present = ds.getDataSourceArea(); 83 if (present != null && !present.isEmpty()) { 84 toDownload = new Area(new Rectangle2D.Double(minlon,minlat,maxlon-minlon,maxlat-minlat)); 85 toDownload.subtract(present); 86 if (!toDownload.isEmpty()) 87 { 88 // the result might not be a rectangle (L shaped etc) 89 Rectangle2D downloadBounds = toDownload.getBounds2D(); 90 minlat = downloadBounds.getMinY(); 91 minlon = downloadBounds.getMinX(); 92 maxlat = downloadBounds.getMaxY(); 93 maxlon = downloadBounds.getMaxX(); 94 } 95 } 96 if((toDownload != null) && toDownload.isEmpty()) 97 { 98 System.out.println("RemoteControl: no download necessary"); 99 } 100 else 101 { 102 Future<?> future = osmTask.download(false /*no new layer*/, new Bounds(minlat,minlon,maxlat,maxlon), null /* let the task manage the progress monitor */); 103 Main.worker.submit(new PostDownloadHandler(osmTask, future)); 104 } 95 105 } 96 106 } … … 100 110 throw new RequestHandlerErrorException(); 101 111 } 102 if (args.containsKey("select") && Main.pref.getBoolean( "remotecontrol.permission.change-selection", true)) {112 if (args.containsKey("select") && Main.pref.getBoolean(changeSelectionPermissionKey, changeSelectionPermissionDefault)) { 103 113 // select objects after downloading, zoom to selection. 104 114 final String selection = args.get("select"); … … 129 139 for (Relation r : ds.getRelations()) if (relations.contains(r.getId())) newSel.add(r); 130 140 ds.setSelected(newSel); 131 if (Main.pref.getBoolean( "remotecontrol.permission.change-viewport", true))141 if (Main.pref.getBoolean(changeViewportPermissionKey, changeViewportPermissionDefault)) 132 142 new AutoScaleAction("selection").actionPerformed(null); 133 143 } 134 144 }); 135 } else if (Main.pref.getBoolean( "remotecontrol.permission.change-viewport", true)) {145 } else if (Main.pref.getBoolean(changeViewportPermissionKey, changeViewportPermissionDefault)) { 136 146 // after downloading, zoom to downloaded area. 137 final Bounds bounds = new Bounds(new LatLon(minlat, minlon), 138 new LatLon(maxlat, maxlon)); 147 zoom(minlat, maxlat, minlon, maxlon); 148 } 149 } 139 150 140 // make sure this isn't called unless there *is* a MapView 141 // 142 if (Main.map != null && Main.map.mapView != null) { 143 Main.worker.execute(new Runnable() { 144 public void run() { 145 BoundingXYVisitor bbox = new BoundingXYVisitor(); 146 bbox.visit(bounds); 147 Main.map.mapView.recalculateCenterScale(bbox); 148 } 149 }); 150 } 151 protected void zoom(double minlat, double maxlat, double minlon, double maxlon) { 152 final Bounds bounds = new Bounds(new LatLon(minlat, minlon), 153 new LatLon(maxlat, maxlon)); 154 155 // make sure this isn't called unless there *is* a MapView 156 // 157 if (Main.map != null && Main.map.mapView != null) { 158 Main.worker.execute(new Runnable() { 159 public void run() { 160 BoundingXYVisitor bbox = new BoundingXYVisitor(); 161 bbox.visit(bounds); 162 Main.map.mapView.recalculateCenterScale(bbox); 163 } 164 }); 151 165 } 152 166 } -
applications/editors/josm/plugins/remotecontrol/src/org/openstreetmap/josm/plugins/remotecontrol/handler/VersionHandler.java
r22675 r22733 3 3 import static org.openstreetmap.josm.tools.I18n.tr; 4 4 5 import org.openstreetmap.josm.plugins.remotecontrol.PermissionPref ;5 import org.openstreetmap.josm.plugins.remotecontrol.PermissionPrefWithDefault; 6 6 import org.openstreetmap.josm.plugins.remotecontrol.RequestHandler; 7 7 import org.openstreetmap.josm.plugins.remotecontrol.RequestHandlerBadRequestException; … … 14 14 public class VersionHandler extends RequestHandler { 15 15 16 public static final String command = "/version"; 16 public static final String command = "version"; 17 public static final String permissionKey = "remotecontrol.permission.read-protocolversion"; 18 public static final boolean permissionDefault = true; 17 19 18 20 @Override … … 31 33 } 32 34 33 public PermissionPref getPermissionPref() 35 @Override 36 public PermissionPrefWithDefault getPermissionPref() 34 37 { 35 return new PermissionPref ("remotecontrol.permission.read-protocolversion",38 return new PermissionPrefWithDefault(permissionKey, permissionDefault, 36 39 "RemoteControl: /version forbidden by preferences"); 37 40 }
Note:
See TracChangeset
for help on using the changeset viewer.