source: gli/trunk/src/org/greenstone/gatherer/gui/WarningDialog.java@ 16335

Last change on this file since 16335 was 16335, checked in by ak19, 16 years ago

Value_field is now a JComponent rather than a JTextField, since URLField.java has changed to encompass JCombobox as well (it was previously a JTextField only)

  • Property svn:keywords set to Author Date Id Revision
File size: 8.9 KB
RevLine 
[6322]1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Author: John Thompson, Greenstone Project, NZDL, University of Waikato
9 *
10 * Copyright (C) 2003 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26 */
[5058]27package org.greenstone.gatherer.gui;
28
29import java.awt.*;
30import java.awt.event.*;
31import javax.swing.*;
32import org.greenstone.gatherer.Configuration;
33import org.greenstone.gatherer.Dictionary;
34import org.greenstone.gatherer.Gatherer;
[10011]35import org.greenstone.gatherer.util.JarTools;
[5058]36
[8605]37
38/** A dialog that warns about something, and allows the user to turn off the warning in the future. */
[5058]39public class WarningDialog
[6155]40 extends ModalDialog
[9350]41 implements ActionListener, KeyListener
42{
[7558]43 static final private Dimension NORMAL_SIZE = new Dimension(450, 160);
44 static final private Dimension SETTING_SIZE = new Dimension(450, 200);
[5058]45
46 private int result = JOptionPane.CANCEL_OPTION;
47 private JButton cancel_button;
48 private JButton ok_button;
49 private JCheckBox show_check;
[16335]50 private JComponent value_field;
[5164]51 private JPanel value_panel;
[5058]52 private String affected_property;
53 private String full_property;
[16335]54 /** Whether or not to display the checkbox that will turn off this dialog in future */
55 private final boolean showcheckbox;
[5058]56
57
[16335]58 public WarningDialog(String warning_name, String warning_title, String warning_message, String affected_property, boolean can_cancel)
[8606]59 {
[16335]60 this(warning_name, warning_title, warning_message, affected_property, can_cancel, true); // true for show checkbox
61 }
62
63
64 public WarningDialog(String warning_name, String warning_title, String warning_message, String affected_property, boolean can_cancel,
65 boolean showcheckbox)
66 {
[5058]67 super(Gatherer.g_man, "Warning", true);
[5564]68
[16335]69 this.showcheckbox = showcheckbox;
70
[5058]71 // Determine the name of this prompt.
72 this.affected_property = affected_property;
[9350]73 this.full_property = warning_name;
[5564]74
[5058]75 // Now build dialog.
[5564]76 if (affected_property != null) {
[5058]77 setSize(SETTING_SIZE);
78 }
79 else {
80 setSize(NORMAL_SIZE);
81 }
[13397]82 setTitle(warning_title);
[12119]83
[5058]84 // Creation
85 JPanel content_pane = (JPanel) getContentPane();
86 JPanel text_pane = new JPanel();
[10011]87 JLabel icon_label = new JLabel(JarTools.getImage("gatherer_medium.gif"));
[5564]88
89 JTextArea text_area = new JTextArea();
90 text_area.setEditable(false);
[5058]91 text_area.setLineWrap(true);
[9864]92 text_area.setText(warning_message);
[12263]93 text_area.setCaretPosition(0);
[5058]94 text_area.setWrapStyleWord(true);
[5564]95
[5164]96 value_panel = new JPanel();
[12119]97 JLabel value_label = new JLabel(Dictionary.get("WarningDialog.Value"));
98
[5058]99 value_field = new JTextField();
100 JPanel bottom_pane = new JPanel();
101 JPanel control_pane = new JPanel();
[12119]102 ok_button = new GLIButton(Dictionary.get("General.OK"), Dictionary.get("General.OK_Tooltip"));
103 cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Pure_Cancel_Tooltip"));
104
[5058]105 // Connection
106 ok_button.addActionListener(this);
107 cancel_button.addActionListener(this);
[5185]108 ok_button.addKeyListener(this);
109 cancel_button.addKeyListener(this);
[6155]110 getRootPane().setDefaultButton(ok_button);
[5564]111
[5058]112 // Layout
113 icon_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
114
115 value_label.setBorder(BorderFactory.createEmptyBorder(0, icon_label.getPreferredSize().width, 0, 0));
116
117 value_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
[7146]118 value_panel.setLayout(new BorderLayout(5,0));
[5058]119 value_panel.add(value_label, BorderLayout.WEST);
120 value_panel.add(value_field, BorderLayout.CENTER);
121
122 text_pane.setLayout(new BorderLayout());
123 text_pane.add(icon_label, BorderLayout.WEST);
124 text_pane.add(new JScrollPane(text_area), BorderLayout.CENTER);
125 if(affected_property != null) {
126 text_pane.add(value_panel, BorderLayout.SOUTH);
127 }
[5564]128
[5058]129 if(can_cancel) {
130 control_pane.setLayout(new GridLayout(1,2,5,0));
131 control_pane.add(ok_button);
132 control_pane.add(cancel_button);
133 }
134 else {
135 control_pane.setLayout(new BorderLayout());
136 control_pane.add(ok_button, BorderLayout.EAST);
137 }
138
139 bottom_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
140 bottom_pane.setLayout(new BorderLayout());
141 bottom_pane.add(control_pane, BorderLayout.EAST);
[16335]142 if(showcheckbox) {
143 show_check = new JCheckBox(Dictionary.get("WarningDialog.Dont_Show_Again"));
144 bottom_pane.add(show_check, BorderLayout.CENTER);
145 }
[5058]146
147 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
148 content_pane.setLayout(new BorderLayout());
149 content_pane.add(text_pane, BorderLayout.CENTER);
150 content_pane.add(bottom_pane, BorderLayout.SOUTH);
[5564]151
[5058]152 // Position
153 Dimension size = getSize();
[5564]154 if (Gatherer.g_man != null) {
[5058]155 Rectangle frame_bounds = Gatherer.g_man.getBounds();
156 setLocation(frame_bounds.x + (frame_bounds.width - size.width) / 2, frame_bounds.y + (frame_bounds.height - size.height) / 2);
157 }
158 else {
159 Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
160 setLocation((screen_size.width - size.width) / 2, (screen_size.height - size.height) / 2);
161 }
162 }
163
164 public void actionPerformed(ActionEvent event) {
[16335]165 boolean valid_value = true;
[5058]166 if(event.getSource() == ok_button) {
[8231]167 if(affected_property != null && Configuration.self != null) {
[16335]168 valid_value = URLField.validateURL(value_field);
169 if(valid_value) {
170 // Store the value of the property
171 URLField.store(value_field, affected_property); //Configuration.setString(affected_property, true, value);
[5164]172 }
173 }
[16335]174 if(valid_value) {
[5164]175 result = JOptionPane.OK_OPTION;
176 }
[16335]177 }
178
179 if(valid_value) {
180 if (Configuration.self != null && showcheckbox) {
[5164]181 // Store the state of the show message checkbox.
[8231]182 Configuration.set(full_property, true, !show_check.isSelected());
[5058]183 }
[5164]184 // Done.
[6155]185 setVisible(false);
[5058]186 }
[5164]187 else {
[5593]188 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("WarningDialog.Invalid_Value"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
[5058]189 }
190 }
191
[5185]192 /** Gives notification of key events on the buttons */
[16335]193 public void keyPressed(KeyEvent e) {}
194
195 public void keyReleased(KeyEvent e) {
[5185]196 if (e.getKeyCode() == KeyEvent.VK_ENTER) {
197 // Enter: Click the button in focus
198 Object source = e.getSource();
199 if (source instanceof AbstractButton) {
200 ((AbstractButton) source).doClick();
201 }
202 }
203 }
204
[16335]205 public void keyTyped(KeyEvent e) {}
[5185]206
207
[5058]208 public int display() {
[8231]209 if (Configuration.self == null || Configuration.get(full_property, false)) {
[6155]210 setVisible(true);
[5058]211 }
212 // We are no longer showing this dialog, so result must always be true.
213 else {
214 result = JOptionPane.OK_OPTION;
215 }
[6538]216 // Ask the parent to repaint, just to be sure
217 if(Gatherer.g_man != null) {
218 Gatherer.g_man.repaint();
219 }
[5058]220 return result;
221 }
222
[6322]223 /** Allows you to specify whether this dialog is a warning dialog, or only a message dialog.
224 * @param message_only true if this dialog shows a message, false for a warning
225 */
226 public void setMessageOnly(boolean message_only) {
[16335]227 if(!showcheckbox) {
228 return;
229 }
230
[6322]231 // If this is a message then change the checkbox
232 if(message_only) {
[12119]233 show_check.setText(Dictionary.get("WarningDialog.Dont_Show_Again_Message"));
[6322]234 }
235 // And if its a warning, change them back
236 else {
[12119]237 show_check.setText(Dictionary.get("WarningDialog.Dont_Show_Again"));
[6322]238 }
239 }
240
[5164]241 /** Allows you to replace the generic text field control with a JTextField subclass with further functionality. For instance you might provide a URLField to allow only valid URLs to be accepted.
242 * @param control the JTextField subclass you want to use for the control
243 */
[16335]244 public void setValueField(JComponent control) {
[5164]245 // Remove the current control
246 value_panel.remove(value_field);
247 // Replace with the new one
248 value_field = control;
249 // Re-add
250 value_panel.add(value_field, BorderLayout.CENTER);
251 }
[6155]252
253 protected void processWindowEvent(WindowEvent event) {
254 if(event.getID() == WindowEvent.WINDOW_ACTIVATED) {
255 if(affected_property != null) {
256 value_field.requestFocus();
257 }
258 else {
259 ok_button.requestFocus();
260 }
261 }
262 else {
263 super.processWindowEvent(event);
264 }
265 }
[14567]266
267 public void setValueField(String field_value){
[16335]268 URLField.setText(this.value_field, field_value);
[14567]269 }
270
[5058]271}
Note: See TracBrowser for help on using the repository browser.