Ticket #2226: clickable presets.patch

File clickable presets.patch, 8.8 KB (added by xeen, 17 years ago)
  • src/org/openstreetmap/josm/gui/dialogs/PropertiesDialog.java

     
    88
    99import java.awt.BorderLayout;
    1010import java.awt.Component;
     11import java.awt.Cursor;
     12import java.awt.Dimension;
    1113import java.awt.Font;
    1214import java.awt.GridBagLayout;
    1315import java.awt.GridLayout;
     
    1820import java.awt.event.KeyEvent;
    1921import java.awt.event.MouseAdapter;
    2022import java.awt.event.MouseEvent;
    21 import java.lang.String;
     23import java.awt.event.MouseListener;
    2224import java.util.Collection;
    2325import java.util.Collections;
    2426import java.util.HashMap;
     
    5961import org.openstreetmap.josm.data.osm.Way;
    6062import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
    6163import org.openstreetmap.josm.gui.ExtendedDialog;
    62 import org.openstreetmap.josm.gui.JMultilineLabel;
    6364import org.openstreetmap.josm.gui.MapFrame;
    6465import org.openstreetmap.josm.gui.SideButton;
    6566import org.openstreetmap.josm.gui.preferences.TaggingPresetPreference;
     
    440441    private final SideButton btnAdd;
    441442    private final SideButton btnEdit;
    442443    private final SideButton btnDel;
    443     private final JMultilineLabel presets = new JMultilineLabel("");
     444    private final JPanel presets = new JPanel(new GridBagLayout());
    444445
    445446    private final JLabel selectSth = new JLabel("<html><p>" + tr("Please select the objects you want to change properties for.") + "</p></html>");
    446447
     
    519520                return c;
    520521            }
    521522        });
     523       
    522524
    523525        // combine both tables and wrap them in a scrollPane
    524526        JPanel bothTables = new JPanel();
    525527        bothTables.setLayout(new GridBagLayout());
     528        bothTables.add(presets, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 2, 5, 2));
    526529        bothTables.add(selectSth, GBC.eol().fill().insets(10, 10, 10, 10));
    527530        bothTables.add(propertyTable.getTableHeader(), GBC.eol().fill(GBC.HORIZONTAL));
    528531        bothTables.add(propertyTable, GBC.eol().fill(GBC.BOTH));
    529532        bothTables.add(membershipTable.getTableHeader(), GBC.eol().fill(GBC.HORIZONTAL));
    530533        bothTables.add(membershipTable, GBC.eol().fill(GBC.BOTH));
    531         bothTables.add(presets, GBC.eol().fill().insets(5, 2, 5, 2));
    532534
    533535        DblClickWatch dblClickWatch = new DblClickWatch();
    534536        propertyTable.addMouseListener(dblClickWatch);
     
    538540        add(scrollPane, BorderLayout.CENTER);
    539541
    540542        selectSth.setPreferredSize(scrollPane.getSize());
    541         presets.setPreferredSize(scrollPane.getSize());
     543        presets.setSize(scrollPane.getSize());
    542544
    543545        JPanel buttonPanel = new JPanel(new GridLayout(1,3));
    544546        ActionListener buttonAction = new ActionListener(){
     
    627629
    628630    private void checkPresets(int nodes, int ways, int relations, int closedways)
    629631    {
     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       
    630662        LinkedList<TaggingPreset> p = new LinkedList<TaggingPreset>();
     663        presets.removeAll();
    631664        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"))))
    635675            {
    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;
    662690                }
     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));
    663698            }
    664699        }
    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);
    674710    }
    675711
    676712    public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
     
    743779        for (Entry<Relation, Collection<RelationMember>> e : roles.entrySet()) {
    744780            membershipData.addRow(new Object[]{e.getKey(), e.getValue()});
    745781        }
     782       
     783        checkPresets(nodes, ways, relations, closedways);
    746784
    747785        membershipTable.getTableHeader().setVisible(membershipData.getRowCount() > 0);
    748786        membershipTable.setVisible(membershipData.getRowCount() > 0);
     
    759797        if(hasTags) propertyTable.changeSelection(0, 0, false, false);
    760798        else if(hasMemberships) membershipTable.changeSelection(0, 0, false, false);
    761799
    762         checkPresets(nodes, ways, relations, closedways);
    763 
    764800        if(propertyData.getRowCount() != 0 || membershipData.getRowCount() != 0) {
    765801            setTitle(tr("Properties: {0} / Memberships: {1}",
    766802                propertyData.getRowCount(), membershipData.getRowCount()), true);