1 23 24 package com.sun.enterprise.tools.guiframework.view.descriptors; 25 26 import java.io.InputStream ; 27 import java.util.Iterator ; 28 import java.util.ArrayList ; 29 import java.util.List ; 30 import java.lang.reflect.Method ; 31 32 import com.iplanet.jato.ModelManager; 33 import com.iplanet.jato.RequestManager; 34 import com.iplanet.jato.RequestContext; 35 import com.iplanet.jato.view.ContainerView; 36 import com.iplanet.jato.view.ContainerViewBase; 37 import com.iplanet.jato.view.View; 38 import com.iplanet.jato.util.NonSyncStringBuffer; 39 40 import com.sun.web.ui.model.CCPageTitleModel; 41 import com.sun.web.ui.model.CCPageTitleModelInterface; 42 import com.sun.web.ui.view.pagetitle.CCPageTitle; 43 import com.sun.web.ui.view.html.CCButton; 44 import com.sun.web.ui.view.html.CCDropDownMenu; 45 import com.sun.web.ui.taglib.html.CCButtonTag; 46 import com.sun.web.ui.taglib.html.CCDropDownMenuTag; 47 import com.sun.web.ui.common.CCTagClass; 48 import com.sun.web.ui.taglib.common.CCDisplayFieldTagBase; 49 import com.sun.web.ui.model.CCPageTitleModelInterface; 50 51 import com.sun.enterprise.tools.guiframework.util.LogUtil; 52 import com.sun.enterprise.tools.guiframework.view.DescriptorCCPageTitle; 53 54 57 public class CCPageTitleDescriptor extends ViewDescriptor { 58 59 private static final String xmlStart = 61 CCPageTitleModelInterface.XML_DELCARATION + "\n" + 62 CCPageTitleModelInterface.DOCTYPE + "\n" + 63 "<" + CCPageTitleModelInterface.PAGE_TITLE_ELEMENT + ">\n"; 64 65 private static final String xmlEnd = 66 "</" + CCPageTitleModelInterface.PAGE_TITLE_ELEMENT + ">\n"; 67 68 69 70 73 public CCPageTitleDescriptor(String name) { 74 super(name); 75 } 76 77 78 public void registerChildren(ContainerViewBase instance) { 79 super.registerChildren(instance); 81 getModel().registerChildren(instance); 82 } 83 84 88 public ViewDescriptor getChildDescriptor(String name) { 89 ViewDescriptor desc = super.getChildDescriptor(name); 91 if (desc != null) { 92 return desc; 93 } 94 95 CCPageTitleModel model = getModel(); 97 if (model != null && model.isChildSupported(name)) { 98 desc = new CCPageTitleChildDescriptor(name); 100 addChildDescriptor(desc); 107 return desc; 108 } 109 return null; 110 } 111 112 private boolean isLegalTagAttribute(String name, CCDisplayFieldTagBase tag) { 113 char c = Character.toUpperCase(name.charAt(0)); 115 String methodName = "set" + c + name.substring(1); 117 try { 118 Method method = tag.getClass().getMethod( 120 methodName, new Class [] {String .class}); 121 if (method == null) 122 return false; 123 } catch (Exception e) { 124 return false; 125 } 126 return true; 127 } 128 129 private void addParameters(NonSyncStringBuffer buff, 130 CCDisplayFieldTagBase tag, ViewDescriptor desc) { 131 Iterator it = desc.getParameterKeys().iterator(); 132 while (it.hasNext()) { 133 String key = (String ) it.next(); 134 if (isLegalTagAttribute(key, tag) == false) { 135 continue; 138 } 139 Object obj = desc.getParameter(key); 140 String value = null; 141 if (obj != null){ 142 value = obj.toString(); 143 } 144 if (value != null) { 145 buff.append(" <attribute name=\"") 146 .append(key) 147 .append("\" value=\"") 148 .append(value) 149 .append("\"/>\n"); 150 } 151 } 152 } 153 154 private void appendPageActionButtons(NonSyncStringBuffer buff) { 155 Iterator it = getChildDescriptors().iterator(); 156 ViewDescriptor desc = null; 157 boolean first = true; 158 159 while (it.hasNext()) { 160 desc = (ViewDescriptor)it.next(); 161 String buttonType = (String ) desc.getParameter("type"); 162 if (buttonType != null && 163 (buttonType.equals(CCButton.TYPE_PRIMARY) || 164 buttonType.equals(CCButton.TYPE_SECONDARY) || 165 buttonType.equals(CCDropDownMenu.TYPE_JUMP) || 166 buttonType.equals(CCDropDownMenu.TYPE_STANDARD))) { 167 continue; 171 } 172 Object show = desc.getParameter("showButton"); 173 if (show != null && show.toString().equals("false")) 174 continue; 175 String buttonName = desc.getName(); 176 if (buttonName == null) 177 continue; 179 if (first) 180 buff.append(" <" + CCPageTitleModelInterface.PAGE_ACTIONS_ELEMENT + ">\n"); 181 182 buff.append(" <cc name=\"") 183 .append(buttonName) 184 .append("\" tagclass=\"" + CCTagClass.BUTTON + "\">\n"); 185 186 addParameters(buff, new CCButtonTag(), desc); 187 188 buff.append(" </cc>\n"); 189 first = false; 190 } 191 if (! first) 192 buff.append(" </" + CCPageTitleModelInterface.PAGE_ACTIONS_ELEMENT + ">\n"); 193 } 194 195 private void appendPageButtons(NonSyncStringBuffer buff) { 196 Iterator it = getChildDescriptors().iterator(); 197 ViewDescriptor desc = null; 198 boolean first = true; 199 200 while (it.hasNext()) { 201 desc = (ViewDescriptor)it.next(); 202 String buttonType = (String ) desc.getParameter("type"); 203 if (buttonType == null || 204 (buttonType.equals(CCButton.TYPE_PRIMARY) == false && 205 buttonType.equals(CCButton.TYPE_SECONDARY) == false)) { 206 continue; 210 } 211 String show = (String ) desc.getParameter("showButton"); 212 if (show != null && show.equals("false")) 213 continue; 214 String buttonName = desc.getName(); 215 if (buttonName == null) 216 continue; 218 if (first) 219 buff.append(" <" + CCPageTitleModelInterface.PAGE_BUTTONS_ELEMENT + ">\n"); 220 221 buff.append(" <cc name=\"") 222 .append(buttonName) 223 .append("\" tagclass=\"" + CCTagClass.BUTTON + "\">\n"); 224 225 addParameters(buff, new CCButtonTag(), desc); 226 227 buff.append(" </cc>\n"); 228 first = false; 229 } 230 if (! first) 231 buff.append(" </" + CCPageTitleModelInterface.PAGE_BUTTONS_ELEMENT + ">\n"); 232 } 233 234 private void addOptionList(NonSyncStringBuffer buff, ViewDescriptor desc) { 235 Object options = desc.getParameter("labels"); 236 if (options != null) { 237 if (options instanceof String ) { 238 List tmp = new ArrayList (); 239 tmp.add(options); 240 options = tmp; 241 } 242 List optionList = (List )options; 243 244 Object values = desc.getParameter("values"); 245 List valueList = optionList; 246 if (values != null) { 247 if (values instanceof String ) { 248 List tmp = new ArrayList (); 249 tmp.add(values); 250 values = tmp; 251 } 252 valueList = (List )values; 253 if (valueList.size() != optionList.size()) { 254 throw new RuntimeException ( 255 "Unequal number of option names / values! " + 256 "(CCPageTitleDescriptor.addOptionList)"); 257 } 258 } 259 for (int i=0; i<valueList.size(); i++) { 260 buff.append(" <option label=\"").append(optionList.get(i)) 261 .append("\" value=\"").append(valueList.get(i)) 262 .append("\"/>\n"); 263 } 264 } 265 } 266 267 private void appendPageViewDropDown(NonSyncStringBuffer buff) { 268 Iterator it = getChildDescriptors().iterator(); 269 ViewDescriptor desc = null; 270 boolean first = true; 271 272 while (it.hasNext()) { 273 desc = (ViewDescriptor)it.next(); 274 String type = (String ) desc.getParameter("type"); 275 if (type == null || 276 (type.equals(CCDropDownMenu.TYPE_JUMP) == false && 277 type.equals(CCDropDownMenu.TYPE_STANDARD) == false)) { 278 continue; 279 } 280 String name = desc.getName(); 281 if (name == null) 282 continue; 284 if (first) 285 buff.append(" <" + CCPageTitleModelInterface.PAGE_VIEWS_ELEMENT + ">\n"); 286 287 buff.append(" <cc name=\"").append(name) 288 .append("\" tagclass=\"" + CCTagClass.DROPDOWNMENU + "\">\n"); 289 290 addParameters(buff, new CCDropDownMenuTag(), desc); 291 addOptionList(buff, desc); 292 293 buff.append(" </cc>\n"); 294 first = false; 295 } 296 if (! first) 297 buff.append(" </" + CCPageTitleModelInterface.PAGE_VIEWS_ELEMENT + ">\n"); 298 } 299 300 private String getPageTitleXML() { 301 NonSyncStringBuffer buff = new NonSyncStringBuffer(2048); 302 buff.append(xmlStart); 303 304 appendPageButtons(buff); 305 appendPageActionButtons(buff); 306 appendPageViewDropDown(buff); 307 308 buff.append(xmlEnd); 309 310 if (LogUtil.isLoggable(LogUtil.FINEST)) { 312 LogUtil.log(LogUtil.FINEST, "trace.pageTitleXML", buff.toString()); 313 } 314 return buff.toString(); 315 } 316 317 private InputStream getPageTitleXMLAsStream() { 318 ViewDescriptor desc = this; 319 InputStream is = null; 320 is = (InputStream )desc.getParameter("fileAsStream"); 321 return is; 322 } 323 324 325 335 public String getDefaultModelInstanceName() { 336 ViewDescriptor top = this; 338 while (top.getParent() != null) { 339 top = top.getParent(); 340 } 341 342 return top.getName()+"."+getName(); 344 } 345 346 347 350 public CCPageTitleModel getModel() { 351 boolean fromSession = shouldGetModelFromSession(); 353 boolean toSession = shouldPutModelToSession(); 354 String instanceName = getModelInstanceName(); 355 356 ModelManager mgr = RequestManager.getRequestContext().getModelManager(); 358 CCPageTitleModel model = (CCPageTitleModel)mgr.getModel( 359 CCPageTitleModel.class, instanceName, fromSession, toSession); 360 InputStream is = null; 362 if(model.getDocument() == null) { 365 if((is = getPageTitleXMLAsStream()) != null) { 366 model.setDocument(is); 367 try { 368 is.close(); 369 } catch (java.io.IOException ex) { 370 } 372 } 373 else { 374 model.setDocument(getPageTitleXML()); 375 } 376 } 377 return model; 378 } 379 380 381 387 public View getInstance(RequestContext ctx, ContainerView container, String name) { 388 return new DescriptorCCPageTitle(ctx, container, name, this, getModel()); 389 } 390 } 391 | Popular Tags |