1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.9. 
4    ** 
5    ** yWorks proprietary/confidential. Use is subject to license terms.
6    **
7    ** Redistribution of this file or of an unauthorized byte-code version
8    ** of this file is strictly forbidden.
9    **
10   ** Copyright (c) 2000-2011 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  package demo.layout.labeling;
15  
16  import y.layout.ProfitModel;
17  import y.layout.LabelCandidate;
18  import y.layout.EdgeLabelModel;
19  import y.layout.RotatedSliderEdgeLabelModel;
20  import y.layout.RotatedDiscreteEdgeLabelModel;
21  import demo.layout.labeling.CompositeEdgeLabelModel;
22  
23  /**
24   * A simple profit model used by the edge labeling demo.
25   *
26   */
27  public class DemoProfitModel implements ProfitModel {  
28    private double angle;
29    private double angleProfit;
30    private double otherProfit;
31  
32    /**
33     * For a given label candidate lc this profit model returns profit "angleProfit" if lc's angle == "angle"
34     * and profit "otherProfit", otherwise.
35     *
36     * @param angle the value of the angle (in radians) that has profit "angleProfit"
37     * @param angleProfit the profit used for angles with value "angle"
38     * @param otherProfit the profit used for angles with values != "angle"
39     */
40    public DemoProfitModel(double angle, double angleProfit, double otherProfit) {
41      this.angle = angle;
42      this.angleProfit = angleProfit;
43      this.otherProfit = otherProfit;
44    }
45  
46    public double getProfit(LabelCandidate candidate) {
47      Object param = candidate.getModelParameter();
48      if (param instanceof CompositeEdgeLabelModel.ModelParameter) {
49        double angle = determineAngle((CompositeEdgeLabelModel.ModelParameter) param);
50        if (angle == this.angle) {
51          return angleProfit;
52        } 
53      }
54      return otherProfit;
55    }
56  
57    //returns the angle encoded by the given model parameter
58    private static double determineAngle(CompositeEdgeLabelModel.ModelParameter param) {
59      EdgeLabelModel elm = param.getModel();
60      double angle = 0;
61      if(elm instanceof RotatedSliderEdgeLabelModel) {
62        angle = ((RotatedSliderEdgeLabelModel) elm).getAngle();
63      } else if(elm instanceof RotatedDiscreteEdgeLabelModel) {
64        angle = ((RotatedDiscreteEdgeLabelModel) elm).getAngle();
65      } else {
66        throw new RuntimeException("Unknown EdgeLableModel!");
67      }
68      return angle;
69    }
70  }
71