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.lang.reflect.InvocationTargetException;
016: import java.lang.reflect.Method;
017: import java.net.MalformedURLException;
018: import java.net.URI;
019: import java.net.URISyntaxException;
020: import java.net.URL;
021:
022: import org.eclipse.core.resources.IFile;
023: import org.eclipse.core.resources.IResource;
024: import org.eclipse.core.runtime.IPath;
025: import org.eclipse.core.runtime.Path;
026: import org.eclipse.swt.dnd.DND;
027: import org.eclipse.swt.dnd.DropTarget;
028: import org.eclipse.swt.dnd.DropTargetEvent;
029: import org.eclipse.swt.dnd.DropTargetListener;
030: import org.eclipse.swt.dnd.Transfer;
031: import org.eclipse.swt.events.ModifyEvent;
032: import org.eclipse.swt.events.ModifyListener;
033: import org.eclipse.swt.events.SelectionEvent;
034: import org.eclipse.swt.widgets.Composite;
035: import org.eclipse.swt.widgets.Control;
036: import org.eclipse.swt.widgets.Text;
037: import org.eclipse.ui.part.ResourceTransfer;
038: import org.pentaho.actionsequence.dom.ActionResource;
039: import org.pentaho.actionsequence.dom.ActionSequenceResource;
040: import org.pentaho.actionsequence.dom.actions.ActionDefinition;
041: import org.pentaho.designstudio.util.SolutionHelper;
042:
043: public class ActionResourceText implements IActionSequenceControl,
044: ModifyListener {
045: public static final String PLAIN_TEXT_MIME_TYPE = "text/plain"; //$NON-NLS-1$
046: protected Text resourceText;
047: protected ActionDefinition actionDefinition;
048: protected String resourceName;
049: DropTarget dropTarget;
050: protected String mimeType = PLAIN_TEXT_MIME_TYPE;
051: IPath basePath;
052: Method getValueMethod;
053: Method setValueMethod;
054:
055: public ActionResourceText(Composite parent, Object layoutData) {
056: resourceText = WidgetFactory.createText(parent, ""); //$NON-NLS-1$
057: if (layoutData != null) {
058: resourceText.setLayoutData(layoutData);
059: }
060:
061: dropTarget = new DropTarget(resourceText, DND.DROP_MOVE
062: | DND.DROP_COPY | DND.DROP_DEFAULT);
063: dropTarget.setTransfer(new Transfer[] {
064: ResourceTransfer.getInstance(),
065: ActionSequenceParamTransfer.getInstance() });
066:
067: dropTarget.addDropListener(new DropTargetListener() {
068:
069: public void dragEnter(DropTargetEvent event) {
070: if (event.detail == DND.DROP_DEFAULT) {
071: if ((event.operations & DND.DROP_COPY) != 0) {
072: event.detail = DND.DROP_COPY;
073: } else {
074: event.detail = DND.DROP_NONE;
075: }
076: }
077: }
078:
079: public void dragOver(DropTargetEvent event) {
080: }
081:
082: public void dragOperationChanged(DropTargetEvent event) {
083: }
084:
085: public void dragLeave(DropTargetEvent event) {
086: }
087:
088: public void dropAccept(DropTargetEvent event) {
089: }
090:
091: public void drop(DropTargetEvent event) {
092: if (ResourceTransfer.getInstance().isSupportedType(
093: event.currentDataType)) {
094: IResource[] resources = (IResource[]) event.data;
095: if ((resources.length == 1)
096: && (resources[0] instanceof IFile)) {
097: performDrop((IFile) resources[0]);
098: }
099: } else if (ActionSequenceParamTransfer.getInstance()
100: .isSupportedType(event.currentDataType)) {
101: if (event.data instanceof ActionSequenceResource) {
102: performDrop((ActionSequenceResource) event.data);
103: }
104: }
105: }
106: });
107: resourceText.addModifyListener(this );
108: }
109:
110: public void setTargetResource(ActionDefinition actionDefinition,
111: String resourceName) {
112: setTargetResource(actionDefinition, resourceName,
113: PLAIN_TEXT_MIME_TYPE);
114: }
115:
116: public void setTargetResource(ActionDefinition actionDefinition,
117: String resourceName, String mimeType) {
118: this .actionDefinition = actionDefinition;
119: if ((resourceName != null)
120: && (resourceName.trim().length() > 0)) {
121: String propertyName = getPropertyName(resourceName);
122: try {
123: getValueMethod = actionDefinition.getClass().getMethod(
124: "get" + propertyName, new Class[0]); //$NON-NLS-1$
125: } catch (Exception ex) {
126: }
127: try {
128: setValueMethod = actionDefinition
129: .getClass()
130: .getMethod(
131: "set" + propertyName, new Class[] { URI.class, String.class }); //$NON-NLS-1$
132: } catch (Exception ex) {
133: }
134: } else {
135: getValueMethod = null;
136: setValueMethod = null;
137: }
138: this .actionDefinition = actionDefinition;
139: this .resourceName = resourceName;
140: this .mimeType = mimeType;
141: refresh();
142: }
143:
144: public ActionDefinition getActionDefinition() {
145: return actionDefinition;
146: }
147:
148: public String getResourceName() {
149: return resourceName;
150: }
151:
152: public void modifyText(ModifyEvent e) {
153: updateActionSequence();
154: }
155:
156: public void refresh() {
157: resourceText.removeModifyListener(this );
158: URI uri = null;
159:
160: try {
161: if (getValueMethod != null) {
162: Object value = getValueMethod.invoke(actionDefinition,
163: new Object[0]);
164: if (value instanceof ActionResource) {
165: uri = ((ActionResource) value).getUri();
166: }
167: }
168: } catch (IllegalArgumentException e) {
169: // TODO Auto-generated catch block
170: e.printStackTrace();
171: } catch (IllegalAccessException e) {
172: // TODO Auto-generated catch block
173: e.printStackTrace();
174: } catch (InvocationTargetException e) {
175: // TODO Auto-generated catch block
176: e.printStackTrace();
177: }
178: if ((uri == null)
179: || (uri.getSchemeSpecificPart().trim().length() == 0)) {
180: resourceText.setText("");
181: } else {
182: resourceText.setText(uri.getScheme() + ":"
183: + uri.getSchemeSpecificPart().trim());
184: }
185: resourceText.addModifyListener(this );
186: }
187:
188: public Control getControl() {
189: return resourceText;
190: }
191:
192: public void widgetDefaultSelected(SelectionEvent e) {
193: }
194:
195: public void widgetSelected(SelectionEvent e) {
196: updateActionSequence();
197: }
198:
199: public void updateActionSequence() {
200: String resourceFileName = resourceText.getText().trim();
201: URI uri = null;
202: try {
203: if ((resourceFileName.trim().length() > 0)
204: && !resourceFileName
205: .equals(ActionSequenceResource.FILE_SCHEME
206: + ":")
207: && !resourceFileName
208: .equals(ActionSequenceResource.SOLUTION_SCHEME
209: + ":")) {
210: if (resourceFileName
211: .startsWith(ActionSequenceResource.SOLUTION_SCHEME
212: + ":")
213: || resourceFileName
214: .startsWith(ActionSequenceResource.FILE_SCHEME
215: + ":")) {
216: try {
217: int index = resourceFileName.indexOf(":");
218: String scheme = resourceFileName.substring(0,
219: index);
220: String schemeSpecificPart = resourceFileName
221: .substring(index + 1);
222: uri = new URI(scheme, schemeSpecificPart, null);
223: } catch (URISyntaxException e) {
224: // Should never get here.
225: }
226: } else {
227: try {
228: new URL(resourceFileName);
229: try {
230: uri = new URI(resourceFileName);
231: } catch (URISyntaxException e) {
232: // Should never get here.
233: }
234: } catch (MalformedURLException e) {
235: try {
236: uri = new URI(
237: ActionSequenceResource.FILE_SCHEME,
238: resourceFileName, null);
239: } catch (URISyntaxException e2) {
240: }
241: }
242: }
243: setValueMethod.invoke(actionDefinition, new Object[] {
244: uri, mimeType });
245: } else {
246: setValueMethod.invoke(actionDefinition, new Object[] {
247: null, null });
248: }
249: } catch (IllegalArgumentException e) {
250: // TODO Auto-generated catch block
251: e.printStackTrace();
252: } catch (IllegalAccessException e) {
253: // TODO Auto-generated catch block
254: e.printStackTrace();
255: } catch (InvocationTargetException e) {
256: // TODO Auto-generated catch block
257: e.printStackTrace();
258: }
259: }
260:
261: protected void performDrop(IFile file) {
262: String fileName = file.getRawLocation().toString();
263: String fileSolution = SolutionHelper.getSolutionName(fileName);
264: Path filePath = new Path(fileName);
265: String baseSolution = SolutionHelper.getSolutionName(basePath
266: .toString());
267: String solutionRoot = SolutionHelper.getSolutionRoot(basePath
268: .toString());
269:
270: if ((baseSolution != null) && baseSolution.equals(fileSolution)) {
271: if (filePath.matchingFirstSegments(basePath) == basePath
272: .segmentCount()) {
273: IPath relativeFilePath = filePath
274: .removeFirstSegments(basePath.segmentCount());
275: resourceText.setText(relativeFilePath.setDevice(null)
276: .toString());
277: } else {
278: Path solutionPath = new Path(solutionRoot);
279: IPath relativePath = filePath
280: .removeFirstSegments(solutionPath
281: .segmentCount());
282: IPath absPath = relativePath.makeAbsolute().setDevice(
283: "solution:"); //$NON-NLS-1$
284: resourceText.setText(absPath.toString());
285: }
286: } else {
287: resourceText.setText(fileName);
288: }
289: }
290:
291: protected void performDrop(
292: ActionSequenceResource actionSequenceResource) {
293: if (actionDefinition.getDocument().getResource(
294: actionSequenceResource.getName()) != null) {
295: actionDefinition.addResourceParam(resourceName).setMapping(
296: actionSequenceResource.getName());
297: refresh();
298: }
299: }
300:
301: public void setBasePath(IPath basePath) {
302: this .basePath = basePath;
303: }
304:
305: public String getText() {
306: return resourceText.getText();
307: }
308:
309: public void setText(String string) {
310: resourceText.setText(string);
311: }
312:
313: private String getPropertyName(String inputName) {
314: StringBuffer stringBuffer = new StringBuffer(inputName
315: .toLowerCase());
316: stringBuffer.setCharAt(0, Character.toUpperCase(stringBuffer
317: .charAt(0)));
318: for (int index = stringBuffer.toString().indexOf('-'); index != -1; index = stringBuffer
319: .toString().indexOf('-')) {
320: stringBuffer.deleteCharAt(index);
321: if (index < stringBuffer.length()) {
322: stringBuffer.setCharAt(index, Character
323: .toUpperCase(stringBuffer.charAt(index)));
324: }
325: }
326: for (int index = stringBuffer.toString().indexOf('_'); index != -1; index = stringBuffer
327: .toString().indexOf('_')) {
328: stringBuffer.deleteCharAt(index);
329: if (index < stringBuffer.length()) {
330: stringBuffer.setCharAt(index, Character
331: .toUpperCase(stringBuffer.charAt(index)));
332: }
333: }
334: return stringBuffer.toString();
335: }
336: }
|