[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);
|
---|
[5564] | 59 |
|
---|
[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();
|
---|
| 75 | JPanel text_pane = new JPanel();
|
---|
[10011] | 76 | JLabel icon_label = new JLabel(JarTools.getImage("gatherer_medium.gif"));
|
---|
[5564] | 77 |
|
---|
| 78 | JTextArea text_area = new JTextArea();
|
---|
| 79 | text_area.setEditable(false);
|
---|
[5058] | 80 | text_area.setLineWrap(true);
|
---|
[9864] | 81 | text_area.setText(warning_message);
|
---|
[12263] | 82 | text_area.setCaretPosition(0);
|
---|
[5058] | 83 | text_area.setWrapStyleWord(true);
|
---|
[5564] | 84 |
|
---|
[5164] | 85 | value_panel = new JPanel();
|
---|
[12119] | 86 | JLabel value_label = new JLabel(Dictionary.get("WarningDialog.Value"));
|
---|
| 87 |
|
---|
[5058] | 88 | value_field = new JTextField();
|
---|
| 89 | JPanel bottom_pane = new JPanel();
|
---|
[12119] | 90 | show_check = new JCheckBox(Dictionary.get("WarningDialog.Dont_Show_Again"));
|
---|
[5058] | 91 | JPanel control_pane = new JPanel();
|
---|
[12119] | 92 | ok_button = new GLIButton(Dictionary.get("General.OK"), Dictionary.get("General.OK_Tooltip"));
|
---|
| 93 | cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Pure_Cancel_Tooltip"));
|
---|
| 94 |
|
---|
[5058] | 95 | // Connection
|
---|
| 96 | ok_button.addActionListener(this);
|
---|
| 97 | cancel_button.addActionListener(this);
|
---|
[5185] | 98 | ok_button.addKeyListener(this);
|
---|
| 99 | cancel_button.addKeyListener(this);
|
---|
[6155] | 100 | getRootPane().setDefaultButton(ok_button);
|
---|
[5564] | 101 |
|
---|
[5058] | 102 | // Layout
|
---|
| 103 | icon_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
|
---|
| 104 |
|
---|
| 105 | value_label.setBorder(BorderFactory.createEmptyBorder(0, icon_label.getPreferredSize().width, 0, 0));
|
---|
| 106 |
|
---|
| 107 | value_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
|
---|
[7146] | 108 | value_panel.setLayout(new BorderLayout(5,0));
|
---|
[5058] | 109 | value_panel.add(value_label, BorderLayout.WEST);
|
---|
| 110 | value_panel.add(value_field, BorderLayout.CENTER);
|
---|
| 111 |
|
---|
| 112 | text_pane.setLayout(new BorderLayout());
|
---|
| 113 | text_pane.add(icon_label, BorderLayout.WEST);
|
---|
| 114 | text_pane.add(new JScrollPane(text_area), BorderLayout.CENTER);
|
---|
| 115 | if(affected_property != null) {
|
---|
| 116 | text_pane.add(value_panel, BorderLayout.SOUTH);
|
---|
| 117 | }
|
---|
[5564] | 118 |
|
---|
[5058] | 119 | if(can_cancel) {
|
---|
| 120 | control_pane.setLayout(new GridLayout(1,2,5,0));
|
---|
| 121 | control_pane.add(ok_button);
|
---|
| 122 | control_pane.add(cancel_button);
|
---|
| 123 | }
|
---|
| 124 | else {
|
---|
| 125 | control_pane.setLayout(new BorderLayout());
|
---|
| 126 | control_pane.add(ok_button, BorderLayout.EAST);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | bottom_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
|
---|
| 130 | bottom_pane.setLayout(new BorderLayout());
|
---|
| 131 | bottom_pane.add(show_check, BorderLayout.CENTER);
|
---|
| 132 | bottom_pane.add(control_pane, BorderLayout.EAST);
|
---|
| 133 |
|
---|
| 134 | content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
| 135 | content_pane.setLayout(new BorderLayout());
|
---|
| 136 | content_pane.add(text_pane, BorderLayout.CENTER);
|
---|
| 137 | content_pane.add(bottom_pane, BorderLayout.SOUTH);
|
---|
[5564] | 138 |
|
---|
[5058] | 139 | // Position
|
---|
| 140 | Dimension size = getSize();
|
---|
[5564] | 141 | if (Gatherer.g_man != null) {
|
---|
[5058] | 142 | Rectangle frame_bounds = Gatherer.g_man.getBounds();
|
---|
| 143 | setLocation(frame_bounds.x + (frame_bounds.width - size.width) / 2, frame_bounds.y + (frame_bounds.height - size.height) / 2);
|
---|
| 144 | }
|
---|
| 145 | else {
|
---|
| 146 | Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
|
---|
| 147 | setLocation((screen_size.width - size.width) / 2, (screen_size.height - size.height) / 2);
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | public void actionPerformed(ActionEvent event) {
|
---|
[5164] | 152 | boolean bad_value = false;
|
---|
[5058] | 153 | if(event.getSource() == ok_button) {
|
---|
[8231] | 154 | if(affected_property != null && Configuration.self != null) {
|
---|
[5164] | 155 | String value = value_field.getText();
|
---|
[14567] | 156 | System.out.println("-------- warningdialog value: "+value);
|
---|
[5665] | 157 | if(value.length() > 0) {
|
---|
| 158 | if(value_field instanceof URLField) {
|
---|
| 159 | bad_value = !((URLField)value_field).validateURL();
|
---|
[14567] | 160 | System.out.println("-------- warningdialog bad_value: "+bad_value);
|
---|
[5665] | 161 | }
|
---|
| 162 | if(!bad_value) {
|
---|
| 163 | // Store the value of the property
|
---|
[8231] | 164 | Configuration.setString(affected_property, true, value_field.getText());
|
---|
[14567] | 165 | System.out.println("-------- warningdialog affected_property: "+affected_property);
|
---|
| 166 | System.out.println("-------- warningdialog value_field.getText(): "+value_field.getText());
|
---|
| 167 | System.out.println("-------- warningdialog Configuration.getString(general.gliserver_url, true): "+Configuration.getString("general.gliserver_url", true));
|
---|
[5665] | 168 | }
|
---|
[5164] | 169 | }
|
---|
| 170 | }
|
---|
| 171 | if(!bad_value) {
|
---|
| 172 | result = JOptionPane.OK_OPTION;
|
---|
[14567] | 173 | System.out.println("-------- warningdialog result: "+result);
|
---|
[5164] | 174 | }
|
---|
| 175 | }
|
---|
| 176 | if(!bad_value) {
|
---|
[8231] | 177 | if (Configuration.self != null) {
|
---|
[5164] | 178 | // Store the state of the show message checkbox.
|
---|
[8231] | 179 | Configuration.set(full_property, true, !show_check.isSelected());
|
---|
[5058] | 180 | }
|
---|
[5164] | 181 | // Done.
|
---|
[6155] | 182 | setVisible(false);
|
---|
[5058] | 183 | }
|
---|
[5164] | 184 | else {
|
---|
[5593] | 185 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("WarningDialog.Invalid_Value"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
[5058] | 186 | }
|
---|
| 187 | }
|
---|
| 188 |
|
---|
[5185] | 189 | /** Gives notification of key events on the buttons */
|
---|
| 190 | public void keyPressed(KeyEvent e) {
|
---|
| 191 | if (e.getKeyCode() == KeyEvent.VK_ENTER) {
|
---|
| 192 | // Enter: Click the button in focus
|
---|
| 193 | Object source = e.getSource();
|
---|
| 194 | if (source instanceof AbstractButton) {
|
---|
| 195 | ((AbstractButton) source).doClick();
|
---|
| 196 | }
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | public void keyReleased(KeyEvent e) { }
|
---|
| 201 |
|
---|
| 202 | public void keyTyped(KeyEvent e) { }
|
---|
| 203 |
|
---|
| 204 |
|
---|
[5058] | 205 | public int display() {
|
---|
[8231] | 206 | if (Configuration.self == null || Configuration.get(full_property, false)) {
|
---|
[6155] | 207 | setVisible(true);
|
---|
[5058] | 208 | }
|
---|
| 209 | // We are no longer showing this dialog, so result must always be true.
|
---|
| 210 | else {
|
---|
| 211 | result = JOptionPane.OK_OPTION;
|
---|
| 212 | }
|
---|
[6538] | 213 | // Ask the parent to repaint, just to be sure
|
---|
| 214 | if(Gatherer.g_man != null) {
|
---|
| 215 | Gatherer.g_man.repaint();
|
---|
| 216 | }
|
---|
[5058] | 217 | return result;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
[6322] | 220 | /** Allows you to specify whether this dialog is a warning dialog, or only a message dialog.
|
---|
| 221 | * @param message_only true if this dialog shows a message, false for a warning
|
---|
| 222 | */
|
---|
| 223 | public void setMessageOnly(boolean message_only) {
|
---|
| 224 | // If this is a message then change the checkbox
|
---|
| 225 | if(message_only) {
|
---|
[12119] | 226 | show_check.setText(Dictionary.get("WarningDialog.Dont_Show_Again_Message"));
|
---|
[6322] | 227 | }
|
---|
| 228 | // And if its a warning, change them back
|
---|
| 229 | else {
|
---|
[12119] | 230 | show_check.setText(Dictionary.get("WarningDialog.Dont_Show_Again"));
|
---|
[6322] | 231 | }
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[5164] | 234 | /** 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.
|
---|
| 235 | * @param control the JTextField subclass you want to use for the control
|
---|
| 236 | */
|
---|
| 237 | public void setValueField(JTextField control) {
|
---|
| 238 | // Remove the current control
|
---|
| 239 | value_panel.remove(value_field);
|
---|
| 240 | // Replace with the new one
|
---|
| 241 | value_field = control;
|
---|
| 242 | // Re-add
|
---|
| 243 | value_panel.add(value_field, BorderLayout.CENTER);
|
---|
| 244 | }
|
---|
[6155] | 245 |
|
---|
| 246 | protected void processWindowEvent(WindowEvent event) {
|
---|
| 247 | if(event.getID() == WindowEvent.WINDOW_ACTIVATED) {
|
---|
| 248 | if(affected_property != null) {
|
---|
| 249 | value_field.requestFocus();
|
---|
| 250 | }
|
---|
| 251 | else {
|
---|
| 252 | ok_button.requestFocus();
|
---|
| 253 | }
|
---|
| 254 | }
|
---|
| 255 | else {
|
---|
| 256 | super.processWindowEvent(event);
|
---|
| 257 | }
|
---|
| 258 | }
|
---|
[14567] | 259 |
|
---|
| 260 | public void setValueField(String field_value){
|
---|
| 261 | this.value_field.setText(field_value);
|
---|
| 262 | }
|
---|
| 263 |
|
---|
[5058] | 264 | }
|
---|