/[schmitzm]/trunk/src/skrueger/swing/TranslationAskJDialog.java
ViewVC logotype

Contents of /trunk/src/skrueger/swing/TranslationAskJDialog.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1137 - (show annotations)
Fri Oct 15 15:21:36 2010 UTC (14 years, 4 months ago) by alfonx
File size: 8168 byte(s)
Organized Imports and some i8n massakre
1 /*******************************************************************************
2 * Copyright (c) 2009 Martin O. J. Schmitz.
3 *
4 * This file is part of the SCHMITZM library - a collection of utility
5 * classes based on Java 1.6, focusing (not only) on Java Swing
6 * and the Geotools library.
7 *
8 * The SCHMITZM project is hosted at:
9 * http://wald.intevation.org/projects/schmitzm/
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 3
14 * of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License (license.txt)
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * or try this link: http://www.gnu.org/licenses/lgpl.html
25 *
26 * Contributors:
27 * Martin O. J. Schmitz - initial API and implementation
28 * Stefan A. Tzeggai - additional utility classes
29 ******************************************************************************/
30 package skrueger.swing;
31
32 import java.awt.BorderLayout;
33 import java.awt.Component;
34 import java.awt.FlowLayout;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.ActionListener;
37 import java.awt.event.KeyEvent;
38 import java.awt.event.WindowAdapter;
39 import java.awt.event.WindowEvent;
40
41 import javax.swing.AbstractAction;
42 import javax.swing.Action;
43 import javax.swing.JButton;
44 import javax.swing.JComponent;
45 import javax.swing.JDialog;
46 import javax.swing.JPanel;
47 import javax.swing.JRootPane;
48 import javax.swing.KeyStroke;
49
50 import schmitzm.swing.SwingUtil;
51
52 public class TranslationAskJDialog extends CancellableDialogAdapter{
53
54 private OkButton okButton;
55 private CancelButton cancelButton;
56
57 public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL";
58 public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY";
59
60 private JComponent[] translationEditJPanelsOrJustComponents;
61
62 private boolean hasBeenCanceled;
63
64 private JButton[] optionalButtons;
65 private TranslationsAskJPanel translationsAskPane;
66
67 /**
68 * Since the registerKeyboardAction() method is part of the JComponent class
69 * definition, you must define the Escape keystroke and register the
70 * keyboard action with a JComponent, not with a JDialog. The JRootPane for
71 * the JDialog serves as an excellent choice to associate the registration,
72 * as this will always be visible. If you override the protected
73 * createRootPane() method of JDialog, you can return your custom JRootPane
74 * with the keystroke enabled:
75 */
76 @Override
77 protected JRootPane createRootPane() {
78 KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
79 JRootPane rootPane = new JRootPane();
80 rootPane.registerKeyboardAction(new ActionListener() {
81
82 public void actionPerformed(ActionEvent e) {
83 cancel();
84 }
85
86 }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
87
88 return rootPane;
89 }
90
91 /**
92 * The {@link TranslationAskJDialog} fills its content pane with an
93 * arbitrary number of components. If these {@link Component}s are
94 * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
95 * values and restore them if the dialog is canceled. Other
96 * {@link JComponent}s are just displayed.<br/>
97 * This class handles the cancel button itself. You may still want to listen
98 * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
99 * to be set visible afterwards.<br/>
100 */
101 public TranslationAskJDialog(Component owner,
102 final JComponent... translationEditJPanels) {
103 super(owner);
104 setComponents(translationEditJPanels);
105 }
106
107 /**
108 * The {@link TranslationAskJDialog} fills its content pane with an
109 * arbitrary number of components. If these {@link Component}s are
110 * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
111 * values and restore them if the dialog is canceled. Other
112 * {@link JComponent}s are just displayed.<br/>
113 * This class handles the cancel button itself. You may still want to listen
114 * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
115 * to be set visible afterwards.<br/>
116 * Using this constructor, you have to call setComponents afterwards.
117 */
118 public TranslationAskJDialog(Component owner) {
119 super(owner);
120 }
121
122 /**
123 * The {@link TranslationAskJDialog} fills its content pane with an
124 * arbitrary number of components. If these {@link Component}s are
125 * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
126 * values and restore them if the dialog is canceled. Other
127 * {@link JComponent}s are just displayed.
128 *
129 * @param translationEditJPanels
130 * Arbitrary list of {@link JComponent}s and
131 * {@link TranslationEditJPanel}s.
132 */
133 public void setComponents(final JComponent... translationEditJPanels) {
134 this.translationEditJPanelsOrJustComponents = translationEditJPanels;
135
136 init();
137 }
138
139
140 private void init() {
141 setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
142 addWindowListener(new WindowAdapter() {
143
144 public void windowClosing(WindowEvent e) {
145 cancel();
146 }
147
148 });
149
150 translationsAskPane = new TranslationsAskJPanel(translationEditJPanelsOrJustComponents);
151 JPanel cp = new JPanel(new BorderLayout());
152 cp.add(translationsAskPane, BorderLayout.WEST);
153 cp.add(getButtons(), BorderLayout.SOUTH);
154 setContentPane(cp);
155
156 setTitle(SwingUtil.R("TranslationAskJDialog.Title"));
157 setModal(true);
158 pack();
159 SwingUtil.setRelativeFramePosition(this, getParent(), .5, .5);
160 }
161
162 public void setOptionalButtons(JButton... optionalButtons) {
163 this.optionalButtons = optionalButtons;
164 init();
165 }
166
167 /**
168 * Called when the dilaog is closed using the cancel button. When
169 * overwriting this method, call super.cancel() after restoring your
170 * properties.
171 */
172 @Override
173 public void cancel() {
174 translationsAskPane.cancel();
175 firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);
176 hasBeenCanceled = true;
177 setVisible(false);
178 dispose();
179 }
180
181
182 private JComponent getButtons() {
183 JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
184
185 if (optionalButtons != null)
186 for (JButton b : optionalButtons) {
187 jPanel.add(b);
188 }
189
190 if (okButton == null) {
191 okButton = new OkButton(new AbstractAction() {
192 {
193 // Set a mnemonic character. In most look and feels, this
194 // causes the
195 // specified character to be underlined This indicates that
196 // if the component
197 // using this action has the focus and In some look and
198 // feels, this causes
199 // the specified character in the label to be underlined and
200 putValue(Action.MNEMONIC_KEY, new Integer(
201 java.awt.event.KeyEvent.VK_E));
202
203 // // Set tool tip text
204 // putValue(Action.SHORT_DESCRIPTION,
205 // "Accept the changes made to the translation."); //i8n
206
207 }
208
209 public void actionPerformed(ActionEvent evt) {
210
211 okClose();
212
213 }
214
215 });
216
217 }
218 jPanel.add(okButton);
219
220 if (cancelButton == null) {
221 cancelButton = new CancelButton(new AbstractAction("") {
222 public void actionPerformed(ActionEvent evt) {
223 cancel();
224 }
225 });
226 }
227 jPanel.add(cancelButton);
228
229 return jPanel;
230 }
231
232 /**
233 * This method is only called when the dialog is closed and not canceled.
234 * Can be overwritten to do anything when the dialog has been accepted.
235 */
236 public boolean okClose() {
237
238 if (!translationsAskPane.checkValidInputs())
239 return false;
240
241
242 dispose();
243
244 TranslationAskJDialog.this.firePropertyChange(
245 PROPERTY_APPLY_AND_CLOSE, null, null);
246 return true;
247 }
248
249
250
251 /**
252 * After the modal dialog has been closed, this allows to find out, whether
253 * the {@link Component} has been canceled.
254 *
255 * @return <code>true</code> if the {@link JDialog} has been canceled.
256 */
257 public boolean isCancelled() {
258 return hasBeenCanceled;
259 }
260
261 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26