1   /****************************************************************************
2    * This demo file is part of yFiles for Java 2.14.
3    * Copyright (c) 2000-2017 by yWorks GmbH, Vor dem Kreuzberg 28,
4    * 72070 Tuebingen, Germany. All rights reserved.
5    * 
6    * yFiles demo files exhibit yFiles for Java functionalities. Any redistribution
7    * of demo files in source code or binary form, with or without
8    * modification, is not permitted.
9    * 
10   * Owners of a valid software license for a yFiles for Java version that this
11   * demo is shipped with are allowed to use the demo source code as basis
12   * for their own yFiles for Java powered applications. Use of such programs is
13   * governed by the rights and conditions as set out in the yFiles for Java
14   * license agreement.
15   * 
16   * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED
17   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18   * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19   * NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21   * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26   *
27   ***************************************************************************/
28  package demo.layout.labeling;
29  
30  import y.layout.ProfitModel;
31  import y.layout.LabelCandidate;
32  import y.layout.EdgeLabelModel;
33  import y.layout.RotatedSliderEdgeLabelModel;
34  import y.layout.RotatedDiscreteEdgeLabelModel;
35  import demo.layout.labeling.CompositeEdgeLabelModel;
36  
37  /**
38   * A simple profit model used by the edge labeling demo.
39   *
40   */
41  public class DemoProfitModel implements ProfitModel {  
42    private double angle;
43    private double angleProfit;
44    private double otherProfit;
45  
46    /**
47     * For a given label candidate lc this profit model returns profit "angleProfit" if lc's angle == "angle"
48     * and profit "otherProfit", otherwise.
49     *
50     * @param angle the value of the angle (in radians) that has profit "angleProfit"
51     * @param angleProfit the profit used for angles with value "angle"
52     * @param otherProfit the profit used for angles with values != "angle"
53     */
54    public DemoProfitModel(double angle, double angleProfit, double otherProfit) {
55      this.angle = angle;
56      this.angleProfit = angleProfit;
57      this.otherProfit = otherProfit;
58    }
59  
60    public double getProfit(LabelCandidate candidate) {
61      Object param = candidate.getModelParameter();
62      if (param instanceof CompositeEdgeLabelModel.ModelParameter) {
63        double angle = determineAngle((CompositeEdgeLabelModel.ModelParameter) param);
64        if (angle == this.angle) {
65          return angleProfit;
66        } 
67      }
68      return otherProfit;
69    }
70  
71    //returns the angle encoded by the given model parameter
72    private static double determineAngle(CompositeEdgeLabelModel.ModelParameter param) {
73      EdgeLabelModel elm = param.getModel();
74      double angle = 0;
75      if(elm instanceof RotatedSliderEdgeLabelModel) {
76        angle = ((RotatedSliderEdgeLabelModel) elm).getAngle();
77      } else if(elm instanceof RotatedDiscreteEdgeLabelModel) {
78        angle = ((RotatedDiscreteEdgeLabelModel) elm).getAngle();
79      } else {
80        throw new RuntimeException("Unknown EdgeLableModel!");
81      }
82      return angle;
83    }
84  }
85