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.mindmap;
29  
30  import y.view.LineType;
31  
32  import javax.swing.Icon;
33  import java.awt.Color;
34  import java.awt.Component;
35  import java.awt.Graphics;
36  import java.awt.Graphics2D;
37  import java.awt.Stroke;
38  import java.util.HashMap;
39  import java.util.Map;
40  
41  /**
42   * Central repository for state icons.
43   */
44  public class StateIconProvider {
45    /**
46     * Singleton instance of the icon repository.
47     */
48    public static final StateIconProvider instance = new StateIconProvider();
49  
50    /**
51     * State icon representing the "icon-less" state.
52     */
53    public static final StateIcon NULL_ICON = new StateIcon(null, new NullIconImpl());
54  
55  
56    /**
57     * Stores the available state icons.
58     */
59    private final Map icons;
60    
61    private StateIconProvider() {
62      icons = new HashMap();
63      add("smiley-happy");
64      add("smiley-not-amused");
65      add("smiley-grumpy");
66      add("abstract-green");
67      add("abstract-red");
68      add("abstract-blue");
69      add("questionmark");
70      add("exclamationmark");
71      add("delete");
72      add("checkmark");
73      add("star");
74    }
75  
76    /**
77     * Adds an image based state icon.
78     * @param name symbolic icon name as well as file name prefix. 
79     */
80    private void add( final String name ) {
81      icons.put(name, new StateIcon(name, MindMapUtil.getIcon(name + "-16.png")));
82    }
83  
84    /**
85     * Returns the state icon for the specified name.
86     * @param iconName one of
87     * <ul>
88     * <li><code>smiley-happy</code></li>
89     * <li><code>smiley-not-amused</code></li>
90     * <li><code>smiley-grumpy</code></li>
91     * <li><code>abstract-green</code></li>
92     * <li><code>abstract-red</code></li>
93     * <li><code>abstract-blue</code></li>
94     * <li><code>questionmark</code></li>
95     * <li><code>exclamationmark</code></li>
96     * <li><code>delete</code></li>
97     * <li><code>checkmark</code></li>
98     * <li><code>star</code></li>
99     * </ul>
100    * @return the state icon for the specified name or <code>null</code>
101    * if there is no such icon.
102    */
103   public StateIcon getIcon(String iconName) {
104     return (StateIcon) icons.get(iconName);
105   }
106 
107 
108   /**
109    * Associates a name to an icon.
110    */
111   public static final class StateIcon implements Icon {
112     private final String name;
113     private final Icon icon;
114 
115     /**
116      * Initializes a new <code>StateIcon</code> instance with the given name.
117      * @param name the name of the state icon.
118      * @param icon the actual visual representation of the state icon.
119      */
120     public StateIcon( final String name, final Icon icon ) {
121       this.name = name;
122       this.icon = icon;
123     }
124 
125     /**
126      * Returns this icon's name.
127      * @return this icon's name.
128      */
129     public String getName() {
130       return name;
131     }
132 
133     public int getIconHeight() {
134       return icon.getIconHeight();
135     }
136 
137     public int getIconWidth() {
138       return icon.getIconWidth();
139     }
140 
141     public void paintIcon(
142             final Component c, final Graphics g, final int x, final int y
143     ) {
144       icon.paintIcon(c, g, x, y);
145     }
146   }
147 
148   /**
149    * Displays a black, crossed-out square.
150    */
151   private static final class NullIconImpl implements Icon {
152     private static final int ICON_SIZE = 16;
153 
154     public int getIconHeight() {
155       return ICON_SIZE;
156     }
157 
158     public int getIconWidth() {
159       return ICON_SIZE;
160     }
161 
162     public void paintIcon( final Component c, final Graphics g, final int x, final int y ) {
163       final Graphics2D gfx = (Graphics2D) g;
164 
165       final Color oldColor = gfx.getColor();
166       gfx.setColor(Color.BLACK);
167       final Stroke oldStroke = gfx.getStroke();
168       gfx.setStroke(LineType.LINE_1);
169 
170       final int offset = 2;
171       final int w = getIconWidth();
172       final int h = getIconHeight();
173       final int innerWidth = w - offset;
174       final int innerHeight = h - offset;
175       gfx.drawRect(x, y, x + w, y + h);
176       gfx.drawLine(x + offset, y + offset, x + innerWidth, y + innerHeight);
177       gfx.drawLine(x + offset, y + innerHeight, x + innerWidth, y + offset);
178 
179       gfx.setStroke(oldStroke);
180       gfx.setColor(oldColor);
181     }
182   }
183 }
184