Index: /applications/editors/josm/plugins/relcontext/build.xml
===================================================================
--- /applications/editors/josm/plugins/relcontext/build.xml	(revision 25666)
+++ /applications/editors/josm/plugins/relcontext/build.xml	(revision 25667)
@@ -28,5 +28,5 @@
 **
 -->
-<project name="myPluginName" default="dist" basedir=".">
+<project name="RelContext" default="dist" basedir=".">
 
     <!-- enter the SVN commit message -->
@@ -44,7 +44,6 @@
     <property name="plugin.src.dir"         value="src"/>
     <!-- this is the directory where the plugin jar is copied to -->
-    <property name="plugin.dist.dir"        value="../../dist"/>
+    <property name="plugin.dist.dir"        value="/Users/Zverik/AppData/Roaming/JOSM/plugins"/>
     <property name="ant.build.javac.target" value="1.5"/>
-    <property name="plugin.dist.dir"        value="../../dist"/>
     <property name="plugin.jar"             value="${plugin.dist.dir}/${ant.project.name}.jar"/>
 
@@ -78,7 +77,4 @@
     <target name="dist" depends="compile,revision">
         <echo message="creating ${ant.project.name}.jar ... "/>
-        <copy todir="${plugin.build.dir}/resources">
-            <fileset dir="resources"/>
-        </copy>
         <copy todir="${plugin.build.dir}/images">
             <fileset dir="images"/>
Index: /applications/editors/josm/plugins/relcontext/src/relcontext/ChosenRelation.java
===================================================================
--- /applications/editors/josm/plugins/relcontext/src/relcontext/ChosenRelation.java	(revision 25666)
+++ /applications/editors/josm/plugins/relcontext/src/relcontext/ChosenRelation.java	(revision 25667)
@@ -41,4 +41,5 @@
         chosenRelation = rel;
         analyse();
+        Main.map.mapView.repaint();
         for( ChosenRelationListener listener : chosenRelationListeners ) {
             listener.chosenRelationChanged(oldRel, chosenRelation);
@@ -92,5 +93,5 @@
         g.setColor(Color.yellow);
         g.setStroke(new BasicStroke(6, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
-        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f));
+        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
         for( OsmPrimitive element : chosenRelation.getMemberPrimitives() ) {
             if( element.getType() == OsmPrimitiveType.NODE ) {
@@ -105,5 +106,5 @@
                     b.moveTo(p.x, p.y);
                     for( int i = 1; i < way.getNodesCount(); i++ ) {
-                        p = mv.getPoint(way.getNode(1));
+                        p = mv.getPoint(way.getNode(i));
                         b.lineTo(p.x, p.y);
                     }
Index: /applications/editors/josm/plugins/relcontext/src/relcontext/ChosenRelationComponent.java
===================================================================
--- /applications/editors/josm/plugins/relcontext/src/relcontext/ChosenRelationComponent.java	(revision 25667)
+++ /applications/editors/josm/plugins/relcontext/src/relcontext/ChosenRelationComponent.java	(revision 25667)
@@ -0,0 +1,43 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package relcontext;
+
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
+import javax.swing.JLabel;
+import org.openstreetmap.josm.Main;
+import org.openstreetmap.josm.data.osm.Relation;
+import org.openstreetmap.josm.gui.DefaultNameFormatter;
+
+/**
+ * Renderer for chosen relation.
+ * [Icon] na=wood U
+ * key is 2-letter; type = icon; to the right — symbol of relation topology (closed, lines, broken).
+ *
+ * @author Zverik
+ */
+public class ChosenRelationComponent extends JLabel implements ChosenRelationListener {
+
+    private ChosenRelation chRel;
+
+    public ChosenRelationComponent(ChosenRelation rel) {
+        super("");
+        this.chRel = rel;
+        rel.addChosenRelationListener(this);
+        addMouseListener(new MouseAdapter() {
+            @Override
+            public void mouseClicked( MouseEvent e ) {
+                if( chRel.get() != null && Main.map.mapView.getEditLayer() != null )
+                    Main.map.mapView.getEditLayer().data.setSelected(chRel.get().getMemberPrimitives());
+            }
+        });
+    }
+
+    public void chosenRelationChanged( Relation oldRelation, Relation newRelation ) {
+        setText(newRelation == null ? "" : newRelation.getDisplayName(DefaultNameFormatter.getInstance()));
+        repaint();
+    }
+}
Index: /applications/editors/josm/plugins/relcontext/src/relcontext/RelContextDialog.java
===================================================================
--- /applications/editors/josm/plugins/relcontext/src/relcontext/RelContextDialog.java	(revision 25666)
+++ /applications/editors/josm/plugins/relcontext/src/relcontext/RelContextDialog.java	(revision 25667)
@@ -2,5 +2,7 @@
 
 import java.awt.event.MouseEvent;
+import java.util.Collection;
 import org.openstreetmap.josm.data.osm.OsmPrimitive;
+import org.openstreetmap.josm.gui.layer.OsmDataLayer;
 import static org.openstreetmap.josm.tools.I18n.tr;
 
@@ -9,4 +11,9 @@
 import java.awt.event.KeyEvent;
 import java.awt.event.MouseAdapter;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+import javax.swing.DefaultListModel;
+import javax.swing.JButton;
 
 import javax.swing.JList;
@@ -15,7 +22,11 @@
 import javax.swing.ListSelectionModel;
 import org.openstreetmap.josm.Main;
+import org.openstreetmap.josm.data.SelectionChangedListener;
 import org.openstreetmap.josm.data.osm.Relation;
+import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
+import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
 
 import org.openstreetmap.josm.gui.MapView;
+import org.openstreetmap.josm.gui.MapView.EditLayerChangeListener;
 import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
 import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
@@ -23,4 +34,9 @@
 import org.openstreetmap.josm.gui.layer.Layer;
 import org.openstreetmap.josm.tools.Shortcut;
+import relcontext.actions.AddRemoveMemberAction;
+import relcontext.actions.ClearChosenRelationAction;
+import relcontext.actions.CreateRelationAction;
+import relcontext.actions.DownloadChosenRelationAction;
+import relcontext.actions.EditChosenRelationAction;
 
 /**
@@ -29,13 +45,16 @@
  * @author Zverik
  */
-public class RelContextDialog extends ToggleDialog implements LayerChangeListener, ChosenRelationListener {
+public class RelContextDialog extends ToggleDialog implements EditLayerChangeListener, ChosenRelationListener, SelectionChangedListener {
     private JList relationsList;
+    private final DefaultListModel relationsData;
     private ChosenRelation chosenRelation;
 
     public RelContextDialog() {
-        super(tr("Open Relation Editor"), "icon_relcontext",
+        super(tr("Advanced Relation Editor"), "icon_relcontext",
                 tr("Opens advanced relation/multipolygon editor panel"),
                 Shortcut.registerShortcut("view:relcontext", tr("Toggle: {0}", tr("Open Relation Editor")),
                 KeyEvent.VK_R, Shortcut.GROUP_LAYER, Shortcut.SHIFT_DEFAULT), 150);
+
+        JPanel rcPanel = new JPanel(new BorderLayout());
 
         chosenRelation = new ChosenRelation();
@@ -43,5 +62,6 @@
         MapView.addEditLayerChangeListener(chosenRelation);
 
-        relationsList = new JList();
+        relationsData = new DefaultListModel();
+        relationsList = new JList(relationsData);
         relationsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
         relationsList.setCellRenderer(new OsmPrimitivRenderer() {
@@ -58,22 +78,43 @@
                 }
                 chosenRelation.set((Relation)relationsList.getSelectedValue());
+                relationsList.clearSelection();
             }
         });
-        add(new JScrollPane(relationsList), BorderLayout.CENTER);
+        rcPanel.add(new JScrollPane(relationsList), BorderLayout.CENTER);
 
         // [±][X] relation U [AZ][Down][Edit]
         JPanel topLine = new JPanel(new BorderLayout());
         JPanel topLeftButtons = new JPanel(new FlowLayout(FlowLayout.LEFT));
+        topLeftButtons.add(new JButton(new AddRemoveMemberAction(chosenRelation)));
+        topLeftButtons.add(new JButton(new ClearChosenRelationAction(chosenRelation)));
+        topLine.add(topLeftButtons, BorderLayout.WEST);
+        topLine.add(new ChosenRelationComponent(chosenRelation), BorderLayout.CENTER);
         JPanel topRightButtons = new JPanel(new FlowLayout(FlowLayout.RIGHT));
-        topLine.add(topLeftButtons, BorderLayout.WEST);
+        topRightButtons.add(new JButton(new DownloadChosenRelationAction(chosenRelation)));
+        topRightButtons.add(new JButton(new EditChosenRelationAction(chosenRelation)));
         topLine.add(topRightButtons, BorderLayout.EAST);
-        add(topLine, BorderLayout.NORTH);
+        rcPanel.add(topLine, BorderLayout.NORTH);
 
         // [+][Multi] [X]Adm [X]Tags [X]1
         JPanel bottomLine = new JPanel(new FlowLayout(FlowLayout.LEFT));
-        add(bottomLine, BorderLayout.SOUTH);
+        bottomLine.add(new JButton(new CreateRelationAction(chosenRelation)));
+        rcPanel.add(bottomLine, BorderLayout.SOUTH);
 
-        MapView.addLayerChangeListener(this);
+        add(rcPanel, BorderLayout.CENTER);
     }
+
+    @Override
+    public void hideNotify() {
+        SelectionEventManager.getInstance().removeSelectionListener(this);
+        MapView.removeEditLayerChangeListener(this);
+    }
+
+    @Override
+    public void showNotify() {
+        SelectionEventManager.getInstance().addSelectionListener(this, FireMode.IN_EDT_CONSOLIDATED);
+        MapView.addEditLayerChangeListener(this);
+    }
+
+
 
     public ChosenRelation getChosenRelation() {
@@ -85,17 +126,33 @@
     }
 
-    @Override
-    public void activeLayerChange( Layer arg0, Layer arg1 ) {
-        // TODO Auto-generated method stub
+    public void selectionChanged( Collection<? extends OsmPrimitive> newSelection ) {
+        if( !isVisible() || relationsData == null )
+            return;
+        // repopulate relations table
+        relationsData.clear();
+        if( newSelection == null )
+            return;
+        Set<Relation> rels = new HashSet<Relation>();
+        for( OsmPrimitive element : newSelection ) {
+            for( OsmPrimitive ref : element.getReferrers() ) {
+                if( ref instanceof Relation && !ref.isIncomplete() && !ref.isDeleted() ) {
+                    rels.add((Relation) ref);
+                }
+            }
+        }
+        for( Relation rel : rels )
+            relationsData.addElement(rel);
     }
 
-    @Override
-    public void layerAdded( Layer arg0 ) {
-        // TODO Auto-generated method stub
+    private void updateSelection() {
+        if (Main.main.getCurrentDataSet() == null) {
+            selectionChanged(Collections.<OsmPrimitive>emptyList());
+        } else {
+            selectionChanged(Main.main.getCurrentDataSet().getSelected());
+        }
     }
 
-    @Override
-    public void layerRemoved( Layer arg0 ) {
-        // TODO Auto-generated method stub
+    public void editLayerChanged( OsmDataLayer oldLayer, OsmDataLayer newLayer ) {
+        updateSelection();
     }
 }
Index: /applications/editors/josm/plugins/relcontext/src/relcontext/actions/AddRemoveMemberAction.java
===================================================================
--- /applications/editors/josm/plugins/relcontext/src/relcontext/actions/AddRemoveMemberAction.java	(revision 25666)
+++ /applications/editors/josm/plugins/relcontext/src/relcontext/actions/AddRemoveMemberAction.java	(revision 25667)
@@ -22,7 +22,8 @@
 
     public AddRemoveMemberAction( ChosenRelation rel ) {
-        super(tr(ACTION_NAME), "add_remove_member", "Add/remove member from the chosen relation",
-                null, false);
+        super("±", null, "Add/remove member from the chosen relation", null, false);
         this.rel = rel;
+        rel.addChosenRelationListener(this);
+        updateEnabledState();
     }
 
Index: /applications/editors/josm/plugins/relcontext/src/relcontext/actions/ClearChosenRelationAction.java
===================================================================
--- /applications/editors/josm/plugins/relcontext/src/relcontext/actions/ClearChosenRelationAction.java	(revision 25666)
+++ /applications/editors/josm/plugins/relcontext/src/relcontext/actions/ClearChosenRelationAction.java	(revision 25667)
@@ -8,17 +8,19 @@
 
 public class ClearChosenRelationAction extends AbstractAction implements ChosenRelationListener {
+    private ChosenRelation rel;
 
-	private ChosenRelation rel;
+    public ClearChosenRelationAction( ChosenRelation rel ) {
+        super("X");
+        this.rel = rel;
+        rel.addChosenRelationListener(this);
+        setEnabled(false);
+    }
 
-	public ClearChosenRelationAction( ChosenRelation rel ) {
-		this.rel = rel;
-	}
+    public void actionPerformed( ActionEvent e ) {
+        rel.clear();
+    }
 
-	public void actionPerformed( ActionEvent e ) {
-		rel.clear();
-	}
-
-	public void chosenRelationChanged( Relation oldRelation, Relation newRelation ) {
-		setEnabled(newRelation != null);
-	}
+    public void chosenRelationChanged( Relation oldRelation, Relation newRelation ) {
+        setEnabled(newRelation != null);
+    }
 }
Index: /applications/editors/josm/plugins/relcontext/src/relcontext/actions/CreateRelationAction.java
===================================================================
--- /applications/editors/josm/plugins/relcontext/src/relcontext/actions/CreateRelationAction.java	(revision 25666)
+++ /applications/editors/josm/plugins/relcontext/src/relcontext/actions/CreateRelationAction.java	(revision 25667)
@@ -21,47 +21,49 @@
  */
 public class CreateRelationAction extends JosmAction {
+    private static final String ACTION_NAME = "Create relation";
+    protected ChosenRelation chRel;
 
-	private static final String ACTION_NAME = "Create relation";
-	protected ChosenRelation chRel;
+    public CreateRelationAction( ChosenRelation chRel ) {
+        super("+", null, "Create a relation from selected objects", null, false);
+        this.chRel = chRel;
+        updateEnabledState();
+    }
 
-	public CreateRelationAction( ChosenRelation chRel ) {
-		super(tr(ACTION_NAME), "create_relation", "Create a relation from selected objects",
-				null, false);
-		this.chRel = chRel;
-	}
+    public CreateRelationAction() {
+        this(null);
+    }
 
-	public CreateRelationAction() {
-		this(null);
-	}
+    public void actionPerformed( ActionEvent e ) {
+        // todo: ask user for relation type
+        String type = "";
 
-	public void actionPerformed( ActionEvent e ) {
-		// todo: ask user for relation type
-		String type = "";
+        Relation rel = new Relation();
+        if( type != null && type.length() > 0 ) {
+            rel.put("type", type);
+        }
+        for( OsmPrimitive selected : getCurrentDataSet().getSelected() ) {
+            rel.addMember(new RelationMember("", selected));
+        }
 
-		Relation rel = new Relation();
-		if( type != null && type.length() > 0 )
-			rel.put("type", type);
-		for( OsmPrimitive selected : getCurrentDataSet().getSelected() ) {
-			rel.addMember(new RelationMember("", selected));
-		}
+        Collection<Command> cmds = new LinkedList<Command>();
+        Main.main.undoRedo.add(new AddCommand(rel));
 
-		Collection<Command> cmds = new LinkedList<Command>();
-		Main.main.undoRedo.add(new AddCommand(rel));
+        if( chRel != null ) {
+            chRel.set(rel);
+        }
+    }
 
-		if( chRel != null)
-			chRel.set(rel);
-	}
+    @Override
+    protected void updateEnabledState() {
+        if( getCurrentDataSet() == null ) {
+            setEnabled(false);
+        } else {
+            updateEnabledState(getCurrentDataSet().getSelected());
+        }
+    }
 
-	@Override
-	protected void updateEnabledState() {
-		if( getCurrentDataSet() == null )
-			setEnabled(false);
-		else
-			updateEnabledState(getCurrentDataSet().getSelected());
-	}
-
-	@Override
-	protected void updateEnabledState( Collection<? extends OsmPrimitive> selection ) {
-		setEnabled(selection != null && !selection.isEmpty());
-	}
+    @Override
+    protected void updateEnabledState( Collection<? extends OsmPrimitive> selection ) {
+        setEnabled(selection != null && !selection.isEmpty());
+    }
 }
Index: /applications/editors/josm/plugins/relcontext/src/relcontext/actions/DownloadChosenRelationAction.java
===================================================================
--- /applications/editors/josm/plugins/relcontext/src/relcontext/actions/DownloadChosenRelationAction.java	(revision 25667)
+++ /applications/editors/josm/plugins/relcontext/src/relcontext/actions/DownloadChosenRelationAction.java	(revision 25667)
@@ -0,0 +1,59 @@
+package relcontext.actions;
+
+import java.awt.event.ActionEvent;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+import javax.swing.AbstractAction;
+import org.openstreetmap.josm.Main;
+import org.openstreetmap.josm.data.osm.OsmPrimitive;
+import org.openstreetmap.josm.data.osm.Relation;
+import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationMemberTask;
+import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationTask;
+import relcontext.ChosenRelation;
+import relcontext.ChosenRelationListener;
+
+/**
+ * Downloads or updates chosen relation members, depending on completeness.
+ * 
+ * @author Zverik
+ */
+public class DownloadChosenRelationAction extends AbstractAction implements ChosenRelationListener {
+    private ChosenRelation rel;
+
+    public DownloadChosenRelationAction( ChosenRelation rel ) {
+        super("D");
+        this.rel = rel;
+        rel.addChosenRelationListener(this);
+        setEnabled(false);
+    }
+
+    public void actionPerformed( ActionEvent e ) {
+        Relation relation = rel.get();
+        if( relation == null || relation.isNew() || !relation.isIncomplete() ) return;
+        int total = relation.getMembersCount();
+        int incomplete = relation.getIncompleteMembers().size();
+        if( incomplete <= 5 && incomplete * 2 < total )
+            downloadIncomplete(relation);
+        else
+            downloadMembers(relation);
+    }
+
+    public void chosenRelationChanged( Relation oldRelation, Relation newRelation ) {
+        setEnabled(newRelation != null && newRelation.isIncomplete());
+    }
+
+    protected void downloadMembers( Relation rel ) {
+        if( !rel.isNew() ) {
+            Main.worker.submit(new DownloadRelationTask(Collections.singletonList(rel), Main.map.mapView.getEditLayer()));
+        }
+    }
+
+    protected void downloadIncomplete( Relation rel ) {
+        if( rel.isNew() ) return;
+        Set<OsmPrimitive> ret = new HashSet<OsmPrimitive>();
+        ret.addAll(rel.getIncompleteMembers());
+        if( ret.isEmpty() ) return;
+        Main.worker.submit(new DownloadRelationMemberTask(Collections.singletonList(rel), ret, Main.map.mapView.getEditLayer()));
+    }
+}
Index: /applications/editors/josm/plugins/relcontext/src/relcontext/actions/EditChosenRelationAction.java
===================================================================
--- /applications/editors/josm/plugins/relcontext/src/relcontext/actions/EditChosenRelationAction.java	(revision 25666)
+++ /applications/editors/josm/plugins/relcontext/src/relcontext/actions/EditChosenRelationAction.java	(revision 25667)
@@ -15,20 +15,21 @@
  */
 public class EditChosenRelationAction extends AbstractAction implements ChosenRelationListener {
+    private ChosenRelation rel;
 
-	private ChosenRelation rel;
+    public EditChosenRelationAction( ChosenRelation rel ) {
+        super("E");
+        this.rel = rel;
+        rel.addChosenRelationListener(this);
+        setEnabled(false);
+    }
 
-	public EditChosenRelationAction( ChosenRelation rel ) {
-		this.rel = rel;
-	}
+    public void actionPerformed( ActionEvent e ) {
+        Relation relation = rel.get();
+        if( relation == null ) return;
+        RelationEditor.getEditor(Main.map.mapView.getEditLayer(), relation, null).setVisible(true);
+    }
 
-	public void actionPerformed( ActionEvent e ) {
-        Relation relation = rel.get();
-		if( relation == null ) return;
-        //Main.map.relationListDialog.selectRelation(relation); // is it needed?
-        RelationEditor.getEditor(Main.map.mapView.getEditLayer(), relation, null).setVisible(true);
-	}
-
-	public void chosenRelationChanged( Relation oldRelation, Relation newRelation ) {
-		setEnabled(newRelation != null);
-	}
+    public void chosenRelationChanged( Relation oldRelation, Relation newRelation ) {
+        setEnabled(newRelation != null);
+    }
 }
