source: trunk/org/expeditee/gui/FrameIO.java@ 4

Last change on this file since 4 was 4, checked in by davidb, 16 years ago

Starting source code to Expeditee

File size: 24.0 KB
Line 
1package org.expeditee.gui;
2
3import java.io.BufferedReader;
4import java.io.BufferedWriter;
5import java.io.File;
6import java.io.FileReader;
7import java.io.FileWriter;
8import java.io.IOException;
9import java.util.HashMap;
10
11import org.expeditee.io.Conversion;
12import org.expeditee.io.ExpReader;
13import org.expeditee.io.ExpWriter;
14import org.expeditee.io.FrameReader;
15import org.expeditee.io.FrameWriter;
16import org.expeditee.io.KMSReader;
17import org.expeditee.io.KMSWriter;
18import org.expeditee.io.Logger;
19import org.expeditee.items.Item;
20import org.expeditee.items.ItemUtils;
21import org.expeditee.items.Text;
22import org.expeditee.stats.SessionStats;
23
24/**
25 * This class provides static methods for all saving and loading of Frames
26 * to\from disk. This class also handles any caching of previously loaded
27 * Frames.
28 *
29 * @author jdm18
30 *
31 */
32public class FrameIO {
33
34 public static void changeParentFolder(String newFolder) {
35 PARENT_FOLDER = newFolder;
36 FRAME_PATH = PARENT_FOLDER + "framesets" + File.separator;
37 IMAGES_PATH = PARENT_FOLDER + "images" + File.separator;
38 HELP_PATH = PARENT_FOLDER + "help" + File.separator;
39 PROFILE_PATH = PARENT_FOLDER + "profiles" + File.separator;
40 EXPORTS_DIR = PARENT_FOLDER + "exports" + File.separator;
41 STATISTICS_DIR = PARENT_FOLDER + "statistics" + File.separator;
42 LOGS_DIR = PARENT_FOLDER + "logs" + File.separator;
43 }
44
45 /**
46 * The default location for storing the framesets. Each frameset has its own
47 * subdirectory in this directory.
48 */
49
50 public static String PARENT_FOLDER;
51
52 public static String FRAME_PATH;
53
54 public static String IMAGES_PATH;
55
56 public static String HELP_PATH;
57
58 public static String PROFILE_PATH;
59
60 public static String EXPORTS_DIR;
61
62 public static String STATISTICS_DIR;
63
64 public static String LOGS_DIR;
65
66 private static final String INF_FILENAME = "frame.inf";
67
68 public static final String ILLEGAL_CHARS = ";:\\/?";
69
70 public static final int MAX_NAME_LENGTH = 64;
71
72 public static final int MAX_CACHE = 20;
73
74 private static HashMap<String, Frame> _Cache = new HashMap<String, Frame>();
75
76 // private static HashMap<String, String> _FramesetNameCache = new
77 // HashMap<String, String>();
78
79 private static boolean ENABLE_CACHE = true;
80
81 private static boolean _UseCache = true;
82
83 private static boolean _SuspendedCache = false;
84
85 // All methods are static, this should not be instantiated
86 private FrameIO() {
87 }
88
89 public static boolean isCacheOn() {
90 return _UseCache && ENABLE_CACHE;
91 }
92
93 public static void Precache(String framename) {
94 // if the cache is turned off, do nothing
95 if (!isCacheOn())
96 return;
97
98 // if the frame is already in the cache, do nothing
99 if (_Cache.containsKey(framename.toLowerCase()))
100 return;
101
102 // otherwise, load the frame and put it in the cache
103 Logger.Log(Logger.SYSTEM, Logger.LOAD, "Precaching " + framename + ".");
104
105 // do not display errors encountered to the user
106 // (they will be shown at load time)
107 FrameGraphics.SupressErrors(true);
108 // loading automatically caches the frame is caching is turned on
109 LoadUnknownPath(framename);
110 FrameGraphics.SupressErrors(false);
111 }
112
113 /**
114 * Checks if a string is a representation of a positive integer.
115 *
116 * @param s
117 * @return true if s is a positive integer
118 */
119 public static boolean isPositiveInteger(String s) {
120 if (s == null || s.length() == 0)
121 return false;
122
123 for (int i = 0; i < s.length(); i++) {
124 if (!Character.isDigit(s.charAt(i)))
125 return false;
126 }
127 return true;
128 }
129
130 public static Frame LoadFrame(String frameName) {
131 // If the frame name is a positive integer add the frameset name
132 if (isPositiveInteger(frameName)){
133 assert(false);
134 frameName = DisplayIO.getCurrentFrame().getFramesetNameAdjusted()
135 + frameName;
136 }
137
138 // first try reading from cache
139 if (isCacheOn() && _Cache.containsKey(frameName.toLowerCase())) {
140 Logger.Log(Logger.SYSTEM, Logger.LOAD, "Loading " + frameName
141 + " from cache.");
142 return _Cache.get(frameName.toLowerCase());
143 }
144
145 Logger.Log(Logger.SYSTEM, Logger.LOAD, "Loading " + frameName
146 + " from disk.");
147 return LoadUnknownPath(frameName);
148 }
149
150 private static Frame LoadUnknownPath(String framename) {
151 Frame loaded = null;
152
153 for (String path : UserSettings.FrameDirs) {
154 loaded = LoadFrame(path, framename);
155 if (loaded != null) {
156 FrameUtils.Parse(loaded, true);
157 break;
158 }
159 }
160
161 return loaded;
162 }
163
164 /**
165 * Gets the full path and file name of the frame.
166 *
167 * @param path-
168 * the directory in which to look for the frameset containing the
169 * frame.
170 * @param frameName-
171 * the name of the frame for which the path is being requested.
172 * @return null if the frame can not be located.
173 */
174 public static String getFrameFullPathName(String path, String frameName) {
175 String source = path + Conversion.getFrameset(frameName)
176 + File.separator;
177
178 File tester = new File(source);
179 if (!tester.exists())
180 return null;
181
182 // check for the new file name format
183 String fullPath = source + Conversion.getFrameNumber(frameName)
184 + ExpReader.EXTENTION;
185 tester = new File(fullPath);
186
187 if (tester.exists())
188 return fullPath;
189
190 // check for oldfile name format
191 fullPath = source + Conversion.getFrameset(frameName) + "."
192 + Conversion.getFrameNumber(frameName);
193 tester = new File(fullPath);
194
195 if (tester.exists())
196 return fullPath;
197
198 return null;
199 }
200
201 public static boolean DoesFrameExist(String frameName) {
202 for (String path : UserSettings.FrameDirs) {
203 if (getFrameFullPathName(path, frameName) != null)
204 return true;
205 }
206 return false;
207 }
208
209 private static Frame LoadFrame(String path, String frameName) {
210 String fullPath = getFrameFullPathName(path, frameName);
211 if (fullPath == null)
212 return null;
213
214 try {
215 FrameReader reader;
216
217 if (fullPath.endsWith(ExpReader.EXTENTION)) {
218 reader = new ExpReader(frameName);
219 } else {
220 reader = new KMSReader();
221 }
222 Frame frame = reader.readFrame(fullPath);
223
224 if (frame == null) {
225 FrameGraphics.ErrorMessage("Error: " + frameName
226 + " could not be successfully loaded.");
227 return null;
228 }
229
230 frame.path = path;
231
232 // do not put 0 frames or virtual frames into the cache
233 if (_Cache.size() > MAX_CACHE)
234 _Cache.clear();
235
236 if (frame.getFrameNumber() > 0 && isCacheOn())
237 _Cache.put(frameName.toLowerCase(), frame);
238
239 return frame;
240 } catch (IOException ioe) {
241 ioe.printStackTrace();
242 Logger.Log(ioe);
243 } catch (Exception e) {
244 e.printStackTrace();
245 Logger.Log(e);
246 FrameGraphics.ErrorMessage("Error: " + frameName
247 + " could not be successfully loaded.");
248 }
249
250 return null;
251 }
252
253 public static void Reload() {
254 // disable cache
255 boolean cache = _UseCache;
256
257 _UseCache = false;
258 Frame fresh = FrameIO.LoadFrame(DisplayIO.getCurrentFrame()
259 .getFrameName());
260 _UseCache = cache;
261
262 if (_Cache.containsKey(fresh.getFrameName().toLowerCase()))
263 _Cache.put(fresh.getFrameName().toLowerCase(), fresh);
264
265 DisplayIO.setCurrentFrame(fresh);
266 }
267
268 public static Frame LoadPrevious(Frame current) {
269 // the current name and number
270 String name = current.getFramesetNameAdjusted();
271 int num = current.getFrameNumber() - 1;
272
273 // loop until a frame that exists is found
274 for (; num >= 0; num--) {
275 Frame f = LoadFrame(name + num);
276 if (f != null)
277 return f;
278 }
279
280 // if we did not find another Frame then this one must be the last one
281 // in the frameset
282 FrameGraphics.DisplayMessage("This is the first frame in the frameset");
283 return null;
284 }
285
286 /**
287 * Returns the next Frame in the current Frameset (The Frame with the next
288 * highest Frame number) If the current Frame is the last one in the
289 * Frameset, or an error occurs then null is returned.
290 *
291 * @return The Frame after this one in the current frameset, or null
292 */
293 public static Frame LoadNext(Frame current) {
294
295 // the current name and number
296 int num = current.getFrameNumber() + 1;
297 int max = num + 1;
298 String name = current.getFramesetNameAdjusted();
299
300 // read the maximum from the INF file
301 try {
302 max = ReadINF(current.path, current.getFramesetName());
303 } catch (IOException ioe) {
304 FrameGraphics.ErrorMessage("Error loading INF file for frameset '"
305 + name + "'");
306 return null;
307 }
308
309 // loop until a frame that exists is found
310 for (; num <= max; num++) {
311 Frame f = LoadFrame(name + num);
312 if (f != null)
313 return f;
314 }
315
316 // if we did not find another Frame then this one must be the last one
317 // in the frameset
318 FrameGraphics.DisplayMessage("This is the last frame in the frameset");
319 return null;
320 }
321
322 public static Frame LoadLast(String framesetName, String path) {
323 // read the maximum from the INF file
324 int max;
325 try {
326 max = ReadINF(path, framesetName);
327 } catch (IOException ioe) {
328 FrameGraphics.ErrorMessage("Error loading INF file for frameset '"
329 + framesetName + "'");
330 return null;
331 }
332
333 String adjustedName = Frame.GetFramesetNameAdjusted(framesetName);
334
335 // loop backwards until a frame that exists is found
336 for (int num = max; num > 0; num--) {
337 Frame f = LoadFrame(adjustedName + num);
338 if (f != null)
339 return f;
340 }
341
342 // if we did not find another Frame then this one must be the last one
343 // in the frameset
344 FrameGraphics.DisplayMessage("This is the last frame in the frameset");
345 return null;
346 }
347
348 public static Frame LoadLast() {
349 Frame current = DisplayIO.getCurrentFrame();
350 return LoadLast(current.getFramesetName(), current.path);
351 }
352
353 public static Frame LoadNext() {
354 return LoadNext(DisplayIO.getCurrentFrame());
355 }
356
357 public static Frame LoadPrevious() {
358 return LoadPrevious(DisplayIO.getCurrentFrame());
359 }
360
361 /**
362 * Deletes the given Frame on disk and removes the cached Frame if there is
363 * one. Also adds the deleted frame into the deletedFrames frameset.
364 *
365 * @param toDelete
366 * The Frame to be deleted
367 * @return The result of File.delete()
368 */
369 public static boolean DeleteFrame(Frame toDelete) throws IOException {
370 SaveFrame(toDelete);
371
372 // get the last used frame in the destination frameset
373 final String DELETED_FRAMES = "DeletedFrames";
374 int lastNumber = FrameIO.getLastNumber(DELETED_FRAMES);
375 String framePath;
376 if (lastNumber < 0) {
377 // create the new frameset
378 Frame one = FrameIO.CreateFrameset(DELETED_FRAMES, toDelete.path);
379 framePath = one.path;
380 lastNumber = 0;
381 } else {
382 Frame zero = FrameIO.LoadFrame(DELETED_FRAMES + "0");
383 framePath = zero.path;
384 }
385
386 // get the fill path to determine which file version it is
387 String source = getFrameFullPathName(toDelete.path, toDelete
388 .getFrameName());
389
390 String oldFrameName = toDelete.getFrameName().toLowerCase();
391 // Now save the frame in the new location
392 toDelete.setFrameset(DELETED_FRAMES);
393 toDelete.setFrameNumber(lastNumber + 1);
394 toDelete.path = framePath;
395 FrameIO.SaveFrame(toDelete);
396
397 if (_Cache.containsKey(oldFrameName))
398 _Cache.remove(oldFrameName);
399
400 File del = new File(source);
401
402 java.io.FileInputStream ff = new java.io.FileInputStream(del);
403 ff.close();
404
405 return del.delete();
406 }
407
408 /**
409 * Creates a new Frame in the given frameset and assigns it the given Title,
410 * which can be null. The newly created Frame is a copy of the frameset's .0
411 * file with the number updated based on the last recorded Frame name in the
412 * frameset's INF file.
413 *
414 * @param frameset
415 * The frameset to create the new Frame in
416 * @param frameTitle
417 * The title to assign to the newly created Frame (can be NULL).
418 * @return The newly created Frame.
419 */
420 public static Frame CreateFrame(String frameset, String frameTitle,
421 String templateFrame) {
422
423 int next = -1;
424
425 // disable caching of 0 frames
426 SuspendCache();
427
428 String adjustFramesetName = Frame.GetFramesetNameAdjusted(frameset);
429 Frame template = null;
430 if (templateFrame == null)
431 // load in frame.0
432 template = LoadFrame(adjustFramesetName + "0");
433 else
434 template = LoadFrame(templateFrame);
435
436 ResumeCache();
437
438 // read the next number from the INF file
439 try {
440 next = ReadINF(template.path, frameset);
441 } catch (IOException ioe) {
442 ioe.printStackTrace();
443 Logger.Log(ioe);
444 return null;
445 }
446
447 // set the number and title of the new frame
448 template.setFrameNumber(++next);
449 template.setTitle(frameTitle);
450
451 Logger.Log(Logger.SYSTEM, Logger.TDFC, "Creating new frame: "
452 + template.getFrameName() + " from TDFC");
453
454 // update INF file
455 try {
456 WriteINF(template.path, frameset, adjustFramesetName + next);
457 } catch (IOException ioe) {
458 ioe.printStackTrace();
459 Logger.Log(ioe);
460 }
461
462 template.setOwner(UserSettings.Username);
463 template.resetDateCreated();
464 for (Item i : template.getItems()) {
465 if (ItemUtils.isTag(i, ItemUtils.TAG_PARENT))
466 i.setLink(null);
467 }
468
469 return template;
470 }
471
472 public static void DisableCache() {
473 _UseCache = false;
474 }
475
476 public static void EnableCache() {
477 _UseCache = true;
478 }
479
480 public static void SuspendCache() {
481 if (_UseCache) {
482 DisableCache();
483 _SuspendedCache = true;
484 } else {
485 _SuspendedCache = false;
486 }
487 }
488
489 public static void ResumeCache() {
490 if (_SuspendedCache) {
491 EnableCache();
492 _SuspendedCache = false;
493 }
494 }
495
496 /**
497 * If the userName ends in a letter a period is appended before adding the
498 * numeric extension
499 *
500 * @param framesetName
501 * the current userName
502 * @return the exension for the current user
503 */
504 public static String getFramesetExtension(String framesetName) {
505 if (framesetName.charAt(framesetName.length() - 1) >= '0'
506 && framesetName.charAt(framesetName.length() - 1) <= '9') {
507 return ".1";
508 }
509 return "1";
510 }
511
512 /**
513 * Creates a new frameset using the given name. This includes creating a new
514 * subdirectory in the <code>FRAME_PATH</code> directory, Copying over the
515 * default.0 frame from the default frameset, copying the .0 Frame to make a
516 * .1 Frame, and creating the frameset's INF file.
517 *
518 * @param frameset
519 * The name of the Frameset to create
520 * @return The first Frame of the new Frameset (Frame.1)
521 */
522 public static Frame CreateFrameset(String frameset, String path) {
523 String conversion = frameset + " --> ";
524
525 // ensure the framename is valid
526 frameset = NameValidation(frameset);
527
528 if (frameset == null)
529 return null;
530
531 conversion += frameset;
532 Logger.Log(Logger.SYSTEM, Logger.NEW_FRAMESET, "Frameset Name: "
533 + conversion);
534 conversion = frameset;
535
536 /**
537 * TODO: Update this to exclude any\all invalid filename characters
538 */
539 // ignore annotation character
540 if (frameset.startsWith("@"))
541 frameset = frameset.substring(1);
542
543 conversion += " --> " + frameset;
544 Logger.Log(Logger.SYSTEM, Logger.NEW_FRAMESET, "Name: " + conversion);
545
546 // create the new Frameset directory
547 File dir = new File(path + frameset.toLowerCase() + File.separator);
548
549 dir.mkdirs();
550
551 // create the new INF file
552 try {
553 WriteINF(path, frameset, frameset + getFramesetExtension(frameset));
554 } catch (IOException ioe) {
555 ioe.printStackTrace();
556 Logger.Log(ioe);
557 }
558
559 SuspendCache();
560 // copy the default .0 and .1 files
561 Frame base;
562 try {
563 base = LoadFrame(UserSettings.DefaultFrame);
564 } catch (Exception e) {
565 base = new Frame();
566 }
567 ResumeCache();
568
569 base.resetDateCreated();
570 base.setFrameset(frameset);
571 base.setFrameNumber(0);
572 base.path = path;
573 SaveFrame(base, false);
574
575 base.resetDateCreated();
576 base.setFrameNumber(1);
577 base.setTitle(frameset);
578 SaveFrame(base, true);
579
580 Logger.Log(Logger.SYSTEM, Logger.NEW_FRAMESET, "Created new frameset: "
581 + frameset);
582
583 return base;
584 }
585
586 /**
587 * Tests if the given String is a 'proper' framename, that is, the String
588 * must begin with a character, end with a number with 0 or more letters and
589 * numbers in between. If there is a dot in the framename all the chars
590 * after it must be digits.
591 *
592 * @param frameName
593 * The String to test for validity as a frame name
594 * @return True if the given framename is proper, false otherwise.
595 */
596 public static boolean isValidFrameName(String frameName) {
597
598 if (frameName == null || frameName.length() < 2)
599 return false;
600
601 // String must begin with a letter and end with a digit
602 if (!Character.isLetter(frameName.charAt(0))
603 || !Character.isDigit(frameName.charAt(frameName.length() - 1)))
604 return false;
605
606 int dotIndex = frameName.indexOf(".");
607 // All the characters inbetween first and last must be letters
608 // or digits
609 for (int i = 1; i < frameName.length(); i++) {
610 if (dotIndex > 0 && i > dotIndex) {
611 if (!Character.isDigit(frameName.charAt(i)))
612 return false;
613 } else if (i != dotIndex) {
614 if (!Character.isLetterOrDigit(frameName.charAt(i)))
615 return false;
616 }
617 }
618
619 return true;
620 }
621
622 /**
623 * Saves the given Frame to disk in the corresponding frameset directory.
624 * This is the same as calling SaveFrame(toSave, true)
625 *
626 * @param toSave
627 * The Frame to save to disk in KMS format.
628 */
629 public static String SaveFrame(Frame toSave) {
630 return SaveFrame(toSave, true);
631 }
632
633 /**
634 * Saves the given Frame to disk in the corresponding frameset directory, if
635 * inc is true then the saved frames counter is incremented, otherwise it is
636 * untouched.
637 *
638 * @param toSave
639 * The Frame to save to disk in KMS format.
640 * @param inc
641 * True if the saved frames counter should be incremented, false
642 * otherwise.
643 */
644 public static String SaveFrame(Frame toSave, boolean inc) {
645
646 if (toSave == null)
647 return "";
648
649 // Get the full path only to determine which format to use for saving
650 // the frame
651 // At this stage use Exp format for saving Exp frames only.
652 // Later this will be changed so that KMS frames will be updated to the
653 // Exp format.
654 String fullPath = getFrameFullPathName(toSave.path, toSave
655 .getFrameName());
656
657 FrameWriter writer = null;
658 try {
659 // if its a new frame or an existing Exp frame...
660 if (fullPath == null || fullPath.endsWith(ExpReader.EXTENTION)) {
661 writer = new ExpWriter();
662 } else {
663 writer = new KMSWriter();
664 }
665
666 if (toSave.getFrameNumber() > 0
667 && ItemUtils.ContainsTag(toSave.getItems(),
668 ItemUtils.TAG_BACKUP)) {
669 SuspendCache();
670 Frame original = LoadFrame(toSave.getFrameName());
671 if (original == null)
672 original = toSave;
673 int orignum = original.getFrameNumber();
674 int nextnum = ReadINF(toSave.path, toSave.getFramesetName()) + 1;
675
676 original.setFrameNumber(-nextnum);
677 SaveFrame(original, inc);
678
679 Item i = ItemUtils.FindTag(toSave.getItems(),
680 ItemUtils.TAG_BACKUP);
681 i.setLink(original.getFrameName());
682 toSave.setFrameNumber(orignum);
683 ResumeCache();
684 } else if (toSave.getFrameNumber() < 0) {
685 toSave.setFrameNumber(-toSave.getFrameNumber());
686 }
687
688 toSave.setLastModifyDate(Logger.EasyDateFormat("ddMMMyyyy:HHmm"));
689 toSave.setLastModifyUser(UserSettings.Username);
690 writer.writeFrame(toSave);
691 toSave.setSaved();
692 if (inc)
693 SessionStats.SavedFrame(toSave.getFrameName());
694
695 // avoid out-of-sync frames (when in TwinFrames mode)
696 if (_Cache.containsKey(toSave.getFrameName().toLowerCase()))
697 _Cache.put(toSave.getFrameName().toLowerCase(), toSave);
698
699 Logger.Log(Logger.SYSTEM, Logger.SAVE, "Saving "
700 + toSave.getFrameName() + " to disk.");
701
702 // check that the INF file is not out of date
703 int last = ReadINF(toSave.path, toSave.getFramesetName());
704 if (last <= toSave.getFrameNumber())
705 WriteINF(toSave.path, toSave.getFramesetName(), toSave
706 .getFrameName());
707
708 // check if this was the profile frame (and thus needs
709 // re-parsing)
710 if (toSave.getFramesetName().toLowerCase().equals(
711 UserSettings.Username.toLowerCase())) {
712 FrameUtils.ParseProfile(toSave);
713 }
714 } catch (IOException ioe) {
715 ioe.printStackTrace();
716 Logger.Log(ioe);
717 }
718
719 return writer.getFileContents();
720 }
721
722 public static Frame LoadProfile(String userName) {
723 return LoadFrame(userName + FrameIO.getFramesetExtension(userName));
724 }
725
726 public static Frame CreateNewProfile(String username) {
727 Frame profile = CreateFrameset(username, PROFILE_PATH);
728 FrameUtils.CreateDefaultProfile(profile);
729 return profile;
730 }
731
732 /**
733 * Reads the INF file that corresponds to the given Frame name
734 *
735 * @param framename
736 * The Frame to lookup the INF file for
737 * @throws IOException
738 * Any exceptions encountered by the BufferedReader used to read
739 * the INF.
740 */
741 private static int ReadINF(String path, String frameset) throws IOException {
742 assert (!frameset.endsWith("."));
743
744 // read INF
745 BufferedReader reader;
746 try {
747 reader = new BufferedReader(new FileReader(path
748 + frameset.toLowerCase() + File.separator + INF_FILENAME));
749 } catch (Exception e) {
750 reader = new BufferedReader(new FileReader(path
751 + frameset.toLowerCase() + File.separator
752 + frameset.toLowerCase() + ".inf"));
753 }
754 String inf = reader.readLine();
755 reader.close();
756
757 return Conversion.getFrameNumber(inf);
758 }
759
760 /**
761 * Writes the given String out to the INF file corresponding to the current
762 * frameset.
763 *
764 * @param toWrite
765 * The String to write to the file.
766 * @throws IOException
767 * Any exception encountered by the BufferedWriter.
768 */
769 private static void WriteINF(String path, String frameset, String frameName)
770 throws IOException {
771 assert (!frameset.endsWith("."));
772
773 path += frameset.toLowerCase() + File.separator + INF_FILENAME;
774
775 BufferedWriter writer = new BufferedWriter(new FileWriter(path));
776 writer.write(frameName);
777 writer.close();
778 }
779
780 public static boolean FrameIsCached(String name) {
781 return _Cache.containsKey(name);
782 }
783
784 private static String NameValidation(String toValidate) {
785 String result = "";
786
787 boolean capital = true;
788 for (int i = 0; i < toValidate.length(); i++) {
789 char cur = toValidate.charAt(i);
790
791 // check for illegal characters
792 if (ILLEGAL_CHARS.contains("" + cur)) {
793 FrameGraphics
794 .DisplayMessage("Frameset name contains illegal character '"
795 + cur + "' at position " + (i + 1));
796 return null;
797 }
798
799 // capitalize all characters after spaces
800 if (cur == ' ') {
801 capital = true;
802 } else {
803 if (capital) {
804 capital = false;
805 result += ((String) "" + cur).toUpperCase();
806 } else
807 result += cur;
808
809 if (result.length() >= MAX_NAME_LENGTH) {
810 FrameGraphics
811 .DisplayMessage("Frameset name is too long (Max "
812 + MAX_NAME_LENGTH + " characters)");
813 return null;
814 }
815 }
816 }
817
818 return result;
819 }
820
821 public static Frame CreateNewFrame(Item linker) {
822 String title = null;
823 if (linker instanceof Text)
824 title = ((Text) linker).getFirstLine();
825
826 String frameset = DisplayIO.getCurrentFrame().getFramesetName();
827
828 String templateLink = linker.getLinkTemplate();
829
830 Frame newFrame = FrameIO.CreateFrame(frameset, title, templateLink);
831
832 // do auto shrinking of the title
833 Text titleItem = newFrame.getTitle();
834
835 while(titleItem.getBoundsWidth() + titleItem.getX() > newFrame.getFrameNameItem().getX() ) {
836 titleItem.setSize(titleItem.getSize() - 1);
837 }
838
839
840 return newFrame;
841 }
842
843 /**
844 * Creates a new Frameset on disk, including a .0, .1, and .inf files. The
845 * Default.0 frame is copied to make the initial .0 and .1 Frames
846 *
847 * @param name
848 * The Frameset name to use
849 * @return The name of the first Frame in the newly created Frameset (the .1
850 * frame)
851 */
852 public static Frame CreateNewFrameset(String name) {
853 String path = DisplayIO.getCurrentFrame().path;
854
855 // if current frameset is profile directory change it to framesets
856 if (path.equals(FrameIO.PROFILE_PATH)) {
857 path = FrameIO.FRAME_PATH;
858 }
859
860 return FrameIO.CreateFrameset(name, path);
861 }
862
863 /**
864 *
865 * @param frameset
866 * @return
867 */
868 public static int getLastNumber(String frameset) { // Rob thinks it might
869 // have been
870 // GetHighestNumExFrame
871 // TODO minimise the number of frames being read in!!
872 int num = -1;
873
874 Frame zero = LoadFrame(frameset + "0");
875
876 // the frameset does not exist (or has no 0 frame)
877 if (zero == null)
878 return -1;
879
880 try {
881 num = ReadINF(zero.path, frameset);
882 } catch (IOException e) {
883 // TODO Auto-generated catch block
884 // e.printStackTrace();
885 }
886
887 /*
888 * Michael doesnt think the code below is really needed... it will just
889 * slow things down when we are reading frames over a network***** for (;
890 * num >= 0; num--) { System.out.println("This code is loading frames to
891 * find the highest existing frame..."); if (LoadFrame(frameset + num) !=
892 * null) break; }
893 */
894
895 return num;
896 }
897
898}
Note: See TracBrowser for help on using the repository browser.