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

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

Changed positioning of overlay link items.

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