1
/*FreeMind - A Program for creating and viewing Mindmaps
2
*Copyright (C) 2000-2007 Christian Foltin, Dimitry Polivaev and others.
4
*See COPYING for Details
6
*This program is free software; you can redistribute it and/or
7
*modify it under the terms of the GNU General Public License
8
*as published by the Free Software Foundation; either version 2
9
*of the License, or (at your option) any later version.
11
*This program is distributed in the hope that it will be useful,
12
*but WITHOUT ANY WARRANTY; without even the implied warranty of
13
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
*GNU General Public License for more details.
16
*You should have received a copy of the GNU General Public License
17
*along with this program; if not, write to the Free Software
18
*Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
* Created on 31.07.2007
22
/*$Id: OptionalDontShowMeAgainDialog.java,v 1.1.2.5 2008/03/30 20:34:45 christianfoltin Exp $*/
24
package freemind.common;
26
import java.awt.Component;
27
import java.awt.GridBagConstraints;
28
import java.awt.GridBagLayout;
29
import java.awt.Insets;
30
import java.awt.event.ActionEvent;
31
import java.awt.event.WindowAdapter;
32
import java.awt.event.WindowEvent;
34
import javax.swing.AbstractAction;
35
import javax.swing.ImageIcon;
36
import javax.swing.JButton;
37
import javax.swing.JCheckBox;
38
import javax.swing.JDialog;
39
import javax.swing.JFrame;
40
import javax.swing.JLabel;
41
import javax.swing.JOptionPane;
43
import freemind.controller.Controller;
44
import freemind.main.FreeMind;
45
import freemind.main.Resources;
46
import freemind.main.Tools;
49
* Dialog with a decision that can be disabled.
54
public class OptionalDontShowMeAgainDialog {
55
public final static int ONLY_OK_SELECTION_IS_STORED = 0;
56
public final static int BOTH_OK_AND_CANCEL_OPTIONS_ARE_STORED = 1;
57
private final String mTitleId;
58
private final String mMessageId;
59
private final TextTranslator mTextTranslator;
60
private final JFrame mParent;
61
private int mResult = JOptionPane.CANCEL_OPTION;
62
private JDialog mDialog;
63
private JCheckBox mDontShowAgainBox;
64
private final DontShowPropertyHandler mDontShowPropertyHandler;
65
private final int mMessageType;
66
private final Component mComponent;
68
public interface DontShowPropertyHandler {
70
* @return accepted are the following values as return values:
71
* * "" (means: show this dialog)
72
* * "true" (means: the answer was ok and I want to remember that).
73
* * "false" (means: the answer was cancel and I want to remember that).
76
void setProperty(String pValue);
80
* Standard property handler, if you have a controller and a property.
83
public static class StandardPropertyHandler implements DontShowPropertyHandler {
84
private final Controller mController;
85
private String mPropertyName;
87
public StandardPropertyHandler(Controller pController, String pPropertyName){
88
mController = pController;
89
mPropertyName = pPropertyName;
92
public String getProperty() {
93
return mController.getProperty(mPropertyName);
96
public void setProperty(String pValue) {
97
mController.setProperty(mPropertyName, pValue);
100
public OptionalDontShowMeAgainDialog(JFrame pFrame, Component pComponent, String pMessageId,
101
String pTitleId, TextTranslator pTextTranslator, DontShowPropertyHandler pDontShowPropertyHandler, int pMessageType) {
102
mComponent = pComponent;
104
mMessageId = pMessageId;
106
mTextTranslator = pTextTranslator;
107
mDontShowPropertyHandler = pDontShowPropertyHandler;
108
mMessageType = pMessageType;
112
* @return an int from JOptionPane (eg. JOptionPane.OK_OPTION).
114
public int getResult() {
119
public OptionalDontShowMeAgainDialog show() {
120
String property = mDontShowPropertyHandler.getProperty();
121
if(Tools.safeEquals(property, "true")) {
122
mResult = JOptionPane.OK_OPTION;
125
if(Tools.safeEquals(property, "false")) {
126
mResult = JOptionPane.CANCEL_OPTION;
130
mDialog = new JDialog(mParent, mTextTranslator.getText(mTitleId));
131
mDialog.setModal(true);
132
mDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
133
AbstractAction cancelAction = new AbstractAction() {
135
public void actionPerformed(ActionEvent pE) {
136
close(JOptionPane.CANCEL_OPTION);
139
AbstractAction okAction = new AbstractAction() {
141
public void actionPerformed(ActionEvent pE) {
142
close(JOptionPane.OK_OPTION);
145
Tools.addEscapeActionToDialog(mDialog, cancelAction);
146
mDialog.addWindowListener(new WindowAdapter() {
147
public void windowClosing(WindowEvent pE) {
148
close(JOptionPane.CANCEL_OPTION);
151
mDialog.getContentPane().setLayout(new GridBagLayout());
152
mDialog.getContentPane().add(new JLabel(mTextTranslator.getText(mMessageId)),
153
new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0,
154
GridBagConstraints.WEST, GridBagConstraints.BOTH,
155
new Insets(5, 5, 0, 0), 0, 0));
156
//TODO: Replace by usual java question mark.
157
ImageIcon questionMark = new ImageIcon(Resources.getInstance().getResource("images/icons/help.png"));
158
mDialog.getContentPane().add(new JLabel(questionMark), new GridBagConstraints(0, 0, 1, 2, 1.0,
159
2.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(
162
if(mMessageType == ONLY_OK_SELECTION_IS_STORED) {
163
boxString = "OptionalDontShowMeAgainDialog.dontShowAgain";
165
boxString = "OptionalDontShowMeAgainDialog.rememberMyDescision";
167
mDontShowAgainBox = new JCheckBox(mTextTranslator
168
.getText(boxString));
169
Tools.setLabelAndMnemonic(mDontShowAgainBox, null);
170
mDialog.getContentPane().add(mDontShowAgainBox, new GridBagConstraints(0, 2, 3, 1, 1.0, 1.0,
171
GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5,
173
JButton okButton = new JButton(mTextTranslator
174
.getText("OptionalDontShowMeAgainDialog.ok"));
175
Tools.setLabelAndMnemonic(okButton, null);
176
okButton.addActionListener(okAction);
177
mDialog.getContentPane().add(okButton, new GridBagConstraints(2, 3, 1, 1, 1.0, 1.0,
178
GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5,
180
JButton cancelButton = new JButton(mTextTranslator
181
.getText("OptionalDontShowMeAgainDialog.cancel"));
182
Tools.setLabelAndMnemonic(cancelButton, null);
183
cancelButton.addActionListener(cancelAction);
184
mDialog.getContentPane().add(cancelButton, new GridBagConstraints(3, 3, 1, 1, 1.0, 1.0,
185
GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5,
187
mDialog.getRootPane().setDefaultButton(okButton);
189
Tools.setDialogLocationRelativeTo(mDialog, mComponent);
190
mDialog.setVisible(true);
194
private void close(int pResult) {
196
if(mDontShowAgainBox.isSelected()){
197
if(mMessageType == ONLY_OK_SELECTION_IS_STORED) {
198
if (mResult == JOptionPane.OK_OPTION) {
199
mDontShowPropertyHandler.setProperty("true");
202
mDontShowPropertyHandler.setProperty((mResult==JOptionPane.OK_OPTION)?"true":"false");
205
mDontShowPropertyHandler.setProperty("");
207
mDialog.setVisible(false);