Changeset 25692 in osm for applications/editors/josm/plugins/relcontext/src
- Timestamp:
- 2011-03-24T19:39:23+01:00 (14 years ago)
- Location:
- applications/editors/josm/plugins/relcontext/src/relcontext
- Files:
-
- 1 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/relcontext/src/relcontext/ChosenRelation.java
r25680 r25692 88 88 // todo: dim chosen relation when changing layer 89 89 // todo: check this WTF! 90 if( newLayer != null ) { 90 clear(); 91 if( newLayer != null && oldLayer == null ) { 91 92 Main.map.mapView.addTemporaryLayer(this); 92 } else {93 } else if( oldLayer != null ) { 93 94 Main.map.mapView.removeTemporaryLayer(this); 94 95 } -
applications/editors/josm/plugins/relcontext/src/relcontext/RelContextDialog.java
r25688 r25692 1 1 package relcontext; 2 2 3 import java.io.IOException; 4 import java.io.BufferedReader; 5 import java.io.InputStream; 6 import java.io.Reader; 7 import java.io.InputStreamReader; 8 import java.awt.Point; 3 9 import java.awt.Component; 4 10 import java.awt.Dimension; … … 50 56 public class RelContextDialog extends ToggleDialog implements EditLayerChangeListener, ChosenRelationListener, SelectionChangedListener { 51 57 58 public final static String PREF_PREFIX = "reltoolbox"; 59 private static final String PREF_ROLEBOX = PREF_PREFIX + ".rolebox"; 60 52 61 private final DefaultTableModel relationsData; 53 62 private ChosenRelation chosenRelation; 54 63 private JPanel chosenRelationPanel; 55 64 private ChosenRelationPopupMenu popupMenu; 65 private MultipolygonSettingsPopup multiPopupMenu; 56 66 private JLabel crRoleIndicator; 57 67 private AutoCompletingComboBox roleBox; … … 59 69 60 70 public RelContextDialog() { 61 super(tr("Relation Toolbox"), "reltoolbox",71 super(tr("Relation Toolbox"), PREF_PREFIX, 62 72 tr("Open relation/multipolygon editor panel"), 63 73 Shortcut.registerShortcut("subwindow:reltoolbox", tr("Toggle: {0}", tr("Relation Toolbox")), 64 74 KeyEvent.VK_R, Shortcut.GROUP_LAYER, Shortcut.SHIFT_DEFAULT), 150, true); 65 75 66 JPanel rcPanel = new JPanel(new BorderLayout());67 68 76 chosenRelation = new ChosenRelation(); 69 77 chosenRelation.addChosenRelationListener(this); 70 78 MapView.addEditLayerChangeListener(chosenRelation); 79 80 popupMenu = new ChosenRelationPopupMenu(); 81 multiPopupMenu = new MultipolygonSettingsPopup(); 82 83 JPanel rcPanel = new JPanel(new BorderLayout()); 71 84 72 85 relationsData = new RelationTableModel(); … … 127 140 @Override 128 141 public void componentHidden( ComponentEvent e ) { 142 Main.pref.put(PREF_ROLEBOX + ".visible", false); 129 143 toggleRolePanelButtonTop.setVisible(true); 130 144 } … … 132 146 @Override 133 147 public void componentShown( ComponentEvent e ) { 148 Main.pref.put(PREF_ROLEBOX + ".visible", true); 134 149 toggleRolePanelButtonTop.setVisible(false); 135 150 } 136 151 }); 152 rolePanel.setVisible(Main.pref.getBoolean(PREF_ROLEBOX + ".visible", true)); 137 153 toggleRolePanelButtonTop.setVisible(!rolePanel.isVisible()); 154 lastSelectedRole = Main.pref.get(PREF_ROLEBOX + ".lastrole"); 138 155 139 156 sortAndFixAction.addPropertyChangeListener(new PropertyChangeListener() { … … 155 172 JPanel bottomLine = new JPanel(new GridBagLayout()); 156 173 bottomLine.add(new JButton(new CreateRelationAction(chosenRelation)), GBC.std()); 157 bottomLine.add(new JButton(new CreateMultipolygonAction(chosenRelation)), GBC.std()); 174 final JButton multipolygonButton = new JButton(new CreateMultipolygonAction(chosenRelation)); 175 bottomLine.add(multipolygonButton, GBC.std()); 176 // bottomLine.add(sizeButton(new JButton(new MultipolygonSettingsAction()), 16, 0), GBC.std().fill(GBC.VERTICAL)); 158 177 bottomLine.add(Box.createHorizontalGlue(), GBC.std().fill()); 159 178 bottomLine.add(new JButton(new FindRelationAction(chosenRelation)), GBC.eol()); 160 179 rcPanel.add(sizeButton(bottomLine, 0, 24), BorderLayout.SOUTH); 161 180 181 multipolygonButton.addMouseListener(new MouseAdapter() { 182 @Override 183 public void mousePressed( MouseEvent e ) { 184 checkPopup(e); 185 } 186 187 @Override 188 public void mouseReleased( MouseEvent e ) { 189 checkPopup(e); 190 } 191 192 private void checkPopup( MouseEvent e ) { 193 if( e.isPopupTrigger() ) 194 multiPopupMenu.show(e.getComponent(), e.getX(), e.getY()); 195 } 196 }); 197 162 198 add(rcPanel, BorderLayout.CENTER); 163 164 popupMenu = new ChosenRelationPopupMenu();165 199 } 166 200 … … 223 257 MapView.removeEditLayerChangeListener(this); 224 258 DatasetEventManager.getInstance().removeDatasetListener(chosenRelation); 259 chosenRelation.clear(); 225 260 } 226 261 … … 319 354 } 320 355 321 private static final Map<String, String[]> possibleRoles = new HashMap<String, String[]>(); 322 static { 323 possibleRoles.put("boundary", new String[] {"admin_centre", "label", "subarea"}); 324 possibleRoles.put("route", new String[] {"forward", "backward", "stop", "platform"}); 325 possibleRoles.put("restriction", new String[] {"from", "to", "via", "location_hint"}); 326 possibleRoles.put("enforcement", new String[] {"device", "from", "to", "force"}); 327 possibleRoles.put("destination_sign", new String[] {"to", "from", "intersection", "sign"}); 328 possibleRoles.put("site", new String[] {"perimeter", "entrance", "label"}); 329 possibleRoles.put("bridge", new String[] {"across", "under", "outline", "edge"}); 330 possibleRoles.put("tunnel", new String[] {"through", "outline", "edge"}); 331 possibleRoles.put("surveillance", new String[] {"camera", "extent", "visible", "hidden"}); 332 possibleRoles.put("street", new String[] {"street", "address", "house", "associated"}); 333 possibleRoles.put("collection", new String[] {"member", "street", "river", "railway", "address", "associated"}); 356 private static final String POSSIBLE_ROLES_FILE = "relcontext/possible_roles.txt"; 357 private static final Map<String, List<String>> possibleRoles = loadRoles(); 358 359 private static Map<String, List<String>> loadRoles() { 360 Map<String, List<String>> result = new HashMap<String, List<String>>(); 361 try { 362 ClassLoader classLoader = RelContextDialog.class.getClassLoader(); 363 final InputStream possibleRolesStream = classLoader.getResourceAsStream(POSSIBLE_ROLES_FILE); 364 BufferedReader r = new BufferedReader(new InputStreamReader(possibleRolesStream)); 365 while( r.ready() ) { 366 String line = r.readLine(); 367 StringTokenizer t = new StringTokenizer(line, " ,;:\""); 368 if( t.hasMoreTokens() ) { 369 String type = t.nextToken(); 370 List<String> roles = new ArrayList<String>(); 371 while( t.hasMoreTokens() ) 372 roles.add(t.nextToken()); 373 result.put(type, roles); 374 } 375 } 376 r.close(); 377 } catch( Exception e ) { 378 System.err.println("[RelToolbox] Error reading possible roles file."); 379 e.printStackTrace(); 380 } 381 return result; 334 382 } 335 383 … … 344 392 } 345 393 if( chosenRelation.get().get("type") != null ) { 346 String[]values = possibleRoles.get(chosenRelation.get().get("type"));394 List<String> values = possibleRoles.get(chosenRelation.get().get("type")); 347 395 if( values != null ) 348 items.addAll( Arrays.asList(values));396 items.addAll(values); 349 397 } 350 398 for( RelationMember m : chosenRelation.get().getMembers() ) … … 354 402 if( currentRole != null && currentRole.length() > 1 ) { 355 403 lastSelectedRole = currentRole; 404 Main.pref.put(PREF_ROLEBOX + ".lastrole", lastSelectedRole); 356 405 } 357 406 roleBox.setPossibleItems(items); … … 456 505 457 506 public void actionPerformed( ActionEvent e ) { 507 Main.pref.put(PREF_ROLEBOX + ".visible", !component.isVisible()); 458 508 component.setVisible(!component.isVisible()); 459 509 } … … 521 571 } 522 572 } 573 574 private class MultipolygonSettingsAction extends AbstractAction { 575 public MultipolygonSettingsAction() { 576 super(); 577 putValue(SMALL_ICON, ImageProvider.get("svpRight")); 578 putValue(SHORT_DESCRIPTION, tr("Change multipolygon creation settings")); 579 } 580 581 public void actionPerformed( ActionEvent e ) { 582 Component c = e.getSource() instanceof Component ? (Component)e.getSource() : Main.parent; 583 Point p = getMousePosition(); 584 multiPopupMenu.show(c, 0, 0); 585 } 586 } 587 588 private class MultipolygonSettingsPopup extends JPopupMenu implements ActionListener { 589 public MultipolygonSettingsPopup() { 590 super(); 591 addMenuItem("boundary", "Create administrative boundary relations"); 592 addMenuItem("boundaryways", "Add tags boundary and admin_level to boundary relation ways"); 593 addMenuItem("tags", "Move area tags from contour to relation").setEnabled(false); 594 addMenuItem("single", "Create a single multipolygon for multiple outer contours").setEnabled(false); 595 } 596 597 protected final JCheckBoxMenuItem addMenuItem( String property, String title ) { 598 String fullProperty = PREF_PREFIX + ".multipolygon." + property; 599 JCheckBoxMenuItem item = new JCheckBoxMenuItem(tr(title)); 600 item.setSelected(Main.pref.getBoolean(fullProperty, CreateMultipolygonAction.getDefaultPropertyValue(property))); 601 item.setActionCommand(fullProperty); 602 item.addActionListener(this); 603 add(item); 604 return item; 605 } 606 607 public void actionPerformed( ActionEvent e ) { 608 String property = e.getActionCommand(); 609 if( property != null && property.length() > 0 && e.getSource() instanceof JCheckBoxMenuItem ) { 610 boolean value = ((JCheckBoxMenuItem)e.getSource()).isSelected(); 611 Main.pref.put(property, value); 612 show(getInvoker(), getX(), getY()); 613 } 614 } 615 } 523 616 } -
applications/editors/josm/plugins/relcontext/src/relcontext/actions/CreateMultipolygonAction.java
r25682 r25692 1 1 package relcontext.actions; 2 2 3 import org.openstreetmap.josm.data.osm.OsmPrimitive; 3 import java.awt.Dialog.ModalityType; 4 import java.awt.GridBagLayout; 4 5 import static org.openstreetmap.josm.tools.I18n.tr; 5 6 import java.awt.event.ActionEvent; 7 import java.awt.event.ActionListener; 6 8 import java.util.*; 7 import javax.swing. JOptionPane;9 import javax.swing.*; 8 10 import org.openstreetmap.josm.Main; 9 11 import org.openstreetmap.josm.actions.JosmAction; 10 import org.openstreetmap.josm.command.AddCommand; 11 import org.openstreetmap.josm.command.ChangePropertyCommand; 12 import org.openstreetmap.josm.command.Command; 13 import org.openstreetmap.josm.command.SequenceCommand; 14 import org.openstreetmap.josm.data.osm.MultipolygonCreate; 15 import org.openstreetmap.josm.data.osm.Relation; 16 import org.openstreetmap.josm.data.osm.RelationMember; 17 import org.openstreetmap.josm.data.osm.Way; 12 import org.openstreetmap.josm.command.*; 13 import org.openstreetmap.josm.data.osm.*; 14 import org.openstreetmap.josm.tools.GBC; 18 15 import relcontext.ChosenRelation; 19 16 … … 26 23 public class CreateMultipolygonAction extends JosmAction { 27 24 private static final String ACTION_NAME = "Create relation"; 25 private static final String PREF_MULTIPOLY = "reltoolbox.multipolygon."; 28 26 protected ChosenRelation chRel; 29 27 … … 36 34 public CreateMultipolygonAction() { 37 35 this(null); 36 } 37 38 public static boolean getDefaultPropertyValue( String property ) { 39 if( property.equals("boundary") ) return false; 40 else if( property.equals("boundaryways") ) return true; 41 else if( property.equals("tags") ) return false; 42 else if( property.equals("single") ) return true; 43 throw new IllegalArgumentException(property); 44 } 45 46 private boolean getPref( String property ) { 47 return Main.pref.getBoolean(PREF_MULTIPOLY + property, getDefaultPropertyValue(property)); 38 48 } 39 49 … … 47 57 } 48 58 Relation rel = new Relation(); 49 rel.put("type", "multipolygon"); 59 boolean isBoundary = getPref("boundary"); 60 if( isBoundary ) { 61 rel.put("type", "boundary"); 62 rel.put("boundary", "administrative"); 63 askForAdminLevelAndName(rel); 64 } else 65 rel.put("type", "multipolygon"); 50 66 for( MultipolygonCreate.JoinedPolygon poly : mpc.outerWays ) 51 67 for( Way w : poly.ways ) … … 54 70 for( Way w : poly.ways ) 55 71 rel.addMember(new RelationMember("inner", w)); 72 if( isBoundary ) 73 addBoundaryMembers(rel); 56 74 List<Command> list = removeTagsFromInnerWays(rel); 75 if( isBoundary && getPref("boundaryways") ) 76 list.addAll(fixWayTagsForBoundary(rel)); 57 77 list.add(new AddCommand(rel)); 58 78 Main.main.undoRedo.add(new SequenceCommand(tr("Create multipolygon"), list)); … … 77 97 enabled = false; 78 98 else { 79 for( OsmPrimitive p : selection ) 80 if( !(p instanceof Way) ) { 81 enabled = false; 82 break; 83 } 99 if( !getPref("boundary") ) { 100 for( OsmPrimitive p : selection ) { 101 if( !(p instanceof Way) ) { 102 enabled = false; 103 break; 104 } 105 } 106 } 84 107 } 85 108 setEnabled(enabled); 109 } 110 111 /** 112 * Add selected nodes and relations with corresponding roles. 113 */ 114 private void addBoundaryMembers( Relation rel ) { 115 for( OsmPrimitive p : getCurrentDataSet().getSelected() ) { 116 String role = null; 117 if( p.getType().equals(OsmPrimitiveType.RELATION) ) { 118 role = "subarea"; 119 } else if( p.getType().equals(OsmPrimitiveType.NODE) ) { 120 Node n = (Node)p; 121 if( !n.isIncomplete() ) { 122 if( n.hasKey("place") ) 123 role = "admin_centre"; 124 else 125 role = "label"; 126 } 127 } 128 if( role != null ) 129 rel.addMember(new RelationMember(role, p)); 130 } 131 } 132 133 /** 134 * For all untagged ways in relation, add tags boundary and admin_level. 135 */ 136 private List<Command> fixWayTagsForBoundary( Relation rel ) { 137 List<Command> commands = new ArrayList<Command>(); 138 if( !rel.hasKey("boundary") || !rel.hasKey("admin_level") ) 139 return commands; 140 String adminLevelStr = rel.get("admin_level"); 141 int adminLevel = 0; 142 try { 143 adminLevel = Integer.parseInt(adminLevelStr); 144 } catch( NumberFormatException e ) { 145 return commands; 146 } 147 Set<OsmPrimitive> waysBoundary = new HashSet<OsmPrimitive>(); 148 Set<OsmPrimitive> waysAdminLevel = new HashSet<OsmPrimitive>(); 149 for( OsmPrimitive p : rel.getMemberPrimitives() ) { 150 if( p instanceof Way ) { 151 int count = 0; 152 if( p.hasKey("boundary") && p.get("boundary").equals("administrative") ) 153 count++; 154 if( p.hasKey("admin_level") ) 155 count++; 156 if( p.keySet().size() - count == 0 ) { 157 if( !p.hasKey("boundary") ) 158 waysBoundary.add(p); 159 if( !p.hasKey("admin_level") ) { 160 waysAdminLevel.add(p); 161 } else { 162 try { 163 int oldAdminLevel = Integer.parseInt(p.get("admin_level")); 164 if( oldAdminLevel > adminLevel ) 165 waysAdminLevel.add(p); 166 } catch( NumberFormatException e ) { 167 waysAdminLevel.add(p); // some garbage, replace it 168 } 169 } 170 } 171 } 172 } 173 if( !waysBoundary.isEmpty() ) 174 commands.add(new ChangePropertyCommand(waysBoundary, "boundary", "administrative")); 175 if( !waysAdminLevel.isEmpty() ) 176 commands.add(new ChangePropertyCommand(waysAdminLevel, "admin_level", adminLevelStr)); 177 return commands; 86 178 } 87 179 … … 137 229 return commands; 138 230 } 231 232 private void askForAdminLevelAndName( Relation rel ) { 233 JPanel panel = new JPanel(new GridBagLayout()); 234 panel.add(new JLabel(tr("Enter admin level and name for the border relation:")), GBC.eol().insets(0, 0, 0, 5)); 235 236 final JTextField admin = new JTextField(); 237 admin.setText(Main.pref.get(PREF_MULTIPOLY + "lastadmin", "")); 238 panel.add(new JLabel(tr("Admin level")), GBC.std()); 239 panel.add(Box.createHorizontalStrut(10), GBC.std()); 240 panel.add(admin, GBC.eol().fill(GBC.HORIZONTAL)); 241 242 final JTextField name = new JTextField(); 243 panel.add(new JLabel(tr("Name")), GBC.std()); 244 panel.add(Box.createHorizontalStrut(10), GBC.std()); 245 panel.add(name, GBC.eol().fill(GBC.HORIZONTAL)); 246 247 final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION) { 248 @Override 249 public void selectInitialValue() { 250 admin.requestFocusInWindow(); 251 admin.selectAll(); 252 } 253 }; 254 final JDialog dlg = optionPane.createDialog(Main.parent, tr("Create relation")); 255 dlg.setModalityType(ModalityType.DOCUMENT_MODAL); 256 257 name.addActionListener(new ActionListener() { 258 public void actionPerformed( ActionEvent e ) { 259 dlg.setVisible(false); 260 optionPane.setValue(JOptionPane.OK_OPTION); 261 } 262 }); 263 264 dlg.setVisible(true); 265 266 Object answer = optionPane.getValue(); 267 if( answer == null || answer == JOptionPane.UNINITIALIZED_VALUE 268 || (answer instanceof Integer && (Integer)answer != JOptionPane.OK_OPTION) ) { 269 return; 270 } 271 272 String admin_level = admin.getText().trim(); 273 String new_name = name.getText().trim(); 274 if( admin_level.equals("10") || (admin_level.length() == 1 && Character.isDigit(admin_level.charAt(0))) ) { 275 rel.put("admin_level", admin_level); 276 Main.pref.put(PREF_MULTIPOLY + "lastadmin", admin_level); 277 } 278 if( new_name.length() > 0 ) 279 rel.put("name", new_name); 280 } 139 281 } -
applications/editors/josm/plugins/relcontext/src/relcontext/actions/CreateRelationAction.java
r25688 r25692 23 23 import org.openstreetmap.josm.tools.GBC; 24 24 import relcontext.ChosenRelation; 25 import relcontext.RelContextDialog; 25 26 26 27 /** … … 32 33 public class CreateRelationAction extends JosmAction { 33 34 private static final String ACTION_NAME = "Create relation"; 35 private static final String PREF_LASTTYPE = "reltoolbox.createrelation.lasttype"; 34 36 protected ChosenRelation chRel; 35 37 … … 90 92 keys.setPossibleItems(RELATION_TYPES); 91 93 keys.setEditable(true); 94 keys.getEditor().setItem(Main.pref.get(PREF_LASTTYPE, "multipolygon")); 92 95 93 96 panel.add(new JLabel(tr("Type")), GBC.std()); … … 120 123 } 121 124 122 return keys.getEditor().getItem().toString().trim(); 125 String result = keys.getEditor().getItem().toString().trim(); 126 Main.pref.put(PREF_LASTTYPE, result); 127 return result; 123 128 } 124 129 } -
applications/editors/josm/plugins/relcontext/src/relcontext/actions/SortAndFixAction.java
r25688 r25692 116 116 for( int i = 0; i < r.getMembersCount(); i++ ) { 117 117 RelationMember m = r.getMember(i); 118 if( m. getType().equals(OsmPrimitiveType.WAY) ) {118 if( m.isWay() ) { 119 119 String role = null; 120 120 if( outerWays.contains((Way)m.getMember()) ) … … 137 137 RelationMember m = r.getMember(i); 138 138 String role = null; 139 if( m. getType().equals(OsmPrimitiveType.RELATION) )139 if( m.isRelation() ) 140 140 role = "subarea"; 141 else if( m. getType().equals(OsmPrimitiveType.NODE) ) {141 else if( m.isNode() ) { 142 142 Node n = (Node)m.getMember(); 143 143 if( !n.isIncomplete() ) {
Note:
See TracChangeset
for help on using the changeset viewer.