KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > contenttool > actions > TextToImageEditorAction


1 /* ===============================================================================
2 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
3 *
4 * ===============================================================================
5 *
6 * Copyright (C)
7 *
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License version 2, as published by the
10 * Free Software Foundation. See the file LICENSE.html for more information.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
18 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
19 *
20 * ===============================================================================
21 */

22
23 package org.infoglue.cms.applications.contenttool.actions;
24
25 import java.awt.Font JavaDoc;
26 import java.awt.GraphicsEnvironment JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 import org.apache.log4j.Logger;
33 import org.infoglue.cms.applications.common.actions.InfoGlueAbstractAction;
34 import org.infoglue.cms.controllers.kernel.impl.simple.CastorDatabaseService;
35 import org.infoglue.cms.controllers.kernel.impl.simple.LanguageController;
36 import org.infoglue.cms.controllers.kernel.impl.simple.SiteNodeController;
37 import org.infoglue.cms.controllers.kernel.impl.simple.SiteNodeTypeDefinitionController;
38 import org.infoglue.cms.controllers.kernel.impl.simple.SiteNodeVersionController;
39 import org.infoglue.cms.entities.management.AvailableServiceBindingVO;
40 import org.infoglue.cms.entities.management.SiteNodeTypeDefinitionVO;
41 import org.infoglue.cms.entities.structure.SiteNodeVO;
42 import org.infoglue.cms.entities.structure.SiteNodeVersionVO;
43 import org.infoglue.cms.exception.SystemException;
44 import org.infoglue.deliver.applications.databeans.DatabaseWrapper;
45 import org.infoglue.deliver.controllers.kernel.impl.simple.BasicTemplateController;
46 import org.infoglue.deliver.controllers.kernel.impl.simple.IntegrationDeliveryController;
47 import org.infoglue.deliver.controllers.kernel.impl.simple.NodeDeliveryController;
48 import org.infoglue.deliver.util.BrowserBean;
49
50 /**
51  * @author Stefan Sik
52  * @version 1.0
53  * @since InfoglueCMS 1.2.0
54  *
55  */

56 public class TextToImageEditorAction extends InfoGlueAbstractAction
57 {
58     private final static Logger logger = Logger.getLogger(TextToImageEditorAction.class.getName());
59
60     private static final long serialVersionUID = 1L;
61
62     private Integer JavaDoc languageId = null;
63     private Integer JavaDoc contentId = null;
64     private Integer JavaDoc siteNodeId = null;
65     private Integer JavaDoc repositoryId = null;
66     
67     /* (non-Javadoc)
68      * @see org.infoglue.cms.applications.common.actions.WebworkAbstractAction#doExecute()
69      */

70     
71     private String JavaDoc[] defaultSizes =
72         { "8", "9",
73             "10","11","12","13","14","15","16","18","20","22",
74             "24","26","28","30","36","42","48","72"
75         };
76     
77     private String JavaDoc text = "Sample";
78     private String JavaDoc canvasWidth = "200";
79     private String JavaDoc canvasHeight = "50";
80     private String JavaDoc textStartPosX = "0";
81     private String JavaDoc textStartPosY = "0";
82     private String JavaDoc textWidth = "20";
83     private String JavaDoc textHeight = "20";
84     private String JavaDoc fontName = "Serif";
85     private String JavaDoc fontStyle = "0";
86     private String JavaDoc fontSize = "12";
87     private String JavaDoc foregroundColor = "000000";
88     private String JavaDoc backgroundColor = "FFFFFF";
89     
90     
91     private String JavaDoc generatedImage = "";
92     private String JavaDoc generatedCommand = "";
93     private Collection JavaDoc fontNames = new ArrayList JavaDoc();
94     private Collection JavaDoc fonts = new ArrayList JavaDoc();
95     private SiteNodeVO siteNodeVO;
96     private SiteNodeVersionVO siteNodeVersionVO;
97     private SiteNodeTypeDefinitionVO siteNodeTypeDefinitionVO;
98     private List JavaDoc availableServiceBindings;
99     private List JavaDoc serviceBindings;
100     
101     public int getBoldValue()
102     {
103         return Font.BOLD;
104     }
105     public int getItalicValue()
106     {
107         return Font.ITALIC;
108     }
109     public String JavaDoc[] getSizes()
110     {
111         return defaultSizes;
112     }
113     
114     
115     private String JavaDoc q(String JavaDoc t)
116     {
117         return "\"" + t + "\"";
118     }
119     private String JavaDoc qc(String JavaDoc t)
120     {
121         return "\"" + t + "\", ";
122     }
123     private String JavaDoc c(String JavaDoc t)
124     {
125         return "" + t + ", ";
126     }
127     
128     protected void initialize(Integer JavaDoc siteNodeId) throws Exception JavaDoc
129     {
130         this.siteNodeVO = SiteNodeController.getSiteNodeVOWithId(siteNodeId);
131         this.siteNodeVersionVO = SiteNodeVersionController.getController().getLatestSiteNodeVersionVO(siteNodeId);
132         
133         if(siteNodeVO.getSiteNodeTypeDefinitionId() != null)
134         {
135             this.siteNodeTypeDefinitionVO = SiteNodeTypeDefinitionController.getController().getSiteNodeTypeDefinitionVOWithId(siteNodeVO.getSiteNodeTypeDefinitionId());
136             this.availableServiceBindings = SiteNodeTypeDefinitionController.getController().getAvailableServiceBindingVOList(siteNodeVO.getSiteNodeTypeDefinitionId());
137             this.serviceBindings = SiteNodeVersionController.getServiceBindningVOList(siteNodeVersionVO.getSiteNodeVersionId());
138         }
139     }
140     
141     protected String JavaDoc doExecute() throws Exception JavaDoc
142     {
143         DatabaseWrapper dbWrapper = new DatabaseWrapper(CastorDatabaseService.getDatabase());
144         //Database db = CastorDatabaseService.getDatabase();
145

146         beginTransaction(dbWrapper.getDatabase());
147
148         try
149         {
150             if(siteNodeId==null)
151                 siteNodeId = SiteNodeController.getController().getRootSiteNodeVO(repositoryId).getId();
152             
153             if(languageId==null)
154                 languageId = LanguageController.getController().getMasterLanguage(repositoryId).getId();
155             
156             if(contentId==null)
157                 contentId = new Integer JavaDoc(-1);
158     
159             initialize(siteNodeId);
160             
161             /* An editor to simplify textToImage statements in templates
162              * This class generates visually a getTextAsImageUrl(..)
163              * Use BasicTemplateController.renderString(template)
164              */

165             
166             /*
167              * Setup font lists
168              */

169             
170             Font JavaDoc allFonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
171             for(int i = 0;i<allFonts.length;i++)
172             {
173                 fonts.add(allFonts[i]);
174             }
175             
176             String JavaDoc[] fontNamesList = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
177             for(int i = 0;i<fontNamesList.length;i++)
178                 fontNames.add(fontNamesList[i]);
179             
180             
181             StringBuffer JavaDoc t = new StringBuffer JavaDoc();
182             t.append("$templateLogic.getStringAsImageUrl(");
183             t.append(qc(text));
184             t.append(c(canvasWidth));
185             t.append(c(canvasHeight));
186             t.append(c(textStartPosX));
187             t.append(c(textStartPosY));
188             t.append(c(textWidth));
189             t.append(c(textHeight));
190             t.append(qc(fontName));
191             t.append(c(fontStyle));
192             t.append(c(fontSize));
193             t.append(qc(foregroundColor));
194             t.append(q(backgroundColor));
195             t.append(")");
196     
197             
198             BasicTemplateController templateController = getTemplateController(dbWrapper, siteNodeId, languageId, contentId);
199             generatedImage = templateController.renderString(t.toString());
200             generatedCommand = t.toString();
201             
202             
203             closeTransaction(dbWrapper.getDatabase());
204         }
205         catch(Exception JavaDoc e)
206         {
207             logger.error("An error occurred so we should not complete the transaction:" + e, e);
208             rollbackTransaction(dbWrapper.getDatabase());
209             throw new SystemException(e.getMessage());
210         }
211
212         return "success";
213     }
214
215     public BasicTemplateController getTemplateController(DatabaseWrapper databaseWrapper, Integer JavaDoc siteNodeId, Integer JavaDoc languageId, Integer JavaDoc contentId) throws SystemException, Exception JavaDoc
216     {
217         BasicTemplateController templateController = new BasicTemplateController(databaseWrapper, this.getInfoGluePrincipal());
218         templateController.setStandardRequestParameters(siteNodeId, languageId, contentId);
219         templateController.setHttpRequest(getRequest());
220         templateController.setBrowserBean(new BrowserBean());
221         templateController.setDeliveryControllers(NodeDeliveryController.getNodeDeliveryController(siteNodeId, languageId, contentId), null, IntegrationDeliveryController.getIntegrationDeliveryController(siteNodeId,languageId, contentId));
222         return templateController;
223     }
224
225     public List JavaDoc getSortedAvailableServiceBindings()
226     {
227         List JavaDoc sortedAvailableServiceBindings = new ArrayList JavaDoc();
228         
229         Iterator JavaDoc iterator = this.availableServiceBindings.iterator();
230         while(iterator.hasNext())
231         {
232             AvailableServiceBindingVO availableServiceBinding = (AvailableServiceBindingVO)iterator.next();
233             int index = 0;
234             Iterator JavaDoc sortedListIterator = sortedAvailableServiceBindings.iterator();
235             while(sortedListIterator.hasNext())
236             {
237                 AvailableServiceBindingVO sortedAvailableServiceBinding = (AvailableServiceBindingVO)sortedListIterator.next();
238                 
239                 String JavaDoc currentAttribute = availableServiceBinding.getName();
240                 String JavaDoc sortedAttribute = sortedAvailableServiceBinding.getName();
241                 
242                 if(currentAttribute != null && sortedAttribute != null && currentAttribute.compareTo(sortedAttribute) < 0)
243                 {
244                     break;
245                 }
246                 index++;
247             }
248             sortedAvailableServiceBindings.add(index, availableServiceBinding);
249         }
250             
251         return sortedAvailableServiceBindings;
252     }
253     
254     /**
255      * This method sorts a list of available service bindings on the name of the binding.
256      */

257
258     public List JavaDoc getSortedAvailableContentServiceBindings()
259     {
260         List JavaDoc sortedAvailableContentServiceBindings = new ArrayList JavaDoc();
261         
262         Iterator JavaDoc sortedListIterator = getSortedAvailableServiceBindings().iterator();
263         while(sortedListIterator.hasNext())
264         {
265             AvailableServiceBindingVO sortedAvailableServiceBinding = (AvailableServiceBindingVO)sortedListIterator.next();
266             if(sortedAvailableServiceBinding.getVisualizationAction().indexOf("Structure") == -1)
267                 sortedAvailableContentServiceBindings.add(sortedAvailableServiceBinding);
268         }
269             
270         return sortedAvailableContentServiceBindings;
271     }
272     public List JavaDoc getSortedAvailableSingleContentServiceBindings()
273     {
274         List JavaDoc sortedAvailableContentServiceBindings = new ArrayList JavaDoc();
275         
276         Iterator JavaDoc sortedListIterator = getSortedAvailableServiceBindings().iterator();
277         while(sortedListIterator.hasNext())
278         {
279             AvailableServiceBindingVO sortedAvailableServiceBinding = (AvailableServiceBindingVO)sortedListIterator.next();
280             if(sortedAvailableServiceBinding.getVisualizationAction().indexOf("Structure") == -1 &&
281                sortedAvailableServiceBinding.getVisualizationAction().indexOf("Multi") == -1 )
282                 sortedAvailableContentServiceBindings.add(sortedAvailableServiceBinding);
283         }
284             
285         return sortedAvailableContentServiceBindings;
286     }
287
288     public List JavaDoc getAvailableAttributes()
289     {
290         List JavaDoc sortedAvailableContentServiceBindings = new ArrayList JavaDoc();
291         
292         Iterator JavaDoc sortedListIterator = getSortedAvailableServiceBindings().iterator();
293         while(sortedListIterator.hasNext())
294         {
295             AvailableServiceBindingVO sortedAvailableServiceBinding = (AvailableServiceBindingVO)sortedListIterator.next();
296             if(sortedAvailableServiceBinding.getVisualizationAction().indexOf("Structure") == -1 &&
297                sortedAvailableServiceBinding.getVisualizationAction().indexOf("Multi") == -1 )
298             {
299                 sortedAvailableContentServiceBindings.add(sortedAvailableServiceBinding);
300                 
301             }
302             
303         }
304         
305         
306         
307         return sortedAvailableContentServiceBindings;
308     }
309
310     /**
311      * This method sorts a list of available service bindings on the name of the binding.
312      */

313
314     public List JavaDoc getSortedAvailableStructureServiceBindings()
315     {
316         List JavaDoc sortedAvailableStructureServiceBindings = new ArrayList JavaDoc();
317         
318         Iterator JavaDoc sortedListIterator = getSortedAvailableServiceBindings().iterator();
319         while(sortedListIterator.hasNext())
320         {
321             AvailableServiceBindingVO sortedAvailableServiceBinding = (AvailableServiceBindingVO)sortedListIterator.next();
322             if(sortedAvailableServiceBinding.getVisualizationAction().indexOf("Structure") > -1)
323                 sortedAvailableStructureServiceBindings.add(sortedAvailableServiceBinding);
324         }
325             
326         return sortedAvailableStructureServiceBindings;
327     }
328
329     
330     public List JavaDoc getServiceBindings()
331     {
332         return this.serviceBindings;
333     }
334     
335     
336     public String JavaDoc getGeneratedImage()
337     {
338         return generatedImage;
339     }
340
341     public Collection JavaDoc getFonts()
342     {
343         return fonts;
344     }
345     /**
346      * @return Returns the backgroundColor.
347      */

348     public String JavaDoc getBackgroundColor()
349     {
350         return backgroundColor;
351     }
352     /**
353      * @param backgroundColor The backgroundColor to set.
354      */

355     public void setBackgroundColor(String JavaDoc backgroundColor)
356     {
357         this.backgroundColor = backgroundColor;
358     }
359     /**
360      * @return Returns the canvasHeight.
361      */

362     public String JavaDoc getCanvasHeight()
363     {
364         return canvasHeight;
365     }
366     /**
367      * @param canvasHeight The canvasHeight to set.
368      */

369     public void setCanvasHeight(String JavaDoc canvasHeight)
370     {
371         this.canvasHeight = canvasHeight;
372     }
373     /**
374      * @return Returns the canvasWidth.
375      */

376     public String JavaDoc getCanvasWidth()
377     {
378         return canvasWidth;
379     }
380     /**
381      * @param canvasWidth The canvasWidth to set.
382      */

383     public void setCanvasWidth(String JavaDoc canvasWidth)
384     {
385         this.canvasWidth = canvasWidth;
386     }
387     /**
388      * @return Returns the fontName.
389      */

390     public String JavaDoc getFontName()
391     {
392         return fontName;
393     }
394     /**
395      * @param fontName The fontName to set.
396      */

397     public void setFontName(String JavaDoc fontName)
398     {
399         this.fontName = fontName;
400     }
401     /**
402      * @return Returns the fontSize.
403      */

404     public String JavaDoc getFontSize()
405     {
406         return fontSize;
407     }
408     /**
409      * @param fontSize The fontSize to set.
410      */

411     public void setFontSize(String JavaDoc fontSize)
412     {
413         this.fontSize = fontSize;
414     }
415     /**
416      * @return Returns the fontStyle.
417      */

418     public String JavaDoc getFontStyle()
419     {
420         return fontStyle;
421     }
422     /**
423      * @param fontStyle The fontStyle to set.
424      */

425     public void setFontStyle(String JavaDoc fontStyle)
426     {
427         this.fontStyle = fontStyle;
428     }
429     /**
430      * @return Returns the foregroundColor.
431      */

432     public String JavaDoc getForegroundColor()
433     {
434         return foregroundColor;
435     }
436     /**
437      * @param foregroundColor The foregroundColor to set.
438      */

439     public void setForegroundColor(String JavaDoc foregroundColor)
440     {
441         this.foregroundColor = foregroundColor;
442     }
443     /**
444      * @return Returns the text.
445      */

446     public String JavaDoc getText()
447     {
448         return text;
449     }
450     /**
451      * @param text The text to set.
452      */

453     public void setText(String JavaDoc text)
454     {
455         this.text = text;
456     }
457     /**
458      * @return Returns the textHeight.
459      */

460     public String JavaDoc getTextHeight()
461     {
462         return textHeight;
463     }
464     /**
465      * @param textHeight The textHeight to set.
466      */

467     public void setTextHeight(String JavaDoc textHeight)
468     {
469         this.textHeight = textHeight;
470     }
471     /**
472      * @return Returns the textStartPosX.
473      */

474     public String JavaDoc getTextStartPosX()
475     {
476         return textStartPosX;
477     }
478     /**
479      * @param textStartPosX The textStartPosX to set.
480      */

481     public void setTextStartPosX(String JavaDoc textStartPosX)
482     {
483         this.textStartPosX = textStartPosX;
484     }
485     /**
486      * @return Returns the textStartPosY.
487      */

488     public String JavaDoc getTextStartPosY()
489     {
490         return textStartPosY;
491     }
492     /**
493      * @param textStartPosY The textStartPosY to set.
494      */

495     public void setTextStartPosY(String JavaDoc textStartPosY)
496     {
497         this.textStartPosY = textStartPosY;
498     }
499     /**
500      * @return Returns the textWidth.
501      */

502     public String JavaDoc getTextWidth()
503     {
504         return textWidth;
505     }
506     /**
507      * @param textWidth The textWidth to set.
508      */

509     public void setTextWidth(String JavaDoc textWidth)
510     {
511         this.textWidth = textWidth;
512     }
513     /**
514      * @return Returns the fontNames.
515      */

516     public Collection JavaDoc getFontNames()
517     {
518         return fontNames;
519     }
520     /**
521      * @return Returns the contentId.
522      */

523     public Integer JavaDoc getContentId()
524     {
525         return contentId;
526     }
527     /**
528      * @param contentId The contentId to set.
529      */

530     public void setContentId(Integer JavaDoc contentId)
531     {
532         this.contentId = contentId;
533     }
534     /**
535      * @return Returns the languageId.
536      */

537     public Integer JavaDoc getLanguageId()
538     {
539         return languageId;
540     }
541     /**
542      * @param languageId The languageId to set.
543      */

544     public void setLanguageId(Integer JavaDoc languageId)
545     {
546         this.languageId = languageId;
547     }
548     /**
549      * @return Returns the repositoryId.
550      */

551     public Integer JavaDoc getRepositoryId()
552     {
553         return repositoryId;
554     }
555     /**
556      * @param repositoryId The repositoryId to set.
557      */

558     public void setRepositoryId(Integer JavaDoc repositoryId)
559     {
560         this.repositoryId = repositoryId;
561     }
562     /**
563      * @return Returns the siteNodeId.
564      */

565     public Integer JavaDoc getSiteNodeId()
566     {
567         return siteNodeId;
568     }
569     /**
570      * @param siteNodeId The siteNodeId to set.
571      */

572     public void setSiteNodeId(Integer JavaDoc siteNodeId)
573     {
574         this.siteNodeId = siteNodeId;
575     }
576     /**
577      * @return Returns the generatedCommand.
578      */

579     public String JavaDoc getGeneratedCommand()
580     {
581         return generatedCommand;
582     }
583     /**
584      * @param generatedCommand The generatedCommand to set.
585      */

586     public void setGeneratedCommand(String JavaDoc generatedCommand)
587     {
588         this.generatedCommand = generatedCommand;
589     }
590 }
591
Popular Tags