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

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

Added a method that is called during loading of the system which makes sure all text items with a user-specified width value are displayed on the frame correctly.

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