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.Iterator;
017:
018: import org.dom4j.Element;
019: import org.eclipse.jface.viewers.IStructuredSelection;
020: import org.eclipse.jface.viewers.ITreeContentProvider;
021: import org.eclipse.jface.viewers.StructuredSelection;
022: import org.eclipse.jface.viewers.TreeViewer;
023: import org.eclipse.jface.viewers.Viewer;
024: import org.eclipse.jface.viewers.ViewerDropAdapter;
025: import org.eclipse.swt.dnd.DND;
026: import org.eclipse.swt.dnd.Transfer;
027: import org.eclipse.swt.dnd.TransferData;
028: import org.eclipse.swt.graphics.Image;
029: import org.eclipse.swt.widgets.Display;
030: import org.eclipse.swt.widgets.Tree;
031: import org.eclipse.swt.widgets.Widget;
032: import org.pentaho.actionsequence.dom.AbstractIOElement;
033: import org.pentaho.actionsequence.dom.ActionControlStatement;
034: import org.pentaho.actionsequence.dom.ActionOutput;
035: import org.pentaho.actionsequence.dom.ActionSequenceDocument;
036: import org.pentaho.actionsequence.dom.ActionSequenceInput;
037: import org.pentaho.actionsequence.dom.ActionSequenceOutput;
038: import org.pentaho.actionsequence.dom.IActionSequenceDocumentListener;
039: import org.pentaho.actionsequence.dom.actions.ActionDefinition;
040: import org.pentaho.designstudio.controls.ActionSequenceParamTransfer;
041: import org.pentaho.designstudio.editors.actionsequence.pages.ActionSequenceLabelProvider;
042:
043: /**
044: * Tree viewer used for viewing and modifying the inputs, outputs, and resources in a
045: * component definition
046: *
047: * @author Angelo Rodriguez
048: */
049: public class ActionSequenceOutputsTree extends TreeViewer implements
050: IActionSequenceDocumentListener {
051:
052: public class LabelProvider extends ActionSequenceLabelProvider {
053: public LabelProvider(Display display) {
054: super (display);
055: }
056:
057: public Image getImage(Object obj) {
058: Image image = null;
059: if (obj instanceof ActionSequenceOutput) {
060: image = super .getImage(((ActionSequenceOutput) obj)
061: .getElement());
062: } else {
063: image = super .getImage(obj);
064: }
065: return image;
066: }
067:
068: public String getText(Object obj) {
069: String text = null;
070: if (obj instanceof ActionSequenceOutput) {
071: text = super .getText(((ActionSequenceOutput) obj)
072: .getElement());
073: } else {
074: text = super .getText(obj);
075: }
076: return text;
077: }
078:
079: }
080:
081: public class ContentProvider implements ITreeContentProvider {
082:
083: public ContentProvider() {
084: }
085:
086: /* (non-Javadoc)
087: * Method declared on ITreeContentProvider.
088: */
089: public Object[] getChildren(Object parent) {
090: return hasChildren(parent) ? actionSequence.getOutputs()
091: : new Object[0];
092: }
093:
094: /* (non-Javadoc)
095: * Method declared on ITreeContentProvider.
096: */
097: public Object getParent(Object child) {
098: return child instanceof ActionSequenceOutput ? getOutputRoot()
099: : null;
100: }
101:
102: /* (non-Javadoc)
103: * Method declared on ITreeContentProvider.
104: */
105: public boolean hasChildren(Object node) {
106: return (node instanceof Element)
107: && (actionSequence.getOutputs().length > 0);
108: }
109:
110: /* (non-Javadoc)
111: * Method declared on ITreeContentProvider.
112: */
113: public Object[] getElements(Object inputElement) {
114: ArrayList elements = new ArrayList();
115: Element outputRoot = getOutputRoot();
116: if (outputRoot != null) {
117: elements.add(outputRoot);
118: }
119: return elements.toArray();
120: }
121:
122: /* (non-Javadoc)
123: * Method declared on ITreeContentProvider.
124: */
125: public void dispose() {
126: actionSequence = null;
127: }
128:
129: /* (non-Javadoc)
130: * Method declared on ITreeContentProvider.
131: */
132: public void inputChanged(Viewer viewer, Object oldInput,
133: Object newInput) {
134: if (actionSequence != null) {
135: actionSequence
136: .removeListener(ActionSequenceOutputsTree.this );
137: }
138: if (newInput instanceof ActionSequenceDocument) {
139: actionSequence = (ActionSequenceDocument) newInput;
140: actionSequence
141: .addListener(ActionSequenceOutputsTree.this );
142: } else {
143: actionSequence = null;
144: }
145: }
146:
147: }
148:
149: class DropAdapter extends ViewerDropAdapter {
150:
151: DropAdapter() {
152: super (ActionSequenceOutputsTree.this );
153: }
154:
155: /* (non-Javadoc)
156: * @see org.eclipse.jface.viewers.ViewerDropAdapter#performDrop(java.lang.Object)
157: */
158: public boolean performDrop(Object data) {
159: return addOutput((AbstractIOElement) data);
160: }
161:
162: /*
163: * (non-Javadoc)
164: *
165: * @see org.eclipse.jface.viewers.ViewerDropAdapter#validateDrop(java.lang.Object,
166: * int, org.eclipse.swt.dnd.TransferData)
167: */
168: public boolean validateDrop(Object target, int op,
169: TransferData type) {
170: return true;
171: }
172: }
173:
174: protected ActionSequenceDocument actionSequence;
175: private ContentProvider treeContentProvider;
176: private DropAdapter dropAdapter;
177:
178: /**
179: * Create an action sequence inputs, outputs, and resources viewer.
180: * @param parent the tree being managed by this viewer
181: */
182: public ActionSequenceOutputsTree(Tree tree) {
183: super (tree);
184: init();
185: }
186:
187: public boolean addOutput(AbstractIOElement output) {
188: boolean result = false;
189: String name = ""; //$NON-NLS-1$
190: String type = ""; //$NON-NLS-1$
191: if ((output instanceof ActionSequenceInput)
192: || (output instanceof ActionOutput)) {
193: if (output instanceof ActionSequenceInput) {
194: name = ((ActionSequenceInput) output).getName();
195: type = ((ActionSequenceInput) output).getType();
196: } else {
197: name = ((ActionOutput) output).getPublicName();
198: type = ((ActionOutput) output).getType();
199: }
200: ActionSequenceOutput docOutput = actionSequence
201: .createOutput(name, type);
202: refresh();
203: result = true;
204: setSelection(new StructuredSelection(docOutput));
205: getTree().forceFocus();
206: }
207: return result;
208: }
209:
210: private void init() {
211: treeContentProvider = new ContentProvider();
212: setContentProvider(treeContentProvider);
213: setLabelProvider(new LabelProvider(getTree().getDisplay()));
214:
215: int ops = DND.DROP_MOVE | DND.DROP_COPY;
216: dropAdapter = new DropAdapter();
217: addDropSupport(ops,
218: new Transfer[] { ActionSequenceParamTransfer
219: .getInstance() }, dropAdapter);
220: }
221:
222: /**
223: * Deletes the selection inputs, outputs, and resources from the action sequence and this viewer.
224: */
225: public void deleteSelectedOutputs() {
226: IStructuredSelection selection = (IStructuredSelection) getSelection();
227: for (Iterator iter = selection.iterator(); iter.hasNext();) {
228: Object output = iter.next();
229: if (output instanceof ActionSequenceOutput) {
230: actionSequence
231: .deleteOutput(((ActionSequenceOutput) output)
232: .getName());
233: }
234: }
235: }
236:
237: private Element getOutputRoot() {
238: Element outputRoot = null;
239: if (actionSequence != null) {
240: Element docRoot = actionSequence.getDocument()
241: .getRootElement();
242: if (docRoot != null) {
243: outputRoot = docRoot
244: .element(ActionSequenceDocument.DOC_OUTPUTS_NAME);
245: }
246: }
247: return outputRoot;
248: }
249:
250: public void ioAdded(AbstractIOElement io) {
251: Element outputElement = getOutputRoot();
252: if (outputElement != null) {
253: Widget outputItem = findItem(outputElement);
254: if (outputItem == null) {
255: createTreeItem(getTree(), outputElement, -1);
256: } else {
257: refresh(outputElement);
258: }
259: }
260: }
261:
262: public void ioChanged(AbstractIOElement io) {
263: if (io instanceof ActionSequenceOutput) {
264: update(io, null);
265: }
266: }
267:
268: public void ioRemoved(Object parent, AbstractIOElement io) {
269: if (io instanceof ActionSequenceOutput) {
270: remove(io);
271: }
272: }
273:
274: public void ioRenamed(AbstractIOElement io) {
275: if (io instanceof ActionSequenceOutput) {
276: update(io, null);
277: }
278: }
279:
280: public void actionAdded(ActionDefinition action) {
281: }
282:
283: public void actionChanged(ActionDefinition action) {
284: }
285:
286: public void actionRemoved(Object parent, ActionDefinition action) {
287: }
288:
289: public void actionRenamed(ActionDefinition action) {
290: }
291:
292: public void controlStatementAdded(
293: ActionControlStatement controlStatement) {
294: }
295:
296: public void controlStatementChanged(
297: ActionControlStatement controlStatement) {
298: }
299:
300: public void controlStatementRemoved(Object parent,
301: ActionControlStatement controlStatement) {
302: }
303:
304: public void resourceAdded(Object resource) {
305: }
306:
307: public void resourceChanged(Object resource) {
308: }
309:
310: public void resourceRemoved(Object parent, Object resource) {
311: }
312:
313: public void resourceRenamed(Object resource) {
314: }
315:
316: public void headerChanged(
317: ActionSequenceDocument actionSequenceDocument) {
318: }
319: }
|