source: other-projects/GlamED/trunk/src/org/honours/Main.java@ 26588

Last change on this file since 26588 was 26588, checked in by davidb, 11 years ago

Initial import of Korii's 520 project for managing digital cultural collections from Greenstone in Expeditee.

File size: 11.2 KB
Line 
1package org.honours;
2
3import java.awt.Point;
4import java.io.File;
5import java.util.HashSet;
6import java.util.Set;
7import java.util.concurrent.CopyOnWriteArrayList;
8
9import javax.swing.SwingUtilities;
10
11import org.expeditee.actions.Actions;
12import org.expeditee.gui.*;
13import org.expeditee.items.*;
14
15import org.honours.collection.*;
16import org.honours.greenstone.ArchiveFileReader;
17import org.honours.gui.*;
18
19/**
20 * Main class for the cultural collections
21 * management system.
22 * @author Korii
23 */
24public class Main {
25
26 public static CopyOnWriteArrayList<Collection> _collections = new CopyOnWriteArrayList<Collection>();
27
28 public static final String SYSTEM_FRAMESET_NAME = "Honours";
29
30 //TODO: Write a script to set up an environment variable which lasts during runtime.
31 public static final String GSDL_HOME = "C:\\Research\\Pei-Jones-Taonga\\expeditee-svn\\images\\greenstone3-svn4\\web";
32
33 //
34 public static final String GSDL_COLLECT_PATH = GSDL_HOME + File.separator + "sites" + File.separator + "localsite" +
35 File.separator + "collect" + File.separator;
36
37 public static Frame _profile;
38
39 //position of assocfilepath annotation on the frame
40 public static final int _assocfilePathXpos = 175;
41 public static final int _assocfilePathYpos = 101;
42
43 /**
44 * Initialize settings and other things for the system.
45 */
46 public static void initialize(){
47
48 SwingUtilities.invokeLater(new Runnable(){
49
50 public void run(){
51 loadSettings();
52 }
53 });
54
55 }
56
57 /**
58 * Load settings required for the system.
59 */
60 private static void loadSettings(){
61
62 MessageBay.displayMessage("Initializing System...");
63
64 Frame profile = FrameIO.LoadProfile(SYSTEM_FRAMESET_NAME);
65
66 if(profile == null){
67
68 try{
69 profile = FrameIO.CreateNewProfile(SYSTEM_FRAMESET_NAME);
70 }catch(Exception e){
71 e.printStackTrace();
72 MessageBay.errorMessage("ERROR: Unable to load profile.");
73 return;
74 }
75 }
76
77 assert(profile != null);
78
79 _profile = profile;
80
81 if(DisplayIO.getCurrentFrame() != _profile)
82 DisplayIO.setCurrentFrame(_profile, true);
83
84 //Set up main overlay display
85 Interfaces.setUpMainOverlay(_profile);
86
87 UserSettings.UserName = "honours";
88
89 initializeMainFramesets();
90
91 addActionsAndAgents();
92 addCustomMouseActions();
93 addCustomKeyboardActions();
94
95 try{
96 setUpCollectionObjects();
97 }catch(Exception e){
98 e.printStackTrace();
99
100 MessageBay.errorMessage("ERROR: Unable to load collection objects.");
101 return;
102 }
103
104 MessageBay.displayMessage("System initialized and is now ready for use.");
105 System.err.println("Collections available: " + _collections.size());
106
107 if(_collections.size() > 0){
108 System.err.println("*** " + _collections.get(0).getItems().size());
109 }
110 }
111
112 /**
113 * Set up collection objects which last for entire runtime.
114 * A "Collections" directory has been specially created where
115 * collection framesets from GSDL are kept. This method will only
116 * search this directory for collections (and not any other directories
117 * where framesets are kept).
118 * @throws Exception
119 */
120 private static void setUpCollectionObjects() throws Exception{
121
122 //File collectionsDir = new File(HonoursFrameIO.COLLECTIONS_PATH);
123
124 File collectionsDir = new File(FrameIO.FRAME_PATH);
125
126 if(collectionsDir.isDirectory()){
127
128 //return list of folders in collections directory
129 File[] folders = collectionsDir.listFiles();
130
131 for(File f : folders){
132
133 if(!f.isDirectory())
134 continue;
135
136 String fName = f.getName();
137
138 if(fName.equals("collectionsmain") || fName.equals("exhibitionsmain") || fName.equals("interface-overlays") || fName.equals("notesmain") || fName.equals("documentation")){
139 continue;
140 }
141
142 Collection collect = obtainCollectionItems(fName);
143
144 if(collect.getItems().size() == 0)
145 continue;
146
147 _collections.add(collect);
148
149 }
150
151 for(Collection c : _collections){
152
153 for(CollectionItem cItem : c.getItems()){
154 Interfaces.setUpToolbarOverlay(cItem);
155 Interfaces.setUpToggleToolbarOverlay(cItem);
156 Interfaces.setUpNotesOverlay(cItem);
157 //Interfaces.setUpBox(cItem);
158 }
159 }
160
161 MessageBay.displayMessage("Interfaces for collection items initialized.");
162 }
163
164 }
165
166 /**
167 * This method obtains all frames in a "collection" frameset
168 * and converts them to collection item objects. These items are
169 * then added to a collection object.
170 * @param collectName
171 * @return
172 */
173 public static Collection obtainCollectionItems(String collectName){
174
175 Collection collect = new Collection(collectName);
176 CollectionInformation cInfo = new CollectionInformation();
177
178 //Having an issue here when running ImportCollections agent.
179 //int last = FrameIO.LoadLast(collectName,HonoursFrameIO.COLLECTIONS_PATH).getNumber();
180 int last = FrameIO.getLastNumber(collectName);
181
182 //TODO: Add content layering stuff back in once I get just normal
183 //loading of collection items working.
184 for(int i = 1; i <= last; i++){
185
186 //Frame curr = FrameIO.LoadFrame(collectName + i, HonoursFrameIO.COLLECTIONS_PATH);
187 Frame curr = FrameIO.LoadFrame(collectName + i);
188
189 if(curr == null)
190 continue;
191
192 String assoc = getAssocFilePath(curr);
193
194 if(assoc == null)
195 continue;
196
197 CollectionItem cItem = new CollectionItem();
198 cItem.setFrame(curr);
199
200 cInfo.addRow("I", curr.getName(), assoc);
201
202 String docXML = GSDL_COLLECT_PATH + collectName + File.separator + "archives" + File.separator + assoc + File.separator + "doc.xml";
203
204 cItem.setDocXML(docXML);
205
206 ArchiveFileReader afr = new ArchiveFileReader(docXML);
207
208 //System.err.println("*** assoc = " + assoc);
209 //System.err.println("*** docXML = " + docXML);
210
211 String[] splitAssocFileName = afr.obtainElementText("gsdlassocfile").split(":");
212
213 String assocFileName = GSDL_COLLECT_PATH + collectName + File.separator +
214 "index" + File.separator + "assoc" + File.separator + assoc + File.separator + splitAssocFileName[0];
215
216 cItem.setAssocfilePath(assoc);
217 cItem.setAssociatedFile(assocFileName);
218 //setInExpeditee(docXML);
219
220 collect.addItem(cItem);
221 }
222
223 cInfo.sort();
224 collect.setCollectionInformation(cInfo);
225 return collect;
226 }
227
228 /**
229 * This method extracts a text annotation on the passed frame
230 * which contains an assocfilepath metadata value
231 * (e.g. "@assocfilepath HASH0170.dir"). Each frame/collection item
232 * has its own unique assocfilepath value.
233 * TODO: There is a bug in GSDL that is affecting this code here... The
234 * assocfilepath metadata is being displayed as "HASH0170, HASH0170" for example
235 * when it should only be displayed as "HASH0170.
236 * @param curr - the frame to extract the assocfilepath text annotation from.
237 * @return - the assocfilepath value. If none is found, return null.
238 */
239 private static String getAssocFilePath(Frame curr){
240
241 String splitString = null;
242
243 for(Item item : curr.getItems()){
244 if(item instanceof Text){
245 Text text = (Text) item;
246
247 if(text.getText().startsWith("@assocfilepath")){
248
249 String[] split = text.getText().split(" ");
250
251 if(splitString == null){
252 splitString = split[1];
253
254 //remove a comma that may be at the end of the string.
255 if(splitString.endsWith(",")){
256 splitString = splitString.substring(0, splitString.length()-1);
257 }
258 }
259
260 //do this so that no one can edit/delete
261 //assocfilepath annotation.
262 item.setPermission(UserAppliedPermission.none);
263
264 //Shift item to position (3,129).
265 if(item.getX() != _assocfilePathXpos && item.getY() != _assocfilePathYpos)
266 item.setPosition(new Point(_assocfilePathXpos,_assocfilePathYpos));
267
268 if(item.getSize() != 14)
269 item.setSize(14);
270
271 curr.setSaved();
272 curr.setChanged(true);
273 }
274 }
275 }
276 return splitString;
277 }
278
279 /**
280 * Initialize (currently three) main framesets:
281 * collections,exhibitions,notes.
282 */
283 private static void initializeMainFramesets(){
284
285 //NOTE: CollectionsMain not to be confused with the collections
286 //directory where all collection framesets are stored.
287 String[] mainFramesetNames = new String[]{"CollectionsMain","ExhibitionsMain","NotesMain"};
288
289 //TODO: might be better just to check frameset directory
290 //instead of trying to load frames...
291 for(String s : mainFramesetNames){
292 Frame frame = FrameIO.LoadFrame(s + "1");
293
294 if(frame == null){
295 try{
296 FrameIO.CreateNewFrameset(s);
297 }catch(Exception e){
298 e.printStackTrace();
299 }
300 }
301 }
302
303 MessageBay.displayMessage("Main framesets initialized.");
304 }
305
306 /**
307 * Method to load and set up actions and agents for this system.
308 */
309 @SuppressWarnings("rawtypes")
310 private static void addActionsAndAgents(){
311
312 Actions.Init();
313
314 Set<String> agents = new HashSet<String>();
315 try{
316 for(Class c : Actions.getClasses("org.honours.agents.")){
317 if(c != null){
318 agents.add(c.getName());
319 }else{
320 throw new ClassNotFoundException("ERROR: No class found.");
321 }
322 }
323
324 }catch(ClassNotFoundException e){
325 e.printStackTrace();
326 }
327
328 Actions.addAgents(agents);
329
330 try{
331 for(Class c : Actions.getClasses("org.honours.actions.")){
332
333 if(c != null)
334 Actions.LoadMethods(c);
335 else
336 throw new ClassNotFoundException("ERROR: No class found.");
337 }
338 }catch(ClassNotFoundException e){
339 e.printStackTrace();
340 }
341
342 MessageBay.displayMessage("Actions and agents initialized.");
343 }
344
345 /**
346 * Method to add custom mouse actions for this system.
347 * These extend on Expedite's mouse actions.
348 */
349 private static void addCustomMouseActions(){
350
351 MouseActions mouseActions = MouseActions.getInstance();
352
353 Browser._theBrowser.getMouseEventRouter().removeExpediteeMouseListener(FrameMouseActions.getInstance());
354 Browser._theBrowser.getMouseEventRouter().addExpediteeMouseListener(mouseActions);
355 Browser._theBrowser.getMouseEventRouter().removeExpediteeMouseMotionListener(FrameMouseActions.getInstance());
356 Browser._theBrowser.getMouseEventRouter().addExpediteeMouseMotionListener(mouseActions);
357
358 MessageBay.displayMessage("Mouse actions initialized");
359 }
360
361 /**
362 * Method to add custom keyboard actions for this system.
363 * These extend on Expeditee's keyboard actions.
364 */
365 private static void addCustomKeyboardActions(){
366
367 KeyboardActions keyboardActions = KeyboardActions.getInstance();
368 Browser._theBrowser.removeKeyListener(FrameKeyboardActions.getInstance());
369 Browser._theBrowser.addKeyListener(keyboardActions);
370
371 Browser._theBrowser.getContentPane().removeKeyListener(FrameKeyboardActions.getInstance());
372 Browser._theBrowser.getContentPane().addKeyListener(keyboardActions);
373
374 MessageBay.displayMessage("Keyboard actions initialized");
375 }
376
377 /**
378 * Main entry point for this system.
379 * @param args - any arguments passed to this system will be passed to the Expeditee browser.
380 */
381 public static void main(final String[] args){
382
383 Browser.main(args);
384
385 SwingUtilities.invokeLater(new Runnable(){
386 public void run(){
387 initialize();
388 }
389 });
390 }
391
392}
Note: See TracBrowser for help on using the repository browser.