001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: */
013: package org.pentaho.designstudio.editors.actionsequence.pages.actions.details;
014:
015: import org.eclipse.core.runtime.IPath;
016: import org.eclipse.jface.viewers.TableViewer;
017: import org.eclipse.swt.SWT;
018: import org.eclipse.swt.custom.StackLayout;
019: import org.eclipse.swt.events.SelectionEvent;
020: import org.eclipse.swt.events.SelectionListener;
021: import org.eclipse.swt.graphics.Font;
022: import org.eclipse.swt.graphics.FontData;
023: import org.eclipse.swt.graphics.Image;
024: import org.eclipse.swt.layout.GridData;
025: import org.eclipse.swt.layout.GridLayout;
026: import org.eclipse.swt.widgets.Composite;
027: import org.eclipse.swt.widgets.Display;
028: import org.eclipse.swt.widgets.Label;
029: import org.eclipse.swt.widgets.ToolBar;
030: import org.eclipse.swt.widgets.ToolItem;
031: import org.pentaho.actionsequence.dom.AbstractIOElement;
032: import org.pentaho.actionsequence.dom.ActionInput;
033: import org.pentaho.actionsequence.dom.ActionOutput;
034: import org.pentaho.actionsequence.dom.ActionSequenceInput;
035: import org.pentaho.actionsequence.dom.IActionSequenceElement;
036: import org.pentaho.actionsequence.dom.actions.JavascriptAction;
037: import org.pentaho.designstudio.controls.ActionInputText;
038: import org.pentaho.designstudio.controls.ActionInputsList;
039: import org.pentaho.designstudio.controls.ActionInputsListToolbar;
040: import org.pentaho.designstudio.controls.ActionOutputsTable;
041: import org.pentaho.designstudio.controls.ActionResourceList;
042: import org.pentaho.designstudio.controls.WidgetFactory;
043: import org.pentaho.designstudio.controls.XmlModificationEvent;
044: import org.pentaho.designstudio.editors.actionsequence.ActionSequenceEditorPlugin;
045: import org.pentaho.designstudio.editors.actionsequence.pages.actions.IActionIOFilter;
046: import org.pentaho.designstudio.messages.Messages;
047:
048: /**
049: * The template describing the action definition used to run JavaScript.
050: *
051: * @author Angelo Rodriguez
052: */
053: public class JavascriptDetailsPage extends ActionDetailsPage implements
054: IActionIOFilter, SelectionListener {
055:
056: public static final String DISPLAY_NAME = Messages
057: .getString("JavascriptDetailsPage.UI_COMPONENT_NAME_JAVASCRIPT"); //$NON-NLS-1$
058:
059: Composite stackedComposite;
060: StackLayout stackLayout;
061: ActionInputText javaScriptText;
062: ActionInputText largeJavaScriptText;
063: ActionInputsList scriptInputs;
064: ActionResourceList importedJavascript;
065: protected TableViewer tableViewer;
066: IPath basePath;
067: ActionInputsListToolbar scriptInputsToolbar;
068: Composite largeEditorComposite;
069: Composite actionConfigComposite;
070: ToolItem expandEditorToolItem;
071: ToolItem collapseEditorToolItem;
072:
073: static final Image EXPAND_WINDOW_ICON = ActionSequenceEditorPlugin
074: .getImageDescriptor(
075: Messages
076: .getString("JavascriptDetailsPage.EXPAND_WINDOW_ICON")).createImage(); //$NON-NLS-1$
077: static final Image COLLAPSE_WINDOW_ICON = ActionSequenceEditorPlugin
078: .getImageDescriptor(
079: Messages
080: .getString("JavascriptDetailsPage.COLLAPSE_WINDOW_ICON")).createImage(); //$NON-NLS-1$
081:
082: /**
083: * Creates a JavaScript template.
084: */
085: public JavascriptDetailsPage(IPath basePath) {
086: super ();
087: this .basePath = basePath;
088: }
089:
090: /* (non-Javadoc)
091: * @see org.pentaho.designstudio.editors.actionsequence.pages.actions.templates.ActionDefinitionTemplate#getComponentClassName()
092: */
093: public String getComponentName() {
094: return JavascriptAction.COMPONENT_NAME;
095: }
096:
097: /* (non-Javadoc)
098: * @see org.pentaho.designstudio.editors.actionsequence.pages.actions.templates.ActionDefinitionTemplate#getDisplayName()
099: */
100: public String getName() {
101: return DISPLAY_NAME;
102: }
103:
104: public void createDetailsComposite(Composite parent) {
105:
106: stackedComposite = parent;
107: stackLayout = new StackLayout();
108: stackedComposite.setLayout(stackLayout);
109:
110: largeEditorComposite = WidgetFactory
111: .createComposite(stackedComposite);
112: GridLayout layout = new GridLayout();
113: largeEditorComposite.setLayout(layout);
114: GridData gridData = new GridData(GridData.FILL_BOTH);
115: gridData.widthHint = 10;
116: largeEditorComposite.setLayoutData(gridData);
117: Composite composite = WidgetFactory
118: .createComposite(largeEditorComposite);
119: GridData layoutData = new GridData(GridData.FILL_HORIZONTAL);
120: layoutData.horizontalSpan = 2;
121: composite.setLayoutData(layoutData);
122: GridLayout gridLayout = new GridLayout(2, false);
123: gridLayout.marginWidth = 0;
124: gridLayout.marginHeight = 0;
125: composite.setLayout(gridLayout);
126: Label label = WidgetFactory
127: .createLabel(
128: composite,
129: Messages
130: .getString("JavascriptDetailsPage.UI_JAVASCRIPT_LABEL")); //$NON-NLS-1$
131: label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
132:
133: ToolBar toolBar = new ToolBar(composite, SWT.FLAT);
134: collapseEditorToolItem = new ToolItem(toolBar, SWT.PUSH);
135: collapseEditorToolItem.setImage(COLLAPSE_WINDOW_ICON);
136: collapseEditorToolItem.setToolTipText(Messages
137: .getString("JavascriptDetailsPage.TOGGLE_EDITOR_SIZE")); //$NON-NLS-1$
138: collapseEditorToolItem.addSelectionListener(this );
139:
140: gridData = new GridData(GridData.FILL_BOTH);
141: gridData.widthHint = 10;
142: largeJavaScriptText = new ActionInputText(largeEditorComposite,
143: gridData, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL
144: | SWT.BORDER);
145: try {
146: FontData[] fontData = largeJavaScriptText.getControl()
147: .getFont().getFontData();
148: Font font = new Font(Display.getDefault(),
149: "Courier New", fontData[0].getHeight(), SWT.NORMAL); //$NON-NLS-1$
150: largeJavaScriptText.getControl().setFont(font);
151: } catch (Exception ex) {
152: try {
153: FontData[] fontData = largeJavaScriptText.getControl()
154: .getFont().getFontData();
155: Font font = new Font(Display.getDefault(),
156: "Courier", fontData[0].getHeight(), SWT.BOLD); //$NON-NLS-1$
157: largeJavaScriptText.getControl().setFont(font);
158: } catch (Exception ex2) {
159: // Do nothing. We'll stick with the default font.
160: }
161: }
162:
163: actionConfigComposite = WidgetFactory
164: .createComposite(stackedComposite);
165: layout = new GridLayout(2, true);
166: layout.horizontalSpacing = 10;
167: actionConfigComposite.setLayout(layout);
168:
169: Composite inputTitleComposite = WidgetFactory
170: .createComposite(actionConfigComposite);
171: gridLayout = new GridLayout(2, false);
172: gridLayout.marginWidth = 0;
173: gridLayout.marginHeight = 0;
174: inputTitleComposite.setLayout(gridLayout);
175: inputTitleComposite.setLayoutData(new GridData(
176: GridData.FILL_HORIZONTAL));
177: label = WidgetFactory
178: .createLabel(
179: inputTitleComposite,
180: Messages
181: .getString("JavascriptDetailsPage.UI_SCRIPT_INPUT_LABEL")); //$NON-NLS-1$
182:
183: label = WidgetFactory
184: .createLabel(
185: actionConfigComposite,
186: Messages
187: .getString("JavascriptDetailsPage.IMPORTED_JAVASCRIPT"), SWT.NONE); //$NON-NLS-1$
188: gridData = new GridData();
189: gridData.verticalAlignment = GridData.END;
190: label.setLayoutData(gridData);
191:
192: scriptInputs = new ActionInputsList(actionConfigComposite);
193: scriptInputs.setFilter(this );
194: layoutData = new GridData(GridData.FILL_BOTH);
195: scriptInputs.getControl().setLayoutData(layoutData);
196:
197: scriptInputsToolbar = new ActionInputsListToolbar(
198: inputTitleComposite, scriptInputs);
199: gridData = new GridData(GridData.FILL_HORIZONTAL);
200: gridData.horizontalAlignment = GridData.END;
201: scriptInputsToolbar.getControl().setLayoutData(gridData);
202:
203: importedJavascript = new ActionResourceList(
204: actionConfigComposite, basePath,
205: "javascript", "text/javascript"); //$NON-NLS-1$ //$NON-NLS-2$
206: gridData = new GridData(GridData.FILL_BOTH);
207: importedJavascript.getList().setLayoutData(gridData);
208:
209: label = WidgetFactory.createLabel(actionConfigComposite, ""); //$NON-NLS-1$
210: label = WidgetFactory.createLabel(actionConfigComposite, ""); //$NON-NLS-1$
211:
212: composite = WidgetFactory
213: .createComposite(actionConfigComposite);
214: layoutData = new GridData(GridData.FILL_HORIZONTAL);
215: layoutData.horizontalSpan = 2;
216: composite.setLayoutData(layoutData);
217: gridLayout = new GridLayout(2, false);
218: gridLayout.marginWidth = 0;
219: gridLayout.marginHeight = 0;
220: composite.setLayout(gridLayout);
221: label = WidgetFactory
222: .createLabel(
223: composite,
224: Messages
225: .getString("JavascriptDetailsPage.UI_JAVASCRIPT_LABEL")); //$NON-NLS-1$
226: label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
227:
228: toolBar = new ToolBar(composite, SWT.FLAT);
229:
230: expandEditorToolItem = new ToolItem(toolBar, SWT.PUSH);
231: expandEditorToolItem.setImage(EXPAND_WINDOW_ICON);
232: expandEditorToolItem.addSelectionListener(this );
233:
234: layoutData = new GridData(GridData.FILL_BOTH);
235: layoutData.widthHint = 10;
236: layoutData.heightHint = 100;
237: layoutData.horizontalSpan = 2;
238: javaScriptText = new ActionInputText(actionConfigComposite,
239: layoutData, SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL
240: | SWT.BORDER);
241: try {
242: FontData[] fontData = javaScriptText.getControl().getFont()
243: .getFontData();
244: Font font = new Font(Display.getDefault(),
245: "Courier New", fontData[0].getHeight(), SWT.NORMAL); //$NON-NLS-1$
246: javaScriptText.getControl().setFont(font);
247: } catch (Exception ex) {
248: try {
249: FontData[] fontData = javaScriptText.getControl()
250: .getFont().getFontData();
251: Font font = new Font(Display.getDefault(),
252: "Courier", fontData[0].getHeight(), SWT.BOLD); //$NON-NLS-1$
253: javaScriptText.getControl().setFont(font);
254: } catch (Exception ex2) {
255: // Do nothing. We'll stick with the default font.
256: }
257: }
258:
259: layoutData = new GridData(GridData.FILL_HORIZONTAL);
260: layoutData.horizontalSpan = 2;
261: layoutData.widthHint = 10;
262: WidgetFactory
263: .createLabel(
264: actionConfigComposite,
265: Messages
266: .getString("JavascriptDetailsPage.VALID_PARAMS_MSG")).setLayoutData(layoutData); //$NON-NLS-1$
267:
268: WidgetFactory.createLabel(actionConfigComposite, ""); //$NON-NLS-1$
269: WidgetFactory.createLabel(actionConfigComposite, ""); //$NON-NLS-1$
270: WidgetFactory
271: .createLabel(
272: actionConfigComposite,
273: Messages
274: .getString("JavascriptDetailsPage.UI_SCRIPT_OUTPUT_LABEL")); //$NON-NLS-1$
275: WidgetFactory.createLabel(actionConfigComposite, ""); //$NON-NLS-1$
276:
277: tableViewer = new ActionOutputsTable(actionConfigComposite);
278: layoutData = new GridData(GridData.FILL_BOTH);
279: tableViewer.getTable().setLayoutData(layoutData);
280:
281: stackLayout.topControl = actionConfigComposite;
282: stackedComposite.layout();
283: }
284:
285: public void contentChanged(XmlModificationEvent event) {
286: if ((event.getSource() == javaScriptText)
287: && ((event.getTypeOfModification() == XmlModificationEvent.NODE_ADDED) || (event
288: .getTypeOfModification() == XmlModificationEvent.NODE_REMOVED))) {
289: scriptInputs.refresh();
290: }
291: }
292:
293: public void refresh() {
294: super .refresh();
295: javaScriptText.setTargetInput(actionDefinition,
296: JavascriptAction.SCRIPT_ELEMENT);
297: largeJavaScriptText.setTargetInput(actionDefinition,
298: JavascriptAction.SCRIPT_ELEMENT);
299: scriptInputs.setInput(actionDefinition);
300: scriptInputsToolbar.refresh();
301: tableViewer.setInput(actionDefinition);
302: importedJavascript.setInput(actionDefinition);
303: }
304:
305: public boolean accept(AbstractIOElement ioElement) {
306: boolean result = false;
307: if ((ioElement instanceof ActionSequenceInput)
308: || (ioElement instanceof ActionInput)) {
309: result = ioElement.getName().indexOf('-') == -1;
310: } else if (ioElement instanceof ActionOutput) {
311: result = ((ActionOutput) ioElement).getPublicName()
312: .indexOf('-') == -1;
313: }
314: return result;
315: }
316:
317: public boolean accepts(IActionSequenceElement actionDef) {
318: return actionDef instanceof JavascriptAction;
319: }
320:
321: public void widgetDefaultSelected(SelectionEvent e) {
322: // TODO Auto-generated method stub
323:
324: }
325:
326: public void widgetSelected(SelectionEvent e) {
327: if (e.getSource() == expandEditorToolItem) {
328: if (stackLayout.topControl != largeEditorComposite) {
329: stackLayout.topControl = largeEditorComposite;
330: largeJavaScriptText.refresh();
331: stackedComposite.layout();
332: }
333: } else if (stackLayout.topControl != actionConfigComposite) {
334: stackLayout.topControl = actionConfigComposite;
335: javaScriptText.refresh();
336: stackedComposite.layout();
337: }
338: }
339: }
|