1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.6. 
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-2008 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  package demo.view.realizer;
15  
16  import java.awt.Color;
17  import java.awt.Graphics2D;
18  import java.awt.Polygon;
19  import javax.swing.JFrame;
20  import javax.swing.JRootPane;
21  
22  import y.view.EditMode;
23  import y.view.Graph2DView;
24  import y.view.NodeRealizer;
25  
26  
27  
28  /**
29   * NodeRealizer implementation that draws a node as
30   * a dog-eared note as thez are used in UML diagrams
31   *
32   * Executing this class will display a sample instance of this realizer.
33   */
34  public class NoteNodeRealizer extends NodeRealizer
35  {
36    /**
37     * Obligatory default constructor.
38     */
39    public NoteNodeRealizer()
40    {
41      setSize(100,30);
42      setFillColor(Color.lightGray);
43    }
44    
45    /**
46     * Obligatory copy constructor
47     */
48    public NoteNodeRealizer(NodeRealizer r)
49    {
50      super(r);
51    }
52    
53    /**
54     * Obligatory polymorphic copy constructor
55     */
56    public NodeRealizer createCopy(NodeRealizer r)
57    {
58      return new NoteNodeRealizer(r);
59    }
60    
61    /**
62     * Paints the node as a dog-eared rectangle.
63     */
64    public void paintNode(Graphics2D gfx)
65    {
66      if(isSelected())
67      {
68        gfx.setColor(getFillColor().darker());
69        paintHotSpots(gfx);
70      }
71      else
72        gfx.setColor(getFillColor());
73      
74      double cornerW = width > 20.0 ? 15.0 : 0.0;
75      double cornerH = height > 20.0 ? 15.0 : 0.0;
76      
77      Polygon corner = new Polygon();
78      corner.addPoint((int)(x+width-cornerW),(int)y);
79      corner.addPoint((int)(x+width-cornerW),(int)(y+cornerH));
80      corner.addPoint((int)(x+width),(int)(y+cornerH));
81      
82      gfx.fillRect((int)x,(int)y,(int)(width-cornerW),(int)cornerH);
83      gfx.fillRect((int)x,(int)(y+cornerH),(int)(width),(int)(height-cornerH));
84      gfx.setColor(Color.white);
85      gfx.fillPolygon(corner);
86      gfx.setColor(Color.black);
87      gfx.drawPolygon(corner);
88      gfx.drawLine((int)x,(int)y,(int)(x+width-cornerW),(int)(y)); //top
89      gfx.drawLine((int)x,(int)(y+height),(int)(x+width),(int)(y+height)); //bottom
90      gfx.drawLine((int)x,(int)y,(int)(x),(int)(y+height)); //left
91      gfx.drawLine((int)(x+width),(int)(y+cornerH),  //right
92                   (int)(x+width),(int)(y+height));
93    
94      paintText(gfx);
95    }
96  
97  
98  
99    public static void addContentTo( final JRootPane rootPane )
100   {
101     final NoteNodeRealizer r = new NoteNodeRealizer();
102     r.setLabelText("This is\na note");
103 
104     final Graph2DView view = new Graph2DView();
105     view.getGraph2D().setDefaultNodeRealizer(r.createCopy());
106     view.setAntialiasedPainting(true);
107     view.getGraph2D().createNode();
108     view.addViewMode(new EditMode());
109     view.fitContent();
110 
111     rootPane.setContentPane(view);
112   }
113 
114   /**
115    * Launcher method. Execute this class to see a sample instantiation of
116    * this node realizer in action.
117    */
118   public static void main(String[] args)
119   {
120     final JFrame frame = new JFrame();
121     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
122     addContentTo(frame.getRootPane());
123     frame.pack();
124     frame.setLocationRelativeTo(null);
125     frame.setVisible(true);
126   }
127 }
128 
129 
130