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.util.ArrayList;
018:
019: import org.eclipse.core.runtime.IPath;
020: import org.eclipse.core.runtime.Path;
021: import org.eclipse.swt.SWT;
022: import org.eclipse.swt.events.SelectionEvent;
023: import org.eclipse.swt.events.SelectionListener;
024: import org.eclipse.swt.graphics.Image;
025: import org.eclipse.swt.graphics.Point;
026: import org.eclipse.swt.graphics.Rectangle;
027: import org.eclipse.swt.widgets.Composite;
028: import org.eclipse.swt.widgets.Control;
029: import org.eclipse.swt.widgets.FileDialog;
030: import org.eclipse.swt.widgets.Menu;
031: import org.eclipse.swt.widgets.MenuItem;
032: import org.eclipse.swt.widgets.ToolBar;
033: import org.eclipse.swt.widgets.ToolItem;
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.ActionSequenceResource;
038: import org.pentaho.actionsequence.dom.IActionInputVariable;
039: import org.pentaho.designstudio.editors.actionsequence.ActionSequenceEditorPlugin;
040: import org.pentaho.designstudio.messages.Messages;
041: import org.pentaho.designstudio.util.SolutionHelper;
042:
043: public class EmailAttachmentsToolbar {
044:
045: ToolBar toolBar;
046: EmailAttachmentsTable emailAttachmentsTable;
047: ToolItem addToolItem;
048: ToolItem deleteToolItem;
049: Menu addInputMenu;
050: IPath basePath;
051:
052: static final Image MOVE_UP_ICON = ActionSequenceEditorPlugin
053: .getImageDescriptor(
054: Messages
055: .getString("ActionsMasterDetailBlock.ICON_MOVE_UP")).createImage(); //$NON-NLS-1$
056: static final Image MOVE_DOWN_ICON = ActionSequenceEditorPlugin
057: .getImageDescriptor(
058: Messages
059: .getString("ActionsMasterDetailBlock.ICON_MOVE_DOWN")).createImage(); //$NON-NLS-1$
060: static final Image ADD_ACTION_ICON = ActionSequenceEditorPlugin
061: .getImageDescriptor(
062: Messages
063: .getString("ActionsMasterDetailBlock.ICON_ADD_ACTION")).createImage(); //$NON-NLS-1$
064: static final Image REMOVE_ACTION_ICON = ActionSequenceEditorPlugin
065: .getImageDescriptor(
066: Messages
067: .getString("ActionsMasterDetailBlock.ICON_REMOVE_ACTION")).createImage(); //$NON-NLS-1$
068:
069: class AddInputMenuItem {
070: IActionInputVariable actionSequenceIO;
071: MenuItem menuItem;
072:
073: AddInputMenuItem(Menu parent) {
074: menuItem = new MenuItem(parent, SWT.NONE);
075: menuItem.setText("Browse...");
076: menuItem.addSelectionListener(new SelectionListener() {
077: public void widgetDefaultSelected(SelectionEvent e) {
078: }
079:
080: public void widgetSelected(SelectionEvent e) {
081: FileDialog fileChooser = new FileDialog(
082: emailAttachmentsTable.getTable().getShell(),
083: SWT.OPEN);
084: fileChooser
085: .setText(Messages
086: .getString("JasperReportDetailsPage.SELECT_JASPER_REPORT")); //$NON-NLS-1$
087: fileChooser
088: .setFilterExtensions(new String[] { "*.*" }); //$NON-NLS-1$ //$NON-NLS-2$
089: fileChooser
090: .setFilterNames(new String[] { Messages
091: .getString("JasperReportDetailsPage.ALL_FILES") }); //$NON-NLS-1$ //$NON-NLS-2$
092: fileChooser.setFilterPath(basePath.toString());
093: String filename = fileChooser.open();
094: if (filename != null) {
095: String fileSolution = SolutionHelper
096: .getSolutionName(filename);
097: Path filePath = new Path(filename);
098: String baseSolution = SolutionHelper
099: .getSolutionName(basePath.toString());
100: String solutionRoot = SolutionHelper
101: .getSolutionRoot(basePath.toString());
102:
103: String scheme = ActionSequenceResource.FILE_SCHEME;
104: String schemeSpecificPart = filename;
105: if ((baseSolution != null)
106: && baseSolution.equals(fileSolution)) {
107: scheme = ActionSequenceResource.SOLUTION_SCHEME;
108: if (filePath
109: .matchingFirstSegments(basePath) == basePath
110: .segmentCount()) {
111: IPath relativeFilePath = filePath
112: .removeFirstSegments(basePath
113: .segmentCount());
114: schemeSpecificPart = relativeFilePath
115: .setDevice(null).toString();
116: } else {
117: Path solutionPath = new Path(
118: solutionRoot);
119: IPath relativePath = filePath
120: .removeFirstSegments(solutionPath
121: .segmentCount());
122: IPath absPath = relativePath
123: .makeAbsolute(); //$NON-NLS-1$
124: absPath.setDevice(null);
125: schemeSpecificPart = absPath.setDevice(
126: null).toString();
127: }
128: }
129: try {
130: emailAttachmentsTable.addAttachment(
131: filePath.lastSegment(), new URI(
132: scheme, schemeSpecificPart,
133: null), "text/plain");
134: } catch (URISyntaxException e1) {
135: // Should never get here.
136: e1.printStackTrace();
137: }
138: }
139: }
140: });
141: }
142:
143: AddInputMenuItem(Menu parent, IActionInputVariable actSeqIO) {
144: menuItem = new MenuItem(parent, SWT.NONE);
145: actionSequenceIO = actSeqIO;
146: menuItem.setText(ActionUtil
147: .parameterToDisplayText(actionSequenceIO
148: .getVariableName()));
149: menuItem.addSelectionListener(new SelectionListener() {
150: public void widgetDefaultSelected(SelectionEvent e) {
151: }
152:
153: public void widgetSelected(SelectionEvent e) {
154: emailAttachmentsTable
155: .addAttachment(actionSequenceIO);
156: }
157: });
158: }
159: }
160:
161: public EmailAttachmentsToolbar(Composite parent,
162: EmailAttachmentsTable attachmentsTable, IPath basePath) {
163: this .basePath = basePath;
164: emailAttachmentsTable = attachmentsTable;
165:
166: toolBar = new ToolBar(parent, SWT.FLAT);
167:
168: addToolItem = new ToolItem(toolBar, SWT.DROP_DOWN);
169: addToolItem.setImage(ADD_ACTION_ICON);
170: addToolItem.setToolTipText(Messages
171: .getString("ActionsMasterDetailBlock.ADD_NEW_INPUT")); //$NON-NLS-1$
172:
173: addToolItem.addSelectionListener(new SelectionListener() {
174: public void widgetDefaultSelected(SelectionEvent e) {
175: }
176:
177: public void widgetSelected(SelectionEvent e) {
178: ToolItem item = (ToolItem) e.widget;
179: Rectangle rectangle = item.getBounds();
180: Point pt = item.getParent().toDisplay(
181: new Point(rectangle.x, rectangle.y));
182: addInputMenu.setLocation(pt.x, pt.y + rectangle.height);
183: addInputMenu.setVisible(true);
184: }
185:
186: });
187:
188: deleteToolItem = new ToolItem(toolBar, SWT.NULL);
189: deleteToolItem.setImage(REMOVE_ACTION_ICON);
190: deleteToolItem
191: .setToolTipText(Messages
192: .getString("ActionsMasterDetailBlock.REMOVE_SELECTED_INPUT")); //$NON-NLS-1$
193: deleteToolItem.addSelectionListener(new SelectionListener() {
194: public void widgetDefaultSelected(SelectionEvent e) {
195: }
196:
197: public void widgetSelected(SelectionEvent e) {
198: emailAttachmentsTable.removeSelectedAttachment();
199: }
200: });
201:
202: }
203:
204: public void refresh() {
205: if (addInputMenu != null) {
206: addInputMenu.dispose();
207: }
208:
209: addInputMenu = new Menu(emailAttachmentsTable.getTable()
210: .getShell(), SWT.POP_UP);
211: IActionInputVariable[] availInputs = emailAttachmentsTable.emailAction
212: .getAvailInputVariables(ActionSequenceDocument.CONTENT_TYPE);
213: ArrayList availInputNames = new ArrayList();
214: for (int i = 0; i < availInputs.length; i++) {
215: if (availInputs[i] instanceof ActionOutput) {
216: ActionOutput actionOutput = (ActionOutput) availInputs[i];
217: if (!availInputNames.contains(actionOutput
218: .getPublicName())) {
219: new AddInputMenuItem(addInputMenu, actionOutput);
220: availInputNames.add(actionOutput.getPublicName());
221: }
222: } else if (availInputs[i] instanceof ActionSequenceInput) {
223: ActionSequenceInput actionSequenceInput = (ActionSequenceInput) availInputs[i];
224: if (!availInputNames.contains(actionSequenceInput
225: .getName())) {
226: new AddInputMenuItem(addInputMenu,
227: actionSequenceInput);
228: availInputNames.add(actionSequenceInput.getName());
229: }
230: }
231: }
232: new AddInputMenuItem(addInputMenu);
233: }
234:
235: public Control getControl() {
236: return toolBar;
237: }
238: }
|