KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > config > ViewConfig


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site (http://www.enhydra.org/).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: ViewConfig.java,v 1.1 2004/05/28 19:39:27 slobodan Exp $
22  */

23 package org.enhydra.barracuda.config;
24
25 import java.io.*;
26 import java.util.*;
27 import javax.servlet.*;
28 import javax.servlet.http.*;
29
30 import org.apache.log4j.*;
31
32 import org.enhydra.barracuda.config.events.*;
33 import org.enhydra.barracuda.core.comp.*;
34 import org.enhydra.barracuda.core.util.dom.*;
35 import org.enhydra.barracuda.core.event.*;
36 import org.enhydra.barracuda.core.forms.*;
37 import org.enhydra.barracuda.plankton.data.*;
38 import org.enhydra.barracuda.plankton.http.*;
39
40 /**
41  * This class provides the methods needed to configure the view
42  * screen
43  */

44 public class ViewConfig extends DefaultEventGateway {
45
46     protected static final Logger logger = Logger.getLogger(ViewConfig.class.getName());
47
48     //form id (must be unique)
49
public static final String JavaDoc FORM = ViewConfig.class.getName()+".Form";
50
51     //model name
52
public static final String JavaDoc MODEL_NAME = "View";
53
54     //model elements (these are the data items supported)
55
public static final String JavaDoc ERROR_MESSAGE = "ErrorMessage";
56     public static final String JavaDoc SUCCESS_MESSAGE = "SuccessMessage";
57     public static final String JavaDoc UPDATE_BUTTON = "UpdateButton";
58
59     //private vars
60
private ListenerFactory updateConfigFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new UpdateConfigHandler();} public String JavaDoc getListenerID() {return getID(UpdateConfigHandler.class);}};
61
62     public ViewConfig() {
63         //specify generic interest
64
specifyLocalEventInterests(updateConfigFactory);
65     }
66
67     //------------------------------------------------------------
68
// Data Models
69
//------------------------------------------------------------
70
/**
71      * define the template model that backs this screen
72      */

73     class ViewModel extends AbstractTemplateModel {
74
75         //register the model by name
76
public String JavaDoc getName() {return MODEL_NAME;}
77         
78         //provide items by key
79
public Object JavaDoc getItem(String JavaDoc key) {
80             if (logger.isDebugEnabled()) logger.debug("Asking for key:"+key);
81             ViewContext vc = getViewContext();
82
83             //Handle requests for data
84
if (key.equals(ERROR_MESSAGE)) {
85                 return ScreenUtil.getErrMsg(vc, FORM, ERROR_MESSAGE);
86             } else if (key.equals(SUCCESS_MESSAGE)) {
87                 return ScreenUtil.getSuccessMsg(vc, FORM, SUCCESS_MESSAGE);
88             } else if (key.equals(UPDATE_BUTTON)) {
89                 //because there's nothing to update, we want the update button
90
//to always be disabled (this doesn't currently work in NN)
91
BAction ba = ScreenUtil.getUpdateButtonComp(vc, updateConfigFactory);
92                 ba.setEnabled(false);
93                 return ba;
94             } else return super.getItem(key);
95         }
96     }
97
98     //------------------------------------------------------------
99
// Form Mappings, Validators
100
//------------------------------------------------------------
101
/**
102      * define the form map that backs the model
103      */

104     class ViewForm extends DefaultFormMap {
105         public ViewForm() {
106             //define the elements (note: these don't need any validators). Note
107
//also that we set the defaults to the current values. This allows
108
//us just to reset all current values down below without checking to
109
//see if the elements actually got passed in from the form.
110
if (logger.isDebugEnabled()) logger.debug("Defining form elements");
111             //(--n/a--)
112
}
113     }
114
115     //------------------------------------------------------------
116
// Model 2 - Controller Event Handlers
117
//------------------------------------------------------------
118
/**
119      * UpdateConfigHandler - handle the request to update the config
120      * screen.
121      */

122     class UpdateConfigHandler extends DefaultBaseEventListener {
123         public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException {
124             //figure out our target locale and get the appropriate screen
125
Locale locale = context.getViewCapabilities().getClientLocale();
126             MasterScreen screen = new MasterScreenFactory().getInstance(ViewConfig.this, context, locale);
127             if (logger.isDebugEnabled()) ServletUtil.showParams(context.getRequest(), logger);
128             
129             //create the login form
130
ValidationException ve = null;
131             ViewForm formMap = new ViewForm();
132             try {
133                 //map/validate the form
134
formMap.map(context.getRequest()).validate(true);
135                 
136                 //If there were no errors, update the data. Note that we just
137
//assume that all the data is here. The reason we can do this is
138
//because we prepopulated the form up above with the default values.
139
//The more proper way would be to check each value, see if it's set,
140
//and then only update it if the value has actually changed. Lot more
141
//code to do that though, and since we're just setting statics, the
142
//cost of doing it this way is minimal.
143

144                 //(--n/a--)
145

146                 //remember our success
147
formMap.putState(SUCCESS_MESSAGE, new Boolean JavaDoc(true));
148
149             } catch (ValidationException e) {
150                 ve = e;
151             }
152
153             //store the error message in the form and then put the form in the
154
//the event context
155
formMap.putState(ERROR_MESSAGE, ve);
156             context.putState(FORM, formMap);
157
158             //fire an update so that the screen will redraw
159
((ViewModel) screen.viewModel).fireModelChanged();
160
161             //redirect to the get screen again
162
throw new ClientSideRedirectException(new GetBConfig());
163         }
164     }
165
166     //------------------------------------------------------------
167
// Utility Methods
168
//------------------------------------------------------------
169
public TemplateModel getModel() {
170         return new ViewModel();
171     }
172 }
173
174
Popular Tags