View Javadoc

1   /*
2    * @(#)$Id: SwingTool.java 689 2009-07-22 00:10:27Z bsigner $
3    *
4    * Author		:	Ueli Kurmann, igesture@uelikurmann.ch
5    *
6    * Purpose		:   Static helper methods used by the swing application.
7    *
8    * -----------------------------------------------------------------------
9    *
10   * Revision Information:
11   *
12   * Date				Who			Reason
13   *
14   * Nov 15, 2006     ukurmann    Initial Release
15   * Mar 24, 2007     bsigner     Cleanup
16   *
17   * -----------------------------------------------------------------------
18   *
19   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
20   *
21   * This software is the proprietary information of ETH Zurich.
22   * Use is subject to license terms.
23   * 
24   */
25  
26  
27  package org.ximtec.igesture.graphics;
28  
29  import java.awt.Dimension;
30  import java.awt.FlowLayout;
31  import java.awt.GridBagConstraints;
32  import java.awt.Insets;
33  
34  import javax.swing.Action;
35  import javax.swing.ImageIcon;
36  import javax.swing.JMenu;
37  import javax.swing.JMenuItem;
38  import javax.swing.JPopupMenu;
39  import javax.swing.ListModel;
40  
41  import org.sigtec.graphix.GuiBundle;
42  import org.sigtec.graphix.widget.BasicDialog;
43  import org.sigtec.graphix.widget.BasicInternalFrame;
44  
45  
46  /**
47   * Static helper methods used by the swing application.
48   * 
49   * @version 1.0 Nov 2006
50   * @author Ueli Kurmann, igesture@uelikurmann.ch
51   * @author Beat Signer, signer@inf.ethz.ch
52   */
53  public class SwingTool {
54  
55     public static final Dimension DIALOG_SIZE = new java.awt.Dimension(300, 300);
56  
57     public static final Dimension FRAME_SIZE = new java.awt.Dimension(240, 320);
58  
59     public static final Dimension FRAME_PREFERRED_SIZE = new java.awt.Dimension(
60           240, 320);
61  
62  
63     /**
64      * Creates a new GridBagConstraints instance.
65      * 
66      * @param gridX the x position.
67      * @param gridY the y position.
68      * @param width number of columns to span.
69      * @param height number of rows to span.
70      * @return the newly created GridBagConstraints instance.
71      */
72     public static GridBagConstraints createGridBagConstraint(int gridX,
73           int gridY, int width, int height, int alignment) {
74        return new GridBagConstraints(gridX, gridY, width, height, 0, 0,
75              alignment, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0);
76     } // createGridBagConstraint
77  
78  
79     public static GridBagConstraints createGridBagConstraint(int gridX,
80           int gridY, int width, int height) {
81        return createGridBagConstraint(gridX, gridY, width, height,
82              GridBagConstraints.CENTER);
83     } // createGridBagConstraint
84  
85  
86     /**
87      * Creates a new GridBagConstraints instance.
88      * 
89      * @param gridX the x position.
90      * @param gridY the y position.
91      * @return the newly created GridBagConstraints instance.
92      */
93     public static GridBagConstraints createGridBagConstraint(int gridX, int gridY) {
94        return createGridBagConstraint(gridX, gridY, 1, 1);
95     } // createGridBagConstraint
96  
97  
98     /**
99      * Creates a new dialog with some default settings.
100     * 
101     * @param key the key of the dialogue to be created.
102     * @return dialogue configured with data from the GUI bundle.
103     */
104    public static BasicDialog createDialog(String key, GuiBundle guiBundle) {
105       final BasicDialog dialog = new BasicDialog(key, guiBundle);
106       dialog.setSize(DIALOG_SIZE);
107       return dialog;
108    } // createDialog
109 
110 
111    /**
112     * Creates a new internal frame with some default settings.
113     * 
114     * @param key the key of the internal frame to be created.
115     * @return internal frame configured with data from the GUI bundle.
116     */
117    public static BasicInternalFrame createInternalFrame(String key,
118          GuiBundle guiBundle) {
119       final BasicInternalFrame frame = new BasicInternalFrame(key, guiBundle);
120       initFrame(frame);
121       return frame;
122    } // createFrame
123 
124 
125    /**
126     * Initialises a frame..
127     * @param frame the frame to be initialised.
128     */
129    public static void initFrame(BasicInternalFrame frame) {
130       frame.setSize(FRAME_SIZE);
131       frame.setLayout(new FlowLayout(FlowLayout.CENTER));
132       frame.setResizable(true);
133       frame.setVisible(true);
134       frame.setPreferredSize(new java.awt.Dimension(FRAME_PREFERRED_SIZE));
135       frame.setClosable(true);
136       frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
137    } // initFrame
138 
139 
140    /**
141     * Creates a new menu item with the given action.
142     * @param action the action to be used.
143     * @param icon the icon the icon to be used.
144     * @return a newly created menu item.
145     */
146    public static JMenuItem createMenuItem(Action action, ImageIcon icon) {
147       final JMenuItem item = new JMenuItem();
148       item.setAction(action);
149       item.setIcon(icon);
150       return item;
151    } // createMenuItem
152 
153 
154    /**
155     * Creates a JMenu
156     * @param name the name of the menu.
157     * @param actions an array of actions.
158     * @return the newly created JMenu with the corresponding items.
159     */
160    public static JMenu createMenu(String name, Action[] actions) {
161       final JMenu menu = new JMenu(name);
162 
163       if (actions != null) {
164 
165          for (final Action action : actions) {
166             menu.add(new JMenuItem(action));
167          }
168 
169       }
170 
171       return menu;
172    } // createMenu
173 
174 
175    /**
176     * Creates a pop-up menu.
177     * @param actions an array of actions.
178     * @return the newly created pop-up menu.
179     */
180    public static JPopupMenu createPopupMenu(Action[] actions) {
181       final JPopupMenu menu = new JPopupMenu();
182 
183       for (final Action action : actions) {
184          menu.add(new JMenuItem(action));
185       }
186 
187       return menu;
188    } // createPopupMenu
189 
190 
191    /**
192     * Returns a scrollable list.
193     * @param model the list model.
194     * @param height the height of the component.
195     * @param width the width of the component.
196     * @return a new scrollable list.
197     */
198    public static ScrollableList createScrollableList(ListModel model,
199          int height, int width) {
200       return new ScrollableList(model, height, width);
201    } // createScrollableList
202 
203 }