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 Digital Library, University of Waikato
|
---|
9 | *
|
---|
10 | * Copyright (C) 1999 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 | */
|
---|
27 | package org.greenstone.gatherer.gui;
|
---|
28 |
|
---|
29 | import java.awt.*;
|
---|
30 | import java.awt.event.*;
|
---|
31 | import java.io.*;
|
---|
32 | import java.util.*;
|
---|
33 | import javax.swing.*;
|
---|
34 | import javax.swing.border.*;
|
---|
35 | import javax.swing.event.*;
|
---|
36 | import org.greenstone.gatherer.Configuration;
|
---|
37 | import org.greenstone.gatherer.Dictionary;
|
---|
38 | import org.greenstone.gatherer.Gatherer;
|
---|
39 | import org.greenstone.gatherer.metadata.MetadataElement;
|
---|
40 | import org.greenstone.gatherer.metadata.MetadataSet;
|
---|
41 | import org.greenstone.gatherer.metadata.MetadataSetManager;
|
---|
42 | import org.greenstone.gatherer.metadata.MetadataTools;
|
---|
43 | import org.greenstone.gatherer.util.Utility;
|
---|
44 |
|
---|
45 |
|
---|
46 | public class NewCollectionMetadataPrompt
|
---|
47 | extends ModalDialog {
|
---|
48 |
|
---|
49 | private boolean cancelled = true;
|
---|
50 | private JDialog self;
|
---|
51 | private JList elements_list;
|
---|
52 | private MetadataSetList sets_list;
|
---|
53 | static private Dimension size = new Dimension(700, 380);
|
---|
54 |
|
---|
55 | public NewCollectionMetadataPrompt() {
|
---|
56 | this(false);
|
---|
57 | }
|
---|
58 | public NewCollectionMetadataPrompt(boolean existing_coll) {
|
---|
59 | super(Gatherer.g_man, true);
|
---|
60 | this.self = this;
|
---|
61 | if (existing_coll) {
|
---|
62 | setJMenuBar(new SimpleMenuBar("openingacollection"));
|
---|
63 | Dictionary.setText(this, "NewCollectionPrompt.Metadata_Title_Existing");
|
---|
64 | } else {
|
---|
65 | setJMenuBar(new SimpleMenuBar("creatingacollection"));
|
---|
66 | Dictionary.setText(this, "NewCollectionPrompt.Title");
|
---|
67 | }
|
---|
68 | setModal(true);
|
---|
69 | setSize(size);
|
---|
70 |
|
---|
71 | // Show the metadata sets (except extracted!) available in the GLI "metadata" folder
|
---|
72 | ArrayList sets = MetadataSetManager.listMetadataSets(new File(Utility.METADATA_DIR));
|
---|
73 | ArrayList metadata_set_list_entries = new ArrayList();
|
---|
74 | for (int i = 0; i < sets.size(); i++) {
|
---|
75 | MetadataSet metadata_set = (MetadataSet) sets.get(i);
|
---|
76 |
|
---|
77 | // Don't show the extracted metadata set
|
---|
78 | if (metadata_set.getNamespace().equals(MetadataSetManager.EXTRACTED_METADATA_NAMESPACE)) {
|
---|
79 | continue;
|
---|
80 | }
|
---|
81 | // Hack: add dublin caore at the top
|
---|
82 | if (metadata_set.getNamespace().equals("dc")) {
|
---|
83 | metadata_set_list_entries.add(0, new MetadataSetListEntry(metadata_set));
|
---|
84 | } else {
|
---|
85 | metadata_set_list_entries.add(new MetadataSetListEntry(metadata_set));
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | // Creation
|
---|
90 | JPanel content_pane = (JPanel) getContentPane();
|
---|
91 |
|
---|
92 | JPanel instructions_panel = new JPanel();
|
---|
93 | JLabel instructions_label1 = new JLabel();
|
---|
94 | Dictionary.setText(instructions_label1, "NewCollectionPrompt.Metadata_Instructions1");
|
---|
95 | JLabel instructions_label2 = new JLabel();
|
---|
96 | Dictionary.setText(instructions_label2, "NewCollectionPrompt.Metadata_Instructions2");
|
---|
97 |
|
---|
98 | JPanel center_pane = new JPanel();
|
---|
99 |
|
---|
100 | JPanel sets_list_pane = new JPanel();
|
---|
101 | JLabel sets_list_label = new JLabel();
|
---|
102 | sets_list_label.setOpaque(false);
|
---|
103 | Dictionary.setText(sets_list_label, "NewCollectionPrompt.Select_MDS");
|
---|
104 | sets_list = new MetadataSetList();
|
---|
105 | sets_list.setListData(new Vector(metadata_set_list_entries));
|
---|
106 | sets_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
107 |
|
---|
108 | JPanel elements_list_pane = new JPanel();
|
---|
109 | JLabel elements_list_label = new JLabel();
|
---|
110 | Dictionary.setText(elements_list_label, "NewCollectionPrompt.Metadata_Elements");
|
---|
111 | elements_list = new JList();
|
---|
112 | elements_list.setCellRenderer(new MetadataElementListCellRenderer());
|
---|
113 | elements_list.setBackground(Configuration.getColor("coloring.collection_tree_background", false));
|
---|
114 | elements_list.setForeground(Configuration.getColor("coloring.collection_tree_foreground", false));
|
---|
115 | elements_list.setSelectionBackground(Configuration.getColor("coloring.collection_tree_background", false));
|
---|
116 | elements_list.setSelectionForeground(Configuration.getColor("coloring.collection_tree_foreground", false));
|
---|
117 |
|
---|
118 | // Select the Dublin Core metadata set by default, at Ian's request
|
---|
119 | sets_list.selectDublinCoreMetadataSet();
|
---|
120 |
|
---|
121 | JPanel button_pane = new JPanel();
|
---|
122 | JButton ok_button = new GLIButton();
|
---|
123 | ok_button.setMnemonic(KeyEvent.VK_O);
|
---|
124 | Dictionary.setBoth(ok_button, "General.OK", "General.OK_Tooltip");
|
---|
125 | JButton cancel_button = new GLIButton();
|
---|
126 | cancel_button.setMnemonic(KeyEvent.VK_C);
|
---|
127 | Dictionary.setBoth(cancel_button, "General.Cancel", "General.Pure_Cancel_Tooltip");
|
---|
128 |
|
---|
129 | // Connection
|
---|
130 | ok_button.addActionListener(new OKButtonListener());
|
---|
131 | cancel_button.addActionListener(new CancelButtonListener());
|
---|
132 |
|
---|
133 | // Display
|
---|
134 | instructions_panel.setLayout(new GridLayout(2,1));
|
---|
135 | instructions_panel.add(instructions_label1);
|
---|
136 | instructions_panel.add(instructions_label2);
|
---|
137 |
|
---|
138 | sets_list_pane.setLayout(new BorderLayout());
|
---|
139 | sets_list_pane.add(sets_list_label, BorderLayout.NORTH);
|
---|
140 | sets_list_pane.add(new JScrollPane(sets_list), BorderLayout.CENTER);
|
---|
141 |
|
---|
142 | elements_list_pane.setLayout(new BorderLayout());
|
---|
143 | elements_list_pane.add(elements_list_label, BorderLayout.NORTH);
|
---|
144 | elements_list_pane.add(new JScrollPane(elements_list), BorderLayout.CENTER);
|
---|
145 |
|
---|
146 | center_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
|
---|
147 | center_pane.setLayout(new GridLayout(2,1,0,5));
|
---|
148 | center_pane.add(sets_list_pane);
|
---|
149 | center_pane.add(elements_list_pane);
|
---|
150 |
|
---|
151 | button_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
|
---|
152 | button_pane.setLayout(new GridLayout(1,2,5,0));
|
---|
153 | button_pane.add(ok_button);
|
---|
154 | button_pane.add(cancel_button);
|
---|
155 |
|
---|
156 | content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
157 | content_pane.setLayout(new BorderLayout());
|
---|
158 | content_pane.add(instructions_panel, BorderLayout.NORTH);
|
---|
159 | content_pane.add(center_pane, BorderLayout.CENTER);
|
---|
160 | content_pane.add(button_pane, BorderLayout.SOUTH);
|
---|
161 |
|
---|
162 | // Show
|
---|
163 | Dimension screen_size = Configuration.screen_size;
|
---|
164 | setLocation((screen_size.width - size.width) / 2, (screen_size.height - size.height) / 2);
|
---|
165 | setVisible(true);
|
---|
166 | }
|
---|
167 |
|
---|
168 | public ArrayList getSets() {
|
---|
169 | return sets_list.getSelectedMetadataSets();
|
---|
170 | }
|
---|
171 |
|
---|
172 | public boolean isCancelled() {
|
---|
173 | return cancelled;
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | private class CancelButtonListener
|
---|
178 | implements ActionListener {
|
---|
179 | public void actionPerformed(ActionEvent event) {
|
---|
180 | cancelled = true;
|
---|
181 | self.dispose();
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | private class MetadataSetList
|
---|
187 | extends JList
|
---|
188 | {
|
---|
189 | public MetadataSetList()
|
---|
190 | {
|
---|
191 | super();
|
---|
192 | addListSelectionListener(new MetadataSetListSelectionListener());
|
---|
193 | addMouseListener(new MetadataSetListSelectionListener());
|
---|
194 | setCellRenderer(new MetadataSetListCellRenderer());
|
---|
195 | }
|
---|
196 |
|
---|
197 |
|
---|
198 | public ArrayList getSelectedMetadataSets()
|
---|
199 | {
|
---|
200 | ArrayList selected_metadata_sets = new ArrayList();
|
---|
201 |
|
---|
202 | ListModel model = (ListModel) getModel();
|
---|
203 | for (int i = 0; i < model.getSize(); i++) {
|
---|
204 | MetadataSetListEntry metadata_set_list_entry = (MetadataSetListEntry) model.getElementAt(i);
|
---|
205 | if (metadata_set_list_entry.isSelected()) {
|
---|
206 | selected_metadata_sets.add(metadata_set_list_entry.getMetadataSet());
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | return selected_metadata_sets;
|
---|
211 | }
|
---|
212 |
|
---|
213 |
|
---|
214 | /** Hack special-case code, used to select the Dublin Core metadata set by default */
|
---|
215 | public void selectDublinCoreMetadataSet()
|
---|
216 | {
|
---|
217 | ListModel model = (ListModel) getModel();
|
---|
218 | for (int i = 0; i < model.getSize(); i++) {
|
---|
219 | MetadataSetListEntry metadata_set_list_entry = (MetadataSetListEntry) model.getElementAt(i);
|
---|
220 | if (metadata_set_list_entry.getMetadataSet().getNamespace().equals("dc")) {
|
---|
221 | metadata_set_list_entry.setSelected(true);
|
---|
222 | metadata_set_list_entry.grabFocus();
|
---|
223 | setSelectedIndex(i);
|
---|
224 | break;
|
---|
225 | }
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | private class MetadataSetListCellRenderer
|
---|
231 | extends JLabel
|
---|
232 | implements ListCellRenderer {
|
---|
233 |
|
---|
234 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean is_selected, boolean cell_has_focus)
|
---|
235 | {
|
---|
236 | JCheckBox checkbox = (JCheckBox) value;
|
---|
237 | checkbox.setBackground(is_selected ? list.getSelectionBackground() : list.getBackground());
|
---|
238 | checkbox.setForeground(is_selected ? list.getSelectionForeground() : list.getForeground());
|
---|
239 | checkbox.setBorderPainted(true);
|
---|
240 | checkbox.setEnabled(list.isEnabled());
|
---|
241 | checkbox.setFont(list.getFont());
|
---|
242 | checkbox.setFocusPainted(false);
|
---|
243 | checkbox.setBorder((is_selected) ? UIManager.getBorder("List.focusCellHighlightBorder") : new EmptyBorder(1, 1, 1, 1));
|
---|
244 | return checkbox;
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 |
|
---|
249 | private class MetadataSetListSelectionListener
|
---|
250 | extends MouseAdapter
|
---|
251 | implements ListSelectionListener {
|
---|
252 |
|
---|
253 | public void mousePressed(MouseEvent e)
|
---|
254 | {
|
---|
255 | JList list = (JList) e.getSource();
|
---|
256 | int index = list.locationToIndex(e.getPoint());
|
---|
257 | MetadataSetListEntry metadata_set_list_entry = (MetadataSetListEntry) list.getModel().getElementAt(index);
|
---|
258 | metadata_set_list_entry.setSelected(!metadata_set_list_entry.isSelected());
|
---|
259 | metadata_set_list_entry.grabFocus();
|
---|
260 | list.ensureIndexIsVisible(index);
|
---|
261 |
|
---|
262 | // Fire a selection changed event
|
---|
263 | fireSelectionValueChanged(index, index, false);
|
---|
264 | }
|
---|
265 |
|
---|
266 | public void valueChanged(ListSelectionEvent event)
|
---|
267 | {
|
---|
268 | if (!sets_list.isSelectionEmpty()) {
|
---|
269 | // Retrieve the selected set
|
---|
270 | MetadataSetListEntry metadata_set_list_entry = (MetadataSetListEntry) sets_list.getSelectedValue();
|
---|
271 | MetadataSet set = (MetadataSet) metadata_set_list_entry.getMetadataSet();
|
---|
272 | elements_list.setListData(new Vector(set.getMetadataSetElements()));
|
---|
273 | }
|
---|
274 | else {
|
---|
275 | elements_list.setListData(new String[0]);
|
---|
276 | }
|
---|
277 | }
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 |
|
---|
282 | private class MetadataSetListEntry
|
---|
283 | extends JCheckBox
|
---|
284 | {
|
---|
285 | private MetadataSet metadata_set = null;
|
---|
286 |
|
---|
287 | public MetadataSetListEntry(MetadataSet metadata_set)
|
---|
288 | {
|
---|
289 | this.metadata_set = metadata_set;
|
---|
290 | }
|
---|
291 |
|
---|
292 | public MetadataSet getMetadataSet()
|
---|
293 | {
|
---|
294 | return metadata_set;
|
---|
295 | }
|
---|
296 |
|
---|
297 | public String getText()
|
---|
298 | {
|
---|
299 | if (metadata_set == null) {
|
---|
300 | return null;
|
---|
301 | }
|
---|
302 |
|
---|
303 | String interface_language_code = Configuration.getLanguage();
|
---|
304 | String metadata_set_name = MetadataTools.getMetadataSetAttribute(metadata_set, "Name", interface_language_code, "en");
|
---|
305 | return metadata_set_name + " (" + metadata_set.getNamespace() + ")";
|
---|
306 | }
|
---|
307 | }
|
---|
308 |
|
---|
309 |
|
---|
310 | private class OKButtonListener
|
---|
311 | implements ActionListener {
|
---|
312 | public void actionPerformed(ActionEvent event) {
|
---|
313 | // See if the user has selected no metadata sets, and if so confirm thats what they really want.
|
---|
314 | ArrayList selected_sets = sets_list.getSelectedMetadataSets();
|
---|
315 | cancelled = false;
|
---|
316 | if(selected_sets.size() == 0) {
|
---|
317 | WarningDialog dialog = new WarningDialog("warning.NoMetadataSetsSelected", null, true);
|
---|
318 | if(dialog.display() == JOptionPane.OK_OPTION) {
|
---|
319 | // Otherwise we are free to go
|
---|
320 | self.dispose();
|
---|
321 | }
|
---|
322 | dialog.dispose();
|
---|
323 | dialog = null;
|
---|
324 | }
|
---|
325 | else {
|
---|
326 | self.dispose();
|
---|
327 | }
|
---|
328 | }
|
---|
329 | }
|
---|
330 | }
|
---|