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

Last change on this file since 10248 was 10248, checked in by Don-vip, 8 years ago

findbugs - NM_CONFUSING

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