~ubuntu-branches/ubuntu/utopic/freemind/utopic

« back to all changes in this revision

Viewing changes to freemind/freemind/common/OptionalDontShowMeAgainDialog.java

  • Committer: Bazaar Package Importer
  • Author(s): Benjamin Drung
  • Date: 2010-01-03 14:19:19 UTC
  • mfrom: (2.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20100103141919-m5az7dkicy21hqop
Tags: 0.9.0~rc6+dfsg-1ubuntu1
* Merge from Debian unstable (LP: #182927), remaining changes:
  - debian/copyright: add license/copyright for
    freemind/freemind/main/ExampleFileFilter.java

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*FreeMind - A Program for creating and viewing Mindmaps
 
2
 *Copyright (C) 2000-2007  Christian Foltin, Dimitry Polivaev and others.
 
3
 *
 
4
 *See COPYING for Details
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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.
 
19
 *
 
20
 * Created on 31.07.2007
 
21
 */
 
22
/*$Id: OptionalDontShowMeAgainDialog.java,v 1.1.2.5 2008/03/30 20:34:45 christianfoltin Exp $*/
 
23
 
 
24
package freemind.common;
 
25
 
 
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;
 
33
 
 
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;
 
42
 
 
43
import freemind.controller.Controller;
 
44
import freemind.main.FreeMind;
 
45
import freemind.main.Resources;
 
46
import freemind.main.Tools;
 
47
 
 
48
/**
 
49
 * Dialog with a decision that can be disabled.
 
50
 * 
 
51
 * @author foltin
 
52
 * 
 
53
 */
 
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;
 
67
 
 
68
        public interface DontShowPropertyHandler {
 
69
                /**
 
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).
 
74
                 */
 
75
                String getProperty();
 
76
                void setProperty(String pValue);
 
77
        }
 
78
        
 
79
        /**
 
80
         * Standard property handler, if you have a controller and a property.
 
81
         *
 
82
         */
 
83
        public static class StandardPropertyHandler implements DontShowPropertyHandler {
 
84
                private final Controller mController;
 
85
                private String mPropertyName;
 
86
 
 
87
                public StandardPropertyHandler(Controller pController, String pPropertyName){
 
88
                        mController = pController;
 
89
                        mPropertyName = pPropertyName;
 
90
                        
 
91
                }
 
92
                public String getProperty() {
 
93
                        return mController.getProperty(mPropertyName);
 
94
                }
 
95
                
 
96
                public void setProperty(String pValue) {
 
97
                        mController.setProperty(mPropertyName, pValue);
 
98
                }
 
99
        }
 
100
        public OptionalDontShowMeAgainDialog(JFrame pFrame, Component pComponent, String pMessageId,
 
101
                        String pTitleId, TextTranslator pTextTranslator, DontShowPropertyHandler pDontShowPropertyHandler, int pMessageType) {
 
102
                mComponent = pComponent;
 
103
                mParent = pFrame;
 
104
                mMessageId = pMessageId;
 
105
                mTitleId = pTitleId;
 
106
                mTextTranslator = pTextTranslator;
 
107
                mDontShowPropertyHandler = pDontShowPropertyHandler;
 
108
                mMessageType = pMessageType;
 
109
        }
 
110
 
 
111
        /**
 
112
         * @return an int from JOptionPane (eg. JOptionPane.OK_OPTION).
 
113
         */
 
114
        public int getResult() {
 
115
                return mResult;
 
116
        }
 
117
 
 
118
 
 
119
        public OptionalDontShowMeAgainDialog show() {
 
120
                String property = mDontShowPropertyHandler.getProperty();
 
121
                if(Tools.safeEquals(property, "true")) {
 
122
                        mResult = JOptionPane.OK_OPTION;
 
123
                        return this;
 
124
                }
 
125
                if(Tools.safeEquals(property, "false")) {
 
126
                        mResult = JOptionPane.CANCEL_OPTION;
 
127
                        return this;
 
128
                }
 
129
                mDialog = null;
 
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() {
 
134
 
 
135
                        public void actionPerformed(ActionEvent pE) {
 
136
                                close(JOptionPane.CANCEL_OPTION);
 
137
                        }
 
138
                };
 
139
                AbstractAction okAction = new AbstractAction() {
 
140
 
 
141
                        public void actionPerformed(ActionEvent pE) {
 
142
                                close(JOptionPane.OK_OPTION);
 
143
                        }
 
144
                };
 
145
                Tools.addEscapeActionToDialog(mDialog, cancelAction);
 
146
                mDialog.addWindowListener(new WindowAdapter() {
 
147
                        public void windowClosing(WindowEvent pE) {
 
148
                                close(JOptionPane.CANCEL_OPTION);
 
149
                        }
 
150
                });
 
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(
 
160
                                                5, 5, 0, 0), 0, 0));
 
161
                String boxString;
 
162
                if(mMessageType == ONLY_OK_SELECTION_IS_STORED) {
 
163
                        boxString = "OptionalDontShowMeAgainDialog.dontShowAgain";                      
 
164
                } else {
 
165
                        boxString = "OptionalDontShowMeAgainDialog.rememberMyDescision";
 
166
                }
 
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,
 
172
                                                0, 0), 0, 0));
 
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,
 
179
                                                0, 0), 0, 0));
 
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,
 
186
                                                0, 0), 0, 0));
 
187
                mDialog.getRootPane().setDefaultButton(okButton);
 
188
                mDialog.pack();
 
189
                Tools.setDialogLocationRelativeTo(mDialog, mComponent);
 
190
                mDialog.setVisible(true);
 
191
                return this;
 
192
        }
 
193
 
 
194
        private void close(int pResult) {
 
195
                mResult = pResult;
 
196
                if(mDontShowAgainBox.isSelected()){
 
197
                        if(mMessageType == ONLY_OK_SELECTION_IS_STORED) {
 
198
                                if (mResult == JOptionPane.OK_OPTION) {
 
199
                                        mDontShowPropertyHandler.setProperty("true");
 
200
                                }
 
201
                        } else {
 
202
                                mDontShowPropertyHandler.setProperty((mResult==JOptionPane.OK_OPTION)?"true":"false");
 
203
                        }
 
204
                } else {
 
205
                        mDontShowPropertyHandler.setProperty("");
 
206
                }
 
207
                mDialog.setVisible(false);
 
208
                mDialog.dispose();
 
209
        }
 
210
}