source: trunk/gli/src/org/greenstone/gatherer/cdm/SuperCollectionManager.java@ 4601

Last change on this file since 4601 was 4601, checked in by kjdon, 21 years ago

now the current coll is back in the list, but it is fixed and always selected

  • Property svn:keywords set to Author Date Id Revision
File size: 8.1 KB
Line 
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 */
27package org.greenstone.gatherer.cdm;
28
29import java.awt.*;
30import java.awt.event.*;
31import java.io.*;
32import java.util.*;
33import javax.swing.*;
34import org.greenstone.gatherer.Configuration;
35import org.greenstone.gatherer.Dictionary;
36import org.greenstone.gatherer.Gatherer;
37import org.greenstone.gatherer.checklist.CheckList;
38import org.greenstone.gatherer.checklist.Entry;
39import org.greenstone.gatherer.collection.CollectionConfiguration;
40import org.greenstone.gatherer.util.Utility;
41
42public class SuperCollectionManager {
43
44 static final public String SUPERCOLLECTION_COMMAND = "supercollection";
45 static final public String CCS_COMMAND = "ccs";
46
47 private String current_coll_name = null;
48 private Control controls;
49 private ArrayList collection_checklist_model;
50
51 SuperCollectionManager() {
52 // We start by building a model of the installed collections.
53 collection_checklist_model = new ArrayList();
54 File gsdl_collection_directory = new File(Utility.getCollectionDir(Gatherer.config.gsdl_path));
55 current_coll_name = Gatherer.c_man.getCollection().getName();
56 File[] possible_collections = gsdl_collection_directory.listFiles();
57 for(int i = 0; possible_collections != null && i < possible_collections.length; i++) {
58 // The simpliest case is if the directory etc/collect.cfg file and a metadata/ in it. Thus it can easily be built upon.
59 File collect_cfg_file = new File(possible_collections[i], Utility.CONFIG_DIR);
60 if(collect_cfg_file.exists()) {
61 CollectionConfiguration collect_cfg = new CollectionConfiguration(collect_cfg_file);
62 String collection_title = collect_cfg.getName();
63 String collection_name = possible_collections[i].getName();
64 // We do have to block the model collection.
65 if(!collection_title.equals("**title**")) {
66 Entry entry = new Entry(collection_title);
67 entry.setProperty(collection_name);
68 if (collection_name.equals(current_coll_name)) {
69 // the current collection is always selected
70 entry.setFixed(true);
71 entry.setSelected(true);
72 }
73 collection_checklist_model.add(entry);
74 entry = null;
75
76 }
77 collection_name = null;
78 collection_title = null;
79 collect_cfg = null;
80 }
81 collect_cfg_file = null;
82 }
83 possible_collections = null;
84 gsdl_collection_directory = null;
85 // Sort the result.
86 Collections.sort(collection_checklist_model);
87 }
88
89 public boolean parse(String command) {
90 String command_lc = command.toLowerCase();
91 if(command_lc.startsWith(SUPERCOLLECTION_COMMAND) || command_lc.startsWith(CCS_COMMAND)) {
92 StringTokenizer tokenizer = new StringTokenizer(command);
93 tokenizer.nextToken();
94 // Read in each of the collection names.
95 ArrayList collection_names = new ArrayList();
96 while(tokenizer.hasMoreTokens()) {
97 collection_names.add(tokenizer.nextToken());
98 }
99 // Now that we have the collection names, select any entries in the checklist that match.
100 for(int i = 0; i < collection_checklist_model.size(); i++) {
101 Entry entry = (Entry) collection_checklist_model.get(i);
102 String collection_name = entry.getProperty();
103 if(collection_names.contains(collection_name)) {
104 entry.setSelected(true);
105 collection_names.remove(collection_name); // Makes successive runs faster.
106 }
107 collection_name = null;
108 entry = null;
109 }
110 // Done.
111 return true;
112 }
113 return false;
114 }
115
116 /** Method to retrieve the control for this manager.
117 * @return A JPanel containing the controls.
118 */
119 public JPanel getControls() {
120 if(controls == null) {
121 controls = new Control();
122 }
123 return controls;
124 }
125
126 public void invalidateControls() {
127 controls = null;
128 }
129
130 /** Return the contents of this manager as a command or block of commands ready to be written to the collection configuration file.
131 * @return A String containing a configuration command or block of commands, terminated with a new line. This value will be null if there was no supercollection command set. */
132 public String toString() {
133 StringBuffer command = new StringBuffer(SUPERCOLLECTION_COMMAND);
134 // Now for each entry selected output its collection name (property setting)
135 boolean found_entry = false;
136 for(int i = 0; i < collection_checklist_model.size(); i++) {
137 Entry entry = (Entry) collection_checklist_model.get(i);
138 if(entry.isSelected()) {
139 if (!entry.getProperty().equals(current_coll_name)){
140 found_entry = true;
141 }
142 command.append(" ");
143 command.append(entry.getProperty());
144 }
145 entry = null;
146 }
147 command.append("\n");
148 command.append("\n");
149
150 // It is possible that there was only one entry - the current collection - in which case we return null.
151 if(found_entry) {
152 return command.toString();
153 }
154 else {
155 return null;
156 }
157 }
158
159 /** Call dictionary get with just a key.
160 * @param key A String which is mapped to a initial String within the ResourceBundle.
161 * @return A String which has been referenced by the key String and that either contains no argument fields, or has had the argument fields automatiically populated with formatting Strings of with argument String provided in the get call.
162 */
163 private String get(String key) {
164 if(key.indexOf(".") == -1) {
165 key = "CDM.SuperCollectionManager." + key;
166 }
167 return Gatherer.dictionary.get(key);
168 }
169
170 /** Provides controls for altering the SuperCollection settings. */
171 private class Control
172 extends JPanel {
173
174 private boolean init = true;
175 //private JSplitPane center_pane;
176
177 Control() {
178 super();
179 // Creation
180 JPanel header_panel = new JPanel();
181
182 JLabel title_label = new JLabel(get("Title"));
183 title_label.setHorizontalAlignment(JLabel.CENTER);
184 title_label.setOpaque(true);
185
186 //center_pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
187
188 JTextArea instructions = new JTextArea(get("Instructions"));
189 instructions.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
190 instructions.setCaretPosition(0);
191 instructions.setEditable(false);
192 instructions.setLineWrap(true);
193 instructions.setWrapStyleWord(true);
194
195 CheckList collection_checklist = new CheckList(collection_checklist_model);
196 // Layout
197 //center_pane.setTopComponent(new JScrollPane(instructions));
198 //center_pane.setBottomComponent(new JScrollPane(collection_checklist));
199 header_panel.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
200 header_panel.setLayout(new BorderLayout());
201 header_panel.add(title_label, BorderLayout.NORTH);
202 header_panel.add(new JScrollPane(instructions), BorderLayout.CENTER);
203
204 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
205 setLayout(new BorderLayout());
206 add(header_panel, BorderLayout.NORTH);
207 add(new JScrollPane(collection_checklist), BorderLayout.CENTER);
208 }
209
210 public void paint(Graphics g) {
211 super.paint(g);
212 // If this is the first time we've painted this control, then set the divider 1/3:2/3
213 //if(init) {
214 // init = false;
215 // center_pane.setDividerLocation(0.3);
216 // super.paint(g);
217 //}
218 }
219 }
220}
Note: See TracBrowser for help on using the repository browser.