DefaultDialog.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.gui.component |
![]() |
![]() |
XNap 3 |
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.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* 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.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xnap.gui.component;
import java.awt.*;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.*;
import java.awt.event.WindowAdapter;
import javax.help.BadIDException;
import javax.help.CSH;
import javax.help.HelpBroker;
import javax.help.HelpSet;
import javax.help.InvalidHelpSetContextException;
import javax.help.Map;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.border.EmptyBorder;
import org.apache.log4j.Logger;
import org.xnap.XNap;
import org.xnap.gui.action.*;
import org.xnap.gui.component.*;
import org.xnap.gui.util.GUIHelper;
import org.xnap.gui.util.HelpManager;
import org.xnap.util.SystemHelper;
/**
* This class provides a default implementation for a dialog. A dialog consists
* of two areas. The main area, located at the center of the dialog, contains
* the user interaction components. The button area, located at the south
* west of the dialog, is surrounded by an empty border and contains the
* buttons.
*
* <p>The most common buttons are provided with default actions.</p>
*
* <p>If you extend this class make sure <code>pack()</code> is called after
* all components have been added.</p>
*/
public class DefaultDialog extends JDialog
{
//--- Constant(s) ---
/**
* The button type for no buttons.
*/
public static int BUTTON_NONE = 0;
/**
* The button type for the okay button.
*/
public static int BUTTON_OKAY = 1;
/**
* The button type for the apply button.
*/
public static int BUTTON_APPLY = 2;
/**
* The button type for the cancel button.
*/
public static int BUTTON_CANCEL = 4;
/**
* The button type for the close button.
*/
public static int BUTTON_CLOSE = 8;
/**
* The button type for the help button.
*/
public static int BUTTON_HELP = 16;
/**
* The button type for the context help button.
*/
public static int BUTTON_CONTEXT_HELP = 32;
//--- Data field(s) ---
private static Logger logger = Logger.getLogger(DefaultDialog.class);
private ApplyAction acApply = new ApplyAction();
private CancelAction acCancel = new CancelAction();
private CloseAction acClose = new CloseAction();
private OkayAction acOkay = new OkayAction();
private HelpAction acHelp = new HelpAction();
private JPanel jpButtonsLeft;
private JPanel jpButtonsRight;
private JPanel jpMain;
private JPanel topPanel;
protected boolean isOkay = false;
//--- Constructor(s) ---
/**
* Constructs a dialog. The escape key is by default bound to the cancel
* button.
*
* @param buttons a logical and combination of button type constants
* @param component the main component that is centered in the dialog
* @param exitOnEnter execute the okay action when the user presses enter
*/
public DefaultDialog(int buttons, JComponent component,
boolean exitOnEnter)
{
// the top most panel
topPanel = new JPanel(new BorderLayout());
topPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
jpMain = new JPanel();
topPanel.add(jpMain, BorderLayout.CENTER);
if (component != null) {
setMainComponent(component);
}
jpButtonsLeft = new JPanel(new FlowLayout(FlowLayout.LEFT));
if ((buttons & BUTTON_HELP) > 0) {
jpButtonsLeft.add(new JButton(acHelp));
}
if ((buttons & BUTTON_CONTEXT_HELP) > 0) {
jpButtonsLeft.add(new XNapButton(new ContextHelpAction()));
}
jpButtonsRight = new JPanel(new FlowLayout(FlowLayout.RIGHT));
if ((buttons & BUTTON_OKAY) > 0) {
JButton jbOkay = new JButton(acOkay);
if (exitOnEnter) {
GUIHelper.bindEnterKey(topPanel, jbOkay.getAction());
}
jpButtonsRight.add(jbOkay);
}
if ((buttons & BUTTON_CLOSE) > 0) {
jpButtonsRight.add(new JButton(acClose));
}
if ((buttons & BUTTON_APPLY) > 0) {
jpButtonsRight.add(new JButton(acApply));
}
if ((buttons & BUTTON_CANCEL) > 0) {
JButton jbCancel = new JButton(acCancel);
GUIHelper.bindEscapeKey(topPanel, jbCancel.getAction());
jpButtonsRight.add(jbCancel);
}
/* the aqua look and feel adds a small resize control in the lower
right corner which overlaps the status panel therefore we add a
little offset. */
if (SystemHelper.isMacOSX) {
jpButtonsRight.add(Box.createHorizontalStrut(15));
}
// separator line
JSeparator js = new JSeparator();
JPanel jpButtons = new JPanel(new BorderLayout());
jpButtons.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));
jpButtons.add(js, BorderLayout.NORTH);
jpButtons.add(jpButtonsLeft, BorderLayout.WEST);
jpButtons.add(jpButtonsRight, BorderLayout.EAST);
topPanel.add(jpButtons, BorderLayout.SOUTH);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(topPanel, BorderLayout.CENTER);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new CloseListener());
if (component != null) {
pack();
}
}
public DefaultDialog(int buttons, JComponent component)
{
this(buttons, component, true);
}
public DefaultDialog(int buttons, boolean exitOnEnter)
{
this(buttons, null, exitOnEnter);
}
public DefaultDialog(int buttons)
{
this(buttons, null);
}
/**
* Constructs a dialog with an okay and cancel button.
*/
public DefaultDialog()
{
this(BUTTON_OKAY | BUTTON_CANCEL);
}
//--- Method(s) ---
/**
* Called by ApplyAction and OkayAction when the dialog is closed.
*
* @return true, if apply was successful and close() should be
* invoked; false, otherwise
* @exception IllegalArgumentException
*/
public boolean apply()
{
return true;
}
/**
* Disposes the dialog. Called by the actions to close the dialog. Sub
* classes can reimplement this.
*
* @see #isOkay()
*/
public void close()
{
dispose();
}
/**
* Returns the button panel. Sub classes can add aditional buttons to this
* panel.
*/
public JPanel getButtonPanel()
{
return jpButtonsRight;
}
/**
* Returns the cancel action.
*/
public Action getCancelAction()
{
return acCancel;
}
/**
* Returns the cancel action.
*/
public Action getOkayAction()
{
return acOkay;
}
/**
* Returns the close action.
*/
public Action getCloseAction()
{
return acClose;
}
/**
* Sets the main component to <code>component</code>.
*/
public void setMainComponent(Component component)
{
getMainPanel().setLayout(new BorderLayout());
getMainPanel().add(component, BorderLayout.CENTER);
}
/**
* Returns the main panel. Sub classes can add components to this panel.
*/
public JPanel getMainPanel()
{
return jpMain;
}
/**
* Returns the top most panel.
*/
public JPanel getTopPanel()
{
return topPanel;
}
/**
* Returns true, if the dialog was closed with the Okay button.
*/
public boolean isOkay()
{
return isOkay;
}
/**
* Sets this dialogs position relative to <code>c</code> and makes it
* visible.
*/
public void show(Component c)
{
if (!isVisible()) {
if (c != null) {
setLocationRelativeTo(c);
}
setVisible(true);
}
else {
toFront();
}
}
/**
* Handles the apply button. Invokes <code>apply()</code> when pressed.
*
* @see #apply()
*/
private class ApplyAction extends AbstractAction {
public ApplyAction()
{
putValue(Action.NAME, XNap.tr("Apply"));
putValue(Action.SHORT_DESCRIPTION, XNap.tr("Applies changes."));
putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_A));
}
public void actionPerformed(ActionEvent event)
{
try {
apply();
}
catch (IllegalArgumentException e) {
JOptionPane.showMessageDialog
(DefaultDialog.this, e.getMessage(),
XNap.tr("Illegal Value"), JOptionPane.ERROR_MESSAGE);
}
}
}
/**
* Handles the Cancel button. Invokes <code>close()</code> when pressed.
*
* @see #close()
*/
private class CancelAction extends AbstractAction {
public CancelAction()
{
putValue(Action.NAME, XNap.tr("Cancel"));
putValue(Action.SHORT_DESCRIPTION,
XNap.tr("Closes the dialog without saving changes."));
putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_C));
}
public void actionPerformed(ActionEvent event)
{
isOkay = false;
close();
}
}
/**
* Handle the Close button. Invokes <code>close()</code> when pressed.
*
* @see #close()
*/
private class CloseAction extends AbstractAction {
public CloseAction()
{
putValue(Action.NAME, XNap.tr("Close"));
putValue(Action.SHORT_DESCRIPTION, XNap.tr("Closes the dialog."));
putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_S));
}
public void actionPerformed(ActionEvent event)
{
isOkay = false;
close();
}
}
/**
* Handles the Help button. Shows the help dialog registered for the
* <code>rootPane</code> of this dialog.
*
* It suffices to register a help id and a helpset for the dialog using
* {@link xnap.gui.util.HelpManager#enableHelpKeys(JComponent, String, HelpSet)} like this:
*
* <pre>
* HelpManager.enableHelpKeys(getRootPane(), "id-string", helpSet);
* </pre>
*/
private class HelpAction extends AbstractAction
{
public HelpAction()
{
putValue(Action.NAME, XNap.tr("Help"));
putValue(Action.SHORT_DESCRIPTION,
XNap.tr("Shows the help dialog."));
putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_H));
if (HelpManager.getMainHelpSet() == null) {
this.setEnabled(false);
}
}
public void actionPerformed(ActionEvent event)
{
HelpBroker hb = HelpManager.getMainHelpBroker();
String id = CSH.getHelpIDString(getRootPane());
HelpSet hs = CSH.getHelpSet(getRootPane());
if (hs == null) {
hs = hb.getHelpSet();
}
try {
Map.ID mapId = Map.ID.create(id, hs);
if (mapId == null) {
mapId = hs.getHomeID();
}
hb.setCurrentID(mapId);
hb.setDisplayed(true);
}
catch (BadIDException be) {
logger.debug("bad id", be);
}
catch (InvalidHelpSetContextException e) {
logger.debug("helpset", e);
}
}
}
/**
* Handle the Okay button. Invokes <code>apply()</code> and
* <code>close()</code> when pressed.
*
* @see #apply()
* @see #close()
*/
private class OkayAction extends AbstractAction {
public OkayAction()
{
putValue(Action.NAME, XNap.tr("OK"));
putValue(Action.SHORT_DESCRIPTION,
XNap.tr("Closes the dialog saving changes."));
putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_O));
}
public void actionPerformed(ActionEvent event)
{
try {
if (!apply()) {
return;
}
}
catch (IllegalArgumentException e) {
JOptionPane.showMessageDialog
(DefaultDialog.this, e.getLocalizedMessage(),
XNap.tr("Illegal Value"), JOptionPane.ERROR_MESSAGE);
return;
}
isOkay = true;
close();
}
}
private class CloseListener extends WindowAdapter
{
public void windowClosing (java.awt.event.WindowEvent evt)
{
isOkay = false;
close();
}
}
}
The table below shows all metrics for DefaultDialog.java.




