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: JDialogConfirm.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 JDialogConfirm extends JDialog implements ActionListener,
022: DefaultFocused {
023: private static final long serialVersionUID = -947283805874742381L;
024:
025: public static final int OK = 0;
026: public static final int CANCEL = 1;
027:
028: protected GridBagConstraints mConstraints = null;
029:
030: protected JFrame mParentFrame = null;
031: protected JPanel mContentPane = null;
032: protected JLabel mConfirmationIcon = null;
033: protected JPanel mConfirmationIconPanel = null;
034: protected JComponent mConfirmationMessage = null;
035: protected JButton mOkButton = null;
036: protected JButton mCancelButton = null;
037: protected JPanel mButtonsPanel = null;
038: protected int mPerformedAction = CANCEL;
039:
040: public JDialogConfirm(JFrame frame, String text) {
041: this (frame,
042: Localization.getString("rife.dialog.confirm.title"),
043: text);
044: }
045:
046: public JDialogConfirm(JFrame frame, String title, String text) {
047: this (frame, title, new JLabel(text));
048: }
049:
050: public JDialogConfirm(JFrame frame, String title,
051: JComponent messageComponent) {
052: super (frame, title, true);
053:
054: mParentFrame = frame;
055:
056: ImageIcon icon = null;
057: if (Images.hasRepInstance()) {
058: icon = Images.getRepInstance().getImageIcon(
059: RifeConfig.Swing.getIconConfirmPath());
060: }
061: if (null == icon) {
062: icon = new ImageIcon(JDialogError.class.getClassLoader()
063: .getResource(RifeConfig.Swing.getIconConfirmPath()));
064: }
065: mConfirmationIcon = new JLabel(icon);
066: mConfirmationIcon.setBorder(BorderFactory.createMatteBorder(10,
067: 10, 10, 10, (Color) null));
068: mConfirmationIcon.setBackground(Colors.CONFIRM_BACKGROUND);
069: mConfirmationIcon.setOpaque(true);
070: mConfirmationIconPanel = new JPanel();
071: mConfirmationIconPanel.setBackground(Colors.CONFIRM_BACKGROUND);
072: mConfirmationIconPanel.add(mConfirmationIcon);
073: mConfirmationMessage = messageComponent;
074: mConfirmationMessage.setBorder(BorderFactory.createEmptyBorder(
075: 10, 10, 10, 10));
076: mOkButton = new JButton();
077: mOkButton.addActionListener(this );
078: mCancelButton = new JButton();
079: mCancelButton.addActionListener(this );
080: mButtonsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
081: setButtonLabels();
082: mButtonsPanel.add(mOkButton);
083: mButtonsPanel.add(mCancelButton);
084:
085: mContentPane = new JPanel(new GridBagLayout());
086: mContentPane.setBackground(Color.white);
087: setContentPane(mContentPane);
088: mConstraints = new GridBagConstraints();
089: mConstraints.gridx = 0;
090: mConstraints.gridy = 0;
091: mConstraints.gridwidth = 1;
092: mConstraints.gridheight = 1;
093: mConstraints.weightx = 0;
094: mConstraints.weighty = 1;
095: mConstraints.anchor = GridBagConstraints.CENTER;
096: mConstraints.fill = GridBagConstraints.BOTH;
097: mContentPane.add(mConfirmationIconPanel, mConstraints);
098: mConstraints.gridx = 1;
099: mConstraints.weightx = 1;
100: mConstraints.anchor = GridBagConstraints.NORTHWEST;
101: mConstraints.fill = GridBagConstraints.HORIZONTAL;
102: mContentPane.add(mConfirmationMessage, mConstraints);
103:
104: mConstraints.gridx = 0;
105: mConstraints.gridy = 1;
106: mConstraints.weightx = 1;
107: mConstraints.weighty = 0;
108: mConstraints.gridwidth = 2;
109: mContentPane.add(new JSeparator(JSeparator.HORIZONTAL),
110: mConstraints);
111:
112: mConstraints.gridy = 2;
113: mContentPane.add(mButtonsPanel, mConstraints);
114:
115: mContentPane.setVisible(true);
116: pack();
117:
118: setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
119: if (null != frame) {
120: setLocationRelativeTo(frame);
121: }
122:
123: new DefaultFocusSetter(this );
124: }
125:
126: public JComponent getDefaultFocus() {
127: return mOkButton;
128: }
129:
130: protected void setButtonLabels() {
131: mOkButton.setText(Localization
132: .getString("rife.dialog.confirm.okbutton"));
133: mOkButton.setMnemonic(Localization
134: .getChar("rife.dialog.confirm.okbutton.mnemonic"));
135: mCancelButton.setText(Localization
136: .getString("rife.dialog.confirm.cancelbutton"));
137: mCancelButton.setMnemonic(Localization
138: .getChar("rife.dialog.confirm.cancelbutton.mnemonic"));
139: }
140:
141: public int getPerformedAction() {
142: return mPerformedAction;
143: }
144:
145: public void actionPerformed(ActionEvent event) {
146: Object source = event.getSource();
147:
148: if (mOkButton == source) {
149: mPerformedAction = OK;
150: dispose();
151: } else if (mCancelButton == source) {
152: mPerformedAction = CANCEL;
153: dispose();
154: }
155: }
156: }
|