View Javadoc

1   /**
2    * This file Copyright (c) 2013 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.module.groovy.field;
35  
36  import groovy.lang.Binding;
37  
38  import info.magnolia.context.Context;
39  import info.magnolia.context.MgnlContext;
40  import info.magnolia.i18nsystem.SimpleTranslator;
41  import info.magnolia.module.groovy.console.MgnlGroovyConsole;
42  import info.magnolia.module.groovy.console.MgnlGroovyConsoleContext;
43  
44  import java.io.ByteArrayInputStream;
45  import java.io.StringWriter;
46  import java.io.Writer;
47  
48  import com.vaadin.data.Item;
49  import com.vaadin.data.Property;
50  import com.vaadin.ui.Alignment;
51  import com.vaadin.ui.Button;
52  import com.vaadin.ui.Button.ClickEvent;
53  import com.vaadin.ui.Component;
54  import com.vaadin.ui.CustomField;
55  import com.vaadin.ui.NativeButton;
56  import com.vaadin.ui.TextArea;
57  import com.vaadin.ui.VerticalLayout;
58  
59  /**
60   * ConsoleOutputField.
61   */
62  public class ConsoleOutputField extends CustomField<String> {
63  
64      private final VerticalLayout rootLayout = new VerticalLayout();
65      private final TextArea outputArea = new TextArea();
66      private final Button runButton = new NativeButton();
67  
68      private Item item;
69      private SimpleTranslator simpleTranslator;
70  
71      public ConsoleOutputField(Item item, SimpleTranslator simpleTranslator) {
72          this.item = item;
73          this.simpleTranslator = simpleTranslator;
74      }
75  
76      @Override
77      protected Component initContent() {
78          setImmediate(true);
79          rootLayout.setSizeFull();
80          rootLayout.setSpacing(true);
81  
82          runButton.addStyleName("magnoliabutton");
83          runButton.setCaption(simpleTranslator.translate("groovy.script.consoleOutput.run"));
84          runButton.addClickListener(createButtonClickListener());
85  
86          rootLayout.addComponent(runButton);
87          rootLayout.setExpandRatio(runButton, 0);
88          rootLayout.setComponentAlignment(runButton, Alignment.MIDDLE_LEFT);
89  
90          outputArea.setImmediate(true);
91          outputArea.addStyleName("console-output");
92          outputArea.setWidth(100, Unit.PERCENTAGE);
93          outputArea.setNullRepresentation("");
94          outputArea.setNullSettingAllowed(true);
95  
96          rootLayout.addComponent(outputArea);
97  
98          return rootLayout;
99      }
100 
101     @Override
102     public String getValue() {
103         return outputArea.getValue();
104     }
105 
106     @Override
107     public Class<String> getType() {
108         return String.class;
109     }
110 
111     @SuppressWarnings("unchecked")
112     private void runScript() {
113 
114         Context originalCtx = MgnlContext.getInstance();
115         try {
116             outputArea.setValue(simpleTranslator.translate("groovy.script.consoleOutput.run.wait"));
117             runButton.setEnabled(false);
118 
119             MgnlGroovyConsoleContext groovyCtx = new MgnlGroovyConsoleContext(originalCtx);
120             MgnlContext.setInstance(groovyCtx);
121             MgnlGroovyConsole console = new MgnlGroovyConsole(new Binding());
122             Property<String> scriptText = item.getItemProperty("text");
123 
124             Writer writer = new StringWriter();
125 
126             Object result = console.evaluate(new ByteArrayInputStream(scriptText.getValue().getBytes()), console.generateScriptName(), writer);
127 
128             StringBuilder sb = new StringBuilder().append(writer.toString()).append("\n").append(result);
129 
130             outputArea.setValue(sb.toString());
131         } catch (Exception e) {
132             outputArea.setValue(e.toString());
133         } finally {
134             MgnlContext.setInstance(originalCtx);
135             runButton.setEnabled(true);
136         }
137     };
138 
139     private Button.ClickListener createButtonClickListener() {
140         return new Button.ClickListener() {
141             @Override
142             public void buttonClick(ClickEvent event) {
143                 runScript();
144             }
145         };
146     }
147 }