Changeset 29228 in osm for applications
- Timestamp:
- 2013-02-04T14:58:23+01:00 (12 years ago)
- Location:
- applications/editors/josm/plugins/roadsigns
- Files:
-
- 680 added
- 2 edited
- 71 moved
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/roadsigns/src/org/openstreetmap/josm/plugins/roadsigns/RoadSignInputDialog.java
r23192 r29228 14 14 import java.awt.event.MouseAdapter; 15 15 import java.awt.event.MouseEvent; 16 import java.io.IOException; 16 17 import java.util.ArrayList; 17 18 import java.util.Collection; … … 24 25 import java.util.TreeMap; 25 26 27 import javax.swing.AbstractAction; 28 import javax.swing.Action; 26 29 import javax.swing.BorderFactory; 30 import javax.swing.Box; 31 import javax.swing.JButton; 27 32 import javax.swing.JCheckBox; 33 import javax.swing.JComboBox; 28 34 import javax.swing.JComponent; 29 35 import javax.swing.JEditorPane; … … 31 37 import javax.swing.JPanel; 32 38 import javax.swing.JScrollPane; 39 import javax.swing.JTabbedPane; 33 40 import javax.swing.JTable; 34 41 import javax.swing.JTextField; … … 52 59 import org.openstreetmap.josm.gui.MultiSplitLayout; 53 60 import org.openstreetmap.josm.gui.MultiSplitPane; 61 import org.openstreetmap.josm.plugins.roadsigns.RoadSignsPlugin.PresetMetaData; 54 62 import org.openstreetmap.josm.plugins.roadsigns.Sign.SignParameter; 55 63 import org.openstreetmap.josm.plugins.roadsigns.Sign.Tag; … … 72 80 */ 73 81 class RoadSignInputDialog extends ExtendedDialog { 74 protected finalSignSelection sel;75 protected finalList<Sign> signs;82 protected SignSelection sel; 83 protected List<Sign> signs; 76 84 protected JTable previewTable; 77 85 protected JCheckBox addTrafficSignTag; … … 84 92 protected JScrollPane scrollInfo; 85 93 86 public RoadSignInputDialog( List<Sign> signs) {94 public RoadSignInputDialog() { 87 95 super(Main.parent, tr("Road Sign Plugin"), new String[] {tr("OK"), tr("Cancel")}, false /* modal */); 88 this.signs = signs;96 this.signs = RoadSignsPlugin.signs; 89 97 sel = new SignSelection(); 90 98 setButtonIcons(new String[] { "ok.png", "cancel.png" }); 91 setContent(buildPanel(), false); 99 final JTabbedPane tabs = new JTabbedPane(); 100 tabs.add(tr("signs"), buildSignsPanel()); 101 Action updateAction = new AbstractAction() { 102 @Override 103 public void actionPerformed(ActionEvent e) { 104 RoadSignInputDialog.this.signs = RoadSignsPlugin.signs; 105 sel = new SignSelection(); 106 tabs.setComponentAt(0, buildSignsPanel()); 107 } 108 }; 109 tabs.add(tr("settings"), new SettingsPanel(false, updateAction)); 110 setContent(tabs, false); 92 111 } 93 112 … … 96 115 if (i == 0) { // OK Button 97 116 Collection<OsmPrimitive> selPrim = Main.main.getCurrentDataSet().getSelected(); 98 if ( selPrim.size() != 0) {117 if (!selPrim.isEmpty()) { 99 118 Main.pref.put("plugin.roadsigns.addTrafficSignTag", addTrafficSignTag.isSelected()); 100 119 … … 115 134 cmds.add(new ChangePropertyCommand(selPrim, key, value)); 116 135 } 117 if (cmds. size() == 0)136 if (cmds.isEmpty()) 118 137 return null; 119 138 else if (cmds.size() == 1) … … 123 142 } 124 143 125 private JComponent build Panel() {144 private JComponent buildSignsPanel() { 126 145 String layoutDef = 127 146 "(COLUMN "+ … … 162 181 if (e == null || e.getURL() == null) 163 182 return; 164 System. err.println(e.getURL());183 System.out.println(e.getURL()); 165 184 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { 166 185 OpenBrowser.displayUrl(e.getURL().toString()); … … 263 282 private Pair<Integer, Integer> findIndex(SignWrapper swFind) { 264 283 int selIdx=0; 265 int combIdx=0;266 284 for (SignCombination sc : combos) { 267 combIdx=0;285 int combIdx=0; 268 286 for (SignWrapper sw : sc.signs) { 269 287 if (swFind == sw) { … … 717 735 718 736 if (sign.wiki != null || sign.loc_wiki != null) { 719 String wikiPrefix = Main. main.pref.get("plugin.roadsigns.wikiprefix", "http://wiki.openstreetmap.org/wiki/");737 String wikiPrefix = Main.pref.get("plugin.roadsigns.wikiprefix", "http://wiki.openstreetmap.org/wiki/"); 720 738 txt.append("<p>"); 721 739 if (sign.loc_wiki != null) { … … 791 809 } 792 810 } 811 812 public static class SettingsPanel extends JPanel { 813 814 private List<PresetMetaData> presetsData; 815 private JComboBox selectionBox; 816 817 public SettingsPanel(boolean standalone, final Action update) { 818 super(new GridBagLayout()); 819 presetsData = RoadSignsPlugin.getAvailablePresetsMetaData(); 820 821 selectionBox = new JComboBox(presetsData.toArray()); 822 String code = Main.pref.get("plugin.roadsigns.preset.selection", null); 823 if (code != null) { 824 for (PresetMetaData data : presetsData) { 825 if (code.equals(data.code)) { 826 selectionBox.setSelectedItem(data); 827 } 828 } 829 } 830 this.add(new JLabel(tr("Country preset:")), GBC.std().insets(5, 5, 5, 5)); 831 this.add(selectionBox, GBC.eol().insets(0, 5, 5, 5)); 832 if (!standalone) { 833 JButton apply = new JButton(new AbstractAction(tr("Apply")) { 834 @Override 835 public void actionPerformed(ActionEvent e) { 836 try { 837 apply(); 838 } catch (IOException ex) { 839 return; 840 } 841 update.actionPerformed(null); 842 } 843 }); 844 this.add(apply, GBC.eol().insets(5, 0, 5, 5)); 845 } 846 this.add(Box.createVerticalGlue(), GBC.eol().fill()); 847 } 848 849 public void apply() throws IOException { 850 RoadSignsPlugin.setSelectedPreset(presetsData.get(selectionBox.getSelectedIndex())); 851 } 852 } 853 793 854 } -
applications/editors/josm/plugins/roadsigns/src/org/openstreetmap/josm/plugins/roadsigns/RoadSignsPlugin.java
r27868 r29228 8 8 import java.awt.event.ActionEvent; 9 9 import java.awt.event.KeyEvent; 10 import java.io.File; 10 11 import java.io.FileInputStream; 11 12 import java.io.IOException; 12 13 import java.io.InputStream; 14 import java.net.MalformedURLException; 13 15 import java.net.URL; 14 16 import java.util.ArrayList; 15 import java.util.Collections; 17 import java.util.Arrays; 18 import java.util.Collection; 16 19 import java.util.List; 17 20 … … 24 27 import org.openstreetmap.josm.Main; 25 28 import org.openstreetmap.josm.actions.JosmAction; 29 import org.openstreetmap.josm.data.Preferences.pref; 30 import org.openstreetmap.josm.gui.ExtendedDialog; 26 31 import org.openstreetmap.josm.gui.dialogs.properties.PropertiesDialog; 27 32 import org.openstreetmap.josm.io.MirroredInputStream; 28 33 import org.openstreetmap.josm.plugins.Plugin; 29 34 import org.openstreetmap.josm.plugins.PluginInformation; 35 import org.openstreetmap.josm.plugins.roadsigns.RoadSignInputDialog.SettingsPanel; 30 36 import org.openstreetmap.josm.tools.Shortcut; 37 import org.openstreetmap.josm.tools.Utils; 31 38 32 39 public class RoadSignsPlugin extends Plugin { 33 private static boolean presetsLoaded = false;40 static PresetMetaData selectedPreset; 34 41 public static List<Sign> signs; 35 static List<String> iconDirs; 42 public static List<String> iconDirs; 43 44 public final static PresetMetaData PRESET_DE = new PresetMetaData("DE", tr("Germany"), "resource://data/roadsignpresetDE.xml", "resource://images/DE/"); 45 public final static PresetMetaData PRESET_PL = new PresetMetaData("PL", tr("Poland"), "resource://data/roadsignpresetPL.xml", "resource://images/PL/"); 46 public final static PresetMetaData PRESET_SK = new PresetMetaData("SK", tr("Slovakia"), "resource://data/roadsignpresetSK.xml", "resource://images/SK/"); 47 public final static Collection<PresetMetaData> DEFAULT_PRESETS = Arrays.asList(PRESET_DE, PRESET_PL, PRESET_SK); 36 48 37 49 public RoadSignsPlugin(PluginInformation info) { … … 56 68 57 69 public void actionPerformed(ActionEvent e) { 58 loadSignPresets(); 59 RoadSignInputDialog input = new RoadSignInputDialog(signs); 70 String code = Main.pref.get("plugin.roadsigns.preset.selection", null); 71 if (code == null) { 72 ExtendedDialog ed = new ExtendedDialog(Main.parent, tr("Settings"), new String[] { tr("Ok"), tr("Cancel") }); 73 ed.setButtonIcons(new String[] {"ok", "cancel"}); 74 SettingsPanel settings = new SettingsPanel(true, null); 75 ed.setContent(settings); 76 ed.showDialog(); 77 if (ed.getValue() != 1) return; 78 try { 79 settings.apply(); 80 } catch (IOException ex) { 81 return; 82 } 83 } 84 try { 85 loadSignPreset(); 86 } catch (IOException ex) { 87 return; 88 } 89 RoadSignInputDialog input = new RoadSignInputDialog(); 60 90 input.showDialog(); 61 91 } … … 63 93 } 64 94 65 protected static void loadSignPresets() { 66 if (presetsLoaded) 67 return; 68 presetsLoaded=true; 69 List<String> files = new ArrayList<String>( 70 Main.pref.getCollection("plugin.roadsigns.sources", 71 Collections.<String>singletonList("resource://data/defaultroadsignpreset.xml"))); 72 iconDirs = new ArrayList<String>( 73 Main.pref.getCollection("plugin.roadsigns.icon.sources", Collections.<String>emptySet())); 74 75 if (Main.pref.getBoolean("plugin.roadsigns.use_default_icon_source", true)) { 76 iconDirs.add("resource://images/"); 77 } 78 79 for (String source : files) { 80 try { 81 InputStream in = getInputStream(source); 82 RoadSignsReader reader = new RoadSignsReader(in); 83 signs = reader.parse(); 84 85 } catch (IOException ex) { 86 ex.printStackTrace(); 87 JOptionPane.showMessageDialog( 88 Main.parent, 89 tr("Could not read tagging preset source: ''{0}''",source), 90 tr("Error"), 91 JOptionPane.ERROR_MESSAGE 92 ); 93 } catch (SAXException ex) { 94 ex.printStackTrace(); 95 JOptionPane.showMessageDialog( 96 Main.parent, 97 tr("Error parsing tagging preset from ''{0}'':\n", source)+ex.getMessage(), 98 tr("Error"), 99 JOptionPane.ERROR_MESSAGE 100 ); 101 } 95 public static class PresetMetaData { 96 @pref public String code; 97 @pref public String display_name; 98 @pref public String preset_path; 99 @pref public String icon_path; 100 101 public PresetMetaData() { 102 } 103 104 public PresetMetaData(String country_code, String display_name, String preset_path, String icons_path) { 105 this.code = country_code; 106 this.display_name = display_name; 107 this.preset_path = preset_path; 108 this.icon_path = icons_path; 109 } 110 111 @Override 112 public String toString() { 113 return display_name; 114 } 115 } 116 117 public static void setSelectedPreset(PresetMetaData preset) throws IOException { 118 Main.pref.put("plugin.roadsigns.preset.selection", preset.code); 119 loadSignPreset(); 120 } 121 122 public static List<PresetMetaData> getAvailablePresetsMetaData() { 123 124 List<PresetMetaData> presetsData = Main.pref.getListOfStructs("plugin.roadsigns.presets", DEFAULT_PRESETS, PresetMetaData.class); 125 126 String customFile = Main.pref.get("plugin.roadsigns.sources", null); 127 if (customFile == null) { 128 // for legacy reasons, try both string and collection preference type 129 Collection<String> customFiles = Main.pref.getCollection("plugin.roadsigns.sources", null); 130 if (customFiles != null && !customFiles.isEmpty()) { 131 customFile = customFiles.iterator().next(); 132 } 133 } 134 135 if (customFile != null) { 136 // first check, if custom file preference has changed. If yes, 137 // change the current preset selection to custom directly 138 String lastCustomFile = Main.pref.get("plugin.roadsigns.sources.last", null); 139 if (!Utils.equal(customFile, lastCustomFile)) { 140 Main.pref.put("plugin.roadsigns.sources.last", customFile); 141 Main.pref.put("plugin.roadsigns.preset.selection", "custom"); 142 } 143 144 String customIconDirsStr = Main.pref.get("plugin.roadsigns.icon.sources", null); 145 Collection<String> customIconDirs = null; 146 if (customIconDirsStr != null) { 147 customIconDirs = new ArrayList<String>(Arrays.asList(customIconDirsStr.split(","))); 148 } else { 149 customIconDirs = Main.pref.getCollection("plugin.roadsigns.icon.sources", null); 150 } 151 if (customIconDirs != null) { 152 customIconDirs = new ArrayList<String>(customIconDirs); 153 } else { 154 customIconDirs = new ArrayList<String>(); 155 } 156 // add icon directory relative to preset file 157 if (!customFile.startsWith("resource:")) { 158 String parentDir = null; 159 try { 160 URL url = new URL(customFile); 161 parentDir = url.getPath(); 162 } catch (MalformedURLException ex) { 163 File f = new File(customFile); 164 parentDir = f.getParent(); 165 } 166 if (parentDir != null && !parentDir.isEmpty()) { 167 customIconDirs.add(parentDir); 168 } 169 } 170 if (Main.pref.getBoolean("plugin.roadsigns.use_default_icon_source", true)) { 171 customIconDirs.add("resource://images/"); 172 } 173 PresetMetaData custom = new PresetMetaData("custom", tr("custom"), customFile, Utils.join(",", customIconDirs)); 174 presetsData.add(custom); 175 } else { 176 Main.pref.put("plugin.roadsigns.sources.last", null); 177 } 178 179 return presetsData; 180 } 181 182 protected static void loadSignPreset() throws IOException { 183 List<PresetMetaData> presetsData = getAvailablePresetsMetaData(); 184 String code = Main.pref.get("plugin.roadsigns.preset.selection", null); 185 if (selectedPreset != null && selectedPreset.code.equals(code)) return; 186 187 for (PresetMetaData data : presetsData) { 188 if (data.code.equals(code)) { 189 selectedPreset = data; 190 break; 191 } 192 } 193 if (selectedPreset == null) { 194 if (!presetsData.isEmpty()) { 195 selectedPreset = presetsData.get(0); 196 } else { 197 selectedPreset = PRESET_DE; 198 } 199 } 200 iconDirs = Arrays.asList(selectedPreset.icon_path.split(",")); 201 String source = selectedPreset.preset_path; 202 203 try { 204 InputStream in = getInputStream(source); 205 RoadSignsReader reader = new RoadSignsReader(in); 206 signs = reader.parse(); 207 208 } catch (IOException ex) { 209 ex.printStackTrace(); 210 JOptionPane.showMessageDialog( 211 Main.parent, 212 tr("Could not read tagging preset source: ''{0}''",source), 213 tr("Error"), 214 JOptionPane.ERROR_MESSAGE 215 ); 216 throw ex; 217 } catch (SAXException ex) { 218 ex.printStackTrace(); 219 JOptionPane.showMessageDialog( 220 Main.parent, 221 tr("Error parsing tagging preset from ''{0}'':\n", source)+ex.getMessage(), 222 tr("Error"), 223 JOptionPane.ERROR_MESSAGE 224 ); 225 throw new IOException(ex); 102 226 } 103 227 }
Note:
See TracChangeset
for help on using the changeset viewer.