View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.gui.dialog;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.core.ItemType;
38  import info.magnolia.cms.gui.control.Button;
39  import info.magnolia.cms.gui.control.Edit;
40  import info.magnolia.cms.gui.misc.CssConstants;
41  
42  import java.io.IOException;
43  import java.io.Writer;
44  import java.util.ArrayList;
45  import java.util.Iterator;
46  import java.util.List;
47  
48  import javax.jcr.PropertyType;
49  import javax.jcr.RepositoryException;
50  import javax.servlet.http.HttpServletRequest;
51  import javax.servlet.http.HttpServletResponse;
52  
53  import org.slf4j.Logger;
54  import org.slf4j.LoggerFactory;
55  
56  
57  /**
58   * <p>
59   * A simple input text with additional configurable buttons. For most uses you can probably stay with the standard date
60   * and link dialogs provided with Magnolia, but if you need additional or custom buttons you can use this control.
61   * </p>
62   * <p>
63   * Sample configuration:
64   * </p>
65   *
66   * <pre>
67   * + dialog
68   *   + tabText
69   *     + sampleEdit
70   *       + buttons
71   *         + button1
72   *           * label      "Say hello"
73   *           * onclick    "alert('hello!')"
74   *         + button2
75   *           * label      "Browse"
76   *           * onclick    "mgnlDialogLinkOpenBrowser('link','website','html', false)"
77   *         + button3
78   *           * label      "Clear"
79   *           * onclick    "getElementById('link').value=''"
80   *       * type           "String"
81   *       * label          "sample link"
82   *       * rows           "1"
83   *       * name           "link"
84   *       * controlType    "editWithButtons"
85   * </pre>
86   *
87   * @author Fabrizio Giustina
88   * @since 2.2
89   */
90  public class DialogEditWithCustomButtons extends DialogBox {
91  
92      /**
93       * Logger.
94       */
95      private static Logger log = LoggerFactory.getLogger(DialogEditWithCustomButtons.class);
96  
97      /**
98       * List of Buttons loaded from configuration.
99       */
100     private List buttons = new ArrayList();
101 
102     /**
103      * @see info.magnolia.cms.gui.dialog.DialogControl#init(HttpServletRequest, HttpServletResponse, Content, Content)
104      */
105     public void init(HttpServletRequest request, HttpServletResponse response, Content websiteNode, Content configNode)
106         throws RepositoryException {
107         super.init(request, response, websiteNode, configNode);
108 
109         try {
110             Iterator it = configNode.getContent("buttons").getChildren(ItemType.CONTENTNODE.getSystemName()).iterator(); //$NON-NLS-1$
111             while (it.hasNext()) {
112                 Content n = (Content) it.next();
113 
114                 Button button = new Button();
115                 button.setOnclick(n.getNodeData("onclick").getString()); //$NON-NLS-1$
116 
117                 String label = null;
118                 if (n.getNodeData("label").isExist()) { //$NON-NLS-1$
119                     label = n.getNodeData("label").getString(); //$NON-NLS-1$
120                     label = this.getMessage(label);
121                 }
122                 button.setLabel(label);
123 
124                 buttons.add(button);
125             }
126         }
127         catch (RepositoryException e) {
128             log.error("Exception caught: " + e.getMessage(), e); //$NON-NLS-1$
129         }
130 
131     }
132 
133     /**
134      * @see info.magnolia.cms.gui.dialog.DialogControl#drawHtml(Writer)
135      */
136     public void drawHtml(Writer out) throws IOException {
137         Edit control = new Edit(this.getName(), this.getValue());
138         control.setType(this.getConfigValue("type", PropertyType.TYPENAME_STRING)); //$NON-NLS-1$
139         if (this.getConfigValue("saveInfo").equals("false")) { //$NON-NLS-1$ //$NON-NLS-2$
140             control.setSaveInfo(false);
141         }
142         control.setCssClass(CssConstants.CSSCLASS_EDIT);
143         control.setRows(this.getConfigValue("rows", "1")); //$NON-NLS-1$ //$NON-NLS-2$
144         control.setCssStyles("width", "100%"); //$NON-NLS-1$ //$NON-NLS-2$
145         if (this.getConfigValue("onchange", null) != null) { //$NON-NLS-1$
146             control.setEvent("onchange", this.getConfigValue("onchange")); //$NON-NLS-1$ //$NON-NLS-2$
147         }
148         this.drawHtmlPre(out);
149         String width = this.getConfigValue("width", "95%"); //$NON-NLS-1$ //$NON-NLS-2$
150         out.write("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"" + width + "\">"); //$NON-NLS-1$ //$NON-NLS-2$
151         out.write("<tr><td width=\"100%\"  class=\"" + CssConstants.CSSCLASS_EDITWITHBUTTON + "\">"); //$NON-NLS-1$ //$NON-NLS-2$
152         out.write(control.getHtml());
153 
154         for (Iterator iter = this.buttons.iterator(); iter.hasNext();) {
155             Button button = (Button) iter.next();
156             out.write("</td><td></td><td class=\"" + CssConstants.CSSCLASS_EDITWITHBUTTON + "\">"); //$NON-NLS-1$ //$NON-NLS-2$
157             out.write(button.getHtml());
158         }
159 
160         out.write("</td></tr></table>"); //$NON-NLS-1$
161 
162         this.drawHtmlPost(out);
163     }
164 }