/*
* Copyright 2009 Prime Technology.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.primefaces.component.dialog;
import javax.faces.component.UIPanel;
import javax.faces.context.FacesContext;
import javax.el.ValueExpression;
import javax.el.MethodExpression;
import javax.faces.render.Renderer;
import java.io.IOException;
import org.primefaces.renderkit.PartialRenderer;
import org.primefaces.component.resource.Resource;
import javax.faces.component.UIComponent;
import javax.faces.event.ListenerFor;
import javax.faces.event.PostAddToViewEvent;
import javax.faces.event.ComponentSystemEvent;
import javax.faces.event.AbortProcessingException;
import java.util.List;
import java.util.ArrayList;
@ListenerFor(systemEventClass = PostAddToViewEvent.class)
public class Dialog extends UIPanel {
public static final String COMPONENT_TYPE = "org.primefaces.component.Dialog";
public static final String COMPONENT_FAMILY = "org.primefaces.component";
private static final String DEFAULT_RENDERER = "org.primefaces.component.DialogRenderer";
private static final String OPTIMIZED_PACKAGE = "org.primefaces.component.";
protected enum PropertyKeys {
widgetVar
,header
,draggable
,resizable
,modal
,visible
,width
,height
,zindex
,minWidth
,minHeight
,styleClass
,closeListener
,onCloseUpdate
,showEffect
,hideEffect
,position
,closeOnEscape
,closable;
String toString;
PropertyKeys(String toString) {
this.toString = toString;
}
PropertyKeys() {}
public String toString() {
return ((this.toString != null) ? this.toString : super.toString());
}
}
public Dialog() {
setRendererType(DEFAULT_RENDERER);
}
public String getFamily() {
return COMPONENT_FAMILY;
}
public java.lang.String getWidgetVar() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.widgetVar, null);
}
public void setWidgetVar(java.lang.String _widgetVar) {
getStateHelper().put(PropertyKeys.widgetVar, _widgetVar);
handleAttribute("widgetVar", _widgetVar);
}
public java.lang.String getHeader() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.header, null);
}
public void setHeader(java.lang.String _header) {
getStateHelper().put(PropertyKeys.header, _header);
handleAttribute("header", _header);
}
public boolean isDraggable() {
return (java.lang.Boolean) getStateHelper().eval(PropertyKeys.draggable, true);
}
public void setDraggable(boolean _draggable) {
getStateHelper().put(PropertyKeys.draggable, _draggable);
handleAttribute("draggable", _draggable);
}
public boolean isResizable() {
return (java.lang.Boolean) getStateHelper().eval(PropertyKeys.resizable, true);
}
public void setResizable(boolean _resizable) {
getStateHelper().put(PropertyKeys.resizable, _resizable);
handleAttribute("resizable", _resizable);
}
public boolean isModal() {
return (java.lang.Boolean) getStateHelper().eval(PropertyKeys.modal, false);
}
public void setModal(boolean _modal) {
getStateHelper().put(PropertyKeys.modal, _modal);
handleAttribute("modal", _modal);
}
public boolean isVisible() {
return (java.lang.Boolean) getStateHelper().eval(PropertyKeys.visible, false);
}
public void setVisible(boolean _visible) {
getStateHelper().put(PropertyKeys.visible, _visible);
handleAttribute("visible", _visible);
}
public int getWidth() {
return (java.lang.Integer) getStateHelper().eval(PropertyKeys.width, 300);
}
public void setWidth(int _width) {
getStateHelper().put(PropertyKeys.width, _width);
handleAttribute("width", _width);
}
public int getHeight() {
return (java.lang.Integer) getStateHelper().eval(PropertyKeys.height, java.lang.Integer.MIN_VALUE);
}
public void setHeight(int _height) {
getStateHelper().put(PropertyKeys.height, _height);
handleAttribute("height", _height);
}
public int getZindex() {
return (java.lang.Integer) getStateHelper().eval(PropertyKeys.zindex, 1000);
}
public void setZindex(int _zindex) {
getStateHelper().put(PropertyKeys.zindex, _zindex);
handleAttribute("zindex", _zindex);
}
public int getMinWidth() {
return (java.lang.Integer) getStateHelper().eval(PropertyKeys.minWidth, 150);
}
public void setMinWidth(int _minWidth) {
getStateHelper().put(PropertyKeys.minWidth, _minWidth);
handleAttribute("minWidth", _minWidth);
}
public int getMinHeight() {
return (java.lang.Integer) getStateHelper().eval(PropertyKeys.minHeight, 0);
}
public void setMinHeight(int _minHeight) {
getStateHelper().put(PropertyKeys.minHeight, _minHeight);
handleAttribute("minHeight", _minHeight);
}
public java.lang.String getStyleClass() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.styleClass, null);
}
public void setStyleClass(java.lang.String _styleClass) {
getStateHelper().put(PropertyKeys.styleClass, _styleClass);
handleAttribute("styleClass", _styleClass);
}
public javax.el.MethodExpression getCloseListener() {
return (javax.el.MethodExpression) getStateHelper().eval(PropertyKeys.closeListener, null);
}
public void setCloseListener(javax.el.MethodExpression _closeListener) {
getStateHelper().put(PropertyKeys.closeListener, _closeListener);
handleAttribute("closeListener", _closeListener);
}
public java.lang.String getOnCloseUpdate() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.onCloseUpdate, null);
}
public void setOnCloseUpdate(java.lang.String _onCloseUpdate) {
getStateHelper().put(PropertyKeys.onCloseUpdate, _onCloseUpdate);
handleAttribute("onCloseUpdate", _onCloseUpdate);
}
public java.lang.String getShowEffect() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.showEffect, null);
}
public void setShowEffect(java.lang.String _showEffect) {
getStateHelper().put(PropertyKeys.showEffect, _showEffect);
handleAttribute("showEffect", _showEffect);
}
public java.lang.String getHideEffect() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.hideEffect, null);
}
public void setHideEffect(java.lang.String _hideEffect) {
getStateHelper().put(PropertyKeys.hideEffect, _hideEffect);
handleAttribute("hideEffect", _hideEffect);
}
public java.lang.String getPosition() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.position, null);
}
public void setPosition(java.lang.String _position) {
getStateHelper().put(PropertyKeys.position, _position);
handleAttribute("position", _position);
}
public boolean isCloseOnEscape() {
return (java.lang.Boolean) getStateHelper().eval(PropertyKeys.closeOnEscape, true);
}
public void setCloseOnEscape(boolean _closeOnEscape) {
getStateHelper().put(PropertyKeys.closeOnEscape, _closeOnEscape);
handleAttribute("closeOnEscape", _closeOnEscape);
}
public boolean isClosable() {
return (java.lang.Boolean) getStateHelper().eval(PropertyKeys.closable, true);
}
public void setClosable(boolean _closable) {
getStateHelper().put(PropertyKeys.closable, _closable);
handleAttribute("closable", _closable);
}
public void broadcast(javax.faces.event.FacesEvent event) throws javax.faces.event.AbortProcessingException {
super.broadcast(event);
FacesContext facesContext = FacesContext.getCurrentInstance();
MethodExpression me = getCloseListener();
if (me != null) {
me.invoke(facesContext.getELContext(), new Object[] {event});
}
}
protected FacesContext getFacesContext() {
return FacesContext.getCurrentInstance();
}
public boolean resourceExists(FacesContext facesContext, String name) {
java.util.List<UIComponent> resources = facesContext.getViewRoot().getComponentResources(facesContext, "head");
for(UIComponent component : resources) {
String value = component.toString();
if(value != null && value.equals(name))
return true;
}
return false;
}
@Override
public void processEvent(ComponentSystemEvent event) throws AbortProcessingException {
FacesContext facesContext = getFacesContext();
javax.faces.component.UIViewRoot viewroot = facesContext.getViewRoot();
Resource resource = null;
if(!resourceExists(facesContext, "/jquery/plugins/ui/jquery.ui.dialog.css")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/jquery/plugins/ui/jquery.ui.dialog.css");
viewroot.addComponentResource(facesContext, resource, "head");
}
if(!resourceExists(facesContext, "/jquery/plugins/ui/jquery.ui.resizable.css")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/jquery/plugins/ui/jquery.ui.resizable.css");
viewroot.addComponentResource(facesContext, resource, "head");
}
if(!resourceExists(facesContext, "/jquery/jquery.js")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/jquery/jquery.js");
viewroot.addComponentResource(facesContext, resource, "head");
}
if(!resourceExists(facesContext, "/jquery/plugins/ui/jquery-ui.custom.js")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/jquery/plugins/ui/jquery-ui.custom.js");
viewroot.addComponentResource(facesContext, resource, "head");
}
if(!resourceExists(facesContext, "/primefaces/core/core.js")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/primefaces/core/core.js");
viewroot.addComponentResource(facesContext, resource, "head");
}
if(!resourceExists(facesContext, "/primefaces/dialog/dialog.js")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/primefaces/dialog/dialog.js");
viewroot.addComponentResource(facesContext, resource, "head");
}
}
public void handleAttribute(String name, Object value) {
List<String> setAttributes = (List<String>) this.getAttributes().get("javax.faces.component.UIComponentBase.attributesThatAreSet");
if(setAttributes == null) {
String cname = this.getClass().getName();
if(cname != null && cname.startsWith(OPTIMIZED_PACKAGE)) {
setAttributes = new ArrayList<String>(6);
this.getAttributes().put("javax.faces.component.UIComponentBase.attributesThatAreSet", setAttributes);
}
}
if(setAttributes != null) {
if(value == null) {
ValueExpression ve = getValueExpression(name);
if(ve == null) {
setAttributes.remove(name);
} else if(!setAttributes.contains(name)) {
setAttributes.add(name);
}
}
}
}
}