Source code: junk/gui/box/JXBox.java
1 /*
2 * @(#)JXBox.java
3 * Copyright (C) 2002 Tay Hock Keong <tay_ivan@hotmail.com>
4 * This file is part of JD4X.
5 * JD4X is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
9 * JD4X is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with JD4X; see the file COPYING. If not, write to the Free
15 * Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16 */
17
18 package junk.gui.box;
19
20 import junk.images.JXLocateImage;
21 import junk.util.*;
22 import junk.gui.JXTheme;
23 import junk.gui.window.JXWindow;
24 import java.awt.event.ActionListener;
25 import java.awt.Font;
26 import java.awt.Color;
27 import java.awt.Container;
28 import java.awt.Dimension;
29 import java.awt.Rectangle;
30 import java.awt.Component;
31 import java.awt.BorderLayout;
32 import java.awt.event.ActionEvent;
33 import javax.swing.JLabel;
34 import javax.swing.JPanel;
35 import javax.swing.JWindow;
36 import javax.swing.JToggleButton;
37 import javax.swing.JPopupMenu;
38 import javax.swing.ImageIcon;
39 import javax.swing.border.EmptyBorder;
40
41 /**
42 * JXBox is a GUI Box used to host programs and components. Unlike a frame it
43 * cannot be closed but can be minimized. It should be used only for components
44 * that need to reside on the desktop visually throughout its desktop life time.
45 * It is mainly designed to hold the JXJvm.
46 * @see junk.gui.window.JXWindow
47 * @version 0.1, 06/08/2002
48 * @since JD4X 1.0
49 * @author Tay Hock Keong
50 */
51
52 public class JXBox{
53
54 /** Root plane of the JXBox */
55 private Container c;
56
57 /** Internally implemented by a JWindow */
58 private JXWindow jxwin = new JXWindow();
59
60 /** For synchronization od state of GUI */
61 private boolean toggle=true;
62
63 /** Title header of box */
64 protected JLabel header;
65
66 /** Box function buttons */
67 protected JToggleButton func;
68
69 /** Box default icons */
70 protected ImageIcon iconUp = null;
71
72 /** Box default icons */
73 protected ImageIcon iconDown = null;
74
75 /** Roll down action button */
76 protected JToggleButton roll;
77
78 /** Container of the box and user components */
79 protected JPanel container, ext, banner;
80
81 /** Box option menu */
82 protected JPopupMenu popupMenu;
83
84 /** Popup menu items for Box function options */
85 protected String[] options={"Dock","UnDock"};
86
87 /** Title header font size */
88 public static int headerFont = 9;
89
90 /** compoent settings */
91 private int oldHeight, oldWidth, oldX, oldY, iconSize;
92
93 /**
94 * Default constructor that initializes the neccessary utilities.
95 */
96 public JXBox(){
97 this(null);
98 init2();
99 }
100
101 /**
102 * Constructor with a title header for the box.
103 * @param title
104 * the title header for the box.
105 */
106 public JXBox(String title){
107 if(title == null){
108 header = new JLabel("New JXBox");
109 }
110 else{
111 header = new JLabel(title);
112 }
113 init();
114 }
115
116 /**
117 * Get the main container of the box.
118 * @return
119 * the container of the box where components are added.
120 **/
121 public JPanel getContentPane(){
122 return ext;
123 }
124
125 /**
126 * Get the minimum dimension of the box.
127 * @return
128 * the preferred size of the box.
129 */
130 public Dimension getMinimumSize(){
131 return getPreferredSize();
132 }
133
134 /**
135 * Get the preferred dimension of the box.
136 * @return
137 * the preferred size of the box, 260 x 52.
138 */
139 public Dimension getPreferredSize(){
140 return new Dimension(260, 52);
141 }
142
143 /**
144 * Get the top left horizontal corner of the box.
145 * @return
146 * the top left x position value of the box.
147 */
148 public int getX(){
149 return jxwin.getX();
150 }
151
152 /**
153 * Get the top left vertical corner of the box.
154 * @return
155 * the top left y position value of the box.
156 */
157 public int getY(){
158 return jxwin.getY();
159 }
160
161 /**
162 * Get the width of the box.
163 * @return
164 * the width value of the box.
165 */
166 public int getWidth(){
167 return jxwin.getWidth();
168 }
169
170 /**
171 * Get the height of the box.
172 * @return
173 * the height value of the box.
174 */
175 public int getHeight(){
176 return jxwin.getHeight();
177 }
178
179 /**
180 * Initializes all needed GUI components and their support classes.
181 */
182 protected void init(){
183 container = new JPanel(new BorderLayout());
184 ext = new JPanel(new BorderLayout());
185 iconUp = JXLocateImage.getImageAsIcon(JXTheme.theme+JXTheme.BOXICONS[0]);
186 iconDown = JXLocateImage.getImageAsIcon(JXTheme.theme+JXTheme.BOXICONS[1]);
187 func = new JToggleButton(JXLocateImage.getImageAsIcon(JXTheme.theme+JXTheme.BOXICONS[2]));
188 func.setPreferredSize(new Dimension(17,15));
189 func.setBorderPainted(false);
190 func.setFocusPainted(false);
191 func.setToolTipText("Options");
192 func.addActionListener(new buttonHandler());
193 container.setBorder(new EmptyBorder(5,5,5,5));
194 header.setFont(new Font(header.getFont().getFontName(), header.getFont().getStyle(), headerFont));
195 roll = new JToggleButton(iconDown);
196 roll.setPreferredSize(new Dimension(12,6));
197 roll.setFocusPainted(false);
198 roll.setBorderPainted(false);
199 roll.addActionListener(new rollAction());
200 roll.setToolTipText("Max/Min");
201 banner = new JPanel(new BorderLayout());
202 banner.add(func, BorderLayout.WEST);
203 banner.add(header, BorderLayout.CENTER);
204 banner.add(roll, BorderLayout.EAST);
205 container.add(banner, BorderLayout.NORTH);
206 container.add(ext, BorderLayout.CENTER);
207 c = jxwin.getContentPane();
208 c.add(container, BorderLayout.CENTER);
209 //setup the popup menu for the box options.
210 popupMenu = new JPopupMenu("JXBox Options");
211 JXConfigMenusParam mparam = new JXConfigMenusParam();
212 mparam.popupmenu = popupMenu;
213 mparam.options = options;
214 mparam.listener = new BoxMenuListener();
215 JXConfigMenus.configPopupMenu(mparam);
216 }
217
218 /**
219 * Initializes all needed GUI components and their support classes
220 * except a title header and the rolling function.
221 */
222 protected void init2(){
223 container = new JPanel(new BorderLayout());
224 ext = new JPanel(new BorderLayout());
225 container.setBorder(new EmptyBorder(5,5,5,5));
226 container.add(ext, BorderLayout.CENTER);
227 c = jxwin.getContentPane();
228 c.add(container, BorderLayout.CENTER);
229 func = new JToggleButton(JXLocateImage.getImageAsIcon(JXTheme.theme+JXTheme.BOXICONS[3]));
230 func.setBorderPainted(false);
231 func.setFocusPainted(false);
232 func.setPreferredSize(new Dimension(22,22));
233 func.addActionListener(new buttonHandler());
234 c.add(func, BorderLayout.WEST);
235 //setup the popup menu for the box options.
236 popupMenu = new JPopupMenu("JXBox Options");
237 JXConfigMenusParam mparam = new JXConfigMenusParam();
238 mparam.popupmenu = popupMenu;
239 mparam.options = options;
240 mparam.listener = new BoxMenuListener();
241 JXConfigMenus.configPopupMenu(mparam);
242 }
243
244 /**
245 * Set a new bounds for the box.
246 * @param x,y
247 * the vertical and horizontal position of the box.
248 * @param w,h
249 * the width and height of the box.
250 */
251 public void setBounds(int x, int y, int w, int h){
252 jxwin.setBounds(x, y, w, h);
253 }
254
255 /**
256 * Set a new title header for the box.
257 * @param title
258 * the title header for the box.
259 */
260 public void setHeader(String title){
261 if(header != null){
262 header.setText(title);
263 }
264 }
265
266 /**
267 * Set a new color for the header of the box.
268 * @param c
269 * the desired color.
270 */
271 public void setHeaderColor(Color c){
272 if(banner != null && roll != null && func != null){
273 banner.setBackground(c);
274 roll.setBackground(c);
275 func.setBackground(c);
276 }
277 }
278
279 /**
280 * Set a new color for the background of the box.
281 * @param c
282 * the desired color.
283 */
284 public void setBackground(Color c){
285 container.setBackground(c);
286 ext.setBackground(c);
287 }
288
289 /**
290 * Set the visibility of the box.
291 * @param v
292 * true=show box, else make box invisible.
293 */
294 public void setVisible(boolean v){
295 jxwin.setVisible(v);
296 }
297
298 /**
299 * Enable or disable the rolling action of the box.
300 * @param e
301 * true=Enable roll action, else disable.
302 */
303 public void setEnableRolling(boolean e){
304 if(roll != null){
305 roll.setEnabled(e);
306 }
307 }
308
309 /**
310 * Set the default icon and color theme of the box.
311 */
312 public void updateTheme(){
313 if(func != null && roll != null){
314 func.invalidate();
315 roll.invalidate();
316 setHeaderColor(JXTheme.getForegroundColorTheme());
317 iconUp = JXLocateImage.getImageAsIcon(JXTheme.theme+JXTheme.BOXICONS[0]);
318 iconDown = JXLocateImage.getImageAsIcon(JXTheme.theme+JXTheme.BOXICONS[1]);
319 func.setIcon(JXLocateImage.getImageAsIcon(JXTheme.theme+JXTheme.BOXICONS[2]));
320 if(toggle){
321 roll.setIcon(iconUp);
322 }
323 else{
324 roll.setIcon(iconDown);
325 }
326 func.validate();
327 roll.validate();
328 }
329 jxwin.updateTheme();
330 }
331
332 /**
333 * Roll down the box to its title header.
334 */
335 private class rollAction implements ActionListener{
336
337 public rollAction(){
338 Dimension d = roll.getPreferredSize();
339 iconSize = ((int)d.getHeight())+19;
340 }
341
342 public void actionPerformed(ActionEvent jvmAddEvent){
343 if(toggle){
344 Rectangle r = jxwin.getBounds();
345 roll.setIcon(iconUp);
346 oldHeight = (int)r.getHeight();
347 oldWidth = (int)r.getWidth();
348 oldY = (int)r.getY();
349 oldX = (int)r.getX();
350 if(JXScreenManager.getMidScreenHeight() < oldY){
351 jxwin.setBounds(oldX, oldY+oldHeight-iconSize, oldWidth, iconSize);
352 }
353 else{
354 jxwin.setBounds(oldX, oldY, oldWidth, iconSize);
355 }
356 toggle=false;
357 }
358 else{
359 roll.setIcon(iconDown);
360 jxwin.setBounds(oldX, oldY, oldWidth, oldHeight);
361 toggle=true;
362 }
363 jxwin.invalidate();
364 jxwin.validate();
365 roll.setSelected(false);
366 }
367 }
368
369 /**
370 * Detect and take action according to the selected option menu choice.
371 * Currently does nothing.
372 */
373 private class BoxMenuListener implements ActionListener{
374 //defines the options under right button desktop menu.
375 public void actionPerformed(ActionEvent popMenuEvent){
376 String choice = popMenuEvent.getActionCommand();
377 if(choice == options[0]){
378 jxwin.removeMouseMotionListener(jxwin);
379 }
380 else if(choice == options[1]){
381 jxwin.addMouseMotionListener(jxwin);
382 }
383 popupMenu.setVisible(false);
384 }
385 }
386
387 /**
388 * Provides the default behaviour of the box through its function
389 * buttons, this behaviour can be over written by a window manger.
390 * Not used for Java L&F.
391 */
392 private class buttonHandler implements ActionListener{
393 public void actionPerformed(ActionEvent bae){
394 JToggleButton but = (JToggleButton)bae.getSource();
395 if(but == func){
396 popupMenu.show((Component)but, but.getX(), but.getY());
397 but.setSelected(false);
398 }
399 }
400 }
401
402 /**
403 * Default invokation method to test JXBox class.
404 * @param arg
405 * All arguments are ignored.
406 */
407 public static void main(String[] args){
408 JXBox box = new JXBox("Testing");
409 JPanel p = box.getContentPane();
410 p.add(new JLabel("<HTML>Hello <font size=5>Sexy<"+"/"+"font>"+" Box!"), BorderLayout.CENTER);
411 p.add(new JLabel("Did you see that Box?"), BorderLayout.NORTH);
412 box.setBounds(100, 100, 200, 200);
413 box.setVisible(true);
414 }
415 }