1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.relation;
|
---|
3 |
|
---|
4 | import java.awt.BasicStroke;
|
---|
5 | import java.awt.Color;
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.Graphics;
|
---|
8 | import java.awt.Graphics2D;
|
---|
9 | import java.awt.Image;
|
---|
10 |
|
---|
11 | import javax.swing.JTable;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.gui.dialogs.relation.sort.WayConnectionType;
|
---|
14 | import org.openstreetmap.josm.gui.dialogs.relation.sort.WayConnectionType.Direction;
|
---|
15 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
16 |
|
---|
17 | public 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 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 | renderForeground(isSelected);
|
---|
36 | setToolTipText(((WayConnectionType)value).getToolTip());
|
---|
37 | renderBackground(getModel(table), null, isSelected);
|
---|
38 | return this;
|
---|
39 | }
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public void paintComponent(Graphics g) {
|
---|
43 | super.paintComponent(g);
|
---|
44 | if (value == null || !value.isValid())
|
---|
45 | return;
|
---|
46 |
|
---|
47 | int ymax=this.getSize().height - 1;
|
---|
48 | int xloop = 10;
|
---|
49 | int xowloop = 0;
|
---|
50 | if(value.isOnewayLoopForwardPart) {
|
---|
51 | xowloop = -3;
|
---|
52 | }
|
---|
53 | if(value.isOnewayLoopBackwardPart) {
|
---|
54 | xowloop = 3;
|
---|
55 | }
|
---|
56 |
|
---|
57 | int xoff = this.getSize().width / 2;
|
---|
58 | if (value.isLoop) {
|
---|
59 | xoff -= xloop / 2 - 1;
|
---|
60 | }
|
---|
61 | int w = 2;
|
---|
62 | int p = 2 + w + 1;
|
---|
63 | int y1 = 0;
|
---|
64 | int y2 = 0;
|
---|
65 |
|
---|
66 | if (value.linkPrev) {
|
---|
67 | g.setColor(Color.black);
|
---|
68 | if(value.isOnewayHead) {
|
---|
69 | g.fillRect(xoff - 1, 0, 3, 1);
|
---|
70 | } else {
|
---|
71 | g.fillRect(xoff - 1 + xowloop, 0, 3, 1);
|
---|
72 | }
|
---|
73 | y1 = 0;
|
---|
74 | } else {
|
---|
75 | if (value.isLoop) {
|
---|
76 | g.setColor(Color.black);
|
---|
77 | y1 = 5;
|
---|
78 | g.drawImage(corners,xoff,y1-3,xoff+3,y1, 0,0,3,3, new Color(0,0,0,0), null);
|
---|
79 | g.drawImage(corners,xoff+xloop-2,y1-3,xoff+xloop+1,y1, 2,0,5,3, new Color(0,0,0,0), null);
|
---|
80 | g.drawLine(xoff+3,y1-3,xoff+xloop-3,y1-3);
|
---|
81 | }
|
---|
82 | else {
|
---|
83 | g.setColor(Color.red);
|
---|
84 | if(value.isOnewayHead) {
|
---|
85 | g.drawRect(xoff-1, p - 3 - w, w, w);
|
---|
86 | } else {
|
---|
87 | g.drawRect(xoff-1 + xowloop, p - 1 - w, w, w);
|
---|
88 | }
|
---|
89 | y1 = p;
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | if (value.linkNext) {
|
---|
94 | g.setColor(Color.black);
|
---|
95 | if(value.isOnewayTail) {
|
---|
96 | g.fillRect(xoff - 1, ymax, 3, 1);
|
---|
97 | } else {
|
---|
98 | g.fillRect(xoff - 1 + xowloop, ymax, 3, 1);
|
---|
99 | }
|
---|
100 | y2 = ymax;
|
---|
101 | } else {
|
---|
102 | if (value.isLoop) {
|
---|
103 | g.setColor(Color.black);
|
---|
104 | y2 = ymax - 5;
|
---|
105 | g.fillRect(xoff-1, y2+2, 3, 3);
|
---|
106 | g.drawLine(xoff, y2, xoff, y2+2);
|
---|
107 | g.drawImage(corners,xoff+xloop-2,y2+1,xoff+xloop+1,y2+4, 2,2,5,5, new Color(0,0,0,0), null);
|
---|
108 | g.drawLine(xoff+3-1,y2+3,xoff+xloop-3,y2+3);
|
---|
109 | }
|
---|
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 = null;
|
---|
163 | switch (value.direction) {
|
---|
164 | case FORWARD:
|
---|
165 | arrow = arrowDown;
|
---|
166 | break;
|
---|
167 | case BACKWARD:
|
---|
168 | arrow = arrowUp;
|
---|
169 | break;
|
---|
170 | }
|
---|
171 | if (value.direction == Direction.ROUNDABOUT_LEFT) {
|
---|
172 | g.drawImage(roundabout_left, xoff-6, 1, null);
|
---|
173 | } else if (value.direction == Direction.ROUNDABOUT_RIGHT) {
|
---|
174 | g.drawImage(roundabout_right, xoff-6, 1, null);
|
---|
175 | }
|
---|
176 |
|
---|
177 | if (!value.isOnewayLoopForwardPart && !value.isOnewayLoopBackwardPart &&
|
---|
178 | (arrow != null)) {
|
---|
179 | g.drawImage(arrow, xoff-3, (y1 + y2) / 2 - 2, null);
|
---|
180 | }
|
---|
181 |
|
---|
182 | if (value.isOnewayLoopBackwardPart && value.isOnewayLoopForwardPart) {
|
---|
183 | if(arrow == arrowDown) {
|
---|
184 | arrow = arrowUp;
|
---|
185 | } else if (arrow == arrowUp) {
|
---|
186 | arrow = arrowDown;
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | if ((arrow != null)) {
|
---|
191 | g.drawImage(arrow, xoff+xowloop-3, (y1 + y2) / 2 - 2, null);
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | private void setDotted(Graphics g) {
|
---|
196 | ((Graphics2D)g).setStroke(new BasicStroke(
|
---|
197 | 1f,
|
---|
198 | BasicStroke.CAP_BUTT,
|
---|
199 | BasicStroke.CAP_BUTT,
|
---|
200 | 5f,
|
---|
201 | new float[] {1f, 2f},
|
---|
202 | 0f));
|
---|
203 | }
|
---|
204 |
|
---|
205 | private void unsetDotted(Graphics g) {
|
---|
206 | ((Graphics2D)g).setStroke(new BasicStroke());
|
---|
207 | }
|
---|
208 | }
|
---|