source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/relation/MemberTableLinkedCellRenderer.java@ 12657

Last change on this file since 12657 was 12657, checked in by michael2402, 7 years ago

See #14794: Add javadoc for gui/dialogs/relation package.

  • Property svn:eol-style set to native
File size: 7.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.relation;
3
4import java.awt.BasicStroke;
5import java.awt.Color;
6import java.awt.Component;
7import java.awt.Graphics;
8import java.awt.Graphics2D;
9import java.awt.Image;
10
11import javax.swing.JTable;
12
13import org.openstreetmap.josm.gui.dialogs.relation.sort.WayConnectionType;
14import org.openstreetmap.josm.gui.dialogs.relation.sort.WayConnectionType.Direction;
15import org.openstreetmap.josm.tools.ImageProvider;
16
17/**
18 * This class renders the link column of the member table. It shows if the way segments are connected or not.
19 */
20public class MemberTableLinkedCellRenderer extends MemberTableCellRenderer {
21
22 private static final Image ARROW_UP = ImageProvider.get("dialogs/relation", "arrowup").getImage();
23 private static final Image ARROW_DOWN = ImageProvider.get("dialogs/relation", "arrowdown").getImage();
24 private static final Image CORNERS = ImageProvider.get("dialogs/relation", "roundedcorners").getImage();
25 private static final Image ROUNDABOUT_RIGHT = ImageProvider.get("dialogs/relation", "roundabout_right_tiny").getImage();
26 private static final Image ROUNDABOUT_LEFT = ImageProvider.get("dialogs/relation", "roundabout_left_tiny").getImage();
27 private transient WayConnectionType value = new WayConnectionType();
28
29 @Override
30 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
31 int row, int column) {
32
33 reset();
34 if (value == null)
35 return this;
36
37 this.value = (WayConnectionType) value;
38 setToolTipText(((WayConnectionType) value).getTooltip());
39 renderBackgroundForeground(getModel(table), null, isSelected);
40 return this;
41 }
42
43 @Override
44 public void paintComponent(Graphics g) {
45 super.paintComponent(g);
46 if (value == null || !value.isValid())
47 return;
48
49 int ymax = this.getSize().height - 1;
50 int xloop = 10;
51 int xowloop = 0;
52 if (value.isOnewayLoopForwardPart) {
53 xowloop = -3;
54 }
55 if (value.isOnewayLoopBackwardPart) {
56 xowloop = 3;
57 }
58
59 int xoff = this.getSize().width / 2;
60 if (value.isLoop) {
61 xoff -= xloop / 2 - 1;
62 }
63 int w = 2;
64 int p = 2 + w + 1;
65 int y1;
66 int y2;
67
68 if (value.linkPrev) {
69 g.setColor(Color.black);
70 if (value.isOnewayHead) {
71 g.fillRect(xoff - 1, 0, 3, 1);
72 } else {
73 g.fillRect(xoff - 1 + xowloop, 0, 3, 1);
74 }
75 y1 = 0;
76 } else {
77 if (value.isLoop) {
78 g.setColor(Color.black);
79 y1 = 5;
80 g.drawImage(CORNERS, xoff, y1-3, xoff+3, y1, 0, 0, 3, 3, new Color(0, 0, 0, 0), null);
81 g.drawImage(CORNERS, xoff+xloop-2, y1-3, xoff+xloop+1, y1, 2, 0, 5, 3, new Color(0, 0, 0, 0), null);
82 g.drawLine(xoff+3, y1-3, xoff+xloop-3, y1-3);
83 } else {
84 g.setColor(Color.red);
85 if (value.isOnewayHead) {
86 g.drawRect(xoff-1, p - 3 - w, w, w);
87 } else {
88 g.drawRect(xoff-1 + xowloop, p - 1 - w, w, w);
89 }
90 y1 = p;
91 }
92 }
93
94 if (value.linkNext) {
95 g.setColor(Color.black);
96 if (value.isOnewayTail) {
97 g.fillRect(xoff - 1, ymax, 3, 1);
98 } else {
99 g.fillRect(xoff - 1 + xowloop, ymax, 3, 1);
100 }
101 y2 = ymax;
102 } else {
103 if (value.isLoop) {
104 g.setColor(Color.black);
105 y2 = ymax - 5;
106 g.fillRect(xoff-1, y2+2, 3, 3);
107 g.drawLine(xoff, y2, xoff, y2+2);
108 g.drawImage(CORNERS, xoff+xloop-2, y2+1, xoff+xloop+1, y2+4, 2, 2, 5, 5, new Color(0, 0, 0, 0), null);
109 g.drawLine(xoff+3-1, y2+3, xoff+xloop-3, y2+3);
110 } else {
111 g.setColor(Color.red);
112 if (value.isOnewayTail) {
113 g.drawRect(xoff-1, ymax - p + 3, w, w);
114 } else {
115 g.drawRect(xoff-1 + xowloop, ymax - p + 1, w, w);
116 }
117 y2 = ymax - p;
118 }
119 }
120
121 /* vertical lines */
122 g.setColor(Color.black);
123 if (value.isLoop) {
124 g.drawLine(xoff+xloop, y1, xoff+xloop, y2);
125 }
126
127 if (value.isOnewayHead) {
128 setDotted(g);
129 y1 = 7;
130
131 int[] xValues = {xoff - xowloop + 1, xoff - xowloop + 1, xoff};
132 int[] yValues = {ymax, y1+1, 1};
133 g.drawPolyline(xValues, yValues, 3);
134 unsetDotted(g);
135 g.drawLine(xoff + xowloop, y1+1, xoff, 1);
136 }
137
138 if (value.isOnewayTail) {
139 setDotted(g);
140 y2 = ymax - 7;
141
142 int[] xValues = {xoff+1, xoff - xowloop + 1, xoff - xowloop + 1};
143 int[] yValues = {ymax-1, y2, y1};
144 g.drawPolyline(xValues, yValues, 3);
145 unsetDotted(g);
146 g.drawLine(xoff + xowloop, y2, xoff, ymax-1);
147 }
148
149 if ((value.isOnewayLoopForwardPart || value.isOnewayLoopBackwardPart) && !value.isOnewayTail && !value.isOnewayHead) {
150 setDotted(g);
151 g.drawLine(xoff - xowloop+1, y1, xoff - xowloop+1, y2 + 1);
152 unsetDotted(g);
153 }
154
155 if (!value.isOnewayLoopForwardPart && !value.isOnewayLoopBackwardPart) {
156 g.drawLine(xoff, y1, xoff, y2);
157 }
158
159 g.drawLine(xoff+xowloop, y1, xoff+xowloop, y2);
160
161 /* special icons */
162 Image arrow;
163 switch (value.direction) {
164 case FORWARD:
165 arrow = ARROW_DOWN;
166 break;
167 case BACKWARD:
168 arrow = ARROW_UP;
169 break;
170 default:
171 arrow = null;
172 }
173 if (value.direction == Direction.ROUNDABOUT_LEFT) {
174 g.drawImage(ROUNDABOUT_LEFT, xoff-6, 1, null);
175 } else if (value.direction == Direction.ROUNDABOUT_RIGHT) {
176 g.drawImage(ROUNDABOUT_RIGHT, xoff-6, 1, null);
177 }
178
179 if (!value.isOnewayLoopForwardPart && !value.isOnewayLoopBackwardPart &&
180 (arrow != null)) {
181 g.drawImage(arrow, xoff-3, (y1 + y2) / 2 - 2, null);
182 }
183
184 if (value.isOnewayLoopBackwardPart && value.isOnewayLoopForwardPart) {
185 if (arrow == ARROW_DOWN) {
186 arrow = ARROW_UP;
187 } else if (arrow == ARROW_UP) {
188 arrow = ARROW_DOWN;
189 }
190 }
191
192 if (arrow != null) {
193 g.drawImage(arrow, xoff+xowloop-3, (y1 + y2) / 2 - 2, null);
194 }
195 }
196
197 private static void setDotted(Graphics g) {
198 ((Graphics2D) g).setStroke(new BasicStroke(
199 1f,
200 BasicStroke.CAP_BUTT,
201 BasicStroke.CAP_BUTT,
202 5f,
203 new float[] {1f, 2f},
204 0f));
205 }
206
207 private static void unsetDotted(Graphics g) {
208 ((Graphics2D) g).setStroke(new BasicStroke());
209 }
210}
Note: See TracBrowser for help on using the repository browser.