Ticket #2226: clickable presets.patch
| File clickable presets.patch, 8.8 KB (added by , 17 years ago) |
|---|
-
src/org/openstreetmap/josm/gui/dialogs/PropertiesDialog.java
8 8 9 9 import java.awt.BorderLayout; 10 10 import java.awt.Component; 11 import java.awt.Cursor; 12 import java.awt.Dimension; 11 13 import java.awt.Font; 12 14 import java.awt.GridBagLayout; 13 15 import java.awt.GridLayout; … … 18 20 import java.awt.event.KeyEvent; 19 21 import java.awt.event.MouseAdapter; 20 22 import java.awt.event.MouseEvent; 21 import java. lang.String;23 import java.awt.event.MouseListener; 22 24 import java.util.Collection; 23 25 import java.util.Collections; 24 26 import java.util.HashMap; … … 59 61 import org.openstreetmap.josm.data.osm.Way; 60 62 import org.openstreetmap.josm.data.osm.visitor.NameVisitor; 61 63 import org.openstreetmap.josm.gui.ExtendedDialog; 62 import org.openstreetmap.josm.gui.JMultilineLabel;63 64 import org.openstreetmap.josm.gui.MapFrame; 64 65 import org.openstreetmap.josm.gui.SideButton; 65 66 import org.openstreetmap.josm.gui.preferences.TaggingPresetPreference; … … 440 441 private final SideButton btnAdd; 441 442 private final SideButton btnEdit; 442 443 private final SideButton btnDel; 443 private final J MultilineLabel presets = new JMultilineLabel("");444 private final JPanel presets = new JPanel(new GridBagLayout()); 444 445 445 446 private final JLabel selectSth = new JLabel("<html><p>" + tr("Please select the objects you want to change properties for.") + "</p></html>"); 446 447 … … 519 520 return c; 520 521 } 521 522 }); 523 522 524 523 525 // combine both tables and wrap them in a scrollPane 524 526 JPanel bothTables = new JPanel(); 525 527 bothTables.setLayout(new GridBagLayout()); 528 bothTables.add(presets, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 2, 5, 2)); 526 529 bothTables.add(selectSth, GBC.eol().fill().insets(10, 10, 10, 10)); 527 530 bothTables.add(propertyTable.getTableHeader(), GBC.eol().fill(GBC.HORIZONTAL)); 528 531 bothTables.add(propertyTable, GBC.eol().fill(GBC.BOTH)); 529 532 bothTables.add(membershipTable.getTableHeader(), GBC.eol().fill(GBC.HORIZONTAL)); 530 533 bothTables.add(membershipTable, GBC.eol().fill(GBC.BOTH)); 531 bothTables.add(presets, GBC.eol().fill().insets(5, 2, 5, 2));532 534 533 535 DblClickWatch dblClickWatch = new DblClickWatch(); 534 536 propertyTable.addMouseListener(dblClickWatch); … … 538 540 add(scrollPane, BorderLayout.CENTER); 539 541 540 542 selectSth.setPreferredSize(scrollPane.getSize()); 541 presets.set PreferredSize(scrollPane.getSize());543 presets.setSize(scrollPane.getSize()); 542 544 543 545 JPanel buttonPanel = new JPanel(new GridLayout(1,3)); 544 546 ActionListener buttonAction = new ActionListener(){ … … 627 629 628 630 private void checkPresets(int nodes, int ways, int relations, int closedways) 629 631 { 632 /** 633 * Small helper class that manages the highlighting of the label on hover as well as opening 634 * the corresponding preset when clicked 635 */ 636 class PresetLabelML implements MouseListener { 637 JLabel label; 638 Font bold; 639 Font normal; 640 TaggingPreset tag; 641 PresetLabelML(JLabel lbl, TaggingPreset t) { 642 super(); 643 label = lbl; 644 lbl.setCursor(new Cursor(Cursor.HAND_CURSOR)); 645 normal = label.getFont(); 646 bold = normal.deriveFont(normal.getStyle() ^ Font.BOLD); 647 tag = t; 648 } 649 public void mouseClicked(MouseEvent arg0) { 650 tag.actionPerformed(null); 651 } 652 public void mouseEntered(MouseEvent arg0) { 653 label.setFont(bold); 654 } 655 public void mouseExited(MouseEvent arg0) { 656 label.setFont(normal); 657 } 658 public void mousePressed(MouseEvent arg0) {} 659 public void mouseReleased(MouseEvent arg0) {} 660 } 661 630 662 LinkedList<TaggingPreset> p = new LinkedList<TaggingPreset>(); 663 presets.removeAll(); 631 664 int total = nodes+ways+relations+closedways; 632 if(total != 0) 633 { 634 for(TaggingPreset t : TaggingPresetPreference.taggingPresets) 665 if(total == 0) { 666 presets.setVisible(false); 667 return; 668 } 669 670 for(TaggingPreset t : TaggingPresetPreference.taggingPresets) { 671 if(t.types == null || !((relations > 0 && !t.types.contains("relation")) && 672 (nodes > 0 && !t.types.contains("node")) && 673 (ways+closedways > 0 && !t.types.contains("way")) && 674 (closedways > 0 && !t.types.contains("closedway")))) 635 675 { 636 if(t.types == null || !((relations > 0 && !t.types.contains("relation")) && 637 (nodes > 0 && !t.types.contains("node")) && 638 (ways+closedways > 0 && !t.types.contains("way")) && 639 (closedways > 0 && !t.types.contains("closedway")))) 640 { 641 int found = 0; 642 for(TaggingPreset.Item i : t.data) 643 { 644 if(i instanceof TaggingPreset.Key) 645 { 646 String val = ((TaggingPreset.Key)i).value; 647 String key = ((TaggingPreset.Key)i).key; 648 // we subtract 100 if not found and add 1 if found 649 found -= 100; 650 if(valueCount.containsKey(key)) 651 { 652 Map<String, Integer> v = valueCount.get(key); 653 if(v.size() == 1 && v.containsKey(val) && v.get(val) == total) 654 { 655 found += 101; 656 } 657 } 658 } 659 } 660 if(found > 0) 661 p.add(t); 676 int found = 0; 677 for(TaggingPreset.Item i : t.data) { 678 if(!(i instanceof TaggingPreset.Key)) 679 continue; 680 String val = ((TaggingPreset.Key)i).value; 681 String key = ((TaggingPreset.Key)i).key; 682 // we subtract 100 if not found and add 1 if found 683 found -= 100; 684 if(!valueCount.containsKey(key)) 685 continue; 686 687 Map<String, Integer> v = valueCount.get(key); 688 if(v.size() == 1 && v.containsKey(val) && v.get(val) == total) 689 found += 101; 662 690 } 691 692 if(found <= 0) 693 continue; 694 695 JLabel lbl = new JLabel(t.getName()); 696 lbl.addMouseListener(new PresetLabelML(lbl, t)); 697 presets.add(lbl, GBC.eol().fill(GBC.HORIZONTAL)); 663 698 } 664 699 } 665 String t = ""; 666 for(TaggingPreset tp : p) 667 { 668 if(t.length() > 0) 669 t += "\n"; 670 t += tp.getName(); 671 } 672 presets.setText(t); 673 presets.setVisible(t.length() > 0); 700 701 if(presets.getComponentCount() > 0) { 702 presets.setVisible(true); 703 // This ensures the presets are exactly as high as needed. 704 int height = presets.getComponentCount() * presets.getComponent(0).getHeight(); 705 Dimension size = new Dimension(presets.getWidth(), height); 706 presets.setMaximumSize(size); 707 presets.setMinimumSize(size); 708 } else 709 presets.setVisible(false); 674 710 } 675 711 676 712 public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) { … … 743 779 for (Entry<Relation, Collection<RelationMember>> e : roles.entrySet()) { 744 780 membershipData.addRow(new Object[]{e.getKey(), e.getValue()}); 745 781 } 782 783 checkPresets(nodes, ways, relations, closedways); 746 784 747 785 membershipTable.getTableHeader().setVisible(membershipData.getRowCount() > 0); 748 786 membershipTable.setVisible(membershipData.getRowCount() > 0); … … 759 797 if(hasTags) propertyTable.changeSelection(0, 0, false, false); 760 798 else if(hasMemberships) membershipTable.changeSelection(0, 0, false, false); 761 799 762 checkPresets(nodes, ways, relations, closedways);763 764 800 if(propertyData.getRowCount() != 0 || membershipData.getRowCount() != 0) { 765 801 setTitle(tr("Properties: {0} / Memberships: {1}", 766 802 propertyData.getRowCount(), membershipData.getRowCount()), true);
