View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components;
21  
22  import java.awt.Container;
23  import java.awt.Dimension;
24  import java.awt.Graphics;
25  import java.awt.Graphics2D;
26  import java.awt.GridLayout;
27  import java.awt.Insets;
28  import java.awt.event.WindowAdapter;
29  import java.awt.event.WindowEvent;
30  
31  import javax.swing.JApplet;
32  import javax.swing.JFrame;
33  import javax.swing.JPanel;
34  
35  import com.cosylab.gui.components.util.PaintHelper;
36  
37  
38  /**
39   * A simple resizable button identified by an arrow icon.
40   *
41   * @author <a href="mailto:jernej.kamenik@cosylab.com">Jernej Kamenik</a>
42   * @version $id$
43   *
44   * @see com.cosylab.gui.components.SimpleButton
45   */
46  public class ArrowButton extends SimpleButton
47  {
48      private static final long serialVersionUID = -2014851617632168689L;
49  
50      public static final int PREFERRED_SIZE = 20;
51  
52      /** Arrow icon pointing upwards. */
53  	public static int UP = 0;
54  
55  	/** Arrow icon pointing downwards. */
56  	public static int DOWN = 1;
57  
58  	/** Arrow icon pointing to the left. */
59  	public static int LEFT = 2;
60  
61  	/** Arrow icon pointing to the right. */
62  	public static int RIGHT = 3;
63  	private Insets insets;
64  	private int orientation;
65  
66  	/**
67  	 * Constructor for ArrowButton. Sets the orientation of the arrow icon.
68  	 *
69  	 * @param newOrientation int
70  	 */
71  	public ArrowButton(int newOrientation)
72  	{
73  		super();
74  
75  		if (newOrientation != UP && newOrientation != DOWN
76  		    && newOrientation != LEFT && newOrientation != RIGHT) {
77  			throw (new IllegalArgumentException());
78  		}
79  
80  		orientation = newOrientation;
81  		setMinimumSize(new Dimension(10, 10));
82  		setPreferredSize(new Dimension(PREFERRED_SIZE, PREFERRED_SIZE));
83  		insets = null;
84  		setActionMode(CHAIN_ACTION_MODE);
85  	}
86  
87  	/**
88  	 * Sets the insets of the arrow icon.
89  	 *
90  	 * @param newInsets Insets
91  	 */
92  	public void setArrowInsets(Insets newInsets)
93  	{
94  		Insets oldInsets = insets;
95  		insets = newInsets;
96  		firePropertyChange("arrowInsets", oldInsets, newInsets);
97  
98  		if (insets != oldInsets) {
99  			repaint();
100 		}
101 	}
102 
103 	/**
104 	 * Returns the insets of the arrow icon.
105 	 *
106 	 * @return Insets
107 	 */
108 	public Insets getArrowInsets()
109 	{
110 		return insets;
111 	}
112 
113 	/**
114 	 * This method has been overriden to implement arrow icon painting.
115 	 *
116 	 * @param g Graphics
117 	 */
118 	public void paintComponent(Graphics g)
119 	{
120 		((Graphics2D)g).addRenderingHints(PaintHelper.getAntialiasingHints());
121 		super.paintComponent(g);
122 
123 		int maxHeight = getBounds().height;
124 		int maxWidth = getBounds().width;
125 		int top;
126 		int bottom;
127 		int left;
128 		int right;
129 
130 		if (insets == null) {
131 			top = Math.min(maxWidth, maxHeight) / 4;
132 			bottom = Math.min(maxWidth, maxHeight) / 4;
133 			left = Math.min(maxWidth, maxHeight) / 4;
134 			right = Math.min(maxWidth, maxHeight) / 4;
135 		} else {
136 			top = insets.top;
137 			bottom = insets.bottom;
138 			left = insets.left;
139 			right = insets.right;
140 		}
141 
142 		g.setColor(getForeground());
143 
144 		if (orientation == DOWN) {
145 			int[] xs = { left, maxWidth / 2, maxWidth - right };
146 			int[] ys = { top, maxHeight - bottom, top };
147 			g.fillPolygon(xs, ys, 3);
148 		} else if (orientation == UP) {
149 			int[] xs = { left, maxWidth / 2, maxWidth - right };
150 			int[] ys = { maxHeight - bottom, top, maxHeight - bottom };
151 			g.fillPolygon(xs, ys, 3);
152 		} else if (orientation == RIGHT) {
153 			int[] ys = { top, maxHeight / 2, maxHeight - bottom };
154 			int[] xs = { left, maxWidth - right, left };
155 			g.fillPolygon(xs, ys, 3);
156 		} else if (orientation == LEFT) {
157 			int[] ys = { top, maxHeight / 2, maxHeight - bottom };
158 			int[] xs = { maxWidth - right, left, maxWidth - right };
159 			g.fillPolygon(xs, ys, 3);
160 		}
161 	}
162 
163 	/**
164 	 * Returns the orientation of the arrow icon.
165 	 *
166 	 * @return int
167 	 */
168 	public int getOrientation()
169 	{
170 		return orientation;
171 	}
172 
173 	/**
174 	 * Sets the orientation of the arrow icon.
175 	 *
176 	 * @param newOrientation int
177 	 */
178 	public void setOrientation(int newOrientation)
179 	{
180 		int oldOrientation = orientation;
181 		orientation = newOrientation;
182 		firePropertyChange("orientation", oldOrientation, newOrientation);
183 
184 		if (oldOrientation != newOrientation) {
185 			repaint();
186 		}
187 	}
188 
189 	/**
190 	 * Run test applet.
191 	 *
192 	 * @param args command line parameters
193 	 */
194     public static void main(String[] args)
195 	{
196 		JApplet applet = new JApplet() {
197 
198 			private static final long serialVersionUID = 1L;
199 
200 				public void init()
201 				{
202 					Container cp = this.getContentPane();
203 					JPanel panel = new JPanel();
204 					panel.setLayout(new GridLayout(1, 0));
205 					panel.add(new ArrowButton(ArrowButton.UP));
206 					panel.add(new ArrowButton(ArrowButton.DOWN));
207 					panel.add(new ArrowButton(ArrowButton.LEFT));
208 					panel.add(new ArrowButton(ArrowButton.RIGHT));
209 					cp.add(panel);
210 				}
211 			};
212 
213 		JFrame frame = new JFrame("Arrow Button Demo");
214 		frame.getContentPane().add(applet);
215 		frame.setSize(300, 400);
216 		frame.addWindowListener(new WindowAdapter() {
217 				public void windowClosing(WindowEvent e)
218 				{
219 					System.exit(0);
220 				}
221 			});
222 		applet.init();
223 		applet.start();
224 		frame.setVisible(true);
225 	}
226 }
227 
228 /* __oOo__ */