- Timestamp:
- 2009-02-19T18:56:23+01:00 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/dialogs/RelationEditor.java
r1415 r1431 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.awt.BorderLayout;7 6 import java.awt.Dimension; 8 7 import java.awt.GridBagLayout; … … 11 10 import java.awt.event.ActionListener; 12 11 import java.awt.event.KeyEvent; 13 import java.beans.PropertyChangeEvent;14 import java.beans.PropertyChangeListener;15 12 import java.io.IOException; 16 13 import java.text.Collator; … … 22 19 import java.util.Map.Entry; 23 20 24 import javax.swing.BoxLayout;25 import javax.swing.JButton;26 import javax.swing.JFrame;27 21 import javax.swing.JLabel; 28 22 import javax.swing.JOptionPane; … … 47 41 import org.openstreetmap.josm.data.osm.RelationMember; 48 42 import org.openstreetmap.josm.data.osm.visitor.MergeVisitor; 43 import org.openstreetmap.josm.gui.ExtendedDialog; 49 44 import org.openstreetmap.josm.gui.OsmPrimitivRenderer; 45 import org.openstreetmap.josm.gui.SideButton; 50 46 import org.openstreetmap.josm.io.OsmServerObjectReader; 51 47 import org.openstreetmap.josm.tools.GBC; 52 import org.openstreetmap.josm.tools. ImageProvider;48 import org.openstreetmap.josm.tools.Shortcut; 53 49 import org.xml.sax.SAXException; 54 50 … … 64 60 * 65 61 */ 66 public class RelationEditor extends JFrame{62 public class RelationEditor extends ExtendedDialog { 67 63 68 64 /** … … 202 198 public RelationEditor(Relation relation, Collection<RelationMember> selectedMembers ) 203 199 { 204 super(relation == null ? tr("Create new relation") : 205 relation.id == 0 ? tr ("Edit new relation") : 206 tr("Edit relation #{0}", relation.id)); 200 // Initalizes ExtendedDialog 201 super(Main.parent, 202 relation == null 203 ? tr("Create new relation") 204 : (relation.id == 0 205 ? tr ("Edit new relation") 206 : tr("Edit relation #{0}", relation.id) 207 ), 208 new String[] { tr("Apply Changes"), tr("Cancel")}, 209 false 210 ); 211 207 212 this.relation = relation; 208 209 213 ordered = !Main.pref.get("osm-server.version", "0.5").equals("0.5"); 210 214 ordered = true; 211 215 if (relation == null) { 212 216 // create a new relation … … 218 222 } 219 223 220 getContentPane().setLayout(new BorderLayout()); 224 JPanel bothTables = setupBasicLayout(selectedMembers); 225 221 226 JTabbedPane tabPane = new JTabbedPane(); 222 getContentPane().add(tabPane, BorderLayout.CENTER); 223 224 // (ab)use JOptionPane to make this look familiar; 225 // hook up with JOptionPane's property change event 226 // to detect button click 227 final JOptionPane okcancel = new JOptionPane("", 228 JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null); 229 getContentPane().add(okcancel, BorderLayout.SOUTH); 230 231 okcancel.addPropertyChangeListener(new PropertyChangeListener() { 232 public void propertyChange(PropertyChangeEvent event) { 233 if (event.getPropertyName().equals(JOptionPane.VALUE_PROPERTY) && event.getNewValue() != null) { 234 if ((Integer)event.getNewValue() == JOptionPane.OK_OPTION) { 235 // clicked ok! 236 if (RelationEditor.this.relation == null) { 237 Main.main.undoRedo.add(new AddCommand(clone)); 238 DataSet.fireSelectionChanged(Main.ds.getSelected()); 239 } else if (!RelationEditor.this.relation.realEqual(clone, true)) { 240 Main.main.undoRedo.add(new ChangeCommand(RelationEditor.this.relation, clone)); 241 DataSet.fireSelectionChanged(Main.ds.getSelected()); 242 } 243 } 244 setVisible(false); 245 } 246 } 247 }); 227 tabPane.add(bothTables, tr("Basic")); 228 229 // This sets the minimum size before scrollbars appear on the dialog 230 tabPane.setPreferredSize(new Dimension(100, 100)); 231 contentConstraints = GBC.eol().fill().insets(5,10,5,0); 232 setupDialog(tabPane, new String[] { "ok.png", "cancel.png" }); 233 // FIXME: Make it remember last screen position 234 setSize(findMaxDialogSize()); 248 235 249 236 try { setAlwaysOnTop(true); } catch (SecurityException sx) {} 250 237 251 // Basic Editor panel has two blocks; 252 // a tag table at the top and a membership list below. 253 238 setVisible(true); 239 240 if(getValue() != 1) 241 return; 242 243 // User clicked "Apply" 244 applyChanges(); 245 } 246 247 /** 248 * Basic Editor panel has two blocks: a tag table at the top and a membership list below 249 * @param selectedMembers 250 * @return a JPanel with the described layout 251 */ 252 private JPanel setupBasicLayout(Collection<RelationMember> selectedMembers) { 254 253 // setting up the properties table 255 256 254 propertyData.setColumnIdentifiers(new String[]{tr("Key"),tr("Value")}); 257 255 propertyTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); … … 319 317 // this is not exactly pretty but the four buttons simply don't fit in one line. 320 318 // we should have smaller buttons for situations like this. 321 JPanel buttonPanel = new JPanel(new GridLayout(2,ordered ? 3 : 2)); 322 323 if (ordered) { 324 buttonPanel.add(createButton(null, "moveup", tr("Move the currently selected members up"), KeyEvent.VK_U, new ActionListener() { 325 public void actionPerformed(ActionEvent e) { 326 moveMembers(-1); 327 } 328 })); 329 } 319 JPanel buttonPanel = setupBasicButtons(); 320 330 321 bothTables.add(new JScrollPane(memberTable), GBC.eol().fill(GBC.BOTH)); 331 332 buttonPanel.add(createButton(marktr("Add Selected"),"addselected",333 tr("Add all currently selected objects as members"), KeyEvent.VK_A, new ActionListener() {334 public void actionPerformed(ActionEvent e) {335 addSelected();336 }337 }));338 339 buttonPanel.add(createButton(marktr("Remove Selected"),"removeselected",340 tr("Remove all currently selected objects from relation"), KeyEvent.VK_R, new ActionListener() {341 public void actionPerformed(ActionEvent e) {342 deleteSelected();343 }344 }));345 346 if(ordered) {347 buttonPanel.add(createButton(null, "movedown", tr("Move the currently selected members down"), KeyEvent.VK_N, new ActionListener() {348 public void actionPerformed(ActionEvent e) {349 moveMembers(1);350 }351 }));352 }353 354 buttonPanel.add(createButton(marktr("Remove"),"remove",355 tr("Remove the member in the current table row from this relation"), KeyEvent.VK_D, new ActionListener() {356 public void actionPerformed(ActionEvent e) {357 int[] rows = memberTable.getSelectedRows();358 RelationMember mem = new RelationMember();359 for (int row : rows) {360 mem.role = memberTable.getValueAt(row, 0).toString();361 mem.member = (OsmPrimitive) memberTable.getValueAt(row, 1);362 clone.members.remove(mem);363 }364 refreshTables();365 }366 }));367 368 buttonPanel.add(createButton(marktr("Download Members"),"downloadincomplete",369 tr("Download all incomplete ways and nodes in relation"), KeyEvent.VK_L, new ActionListener() {370 public void actionPerformed(ActionEvent e) {371 downloadRelationMembers();372 refreshTables();373 }374 }));375 376 322 bothTables.add(buttonPanel, GBC.eop().fill(GBC.HORIZONTAL)); 377 378 tabPane.add(bothTables, tr("Basic"));379 380 323 refreshTables(); 381 324 … … 398 341 } 399 342 } 400 401 setSize(new Dimension(600, 500)); 402 setLocationRelativeTo(Main.parent); 343 return bothTables; 344 } 345 346 /** 347 * Creates the buttons for the basic editing layout 348 * @return JPanel with basic buttons 349 */ 350 private JPanel setupBasicButtons() { 351 JPanel buttonPanel = new JPanel(new GridLayout(2,ordered ? 3 : 2)); 352 353 if (ordered) { 354 buttonPanel.add(createButton(marktr("Move Up"), "moveup", tr("Move the currently selected members up"), KeyEvent.VK_U, new ActionListener() { 355 public void actionPerformed(ActionEvent e) { 356 moveMembers(-1); 357 } 358 })); 359 } 360 361 buttonPanel.add(createButton(marktr("Add Selected"),"addselected", 362 tr("Add all currently selected objects as members"), KeyEvent.VK_D, new ActionListener() { 363 public void actionPerformed(ActionEvent e) { 364 addSelected(); 365 } 366 })); 367 368 buttonPanel.add(createButton(marktr("Remove Selected"),"removeselected", 369 tr("Remove all currently selected objects from relation"), KeyEvent.VK_G, new ActionListener() { 370 public void actionPerformed(ActionEvent e) { 371 deleteSelected(); 372 } 373 })); 374 375 if(ordered) { 376 buttonPanel.add(createButton(marktr("Move Down"), "movedown", tr("Move the currently selected members down"), KeyEvent.VK_J, new ActionListener() { 377 public void actionPerformed(ActionEvent e) { 378 moveMembers(1); 379 } 380 })); 381 } 382 383 buttonPanel.add(createButton(marktr("Remove"),"remove", 384 tr("Remove the member in the current table row from this relation"), KeyEvent.VK_M, new ActionListener() { 385 public void actionPerformed(ActionEvent e) { 386 int[] rows = memberTable.getSelectedRows(); 387 RelationMember mem = new RelationMember(); 388 for (int row : rows) { 389 mem.role = memberTable.getValueAt(row, 0).toString(); 390 mem.member = (OsmPrimitive) memberTable.getValueAt(row, 1); 391 clone.members.remove(mem); 392 } 393 refreshTables(); 394 } 395 })); 396 397 buttonPanel.add(createButton(marktr("Download Members"),"downloadincomplete", 398 tr("Download all incomplete ways and nodes in relation"), KeyEvent.VK_K, new ActionListener() { 399 public void actionPerformed(ActionEvent e) { 400 downloadRelationMembers(); 401 refreshTables(); 402 } 403 })); 404 405 return buttonPanel; 406 } 407 408 /** 409 * This function saves the user's changes. Must be invoked manually. 410 */ 411 private void applyChanges() { 412 if (RelationEditor.this.relation == null) { 413 // If the user wanted to create a new relation, but hasn't added any members or 414 // tags, don't add an empty relation 415 clone.checkTagged(); 416 if(clone.members.size() == 0 && !clone.tagged) 417 return; 418 Main.main.undoRedo.add(new AddCommand(clone)); 419 DataSet.fireSelectionChanged(Main.ds.getSelected()); 420 } else if (!RelationEditor.this.relation.realEqual(clone, true)) { 421 Main.main.undoRedo.add(new ChangeCommand(RelationEditor.this.relation, clone)); 422 DataSet.fireSelectionChanged(Main.ds.getSelected()); 423 } 424 } 425 426 @Override 427 protected Dimension findMaxDialogSize() { 428 // FIXME: Make it remember dialog size 429 return new Dimension(600, 500); 403 430 } 404 431 … … 421 448 } 422 449 423 private JButton createButton(String name, String iconName, String tooltip, int mnemonic, ActionListener actionListener) { 424 JButton b = new JButton((name == null) ? null : tr(name), ImageProvider.get("dialogs", iconName)); 425 b.setActionCommand(name); 426 b.addActionListener(actionListener); 427 b.setToolTipText(tooltip); 428 b.setMnemonic(mnemonic); 429 b.putClientProperty("help", "Dialog/Properties/"+name); 430 return b; 450 private SideButton createButton(String name, String iconName, String tooltip, int mnemonic, ActionListener actionListener) { 451 return 452 new SideButton(name, iconName, "relationEditor", 453 tooltip, 454 Shortcut.registerShortcut("relationeditor:"+iconName, 455 tr("Relation Editor: {0}", name == null ? tooltip : name), 456 mnemonic, 457 Shortcut.GROUP_MNEMONIC), 458 actionListener 459 ); 431 460 } 432 461
Note:
See TracChangeset
for help on using the changeset viewer.