001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: JDialogYesNo.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.swing;
009:
010: import javax.swing.*;
011:
012: import com.uwyn.rife.config.RifeConfig;
013: import com.uwyn.rife.tools.Localization;
014: import java.awt.Color;
015: import java.awt.FlowLayout;
016: import java.awt.GridBagConstraints;
017: import java.awt.GridBagLayout;
018: import java.awt.event.ActionEvent;
019: import java.awt.event.ActionListener;
020:
021: public class JDialogYesNo extends JDialog implements ActionListener,
022: DefaultFocused {
023: private static final long serialVersionUID = -1366980080116312287L;
024:
025: public static final int OK = 0;
026: public static final int NO = 1;
027: public static final int CANCEL = 2;
028:
029: protected GridBagConstraints mConstraints = null;
030:
031: protected JFrame mParentFrame = null;
032: protected JPanel mContentPane = null;
033: protected JLabel mConfirmationIcon = null;
034: protected JPanel mConfirmationIconPanel = null;
035: protected JComponent mConfirmationMessage = null;
036: protected JButton mYesButton = null;
037: protected JButton mNoButton = null;
038: protected JButton mCancelButton = null;
039: protected JPanel mButtonsPanel = null;
040: protected int mPerformedAction = CANCEL;
041:
042: public JDialogYesNo(JFrame frame, String text) {
043: this (frame, Localization.getString("rife.dialog.yesno.title"),
044: text);
045: }
046:
047: public JDialogYesNo(JFrame frame, String title, String text) {
048: this (frame, title, new JLabel(text));
049: }
050:
051: public JDialogYesNo(JFrame frame, String title,
052: JComponent messageComponent) {
053: super (frame, title, true);
054:
055: mParentFrame = frame;
056:
057: ImageIcon icon = null;
058: if (Images.hasRepInstance()) {
059: icon = Images.getRepInstance().getImageIcon(
060: RifeConfig.Swing.getIconConfirmPath());
061: }
062: if (null == icon) {
063: icon = new ImageIcon(JDialogError.class.getClassLoader()
064: .getResource(RifeConfig.Swing.getIconConfirmPath()));
065: }
066: mConfirmationIcon = new JLabel(icon);
067: mConfirmationIcon.setBorder(BorderFactory.createMatteBorder(10,
068: 10, 10, 10, (Color) null));
069: mConfirmationIcon.setBackground(Colors.CONFIRM_BACKGROUND);
070: mConfirmationIcon.setOpaque(true);
071: mConfirmationIconPanel = new JPanel();
072: mConfirmationIconPanel.setBackground(Colors.CONFIRM_BACKGROUND);
073: mConfirmationIconPanel.add(mConfirmationIcon);
074: mConfirmationMessage = messageComponent;
075: mConfirmationMessage.setBorder(BorderFactory.createEmptyBorder(
076: 10, 10, 10, 10));
077: mYesButton = new JButton();
078: mYesButton.addActionListener(this );
079: mNoButton = new JButton();
080: mNoButton.addActionListener(this );
081: mCancelButton = new JButton();
082: mCancelButton.addActionListener(this );
083: mButtonsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
084: setButtonLabels();
085: mButtonsPanel.add(mYesButton);
086: mButtonsPanel.add(mNoButton);
087: mButtonsPanel.add(mCancelButton);
088:
089: mContentPane = new JPanel(new GridBagLayout());
090: mContentPane.setBackground(Color.white);
091: setContentPane(mContentPane);
092: mConstraints = new GridBagConstraints();
093: mConstraints.gridx = 0;
094: mConstraints.gridy = 0;
095: mConstraints.gridwidth = 1;
096: mConstraints.gridheight = 1;
097: mConstraints.weightx = 0;
098: mConstraints.weighty = 1;
099: mConstraints.anchor = GridBagConstraints.CENTER;
100: mConstraints.fill = GridBagConstraints.BOTH;
101: mContentPane.add(mConfirmationIconPanel, mConstraints);
102: mConstraints.gridx = 1;
103: mConstraints.weightx = 1;
104: mConstraints.anchor = GridBagConstraints.NORTHWEST;
105: mConstraints.fill = GridBagConstraints.HORIZONTAL;
106: mContentPane.add(mConfirmationMessage, mConstraints);
107:
108: mConstraints.gridx = 0;
109: mConstraints.gridy = 1;
110: mConstraints.weightx = 1;
111: mConstraints.weighty = 0;
112: mConstraints.gridwidth = 2;
113: mContentPane.add(new JSeparator(JSeparator.HORIZONTAL),
114: mConstraints);
115:
116: mConstraints.gridy = 2;
117: mContentPane.add(mButtonsPanel, mConstraints);
118:
119: mContentPane.setVisible(true);
120: pack();
121:
122: setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
123: if (null != frame) {
124: setLocationRelativeTo(frame);
125: }
126:
127: new DefaultFocusSetter(this );
128: }
129:
130: public JComponent getDefaultFocus() {
131: return mYesButton;
132: }
133:
134: protected void setButtonLabels() {
135: mYesButton.setText(Localization
136: .getString("rife.dialog.yesno.yesbutton"));
137: mYesButton.setMnemonic(Localization
138: .getChar("rife.dialog.yesno.yesbutton.mnemonic"));
139: mNoButton.setText(Localization
140: .getString("rife.dialog.yesno.nobutton"));
141: mNoButton.setMnemonic(Localization
142: .getChar("rife.dialog.yesno.nobutton.mnemonic"));
143: mCancelButton.setText(Localization
144: .getString("rife.dialog.confirm.cancelbutton"));
145: mCancelButton.setMnemonic(Localization
146: .getChar("rife.dialog.confirm.cancelbutton.mnemonic"));
147: }
148:
149: public int getPerformedAction() {
150: return mPerformedAction;
151: }
152:
153: public void actionPerformed(ActionEvent event) {
154: Object source = event.getSource();
155:
156: if (mYesButton == source) {
157: mPerformedAction = OK;
158: dispose();
159: } else if (mNoButton == source) {
160: mPerformedAction = NO;
161: dispose();
162: } else if (mCancelButton == source) {
163: mPerformedAction = CANCEL;
164: dispose();
165: }
166: }
167: }
|