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.apps.cacheadmin;
029
030import org.opencms.loader.CmsImageLoader;
031import org.opencms.main.CmsEvent;
032import org.opencms.main.I_CmsEventListener;
033import org.opencms.main.OpenCms;
034import org.opencms.ui.CmsVaadinUtils;
035import org.opencms.ui.FontOpenCms;
036import org.opencms.ui.apps.cacheadmin.CmsFlushCache.I_CloseableDialog;
037import org.opencms.ui.components.CmsBasicDialog;
038import org.opencms.ui.components.CmsDateField;
039
040import java.util.Calendar;
041import java.util.Collections;
042
043import com.vaadin.ui.Button;
044import com.vaadin.ui.Button.ClickEvent;
045import com.vaadin.ui.Button.ClickListener;
046import com.vaadin.v7.shared.ui.label.ContentMode;
047import com.vaadin.v7.ui.Label;
048
049/**
050 * Dialog to clean Image Cache.<p>
051 */
052public class CmsImageCacheCleanDialog extends CmsBasicDialog implements I_CloseableDialog {
053
054    /**vaadin serial id.*/
055    private static final long serialVersionUID = -6902585433676013120L;
056
057    /**Runnable for close action.*/
058    Runnable m_closeRunnable;
059
060    /**Runnable for ok action. */
061    Runnable m_okRunnable;
062
063    /**Vaadin component.*/
064    private Button m_cancelButton;
065
066    /**Date field.*/
067    private CmsDateField m_dateField;
068
069    /**Vaadin component.*/
070    private Label m_icon;
071
072    /**Vaadin component.*/
073    private Button m_okButton;
074
075    /**
076     * Public constructor.<p>
077     */
078    public CmsImageCacheCleanDialog() {
079
080        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
081
082        //Setup icon
083        m_icon.setContentMode(ContentMode.HTML);
084        m_icon.setValue(FontOpenCms.WARNING.getHtml());
085
086        Calendar cal = Calendar.getInstance();
087        cal.add(Calendar.MINUTE, -30);
088        m_dateField.setDate(cal.getTime());
089        m_okButton.addClickListener(new ClickListener() {
090
091            private static final long serialVersionUID = 8281661241498918564L;
092
093            public void buttonClick(ClickEvent event) {
094
095                flushCache();
096                m_closeRunnable.run();
097                if (m_okRunnable != null) {
098                    m_okRunnable.run();
099                }
100            }
101        });
102        m_cancelButton.addClickListener(new ClickListener() {
103
104            private static final long serialVersionUID = -936541994114016527L;
105
106            public void buttonClick(ClickEvent event) {
107
108                m_closeRunnable.run();
109            }
110        });
111    }
112
113    /**
114     * @see org.opencms.ui.apps.cacheadmin.CmsFlushCache.I_CloseableDialog#setCloseRunnable(java.lang.Runnable)
115     */
116    public void setCloseRunnable(Runnable closeRunnable) {
117
118        m_closeRunnable = closeRunnable;
119
120    }
121
122    /**
123     * @see org.opencms.ui.apps.cacheadmin.CmsFlushCache.I_CloseableDialog#setOkRunnable(java.lang.Runnable)
124     */
125    public void setOkRunnable(Runnable okRunnable) {
126
127        m_okRunnable = okRunnable;
128
129    }
130
131    /**
132     * Clears the Image Cache according to value of date field.<p>
133     */
134    void flushCache() {
135
136        float age = (System.currentTimeMillis() - m_dateField.getDate().getTime()) / (60f * 60f * 1000f);
137        OpenCms.fireCmsEvent(
138            new CmsEvent(
139                I_CmsEventListener.EVENT_CLEAR_CACHES,
140                Collections.<String, Object> singletonMap(CmsImageLoader.PARAM_CLEAR_IMAGES_CACHE, "" + age)));
141    }
142}