Changeset 25071 in osm for applications/editors/josm/plugins/scripting/src
- Timestamp:
- 2011-01-16T18:25:29+01:00 (14 years ago)
- Location:
- applications/editors/josm/plugins/scripting/src/org/openstreetmap/josm/plugins/scripting
- Files:
-
- 12 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/scripting/src/org/openstreetmap/josm/plugins/scripting/RunScriptDialog.java
r25019 r25071 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; 16 19 import java.util.Collections; 17 20 import java.util.LinkedList; … … 175 178 */ 176 179 String currentFile = cbScriptFile.getText(); 177 Main.pref.put(PREF_KEY_ FILE_HISTORY, currentFile.trim());178 Main.pref.putCollection(PREF_KEY_ LAST_FILE, cbScriptFile.getHistory());180 Main.pref.put(PREF_KEY_LAST_FILE, currentFile.trim()); 181 Main.pref.putCollection(PREF_KEY_FILE_HISTORY, cbScriptFile.getHistory()); 179 182 } 180 183 super.setVisible(visible); … … 272 275 273 276 protected ScriptEngine getScriptEngine(File file) { 274 ScriptEngine Manager mgr = newScriptEngineManager(getClass().getClassLoader());275 Matcher matcher = Pattern.compile("\\.([^\\.]+)$").matcher(file.toString());277 ScriptEngine engine = ScriptEngineProvider.getInstance().getEngineForFile(file); 278 if (engine != null) return engine; 276 279 277 // can we derive a suitable script engine from the file name?278 MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();279 // FIXME: provide a resources file for script mime types; provide an editor280 // for these mappings in the preferences281 mimeTypesMap.addMimeTypes("application/x-groovy groovy");282 String mt = mimeTypesMap.getContentType(file);283 if (mt != null){284 ScriptEngine engine = mgr.getEngineByMimeType(mt);285 if (engine != null) return engine;286 }287 288 // no script engine at all? Abort.289 List<ScriptEngineFactory> factories = mgr.getEngineFactories();290 if (factories.isEmpty()) {291 warnNoScriptingEnginesInstalled();292 return null;293 }294 280 // let the user select a script engine 295 281 return ScriptEngineSelectionDialog.select(RunScriptDialog.this); -
applications/editors/josm/plugins/scripting/src/org/openstreetmap/josm/plugins/scripting/ScriptEngineSelectionDialog.java
r25019 r25071 13 13 import java.awt.event.MouseAdapter; 14 14 import java.awt.event.MouseEvent; 15 import java.util.ArrayList;16 import java.util.Collections;17 import java.util.Comparator;18 15 import java.util.List; 19 16 20 17 import javax.script.ScriptEngine; 21 18 import javax.script.ScriptEngineFactory; 22 import javax.script.ScriptEngineManager;23 19 import javax.swing.AbstractAction; 24 import javax.swing.AbstractListModel;25 20 import javax.swing.BorderFactory; 26 21 import javax.swing.JButton; … … 45 40 import org.openstreetmap.josm.tools.ImageProvider; 46 41 import org.openstreetmap.josm.tools.WindowGeometry; 42 import org.openstreetmap.josm.plugins.scripting.ui.ScriptEngineCellRenderer; 47 43 48 44 /** … … 81 77 private JButton btnOK; 82 78 private ScriptEngine selectedEngine; 83 private ScriptEngine ListModelmodel;79 private ScriptEngineProvider model; 84 80 private OKAction actOK; 85 81 … … 137 133 protected JPanel buildScriptEngineListPanel() { 138 134 JPanel pnl = new JPanel(new BorderLayout()); 139 lstEngines = new JList(model = newScriptEngineListModel());135 lstEngines = new JList(model = ScriptEngineProvider.getInstance()); 140 136 lstEngines.setCellRenderer(new ScriptEngineCellRenderer()); 141 137 pnl.add(lstEngines, BorderLayout.CENTER); … … 222 218 super.setVisible(visible); 223 219 } 224 225 /**226 * <p>Provides a list model for the list of available script engines.</p>227 */228 private static class ScriptEngineListModel extends AbstractListModel {229 230 private List<ScriptEngineFactory> factories;231 232 public ScriptEngineListModel(){233 ScriptEngineManager mgr = new ScriptEngineManager(getClass().getClassLoader());234 factories = new ArrayList<ScriptEngineFactory>(mgr.getEngineFactories());235 Collections.sort(factories,236 new Comparator<ScriptEngineFactory>() {237 @Override238 public int compare(ScriptEngineFactory f1, ScriptEngineFactory f2) {239 return f1.getEngineName().compareTo(f2.getEngineName());240 }241 }242 );243 }244 245 /**246 * <p>Replies a script engine created by the i-th script engine factory.</p>247 *248 * @param i the index249 * @return the engine250 */251 public ScriptEngine getScriptEngine(int i){252 ScriptEngine engine = factories.get(i).getScriptEngine();253 return engine;254 }255 256 @Override257 public Object getElementAt(int i) {258 return factories.get(i);259 }260 261 @Override262 public int getSize() {263 return factories.size();264 }265 }266 267 /**268 * <p>Implements a list cell renderer for the list of scripting engines.</p>269 *270 */271 private static class ScriptEngineCellRenderer implements ListCellRenderer {272 273 private final JLabel lbl = new JLabel();274 275 protected String getDisplayName(ScriptEngineFactory factory){276 return tr("{1} (with engine {0})", factory.getEngineName(), factory.getLanguageName());277 }278 279 protected String getTooltipText(ScriptEngineFactory factory){280 StringBuilder sb = new StringBuilder();281 sb.append("<html>");282 sb.append("<strong>").append(tr("Name:")).append("</strong> ").append(factory.getEngineName()).append("<br>");283 sb.append("<strong>").append(tr("Version:")).append("</strong> ").append(factory.getEngineVersion()).append("<br>");284 sb.append("<strong>").append(tr("Language:")).append("</strong> ").append(factory.getLanguageName()).append("<br>");285 sb.append("<strong>").append(tr("Language version:")).append("</strong> ").append(factory.getLanguageVersion()).append("<br>");286 sb.append("<strong>").append(tr("MIME-Types:")).append("</strong> ");287 List<String> types = factory.getMimeTypes();288 for(int i=0; i<types.size(); i++){289 if (i > 0 )sb.append(", ");290 sb.append(types.get(i));291 }292 sb.append("<br>");293 sb.append("</html>");294 295 return sb.toString();296 }297 298 protected void renderColors(boolean selected){299 if (!selected){300 lbl.setForeground(UIManager.getColor("List.foreground"));301 lbl.setBackground(UIManager.getColor("List.background"));302 } else {303 lbl.setForeground(UIManager.getColor("List.selectionForeground"));304 lbl.setBackground(UIManager.getColor("List.selectionBackground"));305 }306 }307 308 public ScriptEngineCellRenderer() {309 lbl.setOpaque(true);310 lbl.setBorder(BorderFactory.createEmptyBorder(1, 3, 1, 3));311 lbl.setIcon(ImageProvider.get("script-engine"));312 }313 314 @Override315 public Component getListCellRendererComponent(JList list, Object obj,int index, boolean isSelected, boolean cellHasFocus) {316 ScriptEngineFactory factory = (ScriptEngineFactory)obj;317 renderColors(isSelected);318 lbl.setText(getDisplayName(factory));319 lbl.setToolTipText(getTooltipText(factory));320 return lbl;321 }322 }323 220 } -
applications/editors/josm/plugins/scripting/src/org/openstreetmap/josm/plugins/scripting/ScriptingPlugin.java
r25019 r25071 1 1 package org.openstreetmap.josm.plugins.scripting; 2 2 3 import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 4 5 6 import java.awt.event.KeyEvent; 7 5 8 import javax.swing.JMenu; 9 import javax.swing.JSeparator; 6 10 7 11 import org.openstreetmap.josm.Main; 12 import org.openstreetmap.josm.gui.preferences.PreferenceSetting; 8 13 import org.openstreetmap.josm.plugins.Plugin; 9 14 import org.openstreetmap.josm.plugins.PluginInformation; 15 import org.openstreetmap.josm.plugins.scripting.preferences.ConfigureAction; 16 import org.openstreetmap.josm.plugins.scripting.preferences.PreferenceEditor; 10 17 11 18 public class ScriptingPlugin extends Plugin { … … 14 21 15 22 public static ScriptingPlugin getInstance() { 16 return instance; 17 23 return instance; 18 24 } 19 25 … … 26 32 protected void installScriptsMenu(){ 27 33 JMenu mnuMacro; 28 Main.main.menu.add (mnuMacro = new JMenu(tr("Scripts")));34 mnuMacro = Main.main.menu.addMenu(tr("Scripting"), KeyEvent.VK_S, Main.main.menu.defaultMenuPos, ht("/Plugin/Scripting")); 29 35 mnuMacro.setMnemonic('S'); 30 mnuMacro.add(new RunScriptAction()); 36 mnuMacro.add(new RunScriptAction()); 37 mnuMacro.add(new JSeparator()); 38 mnuMacro.add(new ConfigureAction()); 39 } 40 41 @Override 42 public PreferenceSetting getPreferenceSetting() { 43 return new PreferenceEditor(); 31 44 } 32 45 }
Note:
See TracChangeset
for help on using the changeset viewer.