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.view;
29  
30  import java.awt.Color;
31  import java.util.Map;
32  
33  import y.base.Node;
34  import y.base.NodeCursor;
35  import y.geom.OrientedRectangle;
36  import y.view.Arrow;
37  import y.view.GenericNodeRealizer;
38  import y.view.Graph2D;
39  import y.view.Graph2DView;
40  import y.view.NodeLabel;
41  import y.view.NodeRealizer;
42  import y.view.PolyLineEdgeRealizer;
43  import y.view.ShinyPlateNodePainter;
44  import y.view.GenericNodeRealizer.Factory;
45  import y.view.SmartNodeLabelModel;
46  
47  import javax.swing.UIManager;
48  
49  /**
50   * Provides default node and edge realizer configurations used by most demos.
51   */
52  public class DemoDefaults {
53    
54    /**
55     * The Name of the GenericNodeRealizer configuration of the default node used by most demos.  
56     */
57    public static final String NODE_CONFIGURATION = "DemoDefaults#Node";           
58    
59    /**
60     * The default node fill color used by most demos 
61     */
62    public static final Color DEFAULT_NODE_COLOR = new Color(255, 153, 0);
63  
64    /**
65     * The default node line color used by most demos. This is set to <code>null</code> meaning no border is drawn.
66     */
67    public static final Color DEFAULT_NODE_LINE_COLOR = null;
68  
69    /**
70     * The default secondary or contract color used by most demos 
71     */
72    public static final Color DEFAULT_CONTRAST_COLOR = new Color(202,227,255);
73  
74    private DemoDefaults() {
75    }
76  
77    static {
78      registerDefaultNodeConfiguration(true);       
79    }
80  
81    /**
82     * Registers the default node configuration for yFiles demo applications.
83     * This method is called automatically when the <code>DemoDefaults</code>
84     * class is initialized. 
85     * @param drawShadows if <code>true</code>, a drop shadow is drawn for nodes
86     * that use the default configuration; otherwise no shadow is drawn.
87     */
88    public static void registerDefaultNodeConfiguration(boolean drawShadows) {
89      Factory factory = GenericNodeRealizer.getFactory();
90      Map configurationMap = factory.createDefaultConfigurationMap();
91  
92      ShinyPlateNodePainter painter = new ShinyPlateNodePainter();
93      // ShinyPlateNodePainter has an option to draw a drop shadow that is more efficient
94      // than wrapping it in a ShadowNodePainter.
95      painter.setDrawShadow(drawShadows);
96  
97      configurationMap.put(GenericNodeRealizer.Painter.class, painter);
98      configurationMap.put(GenericNodeRealizer.ContainsTest.class, painter);
99      factory.addConfiguration(NODE_CONFIGURATION, configurationMap);
100   }
101 
102   /**
103    * Configures the default node and edge realizer of the specified view's
104    * associated graph.
105    * <p>
106    * The default representation used for a node is provided by a
107    * {@link y.view.GenericNodeRealizer} that uses the configuration mapped to
108    * {@link #NODE_CONFIGURATION}.
109    * The default colors (fill, border) for a node are set to
110    * {@link #DEFAULT_NODE_COLOR}, and {@link #DEFAULT_NODE_LINE_COLOR},
111    * respectively.
112    * </p>
113    * <p>
114    * The default representation for an edge is provided by a
115    * {@link y.view.PolyLineEdgeRealizer} with a standard arrow used on its
116    * target side.
117    * </p>
118  ` */
119   public static void configureDefaultRealizers(Graph2DView view) {
120     NodeRealizer nr = new GenericNodeRealizer(NODE_CONFIGURATION);
121     nr.setFillColor(DEFAULT_NODE_COLOR);
122     nr.setLineColor(DEFAULT_NODE_LINE_COLOR);
123     nr.setWidth(60.0);
124     nr.setHeight(30.0);
125     NodeLabel label = nr.getLabel();
126     SmartNodeLabelModel model = new SmartNodeLabelModel();
127     label.setLabelModel(model, model.getDefaultParameter());
128     view.getGraph2D().setDefaultNodeRealizer(nr);    
129         
130     // By default, edges show their direction using the standard arrowhead.
131     PolyLineEdgeRealizer er = new PolyLineEdgeRealizer();
132     er.setTargetArrow(Arrow.STANDARD);
133     view.getGraph2D().setDefaultEdgeRealizer(er);  
134   }
135 
136   /**
137    * Applies NodeRealizer defaults to all nodes. Properties not applied are location and size.
138    */
139   public static void applyRealizerDefaults(Graph2D graph) {
140     applyRealizerDefaults(graph, false, true);
141   }
142 
143   /**
144    * Applies NodeRealizer defaults to all nodes. Properties not applied are location, and,
145    * depending on the given arguments, size and fillColor.      
146    */
147   public static void applyRealizerDefaults(Graph2D graph, boolean applyDefaultSize, boolean applyFillColor) {
148     for(NodeCursor nc = graph.nodes(); nc.ok(); nc.next()) {
149       GenericNodeRealizer gnr = new GenericNodeRealizer(graph.getRealizer(nc.node()));
150       gnr.setConfiguration(NODE_CONFIGURATION);
151       if(applyFillColor) {
152         gnr.setFillColor(graph.getDefaultNodeRealizer().getFillColor());
153       }
154       gnr.setLineColor(null);      
155       if(applyDefaultSize) {
156         gnr.setSize(graph.getDefaultNodeRealizer().getWidth(), graph.getDefaultNodeRealizer().getHeight());
157       }
158       NodeLabel label = gnr.getLabel();
159       OrientedRectangle labelBounds = label.getOrientedBox();
160       SmartNodeLabelModel model = new SmartNodeLabelModel();
161       label.setLabelModel(model, model.createModelParameter(labelBounds, gnr));
162       graph.setRealizer(nc.node(), gnr);      
163     }        
164   }
165 
166   /**
167    * Applies the given fill color to all nodes
168    */
169   public static void applyFillColor(Graph2D graph, Color color) {
170     for (NodeCursor nc = graph.nodes(); nc.ok(); nc.next()) {
171       Node n = nc.node();
172       graph.getRealizer(n).setFillColor(color);
173     } 
174   }
175 
176   /**
177    * Applies the given fill color to all nodes
178    */
179   public static void applyLineColor(Graph2D graph, Color color) {
180     for (NodeCursor nc = graph.nodes(); nc.ok(); nc.next()) {
181       Node n = nc.node();
182       graph.getRealizer(n).setLineColor(color);
183     } 
184   }
185 
186   /**
187    * Initializes to a "nice" look and feel for GUI demo applications.
188    */
189   public static void initLnF() {
190     try {
191       if (!"com.sun.java.swing.plaf.motif.MotifLookAndFeel".equals(UIManager.getSystemLookAndFeelClassName())
192           && !"com.sun.java.swing.plaf.gtk.GTKLookAndFeel".equals(UIManager.getSystemLookAndFeelClassName())
193           && !UIManager.getSystemLookAndFeelClassName().equals(UIManager.getLookAndFeel().getClass().getName())
194           && !isJRE4onWindows7()) {
195         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
196       }
197     } catch (Exception e) {
198       e.printStackTrace();
199     }
200   }
201 
202   private static boolean isJRE4onWindows7() {
203     // check for 'os.name == Windows 7' does not work, since JDK 1.4 uses the compatibility mode
204     return System.getProperty("java.version").startsWith("1.4") && System.getProperty("os.name").startsWith("Windows")
205         && "6.1".equals(System.getProperty("os.version"));
206   }
207 
208 }
209