SwingUtilities.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.geotools.resources |
![]() |
![]() |
MrPostman |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* -*- mode: java; c-basic-indent: 4; indent-tabs-mode: nil -*-
* :indentSize=4:noTabs=true:tabSize=4:indentOnTab=true:indentOnEnter=true:mode=java:
* ex: set tabstop=4 expandtab:
*
* MrPostman - webmail <-> email gateway
* Copyright (C) 2002-2003 MrPostman Development Group
* Projectpage: http://mrbook.org/mrpostman/
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* In particular, this implies that users are responsible for
* using MrPostman after reading the terms and conditions given
* by their web-mail provider.
*
* You should have received a copy of the GNU General Public License
* Named LICENSE in the base directory of this distribution,
* if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
* Geotools - OpenSource mapping toolkit
* (C) 2002, Centre for Computational Geography
* (C) 2001, Institut de Recherche pour le DÈveloppement
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
* Contacts:
* UNITED KINGDOM: James Macgill
* mailto:j.macgill@geog.leeds.ac.uk
*
* FRANCE: Surveillance de l'Environnement AssistÈe par Satellite
* Institut de Recherche pour le DÈveloppement / US-Espace
* mailto:seasnet@teledetection.fr
*
* CANADA: Observatoire du Saint-Laurent
* Institut Maurice-Lamontagne
* mailto:osl@osl.gc.ca
*/
package org.geotools.resources;
import org.geotools.resources.gui.ResourceKeys;
// Geotools dependencies
import org.geotools.resources.gui.Resources;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.event.ActionListener;
import java.awt.event.WindowListener;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.LookAndFeel;
/**
* A collection of utility methods for Swing. All <code>show*</code> methods delegate
* their work to the corresponding method in {@link JOptionPane}, with two differences:
*
* <ul>
* <li><code>SwingUtilities</code>'s method may be invoked from any thread. If they
* are invoked from a non-Swing thread, execution will be delegate to the Swing
* thread and the calling thread will block until completion.</li>
* <li>If a parent component is a {@link JDesktopPane}, dialogs will be rendered as
* internal frames instead of frames.</li>
* </ul>
*
* @version $Id: SwingUtilities.java,v 1.5 2003/02/09 23:38:12 lbruand Exp $
* @author Martin Desruisseaux
*/
public final class SwingUtilities {
public static final String CVSID = "$Id: SwingUtilities.java,v 1.5 2003/02/09 23:38:12 lbruand Exp $";
/**
* Do not allow any instance
* of this class to be created.
*/
private SwingUtilities() {
}
/**
* Insert a Swing component into a frame. The kind of frame depends on the owner:
*
* <ul>
* <li>If <code>owner</code> or one of its parent is a {@link JDesktopPane},
* then <code>panel</code> is added into a {@link JInternalFrame}.</li>
* <li>If <code>owner</code> or one of its parent is a {@link Frame} or a {@link Dialog},
* then <code>panel</code> is added into a {@link JDialog}.</li>
* <li>Otherwise, <code>panel</code> is added into a {@link JFrame}.</li>
* </ul>
*
* @param owner The frame's owner, or <code>null</code> if none.
* @param panel The panel to insert into a frame.
* @param title The frame's title.
* @param listener A listener to receives frame events. If non-null, then this listener will
* be registered to whatever kind of frame this method will constructs. In the special
* case where this method constructs an {@linkplain JInternalFrame internal frame} and
* the <code>listener</code> is not an instance of {@link InternalFrameListener}, then
* this method will wrap the <code>listener</code> into an
* <code>InternalFrameListener</code>.
* @return The frame. This frame is not initially visible. The method
* <code>Component.setVisible(true)</code> must be invoked
* in order to show the frame.
*/
public static Component toFrame(Component owner, final JComponent panel, final String title,
final WindowListener listener) {
while (owner != null) {
if (owner == panel) {
throw new IllegalArgumentException();
}
if (owner instanceof JDesktopPane) {
final JInternalFrame frame = new JInternalFrame(title, true, true, true, true);
frame.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
frame.addInternalFrameListener(InternalWindowListener.wrap(listener));
((JDesktopPane) owner).add(frame);
frame.getContentPane().add(panel);
frame.pack();
return frame;
}
if (owner instanceof Frame) {
final JDialog dialog = new JDialog((Frame) owner, title);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.addWindowListener(listener);
dialog.getContentPane().add(panel);
dialog.pack();
return dialog;
}
if (owner instanceof Dialog) {
final JDialog dialog = new JDialog((Dialog) owner, title);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.addWindowListener(listener);
dialog.getContentPane().add(panel);
dialog.pack();
return dialog;
}
owner = owner.getParent();
}
//
// Add the panel as a standalone window.
// This window has its own button on the task bar.
//
final JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.addWindowListener(listener);
frame.getContentPane().add(panel);
frame.pack();
return frame;
}
/**
* Brings up a "Ok/Cancel" dialog with no icon. This method can be invoked
* from any thread and blocks until the user click on "Ok" or "Cancel".
*
* @param owner The parent component. Dialog will apears on top of this owner.
* @param dialog The dialog content to show.
* @param title The title string for the dialog.
* @return <code>true</code> if user clicked "Ok", <code>false</code> otherwise.
*/
public static boolean showOptionDialog(final Component owner, final Object dialog, final String title) {
return showOptionDialog(owner, dialog, title, null);
}
/**
* Brings up a "Ok/Cancel/Reset" dialog with no icon. This method can be invoked
* from any thread and blocks until the user click on "Ok" or "Cancel".
*
* @param owner The parent component. Dialog will apears on top of this owner.
* @param dialog The dialog content to show.
* @param title The title string for the dialog.
* @param reset Action to execute when user press "Reset", or <code>null</code>
* if there is no "Reset" button. If <code>reset</code> is an
* instance of {@link Action}, the button label will be set
* according the action's properties.
* @return <code>true</code> if user clicked "Ok", <code>false</code> otherwise.
*/
public static boolean showOptionDialog(final Component owner, final Object dialog, final String title,
final ActionListener reset) {
// Delegate to Swing thread if this method
// is invoked from an other thread.
if (!EventQueue.isDispatchThread()) {
final boolean[] result = new boolean[1];
invokeAndWait(new Runnable() {
public void run() {
result[0] = showOptionDialog(owner, dialog, title, reset);
}
});
return result[0];
}
// Construct the buttons bar.
Object[] options = null;
Object initialValue = null;
int okChoice = JOptionPane.OK_OPTION;
if (reset != null) {
final Resources resources = Resources.getResources((owner != null) ? owner.getLocale() : null);
final JButton button;
if (reset instanceof Action) {
button = new JButton((Action) reset);
} else {
button = new JButton(resources.getString(ResourceKeys.RESET));
button.addActionListener(reset);
}
options = new Object[] {
resources.getString(ResourceKeys.OK), resources.getString(ResourceKeys.CANCEL), button
};
initialValue = options[okChoice = 0];
}
// Bring ups the dialog box.
final int choice;
if (JOptionPane.getDesktopPaneForComponent(owner) != null) {
choice = JOptionPane.showInternalOptionDialog(owner, // Composante parente
dialog, // Message
title, // Titre de la boÓte de dialogue
JOptionPane.OK_CANCEL_OPTION, // Boutons ‡ placer
JOptionPane.PLAIN_MESSAGE, // Type du message
null, // Icone
options, // Liste des boutons
initialValue); // Bouton par dÈfaut
} else {
choice = JOptionPane.showOptionDialog(owner, // Composante parente
dialog, // Message
title, // Titre de la boÓte de dialogue
JOptionPane.OK_CANCEL_OPTION, // Boutons ‡ placer
JOptionPane.PLAIN_MESSAGE, // Type du message
null, // Icone
options, // Liste des boutons
initialValue); // Bouton par dÈfaut
}
return choice == okChoice;
}
/**
* Brings up a message dialog with a "Ok" button. This method can be invoked
* from any thread and blocks until the user click on "Ok".
*
* @param owner The parent component. Dialog will apears on top of this owner.
* @param message The dialog content to show.
* @param title The title string for the dialog.
* @param type The message type
* ({@link JOptionPane#ERROR_MESSAGE},
* {@link JOptionPane#INFORMATION_MESSAGE},
* {@link JOptionPane#WARNING_MESSAGE},
* {@link JOptionPane#QUESTION_MESSAGE} or
* {@link JOptionPane#PLAIN_MESSAGE}).
*/
public static void showMessageDialog(final Component owner, final Object message, final String title, final int type) {
if (!EventQueue.isDispatchThread()) {
invokeAndWait(new Runnable() {
public void run() {
showMessageDialog(owner, message, title, type);
}
});
return;
}
if (JOptionPane.getDesktopPaneForComponent(owner) != null) {
JOptionPane.showInternalMessageDialog(owner, // Composante parente
message, // Message
title, // Titre de la boÓte de dialogue
type); // Type du message
} else {
JOptionPane.showMessageDialog(owner, // Composante parente
message, // Message
title, // Titre de la boÓte de dialogue
type); // Type du message
}
}
/**
* Brings up a confirmation dialog with "Yes/No" buttons. This method can be
* invoked from any thread and blocks until the user click on "Yes" or "No".
*
* @param owner The parent component. Dialog will apears on top of this owner.
* @param message The dialog content to show.
* @param title The title string for the dialog.
* @param type The message type
* ({@link JOptionPane#ERROR_MESSAGE},
* {@link JOptionPane#INFORMATION_MESSAGE},
* {@link JOptionPane#WARNING_MESSAGE},
* {@link JOptionPane#QUESTION_MESSAGE} or
* {@link JOptionPane#PLAIN_MESSAGE}).
* @return <code>true</code> if user clicked on "Yes", <code>false</code> otherwise.
*/
public static boolean showConfirmDialog(final Component owner, final Object message, final String title,
final int type) {
if (!EventQueue.isDispatchThread()) {
final boolean[] result = new boolean[1];
invokeAndWait(new Runnable() {
public void run() {
result[0] = showConfirmDialog(owner, message, title, type);
}
});
return result[0];
}
final int choice;
if (JOptionPane.getDesktopPaneForComponent(owner) != null) {
choice = JOptionPane.showInternalConfirmDialog(owner, // Composante parente
message, // Message
title, // Titre de la boÓte de dialogue
JOptionPane.YES_NO_OPTION, // Boutons ‡ faire apparaÓtre
type); // Type du message
} else {
choice = JOptionPane.showConfirmDialog(owner, // Composante parente
message, // Message
title, // Titre de la boÓte de dialogue
JOptionPane.YES_NO_OPTION, // Boutons ‡ faire apparaÓtre
type); // Type du message
}
return choice == JOptionPane.YES_OPTION;
}
/**
* Retourne une Ètiquette pour la composante spÈcifiÈe.
* Le texte de l'Ètiquette pourra Èventuellement Ítre
* distribuÈ sur plusieurs lignes.
*
* @param owner Composante pour laquelle on construit une Ètiquette.
* L'Ètiquette aura la mÍme largeur que <code>owner</code>.
* @param text Texte ‡ placer dans l'Ètiquette.
*/
public static JComponent getMultilineLabelFor(final JComponent owner, final String text) {
final JTextArea label = new JTextArea(text);
final Dimension size = owner.getPreferredSize();
size.height = label.getMaximumSize().height;
label.setMaximumSize(size);
label.setWrapStyleWord(true);
label.setLineWrap(true);
label.setEditable(false);
label.setFocusable(false);
label.setOpaque(false);
label.setBorder(null); // Certains L&F placent une bordure.
LookAndFeel.installColorsAndFont(label, "Label.background", "Label.foreground", "Label.font");
return label;
}
/**
* Causes runnable to have its run method called in the dispatch thread of
* the event queue. This will happen after all pending events are processed.
* The call blocks until this has happened.
*/
public static void invokeAndWait(final Runnable runnable) {
if (EventQueue.isDispatchThread()) {
runnable.run();
} else {
try {
EventQueue.invokeAndWait(runnable);
} catch (InterruptedException exception) {
// Someone don't want to let us sleep. Go back to work.
} catch (InvocationTargetException target) {
final Throwable exception = target.getTargetException();
if (exception instanceof RuntimeException) {
throw (RuntimeException) exception;
}
if (exception instanceof Error) {
throw (Error) exception;
}
// Should not happen, since {@link Runnable#run} do not allow checked exception.
throw new UndeclaredThrowableException(exception, exception.getLocalizedMessage());
}
}
}
}
The table below shows all metrics for SwingUtilities.java.




