KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > gulden > framework > amoda > generic > core > GenericRecentFilesList


1 /*
2  * Project: AMODA - Abstract Modeled Application
3  * Class: de.gulden.framework.amoda.generic.core.GenericRecentFilesList
4  * Version: snapshot-beautyj-1.1
5  *
6  * Date: 2004-09-29
7  *
8  * This is a snapshot version of the AMODA 0.2 development branch,
9  * it is not released as a seperate version.
10  * For AMODA, see http://amoda.berlios.de/.
11  *
12  * This is licensed under the GNU Lesser General Public License (LGPL)
13  * and comes with NO WARRANTY.
14  *
15  * Author: Jens Gulden
16  * Email: amoda@jensgulden.de
17  */

18
19 package de.gulden.framework.amoda.generic.core;
20
21 import de.gulden.framework.amoda.generic.metadata.*;
22 import de.gulden.framework.amoda.generic.option.*;
23 import de.gulden.framework.amoda.model.core.*;
24 import de.gulden.framework.amoda.model.core.RecentFilesList;
25 import java.io.*;
26 import java.net.*;
27 import java.util.*;
28 import java.util.ArrayList JavaDoc;
29
30 /**
31  * Class GenericRecentFilesList.
32  *
33  * @author Jens Gulden
34  * @version snapshot-beautyj-1.1
35  */

36 public class GenericRecentFilesList extends ArrayList JavaDoc implements RecentFilesList {
37
38     // ------------------------------------------------------------------------
39
// --- fields ---
40
// ------------------------------------------------------------------------
41

42     protected Application application;
43
44     protected int max;
45
46
47     // ------------------------------------------------------------------------
48
// --- methods ---
49
// ------------------------------------------------------------------------
50

51     public void init(Application application) {
52         this.application=application;
53         optionsToList();
54         hasBeenChanged();
55     }
56
57     public void putFirst(File file) {
58         if (this.indexOf(file)!=0) { // not already the first one
59
this.remove(file); // remove if already there
60
this.add(0,file);
61             if (this.size()>max) {
62                 this.remove(max);
63             }
64             hasBeenChanged();
65         }
66     }
67
68     public void putFirst(URL url) {
69         if (this.indexOf(url)!=0) { // not already the first one
70
this.remove(url); // remove if already there
71
this.add(0,url);
72             if (this.size()>max) {
73                 this.remove(max);
74             }
75             hasBeenChanged();
76         }
77     }
78
79     public Application getApplication() {
80         return application;
81     }
82
83     public void setApplication(Application _application) {
84         application = _application;
85     }
86
87     public void hasBeenChanged() {
88         //write back to options
89
listToOptions();
90         // save options to file
91
((de.gulden.framework.amoda.generic.option.GenericOptions)application.getOptions()).saveGlobalProperties();
92     }
93
94     public int getMax() {
95         return max;
96     }
97
98     public void setMax(int _max) {
99         max = _max;
100     }
101
102     protected void optionsToList() {
103         max=application.getOptions().getInt("recent-files-max");
104         this.clear();
105         for (int i=0;i<max;i++) {
106             String JavaDoc recent=application.getOptions().getString("recent-file-"+i);
107             if (recent==null) {
108                 return;
109             }
110             if (recent.indexOf(':')!=-1) { // assume URL
111
try {
112                     java.net.URL JavaDoc url=new java.net.URL JavaDoc(recent);
113                     this.add(url);
114                 } catch (java.net.MalformedURLException JavaDoc mue) {
115                     //nop
116
}
117             } else { // file
118
java.io.File JavaDoc file=new java.io.File JavaDoc(recent);
119                 if (file.isFile()&&file.exists()) {
120                     this.add(file);
121                 }
122             }
123         }
124     }
125
126     protected void listToOptions() {
127         for (int i=0;i<size();i++) {
128             Object JavaDoc value=this.get(i);
129             String JavaDoc s=null;
130             if (value instanceof java.io.File JavaDoc) {
131                 s=((java.io.File JavaDoc)value).getAbsolutePath();
132             } else if (value instanceof java.net.URL JavaDoc) {
133                 s=((java.net.URL JavaDoc)value).toExternalForm();
134             }
135             de.gulden.framework.amoda.model.option.OptionEntry entry=application.getOptions().getOptionEntry("recent-file-"+i);
136             ((de.gulden.framework.amoda.generic.data.GenericValue)entry.getValue(entry.STATE_CURRENT)).set(s);
137         }
138     }
139
140 } // end GenericRecentFilesList
141
Popular Tags