Changeset 25107 in osm for applications/editors
- Timestamp:
- 2011-01-22T12:18:25+01:00 (14 years ago)
- Location:
- applications/editors/josm/plugins/scripting
- Files:
-
- 2 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/scripting/build.xml
r25073 r25107 27 27 <project name="scripting" default="dist" basedir="."> 28 28 29 <property name="commit.message" value=" Added preferences editor for scripting engines" />29 <property name="commit.message" value="Now caches compiled scripts, if possible. Fixes OutOfMemory when running groovy scripts." /> 30 30 <property name="plugin.main.version" value="3751" /> 31 31 -
applications/editors/josm/plugins/scripting/scripts/AddHouseNumbers.groovy
r25019 r25107 1 1 2 /* 2 3 * This scripts sets a sequence of house numbers on the currently selected nodes. … … 6 7 7 8 import java.awt.BorderLayout; 9 import javax.swing.JComponent; 10 11 import java.awt.event.KeyEvent; 12 13 import javax.swing.KeyStroke; 8 14 9 15 import groovy.swing.SwingBuilder; … … 30 36 import javax.swing.JLabel; 31 37 38 import javax.swing.Action; 32 39 import javax.swing.BorderFactory; 33 40 import javax.swing.JTextField; … … 38 45 39 46 class AddHouseNumberDialog extends JDialog { 47 48 static private AddHouseNumberDialog instance; 49 static def AddHouseNumberDialog getInstance() { 50 if (instance == null){ 51 instance = new AddHouseNumberDialog() 52 } 53 return instance 54 } 40 55 41 56 private JTextField tfStart; … … 52 67 <html> 53 68 Enter the <strong>first house number</strong> to be applied to the currently selected nodes 54 and the amount by which the house number is <strong>incremented</strong>.69 and the <strong>increment</strong> between consecutive house numbers. 55 70 </html> 56 71 """ … … 61 76 SwingBuilder swing = new SwingBuilder() 62 77 return swing.panel(){ 78 emptyBorder([5,5,5,5],parent:true) 63 79 gridBagLayout() 64 80 label(text: "Start:", … … 67 83 ) 68 84 tfStart = textField(constraints: gbc(gridx:1,gridy:0,weightx:1.0, weighty:0.0, fill: GridBagConstraints.HORIZONTAL, insets:[2,2,2,2])) 85 SelectAllOnFocusGainedDecorator.decorate(tfStart) 69 86 label(text: "Increment:", horizontalAlignment: JLabel.LEFT, constraints: gbc(gridx:0,gridy:1,weightx:0.0, weighty:0.0, anchor: GridBagConstraints.WEST, insets:[2,2,2,2])) 70 87 tfIncrement = textField(constraints: gbc(gridx:1,gridy:1,weightx:1.0, weighty:0.0, fill: GridBagConstraints.HORIZONTAL, anchor: GridBagConstraints.WEST, insets:[2,2,2,2])) 88 SelectAllOnFocusGainedDecorator.decorate(tfIncrement) 71 89 panel(constraints: gbc(gridx:0,gridy:2,weightx:1.0, weighty:1.0, gridwidth:2, fill: GridBagConstraints.BOTH, insets:[2,2,2,2])) 90 tfIncrement.text = "2" 72 91 } 73 92 } … … 76 95 SwingBuilder swing = new SwingBuilder() 77 96 return swing.panel(layout: new FlowLayout(FlowLayout.CENTER)) { 97 def actApply = action(name: "Apply", smallIcon: ImageProvider.get("ok"), closure: {apply(); setVisible(false)}) 98 def btnApply = button(action: actApply) 99 btnApply.setFocusable(true) 100 btnApply.registerKeyboardAction(actApply, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0, false), JComponent.WHEN_FOCUSED) 101 78 102 button(text: "Cancel", icon: ImageProvider.get("cancel"), actionPerformed: {setVisible(false)}) 79 button(text: "Apply", icon: ImageProvider.get("ok"), actionPerformed: {80 apply()81 setVisible(false)82 })83 103 } 84 104 } 85 105 86 106 def apply() { 87 107 def start 108 def incr 88 109 try { 89 110 start = tfStart.text.trim().toInteger() 111 incr = tfIncrement.text.trim().toInteger() 90 112 } catch(NumberFormatException e){ 91 113 e.printStackTrace() 92 114 return 93 115 } 94 def incr 95 try { 96 incr = tfIncrement.text.trim().toInteger() 97 } catch(NumberFormatException e){ 98 e.printStackTrace() 99 return 100 } 101 def nodes = getCurrentlySelectedNodes() 102 def cmds = [] 103 nodes.each {Node n -> 116 def cmds = getCurrentlySelectedNodes().collect { Node n -> 104 117 Node nn = new Node(n) 105 118 nn.put("addr:housenumber", start.toString()) 106 119 start += incr 107 cmds <<new ChangeCommand(n, nn)120 return new ChangeCommand(n, nn) 108 121 } 109 122 if (cmds.isEmpty()) return … … 112 125 113 126 def build() { 114 setTitle("Set house numbers")127 title = "Set house numbers" 115 128 def cp = getContentPane() 116 129 cp.setLayout(new BorderLayout()) … … 121 134 122 135 def getCurrentDataSet() { 123 def layer = Main?.map?.mapView?. getActiveLayer()136 def layer = Main?.map?.mapView?.activeLayer 124 137 if (layer == null) return null 125 138 if (! (layer instanceof OsmDataLayer)) return null … … 142 155 } 143 156 144 def dialog = new AddHouseNumberDialog()145 dialog.setVisible(true) 157 AddHouseNumberDialog.instance.setVisible(true) 158 -
applications/editors/josm/plugins/scripting/src/org/openstreetmap/josm/plugins/scripting/RunScriptDialog.java
r25071 r25107 14 14 import java.io.FileReader; 15 15 import java.io.IOException; 16 import java.net.MalformedURLException;17 import java.net.URL;18 import java.net.URLClassLoader;19 16 import java.util.Collections; 20 17 import java.util.LinkedList; 21 18 import java.util.List; 22 19 import java.util.logging.Logger; 23 import java.util.regex.Matcher; 24 import java.util.regex.Pattern; 25 26 import javax.activation.MimetypesFileTypeMap; 20 21 import javax.script.Compilable; 22 import javax.script.CompiledScript; 27 23 import javax.script.ScriptEngine; 28 import javax.script.ScriptEngineFactory;29 import javax.script.ScriptEngineManager;30 24 import javax.script.ScriptException; 31 25 import javax.swing.AbstractAction; … … 46 40 import org.openstreetmap.josm.gui.widgets.HtmlPanel; 47 41 import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator; 42 import org.openstreetmap.josm.plugins.scripting.preferences.PreferenceKeys; 48 43 import org.openstreetmap.josm.tools.ImageProvider; 49 44 import org.openstreetmap.josm.tools.WindowGeometry; … … 53 48 * running a script.</p> 54 49 */ 55 public class RunScriptDialog extends JDialog {50 public class RunScriptDialog extends JDialog implements PreferenceKeys{ 56 51 static private final Logger logger = Logger.getLogger(RunScriptDialog.class.getName()); 57 58 /**59 * <p>The preferences key for the script file history.</p>60 */61 static private final String PREF_KEY_FILE_HISTORY = "scripting.RunScriptDialog.file-history";62 63 /**64 * <p>The preferences key for the last script file name entered in the script file65 * selection field.</p>66 */67 static private final String PREF_KEY_LAST_FILE = "scripting.RunScriptDialog.last-file";68 52 69 53 /** the input field for the script file name */ … … 227 211 HelpAwareOptionPane.showOptionDialog( 228 212 RunScriptDialog.this, 229 tr("The script file ''{0}'' isn' t readable.", f.toString()),213 tr("The script file ''{0}'' isn''t readable.", f.toString()), 230 214 tr("File not readable"), 231 215 JOptionPane.ERROR_MESSAGE, … … 263 247 "<html>" 264 248 + tr( 265 "<p>The script can''t be executed, beca sue there are currently no scripting engines installed.</p>"249 "<p>The script can''t be executed, because there are currently no scripting engines installed.</p>" 266 250 + "<p>Refer to the online help for information about how to install a scripting engine with JOSM.</p>" 267 251 ) … … 308 292 final ScriptEngine engine = getScriptEngine(f); 309 293 if (engine == null) return; 294 310 295 SwingUtilities.invokeLater( 311 296 new Runnable() { … … 313 298 FileReader reader = null; 314 299 try { 315 reader = new FileReader(f); 316 engine.eval(reader); 300 if (engine instanceof Compilable) { 301 CompiledScript script = CompiledScriptCache.getInstance().compile((Compilable)engine,f); 302 logger.info("running compiled script for " + f); 303 script.eval(); 304 } else { 305 reader = new FileReader(f); 306 engine.eval(reader); 307 } 317 308 } catch(ScriptException e){ 318 309 warnExecutingScriptFailed(e); … … 327 318 } 328 319 ); 320 329 321 setVisible(false); 330 322 } -
applications/editors/josm/plugins/scripting/src/org/openstreetmap/josm/plugins/scripting/ScriptEngineProvider.java
r25071 r25107 7 7 import java.io.IOException; 8 8 import java.io.InputStream; 9 import java.lang.reflect.Field; 9 10 import java.net.MalformedURLException; 10 11 import java.net.URL; … … 44 45 private final List<File> scriptEngineJars = new ArrayList<File>(); 45 46 private MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap(); 47 private ClassLoader scriptClassLoader = getClass().getClassLoader(); 46 48 47 49 protected void loadMimeTypesMap() { … … 81 83 scriptEngineJars.add(new File(jar)); 82 84 } 85 scriptClassLoader = buildClassLoader(); 83 86 } 84 87 … … 151 154 */ 152 155 public ScriptEngine getEngineByName(String name) { 153 ScriptEngineManager mgr = new ScriptEngineManager( buildClassLoader());156 ScriptEngineManager mgr = new ScriptEngineManager(scriptClassLoader); 154 157 return mgr.getEngineByName(name); 155 158 } … … 201 204 } 202 205 } 206 buildClassLoader(); 203 207 loadScriptEngineFactories(); 204 208 fireContentsChanged(this, 0, scriptEngineJars.size()); -
applications/editors/josm/plugins/scripting/src/org/openstreetmap/josm/plugins/scripting/ScriptEngineSelectionDialog.java
r25071 r25107 13 13 import java.awt.event.MouseAdapter; 14 14 import java.awt.event.MouseEvent; 15 import java.util.List;16 15 17 16 import javax.script.ScriptEngine; 18 import javax.script.ScriptEngineFactory;19 17 import javax.swing.AbstractAction; 20 18 import javax.swing.BorderFactory; … … 22 20 import javax.swing.JComponent; 23 21 import javax.swing.JDialog; 24 import javax.swing.JLabel;25 22 import javax.swing.JList; 26 23 import javax.swing.JOptionPane; 27 24 import javax.swing.JPanel; 28 25 import javax.swing.KeyStroke; 29 import javax.swing.ListCellRenderer;30 26 import javax.swing.ListSelectionModel; 31 import javax.swing.UIManager;32 27 import javax.swing.event.ListSelectionEvent; 33 28 import javax.swing.event.ListSelectionListener; … … 38 33 import org.openstreetmap.josm.gui.help.HelpUtil; 39 34 import org.openstreetmap.josm.gui.widgets.HtmlPanel; 35 import org.openstreetmap.josm.plugins.scripting.ui.ScriptEngineCellRenderer; 40 36 import org.openstreetmap.josm.tools.ImageProvider; 41 37 import org.openstreetmap.josm.tools.WindowGeometry; 42 import org.openstreetmap.josm.plugins.scripting.ui.ScriptEngineCellRenderer;43 38 44 39 /** -
applications/editors/josm/plugins/scripting/src/org/openstreetmap/josm/plugins/scripting/preferences/PreferenceKeys.java
r25071 r25107 9 9 */ 10 10 String PREF_KEY_SCRIPTING_ENGINE_JARS = "scripting.engine-jars"; 11 11 12 /** 13 * <p>The preferences key for the script file history.</p> 14 */ 15 String PREF_KEY_FILE_HISTORY = "scripting.RunScriptDialog.file-history"; 16 17 /** 18 * <p>The preferences key for the last script file name entered in the script file 19 * selection field.</p> 20 */ 21 String PREF_KEY_LAST_FILE = "scripting.RunScriptDialog.last-file"; 12 22 }
Note:
See TracChangeset
for help on using the changeset viewer.