Changeset 23885 in osm for applications/editors/josm/plugins
- Timestamp:
- 2010-10-27T23:28:28+02:00 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/multipoly/src/multipoly/MultipolyAction.java
r23771 r23885 43 43 public class MultipolyAction extends JosmAction { 44 44 45 public MultipolyAction() { 46 super(tr("Create multipolygon"), "multipoly_create", 47 tr("Create multipolygon."), Shortcut.registerShortcut( 48 "tools:multipoly", tr("Tool: {0}", 49 tr("Create multipolygon")), KeyEvent.VK_M, 50 Shortcut.GROUP_EDIT, Shortcut.SHIFT_DEFAULT), true); 45 public MultipolyAction() { 46 super(tr("Create multipolygon"), "multipoly_create", tr("Create multipolygon."), 47 Shortcut.registerShortcut("tools:multipoly", tr("Tool: {0}", tr("Create multipolygon")), 48 KeyEvent.VK_M, Shortcut.GROUP_EDIT, Shortcut.SHIFT_DEFAULT), true); 49 } 50 /** 51 * The action button has been clicked 52 * 53 * @param e Action Event 54 */ 55 public void actionPerformed(ActionEvent e) { 56 if (Main.main.getEditLayer() == null) { 57 JOptionPane.showMessageDialog(Main.parent, tr("No data loaded.")); 58 return; 51 59 } 52 60 53 /** 54 * The action button has been clicked 55 * 56 * @param e 57 * Action Event 58 */ 59 public void actionPerformed(ActionEvent e) { 60 if (Main.main.getEditLayer() == null) { 61 JOptionPane.showMessageDialog(Main.parent, tr("No data loaded.")); 62 return; 63 } 64 65 Collection<Way> selectedWays = Main.main.getCurrentDataSet().getSelectedWays(); 66 67 if (selectedWays.size() < 2) { 68 JOptionPane.showMessageDialog(Main.parent, 69 tr("You must select at least two ways.")); 70 return; 71 } 72 73 Multipolygon polygon = this.analyzeWays(selectedWays); 74 75 if (polygon == null) { 76 return; //could not make multipolygon. 77 } 78 79 Relation relation = this.createRelation(polygon); 61 Collection < Way > selectedWays = Main.main.getCurrentDataSet().getSelectedWays(); 80 62 81 //open relation edit window 82 RelationEditor editor = RelationEditor.getEditor( 83 Main.main.getEditLayer(), 84 relation, 85 null); 86 87 editor.setVisible(true); 88 } 89 90 /** Enable this action only if something is selected */ 91 @Override 92 protected void updateEnabledState() { 93 if (getCurrentDataSet() == null) { 94 setEnabled(false); 95 } else { 96 updateEnabledState(getCurrentDataSet().getSelected()); 97 } 63 if (selectedWays.size() < 2) { 64 JOptionPane.showMessageDialog(Main.parent, tr("You must select at least two ways.")); 65 return; 98 66 } 99 67 100 /** Enable this action only if something is selected */ 101 @Override 102 protected void updateEnabledState( 103 Collection<? extends OsmPrimitive> selection) { 104 setEnabled(selection != null && !selection.isEmpty()); 68 Multipolygon polygon = this.analyzeWays(selectedWays); 69 70 if (polygon == null) { 71 return; //could not make multipolygon. 105 72 } 106 107 108 /** 109 * This method analyzes ways and creates multipolygon. 110 * @param selectedWays 111 * @return null, if there was a problem with the ways. 112 */ 113 private Multipolygon analyzeWays(Collection<Way> selectedWays) { 114 115 Multipolygon pol = new Multipolygon(); 116 String error = pol.makeFromWays(selectedWays); 117 118 if (error != null) { 119 JOptionPane.showMessageDialog(Main.parent, error); 120 return null; 121 } 122 else { 123 return pol; 124 } 125 } 126 127 128 /** 129 * Builds a relation from polygon ways. 130 * @param pol 131 * @return 132 */ 133 private Relation createRelation(Multipolygon pol){ 134 // Create new relation 135 Relation rel = new Relation(); 136 rel.put("type", "multipolygon"); 137 // Add ways to it 138 for(JoinedPolygon jway: pol.outerWays){ 139 for(Way way: jway.ways){ 140 rel.addMember(new RelationMember("outer", way)); 141 } 142 } 143 144 for(JoinedPolygon jway: pol.innerWays){ 145 for(Way way: jway.ways){ 146 rel.addMember(new RelationMember("inner", way)); 147 } 148 } 149 150 return rel; 151 } 73 74 Relation relation = this.createRelation(polygon); 75 76 //open relation edit window, if set up in preferences 77 if (Main.pref.getBoolean("multipoly.show-relation-editor", false)) { 78 RelationEditor editor = RelationEditor.getEditor(Main.main.getEditLayer(), relation, null); 79 editor.setVisible(true); 80 } 81 } 82 83 /** Enable this action only if something is selected */ 84 @Override protected void updateEnabledState() { 85 if (getCurrentDataSet() == null) { 86 setEnabled(false); 87 } else { 88 updateEnabledState(getCurrentDataSet().getSelected()); 89 } 90 } 91 92 /** Enable this action only if something is selected */ 93 @Override protected void updateEnabledState(Collection < ? extends OsmPrimitive > selection) { 94 setEnabled(selection != null && !selection.isEmpty()); 95 } 96 97 /** 98 * This method analyzes ways and creates multipolygon. 99 * @param selectedWays 100 * @return null, if there was a problem with the ways. 101 */ 102 private Multipolygon analyzeWays(Collection < Way > selectedWays) { 103 104 Multipolygon pol = new Multipolygon(); 105 String error = pol.makeFromWays(selectedWays); 106 107 if (error != null) { 108 JOptionPane.showMessageDialog(Main.parent, error); 109 return null; 110 } else { 111 return pol; 112 } 113 } 114 115 /** 116 * Builds a relation from polygon ways. 117 * @param pol 118 * @return 119 */ 120 private Relation createRelation(Multipolygon pol) { 121 // Create new relation 122 Relation rel = new Relation(); 123 rel.put("type", "multipolygon"); 124 // Add ways to it 125 for (JoinedPolygon jway:pol.outerWays) { 126 for (Way way:jway.ways) { 127 rel.addMember(new RelationMember("outer", way)); 128 } 129 } 130 131 for (JoinedPolygon jway:pol.innerWays) { 132 for (Way way:jway.ways) { 133 rel.addMember(new RelationMember("inner", way)); 134 } 135 } 136 return rel; 137 } 152 138 }
Note:
See TracChangeset
for help on using the changeset viewer.