View Javadoc
1   /**
2    * This file Copyright (c) 2013-2016 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 info.magnolia.i18nsystem.SimpleTranslator;
37  import info.magnolia.module.groovy.console.MgnlGroovyConsole;
38  import info.magnolia.module.groovy.console.MgnlGroovyConsole.ScriptCallback;
39  import info.magnolia.ui.framework.message.MessagesManager;
40  
41  import java.io.Writer;
42  
43  import com.vaadin.data.Item;
44  import com.vaadin.data.Property;
45  import com.vaadin.ui.Alignment;
46  import com.vaadin.ui.Button;
47  import com.vaadin.ui.Button.ClickEvent;
48  import com.vaadin.ui.Component;
49  import com.vaadin.ui.CustomField;
50  import com.vaadin.ui.NativeButton;
51  import com.vaadin.ui.TextArea;
52  import com.vaadin.ui.UI;
53  import com.vaadin.ui.VerticalLayout;
54  
55  import groovy.lang.Binding;
56  
57  /**
58   * ConsoleOutputField.
59   */
60  public class ConsoleOutputField extends CustomField<String> {
61  
62      private final VerticalLayout rootLayout = new VerticalLayout();
63      private final TextArea outputArea = new TextArea();
64      private final Button runButton = new NativeButton();
65  
66      private Item item;
67      private SimpleTranslator simpleTranslator;
68      private MessagesManager messagesManager;
69  
70      public ConsoleOutputField(Item item, SimpleTranslator simpleTranslator, MessagesManager messagesManager) {
71          this.item = item;
72          this.simpleTranslator = simpleTranslator;
73          this.messagesManager = messagesManager;
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     private void runAsync(String source) {
112         try {
113             final String waitMessage = simpleTranslator.translate("groovy.script.consoleOutput.run.wait");
114             outputArea.setValue(waitMessage);
115             runButton.setEnabled(false);
116 
117             MgnlGroovyConsole console = new MgnlGroovyConsole(new Binding(), messagesManager, simpleTranslator);
118 
119             console.runAsync(source, UI.getCurrent(), new ScriptCallback() {
120                 @Override
121                 public void onSuccess(String result) {
122                     outputArea.setValue(result);
123                     runButton.setEnabled(true);
124                 }
125 
126                 @Override
127                 public void onFailure(Throwable e) {
128                     outputArea.setValue(e.getMessage());
129                     runButton.setEnabled(true);
130                 }
131 
132                 @Override
133                 public void onProgress(Writer out) {
134                     outputArea.setValue(waitMessage.concat("\n").concat(out.toString()));
135                 }
136 
137                 @Override
138                 public boolean requiresNotificationMessageUponCompletion() {
139                     return true;
140                 }
141             });
142         } catch (Exception e) {
143             outputArea.setValue(e.toString());
144             runButton.setEnabled(true);
145         }
146     }
147 
148     private Button.ClickListener createButtonClickListener() {
149         return new Button.ClickListener() {
150             @Override
151             public void buttonClick(ClickEvent event) {
152                 Property<String> scriptText = item.getItemProperty("text");
153                 runAsync(scriptText.getValue());
154             }
155         };
156     }
157 }