From b67c9f4fac973ef1b28d8f09d51752346ceb9953 Mon Sep 17 00:00:00 2001 From: baka Date: Fri, 21 Oct 2016 15:38:36 +0000 Subject: [PATCH] Adds BaseCategoryForm, as well as CategoryRequestLocal - Changed imports to fit the new system - The Form class now uses normal Java Collections git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4396 8810af33-2d31-482b-a856-94f89814c4df --- .../cms/ui/category/BaseCategoryForm.java | 192 ++++++++++++++++++ .../cms/ui/category/CategoryRequestLocal.java | 34 ++++ 2 files changed, 226 insertions(+) create mode 100755 ccm-cms/src/main/java/com/arsdigita/cms/ui/category/BaseCategoryForm.java create mode 100755 ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryRequestLocal.java diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/BaseCategoryForm.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/BaseCategoryForm.java new file mode 100755 index 000000000..037e58a65 --- /dev/null +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/BaseCategoryForm.java @@ -0,0 +1,192 @@ +/* + * Copyright (C) 2003-2004 Red Hat Inc. All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ +package com.arsdigita.cms.ui.category; + +import com.arsdigita.bebop.FormProcessException; +import com.arsdigita.bebop.Label; +import com.arsdigita.bebop.PageState; +import com.arsdigita.bebop.event.ParameterEvent; +import com.arsdigita.bebop.event.ParameterListener; +import com.arsdigita.bebop.form.Option; +import com.arsdigita.bebop.form.RadioGroup; +import com.arsdigita.bebop.form.TextArea; +import com.arsdigita.bebop.form.TextField; +import com.arsdigita.bebop.form.Widget; +import com.arsdigita.bebop.parameters.NotNullValidationListener; +import com.arsdigita.bebop.parameters.TrimmedStringParameter; +import com.arsdigita.bebop.util.GlobalizationUtil; +import com.arsdigita.cms.ui.BaseForm; +import com.arsdigita.globalization.GlobalizedMessage; +import com.arsdigita.web.Web; +import com.arsdigita.xml.Element; + +import org.apache.log4j.Logger; +import org.libreccm.categorization.Category; + +import java.util.Collection; + +/** + * A form which creates a new category. Extends the edit form for + * convenience. + * + * @author Yannick Bülter + * @author Michael Pih + * @author Stanislav Freidin <sfreidin@redhat.com> + * @author Justin Ross <jross@redhat.com> + * @version $Id: BaseCategoryForm.java 1951 2009-06-30 04:35:04Z terry $ + */ +class BaseCategoryForm extends BaseForm { + + private static final Logger s_log = Logger.getLogger(BaseCategoryForm.class); + final CategoryRequestLocal m_parent; + final TextField m_name; + final TextArea m_description; + final TextField m_url; + final RadioGroup m_isAbstract; + final RadioGroup m_isVisible; + final RadioGroup m_isEnabled; + private Label m_script = new Label( new GlobalizedMessage(String.format( + "", + Web.getWebappContextPath())), + false); + private final static String NAME = "name"; + private final static String DESCRIPTION = "description"; + private final static String URL = "url"; + private final static String IS_ABSTRACT = "isAbstract"; + private final static String IS_VISIBLE = "isVisible"; + private final static String IS_ENABLED = "isEnabled"; + + /** + * Constructor. + */ + BaseCategoryForm(final String key, + final GlobalizedMessage heading, + final CategoryRequestLocal parent) { + super(key, heading); + + m_parent = parent; + + m_name = new TextField(new TrimmedStringParameter(NAME)); + addField(gz("cms.ui.name"), m_name); + + m_name.setSize(30); + m_name.setMaxLength(200); + m_name.addValidationListener(new NotNullValidationListener()); + m_name.setOnFocus("if (this.form." + URL + ".value == '') { " + " defaulting = true; this.form." + URL + + ".value = urlize(this.value); }"); + m_name.setOnKeyUp("if (defaulting) { this.form." + URL + ".value = urlize(this.value) }"); + + // is abstract? + m_isAbstract = new RadioGroup(IS_ABSTRACT); + m_isAbstract.addOption(new Option("no", new Label(gz("cms.ui.no")))); + m_isAbstract.addOption(new Option("yes", new Label(gz("cms.ui.yes")))); + addField(gz("cms.ui.category.is_not_abstract"), m_isAbstract); + + // is visible + m_isVisible = new RadioGroup(IS_VISIBLE); + m_isVisible.addOption(new Option("no", new Label(gz("cms.ui.no")))); + m_isVisible.addOption(new Option("yes", new Label(gz("cms.ui.yes")))); + addField(gz("cms.ui.category.is_visible"), m_isVisible); + + // is enabled? + m_isEnabled = new RadioGroup(IS_ENABLED); + m_isEnabled.addOption(new Option("no", new Label(gz("cms.ui.no")))); + m_isEnabled.addOption(new Option("yes", new Label(gz("cms.ui.yes")))); + addField(gz("cms.ui.category.is_enabled"), m_isEnabled); + + + m_description = new TextArea(new TrimmedStringParameter(DESCRIPTION)); + addField(gz("cms.ui.description"), m_description); + + m_description.setWrap(TextArea.SOFT); + m_description.setRows(5); + m_description.setCols(40); + + // URL + // JavaScript auto-url generation is off by default. + // It is turned on under the following circumstances + // + // * If the url is null, upon starting edit of the title + // * If the url is null, upon finishing edit of name + // + // The rationale is that, auto-url generation is useful + // if the url is currently null, but once a name has been + // created you don't want to subsequently change it since + // it breaks URLs & potentially overwrites the user's + // customizations. + m_url = new TextField(new TrimmedStringParameter(URL)); + m_url.setSize(30); + m_url.setMaxLength(200); + m_url.addValidationListener(new NotNullValidationListener()); + m_url.setOnFocus("defaulting = false"); + m_url.setOnBlur("if (this.value == '') " + "{ defaulting = true; this.value = urlize(this.form." + NAME + + ".value) } " + "else { this.value = urlize(this.value); }"); + addField(gz("cms.ui.category.url"), m_url); + + addAction(new Finish()); + addAction(new Cancel()); + } + + public void generateXML(PageState ps, Element parent) { + m_script.generateXML(ps, parent); + super.generateXML(ps, parent); + } + + class NameUniqueListener implements ParameterListener { + + private final CategoryRequestLocal m_category; + private final Widget m_widget; + private final int m_type; + public final static int NAME_FIELD = 1; + public final static int URL_FIELD = 2; + + NameUniqueListener(final CategoryRequestLocal category) { + this(category, m_name, NAME_FIELD); + } + + NameUniqueListener(final CategoryRequestLocal category, + Widget widget, int type) { + m_category = category; + m_widget = widget; + m_type = type; + } + + @Override + public final void validate(final ParameterEvent e) + throws FormProcessException { + final PageState state = e.getPageState(); + final String title = (String) m_widget.getValue(state); + + final Category parent = m_parent.getCategory(state); + + final Collection children = parent.getSubCategories(); + + for (final Category child : children) { + String compField = child.getName(); + //(m_type == URL_FIELD) ? child.getURL() : child.getName(); TODO + if (compField.equalsIgnoreCase(title) + && (m_category == null + || !m_category.getCategory(state).equals(child))) { + throw new FormProcessException(GlobalizationUtil.globalize("cms.ui.category.name_not_unique")); + } + } + } + + } +} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryRequestLocal.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryRequestLocal.java new file mode 100755 index 000000000..603f468ff --- /dev/null +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryRequestLocal.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2003-2004 Red Hat Inc. All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ +package com.arsdigita.cms.ui.category; + +import com.arsdigita.bebop.PageState; +import com.arsdigita.bebop.RequestLocal; +import org.apache.log4j.Logger; +import org.libreccm.categorization.Category; + +public class CategoryRequestLocal extends RequestLocal { + + private static final Logger s_log = Logger.getLogger + (CategoryRequestLocal.class); + + public final Category getCategory(final PageState state) { + return (Category) get(state); + } +}