Changeset 25678 in osm
- Timestamp:
- 2011-03-23T15:51:38+01:00 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/relcontext/src/relcontext/RelContextDialog.java
r25677 r25678 8 8 9 9 import java.awt.BorderLayout; 10 import java.awt.Dialog.ModalityType; 10 11 import java.awt.Dimension; 11 12 import java.awt.FlowLayout; 12 13 import java.awt.GridBagLayout; 14 import java.awt.event.ActionListener; 13 15 import java.awt.event.ComponentAdapter; 14 16 import java.awt.event.FocusAdapter; … … 99 101 crRoleIndicator = new JLabel(); 100 102 rolePanel.add(crRoleIndicator, GBC.std().insets(5, 0, 5, 0)); 101 102 // autocompleting role chooser103 103 roleBox = new AutoCompletingComboBox(); 104 final Action applyNewRoleAction = new ApplyNewRoleAction();105 // roleBox.getEditor().addActionListener(applyNewRoleAction);106 104 roleBox.setEditable(false); 107 105 rolePanel.add(roleBox, GBC.std().fill(GBC.HORIZONTAL)); 108 rolePanel.add(new JButton(applyNewRoleAction), GBC.eol()); 109 rolePanel.setVisible(false); 106 rolePanel.add(new JButton(new ApplyNewRoleAction()), GBC.std()); 107 rolePanel.add(new JButton(new EnterNewRoleAction()), GBC.eol()); 108 // rolePanel.setVisible(false); // todo: take from preferences 110 109 111 110 // [±][X] relation U [AZ][Down][Edit] … … 144 143 } 145 144 }); 145 toggleRolePanelButtonTop.setVisible(!rolePanel.isVisible()); 146 146 147 147 sortAndFixAction.addPropertyChangeListener(new PropertyChangeListener() { … … 260 260 261 261 private void updateRoleAutoCompletionList() { 262 String currentRole = roleBox.getSelectedItem() == null ? null : ((AutoCompletionListItem)roleBox.getSelectedItem()).getValue(); 262 263 List<String> items = new ArrayList<String>(); 263 264 items.add(""); … … 273 274 items.add(value); 274 275 } 275 } else if( roleBox.getSelectedItem() != null ) { 276 lastSelectedRole = ((AutoCompletionListItem)roleBox.getSelectedItem()).getValue(); 276 for( RelationMember m : chosenRelation.get().getMembers() ) 277 if( !items.contains(m.getRole()) ) 278 items.add(m.getRole()); 279 lastSelectedRole = currentRole; 280 } else if( currentRole != null && currentRole.length() > 0 ) { 281 lastSelectedRole = currentRole; 277 282 } 278 283 roleBox.setPossibleItems(items); 279 if( lastSelectedRole != null && items.contains(lastSelectedRole) ) 280 roleBox.setSelectedItem(lastSelectedRole); 284 // todo: store last non-empty role and try to select it if present, but do not store if empty role was chosen 285 // if( lastSelectedRole != null && items.contains(lastSelectedRole) ) 286 // roleBox.setSelectedItem(lastSelectedRole); 287 // else if( lastSelectedRole == null && items.contains("outer") ) 288 // roleBox.setSelectedItem("outer"); 289 } 290 291 private String askForRoleName() { 292 JPanel panel = new JPanel(new GridBagLayout()); 293 294 final AutoCompletingComboBox role = new AutoCompletingComboBox(); 295 List<AutoCompletionListItem> items = new ArrayList<AutoCompletionListItem>(); 296 for( int i = 0; i < roleBox.getModel().getSize(); i++ ) 297 items.add((AutoCompletionListItem)roleBox.getModel().getElementAt(i)); 298 role.setPossibleACItems(items); 299 role.setEditable(true); 300 301 panel.add(new JLabel(tr("Role")), GBC.std()); 302 panel.add(Box.createHorizontalStrut(10), GBC.std()); 303 panel.add(role, GBC.eol().fill(GBC.HORIZONTAL)); 304 305 final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION) { 306 @Override 307 public void selectInitialValue() { 308 role.requestFocusInWindow(); 309 role.getEditor().selectAll(); 310 } 311 }; 312 final JDialog dlg = optionPane.createDialog(Main.parent, tr("Specify role")); 313 dlg.setModalityType(ModalityType.DOCUMENT_MODAL); 314 315 role.getEditor().addActionListener(new ActionListener() { 316 public void actionPerformed( ActionEvent e ) { 317 dlg.setVisible(false); 318 optionPane.setValue(JOptionPane.OK_OPTION); 319 } 320 }); 321 322 dlg.setVisible(true); 323 324 Object answer = optionPane.getValue(); 325 if( answer == null || answer == JOptionPane.UNINITIALIZED_VALUE 326 || (answer instanceof Integer && (Integer)answer != JOptionPane.OK_OPTION) ) { 327 return null; 328 } 329 330 return role.getEditor().getItem().toString().trim(); 281 331 } 282 332 … … 326 376 } 327 377 378 protected void applyRoleToSelection( String role ) { 379 if( chosenRelation != null && chosenRelation.get() != null && Main.main.getCurrentDataSet() != null && !Main.main.getCurrentDataSet().selectionEmpty() ) { 380 Collection<OsmPrimitive> selected = Main.main.getCurrentDataSet().getSelected(); 381 Relation r = new Relation(chosenRelation.get()); 382 boolean fixed = false; 383 for( int i = 0; i < r.getMembersCount(); i++ ) { 384 RelationMember m = r.getMember(i); 385 if( selected.contains(m.getMember()) ) { 386 if( !role.equals(m.getRole()) ) { 387 r.setMember(i, new RelationMember(role, m.getMember())); 388 fixed = true; 389 } 390 } 391 } 392 if( fixed ) 393 Main.main.undoRedo.add(new ChangeCommand(chosenRelation.get(), r)); 394 } 395 } 396 328 397 private class ApplyNewRoleAction extends AbstractAction { 329 398 public ApplyNewRoleAction() { … … 333 402 public void actionPerformed( ActionEvent e ) { 334 403 Object selectedItem = roleBox == null ? null : roleBox.getSelectedItem(); 335 if( selectedItem != null && chosenRelation != null && chosenRelation.get() != null && Main.main.getCurrentDataSet() != null && !Main.main.getCurrentDataSet().selectionEmpty() ) { 336 // String role = roleBox.getEditor().getItem().toString().trim(); 404 if( selectedItem != null ) { 337 405 if( selectedItem instanceof AutoCompletionListItem ) 338 406 selectedItem = ((AutoCompletionListItem)selectedItem).getValue(); 339 String role = selectedItem.toString().trim(); 340 Collection<OsmPrimitive> selected = Main.main.getCurrentDataSet().getSelected(); 341 Relation r = new Relation(chosenRelation.get()); 342 boolean fixed = false; 343 for( int i = 0; i < r.getMembersCount(); i++ ) { 344 RelationMember m = r.getMember(i); 345 if( selected.contains(m.getMember()) ) { 346 if( !role.equals(m.getRole()) ) { 347 r.setMember(i, new RelationMember(role, m.getMember())); 348 fixed = true; 349 } 350 } 351 } 352 if( fixed ) 353 Main.main.undoRedo.add(new ChangeCommand(chosenRelation.get(), r)); 354 } 407 applyRoleToSelection(selectedItem.toString().trim()); 408 } 409 } 410 } 411 412 private class EnterNewRoleAction extends AbstractAction { 413 public EnterNewRoleAction() { 414 super("+"); 415 } 416 417 public void actionPerformed( ActionEvent e ) { 418 String role = askForRoleName(); 419 if( role != null ) 420 applyRoleToSelection(role); 355 421 } 356 422 }
Note:
See TracChangeset
for help on using the changeset viewer.