View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.log4j.chainsaw.icons;
19  
20  import java.awt.BasicStroke;
21  import java.awt.Color;
22  import java.awt.Component;
23  import java.awt.Graphics;
24  import java.awt.Graphics2D;
25  import java.awt.GraphicsEnvironment;
26  import java.awt.RenderingHints;
27  import java.awt.image.BufferedImage;
28  
29  import javax.swing.Icon;
30  import javax.swing.ImageIcon;
31  
32  import org.apache.log4j.LogManager;
33  import org.apache.log4j.Logger;
34  
35  
36  /***
37   * A simple factory/facade for creating some of the standard Icons that are based
38   * on line drawings
39   * @author Paul Smith <psmith@apache.org>
40   * @author Scott Deboy <sdeboy@apache.org>
41   *
42   */
43  public final class LineIconFactory {
44  	private static final Logger logger = LogManager.getLogger(LineIconFactory.class);
45    /***
46     *
47     */
48    private LineIconFactory() {
49    }
50  
51    public static final Icon createExpandIcon() {
52        int size = 8;
53        int xOffSet = 0;
54        int yOffSet = 0;
55      try {
56        GraphicsEnvironment environment =
57          GraphicsEnvironment.getLocalGraphicsEnvironment();
58          BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
59        Graphics2D g2D =
60          environment.createGraphics(
61            image);
62          g2D.setBackground(new Color(0,0,0,0));
63          g2D.clearRect(0,0,size,size);
64          g2D.setStroke(new BasicStroke(1.5f));
65          g2D.setRenderingHint(
66            RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
67          g2D.setColor(Color.black);
68          g2D.drawLine(
69             xOffSet, (size / 2) + yOffSet, size - xOffSet,
70             (size / 2) + yOffSet);
71  
72          g2D.drawLine(
73             xOffSet + (size/2), yOffSet, xOffSet + (size/2),
74             (size) + yOffSet);
75          
76          return new ImageIcon(image);
77      } catch (Exception e) {
78        logger.error("failed to create a Expand icon", e);
79      }
80  
81      return null;
82    }
83    public static final Icon createCollapseIcon() {
84        int size = 8;
85        int xOffSet = 0;
86        int yOffSet = 0;
87      try {
88        GraphicsEnvironment environment =
89          GraphicsEnvironment.getLocalGraphicsEnvironment();
90          BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
91        Graphics2D g2D =
92          environment.createGraphics(
93            image);
94          g2D.setBackground(new Color(0,0,0,0));
95          g2D.clearRect(0,0,size,size);
96          g2D.setStroke(new BasicStroke(1.5f));
97          g2D.setRenderingHint(
98            RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
99          g2D.setColor(Color.black);
100         g2D.drawLine(
101            xOffSet, (size / 2) + yOffSet, size - xOffSet,
102            (size / 2) + yOffSet);
103         
104         return new ImageIcon(image);
105     } catch (Exception e) {
106       logger.error("failed to create a Collapse icon", e);
107     }
108 
109     return null;
110   }
111 
112   public static final Icon createCloseIcon() {
113     return new CloseIcon(8, 0, 0);
114   }
115 
116   public static final Icon createBlankIcon() {
117     return new BlankIcon(16);
118   }
119 
120   /***
121      * A nice and simple 'X' style icon that is used to indicate a 'close' operation.
122      *
123      * @author Scott Deboy <sdeboy@apache.org>
124      *
125      */
126   private static class BlankIcon implements Icon {
127     int size;
128 
129     public BlankIcon(int size) {
130       this.size = size;
131     }
132 
133     public int getIconHeight() {
134       return size;
135     }
136 
137     public int getIconWidth() {
138       return size;
139     }
140 
141     public void paintIcon(Component c, Graphics g, int x, int y) {
142     }
143   }
144 
145   /***
146      * A nice and simple 'X' style icon that is used to indicate a 'close' operation.
147      *
148      * @author Scott Deboy <sdeboy@apache.org>
149      *
150      */
151   private static class CloseIcon implements Icon {
152     int size;
153     int xOffSet;
154     int yOffSet;
155 
156     public CloseIcon(int size, int xOffSet, int yOffSet) {
157       this.size = size;
158       this.xOffSet = xOffSet;
159       this.yOffSet = yOffSet;
160     }
161 
162     public int getIconHeight() {
163       return size;
164     }
165 
166     public int getIconWidth() {
167       return size;
168     }
169 
170     public void paintIcon(Component c, Graphics g, int x, int y) {
171       Graphics2D g2D = (Graphics2D) g;
172       g2D.setStroke(new BasicStroke(1.5f));
173       g2D.setRenderingHint(
174         RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
175       g2D.setColor(Color.black);
176       g2D.drawLine(
177         x + xOffSet, y + yOffSet, x + size + xOffSet, y + size + yOffSet);
178       g2D.drawLine(
179         x + xOffSet, y + size + yOffSet, x + size + xOffSet, y + yOffSet);
180     }
181   }
182 }