Clover icon

magnolia-module-groovy 2.4.2

  1. Project Clover database Thu Jan 14 2016 16:38:54 CET
  2. Package info.magnolia.module.groovy.field

File ConsoleOutputField.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart0.png
51% of files have more coverage

Code metrics

0
35
5
1
147
87
6
0.17
7
5
1.2
9.1% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
ConsoleOutputField 62 35 9.1% 6 40
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /**
2    * This file Copyright (c) 2013-2015 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  0 toggle public ConsoleOutputField(Item item, SimpleTranslator simpleTranslator) {
72  0 this.item = item;
73  0 this.simpleTranslator = simpleTranslator;
74    }
75   
 
76  0 toggle @Override
77    protected Component initContent() {
78  0 setImmediate(true);
79  0 rootLayout.setSizeFull();
80  0 rootLayout.setSpacing(true);
81   
82  0 runButton.addStyleName("magnoliabutton");
83  0 runButton.setCaption(simpleTranslator.translate("groovy.script.consoleOutput.run"));
84  0 runButton.addClickListener(createButtonClickListener());
85   
86  0 rootLayout.addComponent(runButton);
87  0 rootLayout.setExpandRatio(runButton, 0);
88  0 rootLayout.setComponentAlignment(runButton, Alignment.MIDDLE_LEFT);
89   
90  0 outputArea.setImmediate(true);
91  0 outputArea.addStyleName("console-output");
92  0 outputArea.setWidth(100, Unit.PERCENTAGE);
93  0 outputArea.setNullRepresentation("");
94  0 outputArea.setNullSettingAllowed(true);
95   
96  0 rootLayout.addComponent(outputArea);
97   
98  0 return rootLayout;
99    }
100   
 
101    toggle @Override
102    public String getValue() {
103    return outputArea.getValue();
104    }
105   
 
106    toggle @Override
107    public Class<String> getType() {
108    return String.class;
109    }
110   
 
111  0 toggle @SuppressWarnings("unchecked")
112    private void runScript() {
113   
114  0 Context originalCtx = MgnlContext.getInstance();
115  0 try {
116  0 outputArea.setValue(simpleTranslator.translate("groovy.script.consoleOutput.run.wait"));
117  0 runButton.setEnabled(false);
118   
119  0 MgnlGroovyConsoleContext groovyCtx = new MgnlGroovyConsoleContext(originalCtx);
120  0 MgnlContext.setInstance(groovyCtx);
121  0 MgnlGroovyConsole console = new MgnlGroovyConsole(new Binding());
122  0 Property<String> scriptText = item.getItemProperty("text");
123   
124  0 Writer writer = new StringWriter();
125   
126  0 Object result = console.evaluate(new ByteArrayInputStream(scriptText.getValue().getBytes()), console.generateScriptName(), writer);
127   
128  0 StringBuilder sb = new StringBuilder().append(writer.toString()).append("\n").append(result);
129   
130  0 outputArea.setValue(sb.toString());
131    } catch (Exception e) {
132  0 outputArea.setValue(e.toString());
133    } finally {
134  0 MgnlContext.setInstance(originalCtx);
135  0 runButton.setEnabled(true);
136    }
137    };
138   
 
139  0 toggle private Button.ClickListener createButtonClickListener() {
140  0 return new Button.ClickListener() {
 
141  0 toggle @Override
142    public void buttonClick(ClickEvent event) {
143  0 runScript();
144    }
145    };
146    }
147    }