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 java.util.ArrayList;
016: import java.util.Arrays;
017: import java.util.Iterator;
018: import java.util.List;
019:
020: import org.eclipse.jface.viewers.IStructuredSelection;
021: import org.eclipse.jface.viewers.StructuredSelection;
022: import org.eclipse.swt.layout.GridData;
023: import org.eclipse.swt.layout.GridLayout;
024: import org.eclipse.swt.widgets.Composite;
025: import org.eclipse.swt.widgets.Label;
026: import org.pentaho.actionsequence.dom.AbstractIOElement;
027: import org.pentaho.actionsequence.dom.ActionInput;
028: import org.pentaho.actionsequence.dom.ActionOutput;
029: import org.pentaho.actionsequence.dom.ActionSequenceDocument;
030: import org.pentaho.actionsequence.dom.ActionSequenceDocumentAdapter;
031: import org.pentaho.actionsequence.dom.ActionSequenceInput;
032: import org.pentaho.actionsequence.dom.IActionInputValueProvider;
033: import org.pentaho.actionsequence.dom.IActionSequenceElement;
034: import org.pentaho.actionsequence.dom.actions.FormatMsgAction;
035: import org.pentaho.designstudio.controls.ActionInputsList;
036: import org.pentaho.designstudio.controls.ActionInputsListToolbar;
037: import org.pentaho.designstudio.controls.ActionOutputText;
038: import org.pentaho.designstudio.controls.NewActionInputText;
039: import org.pentaho.designstudio.controls.WidgetFactory;
040: import org.pentaho.designstudio.editors.actionsequence.pages.actions.IActionIOFilter;
041: import org.pentaho.designstudio.messages.Messages;
042:
043: public class FormattedMsgDetailsPage extends ActionDetailsPage
044: implements IActionIOFilter {
045:
046: NewActionInputText msgText;
047: ActionOutputText outputName;
048: MessageParamsTable msgParams;
049: MessageParamsToolbar msgParamsToolbar;
050: ActionSequenceListener actionSequenceListener = new ActionSequenceListener();
051:
052: class MessageParamsToolbar extends ActionInputsListToolbar {
053: public MessageParamsToolbar(Composite parent,
054: MessageParamsTable inputsList) {
055: super (parent, inputsList);
056: }
057:
058: protected void moveSelectedItemDown() {
059: IStructuredSelection structuredSelection = (IStructuredSelection) actionInputsList
060: .getSelection();
061: if (structuredSelection.size() == 1) {
062: ArrayList msgInputs = new ArrayList();
063: for (int i = 0; i < actionInputsList.getList()
064: .getItemCount(); i++) {
065: msgInputs.add((ActionInput) actionInputsList
066: .getElementAt(i));
067: }
068: ActionInput selectedActionInput = (ActionInput) structuredSelection
069: .getFirstElement();
070: int currentListIndex = msgInputs
071: .indexOf(selectedActionInput);
072: if (currentListIndex < msgInputs.size() - 1) {
073: msgInputs.remove(currentListIndex);
074: currentListIndex++;
075: if (currentListIndex >= msgInputs.size()) {
076: msgInputs.add(selectedActionInput);
077: } else {
078: msgInputs.add(currentListIndex,
079: selectedActionInput);
080: }
081: FormatMsgAction formatMsgAction = (FormatMsgAction) actionDefinition;
082: formatMsgAction
083: .setMsgInputs((IActionInputValueProvider[]) msgInputs
084: .toArray(new IActionInputValueProvider[0]));
085: actionInputsList.refresh();
086: structuredSelection = new StructuredSelection(
087: (ActionInput) actionInputsList
088: .getElementAt(currentListIndex));
089: actionInputsList.setSelection(structuredSelection);
090: }
091: }
092: }
093:
094: protected void moveSelectedItemUp() {
095: IStructuredSelection structuredSelection = (IStructuredSelection) actionInputsList
096: .getSelection();
097: if (structuredSelection.size() == 1) {
098: ArrayList msgInputs = new ArrayList();
099: for (int i = 0; i < actionInputsList.getList()
100: .getItemCount(); i++) {
101: msgInputs.add((ActionInput) actionInputsList
102: .getElementAt(i));
103: }
104: ActionInput selectedActionInput = (ActionInput) structuredSelection
105: .getFirstElement();
106: int currentListIndex = msgInputs
107: .indexOf(selectedActionInput);
108: if (currentListIndex > 0) {
109: msgInputs.remove(currentListIndex);
110: currentListIndex--;
111: msgInputs
112: .add(currentListIndex, selectedActionInput);
113: FormatMsgAction formatMsgAction = (FormatMsgAction) actionDefinition;
114: formatMsgAction
115: .setMsgInputs((IActionInputValueProvider[]) msgInputs
116: .toArray(new IActionInputValueProvider[0]));
117: actionInputsList.refresh();
118: structuredSelection = new StructuredSelection(
119: (ActionInput) actionInputsList
120: .getElementAt(currentListIndex));
121: actionInputsList.setSelection(structuredSelection);
122: }
123: }
124: }
125: }
126:
127: class MessageParamsTable extends ActionInputsList {
128: public MessageParamsTable(Composite parent) {
129: super (parent);
130: }
131:
132: protected void removeSelectedIOElement() {
133: IStructuredSelection selection = (IStructuredSelection) getSelection();
134: remove(selection.toArray());
135: List selections = selection.toList();
136: FormatMsgAction formatMsgAction = (FormatMsgAction) actionDefinition;
137: ArrayList inputsList = new ArrayList(Arrays
138: .asList(formatMsgAction.getMsgInputs()));
139: inputsList.removeAll(selections);
140: formatMsgAction
141: .setMsgInputs((IActionInputValueProvider[]) inputsList
142: .toArray(new IActionInputValueProvider[0]));
143: refresh();
144: }
145:
146: protected ActionInput addInput(AbstractIOElement input) {
147: FormatMsgAction formatMsgAction = (FormatMsgAction) actionDefinition;
148: List valueProviders = new ArrayList(Arrays
149: .asList(formatMsgAction.getMsgInputs()));
150: valueProviders.add(input);
151: formatMsgAction
152: .setMsgInputs((IActionInputValueProvider[]) valueProviders
153: .toArray(new IActionInputValueProvider[0]));
154: valueProviders = Arrays.asList(formatMsgAction
155: .getMsgInputs());
156: refresh();
157: return (ActionInput) valueProviders.get(valueProviders
158: .size() - 1);
159: }
160:
161: }
162:
163: class ActionSequenceListener extends ActionSequenceDocumentAdapter {
164:
165: public void ioAdded(AbstractIOElement io) {
166: if (io instanceof ActionInput) {
167: ActionInput actionInput = (ActionInput) io;
168: if (actionInput.getActionDefinition().equals(
169: actionDefinition)) {
170: addFormatArg(io.getName());
171: }
172: }
173: super .ioAdded(io);
174: }
175:
176: public void ioChanged(AbstractIOElement io) {
177: if (io instanceof ActionInput) {
178: ActionInput actionInput = (ActionInput) io;
179: if (actionInput.getActionDefinition().equals(
180: actionDefinition)) {
181: syncFormatArgs();
182: }
183: }
184: super .ioAdded(io);
185: }
186:
187: public void ioRemoved(Object parent, AbstractIOElement io) {
188: if (io instanceof ActionInput) {
189: if (parent.equals(actionDefinition)) {
190: removeFormatArg(io.getName());
191: }
192: }
193: }
194: }
195:
196: public String getComponentName() {
197: return FormatMsgAction.COMPONENT_NAME;
198: }
199:
200: public String getName() {
201: return Messages
202: .getString("FormattedMsgDetailsPage.ID_FORMAT_MSG"); //$NON-NLS-1$
203: }
204:
205: public void createDetailsComposite(Composite parent) {
206: Composite actionConfigComposite = parent;
207:
208: GridLayout layout = new GridLayout(2, true);
209: layout.horizontalSpacing = 20;
210: actionConfigComposite.setLayout(layout);
211:
212: Label label = WidgetFactory
213: .createLabel(
214: actionConfigComposite,
215: Messages
216: .getString("FormattedMsgDetailsPage.UI_FORMATTED_MESSAGE_LABEL")); //$NON-NLS-1$
217: GridData layoutData = new GridData(GridData.FILL_HORIZONTAL);
218: layoutData.horizontalSpan = 2;
219: label.setLayoutData(layoutData);
220:
221: layoutData = new GridData(GridData.FILL_BOTH);
222: layoutData.horizontalSpan = 2;
223: msgText = new NewActionInputText(actionConfigComposite,
224: layoutData);
225:
226: label = WidgetFactory.createLabel(actionConfigComposite, ""); //$NON-NLS-1$
227: layoutData = new GridData();
228: layoutData.horizontalSpan = 2;
229: label.setLayoutData(layoutData);
230:
231: Composite composite = WidgetFactory
232: .createComposite(actionConfigComposite);
233: GridLayout gridLayout = new GridLayout(2, false);
234: gridLayout.marginWidth = 0;
235: gridLayout.marginHeight = 0;
236: composite.setLayout(gridLayout);
237: composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
238: WidgetFactory
239: .createLabel(
240: composite,
241: Messages
242: .getString("FormattedMsgDetailsPage.UI_MESSAGE_PARAMS_LABEL")); //$NON-NLS-1$
243:
244: WidgetFactory.createLabel(actionConfigComposite, Messages
245: .getString("FormattedMsgDetailsPage.UI_OUTPUT_LABEL")); //$NON-NLS-1$
246:
247: msgParams = new MessageParamsTable(actionConfigComposite);
248: msgParams.setFilter(this );
249: layoutData = new GridData(GridData.FILL_HORIZONTAL);
250: layoutData.heightHint = 150;
251: msgParams.getList().setLayoutData(layoutData);
252:
253: msgParamsToolbar = new MessageParamsToolbar(composite,
254: msgParams);
255: GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
256: gridData.horizontalAlignment = GridData.END;
257: msgParamsToolbar.getControl().setLayoutData(gridData);
258:
259: layoutData = new GridData(GridData.FILL_HORIZONTAL);
260: layoutData.verticalAlignment = GridData.BEGINNING;
261: outputName = new ActionOutputText(actionConfigComposite,
262: layoutData);
263: }
264:
265: public boolean accepts(IActionSequenceElement actionDef) {
266: return actionDef instanceof FormatMsgAction;
267: }
268:
269: public void refresh() {
270: super .refresh();
271: msgText.setTargetInput(actionDefinition,
272: FormatMsgAction.STRING_FORMAT_ELEMENT);
273: msgParams.setInput(actionDefinition);
274: msgParamsToolbar.refresh();
275: outputName.setTargetOutput(actionDefinition,
276: FormatMsgAction.OUTPUT_STRING);
277: }
278:
279: private void addFormatArg(String arg) {
280: ArrayList formatArgs = new ArrayList(
281: Arrays
282: .asList(actionDefinition
283: .getComponentDefinitionValues(FormatMsgAction.ARGUMENT_XPATH)));
284: if (!formatArgs.contains(arg)) {
285: formatArgs.add(arg);
286: actionDefinition.setComponentDefinition(
287: FormatMsgAction.ARGUMENT_XPATH,
288: (String[]) formatArgs.toArray(new String[0]));
289: }
290: }
291:
292: private void removeFormatArg(String arg) {
293: ArrayList formatArgs = new ArrayList(
294: Arrays
295: .asList(actionDefinition
296: .getComponentDefinitionValues(FormatMsgAction.ARGUMENT_XPATH)));
297: if (formatArgs.contains(arg)) {
298: formatArgs.remove(arg);
299: actionDefinition.setComponentDefinition(
300: FormatMsgAction.ARGUMENT_XPATH,
301: (String[]) formatArgs.toArray(new String[0]));
302: }
303: }
304:
305: private void syncFormatArgs() {
306: ActionInput[] actionInputs = actionDefinition
307: .getAllInputParams();
308: List formatArgs = Arrays
309: .asList(actionDefinition
310: .getComponentDefinitionValues(FormatMsgAction.ARGUMENT_XPATH));
311: for (int i = 0; i < actionInputs.length; i++) {
312: addFormatArg(actionInputs[i].getName());
313: }
314: for (Iterator argIter = formatArgs.iterator(); argIter
315: .hasNext();) {
316: String argElement = (String) argIter.next();
317: boolean inputFound = false;
318: for (int i = 0; (i < actionInputs.length) && !inputFound; i++) {
319: inputFound = actionInputs[i].getName().equals(
320: argElement);
321: }
322: if (!inputFound) {
323: removeFormatArg(argElement);
324: }
325: }
326: }
327:
328: public void setActionSequenceElement(IActionSequenceElement element) {
329: super .setActionSequenceElement(element);
330: if (actionDefinition != null) {
331: actionDefinition.getDocument().addListener(
332: actionSequenceListener);
333: }
334: }
335:
336: public boolean accept(AbstractIOElement ioElement) {
337: boolean result = false;
338: if (ioElement.getType().equals(
339: ActionSequenceDocument.STRING_TYPE)) {
340: if ((ioElement instanceof ActionSequenceInput)
341: || (ioElement instanceof ActionInput)) {
342: result = !ioElement.getName().equals(
343: FormatMsgAction.STRING_FORMAT_ELEMENT);
344: } else if (ioElement instanceof ActionOutput) {
345: result = !((ActionOutput) ioElement).getPublicName()
346: .equals(FormatMsgAction.STRING_FORMAT_ELEMENT);
347: }
348: }
349: return result;
350: }
351:
352: }
|