001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020 package org.apache.myfaces.tobago.component; 021 022 import org.apache.commons.logging.Log; 023 import org.apache.commons.logging.LogFactory; 024 025 import javax.faces.application.Application; 026 import javax.faces.application.ViewHandler; 027 import javax.faces.context.FacesContext; 028 029 public class Window { 030 031 private static final Log LOG = LogFactory.getLog(Window.class); 032 033 private String viewId; 034 private String name; 035 private int width = 300; 036 private int height = 300; 037 private int left = 100; 038 private int top = 100; 039 private boolean dependent; 040 041 public Window(String viewId) { 042 this.viewId = viewId; 043 } 044 045 public Window(String viewId, int width, int height) { 046 this.viewId = viewId; 047 this.width = width; 048 this.height = height; 049 } 050 051 public Window(String viewId, int width, int heigth, int x, int y) { 052 this.viewId = viewId; 053 this.width = width; 054 this.height = heigth; 055 this.left = x; 056 this.top = y; 057 } 058 059 public void activate(UIPage page) { 060 page.getScriptFiles().add("script/popup.js"); 061 062 FacesContext facesContext = FacesContext.getCurrentInstance(); 063 Application application = facesContext.getApplication(); 064 ViewHandler viewHandler = application.getViewHandler(); 065 String actionUrl = viewHandler.getActionURL(facesContext, viewId); 066 actionUrl = facesContext.getExternalContext().encodeActionURL(actionUrl); 067 if (LOG.isDebugEnabled()) { 068 LOG.debug("actionUrl = '" + actionUrl + "'"); 069 } 070 071 StringBuilder buffer = new StringBuilder(); 072 buffer.append("openPopup('"); 073 buffer.append(actionUrl); 074 buffer.append("', '"); 075 buffer.append(name); 076 buffer.append("', '"); 077 buffer.append(width); 078 buffer.append("', '"); 079 buffer.append(height); 080 buffer.append("', '"); 081 if (dependent) { 082 buffer.append('p'); 083 } 084 buffer.append("', '"); 085 buffer.append(left); 086 buffer.append("', '"); 087 buffer.append(top); 088 buffer.append("');"); 089 page.getOnloadScripts().add(buffer.toString()); 090 } 091 092 public String getViewId() { 093 return viewId; 094 } 095 096 public void setViewId(String viewId) { 097 this.viewId = viewId; 098 } 099 100 public String getName() { 101 return name; 102 } 103 104 public void setName(String name) { 105 this.name = name; 106 } 107 108 public int getWidth() { 109 return width; 110 } 111 112 public void setWidth(int width) { 113 this.width = width; 114 } 115 116 public int getHeight() { 117 return height; 118 } 119 120 public void setHeight(int height) { 121 this.height = height; 122 } 123 124 public int getLeft() { 125 return left; 126 } 127 128 public void setLeft(int left) { 129 this.left = left; 130 } 131 132 public int getTop() { 133 return top; 134 } 135 136 public void setTop(int top) { 137 this.top = top; 138 } 139 140 public boolean isDependent() { 141 return dependent; 142 } 143 144 public void setDependent(boolean dependent) { 145 this.dependent = dependent; 146 } 147 } 148