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.dbmanager;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsLog;
034import org.opencms.main.OpenCms;
035import org.opencms.ui.A_CmsUI;
036import org.opencms.ui.CmsCssIcon;
037import org.opencms.ui.CmsVaadinUtils;
038import org.opencms.ui.FontOpenCms;
039import org.opencms.ui.components.CmsBasicDialog;
040import org.opencms.ui.components.CmsResourceInfo;
041import org.opencms.ui.components.OpenCmsTheme;
042import org.opencms.ui.report.CmsReportWidget;
043import org.opencms.workplace.Messages;
044
045import java.util.Collections;
046import java.util.List;
047
048import org.apache.commons.logging.Log;
049
050import com.vaadin.v7.data.Property.ValueChangeEvent;
051import com.vaadin.v7.data.Property.ValueChangeListener;
052import com.vaadin.v7.shared.ui.label.ContentMode;
053import com.vaadin.ui.Button;
054import com.vaadin.ui.Button.ClickEvent;
055import com.vaadin.ui.Button.ClickListener;
056import com.vaadin.v7.ui.CheckBox;
057import com.vaadin.ui.FormLayout;
058import com.vaadin.v7.ui.Label;
059import com.vaadin.ui.Panel;
060import com.vaadin.v7.ui.VerticalLayout;
061import com.vaadin.ui.Window;
062
063/**
064 * Dialog to delete property definitions.<p>
065 */
066public class CmsPropertyDeleteDialog extends CmsBasicDialog {
067
068    /**vaadin serial id.*/
069    private static final long serialVersionUID = -3397853426667893254L;
070
071    /** The logger for this class. */
072    static Log LOG = CmsLog.getLog(CmsPropertyDeleteDialog.class.getName());
073
074    /**List of resources having the property to be deleted.*/
075    protected List<CmsResource> m_resources;
076
077    /**Vaadin component.*/
078    private VerticalLayout m_forceDeleteLayout;
079
080    /**Vaadin component.*/
081    protected VerticalLayout m_start;
082
083    /**Vaadin component.*/
084    private Label m_icon;
085
086    /**Vaadin component.*/
087    protected Button m_cancelButton;
088
089    /**Vaadin component.*/
090    protected Button m_okButton;
091
092    /**Vaadin component.*/
093    protected CheckBox m_forceDelete;
094
095    /**Vaadin component.*/
096    protected FormLayout m_threadReport;
097
098    /**Vaadin component.*/
099    protected Panel m_report;
100
101    /**CmsObject.*/
102    private CmsObject m_cms;
103
104    /**
105     * public constructor.<p>
106     *
107     * @param propName name of property
108     * @param window to be closed
109     * @param runnable to update table
110     */
111    public CmsPropertyDeleteDialog(final String propName, final Window window, final Runnable runnable) {
112        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
113
114        displayResourceInfoDirectly(
115            Collections.singletonList(new CmsResourceInfo(propName, "", new CmsCssIcon(OpenCmsTheme.ICON_DATABASE))));
116
117        //Setup icon
118        m_icon.setContentMode(ContentMode.HTML);
119        m_icon.setValue(FontOpenCms.WARNING.getHtml());
120
121        m_report.setVisible(false);
122
123        try {
124            m_resources = getCms().readResourcesWithProperty(propName);
125            m_forceDeleteLayout.setVisible(!m_resources.isEmpty());
126            m_okButton.setEnabled(m_resources.isEmpty());
127        } catch (CmsException e) {
128            //
129        }
130        m_cancelButton.addClickListener(new ClickListener() {
131
132            private static final long serialVersionUID = 4788359189538313935L;
133
134            public void buttonClick(ClickEvent event) {
135
136                window.close();
137            }
138        });
139
140        m_forceDelete.addValueChangeListener(new ValueChangeListener() {
141
142            private static final long serialVersionUID = 7482690600008082762L;
143
144            public void valueChange(ValueChangeEvent event) {
145
146                m_okButton.setEnabled(m_forceDelete.getValue().booleanValue());
147
148            }
149
150        });
151
152        m_okButton.addClickListener(new ClickListener() {
153
154            private static final long serialVersionUID = -7861406021237202016L;
155
156            public void buttonClick(ClickEvent event) {
157
158                if (!m_resources.isEmpty()) {
159                    m_start.setVisible(false);
160                    m_report.setVisible(true);
161                    m_okButton.setEnabled(false);
162
163                    CmsRemovePropertyFromResourcesThread thread = new CmsRemovePropertyFromResourcesThread(
164                        getCms(),
165                        propName);
166                    m_threadReport.addComponent(new CmsReportWidget(thread));
167
168                    thread.start();
169                    m_cancelButton.setCaption(CmsVaadinUtils.getMessageText(Messages.GUI_DIALOG_BUTTON_CLOSE_0));
170                    m_cancelButton.addClickListener(new ClickListener() {
171
172                        public void buttonClick(ClickEvent event) {
173
174                            runnable.run();
175
176                        }
177
178                    });
179                    return;
180                }
181                try {
182                    getCms().deletePropertyDefinition(propName);
183                    runnable.run();
184                } catch (CmsException e) {
185                    LOG.error("Unable to delete property definition", e);
186                }
187                window.close();
188            }
189
190        });
191    }
192
193    /**
194     * Gets a copy of the cms object set to root site.<p>
195     *
196     * @return CmsObject
197     */
198    protected CmsObject getCms() {
199
200        if (m_cms == null) {
201            try {
202                m_cms = OpenCms.initCmsObject(A_CmsUI.getCmsObject());
203                m_cms.getRequestContext().setSiteRoot("");
204            } catch (CmsException e) {
205                return null;
206            }
207        }
208        return m_cms;
209    }
210}