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