001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.dialogs.relation; 003 004import java.awt.BasicStroke; 005import java.awt.Color; 006import java.awt.Component; 007import java.awt.Graphics; 008import java.awt.Graphics2D; 009import java.awt.Image; 010 011import javax.swing.JTable; 012 013import org.openstreetmap.josm.gui.dialogs.relation.sort.WayConnectionType; 014import org.openstreetmap.josm.gui.dialogs.relation.sort.WayConnectionType.Direction; 015import org.openstreetmap.josm.tools.ImageProvider; 016 017/** 018 * This class renders the link column of the member table. It shows if the way segments are connected or not. 019 */ 020public class MemberTableLinkedCellRenderer extends MemberTableCellRenderer { 021 022 private static final Image ARROW_UP = ImageProvider.get("dialogs/relation", "arrowup").getImage(); 023 private static final Image ARROW_DOWN = ImageProvider.get("dialogs/relation", "arrowdown").getImage(); 024 private static final Image CORNERS = ImageProvider.get("dialogs/relation", "roundedcorners").getImage(); 025 private static final Image ROUNDABOUT_RIGHT = ImageProvider.get("dialogs/relation", "roundabout_right_tiny").getImage(); 026 private static final Image ROUNDABOUT_LEFT = ImageProvider.get("dialogs/relation", "roundabout_left_tiny").getImage(); 027 private transient WayConnectionType value = new WayConnectionType(); 028 029 @Override 030 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, 031 int row, int column) { 032 033 reset(); 034 if (value == null) 035 return this; 036 037 this.value = (WayConnectionType) value; 038 setToolTipText(((WayConnectionType) value).getTooltip()); 039 renderBackgroundForeground(getModel(table), null, isSelected); 040 return this; 041 } 042 043 @Override 044 public void paintComponent(Graphics g) { 045 super.paintComponent(g); 046 if (value == null || !value.isValid()) 047 return; 048 049 int ymax = this.getSize().height - 1; 050 int xloop = 10; 051 int xowloop = 0; 052 if (value.isOnewayLoopForwardPart) { 053 xowloop = -3; 054 } 055 if (value.isOnewayLoopBackwardPart) { 056 xowloop = 3; 057 } 058 059 int xoff = this.getSize().width / 2; 060 if (value.isLoop) { 061 xoff -= xloop / 2 - 1; 062 } 063 int w = 2; 064 int p = 2 + w + 1; 065 int y1; 066 int y2; 067 068 if (value.linkPrev) { 069 g.setColor(Color.black); 070 if (value.isOnewayHead) { 071 g.fillRect(xoff - 1, 0, 3, 1); 072 } else { 073 g.fillRect(xoff - 1 + xowloop, 0, 3, 1); 074 } 075 y1 = 0; 076 } else { 077 if (value.isLoop) { 078 g.setColor(Color.black); 079 y1 = 5; 080 g.drawImage(CORNERS, xoff, y1-3, xoff+3, y1, 0, 0, 3, 3, new Color(0, 0, 0, 0), null); 081 g.drawImage(CORNERS, xoff+xloop-2, y1-3, xoff+xloop+1, y1, 2, 0, 5, 3, new Color(0, 0, 0, 0), null); 082 g.drawLine(xoff+3, y1-3, xoff+xloop-3, y1-3); 083 } else { 084 g.setColor(Color.red); 085 if (value.isOnewayHead) { 086 g.drawRect(xoff-1, p - 3 - w, w, w); 087 } else { 088 g.drawRect(xoff-1 + xowloop, p - 1 - w, w, w); 089 } 090 y1 = p; 091 } 092 } 093 094 if (value.linkNext) { 095 g.setColor(Color.black); 096 if (value.isOnewayTail) { 097 g.fillRect(xoff - 1, ymax, 3, 1); 098 } else { 099 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}