[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] | 27 | package org.greenstone.gatherer.gui;
|
---|
| 28 |
|
---|
| 29 | import java.awt.*;
|
---|
| 30 | import java.awt.event.*;
|
---|
| 31 | import javax.swing.*;
|
---|
| 32 | import org.greenstone.gatherer.Configuration;
|
---|
| 33 | import org.greenstone.gatherer.Dictionary;
|
---|
| 34 | import org.greenstone.gatherer.Gatherer;
|
---|
[10011] | 35 | import 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] | 39 | public 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;
|
---|
| 50 | private JTextField value_field;
|
---|
[5164] | 51 | private JPanel value_panel;
|
---|
[5058] | 52 | private String affected_property;
|
---|
| 53 | private String full_property;
|
---|
| 54 |
|
---|
| 55 |
|
---|
[13397] | 56 | public WarningDialog(String warning_name, String warning_title, String warning_message, String affected_property, boolean can_cancel)
|
---|
[8606] | 57 | {
|
---|
[5058] | 58 | super(Gatherer.g_man, "Warning", true);
|
---|
[18297] | 59 | this.setComponentOrientation(Dictionary.getOrientation());
|
---|
[5058] | 60 | // Determine the name of this prompt.
|
---|
| 61 | this.affected_property = affected_property;
|
---|
[9350] | 62 | this.full_property = warning_name;
|
---|
[5564] | 63 |
|
---|
[5058] | 64 | // Now build dialog.
|
---|
[5564] | 65 | if (affected_property != null) {
|
---|
[5058] | 66 | setSize(SETTING_SIZE);
|
---|
| 67 | }
|
---|
| 68 | else {
|
---|
| 69 | setSize(NORMAL_SIZE);
|
---|
| 70 | }
|
---|
[13397] | 71 | setTitle(warning_title);
|
---|
[12119] | 72 |
|
---|
[5058] | 73 | // Creation
|
---|
| 74 | JPanel content_pane = (JPanel) getContentPane();
|
---|
[18297] | 75 | content_pane.setComponentOrientation(Dictionary.getOrientation());
|
---|
[5058] | 76 | JPanel text_pane = new JPanel();
|
---|
[18297] | 77 | text_pane.setComponentOrientation(Dictionary.getOrientation());
|
---|
[10011] | 78 | JLabel icon_label = new JLabel(JarTools.getImage("gatherer_medium.gif"));
|
---|
[18297] | 79 | icon_label.setComponentOrientation(Dictionary.getOrientation());
|
---|
| 80 |
|
---|
[5564] | 81 | JTextArea text_area = new JTextArea();
|
---|
[18297] | 82 | text_area.setComponentOrientation(Dictionary.getOrientation());
|
---|
[5564] | 83 | text_area.setEditable(false);
|
---|
[5058] | 84 | text_area.setLineWrap(true);
|
---|
[9864] | 85 | text_area.setText(warning_message);
|
---|
[12263] | 86 | text_area.setCaretPosition(0);
|
---|
[5058] | 87 | text_area.setWrapStyleWord(true);
|
---|
[5564] | 88 |
|
---|
[5164] | 89 | value_panel = new JPanel();
|
---|
[18297] | 90 | value_panel.setComponentOrientation(Dictionary.getOrientation());
|
---|
[12119] | 91 | JLabel value_label = new JLabel(Dictionary.get("WarningDialog.Value"));
|
---|
[18297] | 92 | value_label.setComponentOrientation(Dictionary.getOrientation());
|
---|
| 93 |
|
---|
[5058] | 94 | value_field = new JTextField();
|
---|
[18297] | 95 | value_field.setComponentOrientation(Dictionary.getOrientation());
|
---|
[5058] | 96 | JPanel bottom_pane = new JPanel();
|
---|
[18297] | 97 | bottom_pane.setComponentOrientation(Dictionary.getOrientation());
|
---|
[12119] | 98 | show_check = new JCheckBox(Dictionary.get("WarningDialog.Dont_Show_Again"));
|
---|
[18297] | 99 | show_check.setComponentOrientation(Dictionary.getOrientation());
|
---|
[5058] | 100 | JPanel control_pane = new JPanel();
|
---|
[18297] | 101 | control_pane.setComponentOrientation(Dictionary.getOrientation());
|
---|
[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));
|
---|
[18297] | 119 | value_panel.add(value_label, BorderLayout.LINE_START);
|
---|
[5058] | 120 | value_panel.add(value_field, BorderLayout.CENTER);
|
---|
| 121 |
|
---|
| 122 | text_pane.setLayout(new BorderLayout());
|
---|
[18297] | 123 | text_pane.add(icon_label, BorderLayout.LINE_START);
|
---|
[5058] | 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());
|
---|
[18297] | 136 | control_pane.add(ok_button, BorderLayout.LINE_END);
|
---|
[5058] | 137 | }
|
---|
| 138 |
|
---|
| 139 | bottom_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
|
---|
| 140 | bottom_pane.setLayout(new BorderLayout());
|
---|
| 141 | bottom_pane.add(show_check, BorderLayout.CENTER);
|
---|
[18297] | 142 | bottom_pane.add(control_pane, BorderLayout.LINE_END);
|
---|
[5058] | 143 |
|
---|
| 144 | content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
| 145 | content_pane.setLayout(new BorderLayout());
|
---|
| 146 | content_pane.add(text_pane, BorderLayout.CENTER);
|
---|
| 147 | content_pane.add(bottom_pane, BorderLayout.SOUTH);
|
---|
[5564] | 148 |
|
---|
[5058] | 149 | // Position
|
---|
| 150 | Dimension size = getSize();
|
---|
[5564] | 151 | if (Gatherer.g_man != null) {
|
---|
[5058] | 152 | Rectangle frame_bounds = Gatherer.g_man.getBounds();
|
---|
| 153 | setLocation(frame_bounds.x + (frame_bounds.width - size.width) / 2, frame_bounds.y + (frame_bounds.height - size.height) / 2);
|
---|
| 154 | }
|
---|
| 155 | else {
|
---|
| 156 | Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
|
---|
| 157 | setLocation((screen_size.width - size.width) / 2, (screen_size.height - size.height) / 2);
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | public void actionPerformed(ActionEvent event) {
|
---|
[5164] | 162 | boolean bad_value = false;
|
---|
[5058] | 163 | if(event.getSource() == ok_button) {
|
---|
[8231] | 164 | if(affected_property != null && Configuration.self != null) {
|
---|
[5164] | 165 | String value = value_field.getText();
|
---|
[5665] | 166 | if(value.length() > 0) {
|
---|
| 167 | if(value_field instanceof URLField) {
|
---|
| 168 | bad_value = !((URLField)value_field).validateURL();
|
---|
| 169 | }
|
---|
| 170 | if(!bad_value) {
|
---|
| 171 | // Store the value of the property
|
---|
[8231] | 172 | Configuration.setString(affected_property, true, value_field.getText());
|
---|
[5665] | 173 | }
|
---|
[5164] | 174 | }
|
---|
| 175 | }
|
---|
| 176 | if(!bad_value) {
|
---|
| 177 | result = JOptionPane.OK_OPTION;
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 | if(!bad_value) {
|
---|
[8231] | 181 | if (Configuration.self != null) {
|
---|
[5164] | 182 | // Store the state of the show message checkbox.
|
---|
[8231] | 183 | Configuration.set(full_property, true, !show_check.isSelected());
|
---|
[5058] | 184 | }
|
---|
[5164] | 185 | // Done.
|
---|
[6155] | 186 | setVisible(false);
|
---|
[5058] | 187 | }
|
---|
[5164] | 188 | else {
|
---|
[5593] | 189 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("WarningDialog.Invalid_Value"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
[5058] | 190 | }
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[5185] | 193 | /** Gives notification of key events on the buttons */
|
---|
| 194 | public void keyPressed(KeyEvent e) {
|
---|
| 195 | if (e.getKeyCode() == KeyEvent.VK_ENTER) {
|
---|
| 196 | // Enter: Click the button in focus
|
---|
| 197 | Object source = e.getSource();
|
---|
| 198 | if (source instanceof AbstractButton) {
|
---|
| 199 | ((AbstractButton) source).doClick();
|
---|
| 200 | }
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | public void keyReleased(KeyEvent e) { }
|
---|
| 205 |
|
---|
| 206 | public void keyTyped(KeyEvent e) { }
|
---|
| 207 |
|
---|
| 208 |
|
---|
[5058] | 209 | public int display() {
|
---|
[8231] | 210 | if (Configuration.self == null || Configuration.get(full_property, false)) {
|
---|
[6155] | 211 | setVisible(true);
|
---|
[5058] | 212 | }
|
---|
| 213 | // We are no longer showing this dialog, so result must always be true.
|
---|
| 214 | else {
|
---|
| 215 | result = JOptionPane.OK_OPTION;
|
---|
| 216 | }
|
---|
[6538] | 217 | // Ask the parent to repaint, just to be sure
|
---|
| 218 | if(Gatherer.g_man != null) {
|
---|
| 219 | Gatherer.g_man.repaint();
|
---|
| 220 | }
|
---|
[5058] | 221 | return result;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
[6322] | 224 | /** Allows you to specify whether this dialog is a warning dialog, or only a message dialog.
|
---|
| 225 | * @param message_only true if this dialog shows a message, false for a warning
|
---|
| 226 | */
|
---|
| 227 | public void setMessageOnly(boolean message_only) {
|
---|
| 228 | // If this is a message then change the checkbox
|
---|
| 229 | if(message_only) {
|
---|
[12119] | 230 | show_check.setText(Dictionary.get("WarningDialog.Dont_Show_Again_Message"));
|
---|
[6322] | 231 | }
|
---|
| 232 | // And if its a warning, change them back
|
---|
| 233 | else {
|
---|
[12119] | 234 | show_check.setText(Dictionary.get("WarningDialog.Dont_Show_Again"));
|
---|
[6322] | 235 | }
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[5164] | 238 | /** 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.
|
---|
| 239 | * @param control the JTextField subclass you want to use for the control
|
---|
| 240 | */
|
---|
| 241 | public void setValueField(JTextField control) {
|
---|
| 242 | // Remove the current control
|
---|
| 243 | value_panel.remove(value_field);
|
---|
| 244 | // Replace with the new one
|
---|
| 245 | value_field = control;
|
---|
| 246 | // Re-add
|
---|
| 247 | value_panel.add(value_field, BorderLayout.CENTER);
|
---|
| 248 | }
|
---|
[6155] | 249 |
|
---|
| 250 | protected void processWindowEvent(WindowEvent event) {
|
---|
| 251 | if(event.getID() == WindowEvent.WINDOW_ACTIVATED) {
|
---|
| 252 | if(affected_property != null) {
|
---|
| 253 | value_field.requestFocus();
|
---|
| 254 | }
|
---|
| 255 | else {
|
---|
| 256 | ok_button.requestFocus();
|
---|
| 257 | }
|
---|
| 258 | }
|
---|
| 259 | else {
|
---|
| 260 | super.processWindowEvent(event);
|
---|
| 261 | }
|
---|
| 262 | }
|
---|
[14567] | 263 |
|
---|
| 264 | public void setValueField(String field_value){
|
---|
| 265 | this.value_field.setText(field_value);
|
---|
| 266 | }
|
---|
| 267 |
|
---|
[5058] | 268 | }
|
---|