Changeset 25669 in osm for applications/editors/josm/plugins/relcontext/src
- Timestamp:
- 2011-03-22T22:00:02+01:00 (14 years ago)
- 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 1 1 package relcontext; 2 2 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; 3 import java.awt.*; 8 4 import java.awt.geom.GeneralPath; 9 import java.util.ArrayList; 10 import java.util.Collection; 11 import java.util.List; 12 import java.util.Set; 5 import java.util.*; 13 6 import org.openstreetmap.josm.Main; 14 7 import 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; 8 import org.openstreetmap.josm.data.osm.*; 9 import org.openstreetmap.josm.data.osm.event.*; 20 10 import org.openstreetmap.josm.gui.MapView; 21 11 import org.openstreetmap.josm.gui.MapView.EditLayerChangeListener; 22 import org.openstreetmap.josm.gui.MapView.LayerChangeListener;23 import org.openstreetmap.josm.gui.layer.Layer;24 12 import org.openstreetmap.josm.gui.layer.MapViewPaintable; 25 13 import org.openstreetmap.josm.gui.layer.OsmDataLayer; … … 30 18 * @author Zverik 31 19 */ 32 public class ChosenRelation implements EditLayerChangeListener, MapViewPaintable {20 public class ChosenRelation implements EditLayerChangeListener, MapViewPaintable, DataSetListener { 33 21 private Relation chosenRelation = null; 34 private List<ChosenRelationListener> chosenRelationListeners = new ArrayList<ChosenRelationListener>();22 private Set<ChosenRelationListener> chosenRelationListeners = new HashSet<ChosenRelationListener>(); 35 23 36 24 public void set( Relation rel ) { … … 42 30 analyse(); 43 31 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 ) 45 37 listener.chosenRelationChanged(oldRel, chosenRelation); 46 }47 return;48 38 } 49 39 … … 91 81 } 92 82 83 Stroke oldStroke = g.getStroke(); 84 Composite oldComposite = g.getComposite(); 93 85 g.setColor(Color.yellow); 94 86 g.setStroke(new BasicStroke(6, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); … … 116 108 // todo: closedway, multipolygon - ? 117 109 } 118 g.setStroke(new BasicStroke(1)); // from building_tools; is it really needed? 110 g.setStroke(oldStroke); 111 g.setComposite(oldComposite); 119 112 } 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 ) {} 120 134 } -
applications/editors/josm/plugins/relcontext/src/relcontext/ChosenRelationComponent.java
r25667 r25669 32 32 public void mouseClicked( MouseEvent e ) { 33 33 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()); 35 35 } 36 36 }); … … 38 38 39 39 public void chosenRelationChanged( Relation oldRelation, Relation newRelation ) { 40 setText( newRelation == null ? "" : newRelation.getDisplayName(DefaultNameFormatter.getInstance()));40 setText(prepareText(newRelation)); 41 41 repaint(); 42 42 } 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 } 43 78 } -
applications/editors/josm/plugins/relcontext/src/relcontext/RelContextDialog.java
r25667 r25669 1 1 package relcontext; 2 2 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; 3 import java.beans.PropertyChangeEvent; 7 4 import static org.openstreetmap.josm.tools.I18n.tr; 8 5 … … 10 7 import java.awt.FlowLayout; 11 8 import java.awt.event.KeyEvent; 9 import java.awt.event.MouseEvent; 12 10 import 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; 11 import java.beans.PropertyChangeListener; 18 12 19 import javax.swing.JList;20 import javax.swing.JPanel;21 import javax.swing.JScrollPane;22 import javax.swing.ListSelectionModel;23 13 import org.openstreetmap.josm.Main; 24 14 import org.openstreetmap.josm.data.SelectionChangedListener; … … 26 16 import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode; 27 17 import org.openstreetmap.josm.data.osm.event.SelectionEventManager; 28 18 import org.openstreetmap.josm.data.osm.OsmPrimitive; 19 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 29 20 import org.openstreetmap.josm.gui.MapView; 30 21 import org.openstreetmap.josm.gui.MapView.EditLayerChangeListener; 31 import org.openstreetmap.josm.gui.MapView.LayerChangeListener;32 22 import org.openstreetmap.josm.gui.OsmPrimitivRenderer; 33 23 import org.openstreetmap.josm.gui.dialogs.ToggleDialog; 34 import org.openstreetmap.josm.gui.layer.Layer;35 24 import 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 26 import java.util.*; 27 import javax.swing.*; 28 import org.openstreetmap.josm.data.osm.DataSet; 29 import org.openstreetmap.josm.data.osm.event.DatasetEventManager; 30 import relcontext.actions.*; 41 31 42 32 /** … … 49 39 private final DefaultListModel relationsData; 50 40 private ChosenRelation chosenRelation; 41 private JPanel topLine; 51 42 52 43 public RelContextDialog() { … … 84 75 85 76 // [±][X] relation U [AZ][Down][Edit] 86 JPaneltopLine = new JPanel(new BorderLayout());77 topLine = new JPanel(new BorderLayout()); 87 78 JPanel topLeftButtons = new JPanel(new FlowLayout(FlowLayout.LEFT)); 88 79 topLeftButtons.add(new JButton(new AddRemoveMemberAction(chosenRelation))); … … 91 82 topLine.add(new ChosenRelationComponent(chosenRelation), BorderLayout.CENTER); 92 83 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); 94 87 topRightButtons.add(new JButton(new EditChosenRelationAction(chosenRelation))); 95 88 topLine.add(topRightButtons, BorderLayout.EAST); 96 89 rcPanel.add(topLine, BorderLayout.NORTH); 97 90 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 98 99 // [+][Multi] [X]Adm [X]Tags [X]1 99 100 JPanel bottomLine = new JPanel(new FlowLayout(FlowLayout.LEFT)); 100 101 bottomLine.add(new JButton(new CreateRelationAction(chosenRelation))); 102 bottomLine.add(new JButton(new CreateMultipolygonAction(chosenRelation))); 101 103 rcPanel.add(bottomLine, BorderLayout.SOUTH); 102 104 … … 108 110 SelectionEventManager.getInstance().removeSelectionListener(this); 109 111 MapView.removeEditLayerChangeListener(this); 112 DatasetEventManager.getInstance().removeDatasetListener(chosenRelation); 110 113 } 111 114 … … 114 117 SelectionEventManager.getInstance().addSelectionListener(this, FireMode.IN_EDT_CONSOLIDATED); 115 118 MapView.addEditLayerChangeListener(this); 119 DatasetEventManager.getInstance().addDatasetListener(chosenRelation, FireMode.IN_EDT); 116 120 } 117 118 119 121 120 122 public ChosenRelation getChosenRelation() { … … 123 125 124 126 public void chosenRelationChanged( Relation oldRelation, Relation newRelation ) { 127 if( topLine != null ) 128 topLine.setVisible(newRelation != null); 125 129 // ? 126 130 } -
applications/editors/josm/plugins/relcontext/src/relcontext/actions/AddRemoveMemberAction.java
r25667 r25669 1 1 package relcontext.actions; 2 2 3 import java.util. Collection;3 import java.util.*; 4 4 import org.openstreetmap.josm.data.osm.OsmPrimitive; 5 5 import static org.openstreetmap.josm.tools.I18n.tr; 6 6 import java.awt.event.ActionEvent; 7 import javax.swing.Action; 8 import org.openstreetmap.josm.Main; 7 9 import org.openstreetmap.josm.actions.JosmAction; 10 import org.openstreetmap.josm.command.ChangeCommand; 8 11 import org.openstreetmap.josm.data.osm.Relation; 12 import org.openstreetmap.josm.data.osm.RelationMember; 9 13 import relcontext.ChosenRelation; 10 14 import relcontext.ChosenRelationListener; … … 29 33 30 34 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)); 32 54 } 33 55 34 56 public void chosenRelationChanged( Relation oldRelation, Relation newRelation ) { 35 setEnabled(newRelation != null && getCurrentDataSet() != null);57 updateEnabledState(); 36 58 } 37 59 38 60 @Override 39 61 protected void updateEnabledState() { 40 if( getCurrentDataSet() == null ) { 41 setEnabled(false); 42 } else { 43 updateEnabledState(getCurrentDataSet().getSelected()); 44 } 62 updateEnabledState(getCurrentDataSet() == null ? null : getCurrentDataSet().getSelected()); 45 63 } 46 64 47 65 @Override 48 66 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()) ) { 50 73 setEnabled(false); 51 74 return; 52 75 } 53 76 setEnabled(true); 77 } 78 79 protected void updateIcon() { 54 80 // 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); 55 102 } 56 103 } -
applications/editors/josm/plugins/relcontext/src/relcontext/actions/CreateRelationAction.java
r25667 r25669 46 46 } 47 47 48 Collection<Command> cmds = new LinkedList<Command>();49 48 Main.main.undoRedo.add(new AddCommand(rel)); 50 49 -
applications/editors/josm/plugins/relcontext/src/relcontext/actions/DownloadChosenRelationAction.java
r25667 r25669 9 9 import org.openstreetmap.josm.data.osm.OsmPrimitive; 10 10 import org.openstreetmap.josm.data.osm.Relation; 11 import org.openstreetmap.josm.data.osm.RelationMember; 11 12 import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationMemberTask; 12 13 import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationTask; … … 31 32 public void actionPerformed( ActionEvent e ) { 32 33 Relation relation = rel.get(); 33 if( relation == null || relation.isNew() || !relation.isIncomplete()) return;34 if( relation == null || relation.isNew() ) return; 34 35 int total = relation.getMembersCount(); 35 36 int incomplete = relation.getIncompleteMembers().size(); 36 if( incomplete <= 5 && incomplete * 2 < total)37 if( incomplete <= 5 || (incomplete <= 10 && incomplete * 3 < total) ) 37 38 downloadIncomplete(relation); 38 39 else … … 41 42 42 43 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); 44 54 } 45 55
Note:
See TracChangeset
for help on using the changeset viewer.