1 | package org.greenstone.gatherer.feedback;
|
---|
2 |
|
---|
3 | import java.util.*;
|
---|
4 | import java.awt.image.*;
|
---|
5 | import java.io.*;
|
---|
6 | import javax.imageio.*;
|
---|
7 | import javax.swing.*;
|
---|
8 | import java.awt.*;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * This class willl hold the information about a component and the contents inside the component.
|
---|
12 | * @author Veronica Liesaputra
|
---|
13 | */
|
---|
14 | public class UserComponent implements Serializable
|
---|
15 | {
|
---|
16 | /**
|
---|
17 | * This variable will hold what is the type of instance this component is.
|
---|
18 | */
|
---|
19 | private String type = null;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * This variable will store the title of this component.
|
---|
23 | */
|
---|
24 | private String title = null;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * This varaible will store the array list of the contents of this component.
|
---|
28 | */
|
---|
29 | private ArrayList content = null;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * This variable will store the status of this component, whether or not
|
---|
33 | * this component is selected by the user.
|
---|
34 | */
|
---|
35 | private String selected = null;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * This variable will store the status of this component, whether or not
|
---|
39 | * this component is visible.
|
---|
40 | */
|
---|
41 | private String visible = null;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * This variable will hold the descrption text saying that there is no
|
---|
45 | * image in this component.
|
---|
46 | */
|
---|
47 | private String image = null;
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * This is the supposed filename of the image that is in this component should use if
|
---|
51 | * we want to save it to a jpeg file.
|
---|
52 | */
|
---|
53 | private String img = null;
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * This is the string representation of the image that is in this component.
|
---|
57 | */
|
---|
58 | private String imgFile = null;
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * This is the index that will help storing the data to the correct data member.
|
---|
62 | */
|
---|
63 | private int index;
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * This is the height of the image that is in this component.
|
---|
67 | */
|
---|
68 | private String height;
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * This is the width of the image that is in this component.
|
---|
72 | */
|
---|
73 | private String width;
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * This variable will hold the tooltiptext set for this component.
|
---|
77 | */
|
---|
78 | private String tooltip;
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * This will initialized content to a new empty Arrray list.
|
---|
82 | */
|
---|
83 | public UserComponent()
|
---|
84 | {
|
---|
85 | content = new ArrayList();
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * This method will get the width of the image that is inside this component.
|
---|
90 | * @return image's width.
|
---|
91 | */
|
---|
92 | public String getWidth()
|
---|
93 | {
|
---|
94 | return width;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * This method will get the height of the image that is inside this component.
|
---|
99 | * @return image's height.
|
---|
100 | */
|
---|
101 | public String getHeight()
|
---|
102 | {
|
---|
103 | return height;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * This method will get the type of instance this component is.
|
---|
108 | * @return component's type.
|
---|
109 | */
|
---|
110 | public String getType()
|
---|
111 | {
|
---|
112 | return type;
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * This method will get the tooltiptext set for this component.
|
---|
117 | * @return component's tooltiptext.
|
---|
118 | */
|
---|
119 | public String getToolTip()
|
---|
120 | {
|
---|
121 | return tooltip;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * This method will get the title for this component.
|
---|
126 | * @return component's title.
|
---|
127 | */
|
---|
128 | public String getTitle()
|
---|
129 | {
|
---|
130 | return title;
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * This method will get the content inside this component.
|
---|
135 | * @return component's content.
|
---|
136 | */
|
---|
137 | public ArrayList getContent()
|
---|
138 | {
|
---|
139 | return content;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * This method will get whether or not this component is
|
---|
144 | * selected by the user.
|
---|
145 | * @return component selected or not.
|
---|
146 | */
|
---|
147 | public String getSelected()
|
---|
148 | {
|
---|
149 | return selected;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * This method will get whether or not this component is
|
---|
154 | * visible.
|
---|
155 | * @return component visible or not.
|
---|
156 | */
|
---|
157 | public String getVisible()
|
---|
158 | {
|
---|
159 | return visible;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * This method will get the descrption text saying that there is no
|
---|
164 | * image in this component.
|
---|
165 | * @return the description text.
|
---|
166 | */
|
---|
167 | public String getImage()
|
---|
168 | {
|
---|
169 | return image;
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * This method will get the supposed filename of the image that is in this component should use if
|
---|
174 | * we want to save it to a jpeg file.
|
---|
175 | * @return the image file name.
|
---|
176 | */
|
---|
177 | public String getImageFileName()
|
---|
178 | {
|
---|
179 | return img;
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * This method will get the string representation of the image that is in this component.
|
---|
184 | * @return the image.
|
---|
185 | */
|
---|
186 | public String getImageFile()
|
---|
187 | {
|
---|
188 | return imgFile;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * This method will set the index value.
|
---|
193 | * (Precondition: (num > 0))
|
---|
194 | * @param num the new index value.
|
---|
195 | */
|
---|
196 | public void startContent (int num)
|
---|
197 | {
|
---|
198 | index = num;
|
---|
199 | }
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * This method will add the UserComponent to the content array list.
|
---|
203 | * @param comp the content of the component.
|
---|
204 | */
|
---|
205 | public void saveContent (UserComponent comp)
|
---|
206 | {
|
---|
207 | content.add(comp);
|
---|
208 | }
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * This method will set the data member value to the text passed
|
---|
212 | * to this method depending on the index value.
|
---|
213 | * @param text the value to be set to the data member.
|
---|
214 | */
|
---|
215 | public void saveContent (String text)
|
---|
216 | {
|
---|
217 | if (text == null)
|
---|
218 | text = " ";
|
---|
219 |
|
---|
220 | switch (index)
|
---|
221 | {
|
---|
222 | case 1 : type = text; break;
|
---|
223 | case 2 : title = text; break;
|
---|
224 | case 3 : content.add(text); break;
|
---|
225 | case 4 : selected = text;break;
|
---|
226 | case 5 : visible = text;break;
|
---|
227 | case 6 : image = text; break;
|
---|
228 | case 7 : tooltip = text;break;
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * This method will encode the ImageIcon into its String representation using Based64encoder and
|
---|
234 | * it will also set the image's filename,height and width.
|
---|
235 | * @param image is the image to be converted.
|
---|
236 | * @param filename is the file name of the image.
|
---|
237 | */
|
---|
238 | public void saveImage (ImageIcon image,String filename)
|
---|
239 | {
|
---|
240 | /*int w;
|
---|
241 | w = image.getIconWidth();
|
---|
242 | int h;
|
---|
243 | h = image.getIconHeight();
|
---|
244 |
|
---|
245 | height = "" + h;
|
---|
246 | width = "" + w;
|
---|
247 | img = filename;
|
---|
248 |
|
---|
249 | if (w > 50)
|
---|
250 | w = w - 50;
|
---|
251 |
|
---|
252 | if (h > 50)
|
---|
253 | h = h - 50;
|
---|
254 |
|
---|
255 | ImageIcon icon;
|
---|
256 | icon = new ImageIcon(image.getImage().getScaledInstance(w,h,Image.SCALE_SMOOTH));
|
---|
257 |
|
---|
258 | if ( w < 0 || h < 0)
|
---|
259 | {
|
---|
260 | System.out.println(img);
|
---|
261 | return;
|
---|
262 | }
|
---|
263 |
|
---|
264 | BufferedImage bi;
|
---|
265 | bi =new BufferedImage(w,h, BufferedImage.TYPE_INT_RGB);
|
---|
266 | Graphics2D big;
|
---|
267 | big = bi.createGraphics();
|
---|
268 |
|
---|
269 | big.drawImage(icon.getImage(),0,0,null);
|
---|
270 |
|
---|
271 | try
|
---|
272 | {
|
---|
273 | ByteArrayOutputStream stream;
|
---|
274 | stream = new ByteArrayOutputStream();
|
---|
275 | ImageIO.write(bi,"jpeg",stream);
|
---|
276 | byte[] bytes;
|
---|
277 | bytes = stream.toByteArray();
|
---|
278 | imgFile = Base64.encodeBytes(bytes);
|
---|
279 | }
|
---|
280 | catch (IOException exp) {}
|
---|
281 |
|
---|
282 | big.dispose();
|
---|
283 | image = null;
|
---|
284 | bi = null;
|
---|
285 | icon = null;*/
|
---|
286 | }
|
---|
287 |
|
---|
288 | /**
|
---|
289 | * This method will encode the Icon into its String representation using Based64encoder and
|
---|
290 | * it will also set the image's filename,height and width.
|
---|
291 | * @param image is the image to be converted.
|
---|
292 | * @param filename is the file name of the image
|
---|
293 | */
|
---|
294 | public void saveImage (Icon image,String filename)
|
---|
295 | {
|
---|
296 | if (image == null)
|
---|
297 | System.out.println("null image??");
|
---|
298 | else if (image instanceof ImageIcon)
|
---|
299 | saveImage((ImageIcon)image, filename);
|
---|
300 | else
|
---|
301 | System.out.println("Its a icon");
|
---|
302 | //saveIcon (image,filename);
|
---|
303 | }
|
---|
304 |
|
---|
305 | /**
|
---|
306 | * This method will encode the Icon into its String representation using Based64encoder and
|
---|
307 | * it will also set the image's filename,height and width.
|
---|
308 | * @param image is the image to be converted.
|
---|
309 | * @param filename is the file name of the image.
|
---|
310 | */
|
---|
311 | private void saveIcon (Icon image,String filename)
|
---|
312 | {
|
---|
313 | JFrame lbl = new JFrame();
|
---|
314 | lbl.setVisible(false);
|
---|
315 |
|
---|
316 | ImageIcon icon;
|
---|
317 | Image img;
|
---|
318 | img = iconToImage(image,lbl);
|
---|
319 | icon = new ImageIcon(img);
|
---|
320 | lbl.dispose();
|
---|
321 | lbl = null;
|
---|
322 | saveImage(icon,filename);
|
---|
323 | }
|
---|
324 |
|
---|
325 | /**
|
---|
326 | * This method will save to content of this UserComponent to an xml file.
|
---|
327 | * The xml file should already have an opening tag and closing tag first before calling
|
---|
328 | * this method or it wont make a valid xml file.
|
---|
329 | * @param sx this is the xml file where we want to save it.
|
---|
330 | */
|
---|
331 | public void sendingXML (SaveToXML sx)
|
---|
332 | {
|
---|
333 | if (type != null)
|
---|
334 | {
|
---|
335 | sx.startContent(1);
|
---|
336 | sx.saveContent(type);
|
---|
337 | sx.closeContent(1);
|
---|
338 | }
|
---|
339 |
|
---|
340 | if (title != null)
|
---|
341 | {
|
---|
342 | sx.startContent(2);
|
---|
343 | sx.saveContent(title);
|
---|
344 | sx.closeContent(2);
|
---|
345 | }
|
---|
346 |
|
---|
347 | if (content != null)
|
---|
348 | {
|
---|
349 | sx.startContent(3);
|
---|
350 | for (int j = 0 ; j < content.size() ; j++)
|
---|
351 | {
|
---|
352 | if (content.get(j) instanceof String)
|
---|
353 | sx.saveContent((String) content.get(j));
|
---|
354 | else
|
---|
355 | {
|
---|
356 | sx.startContent(0);
|
---|
357 | UserComponent cmp;
|
---|
358 | cmp = (UserComponent) content.get(j);
|
---|
359 | cmp.sendingXML(sx);
|
---|
360 | sx.closeContent(0);
|
---|
361 | }
|
---|
362 | }
|
---|
363 | sx.closeContent(3);
|
---|
364 | }
|
---|
365 |
|
---|
366 | if (selected != null)
|
---|
367 | {
|
---|
368 | sx.startContent(4);
|
---|
369 | sx.saveContent(selected);
|
---|
370 | sx.closeContent(4);
|
---|
371 | }
|
---|
372 |
|
---|
373 | if (visible != null)
|
---|
374 | {
|
---|
375 | sx.startContent(5);
|
---|
376 | sx.saveContent(visible);
|
---|
377 | sx.closeContent(5);
|
---|
378 | }
|
---|
379 |
|
---|
380 | if (img != null)
|
---|
381 | {
|
---|
382 | sx.startContent(6);
|
---|
383 | sx.saveImage(imgFile,img,width,height);
|
---|
384 | sx.closeContent(6);
|
---|
385 | }
|
---|
386 | else
|
---|
387 | {
|
---|
388 | if (image != null)
|
---|
389 | {
|
---|
390 | sx.startContent(11);
|
---|
391 | sx.saveContent(image);
|
---|
392 | sx.closeContent(11);
|
---|
393 | }
|
---|
394 | }
|
---|
395 |
|
---|
396 | if (tooltip != null)
|
---|
397 | {
|
---|
398 | sx.startContent(12);
|
---|
399 | sx.saveContent(tooltip);
|
---|
400 | sx.closeContent(12);
|
---|
401 | }
|
---|
402 | }
|
---|
403 |
|
---|
404 | /**
|
---|
405 | * This method will convert an Icon to a BufferedImage.
|
---|
406 | * @param icon icon to be converted.
|
---|
407 | * @param targetComponent is the component where we want to draw this BufferedImage.
|
---|
408 | * @return is the BufferedImage of the icon.
|
---|
409 | */
|
---|
410 | public BufferedImage iconToImage(Icon icon, Component targetComponent)
|
---|
411 | {
|
---|
412 | int w,h;
|
---|
413 | w = icon.getIconWidth();
|
---|
414 | h = icon.getIconHeight();
|
---|
415 | GraphicsConfiguration gc;
|
---|
416 | gc = targetComponent.getGraphicsConfiguration();
|
---|
417 | BufferedImage image;
|
---|
418 | image = gc.createCompatibleImage(w, h,Transparency.TRANSLUCENT);
|
---|
419 |
|
---|
420 | Graphics2D g;
|
---|
421 | g = image.createGraphics();
|
---|
422 | icon.paintIcon(targetComponent,g,0,0);
|
---|
423 | g.dispose();
|
---|
424 | return image;
|
---|
425 | }
|
---|
426 | }
|
---|
427 |
|
---|
428 |
|
---|
429 |
|
---|
430 |
|
---|
431 |
|
---|
432 |
|
---|
433 |
|
---|
434 |
|
---|
435 |
|
---|
436 |
|
---|
437 |
|
---|
438 |
|
---|
439 |
|
---|