[12660] | 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 | * <BR><BR>
|
---|
| 9 | *
|
---|
| 10 | * Author: Katherine Don, Greenstone Digital Library, University of Waikato
|
---|
| 11 | *
|
---|
| 12 | * <BR><BR>
|
---|
| 13 | *
|
---|
| 14 | * Copyright (C) 2006 New Zealand Digital Library Project
|
---|
| 15 | *
|
---|
| 16 | * <BR><BR>
|
---|
| 17 | *
|
---|
| 18 | * This program is free software; you can redistribute it and/or modify
|
---|
| 19 | * it under the terms of the GNU General Public License as published by
|
---|
| 20 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 21 | * (at your option) any later version.
|
---|
| 22 | *
|
---|
| 23 | * <BR><BR>
|
---|
| 24 | *
|
---|
| 25 | * This program is distributed in the hope that it will be useful,
|
---|
| 26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 28 | * GNU General Public License for more details.
|
---|
| 29 | *
|
---|
| 30 | * <BR><BR>
|
---|
| 31 | *
|
---|
| 32 | * You should have received a copy of the GNU General Public License
|
---|
| 33 | * along with this program; if not, write to the Free Software
|
---|
| 34 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 35 | *########################################################################
|
---|
| 36 | */
|
---|
| 37 | package org.greenstone.gatherer.gems;
|
---|
| 38 |
|
---|
| 39 | import java.awt.*;
|
---|
| 40 | import java.awt.event.*;
|
---|
| 41 | import javax.swing.*;
|
---|
| 42 |
|
---|
| 43 | import org.greenstone.gatherer.Configuration;
|
---|
| 44 | import org.greenstone.gatherer.Dictionary;
|
---|
| 45 | import org.greenstone.gatherer.gui.GLIButton;
|
---|
| 46 | import org.greenstone.gatherer.gui.ModalDialog;
|
---|
| 47 |
|
---|
| 48 | public class NewMetadataElementNamePrompt
|
---|
| 49 | extends ModalDialog {
|
---|
| 50 | private Dimension SIZE = new Dimension(350, 120);
|
---|
| 51 |
|
---|
| 52 | private boolean cancelled = false;
|
---|
| 53 | private String element_name = null;
|
---|
| 54 | private JTextField name_textfield = null;
|
---|
| 55 | private JDialog prompt;
|
---|
[12838] | 56 | private Object model;
|
---|
| 57 | public NewMetadataElementNamePrompt(Frame parent, boolean subelement, Object model) {
|
---|
[12660] | 58 | super(parent, true);
|
---|
| 59 | setSize(SIZE);
|
---|
| 60 | prompt = this;
|
---|
[12838] | 61 | this.model = model;
|
---|
[12660] | 62 | JPanel content_pane = (JPanel) getContentPane();
|
---|
| 63 | content_pane.setOpaque(true);
|
---|
| 64 |
|
---|
[12734] | 65 | JLabel name_label = new JLabel();
|
---|
| 66 | if (subelement) {
|
---|
| 67 | setTitle(Dictionary.get("GEMS.NewMetadataElementNamePrompt.SubTitle"));
|
---|
| 68 | name_label.setText(Dictionary.get("GEMS.NewMetadataElementNamePrompt.SubName"));
|
---|
| 69 | } else {
|
---|
| 70 | setTitle(Dictionary.get("GEMS.NewMetadataElementNamePrompt.Title"));
|
---|
| 71 | name_label.setText(Dictionary.get("GEMS.NewMetadataElementNamePrompt.Name"));
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[12660] | 74 | name_textfield = new JTextField();
|
---|
| 75 |
|
---|
| 76 | name_textfield.addKeyListener(new KeyAdapter() {
|
---|
| 77 | public void keyPressed(KeyEvent e) {
|
---|
| 78 | if (e.getKeyCode() == KeyEvent.VK_ENTER) {
|
---|
| 79 | //same as clicking OK button
|
---|
[12838] | 80 | validateElementName();
|
---|
[12660] | 81 | }
|
---|
| 82 | }
|
---|
| 83 | });
|
---|
| 84 |
|
---|
| 85 | JPanel details_pane = new JPanel();
|
---|
| 86 | details_pane.setLayout(new GridLayout(2,1));
|
---|
| 87 | details_pane.add(name_label);
|
---|
| 88 | details_pane.add(name_textfield);
|
---|
| 89 |
|
---|
| 90 | JPanel button_pane = new JPanel();
|
---|
| 91 | button_pane.setLayout(new GridLayout(1,2));
|
---|
| 92 | GLIButton ok_button = new GLIButton(Dictionary.get("General.OK"), Dictionary.get("General.OK_Tooltip"));
|
---|
| 93 | GLIButton cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Cancel_Tooltip"));
|
---|
| 94 | button_pane.add(ok_button);
|
---|
| 95 | button_pane.add(cancel_button);
|
---|
| 96 |
|
---|
| 97 | // Add listeners
|
---|
| 98 | ok_button.addActionListener(new ActionListener() {
|
---|
| 99 | public void actionPerformed(ActionEvent event) {
|
---|
[12838] | 100 | validateElementName();
|
---|
[12660] | 101 | }
|
---|
| 102 | });
|
---|
| 103 | cancel_button.addActionListener(new ActionListener() {
|
---|
| 104 | public void actionPerformed(ActionEvent event) {
|
---|
| 105 | cancelled = true;
|
---|
| 106 | prompt.dispose();
|
---|
| 107 | }
|
---|
| 108 | });
|
---|
| 109 |
|
---|
| 110 | content_pane.setLayout(new BorderLayout(5,5));
|
---|
| 111 | content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
| 112 | content_pane.add(details_pane, BorderLayout.CENTER);
|
---|
| 113 | content_pane.add(button_pane, BorderLayout.SOUTH);
|
---|
| 114 |
|
---|
| 115 | Dimension screen_size = Configuration.screen_size;
|
---|
| 116 | setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
|
---|
| 117 | setVisible(true);
|
---|
| 118 |
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | public String getName() {
|
---|
| 122 | return element_name;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | public boolean isCancelled() {
|
---|
| 126 | return cancelled;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[12838] | 129 | private void validateElementName() {
|
---|
| 130 | String name = name_textfield.getText();
|
---|
| 131 | if (name.equals("")) {
|
---|
| 132 | JOptionPane.showMessageDialog(prompt,Dictionary.get("GEMS.NewMetadataElementNamePrompt.EmptyName_Error_Message"), Dictionary.get("GEMS.NewMetadataElementNamePrompt.Name_Error"), JOptionPane.ERROR_MESSAGE);
|
---|
| 133 | return;
|
---|
| 134 | }
|
---|
| 135 | boolean already_used = false;
|
---|
| 136 | if (model instanceof MetadataSetModel) {
|
---|
| 137 | already_used = ((MetadataSetModel)model).doesChildWithThisNameExist(name);
|
---|
| 138 | } else if (model instanceof MetadataElementModel) {
|
---|
| 139 | already_used = ((MetadataElementModel)model).doesChildWithThisNameExist(name);
|
---|
| 140 | }
|
---|
| 141 | if (!already_used) {
|
---|
| 142 | element_name = name;
|
---|
| 143 | prompt.dispose();
|
---|
| 144 | } else {
|
---|
| 145 | JOptionPane.showMessageDialog(prompt,Dictionary.get("GEMS.NewMetadataElementNamePrompt.Name_Error_Message"), Dictionary.get("GEMS.NewMetadataElementNamePrompt.Name_Error"), JOptionPane.ERROR_MESSAGE);
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
[12660] | 148 | }
|
---|