~ubuntu-branches/ubuntu/quantal/libjgraph-java/quantal

« back to all changes in this revision

Viewing changes to src/org/jgraph/graph/EdgeView.java

  • Committer: Bazaar Package Importer
  • Author(s): gregor herrmann
  • Date: 2008-05-09 18:29:56 UTC
  • mfrom: (1.1.5 upstream) (4.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080509182956-n9mkbgtt36kkj2h0
Tags: 5.12.1.0.dfsg-1
* New upstream release.
* Switch from dpatch to quilt.
* Change my email address in several files under debian/.
* debian/copyright: update and compact.
* debian/control: build depend on default-jdk-builddep, depend on
  default-jre instead of specifying a specific JDK/JRE; change JAVA_HOME
  in debian/rules accordingly.
* Install API documentation to /usr/share/doc/libjgraph-java-doc/api/
  instead of directly under libjgraph-java-doc/

Show diffs side-by-side

added added

removed removed

Lines of Context:
582
582
                return labelVector;
583
583
        }
584
584
 
 
585
        /**
 
586
         * Returns the absolute position of the main label
 
587
         * @return the absolute position of the main label
 
588
         */
 
589
        protected Point2D getAbsoluteLabelPosition() {
 
590
                Point2D result = getAbsoluteLabelPositionFromRelative(GraphConstants.getLabelPosition(getAllAttributes()));
 
591
                return result;
 
592
        }
 
593
        
 
594
        /**
 
595
         * Returns the absolute position of the specified extra label
 
596
         * @param index the index of the extra label
 
597
         * @return the absolute position of the specified extra label
 
598
         */
 
599
        protected Point2D getAbsoluteExtraLabelPosition(int index) {
 
600
                Point2D[] positions = GraphConstants
 
601
                                .getExtraLabelPositions(getAllAttributes());
 
602
                if (positions != null && positions.length > index) {
 
603
                        Point2D result = getAbsoluteLabelPositionFromRelative(positions[index]);
 
604
                        return result;
 
605
                }
 
606
                return null;
 
607
        }
 
608
        
 
609
        /**
 
610
         * Converts relative label position to absolute and allows for
 
611
         * any label offset.
 
612
         * @param geometry the relative label position
 
613
         * @return the absolute label position including any offset
 
614
         */
 
615
        protected Point2D getAbsoluteLabelPositionFromRelative(Point2D geometry) {
 
616
                Point2D result = convertRelativeLabelPositionToAbsolute(geometry);
 
617
                
 
618
                if (result != null)
 
619
                {
 
620
                        double offsetX = 0;
 
621
                        double offsetY = 0;
 
622
 
 
623
                        Point2D offset = GraphConstants.getOffset(getAllAttributes());
 
624
 
 
625
                        if (offset != null) {
 
626
                                offsetX = offset.getX();
 
627
                                offsetY = offset.getY();
 
628
                        }
 
629
 
 
630
                        double x = result.getX() + offsetX;
 
631
                        double y = result.getY() + offsetY;
 
632
                        return new Point2D.Double(x, y);
 
633
                }
 
634
                
 
635
                return null;
 
636
        }
 
637
        
 
638
        /**
 
639
         * Converts an relative label position (x is distance along edge and y is
 
640
         * distance above/below edge vector) into an absolute co-ordination point
 
641
         * @param geometry the relative label position
 
642
         * @return the absolute label position
 
643
         */
 
644
        protected Point2D convertRelativeLabelPositionToAbsolute(Point2D geometry) {
 
645
                int pointCount = getPointCount();
 
646
                
 
647
                double length = 0;
 
648
                double[] segments = new double[pointCount];
 
649
                Point2D pt = getPoint(0);
 
650
                
 
651
                if (pt != null)
 
652
                {
 
653
                        // Find the total length of the segments and also store the length
 
654
                        // of each segment
 
655
                        for (int i = 1; i < pointCount; i++)
 
656
                        {
 
657
                                Point2D tmp = getPoint(i);
 
658
                                
 
659
                                if (tmp != null)
 
660
                                {
 
661
                                        double dx = pt.getX() - tmp.getX();
 
662
                                        double dy = pt.getY() - tmp.getY();
 
663
 
 
664
                                        double segment = Math.sqrt(dx * dx + dy * dy);
 
665
                                        
 
666
                                        segments[i - 1] = segment;
 
667
                                        length += segment;
 
668
                                        pt = tmp;
 
669
                                }
 
670
                        }
 
671
 
 
672
                        // Change x to be a value between 0 and 1 indicating how far
 
673
                        // along the edge the label is
 
674
                        double x = geometry.getX()/GraphConstants.PERMILLE;
 
675
                        double y = geometry.getY();
 
676
                        
 
677
                        // dist is the distance along the edge the label is
 
678
                        double dist = x * length;
 
679
                        length = 0;
 
680
                        
 
681
                        int index = 1;
 
682
                        double segment = segments[0];
 
683
 
 
684
                        // Find the length up to the start of the segment the label is
 
685
                        // on (length) and retrieve the length of that segment (segment)
 
686
                        while (dist > length + segment && index < pointCount - 1)
 
687
                        {
 
688
                                length += segment;
 
689
                                segment = segments[index++];
 
690
                        }
 
691
 
 
692
                        // factor is the proportion along this segment the label lies at
 
693
                        double factor = (dist - length) / segment;
 
694
                        
 
695
                        Point2D p0 = getPoint(index - 1);
 
696
                        Point2D pe = getPoint(index);
 
697
 
 
698
                        if (p0 != null && pe != null)
 
699
                        {
 
700
                                // The x and y offsets of the label from the start point
 
701
                                // of the segment
 
702
                                double dx = pe.getX() - p0.getX();
 
703
                                double dy = pe.getY() - p0.getY();
 
704
                                
 
705
                                // The normal vectors of
 
706
                                double nx = dy / segment;
 
707
                                double ny = dx / segment;
 
708
                                
 
709
                                // The x position is the start x of the segment + the factor of
 
710
                                // the x offset between the start and end of the segment + the
 
711
                                // x component of the y (height) offset contributed along the
 
712
                                // normal vector.
 
713
                                x = p0.getX() + dx * factor - nx * y;
 
714
 
 
715
                                // The x position is the start y of the segment + the factor of
 
716
                                // the y offset between the start and end of the segment + the
 
717
                                // y component of the y (height) offset contributed along the
 
718
                                // normal vector.
 
719
                                y = p0.getY() + dy * factor + ny * y;
 
720
                                return new Point2D.Double(x, y);
 
721
                        }
 
722
                }
 
723
                
 
724
                return null;
 
725
        }
 
726
 
585
727
        //
586
728
        // Routing
587
729
        //
1023
1165
                                                }
1024
1166
                                        }
1025
1167
                                        Rectangle2D dirty = edge.getBounds();
1026
 
                                        if (label)
 
1168
                                        if (label) {
1027
1169
                                                edge.setLabelPosition(p);
1028
 
                                        else
 
1170
                                        } else {
1029
1171
                                                edge.setExtraLabelPosition(currentLabel, p);
 
1172
                                        }
1030
1173
                                        edge.update(graph.getGraphLayoutCache());
1031
1174
                                        if (graph.isXorEnabled()) {
1032
1175
                                                overlay(graph.getGraphics());
1147
1290
                        Point2D p0 = edge.getPoint(0);
1148
1291
                        Point2D pt = p0;
1149
1292
                        
 
1293
                        // Calculate the total length of the edge
1150
1294
                        for (int i = 1; i < pointCount; i++)
1151
1295
                        {
1152
1296
                                Point2D tmp = edge.getPoint(i);
1164
1308
                                }
1165
1309
                        }
1166
1310
                        
 
1311
                        // Work which line segment the point of the label is closest to
1167
1312
                        Point2D last = edge.getPoint(1);
1168
1313
                        Line2D line = new Line2D.Double(p0, last);
1169
1314
                        double minDist = line.ptSegDistSq(p);
1177
1322
                                tmp += segments[i-2];
1178
1323
                                
1179
1324
                                line = new Line2D.Double(edge.getPoint(i), last);
1180
 
                                double dist = line.ptLineDistSq(p);
 
1325
                                double dist = line.ptSegDistSq(p);
1181
1326
                                
1182
1327
                                if (dist < minDist) {
1183
1328
                                        minDist = dist;
1195
1340
                        double x2 = pt.getX();
1196
1341
                        double y2 = pt.getY();
1197
1342
                        
1198
 
                        pt = edge.getPoint(index+1);
 
1343
                        Point2D pt2 = edge.getPoint(index+1);
1199
1344
                        
1200
 
                        double x1 = pt.getX();
1201
 
                        double y1 = pt.getY();
 
1345
                        double x1 = pt2.getX();
 
1346
                        double y1 = pt2.getY();
1202
1347
                        
1203
1348
                        double px = p.getX();
1204
1349
                        double py = p.getY();
1205
1350
                        
1206
 
                        x2 -= x1;
1207
 
                        y2 -= y1;
 
1351
                        double xSegment = x2 - x1;
 
1352
                        double ySegment = y2 - y1;
1208
1353
        
1209
1354
                        px -= x1;
1210
1355
                        py -= y1;
1211
1356
        
1212
 
                        double dotprod = px * x2 + py * y2;
1213
1357
                        double projlenSq = 0;
1214
1358
        
1215
 
                    px = x2 - px;
1216
 
                    py = y2 - py;
1217
 
                    dotprod = px * x2 + py * y2;
 
1359
                    px = xSegment - px;
 
1360
                    py = ySegment - py;
 
1361
                    double dotprod = px * xSegment + py * ySegment;
1218
1362
 
1219
1363
                    if (dotprod <= 0.0)
1220
1364
                    {
1222
1366
                    }
1223
1367
                    else
1224
1368
                    {
1225
 
                                projlenSq = dotprod * dotprod / (x2 * x2 + y2 * y2);
 
1369
                                projlenSq = dotprod * dotprod / (xSegment * xSegment + ySegment * ySegment);
1226
1370
                    }
1227
1371
 
1228
 
                        tmp = Math.sqrt(projlenSq);
1229
 
                        if (tmp > seg)
1230
 
                        {
1231
 
                                tmp = seg;                                              
1232
 
                        }
1233
 
 
1234
 
                        p0 = edge.getPoint(index);
1235
 
                        Point2D pe = edge.getPoint(index+1);
1236
 
                        
1237
 
                        double offsetX = 0;
1238
 
                        double offsetY = 0;
1239
 
 
1240
 
                        if (p0 != null && pe != null)
1241
 
                        {
1242
 
                                double factor = tmp / seg;
1243
 
                                
1244
 
                                double dx = pe.getX() - p0.getX();
1245
 
                                double dy = pe.getY() - p0.getY();
1246
 
 
1247
 
                                double tx = p0.getX() + dx * factor;
1248
 
                                double ty = p0.getY() + dy * factor;
1249
 
                                
1250
 
                                offsetX = p.getX() - tx;
1251
 
                                offsetY = p.getY() - ty;
1252
 
                        }
1253
 
                        
1254
 
                        // Uses the offset constant to align the label to the point
1255
 
                        // from the relative location on the edge shape
1256
 
                        Point2D off = new Point2D.Double(offsetX, offsetY);
1257
 
                        GraphConstants.setOffset(edge.getAllAttributes(), off);
1258
 
 
1259
 
                        // Contructs the relative point for the label
1260
 
                        Point2D result = new Point2D.Double(((((totalLength/2 - length - tmp)/
1261
 
                                        totalLength)*-2)+1)*GraphConstants.PERMILLE / 2, 0);
1262
 
 
 
1372
                    double projlen = Math.sqrt(projlenSq);
 
1373
                        if (projlen > seg)
 
1374
                        {
 
1375
                                projlen = seg;                                          
 
1376
                        }
 
1377
 
 
1378
                        double yDistance = Line2D.ptLineDist(pt2.getX(), pt2.getY(), pt.getX(), pt.getY(), p.getX(), p.getY());
 
1379
                        int direction = Line2D.relativeCCW(pt2.getX(), pt2.getY(), pt.getX(), pt.getY(), p.getX(), p.getY());
 
1380
                        
 
1381
                        if (direction == -1) {
 
1382
                                yDistance = -yDistance;
 
1383
                        }
 
1384
                        
 
1385
                        // Constructs the relative point for the label
 
1386
                        Point2D result = new Point2D.Double(((((totalLength/2 - length - projlen)/
 
1387
                                        totalLength)*-2)+1)*GraphConstants.PERMILLE / 2, yDistance);
 
1388
                        
 
1389
                        // Use the utility method to find 
 
1390
                        Point2D storedRelativePosition = edge.convertRelativeLabelPositionToAbsolute(result);
 
1391
                        if (p.equals(storedRelativePosition)) {
 
1392
                                GraphConstants.setRemoveAttributes(edge.getAllAttributes(), new Object[] {GraphConstants.OFFSET});
 
1393
                        } else {
 
1394
                                Point2D off = new Point2D.Double(p.getX() - storedRelativePosition.getX(), p.getY() - storedRelativePosition.getY());
 
1395
                                GraphConstants.setOffset(edge.getAllAttributes(), off);
 
1396
                        }
1263
1397
                        return result;
1264
1398
                }
1265
1399
 
1388
1522
                        EdgeView e = relevantEdge;
1389
1523
                        int handlesize = graph.getHandleSize();
1390
1524
                        EdgeRenderer er = (EdgeRenderer) edge.getRenderer();
1391
 
                        Point2D p = graph.toScreen(er.getLabelPosition(e));
 
1525
                        Point2D labelPosition = er.getLabelPosition(e);
 
1526
                        Point2D p = null;
 
1527
                        if (labelPosition != null) {
 
1528
                                p = (Point2D)labelPosition.clone();
 
1529
                                graph.toScreen(p);
 
1530
                        }
1392
1531
                        Dimension d = er.getLabelSize(e, graph.convertValueToString(e));
1393
1532
                        if (p != null && d != null) {
1394
1533
                                Point2D s = graph.toScreen(new Point2D.Double(d.width,