package org.greenstone.gatherer.gui; import java.awt.*; import java.awt.event.*; import javax.swing.*; import org.greenstone.gatherer.Configuration; import org.greenstone.gatherer.Dictionary; import org.greenstone.gatherer.Gatherer; import org.greenstone.gatherer.util.FocusChangerTask; import org.greenstone.gatherer.util.Utility; /** Warn a user that they are about to add folder level metadata. */ public class WarningDialog extends JDialog implements ActionListener, KeyListener { static final private Dimension LABEL_SIZE = new Dimension(150, 25); static final private Dimension NORMAL_SIZE = new Dimension(400, 160); static final private Dimension SETTING_SIZE = new Dimension(400, 200); private Dictionary dictionary; private int result = JOptionPane.CANCEL_OPTION; private JButton cancel_button; private JButton ok_button; private JCheckBox show_check; private JTextField value_field; private JPanel value_panel; private String affected_property; private String full_property; private String warning_name; public WarningDialog(String full_property) { this(full_property, false, null, Gatherer.dictionary); } public WarningDialog(String full_property, boolean can_cancel) { this(full_property, can_cancel, null, Gatherer.dictionary); } public WarningDialog(String full_property, String affected_property) { this(full_property, false, affected_property, Gatherer.dictionary); } public WarningDialog(String full_property, boolean can_cancel, Dictionary dictionary) { this(full_property, can_cancel, null, dictionary); } public WarningDialog(String full_property, String affected_feature, Dictionary dictionary) { this(full_property, false, affected_feature, dictionary); } public WarningDialog(String full_property, boolean can_cancel, String affected_property) { this(full_property, can_cancel, affected_property, Gatherer.dictionary); } public WarningDialog(String full_property, boolean can_cancel, String affected_property, Dictionary dictionary) { super(Gatherer.g_man, "Warning", true); // Determine the name of this prompt. this.affected_property = affected_property; this.dictionary = dictionary; this.full_property = full_property; warning_name = full_property.substring(full_property.indexOf(".") + 1); // Now build dialog. if (affected_property != null) { setSize(SETTING_SIZE); } else { setSize(NORMAL_SIZE); } Dictionary.setText(this, warning_name + ".Title"); // Creation JPanel content_pane = (JPanel) getContentPane(); JPanel text_pane = new JPanel(); JLabel icon_label = new JLabel(Utility.getImage("gatherer_medium.gif")); JTextArea text_area = new JTextArea(); text_area.setCaretPosition(0); text_area.setEditable(false); text_area.setLineWrap(true); text_area.setWrapStyleWord(true); Dictionary.setText(text_area, warning_name + ".Message"); value_panel = new JPanel(); JLabel value_label = new JLabel(); value_label.setPreferredSize(LABEL_SIZE); Dictionary.setText(value_label, "WarningDialog.Value"); value_field = new JTextField(); JPanel bottom_pane = new JPanel(); show_check = new JCheckBox(); Dictionary.setText(show_check, "WarningDialog.Dont_Show_Again"); JPanel control_pane = new JPanel(); ok_button = new JButton(); Dictionary.setBoth(ok_button, "General.OK", "General.OK_Tooltip"); cancel_button = new JButton(); Dictionary.setBoth(cancel_button, "General.Cancel", "General.Pure_Cancel_Tooltip"); // Connection ok_button.addActionListener(this); cancel_button.addActionListener(this); ok_button.addKeyListener(this); cancel_button.addKeyListener(this); // Layout icon_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,5)); value_label.setBorder(BorderFactory.createEmptyBorder(0, icon_label.getPreferredSize().width, 0, 0)); value_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0)); value_panel.setLayout(new BorderLayout()); value_panel.add(value_label, BorderLayout.WEST); value_panel.add(value_field, BorderLayout.CENTER); text_pane.setLayout(new BorderLayout()); text_pane.add(icon_label, BorderLayout.WEST); text_pane.add(new JScrollPane(text_area), BorderLayout.CENTER); if(affected_property != null) { text_pane.add(value_panel, BorderLayout.SOUTH); } if(can_cancel) { control_pane.setLayout(new GridLayout(1,2,5,0)); control_pane.add(ok_button); control_pane.add(cancel_button); } else { control_pane.setLayout(new BorderLayout()); control_pane.add(ok_button, BorderLayout.EAST); } bottom_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0)); bottom_pane.setLayout(new BorderLayout()); bottom_pane.add(show_check, BorderLayout.CENTER); bottom_pane.add(control_pane, BorderLayout.EAST); content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); content_pane.setLayout(new BorderLayout()); content_pane.add(text_pane, BorderLayout.CENTER); content_pane.add(bottom_pane, BorderLayout.SOUTH); // Position Dimension size = getSize(); if (Gatherer.g_man != null) { Rectangle frame_bounds = Gatherer.g_man.getBounds(); setLocation(frame_bounds.x + (frame_bounds.width - size.width) / 2, frame_bounds.y + (frame_bounds.height - size.height) / 2); } else { Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize(); setLocation((screen_size.width - size.width) / 2, (screen_size.height - size.height) / 2); } } public void actionPerformed(ActionEvent event) { boolean bad_value = false; if(event.getSource() == ok_button) { if(affected_property != null && Gatherer.config != null) { String value = value_field.getText(); if(value.length() == 0) { bad_value = true; } else if(value_field instanceof URLField) { bad_value = !((URLField)value_field).validateURL(); } if(!bad_value) { // Store the value of the property Gatherer.config.setString(affected_property, true, value_field.getText()); } } if(!bad_value) { result = JOptionPane.OK_OPTION; } } if(!bad_value) { if(Gatherer.config != null) { // Store the state of the show message checkbox. Gatherer.config.set(full_property, true, !show_check.isSelected()); } // Done. dispose(); } else { JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("WarningDialog.Invalid_Value"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE); } } /** Gives notification of key events on the buttons */ public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER) { // Enter: Click the button in focus Object source = e.getSource(); if (source instanceof AbstractButton) { ((AbstractButton) source).doClick(); } } } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } public int display() { ///ystem.err.println("Show " + full_property + ": " + Gatherer.config.get(full_property, false)); if(Gatherer.config == null || Gatherer.config.get(full_property, false)) { // Create and run the shortlived move focus thread FocusChangerTask task = null; if(affected_property != null) { task = new FocusChangerTask(value_field); } else { task = new FocusChangerTask(ok_button); } task.start(); show(); } // We are no longer showing this dialog, so result must always be true. else { result = JOptionPane.OK_OPTION; } return result; } /** 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. * @param control the JTextField subclass you want to use for the control */ public void setValueField(JTextField control) { // Remove the current control value_panel.remove(value_field); // Replace with the new one value_field = control; // Re-add value_panel.add(value_field, BorderLayout.CENTER); } }