Changeset 1113 in josm for trunk/src


Ignore:
Timestamp:
2008-12-11T23:52:36+01:00 (16 years ago)
Author:
framm
Message:
  • add support for ordered relations (triggered if version is set to 0.6)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/RelationEditor.java

    r999 r1113  
    1414import java.beans.PropertyChangeListener;
    1515import java.io.IOException;
     16import java.lang.reflect.Array;
    1617import java.text.Collator;
    1718import java.util.ArrayList;
     19import java.util.Arrays;
    1820import java.util.Collection;
    1921import java.util.Collections;
     
    2123import java.util.Map.Entry;
    2224
     25import javax.swing.BoxLayout;
    2326import javax.swing.JButton;
    2427import javax.swing.JFrame;
     
    7073        private JLabel status;
    7174
     75    /**
     76     * True if the relation is ordered (API 0.6). False for API 0.5.
     77     */
     78    boolean ordered;
     79
    7280        /**
    7381         * The property data.
     
    100108        private final JTable memberTable = new JTable(memberData);
    101109
     110    // =================== FIXME FIXME FIXME =====================
     111    // As soon as API 0.5 is dead, drop all the collation stuff from here ...
     112   
    102113        /**
    103114         * Collator for sorting the roles and entries of the member table.
     
    164175        };
    165176
     177    // =================== FIXME FIXME FIXME =====================
     178    // ... until here, and also get rid of the "Collections.sort..." below.
     179   
    166180        /**
    167181         * Creates a new relation editor for the given relation. The relation
     
    192206                this.relation = relation;
    193207
     208        ordered = Main.pref.get("osm-server.version", "0.5").equals("0.6");
     209       
    194210                if (relation == null) {
    195211                        // create a new relation
     
    198214                        // edit an existing relation
    199215                        this.clone = new Relation(relation);
    200                         Collections.sort(this.clone.members, memberComparator);
     216                        if (!ordered) Collections.sort(this.clone.members, memberComparator);
    201217                }
    202218
     
    278294                memberTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
    279295
    280 
    281296                // combine both tables and wrap them in a scrollPane
    282297                JPanel bothTables = new JPanel();
     
    285300                bothTables.add(new JScrollPane(propertyTable), GBC.eop().fill(GBC.BOTH));
    286301                bothTables.add(status = new JLabel(tr("Members")), GBC.eol().fill(GBC.HORIZONTAL));
    287                 bothTables.add(new JScrollPane(memberTable), GBC.eol().fill(GBC.BOTH));
    288 
     302        if (ordered) {
     303            JPanel upDownPanel = new JPanel();
     304            upDownPanel.setLayout(new BoxLayout(upDownPanel, BoxLayout.Y_AXIS));
     305
     306           
     307
     308            upDownPanel.add(createButton(null, "moveup", tr("Move the currently selected member(s) up"),
     309                    KeyEvent.VK_U, new ActionListener() {
     310                public void actionPerformed(ActionEvent e) {
     311                    moveMembers(-1);
     312                }
     313            }));
     314            upDownPanel.add(createButton(null, "movedown", tr("Move the currently selected member(s) down"),
     315                    KeyEvent.VK_N, new ActionListener() {
     316                public void actionPerformed(ActionEvent e) {
     317                    moveMembers(1);
     318                }
     319            }));
     320
     321           
     322            bothTables.add(new JScrollPane(memberTable), GBC.std().fill(GBC.BOTH));
     323            bothTables.add(upDownPanel, GBC.eol().fill(GBC.VERTICAL));
     324        } else {
     325            bothTables.add(new JScrollPane(memberTable), GBC.eol().fill(GBC.BOTH));
     326        }
     327       
    289328                JPanel buttonPanel = new JPanel(new GridLayout(1,3));
    290329
     
    297336
    298337                buttonPanel.add(createButton(marktr("Delete Selected"),"deleteselected",
    299                 tr("Delete all currently selected objects from releation"), KeyEvent.VK_R, new ActionListener() {
     338                tr("Delete all currently selected objects from relation"), KeyEvent.VK_R, new ActionListener() {
    300339                        public void actionPerformed(ActionEvent e) {
    301340                                deleteSelected();
     
    406445                for (OsmPrimitive p : Main.ds.getSelected()) {
    407446                        boolean skip = false;
    408                         for (RelationMember rm : clone.members) {
    409                                 if (rm.member == p || p == relation)
    410                                 {
    411                                         skip = true;
    412                                         break;
    413                                 }
    414                         }
    415                         if(!skip)
     447            // ordered relations may have the same member multiple times.
     448            // TODO: visual indication of the fact that one is there more than once?
     449            if (!ordered)
     450            {
     451                for (RelationMember rm : clone.members) {
     452                    if (rm.member == p || p == relation)
     453                    {
     454                        skip = true;
     455                        break;
     456                    }
     457                }
     458            }
     459                        if (!skip)
    416460                        {
    417461                                RelationMember em = new RelationMember();
    418462                                em.member = p;
    419463                                em.role = "";
    420                                 clone.members.add(em);
     464                // when working with ordered relations, we make an effort to
     465                // add the element before the first selected member.
     466                int[] rows = memberTable.getSelectedRows();
     467                if (ordered && rows.length > 0) {
     468                    clone.members.add(rows[0], em);
     469                } else {
     470                    clone.members.add(em);
     471                }
    421472                        }
    422473                }
     
    438489                }
    439490                refreshTables();
     491        }
     492
     493        private void moveMembers(int direction) {
     494            int[] rows = memberTable.getSelectedRows();
     495        if (rows.length == 0) return;
     496       
     497        // check if user attempted to move anything beyond the boundary of the list
     498        if (rows[0] + direction < 0) return;
     499        if (rows[rows.length-1] + direction >= clone.members.size()) return;
     500       
     501        RelationMember m[] = new RelationMember[clone.members.size()];
     502       
     503        // first move all selected rows from the member list into a new array,
     504        // displaced by the move amount
     505        for (Integer i: rows) {
     506            m[i+direction] = clone.members.get(i);
     507            clone.members.set(i, null);           
     508        }
     509       
     510        // now fill the empty spots in the destination array with the remaining
     511        // elements.
     512        int i = 0;
     513        for (RelationMember rm : clone.members) {
     514            if (rm != null) {
     515                while (m[i] != null) i++;
     516                m[i++] = rm;
     517            }
     518        }
     519       
     520        // and write the array back into the member list.
     521        clone.members.clear();
     522        clone.members.addAll(Arrays.asList(m));       
     523            refreshTables();
     524        ListSelectionModel lsm = memberTable.getSelectionModel();
     525        lsm.setValueIsAdjusting(true);
     526        for (Integer j: rows) {
     527            lsm.addSelectionInterval(j + direction, j + direction);
     528        }
     529        lsm.setValueIsAdjusting(false);
    440530        }
    441531
Note: See TracChangeset for help on using the changeset viewer.