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.controls;
014:
015: import java.net.URI;
016: import java.net.URISyntaxException;
017: import java.text.MessageFormat;
018: import java.util.ArrayList;
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import org.eclipse.core.runtime.IPath;
023: import org.eclipse.core.runtime.Path;
024: import org.eclipse.jface.action.Action;
025: import org.eclipse.jface.action.IMenuListener;
026: import org.eclipse.jface.action.IMenuManager;
027: import org.eclipse.jface.action.MenuManager;
028: import org.eclipse.jface.viewers.IStructuredContentProvider;
029: import org.eclipse.jface.viewers.IStructuredSelection;
030: import org.eclipse.jface.viewers.LabelProvider;
031: import org.eclipse.jface.viewers.ListViewer;
032: import org.eclipse.jface.viewers.Viewer;
033: import org.eclipse.jface.viewers.ViewerDropAdapter;
034: import org.eclipse.swt.SWT;
035: import org.eclipse.swt.dnd.DND;
036: import org.eclipse.swt.dnd.Transfer;
037: import org.eclipse.swt.dnd.TransferData;
038: import org.eclipse.swt.widgets.Composite;
039: import org.eclipse.swt.widgets.FileDialog;
040: import org.eclipse.swt.widgets.Menu;
041: import org.pentaho.actionsequence.dom.ActionResource;
042: import org.pentaho.actionsequence.dom.ActionSequenceResource;
043: import org.pentaho.actionsequence.dom.actions.ActionDefinition;
044: import org.pentaho.designstudio.messages.Messages;
045: import org.pentaho.designstudio.util.SolutionHelper;
046:
047: /**
048: * Table viewer used for viewing and modifying the inputs and outputs of an action
049: * definition element.
050: *
051: * @author Angelo Rodriguez
052: */
053: public class ActionResourceList extends ListViewer {
054:
055: class ResourceListDropAdapter extends ViewerDropAdapter {
056:
057: ResourceListDropAdapter() {
058: super (ActionResourceList.this );
059: setFeedbackEnabled(true);
060: }
061:
062: public boolean performDrop(Object data) {
063: boolean result = false;
064: if (data instanceof ActionSequenceResource) {
065: ActionSequenceResource resource = (ActionSequenceResource) data;
066: actionDefinition.addResourceParam(resource.getName());
067: refresh();
068: result = true;
069: }
070: return result;
071: }
072:
073: /*
074: * (non-Javadoc)
075: *
076: * @see org.eclipse.jface.viewers.ViewerDropAdapter#validateDrop(java.lang.Object,
077: * int, org.eclipse.swt.dnd.TransferData)
078: */
079: public boolean validateDrop(Object target, int op,
080: TransferData type) {
081: return canDrop(target, op, type);
082: }
083: }
084:
085: public class ContentProvider implements IStructuredContentProvider {
086:
087: public Object[] getElements(Object ioElement) {
088: ActionResource[] resources = new ActionResource[0];
089: if (actionDefinition != null) {
090: resources = actionDefinition.getAllResourceParams();
091: }
092: ArrayList resourceList = new ArrayList();
093: for (int i = 0; i < resources.length; i++) {
094: String logicalName = resources[i].getPublicName();
095: ActionSequenceResource actionSequenceResource = actionDefinition
096: .getDocument().getResource(logicalName);
097: if ((actionSequenceResource != null)/* && actionSequenceResource.getMimeType().equals(resourceMimeType)*/) {
098: resourceList.add(resources[i]);
099: }
100:
101: }
102: return resourceList.toArray();
103: }
104:
105: public void dispose() {
106: }
107:
108: public void inputChanged(Viewer viewer, Object oldInput,
109: Object newInput) {
110: actionDefinition = (ActionDefinition) newInput;
111: }
112: }
113:
114: protected class DeleteResourceAction extends Action {
115: public DeleteResourceAction() {
116: super ("Delete"); //$NON-NLS-1$
117: }
118:
119: public void run() {
120: removeSelectedResources();
121: }
122: }
123:
124: protected class AddResourceAction extends Action {
125: public AddResourceAction() {
126: super ("Add"); //$NON-NLS-1$
127: }
128:
129: public void run() {
130: addNewResource();
131: }
132: }
133:
134: ActionDefinition actionDefinition;
135:
136: IPath basePath;
137:
138: protected DeleteResourceAction deleteResourceAction = new DeleteResourceAction();
139:
140: protected AddResourceAction addResourceAction = new AddResourceAction();
141:
142: private ResourceListDropAdapter dropAdapter;
143:
144: private ContentProvider contentProvider;
145:
146: private String resourceNamePrefix;
147:
148: private String resourceMimeType;
149:
150: public static final String SOLUTION_FILE_PREFIX = "solution:"; //$NON-NLS-1$
151:
152: /**
153: * Creates an viewer
154: * @param parent the parent of this viewer.
155: * @param toolkit the form toolkit.
156: */
157: public ActionResourceList(Composite parent, IPath basePath,
158: String resourceNamePrefix, String resourceMimeType) {
159: super (parent, SWT.BORDER);
160: this .basePath = basePath;
161: this .resourceNamePrefix = resourceNamePrefix;
162: this .resourceMimeType = resourceMimeType;
163:
164: contentProvider = new ContentProvider();
165: setContentProvider(contentProvider);
166: setLabelProvider(new LabelProvider() {
167: public String getText(Object element) {
168: String text = ""; //$NON-NLS-1$
169: if (element instanceof ActionResource) {
170: ActionResource actionResource = (ActionResource) element;
171: String logicalName = actionResource.getPublicName();
172: ActionSequenceResource actionSequenceResource = actionDefinition
173: .getDocument().getResource(logicalName);
174: if (actionSequenceResource != null) {
175: text = actionSequenceResource.getPath();
176: if (actionSequenceResource
177: .getType()
178: .equals(
179: ActionSequenceResource.SOLUTION_FILE_RESOURCE_TYPE)) {
180: Path path = new Path(actionSequenceResource
181: .getPath());
182: if (path.isAbsolute()
183: && (path.getDevice() == null)) {
184: text = SOLUTION_FILE_PREFIX
185: + path.toString();
186: }
187: }
188: }
189: }
190: return text;
191: }
192: });
193: createPopupMenu();
194: int ops = DND.DROP_MOVE | DND.DROP_COPY;
195: dropAdapter = new ResourceListDropAdapter();
196: addDropSupport(ops,
197: new Transfer[] { ActionSequenceParamTransfer
198: .getInstance() }, dropAdapter);
199:
200: }
201:
202: /**
203: * Creates the popup menu used by this viewer.
204: */
205: protected void createPopupMenu() {
206: MenuManager popupMenu = new MenuManager();
207: popupMenu.add(addResourceAction);
208: popupMenu.add(deleteResourceAction);
209: popupMenu.addMenuListener(new IMenuListener() {
210: public void menuAboutToShow(IMenuManager manager) {
211: updatePopupMenuState(manager);
212: }
213: });
214:
215: org.eclipse.swt.widgets.List list = getList();
216: Menu menu = popupMenu.createContextMenu(list);
217: list.setMenu(menu);
218: }
219:
220: /**
221: * Removes the selected inputs/ouputs from the action definition.
222: */
223: protected void removeSelectedResources() {
224: IStructuredSelection selection = (IStructuredSelection) getSelection();
225: remove(selection.toArray());
226: List selections = selection.toList();
227: for (Iterator iter = selections.iterator(); iter.hasNext();) {
228: ActionResource selectedResource = (ActionResource) iter
229: .next();
230: String logicalName = selectedResource.getPublicName();
231: selectedResource.delete();
232: actionDefinition.getDocument().deleteResource(logicalName);
233: }
234: refresh();
235: }
236:
237: protected void addNewResource() {
238: FileDialog fileChooser = new FileDialog(getList().getShell(),
239: SWT.OPEN);
240: fileChooser
241: .setText(MessageFormat
242: .format(
243: Messages
244: .getString("ActionResourceList.SELECT_FILE"), new Object[] { Messages.getString("ActionResourceList.JAVASCRIPT_FILE") })); //$NON-NLS-1$ //$NON-NLS-2$
245: String filename = fileChooser.open();
246: if (filename != null) {
247: try {
248: addResource(filename);
249: } catch (URISyntaxException e) {
250: // File chooser should alwas return a valid path that can be used to create a URI.
251: }
252: }
253: }
254:
255: private void addResource(String resourceFileName)
256: throws URISyntaxException {
257: String fileSolution = SolutionHelper
258: .getSolutionName(resourceFileName);
259: Path filePath = new Path(resourceFileName);
260: String baseSolution = SolutionHelper.getSolutionName(basePath
261: .toString());
262: String solutionRoot = SolutionHelper.getSolutionRoot(basePath
263: .toString());
264: String uriScheme = ActionSequenceResource.FILE_SCHEME;
265:
266: if ((baseSolution != null) && baseSolution.equals(fileSolution)) {
267: if (filePath.matchingFirstSegments(basePath) == basePath
268: .segmentCount()) {
269: IPath relativeFilePath = filePath
270: .removeFirstSegments(basePath.segmentCount());
271: resourceFileName = relativeFilePath.setDevice(null)
272: .toString();
273: } else {
274: Path solutionPath = new Path(solutionRoot);
275: IPath relativePath = filePath
276: .removeFirstSegments(solutionPath
277: .segmentCount());
278: IPath absPath = relativePath.makeAbsolute().setDevice(
279: null);
280: resourceFileName = absPath.toString();
281: }
282: uriScheme = ActionSequenceResource.SOLUTION_SCHEME;
283: }
284:
285: ActionResource[] actionResources = actionDefinition
286: .getAllResourceParams();
287: boolean resourceFound = false;
288: ActionSequenceResource actionSequenceResource = null;
289: for (int i = 0; (i < actionResources.length) && !resourceFound; i++) {
290: String logicalName = actionResources[i].getPublicName();
291: actionSequenceResource = actionDefinition.getDocument()
292: .getResource(logicalName);
293: if (actionSequenceResource != null) {
294: resourceFound = actionSequenceResource.getPath()
295: .equals(resourceFileName);
296: }
297: }
298:
299: if (!resourceFound) {
300: String newResourceName = resourceNamePrefix;
301: int index = 1;
302: while ((actionDefinition.getResourceParam(newResourceName) != null)
303: || (actionDefinition.getDocument().getResource(
304: newResourceName) != null)) {
305: newResourceName = resourceNamePrefix
306: + Integer.toString(index++);
307: }
308:
309: URI uri = new URI(uriScheme, resourceFileName, null);
310: actionSequenceResource = actionDefinition.getDocument()
311: .setResourceUri(newResourceName, uri,
312: resourceMimeType);
313: ActionResource actionResource = actionDefinition
314: .addResourceParam(newResourceName);
315: add(actionResource);
316: }
317: }
318:
319: protected void updatePopupMenuState(IMenuManager menuMgr) {
320: IStructuredSelection selection = (IStructuredSelection) getSelection();
321: deleteResourceAction.setEnabled(selection.size() > 0);
322: }
323:
324: /* (non-Javadoc)
325: * @see org.eclipse.jface.viewers.Viewer#inputChanged(java.lang.Object, java.lang.Object)
326: */
327: protected void inputChanged(Object input, Object oldInput) {
328: actionDefinition = (ActionDefinition) input;
329:
330: super .inputChanged(input, oldInput);
331: }
332:
333: protected boolean canDrop(Object target, int op, TransferData type) {
334: return true;
335: }
336:
337: }
|