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

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

When loading in collection/collection item objects each frameset directory will be searched for a collection.inf file. If this file exists then the frameset is considered a GlamED collection.

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