Ignore:
Timestamp:
2011-03-22T22:00:02+01:00 (14 years ago)
Author:
zverik
Message:

relcontext is in workable state (although far from feature complete :)

Location:
applications/editors/josm/plugins/relcontext/src/relcontext
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/relcontext/src/relcontext/ChosenRelation.java

    r25667 r25669  
    11package relcontext;
    22
    3 import java.awt.AlphaComposite;
    4 import java.awt.BasicStroke;
    5 import java.awt.Color;
    6 import java.awt.Graphics2D;
    7 import java.awt.Point;
     3import java.awt.*;
    84import java.awt.geom.GeneralPath;
    9 import java.util.ArrayList;
    10 import java.util.Collection;
    11 import java.util.List;
    12 import java.util.Set;
     5import java.util.*;
    136import org.openstreetmap.josm.Main;
    147import org.openstreetmap.josm.data.Bounds;
    15 import org.openstreetmap.josm.data.osm.Node;
    16 import org.openstreetmap.josm.data.osm.OsmPrimitive;
    17 import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    18 import org.openstreetmap.josm.data.osm.Relation;
    19 import org.openstreetmap.josm.data.osm.Way;
     8import org.openstreetmap.josm.data.osm.*;
     9import org.openstreetmap.josm.data.osm.event.*;
    2010import org.openstreetmap.josm.gui.MapView;
    2111import org.openstreetmap.josm.gui.MapView.EditLayerChangeListener;
    22 import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
    23 import org.openstreetmap.josm.gui.layer.Layer;
    2412import org.openstreetmap.josm.gui.layer.MapViewPaintable;
    2513import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     
    3018 * @author Zverik
    3119 */
    32 public class ChosenRelation implements EditLayerChangeListener, MapViewPaintable {
     20public class ChosenRelation implements EditLayerChangeListener, MapViewPaintable, DataSetListener {
    3321    private Relation chosenRelation = null;
    34     private List<ChosenRelationListener> chosenRelationListeners = new ArrayList<ChosenRelationListener>();
     22    private Set<ChosenRelationListener> chosenRelationListeners = new HashSet<ChosenRelationListener>();
    3523
    3624    public void set( Relation rel ) {
     
    4230        analyse();
    4331        Main.map.mapView.repaint();
    44         for( ChosenRelationListener listener : chosenRelationListeners ) {
     32        fireRelationChanged(oldRel);
     33    }
     34
     35    protected void fireRelationChanged( Relation oldRel ) {
     36        for( ChosenRelationListener listener : chosenRelationListeners )
    4537            listener.chosenRelationChanged(oldRel, chosenRelation);
    46         }
    47         return;
    4838    }
    4939
     
    9181        }
    9282
     83        Stroke oldStroke = g.getStroke();
     84        Composite oldComposite = g.getComposite();
    9385        g.setColor(Color.yellow);
    9486        g.setStroke(new BasicStroke(6, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
     
    116108            // todo: closedway, multipolygon - ?
    117109        }
    118         g.setStroke(new BasicStroke(1)); // from building_tools; is it really needed?
     110        g.setStroke(oldStroke);
     111        g.setComposite(oldComposite);
    119112    }
     113
     114    public void relationMembersChanged( RelationMembersChangedEvent event ) {
     115        if( chosenRelation != null && event.getRelation().equals(chosenRelation) )
     116            fireRelationChanged(chosenRelation);
     117    }
     118   
     119    public void tagsChanged( TagsChangedEvent event ) {
     120        if( chosenRelation != null && event.getPrimitive().equals(chosenRelation) )
     121            fireRelationChanged(chosenRelation);
     122    }
     123
     124    public void dataChanged( DataChangedEvent event ) {
     125        if( chosenRelation != null )
     126            fireRelationChanged(chosenRelation);
     127    }
     128
     129    public void nodeMoved( NodeMovedEvent event ) {}
     130    public void otherDatasetChange( AbstractDatasetChangedEvent event ) {}
     131    public void primtivesAdded( PrimitivesAddedEvent event ) {}
     132    public void primtivesRemoved( PrimitivesRemovedEvent event ) {}
     133    public void wayNodesChanged( WayNodesChangedEvent event ) {}
    120134}
  • applications/editors/josm/plugins/relcontext/src/relcontext/ChosenRelationComponent.java

    r25667 r25669  
    3232            public void mouseClicked( MouseEvent e ) {
    3333                if( chRel.get() != null && Main.map.mapView.getEditLayer() != null )
    34                     Main.map.mapView.getEditLayer().data.setSelected(chRel.get().getMemberPrimitives());
     34                    Main.map.mapView.getEditLayer().data.setSelected(chRel.get());
    3535            }
    3636        });
     
    3838
    3939    public void chosenRelationChanged( Relation oldRelation, Relation newRelation ) {
    40         setText(newRelation == null ? "" : newRelation.getDisplayName(DefaultNameFormatter.getInstance()));
     40        setText(prepareText(newRelation));
    4141        repaint();
    4242    }
     43   
     44    private final static String[] typeKeys = new String[] {
     45        "natural", "landuse", "place", "waterway", "leisure", "amenity"
     46    };
     47
     48    protected String prepareText( Relation rel ) {
     49        if( rel == null )
     50            return "";
     51
     52        String type = rel.get("type");
     53        if( type == null || type.length() == 0 )
     54            type = "-";
     55
     56        String tag = null;
     57        for( int i = 0; i < typeKeys.length && tag == null; i++ )
     58            if( rel.hasKey(typeKeys[i]))
     59                tag = typeKeys[i];
     60        if( tag != null )
     61            tag = tag.substring(0, 2) + "=" + rel.get(tag);
     62
     63        String name = rel.get("name");
     64        if( name == null && rel.hasKey("place_name") )
     65            name = rel.get("place_name");
     66
     67        StringBuilder sb = new StringBuilder();
     68        sb.append(type.substring(0, 1));
     69        if( type.equals("boundary") && rel.hasKey("admin_level") )
     70            sb.append(rel.get("admin_level"));
     71        if( name != null )
     72            sb.append(" \"").append(name).append('"');
     73        if( tag != null )
     74            sb.append("; ").append(tag);
     75
     76        return sb.toString();
     77    }
    4378}
  • applications/editors/josm/plugins/relcontext/src/relcontext/RelContextDialog.java

    r25667 r25669  
    11package relcontext;
    22
    3 import java.awt.event.MouseEvent;
    4 import java.util.Collection;
    5 import org.openstreetmap.josm.data.osm.OsmPrimitive;
    6 import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     3import java.beans.PropertyChangeEvent;
    74import static org.openstreetmap.josm.tools.I18n.tr;
    85
     
    107import java.awt.FlowLayout;
    118import java.awt.event.KeyEvent;
     9import java.awt.event.MouseEvent;
    1210import java.awt.event.MouseAdapter;
    13 import java.util.Collections;
    14 import java.util.HashSet;
    15 import java.util.Set;
    16 import javax.swing.DefaultListModel;
    17 import javax.swing.JButton;
     11import java.beans.PropertyChangeListener;
    1812
    19 import javax.swing.JList;
    20 import javax.swing.JPanel;
    21 import javax.swing.JScrollPane;
    22 import javax.swing.ListSelectionModel;
    2313import org.openstreetmap.josm.Main;
    2414import org.openstreetmap.josm.data.SelectionChangedListener;
     
    2616import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
    2717import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
    28 
     18import org.openstreetmap.josm.data.osm.OsmPrimitive;
     19import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    2920import org.openstreetmap.josm.gui.MapView;
    3021import org.openstreetmap.josm.gui.MapView.EditLayerChangeListener;
    31 import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
    3222import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
    3323import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
    34 import org.openstreetmap.josm.gui.layer.Layer;
    3524import org.openstreetmap.josm.tools.Shortcut;
    36 import relcontext.actions.AddRemoveMemberAction;
    37 import relcontext.actions.ClearChosenRelationAction;
    38 import relcontext.actions.CreateRelationAction;
    39 import relcontext.actions.DownloadChosenRelationAction;
    40 import relcontext.actions.EditChosenRelationAction;
     25
     26import java.util.*;
     27import javax.swing.*;
     28import org.openstreetmap.josm.data.osm.DataSet;
     29import org.openstreetmap.josm.data.osm.event.DatasetEventManager;
     30import relcontext.actions.*;
    4131
    4232/**
     
    4939    private final DefaultListModel relationsData;
    5040    private ChosenRelation chosenRelation;
     41    private JPanel topLine;
    5142
    5243    public RelContextDialog() {
     
    8475
    8576        // [±][X] relation U [AZ][Down][Edit]
    86         JPanel topLine = new JPanel(new BorderLayout());
     77        topLine = new JPanel(new BorderLayout());
    8778        JPanel topLeftButtons = new JPanel(new FlowLayout(FlowLayout.LEFT));
    8879        topLeftButtons.add(new JButton(new AddRemoveMemberAction(chosenRelation)));
     
    9182        topLine.add(new ChosenRelationComponent(chosenRelation), BorderLayout.CENTER);
    9283        JPanel topRightButtons = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    93         topRightButtons.add(new JButton(new DownloadChosenRelationAction(chosenRelation)));
     84        final Action downloadChosenRelationAction = new DownloadChosenRelationAction(chosenRelation);
     85        final JButton downloadButton = new JButton(downloadChosenRelationAction);
     86        topRightButtons.add(downloadButton);
    9487        topRightButtons.add(new JButton(new EditChosenRelationAction(chosenRelation)));
    9588        topLine.add(topRightButtons, BorderLayout.EAST);
    9689        rcPanel.add(topLine, BorderLayout.NORTH);
    9790
     91        downloadChosenRelationAction.addPropertyChangeListener(new PropertyChangeListener() {
     92            public void propertyChange( PropertyChangeEvent evt ) {
     93                downloadButton.setVisible(downloadChosenRelationAction.isEnabled());
     94            }
     95        });
     96        downloadButton.setVisible(false);
     97        topLine.setVisible(false);
     98
    9899        // [+][Multi] [X]Adm [X]Tags [X]1
    99100        JPanel bottomLine = new JPanel(new FlowLayout(FlowLayout.LEFT));
    100101        bottomLine.add(new JButton(new CreateRelationAction(chosenRelation)));
     102        bottomLine.add(new JButton(new CreateMultipolygonAction(chosenRelation)));
    101103        rcPanel.add(bottomLine, BorderLayout.SOUTH);
    102104
     
    108110        SelectionEventManager.getInstance().removeSelectionListener(this);
    109111        MapView.removeEditLayerChangeListener(this);
     112        DatasetEventManager.getInstance().removeDatasetListener(chosenRelation);
    110113    }
    111114
     
    114117        SelectionEventManager.getInstance().addSelectionListener(this, FireMode.IN_EDT_CONSOLIDATED);
    115118        MapView.addEditLayerChangeListener(this);
     119        DatasetEventManager.getInstance().addDatasetListener(chosenRelation, FireMode.IN_EDT);
    116120    }
    117 
    118 
    119121
    120122    public ChosenRelation getChosenRelation() {
     
    123125
    124126    public void chosenRelationChanged( Relation oldRelation, Relation newRelation ) {
     127        if( topLine != null )
     128            topLine.setVisible(newRelation != null);
    125129        // ?
    126130    }
  • applications/editors/josm/plugins/relcontext/src/relcontext/actions/AddRemoveMemberAction.java

    r25667 r25669  
    11package relcontext.actions;
    22
    3 import java.util.Collection;
     3import java.util.*;
    44import org.openstreetmap.josm.data.osm.OsmPrimitive;
    55import static org.openstreetmap.josm.tools.I18n.tr;
    66import java.awt.event.ActionEvent;
     7import javax.swing.Action;
     8import org.openstreetmap.josm.Main;
    79import org.openstreetmap.josm.actions.JosmAction;
     10import org.openstreetmap.josm.command.ChangeCommand;
    811import org.openstreetmap.josm.data.osm.Relation;
     12import org.openstreetmap.josm.data.osm.RelationMember;
    913import relcontext.ChosenRelation;
    1014import relcontext.ChosenRelationListener;
     
    2933
    3034    public void actionPerformed( ActionEvent e ) {
    31         // todo
     35        if( rel.get() == null )
     36            return;
     37
     38        Relation r = new Relation(rel.get());
     39
     40        Collection<OsmPrimitive> toAdd = new ArrayList<OsmPrimitive>(getCurrentDataSet().getSelected());
     41        toAdd.remove(rel.get());
     42        toAdd.removeAll(r.getMemberPrimitives());
     43
     44        // 1. remove all present members
     45        r.removeMembersFor(getCurrentDataSet().getSelected());
     46
     47        // 2. add all new members
     48        for( OsmPrimitive p : toAdd ) {
     49            r.addMember(new RelationMember("", p));
     50        }
     51
     52        if( !r.getMemberPrimitives().equals(rel.get().getMemberPrimitives()) )
     53            Main.main.undoRedo.add(new ChangeCommand(rel.get(), r));
    3254    }
    3355
    3456    public void chosenRelationChanged( Relation oldRelation, Relation newRelation ) {
    35         setEnabled(newRelation != null && getCurrentDataSet() != null);
     57        updateEnabledState();
    3658    }
    3759
    3860    @Override
    3961    protected void updateEnabledState() {
    40         if( getCurrentDataSet() == null ) {
    41             setEnabled(false);
    42         } else {
    43             updateEnabledState(getCurrentDataSet().getSelected());
    44         }
     62        updateEnabledState(getCurrentDataSet() == null ? null : getCurrentDataSet().getSelected());
    4563    }
    4664
    4765    @Override
    4866    protected void updateEnabledState( Collection<? extends OsmPrimitive> selection ) {
    49         if( rel.get() == null || selection == null || selection.isEmpty() ) {
     67        updateIcon();
     68        if( rel == null || rel.get() == null || selection == null || selection.isEmpty() ) {
     69            setEnabled(false);
     70            return;
     71        }
     72        if( selection.size() == 1 && selection.contains(rel.get()) ) {
    5073            setEnabled(false);
    5174            return;
    5275        }
    5376        setEnabled(true);
     77    }
     78
     79    protected void updateIcon() {
    5480        // todo: change icon based on selection
     81        String name = "";
     82        if( getCurrentDataSet() == null || getCurrentDataSet().getSelected() == null
     83                || getCurrentDataSet().getSelected().size() == 0 || rel == null || rel.get() == null )
     84            name = "?";
     85        else {
     86            Collection<OsmPrimitive> toAdd = new ArrayList<OsmPrimitive>(getCurrentDataSet().getSelected());
     87            toAdd.remove(rel.get());
     88            int selectedSize = toAdd.size();
     89            if( selectedSize == 0 )
     90                name = "?";
     91            else {
     92                toAdd.removeAll(rel.get().getMemberPrimitives());
     93                if( toAdd.isEmpty() )
     94                    name = "-";
     95                else if( toAdd.size() < selectedSize )
     96                    name = "±";
     97                else
     98                    name = "+";
     99            }
     100        }
     101        putValue(Action.NAME, name);
    55102    }
    56103}
  • applications/editors/josm/plugins/relcontext/src/relcontext/actions/CreateRelationAction.java

    r25667 r25669  
    4646        }
    4747
    48         Collection<Command> cmds = new LinkedList<Command>();
    4948        Main.main.undoRedo.add(new AddCommand(rel));
    5049
  • applications/editors/josm/plugins/relcontext/src/relcontext/actions/DownloadChosenRelationAction.java

    r25667 r25669  
    99import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1010import org.openstreetmap.josm.data.osm.Relation;
     11import org.openstreetmap.josm.data.osm.RelationMember;
    1112import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationMemberTask;
    1213import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationTask;
     
    3132    public void actionPerformed( ActionEvent e ) {
    3233        Relation relation = rel.get();
    33         if( relation == null || relation.isNew() || !relation.isIncomplete() ) return;
     34        if( relation == null || relation.isNew() ) return;
    3435        int total = relation.getMembersCount();
    3536        int incomplete = relation.getIncompleteMembers().size();
    36         if( incomplete <= 5 && incomplete * 2 < total )
     37        if( incomplete <= 5 || (incomplete <= 10 && incomplete * 3 < total) )
    3738            downloadIncomplete(relation);
    3839        else
     
    4142
    4243    public void chosenRelationChanged( Relation oldRelation, Relation newRelation ) {
    43         setEnabled(newRelation != null && newRelation.isIncomplete());
     44        boolean incomplete = false;
     45        if( newRelation != null ) {
     46            for( RelationMember m : newRelation.getMembers()) {
     47                if( m.getMember().isIncomplete() ) {
     48                    incomplete = true;
     49                    break;
     50                }
     51            }
     52        }
     53        setEnabled(newRelation != null && incomplete);
    4454    }
    4555
Note: See TracChangeset for help on using the changeset viewer.