KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > snip > label > TypeLabel


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.snip.label;
26
27 import org.radeox.util.Service;
28 import org.radeox.util.i18n.ResourceManager;
29 import org.snipsnap.app.Application;
30 import org.snipsnap.net.ServletPluginLoader;
31
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Set JavaDoc;
36
37 public class TypeLabel extends BaseLabel {
38   private static Map JavaDoc types = null;
39
40   /**
41    * Get known types and their handlers (potentially no handlers)
42    * The syntaxt of the file is "<type> <viewhandler>:<edithandler>"
43    *
44    * @return a map of the structure type:{viewhandler,edithandler}
45    */

46   private static Map JavaDoc getTypeMap() {
47     if (types == null) {
48       types = new HashMap JavaDoc();
49       Iterator JavaDoc iter = Service.providerNames(TypeLabel.class);
50
51       while (iter.hasNext()) {
52         String JavaDoc typeInfo = ((String JavaDoc) iter.next()).trim();
53         if (!"".equals(typeInfo) && !typeInfo.startsWith("#")) {
54           String JavaDoc[] entry = typeInfo.split("\\p{Space}+|:");
55           String JavaDoc[] handlers = new String JavaDoc[2];
56           switch (entry.length) {
57             case 3:
58               handlers[1] = entry[2];
59             case 2:
60               handlers[0] = entry[1];
61             case 1:
62               types.put(entry[0], handlers);
63               break;
64           }
65         }
66       }
67     }
68     return types;
69   }
70
71   /**
72    * Get known types.
73    *
74    * @return a set of type names
75    */

76   public static Set JavaDoc getTypes() {
77     return getTypeMap().keySet();
78   }
79
80   /**
81    * Get a view handler for the provided type name.
82    *
83    * @param type the type name
84    * @return a view handler or null
85    */

86   public static String JavaDoc getViewHandler(String JavaDoc type) {
87     String JavaDoc[] handlers = (String JavaDoc[]) getTypeMap().get(type);
88     if (null != handlers) {
89       return handlers[0];
90     }
91     return null;
92   }
93
94   /**
95    * Get an edit handler for the provided type name.
96    *
97    * @param type the type name
98    * @return an edit handler or null
99    */

100   public static String JavaDoc getEditHandler(String JavaDoc type) {
101     String JavaDoc[] handlers = (String JavaDoc[]) getTypeMap().get(type);
102     if (null != handlers) {
103       return handlers[1];
104     }
105     return null;
106   }
107
108   private String JavaDoc type;
109   private String JavaDoc viewHandler;
110   private String JavaDoc editHandler;
111
112   public TypeLabel() {
113     name = "Type";
114     setValue(value);
115   }
116
117   public TypeLabel(String JavaDoc value) {
118     this();
119     setValue(value);
120   }
121
122   public String JavaDoc getType() {
123     return "TypeLabel";
124   }
125
126   public void setValue(String JavaDoc value) {
127     if (null != value && !"".equals(value)) {
128       String JavaDoc values[] = value.split(",");
129       type = (values.length > 0 ? values[0] : "");
130       viewHandler = (values.length > 1 ? values[1] : "");
131       editHandler = (values.length > 2 ? values[2] : "");
132     }
133     super.setValue(value);
134   }
135
136   public String JavaDoc getValue() {
137     return
138             (isNull(type) ? "" : type) +
139             (isNull(viewHandler) ? (isNull(editHandler) ? "" : ",") : "," + viewHandler) +
140             (isNull(editHandler) ? "" : "," + editHandler);
141   }
142
143   private boolean isNull(String JavaDoc var) {
144     return var == null || "".equals(var);
145   }
146
147   public String JavaDoc getTypeValue() {
148     return type;
149   }
150
151   public String JavaDoc getViewHandler() {
152     return "".equals(viewHandler) ? null : viewHandler;
153   }
154
155   public String JavaDoc getEditHandler() {
156     return "".equals(editHandler) ? null : editHandler;
157   }
158
159   public String JavaDoc getInputProxy() {
160     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
161     if (Application.get().getUser().isAdmin()) {
162       buffer.append("<input type=\"hidden\" name=\"label.name\" value=\"");
163       buffer.append(name);
164       buffer.append("\"/>");
165       buffer.append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"2\">");
166       buffer.append("<tr>");
167       buffer.append("<td>");
168       buffer.append(ResourceManager.getString("i18n.messages", "label.type.type"));
169       buffer.append("</td>");
170       buffer.append("<td>");
171
172       Iterator JavaDoc iterator = getTypes().iterator();
173       buffer.append("<select name=\"label.type\" size=\"1\">");
174       while (iterator.hasNext()) {
175         String JavaDoc typeString = (String JavaDoc) iterator.next();
176         buffer.append("<option");
177         if (typeString.equals(type)) {
178           buffer.append(" selected=\"selected\"");
179         }
180         buffer.append(">");
181         // @TODO: Check if type == value
182
buffer.append(typeString);
183         buffer.append("</option>");
184       }
185       buffer.append("</select>");
186
187       buffer.append("</tr><tr>");
188       buffer.append("<td>");
189       buffer.append(ResourceManager.getString("i18n.messages", "label.type.view"));
190       buffer.append("</td>");
191       buffer.append("<td>");
192
193       Map JavaDoc handlers = ServletPluginLoader.getPlugins();
194       // add view handlers
195
buffer.append("<select name=\"label.viewhandler\" size=\"1\">");
196       buffer.append("<option value=\"\">");
197       buffer.append(ResourceManager.getString("i18n.messages", "label.type.nohandler"));
198       buffer.append("</option>");
199       Iterator JavaDoc it = handlers.keySet().iterator();
200       while (it.hasNext()) {
201         String JavaDoc handlername = (String JavaDoc) it.next();
202         buffer.append("<option");
203         if (handlername.equals(getViewHandler())) {
204           buffer.append(" selected=\"selected\"");
205         }
206         buffer.append(">");
207         buffer.append(handlername);
208         buffer.append("</option>");
209       }
210
211       buffer.append(handlers);
212       buffer.append("</select>");
213       buffer.append("</td>");
214       buffer.append("</tr><tr>");
215       buffer.append("<td>");
216       buffer.append(ResourceManager.getString("i18n.messages", "label.type.edit"));
217       buffer.append("</td>");
218       buffer.append("<td>");
219
220       // add edit handlers
221
buffer.append("<select name=\"label.edithandler\" size=\"1\">");
222       buffer.append("<option value=\"\">");
223       buffer.append(ResourceManager.getString("i18n.messages", "label.type.nohandler"));
224       buffer.append("</option>");
225       it = handlers.keySet().iterator();
226       while (it.hasNext()) {
227         String JavaDoc handlername = (String JavaDoc) it.next();
228         buffer.append("<option");
229         if (handlername.equals(getEditHandler())) {
230           buffer.append(" selected=\"selected\"");
231         }
232         buffer.append(">");
233         buffer.append(handlername);
234         buffer.append("</option>");
235       }
236       buffer.append("</select>");
237       buffer.append("</td></tr></table>");
238     } else {
239       buffer.append(ResourceManager.getString("i18n.messages", "label.type.adminonly"));
240     }
241
242     return buffer.toString();
243   }
244
245   public String JavaDoc getListProxy() {
246     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
247     buffer.append("<td>");
248     buffer.append(name);
249     buffer.append("</td><td>");
250     buffer.append(type);
251     buffer.append("</td>");
252     return buffer.toString();
253   }
254
255   public void handleInput(Map JavaDoc input) {
256     // ADMIN ONLY!
257
if (Application.get().getUser().isAdmin()) {
258       super.handleInput(input);
259       if (input.containsKey("label.type")) {
260         type = (String JavaDoc) input.get("label.type");
261       }
262       if (input.containsKey("label.viewhandler")) {
263         viewHandler = (String JavaDoc) input.get("label.viewhandler");
264       }
265       if (input.containsKey("label.edithandler")) {
266         editHandler = (String JavaDoc) input.get("label.edithandler");
267       }
268       // tricky ... set the actual value by using our own getValue() method
269
super.setValue(getValue());
270     }
271   }
272
273 }
274
Popular Tags