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.MalformedURLException;
016: import java.net.URI;
017: import java.net.URISyntaxException;
018: import java.net.URL;
019:
020: import org.eclipse.core.resources.IFile;
021: import org.eclipse.core.resources.IResource;
022: import org.eclipse.core.runtime.IPath;
023: import org.eclipse.core.runtime.Path;
024: import org.eclipse.jface.dialogs.MessageDialog;
025: import org.eclipse.swt.dnd.DND;
026: import org.eclipse.swt.dnd.DropTarget;
027: import org.eclipse.swt.dnd.DropTargetEvent;
028: import org.eclipse.swt.dnd.DropTargetListener;
029: import org.eclipse.swt.dnd.Transfer;
030: import org.eclipse.swt.events.ModifyEvent;
031: import org.eclipse.swt.events.ModifyListener;
032: import org.eclipse.swt.events.SelectionEvent;
033: import org.eclipse.swt.widgets.Composite;
034: import org.eclipse.swt.widgets.Control;
035: import org.eclipse.swt.widgets.Text;
036: import org.eclipse.ui.part.ResourceTransfer;
037: import org.pentaho.actionsequence.dom.ActionInputConstant;
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.actionsequence.dom.actions.JFreeReportAction;
042: import org.pentaho.actionsequence.dom.actions.SecureFilterAction;
043: import org.pentaho.designstudio.messages.Messages;
044: import org.pentaho.designstudio.util.SolutionHelper;
045:
046: public class PageTemplateText implements IActionSequenceControl,
047: ModifyListener {
048: public static final String SOLUTION_FILE_PREFIX = "solution:"; //$NON-NLS-1$
049: public static final String PLAIN_TEXT_MIME_TYPE = "text/plain"; //$NON-NLS-1$
050: protected Text resourceText;
051: protected ActionDefinition actionDefinition;
052: protected String resourceName;
053: DropTarget dropTarget;
054: String mimeType = PLAIN_TEXT_MIME_TYPE;
055: IPath basePath;
056:
057: public PageTemplateText(Composite parent, Object layoutData) {
058: resourceText = WidgetFactory.createText(parent, ""); //$NON-NLS-1$
059: if (layoutData != null) {
060: resourceText.setLayoutData(layoutData);
061: }
062:
063: dropTarget = new DropTarget(resourceText, DND.DROP_MOVE
064: | DND.DROP_COPY | DND.DROP_DEFAULT);
065: dropTarget.setTransfer(new Transfer[] {
066: ResourceTransfer.getInstance(),
067: ActionSequenceParamTransfer.getInstance() });
068:
069: dropTarget.addDropListener(new DropTargetListener() {
070:
071: public void dragEnter(DropTargetEvent event) {
072: if (event.detail == DND.DROP_DEFAULT) {
073: if ((event.operations & DND.DROP_COPY) != 0) {
074: event.detail = DND.DROP_COPY;
075: } else {
076: event.detail = DND.DROP_NONE;
077: }
078: }
079: }
080:
081: public void dragOver(DropTargetEvent event) {
082: }
083:
084: public void dragOperationChanged(DropTargetEvent event) {
085: }
086:
087: public void dragLeave(DropTargetEvent event) {
088: }
089:
090: public void dropAccept(DropTargetEvent event) {
091: }
092:
093: public void drop(DropTargetEvent event) {
094: if (ResourceTransfer.getInstance().isSupportedType(
095: event.currentDataType)) {
096: IResource[] resources = (IResource[]) event.data;
097: if ((resources.length == 1)
098: && (resources[0] instanceof IFile)) {
099: performDrop((IFile) resources[0]);
100: }
101: } else if (ActionSequenceParamTransfer.getInstance()
102: .isSupportedType(event.currentDataType)) {
103: if (event.data instanceof ActionSequenceResource) {
104: performDrop((ActionSequenceResource) event.data);
105: }
106: }
107: }
108: });
109: resourceText.addModifyListener(this );
110: }
111:
112: public void setActionDefinition(SecureFilterAction actionDefinition) {
113: this .actionDefinition = actionDefinition;
114: String template = actionDefinition.getXsl().getStringValue(); //$NON-NLS-1$
115: if ((template == null) || template.endsWith(".xsl")) { //$NON-NLS-1$
116: resourceName = "pageTemplate"; //$NON-NLS-1$
117: } else {
118: resourceName = template;
119: }
120: this .mimeType = PLAIN_TEXT_MIME_TYPE;
121: refresh();
122: }
123:
124: public void setActionDefinition(JFreeReportAction actionDefinition) {
125: this .actionDefinition = actionDefinition;
126: String template = actionDefinition.getXsl().getStringValue(); //$NON-NLS-1$
127: if ((template == null) || template.endsWith(".xsl")) { //$NON-NLS-1$
128: resourceName = "pageTemplate"; //$NON-NLS-1$
129: } else {
130: resourceName = template;
131: }
132: this .mimeType = PLAIN_TEXT_MIME_TYPE;
133: refresh();
134: }
135:
136: public ActionDefinition getActionDefinition() {
137: return actionDefinition;
138: }
139:
140: public String getResourceName() {
141: return resourceName;
142: }
143:
144: public void modifyText(ModifyEvent e) {
145: updateActionSequence();
146: }
147:
148: public void refresh() {
149: resourceText.removeModifyListener(this );
150: String textString = ""; //$NON-NLS-1$
151: String template = null;
152: if (actionDefinition instanceof SecureFilterAction) {
153: template = ((SecureFilterAction) actionDefinition).getXsl()
154: .getStringValue();
155: } else if (actionDefinition instanceof JFreeReportAction) {
156: template = ((JFreeReportAction) actionDefinition).getXsl()
157: .getStringValue();
158: }
159: if ((template != null) && (template.trim().length() > 0)) {
160: if (template.endsWith(".xsl")) { //$NON-NLS-1$
161: textString = template;
162: } else {
163: ActionResource actionResource = actionDefinition
164: .getResourceParam(resourceName);
165: if (actionResource != null) {
166: String logicalName = actionResource.getPublicName();
167: ActionSequenceResource actionSequenceResource = actionDefinition
168: .getDocument().getResource(logicalName);
169: if (actionSequenceResource != null) {
170: textString = actionSequenceResource.getPath();
171: if (actionSequenceResource
172: .getType()
173: .equals(
174: ActionSequenceResource.SOLUTION_FILE_RESOURCE_TYPE)) {
175: Path path = new Path(actionSequenceResource
176: .getPath());
177: if (path.isAbsolute()
178: && (path.getDevice() == null)) {
179: textString = SOLUTION_FILE_PREFIX
180: + path.toString();
181: }
182: }
183: }
184: }
185: }
186: }
187: resourceText.setText(textString);
188: resourceText.addModifyListener(this );
189: }
190:
191: public Control getControl() {
192: return resourceText;
193: }
194:
195: public void widgetDefaultSelected(SelectionEvent e) {
196: }
197:
198: public void widgetSelected(SelectionEvent e) {
199: updateActionSequence();
200: }
201:
202: private void removeResource() {
203: ActionResource actionResource = actionDefinition
204: .getResourceParam(resourceName);
205: if (actionResource != null) {
206: String logicalName = actionResource.getPublicName();
207: actionResource.delete();
208: ActionSequenceResource actionSequenceResource = actionDefinition
209: .getDocument().getResource(logicalName);
210: if ((actionSequenceResource != null)
211: && (actionDefinition.getDocument().getReferencesTo(
212: actionSequenceResource).length == 0)) {
213: actionSequenceResource.delete();
214: }
215: }
216: }
217:
218: public void updateActionSequence() {
219: String templateName = resourceText.getText().trim();
220: if ((templateName.length() == 0)
221: || templateName.equals(SOLUTION_FILE_PREFIX)) {
222: removeResource();
223: if (actionDefinition instanceof SecureFilterAction) {
224: ((SecureFilterAction) actionDefinition).setXsl(null);
225: } else if (actionDefinition instanceof JFreeReportAction) {
226: ((JFreeReportAction) actionDefinition).setXsl(null);
227: }
228: } else if (templateName.endsWith(".xsl")) { //$NON-NLS-1$
229: removeResource();
230: if (actionDefinition instanceof SecureFilterAction) {
231: ((SecureFilterAction) actionDefinition)
232: .setXsl(new ActionInputConstant(templateName));
233: } else if (actionDefinition instanceof JFreeReportAction) {
234: ((JFreeReportAction) actionDefinition)
235: .setXsl(new ActionInputConstant(templateName));
236: }
237: } else {
238: if (actionDefinition instanceof SecureFilterAction) {
239: ((SecureFilterAction) actionDefinition)
240: .setXsl(new ActionInputConstant(resourceName));
241: } else if (actionDefinition instanceof JFreeReportAction) {
242: ((JFreeReportAction) actionDefinition)
243: .setXsl(new ActionInputConstant(resourceName));
244: }
245: URI uri = null;
246: if (templateName
247: .startsWith(ActionSequenceResource.SOLUTION_SCHEME
248: + ":")
249: || templateName
250: .startsWith(ActionSequenceResource.FILE_SCHEME
251: + ":")) {
252: try {
253: int index = templateName.indexOf(":");
254: String scheme = templateName.substring(0, index);
255: String schemeSpecificPart = " ";
256: try {
257: schemeSpecificPart = templateName
258: .substring(index + 1);
259: } catch (Exception ex) {
260: // Do nothing. Scheme specific part will be null.
261: }
262: uri = new URI(scheme, schemeSpecificPart, null);
263: } catch (URISyntaxException e) {
264: // Should never get here.
265: }
266: } else {
267: try {
268: new URL(templateName);
269: try {
270: uri = new URI(templateName);
271: } catch (URISyntaxException e) {
272: // Should never get here.
273: }
274: } catch (MalformedURLException e) {
275: try {
276: uri = new URI(
277: ActionSequenceResource.FILE_SCHEME,
278: templateName, null);
279: } catch (URISyntaxException e2) {
280: }
281: }
282: }
283: actionDefinition
284: .setResourceUri(resourceName, uri, mimeType);
285: }
286: }
287:
288: private String getResourceType(String fileName) {
289: String resourceType = ActionSequenceResource.FILE_RESOURCE_TYPE;
290: if (fileName.startsWith(SOLUTION_FILE_PREFIX)) {
291: resourceType = ActionSequenceResource.SOLUTION_FILE_RESOURCE_TYPE;
292: } else {
293: try {
294: new URL(fileName);
295: resourceType = ActionSequenceResource.URL_RESOURCE_TYPE;
296: } catch (Exception ex) {
297: Path path = new Path(fileName);
298: if (!path.isAbsolute()) {
299: resourceType = ActionSequenceResource.SOLUTION_FILE_RESOURCE_TYPE;
300: }
301: }
302: }
303: return resourceType;
304: }
305:
306: protected void performDrop(IFile file) {
307: String fileName = file.getRawLocation().toString();
308: String fileSolution = SolutionHelper.getSolutionName(fileName);
309: Path filePath = new Path(fileName);
310: String baseSolution = SolutionHelper.getSolutionName(basePath
311: .toString());
312: String solutionRoot = SolutionHelper.getSolutionRoot(basePath
313: .toString());
314:
315: if (fileName.endsWith(".xsl")) { //$NON-NLS-1$
316: if ((baseSolution != null)
317: && baseSolution.equals(fileSolution)) {
318: boolean result = true;
319: if (filePath.matchingFirstSegments(basePath) == basePath
320: .segmentCount()) {
321: IPath relativeFilePath = filePath
322: .removeFirstSegments(basePath
323: .segmentCount());
324: if (relativeFilePath.segmentCount() != 1) {
325: MessageDialog
326: .openWarning(
327: resourceText.getShell(),
328: Messages
329: .getString("PageTemplateText.INVALID_FILE_LOCATION"), Messages.getString("PageTemplateText.INVALID_XSL_LOCATION")); //$NON-NLS-1$ //$NON-NLS-2$
330: result = false;
331: }
332: } else {
333: IPath solutionPath = new Path(solutionRoot);
334: IPath defaultXslPath = solutionPath
335: .removeLastSegments(1).append(
336: "system/custom/xsl"); //$NON-NLS-1$
337: if (!defaultXslPath.equals(filePath
338: .removeLastSegments(1))) {
339: MessageDialog
340: .openWarning(
341: resourceText.getShell(),
342: Messages
343: .getString("PageTemplateText.INVALID_FILE_LOCATION"), Messages.getString("PageTemplateText.INVALID_XSL_LOCATION")); //$NON-NLS-1$ //$NON-NLS-2$
344: result = false;
345: }
346: }
347: if (result) {
348: resourceText.setText(filePath.lastSegment());
349: }
350: } else {
351: resourceText.setText(filePath.lastSegment());
352: }
353: } else {
354: if ((baseSolution != null)
355: && baseSolution.equals(fileSolution)) {
356: if (filePath.matchingFirstSegments(basePath) == basePath
357: .segmentCount()) {
358: IPath relativeFilePath = filePath
359: .removeFirstSegments(basePath
360: .segmentCount());
361: resourceText.setText(relativeFilePath.setDevice(
362: null).toString());
363: } else {
364: Path solutionPath = new Path(solutionRoot);
365: IPath relativePath = filePath
366: .removeFirstSegments(solutionPath
367: .segmentCount());
368: IPath absPath = relativePath.makeAbsolute()
369: .setDevice("solution:"); //$NON-NLS-1$
370: resourceText.setText(absPath.toString());
371: }
372: } else {
373: resourceText.setText(fileName);
374: }
375: }
376: }
377:
378: protected void performDrop(
379: ActionSequenceResource actionSequenceResource) {
380: if (actionDefinition.getDocument().getResource(
381: actionSequenceResource.getName()) != null) {
382: actionDefinition.addResourceParam(resourceName).setMapping(
383: actionSequenceResource.getName());
384: refresh();
385: }
386: }
387:
388: public void setBasePath(IPath basePath) {
389: this .basePath = basePath;
390: }
391:
392: public String getText() {
393: return resourceText.getText();
394: }
395:
396: public void setText(String string) {
397: resourceText.setText(string);
398: }
399: }
|