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

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

removing unnecessary debug print lines.

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