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: Shaoqun Wu, 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 | import javax.swing.event.*;
|
---|
43 | import java.util.ArrayList;
|
---|
44 | import java.util.HashMap;
|
---|
45 | import java.util.Collection;
|
---|
46 | import java.util.Vector;
|
---|
47 | import java.io.File;
|
---|
48 |
|
---|
49 |
|
---|
50 | import org.greenstone.gatherer.Configuration;
|
---|
51 | import org.greenstone.gatherer.Dictionary;
|
---|
52 | import org.greenstone.gatherer.gui.ModalDialog;
|
---|
53 | import org.greenstone.gatherer.gui.GLIButton;
|
---|
54 |
|
---|
55 | public class OpenMetadataSetPrompt
|
---|
56 | extends ModalDialog {
|
---|
57 |
|
---|
58 | static private Dimension SIZE = new Dimension(500, 500);
|
---|
59 |
|
---|
60 | private ArrayList available_metadata_sets;
|
---|
61 | private ArrayList listeners;
|
---|
62 |
|
---|
63 | private JButton open_button = null;
|
---|
64 | private JButton cancel_button = null;
|
---|
65 | private JButton browse_button = null;
|
---|
66 | private JList available_set_list = null;
|
---|
67 | private OpenMetadataSetPrompt self;
|
---|
68 | private MetadataSetManager meta_manager;
|
---|
69 | private JTextArea description_textarea = null;
|
---|
70 |
|
---|
71 | private boolean cancelled = false;
|
---|
72 |
|
---|
73 | public OpenMetadataSetPrompt(Frame parent,MetadataSetManager msm) {
|
---|
74 | super(parent, true);
|
---|
75 |
|
---|
76 | self = this;
|
---|
77 | meta_manager = msm;
|
---|
78 | setSize(SIZE);
|
---|
79 | setTitle(Dictionary.get("GEMS.OpenMetadataSetPrompt.Title"));
|
---|
80 | listeners = new ArrayList();
|
---|
81 |
|
---|
82 | JPanel content_pane = (JPanel) getContentPane();
|
---|
83 | content_pane.setOpaque(true);
|
---|
84 | content_pane.setComponentOrientation(Dictionary.getOrientation());
|
---|
85 |
|
---|
86 | JLabel available_metadata_sets_label = new JLabel(Dictionary.get("GEMS.OpenMetadataSetPrompt.Available_Sets"));
|
---|
87 | available_metadata_sets_label.setOpaque(true);
|
---|
88 | available_metadata_sets_label.setComponentOrientation(Dictionary.getOrientation());
|
---|
89 |
|
---|
90 | available_set_list = new JList();
|
---|
91 | available_set_list.setCellRenderer(new MetadatSetListCellRenderer());
|
---|
92 | available_set_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
93 | available_set_list.setFixedCellHeight(20);
|
---|
94 | available_set_list.addListSelectionListener(new MetadataSetListSelectionListener());
|
---|
95 | available_set_list.setComponentOrientation(Dictionary.getOrientation());
|
---|
96 |
|
---|
97 | JPanel set_pane = new JPanel();
|
---|
98 | set_pane.setComponentOrientation(Dictionary.getOrientation());
|
---|
99 | set_pane.setLayout(new BorderLayout());
|
---|
100 | set_pane.add(available_metadata_sets_label,BorderLayout.NORTH);
|
---|
101 | set_pane.add(new JScrollPane(available_set_list),BorderLayout.CENTER);
|
---|
102 |
|
---|
103 | JLabel metadata_set_des_label = new JLabel(Dictionary.get("GEMS.Set_Description"));
|
---|
104 | metadata_set_des_label.setOpaque(true);
|
---|
105 | metadata_set_des_label.setComponentOrientation(Dictionary.getOrientation());
|
---|
106 |
|
---|
107 | description_textarea = new JTextArea();
|
---|
108 | description_textarea.setOpaque(true);
|
---|
109 | description_textarea.setEditable(false);
|
---|
110 | description_textarea.setLineWrap(true);
|
---|
111 | description_textarea.setWrapStyleWord(true);
|
---|
112 | description_textarea.setComponentOrientation(Dictionary.getOrientation());
|
---|
113 |
|
---|
114 | JPanel des_pane = new JPanel();
|
---|
115 | des_pane.setLayout(new BorderLayout());
|
---|
116 | des_pane.add(metadata_set_des_label,BorderLayout.NORTH);
|
---|
117 | des_pane.add(new JScrollPane(description_textarea),BorderLayout.CENTER);
|
---|
118 | des_pane.setComponentOrientation(Dictionary.getOrientation());
|
---|
119 |
|
---|
120 | JPanel button_pane = new JPanel();
|
---|
121 | button_pane.setComponentOrientation(Dictionary.getOrientation());
|
---|
122 |
|
---|
123 | open_button = new GLIButton(Dictionary.get("GEMS.OpenMetadataSetPrompt.Open"), Dictionary.get("GEMS.OpenMetadataSetPrompt.Open_Tooltip"));
|
---|
124 | open_button.setEnabled(true);
|
---|
125 |
|
---|
126 | browse_button = new GLIButton(Dictionary.get("General.Browse"));
|
---|
127 | browse_button.setEnabled(true);
|
---|
128 |
|
---|
129 | cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Cancel_Tooltip"));
|
---|
130 | cancel_button.setEnabled(true);
|
---|
131 |
|
---|
132 |
|
---|
133 | // Add listeners
|
---|
134 | open_button.addActionListener(new ActionListener() {
|
---|
135 | public void actionPerformed(ActionEvent event) {
|
---|
136 | notifyListeners();
|
---|
137 | self.dispose();
|
---|
138 | }
|
---|
139 | });
|
---|
140 |
|
---|
141 | browse_button.addActionListener(new ActionListener() {
|
---|
142 | public void actionPerformed(ActionEvent event) {
|
---|
143 | String collect_dir = meta_manager.getCollectionPath();
|
---|
144 | JFileChooser file_chooser = new JFileChooser(new File(collect_dir));
|
---|
145 | file_chooser.setMultiSelectionEnabled(false);
|
---|
146 | int result = file_chooser.showOpenDialog(null);
|
---|
147 | if (result == JFileChooser.APPROVE_OPTION){
|
---|
148 | String file_path = file_chooser.getSelectedFile().toString();
|
---|
149 | MetadataSetInfo meta_info = meta_manager.getMetadataSet(file_path);
|
---|
150 | meta_info.setNew(false);
|
---|
151 | MetadataSetEvent mse = new MetadataSetEvent(meta_info);
|
---|
152 | for(int i=0;i<listeners.size();i++){
|
---|
153 | MetadataSetListener msl = (MetadataSetListener)listeners.get(i);
|
---|
154 | msl.metadataSetChanged(mse);
|
---|
155 | }
|
---|
156 |
|
---|
157 | }
|
---|
158 | notifyListeners();
|
---|
159 | self.dispose();
|
---|
160 | }
|
---|
161 | });
|
---|
162 |
|
---|
163 |
|
---|
164 |
|
---|
165 | cancel_button.addActionListener(new ActionListener() {
|
---|
166 | public void actionPerformed(ActionEvent event) {
|
---|
167 | cancelled = true;
|
---|
168 | self.dispose();
|
---|
169 | }
|
---|
170 | });
|
---|
171 |
|
---|
172 |
|
---|
173 | button_pane.setLayout(new GridLayout(1,3));
|
---|
174 | button_pane.add(open_button);
|
---|
175 | button_pane.add(browse_button);
|
---|
176 | button_pane.add(cancel_button);
|
---|
177 |
|
---|
178 |
|
---|
179 | content_pane.setLayout(new BorderLayout());
|
---|
180 | content_pane.add(set_pane, BorderLayout.NORTH);
|
---|
181 | content_pane.add(des_pane, BorderLayout.CENTER);
|
---|
182 | content_pane.add(button_pane, BorderLayout.SOUTH);
|
---|
183 |
|
---|
184 | // Show
|
---|
185 | Dimension screen_size = Configuration.screen_size;
|
---|
186 | setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
|
---|
187 | setVisible(false);
|
---|
188 |
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | public void display(){
|
---|
193 | cancelled = false;
|
---|
194 | available_metadata_sets = meta_manager.getAvailableMetadataSets();
|
---|
195 | available_set_list.setListData(new Vector((Collection)available_metadata_sets));
|
---|
196 | setVisible(true);
|
---|
197 | }
|
---|
198 |
|
---|
199 | public void addMetadataSetListener(MetadataSetListener msl){
|
---|
200 | listeners.add(msl);
|
---|
201 |
|
---|
202 | }
|
---|
203 |
|
---|
204 | public boolean isCancelled() {
|
---|
205 | return cancelled;
|
---|
206 | }
|
---|
207 |
|
---|
208 | private void notifyListeners(){
|
---|
209 | Object selectedValue = available_set_list.getSelectedValue();
|
---|
210 |
|
---|
211 | if (selectedValue !=null && (selectedValue instanceof MetadataSetInfo)){
|
---|
212 | MetadataSetEvent mse = new MetadataSetEvent((MetadataSetInfo)selectedValue);
|
---|
213 | for(int i=0;i<listeners.size();i++){
|
---|
214 | MetadataSetListener msl = (MetadataSetListener)listeners.get(i);
|
---|
215 | msl.metadataSetChanged(mse);
|
---|
216 | }
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | public void openMetadataSet(String metadata_path){
|
---|
221 | MetadataSetInfo set_info = meta_manager.getMetadataSet(metadata_path);
|
---|
222 | notifyListeners(set_info);
|
---|
223 | }
|
---|
224 |
|
---|
225 | private void notifyListeners(MetadataSetInfo set_info){
|
---|
226 | MetadataSetEvent mse = new MetadataSetEvent(set_info);
|
---|
227 | for(int i=0;i<listeners.size();i++){
|
---|
228 | MetadataSetListener msl = (MetadataSetListener)listeners.get(i);
|
---|
229 | msl.metadataSetChanged(mse);
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | private class MetadatSetListCellRenderer extends JLabel implements ListCellRenderer {
|
---|
234 | public MetadatSetListCellRenderer() {
|
---|
235 | setOpaque(true);
|
---|
236 | }
|
---|
237 |
|
---|
238 | public Component getListCellRendererComponent(
|
---|
239 | JList list,
|
---|
240 | Object value,
|
---|
241 | int index,
|
---|
242 | boolean isSelected,
|
---|
243 | boolean cellHasFocus)
|
---|
244 | {
|
---|
245 | String name= "unknown";
|
---|
246 |
|
---|
247 | if (value instanceof MetadataSetInfo){
|
---|
248 | MetadataSetInfo meta_info = (MetadataSetInfo) value;
|
---|
249 | name = meta_info.getMetadataSetName(); //get the name of metadata set
|
---|
250 | }
|
---|
251 |
|
---|
252 | setText(name);
|
---|
253 | if (isSelected) {
|
---|
254 | setBackground(list.getSelectionBackground());
|
---|
255 | setForeground(list.getSelectionForeground());
|
---|
256 | }
|
---|
257 | else {
|
---|
258 | setBackground(list.getBackground());
|
---|
259 | setForeground(list.getForeground());
|
---|
260 | }
|
---|
261 |
|
---|
262 | return this;
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 |
|
---|
267 | private class MetadataSetListSelectionListener implements ListSelectionListener {
|
---|
268 | public void valueChanged(ListSelectionEvent lse){
|
---|
269 | if (lse.getValueIsAdjusting()) return;
|
---|
270 |
|
---|
271 | Object selectedValue = available_set_list.getSelectedValue();
|
---|
272 |
|
---|
273 | if (selectedValue !=null && (selectedValue instanceof MetadataSetInfo)){
|
---|
274 | MetadataSetInfo meta_info = (MetadataSetInfo)selectedValue;
|
---|
275 | String des = meta_info.getMetadataSetDescription(); //get the description of the metadata set
|
---|
276 | description_textarea.setText(des);
|
---|
277 | }
|
---|
278 |
|
---|
279 | }
|
---|
280 | }
|
---|
281 |
|
---|
282 |
|
---|
283 | }
|
---|