Changeset 23189 in osm for applications/editors/josm/plugins/multipoly-convert
- Timestamp:
- 2010-09-15T18:53:09+02:00 (14 years ago)
- Location:
- applications/editors/josm/plugins/multipoly-convert/src/converttomultipoly
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/multipoly-convert/src/converttomultipoly/MultipolyAction.java
r20416 r23189 27 27 /** 28 28 * Convert an area into an advance multipolygon. 29 * 29 * 30 30 * New relation with type=multipolygon is created for each ways. 31 * 31 * 32 32 * All the tags (except the source tag) will be moved into the relation. 33 33 */ … … 35 35 public class MultipolyAction extends JosmAction { 36 36 37 38 39 40 41 42 43 37 public MultipolyAction() { 38 super(tr("Convert to multipolygon"), "multipoly_convert", 39 tr("Convert to multipolygon."), Shortcut.registerShortcut( 40 "tools:multipolyconv", tr("Tool: {0}", 41 tr("Convert to multipolygon")), KeyEvent.VK_M, 42 Shortcut.GROUP_EDIT, Shortcut.SHIFT_DEFAULT), true); 43 } 44 44 45 46 47 * 48 49 50 51 45 /** 46 * The action button has been clicked 47 * 48 * @param e 49 * Action Event 50 */ 51 public void actionPerformed(ActionEvent e) { 52 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 53 // Get all ways in some type=multipolygon relation 54 HashSet<OsmPrimitive> relationsInMulti = new HashSet<OsmPrimitive>(); 55 for (Relation r : Main.main.getCurrentDataSet().getRelations()) { 56 if (!r.isUsable()) 57 continue; 58 if (r.get("type") != "multipolygon") 59 continue; 60 for (RelationMember rm : r.getMembers()) { 61 OsmPrimitive m = rm.getMember(); 62 if (m instanceof Way) { 63 relationsInMulti.add(m); 64 } 65 } 66 } 67 67 68 // List of selected ways 69 ArrayList<Way> selectedWays = new ArrayList<Way>(); 70 68 // List of selected ways 69 ArrayList<Way> selectedWays = new ArrayList<Way>(); 71 70 72 // For every selected way73 for (OsmPrimitive osm : Main.main.getCurrentDataSet().getSelected()) {74 if (osm instanceof Way) {75 Way way = (Way) osm;76 // Check if way is already in another multipolygon77 if (relationsInMulti.contains(osm)) {78 JOptionPane79 .showMessageDialog(80 Main.parent,81 tr("One of the selected ways is already part of another multipolygon."));82 return;83 }84 71 85 selectedWays.add(way); 86 } 87 } 72 // For every selected way 73 for (OsmPrimitive osm : Main.main.getCurrentDataSet().getSelected()) { 74 if (osm instanceof Way) { 75 Way way = (Way) osm; 76 // Check if way is already in another multipolygon 77 if (relationsInMulti.contains(osm)) { 78 JOptionPane 79 .showMessageDialog( 80 Main.parent, 81 tr("One of the selected ways is already part of another multipolygon.")); 82 return; 83 } 88 84 89 if (Main.map == null) { 90 JOptionPane.showMessageDialog(Main.parent, tr("No data loaded.")); 91 return; 92 } 85 selectedWays.add(way); 86 } 87 } 93 88 94 Collection<Command> cmds = new LinkedList<Command>(); 95 // Add ways to it 96 for (int i = 0; i < selectedWays.size(); i++) { 97 Way way = selectedWays.get(i); 89 if (Main.map == null) { 90 JOptionPane.showMessageDialog(Main.parent, tr("No data loaded.")); 91 return; 92 } 98 93 99 // Create new relation 100 Relation rel = new Relation(); 101 rel.put("type", "multipolygon"); 94 Collection<Command> cmds = new LinkedList<Command>(); 95 // Add ways to it 96 for (int i = 0; i < selectedWays.size(); i++) { 97 Way way = selectedWays.get(i); 102 98 103 RelationMember rm = new RelationMember("outer", way); 104 rel.addMember(rm); 105 106 for (Iterator<String> keyi = way.getKeys().keySet().iterator() ; keyi.hasNext() ; ) { 107 String key = keyi.next(); 108 rel.put(key, way.get(key)); 109 System.out.println(key); 110 if (key.compareTo("source") != 0) { 111 way.remove(key); 112 } 113 } 114 // Add relation 115 cmds.add(new AddCommand(rel)); 116 } 117 // Commit 118 Main.main.undoRedo.add(new SequenceCommand(tr("Create multipolygon"), cmds)); 119 Main.map.repaint(); 120 } 99 // Create new relation 100 Relation rel = new Relation(); 101 rel.put("type", "multipolygon"); 121 102 122 /** Enable this action only if something is selected */ 123 @Override 124 protected void updateEnabledState() { 125 if (getCurrentDataSet() == null) { 126 setEnabled(false); 127 } else { 128 updateEnabledState(getCurrentDataSet().getSelected()); 129 } 130 } 103 RelationMember rm = new RelationMember("outer", way); 104 rel.addMember(rm); 131 105 132 /** Enable this action only if something is selected */ 133 @Override 134 protected void updateEnabledState( 135 Collection<? extends OsmPrimitive> selection) { 136 setEnabled(selection != null && !selection.isEmpty()); 137 } 106 for (Iterator<String> keyi = way.getKeys().keySet().iterator() ; keyi.hasNext() ; ) { 107 String key = keyi.next(); 108 rel.put(key, way.get(key)); 109 System.out.println(key); 110 if (key.compareTo("source") != 0) { 111 way.remove(key); 112 } 113 } 114 // Add relation 115 cmds.add(new AddCommand(rel)); 116 } 117 // Commit 118 Main.main.undoRedo.add(new SequenceCommand(tr("Create multipolygon"), cmds)); 119 Main.map.repaint(); 120 } 121 122 /** Enable this action only if something is selected */ 123 @Override 124 protected void updateEnabledState() { 125 if (getCurrentDataSet() == null) { 126 setEnabled(false); 127 } else { 128 updateEnabledState(getCurrentDataSet().getSelected()); 129 } 130 } 131 132 /** Enable this action only if something is selected */ 133 @Override 134 protected void updateEnabledState( 135 Collection<? extends OsmPrimitive> selection) { 136 setEnabled(selection != null && !selection.isEmpty()); 137 } 138 138 } -
applications/editors/josm/plugins/multipoly-convert/src/converttomultipoly/MultipolyPlugin.java
r20416 r23189 15 15 public class MultipolyPlugin extends Plugin { 16 16 17 17 protected String name; 18 18 19 19 // public MultipolyPlugin(PluginInformation info) { 20 20 public MultipolyPlugin(PluginInformation info) { 21 22 23 24 25 26 27 28 29 30 21 super(info); 22 name = tr("Convert to multipolygon"); 23 JMenu toolsMenu = null; 24 for (int i = 0; i < Main.main.menu.getMenuCount() && toolsMenu == null; i++) { 25 JMenu menu = Main.main.menu.getMenu(i); 26 String name = menu.getText(); 27 if (name != null && name.equals(tr("Tools"))) { 28 toolsMenu = menu; 29 } 30 } 31 31 32 33 34 35 36 37 38 39 40 32 if (toolsMenu == null) { 33 toolsMenu = new JMenu(name); 34 toolsMenu.add(new JMenuItem(new MultipolyAction())); 35 Main.main.menu.add(toolsMenu, 2); 36 } else { 37 toolsMenu.addSeparator(); 38 toolsMenu.add(new JMenuItem(new MultipolyAction())); 39 } 40 } 41 41 }
Note:
See TracChangeset
for help on using the changeset viewer.