KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > module > gui > html > MMEdit


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.module.gui.html;
11
12 import java.util.*;
13
14 import org.mmbase.module.*;
15 import org.mmbase.module.core.*;
16 import org.mmbase.util.*;
17
18 /**
19  * The module which provides access to the multimedia database
20  * it creates, deletes and gives you methods to keep track of
21  * multimedia objects. It does not give you direct methods for
22  * inserting and reading them thats done by other objects
23  *
24  * @application SCAN
25  * @author Daniel Ockeloen
26  * @author Hans Speijer
27  * @version $Id: MMEdit.java,v 1.12 2005/01/30 16:46:39 nico Exp $
28  */

29 public class MMEdit extends ProcessorModule {
30
31     Hashtable commandHandlers; // The objects that handle process, replace and
32
// list commands
33
StateManager stateMngr;
34
35     MMBase mmBase;
36
37     /**
38      * The central initialisation wich builts up the complete module including
39      * all helper objects
40      */

41     public void init() {
42         CommandHandlerInterface newHandler;
43
44         mmBase= (MMBase)getModule("MMBASEROOT");
45         commandHandlers = new Hashtable();
46         stateMngr = new StateManager(mmBase);
47         commandHandlers.put("STATE", stateMngr);
48         newHandler = new ObjectSelector(stateMngr);
49         commandHandlers.put("SELECT", newHandler);
50         newHandler = new FieldSelector(stateMngr);
51         commandHandlers.put("FIELDS", newHandler);
52         newHandler = new FieldEditor(stateMngr);
53         commandHandlers.put("EDIT", newHandler);
54     }
55
56     /**
57      * Only used by the classloader (no functionality
58      */

59     public MMEdit() {}
60
61
62     public void reload() {
63         //init();
64
}
65
66
67     public void unload() {}
68
69
70     public void shutdown() {}
71
72
73     /*
74     */

75     private String JavaDoc leadZero (int m) {
76         if (m < 0 || m > 9) return "" + m;
77         return "0" + m;
78     }
79
80
81     /*
82     */

83     private Vector numberList (int begin, int end) {
84         Vector v = new Vector ();
85
86         for (int n = begin; n <= end; n++) v.addElement (leadZero (n));
87         return v;
88     }
89
90
91     /*
92     */

93     private Vector allMonths () {
94         Vector v = new Vector ();
95
96         for (int m = 1; m <= 12; m++) v.addElement (FieldEditor.getMonthString (m));
97
98         return v;
99     }
100
101
102     /*
103     */

104     private Vector editDate (String JavaDoc what) {
105         Vector v = new Vector ();
106
107         if (what.equals ("DAYS")) v = numberList (1, 31);
108         else if (what.equals ("MONTHS")) v = allMonths ();
109         else if (what.equals ("YEARS")) v = numberList (1901, 2005);
110         else if (what.equals ("HOURS")) v = numberList (0, 23);
111         else if (what.equals ("MINUTES")) v = numberList (0, 59);
112         else if (what.equals ("SECONDS")) v = numberList (0, 59);
113
114         return v;
115     }
116
117
118     /**
119      * The hook that passes all list related pages to the correct handler
120      */

121     public Vector getList(scanpage sp,StringTagger tagger, String JavaDoc command) throws ParseException {
122         Vector result = new Vector();
123         CommandHandlerInterface handler;
124
125         StringTokenizer tok = new StringTokenizer(Strip.DoubleQuote(command,Strip.BOTH),"-\n\r");
126         String JavaDoc token = tok.nextToken();
127
128         if (token.startsWith ("DATE")) {
129             result = editDate (tok.nextToken ());
130         } else {
131             if (token.startsWith ("FILES")) {
132                 DirectoryLister dirlister = new DirectoryLister();
133                 String JavaDoc path = tok.nextToken();
134                 result = dirlister.getDirectories(path); //Retrieve all filepaths.
135
}
136             else {
137                 if (token.startsWith("TYPE_IMAGES")) {
138                     String JavaDoc comparefield = "modtime";
139                     DirectoryLister imglister = new DirectoryLister();
140                     String JavaDoc path = tok.nextToken();
141                     Vector unsorted = imglister.getDirectories(path); //Retrieve all filepaths
142
Vector sorted = imglister.sortDirectories(unsorted,comparefield);
143                     result = imglister.createThreeItems(sorted,tagger);
144                     tagger.setValue("ITEMS", "3");
145                     //added 27jan1999
146
String JavaDoc reverse = tagger.Value("REVERSE");
147                     if (reverse!=null) {
148                         if(reverse.equals("YES")) {
149                             int items = 3;
150                             result = imglister.reverse(result,items);
151                         }
152                     }
153                 } else {
154
155                     handler = (CommandHandlerInterface)commandHandlers.get(token);
156
157                     if (handler != null) {
158                         result = handler.getList(sp, tagger, tok);
159                     } else {
160                         result.addElement("List not defined (MMEdit)");
161                     }
162                 }
163
164             }
165         }
166
167         return result;
168     }
169
170     /**
171      * The hook that passes all form related pages to the correct handler
172      */

173     public boolean process(scanpage sp, Hashtable cmds, Hashtable vars) {
174
175         CommandHandlerInterface handler;
176         String JavaDoc token, cmdline;
177
178         for (Enumeration h = cmds.keys();h.hasMoreElements();) {
179             cmdline=(String JavaDoc)h.nextElement();
180             StringTokenizer tok = new StringTokenizer(cmdline,"-\n\r");
181             token = tok.nextToken();
182             handler = (CommandHandlerInterface)commandHandlers.get(token);
183
184             if (handler != null) {
185                 handler.process(sp, tok, cmds, vars);
186             } else if (token.equals("BUILDER")) {
187                 // If a variable PRC-CMD-BUILDER-bla-bla is set, this code here is called.
188
// It calls the process method of the java code of the builder. EditState
189
// - which allows one to get to the posted variables directly - is passed
190
// on through the vars Hashtable with key "EDITSTATE".
191
String JavaDoc userName=HttpAuth.getRemoteUser(sp);
192                 EditState state = stateMngr.getEditState(userName);
193                 MMObjectBuilder builder = state.getBuilder();
194                 vars.put("EDITSTATE",state);
195                 // XXX pity: I get a ClassCast Exception here
196
//handler = (CommandHandlerInterface)builder;
197
//if (debug) log.debug("process() calling BUILDER "+builder.getTableName());
198
builder.process(sp,tok,cmds,vars);
199                 //if (debug) log.debug("mark: builder.process() called");
200
}
201
202         }
203
204         return true;
205     }
206
207     /**
208      * The hook that passes all replace and trigger related pages to the
209      * correct handler
210      */

211     public String JavaDoc replace(scanpage sp, String JavaDoc command) {
212         CommandHandlerInterface handler;
213         String JavaDoc token;
214
215         StringTokenizer tok = new StringTokenizer(command,"-\n\r");
216         token = tok.nextToken();
217         // log.error("MMEDIT->"+token+" "+commandHandlers);
218
handler = (CommandHandlerInterface)commandHandlers.get(token);
219
220         if (handler != null) return handler.replace(sp, tok);
221
222         return "Command not defined (MMEdit)";
223     }
224 }
225
Popular Tags