1 | package org.honours.CollectionSpace;
|
---|
2 |
|
---|
3 | import java.io.BufferedReader;
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.FileReader;
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.List;
|
---|
8 |
|
---|
9 | import org.expeditee.actions.Navigation;
|
---|
10 | import org.expeditee.gui.Frame;
|
---|
11 | import org.expeditee.gui.FrameIO;
|
---|
12 | import org.expeditee.gui.MessageBay;
|
---|
13 | import org.expeditee.items.ItemParentStateChangedEvent;
|
---|
14 | import org.expeditee.items.Picture;
|
---|
15 | import org.expeditee.items.Text;
|
---|
16 | import org.honours.agents.GenerateThumbnails;
|
---|
17 | import org.honours.collection.Collection;
|
---|
18 | import org.honours.gui.HonoursFrameIO;
|
---|
19 | import org.honours.items.PictureInformation;
|
---|
20 |
|
---|
21 | public class CollectionSpace {
|
---|
22 |
|
---|
23 | //the collection this collection space is associated with
|
---|
24 | private Collection _associatedCollection;
|
---|
25 |
|
---|
26 | private String _name;
|
---|
27 |
|
---|
28 | //FOr storing thumbnails for the collection space
|
---|
29 | private String _thumbnailsDirectory;
|
---|
30 |
|
---|
31 | private List<Picture> _thumbnails = new ArrayList<Picture>();
|
---|
32 |
|
---|
33 | private int _startX = 150;
|
---|
34 | private int _startY = 150;
|
---|
35 |
|
---|
36 | public static final int _gap = 20;
|
---|
37 | public static final int _columns = 4;
|
---|
38 | public static final int _imageSize = 150;
|
---|
39 | public static final int _rows = 3;
|
---|
40 |
|
---|
41 | public CollectionSpace(){
|
---|
42 |
|
---|
43 | }
|
---|
44 |
|
---|
45 | public CollectionSpace(Collection associatedCollection){
|
---|
46 | _associatedCollection = associatedCollection;
|
---|
47 | _name = "collectionspace-" + _associatedCollection.getName();
|
---|
48 | _thumbnailsDirectory = "expeditee" + File.separator + "thumbnails-" +
|
---|
49 | _associatedCollection.getName();
|
---|
50 | }
|
---|
51 |
|
---|
52 | public void setAssociatedCollection(Collection assocCollection){
|
---|
53 | _associatedCollection = assocCollection;
|
---|
54 | }
|
---|
55 |
|
---|
56 | public Collection getAssociatedCollection(){
|
---|
57 | return _associatedCollection;
|
---|
58 | }
|
---|
59 |
|
---|
60 | public void setName(String name){
|
---|
61 | _name = name;
|
---|
62 | }
|
---|
63 |
|
---|
64 | public String getName(){
|
---|
65 | return _name;
|
---|
66 | }
|
---|
67 |
|
---|
68 | public void setThumbnailsDirectory(String dir){
|
---|
69 |
|
---|
70 | _thumbnailsDirectory = dir;
|
---|
71 |
|
---|
72 | }
|
---|
73 |
|
---|
74 | public String getThumbnailsDirectory(){
|
---|
75 | return _thumbnailsDirectory;
|
---|
76 | }
|
---|
77 |
|
---|
78 | public void generate(){
|
---|
79 | try{
|
---|
80 |
|
---|
81 | if(_thumbnailsDirectory == null){
|
---|
82 |
|
---|
83 | MessageBay.errorMessage("ERROR: No thumbnails directory set up so cannot generate CollectionSpace. Try running the GenerateThumbnails agent first before generating the Collection Space.");
|
---|
84 |
|
---|
85 | return;
|
---|
86 | }
|
---|
87 |
|
---|
88 | if(!new File(_thumbnailsDirectory).exists()){
|
---|
89 | GenerateThumbnails gt = new GenerateThumbnails();
|
---|
90 | Frame toLoad = FrameIO.LoadFrame(_associatedCollection + "1");
|
---|
91 | System.err.println("*** _associatedCollection = " + _associatedCollection);
|
---|
92 | System.err.println("*** toLoad = " + toLoad);
|
---|
93 | gt.process(FrameIO.LoadFrame(_associatedCollection.getName() + "1"));
|
---|
94 | }
|
---|
95 |
|
---|
96 | //generate actual collectionSpace frameset using _name.
|
---|
97 | createCollectionSpace();
|
---|
98 |
|
---|
99 | //Open thumbnails.inf file.
|
---|
100 | BufferedReader br = new BufferedReader(new FileReader(
|
---|
101 | _thumbnailsDirectory + File.separator +
|
---|
102 | "thumbnails.inf"));
|
---|
103 | String line;
|
---|
104 |
|
---|
105 | while((line = br.readLine()) != null){
|
---|
106 |
|
---|
107 | String[] split = line.split(",");
|
---|
108 |
|
---|
109 | //1st entry in split is filename, 2nd is associated frame name.
|
---|
110 | Text imageSource = new Text("@i: " + split[0] + " 1.0");
|
---|
111 | Picture newPic = PictureInformation.getPicture(imageSource);
|
---|
112 |
|
---|
113 | //Remove "thumbnail." from value in split[1].
|
---|
114 | //String picLink = split[1].substring("thumbnail.".length());
|
---|
115 |
|
---|
116 | newPic.setLink(split[1]);
|
---|
117 | //newPic.setLink(picLink);
|
---|
118 | _thumbnails.add(newPic);
|
---|
119 | }
|
---|
120 | br.close();
|
---|
121 |
|
---|
122 | writeThumbnails();
|
---|
123 |
|
---|
124 | MessageBay.displayMessage("Collection Space generated!");
|
---|
125 |
|
---|
126 | Navigation.Goto(_name + "1");
|
---|
127 |
|
---|
128 | }catch(Exception e){
|
---|
129 | e.printStackTrace();
|
---|
130 | }
|
---|
131 |
|
---|
132 | }
|
---|
133 |
|
---|
134 | /** Method to output thumbnails into collection space **/
|
---|
135 | private void writeThumbnails(){
|
---|
136 |
|
---|
137 | Frame currFrame = null;
|
---|
138 | int currX = _startX;
|
---|
139 | int currY = _startY;
|
---|
140 |
|
---|
141 | int frameIter = 1;
|
---|
142 |
|
---|
143 | generateNavigationLinks(frameIter);
|
---|
144 | generateDropBox(frameIter);
|
---|
145 |
|
---|
146 | for(int i = 0; i < _thumbnails.size(); i++)
|
---|
147 | {
|
---|
148 | Picture currPic = _thumbnails.get(i);
|
---|
149 |
|
---|
150 | if(i % _columns ==0 && i == 0){
|
---|
151 |
|
---|
152 | }else if(i % _columns == 0 && i > 0){
|
---|
153 | currX = _startX;
|
---|
154 | currY += _gap + _imageSize;
|
---|
155 | }else{
|
---|
156 | currX += _gap + _imageSize;
|
---|
157 | }
|
---|
158 |
|
---|
159 | //Create a new frame to display next lot of items.
|
---|
160 | if(i % (_columns * _rows) == 0 && i < _columns * _rows){
|
---|
161 | currFrame = FrameIO.LoadFrame(_name + frameIter);
|
---|
162 |
|
---|
163 | }else if(i % (_columns * _rows) == 0 && i >= _columns * _rows){
|
---|
164 | frameIter++;
|
---|
165 | Frame check = FrameIO.LoadFrame(_name + frameIter);
|
---|
166 | if(check == null){
|
---|
167 | //create new frame
|
---|
168 | HonoursFrameIO.generateFrame(frameIter, _name);
|
---|
169 | currFrame = FrameIO.LoadFrame(_name + frameIter);
|
---|
170 |
|
---|
171 | currX = _startX;
|
---|
172 | currY = _startY;
|
---|
173 | generateNavigationLinks(frameIter);
|
---|
174 | generateDropBox(frameIter);
|
---|
175 | }
|
---|
176 |
|
---|
177 | }
|
---|
178 |
|
---|
179 | currFrame.addText(currX, currY, currPic.getSource().getText(),
|
---|
180 | null, currPic.getLink());
|
---|
181 | currFrame.setSaved();
|
---|
182 | }
|
---|
183 |
|
---|
184 | _associatedCollection.setCollectionSpace(this);
|
---|
185 | }
|
---|
186 |
|
---|
187 | private void generateNavigationLinks(int frameIter){
|
---|
188 |
|
---|
189 | String backText = "<<";
|
---|
190 | String forwardText = ">>";
|
---|
191 |
|
---|
192 | int backButtonX = _startX/2;
|
---|
193 | int forwardButtonX = _startX + (_columns*_gap)+(_columns*_imageSize);
|
---|
194 | int buttonPosY = ((_rows*_imageSize) + (_rows*_gap))/2;
|
---|
195 |
|
---|
196 | Frame currFrame = FrameIO.LoadFrame(_name+frameIter);
|
---|
197 | currFrame.addText(backButtonX,buttonPosY,backText,"RotateBack");
|
---|
198 | currFrame.addText(forwardButtonX,buttonPosY,forwardText,"RotateNext");
|
---|
199 | currFrame.setSaved();
|
---|
200 | }
|
---|
201 |
|
---|
202 | private void generateDropBox(int frameIter){
|
---|
203 | int maxRowLength = _startX + (_columns * _imageSize) + ((_columns-1)* _gap);
|
---|
204 |
|
---|
205 | //the maximum height of the area displaying all the rows.
|
---|
206 | int maxRowsAreaHeight = _startY + (_rows * _imageSize) + ((_rows-1)*_gap);
|
---|
207 |
|
---|
208 | int dropBoxGap = 80;
|
---|
209 |
|
---|
210 | int dropBoxStartX = maxRowLength + (dropBoxGap*2);
|
---|
211 | int dropBoxStartY = _startY;
|
---|
212 |
|
---|
213 | int dropBoxWidth = 300;
|
---|
214 | int dropBoxHeight = maxRowsAreaHeight;
|
---|
215 |
|
---|
216 | Frame currFrame = FrameIO.LoadFrame(_name + frameIter);
|
---|
217 |
|
---|
218 | String dropBoxSourceText = "@iw: org.honours.widgets.CollectionSpaceDropBox " + dropBoxWidth + " " + dropBoxHeight;
|
---|
219 | Text newText = currFrame.addText(dropBoxStartX, dropBoxStartY, dropBoxSourceText, null);
|
---|
220 | newText.onParentStateChanged(new ItemParentStateChangedEvent(currFrame, ItemParentStateChangedEvent.EVENT_TYPE_ADDED));
|
---|
221 |
|
---|
222 | currFrame.setSaved();
|
---|
223 | }
|
---|
224 |
|
---|
225 | private void createCollectionSpace(){
|
---|
226 | Frame check = FrameIO.LoadFrame(_name + "1");
|
---|
227 |
|
---|
228 | if(check != null){
|
---|
229 | FrameIO.deleteFrameset(_name);
|
---|
230 | }
|
---|
231 |
|
---|
232 | try{
|
---|
233 | FrameIO.CreateNewFrameset(_name);
|
---|
234 | }catch(Exception e){
|
---|
235 | e.printStackTrace();
|
---|
236 | }
|
---|
237 | }
|
---|
238 | }
|
---|