001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.ui.report; 029 030import org.opencms.report.A_CmsReportThread; 031import org.opencms.report.CmsVaadinHtmlReportUpdateFormatter; 032import org.opencms.report.CmsWorkplaceReport; 033import org.opencms.ui.shared.components.CmsReportWidgetState; 034import org.opencms.ui.shared.rpc.I_CmsReportClientRpc; 035import org.opencms.ui.shared.rpc.I_CmsReportServerRpc; 036 037import java.util.List; 038 039import com.google.common.collect.Lists; 040import com.google.gwt.event.shared.HandlerRegistration; 041import com.vaadin.ui.AbstractComponent; 042 043/** 044 * A widget used to display an OpenCms report.<p> 045 */ 046public class CmsReportWidget extends AbstractComponent implements I_CmsReportServerRpc { 047 048 /** Serial version id. */ 049 private static final long serialVersionUID = 1L; 050 051 /** The report update formatter. */ 052 private CmsVaadinHtmlReportUpdateFormatter m_formatter = new CmsVaadinHtmlReportUpdateFormatter(); 053 054 /** Handlers to execute when the report finishes while displaying the report widget. */ 055 private List<Runnable> m_reportFinishedHandlers = Lists.newArrayList(); 056 057 /** The report thread. */ 058 private A_CmsReportThread m_thread; 059 060 /** True if the report thread is finished. */ 061 private boolean m_threadFinished; 062 063 /** The report to display. */ 064 private CmsWorkplaceReport m_report; 065 066 /** 067 * Creates a new instance.<p> 068 * Use in declarative layouts, remember to call .<p> 069 * This does not start the report thread.<p> 070 */ 071 public CmsReportWidget() { 072 073 registerRpc(this, I_CmsReportServerRpc.class); 074 } 075 076 /** 077 * Creates a new instance.<p> 078 * 079 * This does not start the report thread. 080 * 081 * @param thread the report thread 082 */ 083 public CmsReportWidget(A_CmsReportThread thread) { 084 085 this(); 086 m_thread = thread; 087 } 088 089 /** 090 * Creates a new instance.<p> 091 * Use this constructor in case no report thread is available.<p> 092 * 093 * @param report the report to display 094 */ 095 public CmsReportWidget(CmsWorkplaceReport report) { 096 097 this(); 098 m_report = report; 099 } 100 101 /** 102 * Adds an action that should be executed if the report is finished.<p> 103 * 104 * Note that this action will only be called if the report is finished while the report widget is actually 105 * displayed. For example, if the user closes the browser window before the report is finished, this will not be executed. 106 * 107 * @param handler the handler 108 * @return the handler registration 109 */ 110 public HandlerRegistration addReportFinishedHandler(final Runnable handler) { 111 112 m_reportFinishedHandlers.add(handler); 113 return new HandlerRegistration() { 114 115 @SuppressWarnings("synthetic-access") 116 public void removeHandler() { 117 118 m_reportFinishedHandlers.remove(handler); 119 } 120 }; 121 } 122 123 /** 124 * @see org.opencms.ui.shared.rpc.I_CmsReportServerRpc#requestReportUpdate() 125 */ 126 public void requestReportUpdate() { 127 128 String reportUpdate = null; 129 if (m_thread != null) { 130 if (!m_threadFinished) { 131 // if thread is not alive at this point, there may still be report updates 132 reportUpdate = m_thread.getReportUpdate(m_formatter); 133 if (!m_thread.isAlive()) { 134 m_threadFinished = true; 135 runReportFinishedHandlers(); 136 } 137 } 138 } else if (m_report != null) { 139 reportUpdate = m_report.getReportUpdate(m_formatter); 140 } 141 getRpcProxy(I_CmsReportClientRpc.class).handleReportUpdate(reportUpdate); 142 } 143 144 /** 145 * Sets the report thread.<p> 146 * 147 * @param thread the report thread 148 */ 149 public void setReportThread(A_CmsReportThread thread) { 150 151 m_thread = thread; 152 } 153 154 /** 155 * @see com.vaadin.ui.AbstractComponent#getState() 156 */ 157 @Override 158 protected CmsReportWidgetState getState() { 159 160 return (CmsReportWidgetState)(super.getState()); 161 } 162 163 /** 164 * Runs the 'report finished' handlers. 165 */ 166 protected void runReportFinishedHandlers() { 167 168 for (Runnable handler : m_reportFinishedHandlers) { 169 handler.run(); 170 } 171 } 172}