QJCC homepage

biz.chitec.quarterback.swing
Class QSwingUtilities

java.lang.Object
  extended bybiz.chitec.quarterback.swing.QSwingUtilities

public class QSwingUtilities
extends java.lang.Object

Collection of helper methods for certain more complex Swing/GUI-based tasks.

Version:
$Id: b7a101be64bc20a69679e60f87323950145807d5 $
Author:
cantamen/Dirk Hillbrecht 2003-2007. Distributed under the terms of the GNU LGPL.

Nested Class Summary
private static class QSwingUtilities.StandardStreamLogger
          Internal helper class for log frame implementation.
 
Field Summary
private static java.io.PrintStream oldstderr
           
private static java.io.PrintStream oldstdout
           
private static java.util.ResourceBundle rb
           
 
Constructor Summary
private QSwingUtilities()
          Fully-static object.
 
Method Summary
static void addLabelAndElementToPanel(java.awt.Container p, java.util.ResourceBundle rb, java.lang.String labelname, java.awt.Component elem, java.lang.Object constraint1, java.lang.Object constraint2)
          Adds an element together with a referring label into a container.
static void addLabelAndElementToPanel(java.awt.Container p, java.util.ResourceBundle rb, java.lang.String labelname, java.awt.Component elem, java.lang.Object constraint1, java.lang.Object constraint2, boolean linklabel)
          Adds an element together with a label into a container.
static void addLabelAndElementToPanel(java.awt.Container p, java.lang.String labelname, java.awt.Component elem, java.lang.Object constraint1, java.lang.Object constraint2)
          Adds an element together with a referring label into a container.
static void addShownCA(java.awt.Component outerc, java.awt.Component innerc)
          Adds a ComponentListener to outerc so that innerc is hidden and shownin sync with outerc.
static void adjustFrameOf(java.awt.Component c, java.awt.Dimension tobeadded, boolean hard)
          Ensures that the frame containing c is at least big enough to contain all children.
static void adjustTabbedPaneSelection(javax.swing.JTabbedPane jtp)
          Checks and realigns the currently selected tab of a tabbed pane against the enabled states of the tabs.
static void assertEventDispatchingThread()
          Tests whether called in event dispatching thread.
static void centerInternalFrame(javax.swing.JInternalFrame jif)
          Centers a JInternalFrame within its JDesktopPane.
static javax.swing.Box createDefaultHorizontalBox(int defaultstrut, int horizontalmargins, java.awt.Component[] components)
          Returns a horizontal box containing all given components centered and with the same distance.
static void disposeFrameOf(java.awt.Component c)
          Disposes the Window/Frame, JFrame, or JInternalFrame object which contains the given Component
static javax.swing.JFrame frameFor(java.awt.Container c)
          Puts the given Component in an unlabelled JFrame.
static javax.swing.JFrame frameFor(java.awt.Container c, java.lang.String name)
          Puts the given Container into a JFrame and installs a pass-through for component-shown events.
static java.awt.Component getButtonForLabel(java.awt.Container container, java.lang.String labeltext)
          Returns the first button with the given text.
static java.awt.Component getComponentForLabel(java.awt.Container container, java.lang.String labeltext)
          Returns the first component which is connected to a label with the given text.
static java.awt.Component getComponentForTab(java.awt.Container container, java.lang.String labeltext)
          Returns the first component which is in a JTabbedPane under a tab with the given label.
static java.awt.Container getFrameOf(java.awt.Component c)
          Return the frame object containing the given component.
static java.lang.String getHelpID(java.awt.Component c)
          Returns the next available help id starting from the given component (usually the one with the focus).
static java.awt.Frame getOutermostFrameOf(java.awt.Component c)
          Return "real" frame, even for components put into internal frames.
private static java.util.ResourceBundle getRB()
          Helper method for loading the internally used resource bundle object
static javax.swing.JInternalFrame internalFrameFor(java.awt.Container c)
          Puts the given Component in an unlabelled JInternalFrame.
static javax.swing.JInternalFrame internalFrameFor(java.awt.Container c, java.util.ResourceBundle rb, java.lang.String resname)
          Puts the given Container into a JInternalFrame and installs a pass-through for component-shown events.
static javax.swing.JInternalFrame internalFrameFor(java.awt.Container c, java.lang.String name)
          Deprecated. You should use the ResourceBundle'd version of this method
static void loadSizeFromProperty(java.awt.Component theframe, java.util.Properties props, java.lang.String key, java.awt.Rectangle alternative)
          Load the shape of a component (mainly a frame or an internal frame) from a properties file.
static void openStandardLogFrame()
          Opens an independant JFrame showing all output from standard output and standard error streams.
static void showCenteredPopupMenu(java.awt.Component invoker, javax.swing.JPopupMenu popupmenu)
          Places a popup menu centered to its invoker.
static void showTabForLabel(java.awt.Container container, java.lang.String labeltext)
          Shows the first tab in the first JTabbedPane which has the given label text.
static void showWindow(java.awt.Window win)
          Places a window centered to its owner and shows it on screen.
static void storeSizeToProperty(java.awt.Component theframe, java.util.Properties props, java.lang.String key)
          Store the shape of a component (mainly a frame or an internal frame) into a properties file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

oldstdout

private static java.io.PrintStream oldstdout

oldstderr

private static java.io.PrintStream oldstderr

rb

private static java.util.ResourceBundle rb
Constructor Detail

QSwingUtilities

private QSwingUtilities()
Fully-static object.

Method Detail

getRB

private static java.util.ResourceBundle getRB()
Helper method for loading the internally used resource bundle object


getFrameOf

public static java.awt.Container getFrameOf(java.awt.Component c)
Return the frame object containing the given component. Passes the widget tree upward until it finds a java.awt.Window (which covers also its subclass java.awt.Frame) or a javax.swing.JInternalFrame. Returns that one. Returns null if no frame-like parent widget is found.

Parameters:
c - A widget component of the GUI
Returns:
The top-level frame containing the given component

getOutermostFrameOf

public static java.awt.Frame getOutermostFrameOf(java.awt.Component c)
Return "real" frame, even for components put into internal frames. Goes the widget tree upwards from the given component on to find the enclosing JFrame or Frame object. If a JInternalFrame is on this way, jump to the according JDesktopPane and continue there. If the component is put into a Window (not Frame), return null.

Parameters:
c - A widget component of the GUI
Returns:
The Frame (or JFrame) containing the component

adjustFrameOf

public static void adjustFrameOf(java.awt.Component c,
                                 java.awt.Dimension tobeadded,
                                 boolean hard)
Ensures that the frame containing c is at least big enough to contain all children. Searches for the father frame of the given Component. Searches the widget tree upward until it comes to a Window or JInternalFrame. If tobeadded is given, adds tobeadded to current size of that one. If hard is true, always pack()s the top-level frame containing the given Component. Otherwise, any dimension larger than the required minimum is left as it is.

Parameters:
c - A component in the frame to be resized
tobeadded - Additional space to be added or removed before size check
hard - If true, just pack()

disposeFrameOf

public static void disposeFrameOf(java.awt.Component c)
Disposes the Window/Frame, JFrame, or JInternalFrame object which contains the given Component


addShownCA

public static void addShownCA(java.awt.Component outerc,
                              java.awt.Component innerc)
Adds a ComponentListener to outerc so that innerc is hidden and shownin sync with outerc.


frameFor

public static javax.swing.JFrame frameFor(java.awt.Container c,
                                          java.lang.String name)
Puts the given Container into a JFrame and installs a pass-through for component-shown events. The JFrame is created and the given Container is set as content pane. The JFrame is neither pack()ed nor show()n.

Parameters:
c - The Container to be put into the JFrame
name - JFrame's title
Returns:
The created JFrame object

frameFor

public static javax.swing.JFrame frameFor(java.awt.Container c)
Puts the given Component in an unlabelled JFrame.


internalFrameFor

public static javax.swing.JInternalFrame internalFrameFor(java.awt.Container c,
                                                          java.lang.String name)
Deprecated. You should use the ResourceBundle'd version of this method

Puts the given Container into a JInternalFrame and installs a pass-through for component-shown events. The JInternalFrame is created and the given Container is set as content pane. The JInternalFrame is neither pack()ed nor show()n.

Parameters:
c - The Container to be put into the JFrame
name - JFrame's title
Returns:
The created JFrame object

internalFrameFor

public static javax.swing.JInternalFrame internalFrameFor(java.awt.Container c,
                                                          java.util.ResourceBundle rb,
                                                          java.lang.String resname)
Puts the given Container into a JInternalFrame and installs a pass-through for component-shown events. The JInternalFrame is created and the given Container is set as content pane. The JInternalFrame is neither pack()ed nor show()n.

Parameters:
c - The Container to be put into the JFrame
rb - The ResourceBundle to read title and icon file name from
resname - The name of the resource with the title and the iconfile name
Returns:
The created JFrame object

internalFrameFor

public static javax.swing.JInternalFrame internalFrameFor(java.awt.Container c)
Puts the given Component in an unlabelled JInternalFrame.


adjustTabbedPaneSelection

public static void adjustTabbedPaneSelection(javax.swing.JTabbedPane jtp)
Checks and realigns the currently selected tab of a tabbed pane against the enabled states of the tabs.

If the currently selected tab is disabled, the next tab is selected. This is repeated until an enabled tab has been found or all tabs have been tried. In the later case, the original tab stays selected.

Parameters:
jtp - The JTabbedPane to adjust

createDefaultHorizontalBox

public static javax.swing.Box createDefaultHorizontalBox(int defaultstrut,
                                                         int horizontalmargins,
                                                         java.awt.Component[] components)
Returns a horizontal box containing all given components centered and with the same distance. Usually, the passed components are JButtons.

Parameters:
defaultstrut - Distance between the components in pixels
horizontalmargins - Free space above and below the components in pixels
components - Components to arrange in the Box
Returns:
Complete box, ready to insert into a larger layout.

addLabelAndElementToPanel

public static void addLabelAndElementToPanel(java.awt.Container p,
                                             java.util.ResourceBundle rb,
                                             java.lang.String labelname,
                                             java.awt.Component elem,
                                             java.lang.Object constraint1,
                                             java.lang.Object constraint2,
                                             boolean linklabel)
Adds an element together with a label into a container.

The label is added first, where the given ResourceBundle and String is taken to obtain the label's content. It is added with constraint1. Then, the element itself is added using constraint2. The label can be bound to the element to make it shortcuttable.

Parameters:
p - The Container to put everything in
rb - ResourceBundle with the label's content
labelname - Name of the label
elem - The element to put into the Container
constraint1 - Layout constraints for the label
constraint2 - Layout constraints for the element
linklabel - Flag whether the label should be linked to the element, important for hotkeying

addLabelAndElementToPanel

public static void addLabelAndElementToPanel(java.awt.Container p,
                                             java.util.ResourceBundle rb,
                                             java.lang.String labelname,
                                             java.awt.Component elem,
                                             java.lang.Object constraint1,
                                             java.lang.Object constraint2)
Adds an element together with a referring label into a container. The label is always added with link to the element.

Parameters:
p - The Container to put everything in
rb - ResourceBundle with the label's content
labelname - Name of the label
elem - The element to put into the Container
constraint1 - Layout constraints for the label
constraint2 - Layout constraints for the element

addLabelAndElementToPanel

public static void addLabelAndElementToPanel(java.awt.Container p,
                                             java.lang.String labelname,
                                             java.awt.Component elem,
                                             java.lang.Object constraint1,
                                             java.lang.Object constraint2)
Adds an element together with a referring label into a container.

The label is added first, it is assumed to be correctly i18n'd. It is added with constraint1. Then, the element itself is added using constraint2. The label is bound to the element, so a shortcut can be given.

Parameters:
p - The Container to put everything in
labelname - I18N-aware name of the label
elem - The element to put into the Container
constraint1 - Layout constraints for the label
constraint2 - Layout constraints for the element

showWindow

public static void showWindow(java.awt.Window win)
Places a window centered to its owner and shows it on screen. If the window has no owner, it is shown centered on screen.

This method is thread-safe. If it is not called from the event dispatching thread, it redispatched itself automatically into that thread. This means that, in this case, showing of the window on screen is done asynchronously to the caller.

Parameters:
win - The window to be shown.

showCenteredPopupMenu

public static void showCenteredPopupMenu(java.awt.Component invoker,
                                         javax.swing.JPopupMenu popupmenu)
Places a popup menu centered to its invoker. Used for cases where a popup menu is taken as a more global context menu, e.g. for window lists of desktop panes.

Parameters:
invoker - Invoker of the popup menu
popupmenu - The popup menu to show

getHelpID

public static java.lang.String getHelpID(java.awt.Component c)
Returns the next available help id starting from the given component (usually the one with the focus). The helpids are expected to be added in the same way as TOM does it. The method traverses the component tree upward until either a help id is found or the top-level window has been reached.

Parameters:
c - Component Any component to start search from
Returns:
First found help id or null if no help id could be found.

centerInternalFrame

public static void centerInternalFrame(javax.swing.JInternalFrame jif)
Centers a JInternalFrame within its JDesktopPane. The JInternalFrame has to be added to its JDesktopPane already. Relocation is done directly, not using the DesktopManager.

Parameters:
jif - The JInternalFrame

loadSizeFromProperty

public static void loadSizeFromProperty(java.awt.Component theframe,
                                        java.util.Properties props,
                                        java.lang.String key,
                                        java.awt.Rectangle alternative)
Load the shape of a component (mainly a frame or an internal frame) from a properties file.

Parameters:
theframe - Frame to set the shape of
props - Properties file to read the data from
key - Name under which the data is stored
alternative - Shape to set the frame to if no data can be found in the properties file

storeSizeToProperty

public static void storeSizeToProperty(java.awt.Component theframe,
                                       java.util.Properties props,
                                       java.lang.String key)
Store the shape of a component (mainly a frame or an internal frame) into a properties file.

Parameters:
theframe - Frame to store the shape from.
props - Properties file to store the shape information into.
key - Key under which the properties information is stored.

assertEventDispatchingThread

public static void assertEventDispatchingThread()
Tests whether called in event dispatching thread. Prints stack trace if not.


getComponentForLabel

public static java.awt.Component getComponentForLabel(java.awt.Container container,
                                                      java.lang.String labeltext)
Returns the first component which is connected to a label with the given text. The search recognizes java.awt.Label and javax.swing.JLabel. Search is done recursively through all subcontainers of the given container element.

If the component after the label is a non-empty Container itself (e.g. a JPanel), its first subcomponent is returned.

Parameters:
container - The container to search in
labeltext - Text on the label
Returns:
Component which comes directly after the label or null if the label is not found

getButtonForLabel

public static java.awt.Component getButtonForLabel(java.awt.Container container,
                                                   java.lang.String labeltext)
Returns the first button with the given text. The search recognizes java.awt.Button and javax.swing.JButton. Search is done recursively through all subcontainers of the given container element.

Parameters:
container - The container to search in
labeltext - Text on the Button
Returns:
Component which instance matches to a button and the given text or null if the button is not found

showTabForLabel

public static void showTabForLabel(java.awt.Container container,
                                   java.lang.String labeltext)
Shows the first tab in the first JTabbedPane which has the given label text. The given container is searched recursively for any JTabbedPanes. The JTabbedPanes are searched for the labels of their tabs. If a tab is found with the given labeltext, setSelectedIndex() is called on the JTabbedPane. If no fitting tab is found, nothing happens.

Parameters:
container - A container to search in for JTabbedPanes. Not the JTabbedPane itself!
labeltext - Text to find on a label.

getComponentForTab

public static java.awt.Component getComponentForTab(java.awt.Container container,
                                                    java.lang.String labeltext)
Returns the first component which is in a JTabbedPane under a tab with the given label. The given container is searched recursively for any JTabbedPanes. The JTabbedPanes are searched for the labels of their tabs. If a tab is found with the given labeltext, the according content is returned. If no fitting tab is found, null is returned.

If the component after the label is a non-empty Container itself (e.g. a JPanel), its first subcomponent is returned.

Parameters:
container - A container to search in for JTabbedPanes. Not the JTabbedPane itself!
labeltext - Text to find on a label.
Returns:
Component accessible by the tab with the given text or null of none has been found.

openStandardLogFrame

public static void openStandardLogFrame()
Opens an independant JFrame showing all output from standard output and standard error streams. Creates a new JFrame with a LogPanel widget. Reroutes standard output and standard error stream in a way that all output of them is shown in that LogPanel. If the JFrame is closed, original routing is restored. It is not possible to open more than one log frames.


QJCC homepage