/* * Copyright 2004-2013 ICEsoft Technologies Canada Corp. * * 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.icefaces.samples.showcase.example.ace.file; import org.icefaces.application.PortableRenderer; import org.icefaces.application.PushOptions; import org.icefaces.application.PushRenderer; import org.icefaces.impl.event.BridgeSetup; import org.icefaces.samples.showcase.dataGenerators.ImageSet.ImageInfo; import org.icefaces.samples.showcase.metadata.annotation.*; import org.icefaces.samples.showcase.metadata.context.ComponentExampleImpl; import org.icefaces.ace.component.fileentry.*; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.faces.bean.CustomScoped; import javax.faces.bean.ManagedBean; import javax.faces.context.FacesContext; import javax.faces.event.AbortProcessingException; import javax.faces.event.PreRenderViewEvent; import javax.faces.event.SystemEvent; import javax.faces.event.SystemEventListener; import java.io.File; import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; import org.icefaces.samples.showcase.dataGenerators.ImageSet; import org.icefaces.util.EnvUtils; import org.icefaces.util.JavaScriptRunner; @ComponentExample( title = "example.ace.fileentry.title", description = "example.ace.fileentry.description", example = "/resources/examples/ace/fileentry/fileentry.xhtml" ) @ExampleResources( resources ={ // xhtml @ExampleResource(type = ResourceType.xhtml, title="FileEntry.xhtml", resource = "/resources/examples/ace/"+ "fileentry/fileentry.xhtml"), // Java Source @ExampleResource(type = ResourceType.java, title="FileEntryBean.java", resource = "/WEB-INF/classes/org/icefaces/samples/"+ "showcase/example/ace/file/FileEntryBean.java") } ) @Menu( title = "menu.ace.fileentry.subMenu.title", menuLinks = { @MenuLink(title = "menu.ace.fileentry.subMenu.main", isDefault = true, exampleBeanName = FileEntryBean.BEAN_NAME), @MenuLink(title = "menu.ace.fileentry.subMenu.listener", exampleBeanName = FileEntryListenerBean.BEAN_NAME), @MenuLink(title = "menu.ace.fileentry.subMenu.validation",exampleBeanName = FileEntryValidationOptionsBean.BEAN_NAME), @MenuLink(title = "menu.ace.fileentry.subMenu.callback",exampleBeanName = FileEntryCallbackBean.BEAN_NAME) }) @ManagedBean(name= FileEntryBean.BEAN_NAME) @CustomScoped(value = "#{window}") public class FileEntryBean extends ComponentExampleImpl implements Serializable /*vvv*/ ,SystemEventListener /*^^^*/ { public static final String BEAN_NAME = "fileEntry"; private List fileData; private ImageSet.ImageInfo arrowImage; /*vvv*/ private String groupName; private boolean nextServerPushUpdatePage; private boolean ieLargeFileUpdateRendered = false; /*^^^*/ public FileEntryBean() { super(FileEntryBean.class); arrowImage = ImageSet.getImage(ImageSet.ImageSelect.FORWARD_ARROW); /*vvv*/ FacesContext.getCurrentInstance().getApplication().subscribeToEvent(PreRenderViewEvent.class, null, this); /*^^^*/ } @PostConstruct public void initMetaData() { super.initMetaData(); } @PreDestroy public void windowDestroy() { //System.out.println("FileEntryBean.windowDestroy this: " + this); } public void sampleListener(FileEntryEvent e) { FileEntry fe = (FileEntry)e.getComponent(); FileEntryResults results = fe.getResults(); File parent = null; fileData = new ArrayList(); //get data About File for (FileEntryResults.FileInfo i : results.getFiles()) { fileData.add("File Name: " + i.getFileName()); if (i.isSaved()) { fileData.add("File Size: " + i.getSize() + " bytes"); File file = i.getFile(); if (file != null) { parent = file.getParentFile(); } } else { fileData.add("File was not saved because: " + i.getStatus().getFacesMessage( FacesContext.getCurrentInstance(), fe, i).getSummary()); } } if (parent != null) { long dirSize = 0; int fileCount = 0; for (File file : parent.listFiles()) { fileCount++; dirSize += file.length(); } fileData.add("Total Files In Upload Directory: " + fileCount); fileData.add("Total Size of Files In Directory: " + dirSize + " bytes"); } //System.out.println("FileEntryBean.sampleListener() " + fileData + " this: " + this); /*vvv*/ if (groupName != null) { nextServerPushUpdatePage = true; final PortableRenderer portable = PushRenderer.getPortableRenderer(); ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1); executor.schedule(new Runnable() { public void run() { try { HashMap options = new HashMap(1); options.put("ieLargeFileUpdate_pushed", "true"); portable.render(groupName, new PushOptions(options)); } catch (Exception e) { e.printStackTrace(); } } }, 10, TimeUnit.SECONDS); executor.shutdown(); JavaScriptRunner.runScript(FacesContext.getCurrentInstance(), "ice.ser(null, 'ieLargeFileUpdateForm:ieLargeFileUpdateButton', function(parameter){parameter('ieLargeFileUpdate_ranPosted','true');});"); } /*^^^*/ } /*vvv*/ public void ieLargeFileUpdate() { //System.out.println("FileEntryBean.ieLargeFileUpdate()"); if (FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().containsKey("ieLargeFileUpdate_ranPosted")) { nextServerPushUpdatePage = false; //System.out.println("FileEntryBean.ieLargeFileUpdate() nextServerPushUpdatePage = false"); } } public boolean isIeLargeFileUpdateRendered() { return ieLargeFileUpdateRendered; } public void processEvent(SystemEvent event) throws AbortProcessingException { //System.out.println("FileEntryBean.processEvent() event: " + event); FacesContext facesContext = FacesContext.getCurrentInstance(); if (groupName == null) { groupName = "ieLargeFileUpdateGroup_" + BridgeSetup.getViewID(facesContext.getExternalContext()); PushRenderer.addCurrentView(groupName); //System.out.println("FileEntryBean.processEvent() Setup push rendering"); } else if (nextServerPushUpdatePage && EnvUtils.isPushRequest(facesContext)) { ieLargeFileUpdateRendered = !ieLargeFileUpdateRendered; nextServerPushUpdatePage = false; //System.out.println("FileEntryBean.processEvent() Flipped rendered"); } } public boolean isListenerForSource(Object source) { return true; } /*^^^*/ public List getFileData() { return fileData; } public ImageInfo getArrowImage() { return arrowImage; } public void setArrowImage(ImageInfo arrowImage) { this.arrowImage = arrowImage; } }