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

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

An "Import Collections" action text item is automatically generated for the Manage Collections frameset, so the user doesn't have to add this item themselves.

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