1 19 20 package taskblocks.graph; 21 22 import java.awt.FontMetrics ; 23 import java.awt.Graphics2D ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 27 33 class TaskLayouter { 34 35 38 static void recountBounds(int _graphTop, int _rowHeight, 39 TaskGraphRepresentation _builder, TaskGraphComponent _graph, Graphics2D g2) { 40 41 int rowIndex = 0; 42 FontMetrics fm = g2.getFontMetrics(); 43 int maxRowWidth = fm.stringWidth("Worker"); 44 for (TaskRow row : _builder._rows) { 45 maxRowWidth = Math.max(maxRowWidth, fm.stringWidth(row._name)); 46 } 47 _graph._headerWidth = maxRowWidth + 20; 48 _graph.recountBounds(); 49 50 int cummulatedRowAdd = _graph._scrollTop; 52 for (TaskRow row : _builder._rows) { 53 int rowTop = (int) (_graphTop + rowIndex * _rowHeight + cummulatedRowAdd); 54 55 int maxUpPadding = 0; 58 int maxDownPadding = 0; 59 List <Connection> processedConnections = new ArrayList <Connection>(); 60 for (Task t : row._tasks) { 61 for (int i = 0; i < t._outgoingConnections.length; i++) { 62 Connection c = t._outgoingConnections[i]; 63 boolean goingDown = row._index <= c._toTask.getRow()._index; 65 int connPadding = 1; 66 67 if (c._fromTask.getRow() == c._toTask.getRow()) { 71 connPadding = 2; 72 } 73 boolean run = true; 79 while (run) { 80 run = false; 81 for (Connection c2 : processedConnections) { 82 boolean c2GoingDown = row._index <= c2._toTask 83 .getRow()._index; 84 if (goingDown == c2GoingDown 85 && connPadding == c2._padding 86 && crosses(c, c2)) { 87 connPadding++; 88 run = true; 89 break; } 91 } 92 } 93 c._padding = connPadding; 94 processedConnections.add(c); 95 if (goingDown) { 96 maxDownPadding = Math.max(maxDownPadding, connPadding); 97 } else { 98 maxUpPadding = Math.max(maxUpPadding, connPadding); 99 } 100 } 101 } 102 rowTop += maxUpPadding * TaskGraphComponent.CONN_PADDING_FACTOR; 103 cummulatedRowAdd += (maxDownPadding + 1) * TaskGraphComponent.CONN_PADDING_FACTOR 104 + maxUpPadding * TaskGraphComponent.CONN_PADDING_FACTOR; 105 row._topPosition = rowTop; 106 row._topPadding = maxUpPadding; 107 row._bottomPadding = maxDownPadding+1; 108 109 for (Task t : row._tasks) { 110 111 double left = _graph.timeToX(t.getStartTime()); 112 double width = t.getRealDuration() * _graph._dayWidth; 113 double top = rowTop + TaskGraphComponent.CONN_PADDING_FACTOR; 114 double height = _rowHeight 115 - TaskGraphComponent.CONN_PADDING_FACTOR; 116 t._bounds.setBounds((int) left+2, (int) top, (int) width-4, 117 (int) height); 118 } 119 rowIndex++; 120 121 } 123 for(Connection c: _builder._connections) { 124 recountConnectionBounds(c, _graph); 125 } 126 127 _builder.clearPaintDirtyFlag(); 128 129 } 130 131 138 static boolean crosses(Connection c1, Connection c2) { 139 long c1Left = c1._fromTask.getFinishTime(); 140 long c1Right = c1._toTask.getStartTime(); 141 long c2Left = c2._fromTask.getFinishTime(); 142 long c2Right = c2._toTask.getStartTime(); 143 144 if (c1Left >= c2Left && c1Left <= c2Right) { 146 return true; 147 } 148 if (c1Right >= c2Left && c1Right <= c2Right) { 150 return true; 151 } 152 153 if (c2Left >= c1Left && c2Left <= c1Right) { 155 return true; 156 } 157 if (c2Right >= c1Left && c2Right <= c1Right) { 159 return true; 160 } 161 162 return false; 163 } 164 165 static void recountConnectionBounds(Connection c, TaskGraphComponent _graph) { 166 long fromTime = c._fromTask.getFinishTime(); 167 long toTime = c._toTask.getStartTime(); 168 int x1 = _graph.timeToX(fromTime); 169 int x2 = _graph.timeToX(toTime); 170 int y1, y2; 171 boolean goingDown = c._fromTask.getRow()._index <= c._toTask.getRow()._index; 172 if(goingDown) { 173 y1 = c._fromTask.getRow()._topPosition + TaskGraphComponent.ROW_HEIGHT; 174 } else { 175 y1 = c._fromTask.getRow()._topPosition + TaskGraphComponent.CONN_PADDING_FACTOR; 176 } 177 boolean commingFromUp = c._fromTask.getRow()._index < c._toTask.getRow()._index; 178 if(commingFromUp) { 179 y2 = c._toTask.getRow()._topPosition + TaskGraphComponent.CONN_PADDING_FACTOR; 180 } else { 181 y2 = c._toTask.getRow()._topPosition + TaskGraphComponent.ROW_HEIGHT; 182 } 183 184 185 int mediumY; 186 if(y2 >= y1) { 187 mediumY = y1 + c._padding*TaskGraphComponent.CONN_PADDING_FACTOR; 188 } else { 189 mediumY = y1 - c._padding*TaskGraphComponent.CONN_PADDING_FACTOR; 190 } 191 c._path.xpoints[0] = x1; 192 c._path.ypoints[0] = y1; 193 c._path.xpoints[1] = x1; 194 c._path.ypoints[1] = mediumY; 195 c._path.xpoints[2] = x2; 196 c._path.ypoints[2] = mediumY; 197 c._path.xpoints[3] = x2; 198 c._path.ypoints[3] = y2; 199 200 } 201 202 } 203 | Popular Tags |