Changeset 34230 in osm for applications/editors
- Timestamp:
- 2018-05-30T10:07:52+02:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/pt_assistant/src/org/openstreetmap/josm/plugins/pt_assistant/gui/PTAssistantLayer.java
r33571 r34230 39 39 * 40 40 */ 41 public final class PTAssistantLayer extends Layer 42 implements LayerChangeListener { 43 44 private List<OsmPrimitive> primitives = new ArrayList<>(); 45 private PTAssistantPaintVisitor paintVisitor; 46 private HashMap<Character, List<PTWay>> fixVariants = new HashMap<>(); 47 private HashMap<Way, List<Character>> wayColoring = new HashMap<>(); 48 49 public PTAssistantLayer() { 50 super("pt_assistant layer"); 51 MainApplication.getLayerManager().addLayerChangeListener(this); 52 MainApplication.getLayerManager().addLayer(this); 53 } 54 55 /** 56 * Adds a primitive (route) to be displayed in this layer 57 * 58 * @param primitive primitive (route) 59 */ 60 public void addPrimitive(OsmPrimitive primitive) { 61 this.primitives.add(primitive); 62 } 63 64 /** 65 * Clears all primitives (routes) from being displayed. 66 */ 67 public void clear() { 68 this.primitives.clear(); 69 } 70 71 public void clearFixVariants() { 72 fixVariants.clear(); 73 wayColoring.clear(); 74 MainApplication.getMap().mapView.repaint(); 75 } 76 77 /** 78 * Adds the first 5 fix variants to be displayed in the pt_assistant layer 79 * 80 * @param fixVariants fix variants 81 */ 82 public void addFixVariants(List<List<PTWay>> fixVariants) { 83 HashMap<List<PTWay>, Character> fixVariantLetterMap = new HashMap<>(); 84 85 char alphabet = 'A'; 86 for (int i = 0; i < 5 && i < fixVariants.size(); i++) { 87 List<PTWay> fixVariant = fixVariants.get(i); 88 this.fixVariants.put(alphabet, fixVariant); 89 fixVariantLetterMap.put(fixVariant, alphabet); 90 alphabet++; 91 } 92 93 for (Entry<Character, List<PTWay>> entry : this.fixVariants.entrySet()) { 94 Character currentFixVariantLetter = entry.getKey(); 95 List<PTWay> fixVariant = entry.getValue(); 96 for (PTWay ptway : fixVariant) { 97 for (Way way : ptway.getWays()) { 98 if (wayColoring.containsKey(way)) { 99 if (!wayColoring.get(way).contains(currentFixVariantLetter)) { 100 wayColoring.get(way).add(currentFixVariantLetter); 101 } 102 } else { 103 List<Character> letterList = new ArrayList<>(); 104 letterList.add(currentFixVariantLetter); 105 wayColoring.put(way, letterList); 106 } 107 } 108 } 109 } 110 } 111 112 /** 113 * Returns fix variant (represented by a list of PTWays) that corresponds to 114 * the given character. 115 * 116 * @param c character 117 * @return fix variant 118 */ 119 public List<PTWay> getFixVariant(char c) { 120 return fixVariants.get(Character.toUpperCase(c)); 121 } 122 123 public void setPrimitives(List<OsmPrimitive> newPrimitives) { 124 primitives = new ArrayList<>(newPrimitives); 125 } 126 127 @Override 128 public void paint(final Graphics2D g, final MapView mv, Bounds bounds) { 129 130 paintVisitor = new PTAssistantPaintVisitor(g, mv); 131 132 for (OsmPrimitive primitive : primitives) { 133 paintVisitor.visit(primitive); 134 } 135 136 paintVisitor.visitFixVariants(fixVariants, wayColoring); 137 138 } 139 140 @Override 141 public Icon getIcon() { 142 return ImageProvider.get("layer", "osmdata_small"); 143 } 144 145 @Override 146 public Object getInfoComponent() { 147 return getToolTipText(); 148 } 149 150 @Override 151 public Action[] getMenuEntries() { 152 return new Action[] {LayerListDialog.getInstance().createShowHideLayerAction(), 153 LayerListDialog.getInstance().createDeleteLayerAction(), SeparatorLayerAction.INSTANCE, 154 new RenameLayerAction(null, this), SeparatorLayerAction.INSTANCE, new LayerListPopup.InfoAction(this)}; 155 } 156 157 @Override 158 public String getToolTipText() { 159 return "pt_assistant layer"; 160 } 161 162 @Override 163 public boolean isMergable(Layer arg0) { 164 return false; 165 } 166 167 @Override 168 public void mergeFrom(Layer arg0) { 169 // do nothing 170 171 } 172 173 @Override 174 public void visitBoundingBox(BoundingXYVisitor arg0) { 175 // do nothing 176 177 } 178 179 @Override 180 public LayerPositionStrategy getDefaultLayerPosition() { 181 return LayerPositionStrategy.IN_FRONT; 182 } 183 184 /** 185 * Repaints the layer in cases when there was no selection change 186 * 187 * @param relation relation 188 */ 189 public void repaint(Relation relation) { 190 primitives.clear(); 191 primitives.add(relation); 192 if (!MainApplication.getLayerManager().containsLayer(this)) { 193 MainApplication.getLayerManager().addLayer(this); 194 } 195 196 if (paintVisitor == null) { 197 MapView mv = MainApplication.getMap().mapView; 198 paintVisitor = new PTAssistantPaintVisitor(mv.getGraphics(), mv); 199 } 200 201 for (OsmPrimitive primitive : primitives) { 202 paintVisitor.visit(primitive); 203 } 204 205 paintVisitor.visitFixVariants(fixVariants, wayColoring); 206 207 MainApplication.getMap().mapView.repaint(); 208 } 209 210 @Override 211 public void layerAdded(LayerAddEvent arg0) { 212 // do nothing 213 } 214 215 @Override 216 public void layerOrderChanged(LayerOrderChangeEvent arg0) { 217 // do nothing 218 219 } 220 221 @Override 222 public void layerRemoving(LayerRemoveEvent event) { 223 224 if (event.getRemovedLayer() instanceof OsmDataLayer) { 225 primitives.clear(); 226 fixVariants.clear(); 227 wayColoring.clear(); 228 MainApplication.getMap().mapView.repaint(); 229 } 230 231 if (event.getRemovedLayer() instanceof OsmDataLayer 232 && event.getSource().getLayersOfType(OsmDataLayer.class).isEmpty()) 233 event.scheduleRemoval(Collections.singleton(this)); 234 235 if (event.getRemovedLayer() == this) { 236 PTAssistantLayerManager.PTLM.resetLayer(); 237 PTAssistantPlugin.clearHighlightedRelations(); 238 } 239 } 240 241 @Override 242 public synchronized void destroy() { 243 MainApplication.getLayerManager().removeLayerChangeListener(this); 244 super.destroy(); 245 } 41 public final class PTAssistantLayer extends Layer implements LayerChangeListener { 42 43 private List<OsmPrimitive> primitives = new ArrayList<>(); 44 private PTAssistantPaintVisitor paintVisitor; 45 private HashMap<Character, List<PTWay>> fixVariants = new HashMap<>(); 46 private HashMap<Way, List<Character>> wayColoring = new HashMap<>(); 47 public String modeOfTravel = null; 48 49 public PTAssistantLayer() { 50 super("pt_assistant layer"); 51 MainApplication.getLayerManager().addLayerChangeListener(this); 52 MainApplication.getLayerManager().addLayer(this); 53 } 54 55 /** 56 * Adds a primitive (route) to be displayed in this layer 57 * 58 * @param primitive 59 * primitive (route) 60 */ 61 public void addPrimitive(OsmPrimitive primitive) { 62 this.primitives.add(primitive); 63 } 64 65 /** 66 * Clears all primitives (routes) from being displayed. 67 */ 68 public void clear() { 69 this.primitives.clear(); 70 } 71 72 public void clearFixVariants() { 73 fixVariants.clear(); 74 wayColoring.clear(); 75 MainApplication.getMap().mapView.repaint(); 76 } 77 78 /** 79 * Adds the first 5 fix variants to be displayed in the pt_assistant layer 80 * 81 * @param fixVariants 82 * fix variants 83 */ 84 public void addFixVariants(List<List<PTWay>> fixVariants) { 85 HashMap<List<PTWay>, Character> fixVariantLetterMap = new HashMap<>(); 86 87 char alphabet = 'A'; 88 for (int i = 0; i < 5 && i < fixVariants.size(); i++) { 89 List<PTWay> fixVariant = fixVariants.get(i); 90 this.fixVariants.put(alphabet, fixVariant); 91 fixVariantLetterMap.put(fixVariant, alphabet); 92 alphabet++; 93 } 94 95 for (Entry<Character, List<PTWay>> entry : this.fixVariants.entrySet()) { 96 Character currentFixVariantLetter = entry.getKey(); 97 List<PTWay> fixVariant = entry.getValue(); 98 for (PTWay ptway : fixVariant) { 99 for (Way way : ptway.getWays()) { 100 if (wayColoring.containsKey(way)) { 101 if (!wayColoring.get(way).contains(currentFixVariantLetter)) { 102 wayColoring.get(way).add(currentFixVariantLetter); 103 } 104 } else { 105 List<Character> letterList = new ArrayList<>(); 106 letterList.add(currentFixVariantLetter); 107 wayColoring.put(way, letterList); 108 } 109 } 110 } 111 } 112 } 113 114 /** 115 * Returns fix variant (represented by a list of PTWays) that corresponds to the 116 * given character. 117 * 118 * @param c 119 * character 120 * @return fix variant 121 */ 122 public List<PTWay> getFixVariant(char c) { 123 return fixVariants.get(Character.toUpperCase(c)); 124 } 125 126 public void setPrimitives(List<OsmPrimitive> newPrimitives) { 127 primitives = new ArrayList<>(newPrimitives); 128 } 129 130 @Override 131 public void paint(final Graphics2D g, final MapView mv, Bounds bounds) { 132 133 paintVisitor = new PTAssistantPaintVisitor(g, mv); 134 135 for (OsmPrimitive primitive : primitives) { 136 paintVisitor.visit(primitive); 137 } 138 139 paintVisitor.visitFixVariants(fixVariants, wayColoring); 140 } 141 142 @Override 143 public Icon getIcon() { 144 return ImageProvider.get("layer", "osmdata_small"); 145 } 146 147 @Override 148 public Object getInfoComponent() { 149 return getToolTipText(); 150 } 151 152 @Override 153 public Action[] getMenuEntries() { 154 return new Action[] { LayerListDialog.getInstance().createShowHideLayerAction(), 155 LayerListDialog.getInstance().createDeleteLayerAction(), SeparatorLayerAction.INSTANCE, 156 new RenameLayerAction(null, this), SeparatorLayerAction.INSTANCE, new LayerListPopup.InfoAction(this) }; 157 } 158 159 @Override 160 public String getToolTipText() { 161 return "pt_assistant layer"; 162 } 163 164 @Override 165 public boolean isMergable(Layer arg0) { 166 return false; 167 } 168 169 @Override 170 public void mergeFrom(Layer arg0) { 171 // do nothing 172 173 } 174 175 @Override 176 public void visitBoundingBox(BoundingXYVisitor arg0) { 177 // do nothing 178 179 } 180 181 @Override 182 public LayerPositionStrategy getDefaultLayerPosition() { 183 return LayerPositionStrategy.IN_FRONT; 184 } 185 186 /** 187 * Repaints the layer in cases when there was no selection change 188 * 189 * @param relation 190 * relation 191 */ 192 public void repaint(Relation relation) { 193 primitives.clear(); 194 primitives.add(relation); 195 if (!MainApplication.getLayerManager().containsLayer(this)) { 196 MainApplication.getLayerManager().addLayer(this); 197 } 198 199 if (paintVisitor == null) { 200 MapView mv = MainApplication.getMap().mapView; 201 paintVisitor = new PTAssistantPaintVisitor(mv.getGraphics(), mv); 202 } 203 204 for (OsmPrimitive primitive : primitives) { 205 paintVisitor.visit(primitive); 206 } 207 208 paintVisitor.visitFixVariants(fixVariants, wayColoring); 209 210 MainApplication.getMap().mapView.repaint(); 211 setModeOfTravel(relation); 212 } 213 214 private void setModeOfTravel(Relation relation) { 215 if (relation.hasKey("route")) 216 modeOfTravel = relation.get("route"); 217 } 218 219 public String getModeOfTravel() { 220 return modeOfTravel; 221 } 222 223 @Override 224 public void layerAdded(LayerAddEvent arg0) { 225 // do nothing 226 } 227 228 @Override 229 public void layerOrderChanged(LayerOrderChangeEvent arg0) { 230 // do nothing 231 232 } 233 234 @Override 235 public void layerRemoving(LayerRemoveEvent event) { 236 237 if (event.getRemovedLayer() instanceof OsmDataLayer) { 238 primitives.clear(); 239 fixVariants.clear(); 240 wayColoring.clear(); 241 MainApplication.getMap().mapView.repaint(); 242 } 243 244 if (event.getRemovedLayer() instanceof OsmDataLayer 245 && event.getSource().getLayersOfType(OsmDataLayer.class).isEmpty()) 246 event.scheduleRemoval(Collections.singleton(this)); 247 248 if (event.getRemovedLayer() == this) { 249 PTAssistantLayerManager.PTLM.resetLayer(); 250 PTAssistantPlugin.clearHighlightedRelations(); 251 } 252 } 253 254 @Override 255 public synchronized void destroy() { 256 MainApplication.getLayerManager().removeLayerChangeListener(this); 257 super.destroy(); 258 } 246 259 }
Note:
See TracChangeset
for help on using the changeset viewer.