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.parameters;
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.dialogs.Dialog;
021: import org.eclipse.jface.dialogs.IDialogConstants;
022: import org.eclipse.jface.dialogs.MessageDialog;
023: import org.eclipse.jface.viewers.CheckboxTableViewer;
024: import org.eclipse.jface.viewers.IStructuredContentProvider;
025: import org.eclipse.jface.viewers.LabelProvider;
026: import org.eclipse.jface.viewers.Viewer;
027: import org.eclipse.swt.SWT;
028: import org.eclipse.swt.layout.GridData;
029: import org.eclipse.swt.widgets.Composite;
030: import org.eclipse.swt.widgets.Control;
031: import org.eclipse.swt.widgets.Shell;
032: import org.pentaho.actionsequence.dom.AbstractIOElement;
033: import org.pentaho.actionsequence.dom.ActionInput;
034: import org.pentaho.actionsequence.dom.ActionLoop;
035: import org.pentaho.actionsequence.dom.ActionOutput;
036: import org.pentaho.actionsequence.dom.ActionResource;
037: import org.pentaho.actionsequence.dom.ActionSequenceInput;
038: import org.pentaho.actionsequence.dom.ActionSequenceOutput;
039: import org.pentaho.actionsequence.dom.ActionSequenceResource;
040: import org.pentaho.actionsequence.dom.actions.ActionDefinition;
041: import org.pentaho.designstudio.controls.WidgetFactory;
042: import org.pentaho.designstudio.editors.actionsequence.pages.ActionSequenceLabelProvider;
043: import org.pentaho.designstudio.messages.Messages;
044:
045: public class UpdateParamReferencesDialog extends Dialog implements
046: IStructuredContentProvider {
047:
048: AbstractIOElement actionSequenceParameter;
049: CheckboxTableViewer checkboxTableViewer;
050: List parameterReferences;
051:
052: public UpdateParamReferencesDialog(Shell parentShell,
053: AbstractIOElement actionSequenceParam,
054: List parameterReferences) {
055: super (parentShell);
056: actionSequenceParameter = actionSequenceParam;
057: this .parameterReferences = parameterReferences;
058: }
059:
060: protected void configureShell(Shell shell) {
061: super .configureShell(shell);
062: shell
063: .setText(Messages
064: .getString("UpdateParamReferencesDialog.UPDATE_PARAM_REFERENCES")); //$NON-NLS-1$
065: }
066:
067: protected void createButtonsForButtonBar(Composite parent) {
068: createButton(parent, IDialogConstants.OK_ID,
069: IDialogConstants.OK_LABEL, true);
070: createButton(parent, IDialogConstants.CANCEL_ID,
071: IDialogConstants.CANCEL_LABEL, false);
072: }
073:
074: protected Control createDialogArea(Composite parent) {
075: Composite composite = (Composite) super
076: .createDialogArea(parent);
077: GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
078: gridData.widthHint = 10;
079: gridData.heightHint = 75;
080: WidgetFactory
081: .createLabel(
082: composite,
083: Messages
084: .getString("RenameActionSequenceInputDialog.REFERENCE_LIST_LABEL"), SWT.WRAP).setLayoutData(gridData); //$NON-NLS-1$
085: gridData = new GridData(GridData.FILL_HORIZONTAL);
086: gridData.heightHint = 100;
087: gridData.widthHint = 300;
088: checkboxTableViewer = CheckboxTableViewer.newCheckList(
089: composite, SWT.NONE);
090: checkboxTableViewer.getControl().setLayoutData(gridData);
091: checkboxTableViewer.setContentProvider(this );
092: checkboxTableViewer.setLabelProvider(new LabelProvider() {
093: public String getText(Object element) {
094: String text = element.toString();
095: if (element instanceof ActionDefinition) {
096: text = ActionSequenceLabelProvider
097: .getText((ActionDefinition) element);
098: } else if (element instanceof ActionSequenceOutput) {
099: text = Messages
100: .getString("RenameParameterDialog.ACTION_SEQUENCE_OUTPUT"); //$NON-NLS-1$
101: } else if (element instanceof ActionLoop) {
102: text = ActionSequenceLabelProvider
103: .getText((ActionLoop) element);
104: }
105: return text;
106: }
107: });
108: checkboxTableViewer.setInput(actionSequenceParameter);
109: checkboxTableViewer.setAllChecked(true);
110: return composite;
111: }
112:
113: public Object[] getElements(Object inputElement) {
114: ArrayList elements = new ArrayList();
115: for (Iterator iter = parameterReferences.iterator(); iter
116: .hasNext();) {
117: Object object = iter.next();
118: if (object instanceof ActionInput) {
119: ActionInput actionInput = (ActionInput) object;
120: ActionDefinition actionDefinition = actionInput
121: .getActionDefinition();
122: if (!elements.contains(actionDefinition)) {
123: elements.add(actionInput.getActionDefinition());
124: }
125: } else if (object instanceof ActionResource) {
126: ActionResource actionResource = (ActionResource) object;
127: ActionDefinition actionDefinition = actionResource
128: .getActionDefinition();
129: if (!elements.contains(actionDefinition)) {
130: elements.add(actionResource.getActionDefinition());
131: }
132: } else if ((object instanceof ActionLoop)
133: || (object instanceof ActionSequenceOutput)) {
134: elements.add(object);
135: }
136: }
137: return elements.toArray();
138: }
139:
140: public void dispose() {
141: }
142:
143: public void inputChanged(Viewer viewer, Object oldInput,
144: Object newInput) {
145: }
146:
147: private void updateInputReferences(String newName) {
148: Object[] checkedActions = checkboxTableViewer
149: .getCheckedElements();
150: for (int i = 0; i < checkedActions.length; i++) {
151: if (checkedActions[i] instanceof ActionDefinition) {
152: ActionDefinition actionDefinition = (ActionDefinition) checkedActions[i];
153: for (Iterator iterator = parameterReferences.iterator(); iterator
154: .hasNext();) {
155: Object object = iterator.next();
156: if ((object instanceof ActionInput)
157: && ((ActionInput) object)
158: .getActionDefinition().equals(
159: actionDefinition)) {
160: ActionInput actionInput = (ActionInput) object;
161: String mapping = actionInput.getMapping();
162: if ((mapping != null)
163: && (mapping.trim().length() > 0)) {
164: actionInput.setMapping(newName);
165: } else {
166: if (Arrays.asList(
167: actionDefinition
168: .getReservedInputNames())
169: .contains(actionInput.getName())) {
170: actionInput.setMapping(newName);
171: } else {
172: actionInput.setMapping(null);
173: actionDefinition.renameInputParam(
174: actionInput.getName(), newName);
175: }
176: }
177: }
178: }
179: } else if (checkedActions[i] instanceof ActionSequenceOutput) {
180: ((ActionSequenceOutput) checkedActions[i])
181: .setName(newName);
182: } else if (checkedActions[i] instanceof ActionLoop) {
183: ((ActionLoop) checkedActions[i]).setLoopOn(newName);
184: }
185: }
186: }
187:
188: private void updateResourceReferences(String newName) {
189: Object[] checkedActions = checkboxTableViewer
190: .getCheckedElements();
191: for (int i = 0; i < checkedActions.length; i++) {
192: ActionDefinition actionDefinition = (ActionDefinition) checkedActions[i];
193: for (Iterator iterator = parameterReferences.iterator(); iterator
194: .hasNext();) {
195: Object object = iterator.next();
196: if (((ActionResource) object).getActionDefinition()
197: .equals(actionDefinition)) {
198: ActionResource actionResource = (ActionResource) object;
199: String mapping = actionResource.getMapping();
200: if ((mapping != null)
201: && (mapping.trim().length() > 0)) {
202: actionResource.setMapping(newName);
203: } else {
204: if (Arrays.asList(
205: actionDefinition
206: .getReservedResourceNames())
207: .contains(actionResource.getName())) {
208: actionResource.setMapping(newName);
209: } else {
210: actionResource.setMapping(null);
211: actionResource.setName(newName);
212: }
213: }
214: }
215: }
216: }
217: }
218:
219: protected void updateOutputReferences(String newName) {
220: Object[] checkedActions = checkboxTableViewer
221: .getCheckedElements();
222: for (int i = 0; i < checkedActions.length; i++) {
223: if (checkedActions[i] instanceof ActionDefinition) {
224: ActionDefinition actionDefinition = (ActionDefinition) checkedActions[i];
225: for (Iterator iterator = parameterReferences.iterator(); iterator
226: .hasNext();) {
227: Object object = iterator.next();
228: if ((object instanceof ActionInput)
229: && ((ActionInput) object)
230: .getActionDefinition().equals(
231: actionDefinition)) {
232: ActionInput actionInput = (ActionInput) object;
233: String mapping = actionInput.getMapping();
234: if ((mapping != null)
235: && (mapping.trim().length() > 0)) {
236: actionInput.setMapping(newName);
237: } else {
238: if (Arrays.asList(
239: actionDefinition
240: .getReservedInputNames())
241: .contains(actionInput.getName())) {
242: actionInput.setMapping(newName);
243: } else {
244: actionInput.setMapping(null);
245: actionDefinition.renameInputParam(
246: actionInput.getName(), newName);
247: }
248: }
249: }
250: }
251: } else if (checkedActions[i] instanceof ActionSequenceOutput) {
252: ((ActionSequenceOutput) checkedActions[i])
253: .setName(newName);
254: } else if (checkedActions[i] instanceof ActionLoop) {
255: ((ActionLoop) checkedActions[i]).setLoopOn(newName);
256: }
257: }
258: }
259:
260: protected void okPressed() {
261: if (actionSequenceParameter instanceof ActionSequenceInput) {
262: ActionSequenceInput actionSequenceInput = (ActionSequenceInput) actionSequenceParameter;
263: String newName = actionSequenceInput.getName();
264: Object[] checkedActions = checkboxTableViewer
265: .getCheckedElements();
266: boolean foundDuplicateName = false;
267: for (int i = 0; (i < checkedActions.length)
268: && !foundDuplicateName; i++) {
269: foundDuplicateName = (checkedActions[i] instanceof ActionSequenceOutput)
270: && ((ActionSequenceOutput) checkedActions[i])
271: .getName().equals(newName);
272: }
273: if (foundDuplicateName) {
274: MessageDialog
275: .openError(
276: getShell(),
277: Messages
278: .getString("RenameActionSequenceInputDialog.RENAME_ERROR_DIALOG_TITLE"), Messages.getString("RenameParameterDialog.DUPLICATE_ACTION_SEQUENCE_OUTPUT")); //$NON-NLS-1$ //$NON-NLS-2$
279: } else {
280: updateInputReferences(newName);
281: setReturnCode(OK);
282: close();
283: }
284: } else if (actionSequenceParameter instanceof ActionSequenceResource) {
285: ActionSequenceResource actionSequenceResource = (ActionSequenceResource) actionSequenceParameter;
286: String newName = actionSequenceResource.getName();
287: updateResourceReferences(newName);
288: setReturnCode(OK);
289: close();
290: } else if (actionSequenceParameter instanceof ActionOutput) {
291: ActionOutput actionOutput = (ActionOutput) actionSequenceParameter;
292: String newName = actionOutput.getPublicName();
293: boolean foundDuplicateName = false;
294: Object[] checkedActions = checkboxTableViewer
295: .getCheckedElements();
296: for (int i = 0; (i < checkedActions.length)
297: && !foundDuplicateName; i++) {
298: foundDuplicateName = (checkedActions[i] instanceof ActionSequenceOutput)
299: && ((ActionSequenceOutput) checkedActions[i])
300: .getName().equals(newName);
301: }
302: if (foundDuplicateName) {
303: MessageDialog
304: .openError(
305: getShell(),
306: Messages
307: .getString("RenameActionSequenceInputDialog.RENAME_ERROR_DIALOG_TITLE"), Messages.getString("RenameParameterDialog.DUPLICATE_ACTION_SEQUENCE_OUTPUT")); //$NON-NLS-1$ //$NON-NLS-2$
308: } else {
309: updateOutputReferences(newName);
310: setReturnCode(OK);
311: close();
312: }
313: }
314: }
315:
316: }
|