KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > engines > shared > HtmlEditorsViewHelper


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13

14 package org.jahia.engines.shared;
15
16 import java.util.Enumeration JavaDoc;
17 import java.util.Hashtable JavaDoc;
18
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20
21 import org.jahia.exceptions.JahiaException;
22 import org.jahia.registries.ServicesRegistry;
23 import org.jahia.services.htmleditors.HtmlEditor;
24 import org.jahia.services.htmleditors.HtmlEditorCSS;
25 import org.jahia.services.htmleditors.HtmlEditorsService;
26 import org.jahia.services.htmleditors.JahiaClientCapabilities;
27 import java.util.Vector JavaDoc;
28 import java.io.Serializable JavaDoc;
29
30 /**
31  * <p>Title: HtmlEditorsViewHelper</p>
32  * <p>Description: Html Editors Helper</p>
33  * <p>Copyright: Copyright (c) 2002</p>
34  * <p>Company: Jahia S.A.R.L</p>
35  * @author Khue Nguyen
36  * @version 1.0
37  */

38 public class HtmlEditorsViewHelper implements Serializable JavaDoc {
39
40     private Hashtable JavaDoc editors;
41     private Vector JavaDoc editorList;
42     private Hashtable JavaDoc enabledCSSs;
43     private String JavaDoc defaultEditor;
44     private String JavaDoc defaultCSS;
45
46     public HtmlEditorsViewHelper(){
47         this.editors = new Hashtable JavaDoc();
48         this.editorList = new Vector JavaDoc();
49         this.enabledCSSs = new Hashtable JavaDoc();
50         this.defaultEditor = "";
51         this.defaultCSS = "";
52     }
53
54     /**
55      * Can set a default editor
56      *
57      * @param defaultEditor
58      */

59     public HtmlEditorsViewHelper(String JavaDoc defaultEditor){
60         this.editors = new Hashtable JavaDoc();
61         this.editorList = new Vector JavaDoc();
62         this.enabledCSSs = new Hashtable JavaDoc();
63         this.defaultEditor = defaultEditor;
64     }
65
66     /**
67      * Load the editors for a given site, only those that are Client Capable.
68      *
69      * @param siteID
70      * @param request
71      * @throws JahiaException
72      */

73     public void loadHtmlEditors(int siteID, HttpServletRequest JavaDoc request)
74     throws JahiaException {
75         HtmlEditorsService heServ = ServicesRegistry.getInstance()
76                                   .getHtmlEditorsService();
77         JahiaClientCapabilities clientCapabilities =
78                 new JahiaClientCapabilities( request.getHeader( "user-agent" ) );
79         Enumeration JavaDoc enumeration = heServ.getEditors(siteID);
80         HtmlEditor editor = null;
81         while ( enumeration.hasMoreElements() )
82         {
83             editor = (HtmlEditor)enumeration.nextElement();
84             if ( editor.isClientCapable( clientCapabilities ) ){
85                 this.editors.put(editor.getId(),editor);
86                 this.editorList.add(editor);
87             }
88         }
89
90         // retrieve enabled CSS for this site
91
enumeration = heServ.getCSSs(siteID);
92         HtmlEditorCSS css = null;
93         while ( enumeration.hasMoreElements() )
94         {
95             css = (HtmlEditorCSS)enumeration.nextElement();
96             this.enabledCSSs.put(css.getId(),css);
97         }
98
99     }
100
101     /**
102      *
103      * @return the default Editor ID.
104      */

105     public String JavaDoc getDefaultEditorID(){
106         return this.defaultEditor;
107     }
108
109     /**
110      * Set the default editor ID
111      *
112      * @param Set the default Editor ID.
113      */

114     public void setDefaultEditorID(String JavaDoc id){
115         if ( id==null ){
116             id = "";
117         }
118         this.defaultEditor = id;
119     }
120
121     /**
122      * Set the default CSS
123      *
124      * @param Set the default Editor ID.
125      */

126     public void setDefaultCSSID(String JavaDoc id){
127         if ( id==null ){
128             id = "";
129         }
130         this.defaultCSS = id;
131     }
132
133     /**
134      *
135      * @return the default CSS ID.
136      */

137     public String JavaDoc getDefaultCSSID(){
138         return this.defaultCSS;
139     }
140
141     /**
142      * Returns the editors as an enumeration.
143      *
144      * @return the editors as an enumeration
145      */

146     public Enumeration JavaDoc getEditors(){
147         return this.editorList.elements();
148     }
149
150     /**
151      * Returns the list of enabled CSS.
152      *
153      * @return the list of enabled CSS.
154      */

155     public Hashtable JavaDoc getEnabledCSSs(){
156         return this.enabledCSSs;
157     }
158     /**
159      * Return the default Editor if any.
160      *
161      * @return the default editor if any
162      */

163     public HtmlEditor getDefaultEditor(){
164         HtmlEditor editor = (HtmlEditor)this.editors.get(this.defaultEditor);
165         return editor;
166     }
167
168     /**
169      * Return the default Editor if any.If not set, return the first available
170      * and set it as the default.
171      *
172      * @return the default editor if any.
173      */

174     public HtmlEditor getDefaultEditor(boolean firstAvailableIfAny){
175         HtmlEditor editor = getDefaultEditor();
176         if ( !firstAvailableIfAny ){
177             return editor;
178         }
179         if ( editor == null && this.editorList.size() > 0 ){
180             editor = (HtmlEditor)this.editorList.get(0);
181         }
182         return editor;
183     }
184
185 }
186
Popular Tags