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.util.ArrayList;
017: import java.util.Iterator;
018: import java.util.List;
019:
020: import org.eclipse.jface.action.Action;
021: import org.eclipse.jface.action.IMenuListener;
022: import org.eclipse.jface.action.IMenuManager;
023: import org.eclipse.jface.action.MenuManager;
024: import org.eclipse.jface.viewers.CellEditor;
025: import org.eclipse.jface.viewers.ICellModifier;
026: import org.eclipse.jface.viewers.ILabelProviderListener;
027: import org.eclipse.jface.viewers.IStructuredContentProvider;
028: import org.eclipse.jface.viewers.IStructuredSelection;
029: import org.eclipse.jface.viewers.ITableLabelProvider;
030: import org.eclipse.jface.viewers.TableViewer;
031: import org.eclipse.jface.viewers.Viewer;
032: import org.eclipse.jface.viewers.ViewerDropAdapter;
033: import org.eclipse.swt.SWT;
034: import org.eclipse.swt.custom.CCombo;
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.graphics.Image;
039: import org.eclipse.swt.widgets.Composite;
040: import org.eclipse.swt.widgets.Menu;
041: import org.eclipse.swt.widgets.Table;
042: import org.eclipse.swt.widgets.TableColumn;
043: import org.eclipse.swt.widgets.TableItem;
044: import org.pentaho.actionsequence.dom.ActionInput;
045: import org.pentaho.actionsequence.dom.ActionResource;
046: import org.pentaho.actionsequence.dom.ActionSequenceDocument;
047: import org.pentaho.actionsequence.dom.ActionSequenceResource;
048: import org.pentaho.actionsequence.dom.IActionInputVariable;
049: import org.pentaho.actionsequence.dom.SimpleActionInputVariable;
050: import org.pentaho.actionsequence.dom.actions.EmailAction;
051: import org.pentaho.actionsequence.dom.actions.EmailAttachment;
052: import org.pentaho.designstudio.messages.Messages;
053:
054: /**
055: * Table viewer used for modifying email attachments in an email action definition.
056: *
057: * @author Angelo Rodriguez
058: */
059: public class EmailAttachmentsTable extends TableViewer {
060:
061: static final int NAME_COLUMN_IDX = 0;
062: static final String NAME_COLUMN_HDR = Messages
063: .getString("EmailAttachmentsTable.UI_NAME_COL_LABEL"); //$NON-NLS-1$
064: static final String NAME_COLUMN_PROP = "NAME"; //$NON-NLS-1$
065:
066: static final int ATTACH_COLUMN_IDX = 1;
067: static final String ATTACH_COLUMN_HDR = Messages
068: .getString("EmailAttachmentsTable.UI_ATTACHMENT_COL_LABEL"); //$NON-NLS-1$
069: static final String ATTACH_COLUMN_PROP = "ATTACHMENT"; //$NON-NLS-1$
070:
071: EmailAction emailAction;
072:
073: DeleteAttachmentAction deleteAttachmentAction = new DeleteAttachmentAction();
074:
075: ContentProvider contentProvider;
076: private AttachmentDropAdapter dropAdapter;
077: AttachmentNameCellEditor attachmentNameCellEditor;
078:
079: class AttachmentNameCellEditor extends CustomComboBoxCellEditor {
080:
081: public AttachmentNameCellEditor(Composite parent) {
082: super (parent, new String[0]);
083: }
084:
085: protected void doSetValue(Object value) {
086: ((CCombo) getControl()).setText(value.toString());
087: }
088:
089: protected Object doGetValue() {
090: return ((CCombo) getControl()).getText();
091: }
092: }
093:
094: class AttachmentDropAdapter extends ViewerDropAdapter {
095:
096: AttachmentDropAdapter() {
097: super (EmailAttachmentsTable.this );
098: setFeedbackEnabled(true);
099: }
100:
101: public boolean performDrop(Object data) {
102: if (data instanceof IActionInputVariable) {
103: IActionInputVariable actionVariable = (IActionInputVariable) data;
104: if (ActionSequenceDocument.CONTENT_TYPE
105: .equals(actionVariable.getType())) {
106: addAttachment(actionVariable);
107: }
108: } else if (data instanceof ActionSequenceResource) {
109: ActionSequenceResource actionSequenceResource = (ActionSequenceResource) data;
110: addAttachment(actionSequenceResource.getName(),
111: actionSequenceResource.getUri(),
112: actionSequenceResource.getMimeType());
113: }
114: return true;
115: }
116:
117: public boolean validateDrop(Object target, int op,
118: TransferData type) {
119: return true;
120: }
121: }
122:
123: public class CellModifier implements ICellModifier {
124:
125: /**
126: * Creates a cell modifier.
127: * @param viewer the viewer whose cells will be modified.
128: */
129: public CellModifier() {
130: super ();
131: }
132:
133: /* (non-Javadoc)
134: * @see org.eclipse.jface.viewers.ICellModifier#canModify(java.lang.Object, java.lang.String)
135: */
136: public boolean canModify(Object element, String property) {
137: return property.equals(NAME_COLUMN_PROP);
138: }
139:
140: /* (non-Javadoc)
141: * @see org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.Object, java.lang.String)
142: */
143: public Object getValue(Object tableObject, String property) {
144: String value = null;
145: if (property.equals(NAME_COLUMN_PROP)) {
146: if (tableObject instanceof EmailAttachment) {
147: EmailAttachment emailAttachment = (EmailAttachment) tableObject;
148: ActionInput actionInput = emailAttachment
149: .getNameParam();
150: if (actionInput != null) {
151: value = ActionUtil
152: .parameterToDisplayText(actionInput
153: .getName());
154: } else {
155: value = emailAttachment.getName();
156: }
157: }
158: }
159:
160: return value != null ? value : ""; //$NON-NLS-1$
161: }
162:
163: /* (non-Javadoc)
164: * @see org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Object, java.lang.String, java.lang.Object)
165: */
166: public void modify(Object tableObject, String property,
167: Object value) {
168: if (value != null) {
169: String name = value.toString().trim();
170: if (property.equals(NAME_COLUMN_PROP)
171: && (name.length() > 0)) {
172: TableItem tableItem = (TableItem) tableObject;
173: EmailAttachment emailAttachment = (EmailAttachment) tableItem
174: .getData();
175: if (ActionUtil.isParamDispText(name)) {
176: name = ActionUtil
177: .parameterFromDisplayText(name);
178: IActionInputVariable[] availInputs = emailAction
179: .getAvailInputVariables(ActionSequenceDocument.STRING_TYPE);
180: IActionInputVariable availInput = null;
181: for (int i = 0; i < availInputs.length; i++) {
182: if (availInputs[i].getVariableName()
183: .equals(name)) {
184: availInput = availInputs[i];
185: break;
186: }
187: }
188: if (availInput == null) {
189: availInput = new SimpleActionInputVariable(
190: name,
191: ActionSequenceDocument.STRING_TYPE);
192: }
193: emailAttachment.setNameParam(availInput);
194: } else {
195: emailAttachment.setName(name);
196: }
197: refresh();
198: }
199: }
200: }
201: }
202:
203: public class LabelProvider implements ITableLabelProvider {
204:
205: List listeners = new ArrayList();
206:
207: /**
208: * Creates a label provider.
209: * @param ioInfo a description of the inputs or outputs being displayed by the viewer.
210: */
211: public LabelProvider() {
212: super ();
213: }
214:
215: /* (non-Javadoc)
216: * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
217: */
218: public Image getColumnImage(Object element, int columnIndex) {
219: return null;
220: }
221:
222: /* (non-Javadoc)
223: * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
224: */
225: public String getColumnText(Object element, int columnIndex) {
226: String columnText = ""; //$NON-NLS-1$
227: EmailAttachment emailAttachment = (EmailAttachment) element;
228: ActionInput actionInput = null;
229: switch (columnIndex) {
230: case NAME_COLUMN_IDX:
231: actionInput = emailAttachment.getNameParam();
232: if (actionInput != null) {
233: columnText = ActionUtil
234: .parameterToDisplayText(actionInput
235: .getReferencedVariableName());
236: } else {
237: columnText = emailAttachment.getName();
238: if (columnText == null) {
239: columnText = ""; //$NON-NLS-1$
240: }
241: }
242: break;
243: case ATTACH_COLUMN_IDX:
244: actionInput = emailAttachment.getContentParam();
245: if (actionInput != null) {
246: columnText = ActionUtil
247: .parameterToDisplayText(actionInput
248: .getName());
249: } else {
250: ActionResource actionResource = emailAttachment
251: .getContentResource();
252: if (actionResource != null) {
253: URI uri = actionResource.getUri();
254: if ((uri != null)
255: && (uri.getSchemeSpecificPart().trim()
256: .length() > 0)) {
257: columnText = uri.getScheme()
258: + ":"
259: + uri.getSchemeSpecificPart()
260: .trim();
261: }
262: }
263: }
264: break;
265: }
266: return columnText;
267: }
268:
269: /* (non-Javadoc)
270: * @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener)
271: */
272: public void addListener(ILabelProviderListener listener) {
273: listeners.add(listener);
274: }
275:
276: /* (non-Javadoc)
277: * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
278: */
279: public void dispose() {
280: }
281:
282: /* (non-Javadoc)
283: * @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java.lang.Object, java.lang.String)
284: */
285: public boolean isLabelProperty(Object element, String property) {
286: return true;
287: }
288:
289: /* (non-Javadoc)
290: * @see org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener)
291: */
292: public void removeListener(ILabelProviderListener listener) {
293: listeners.add(listener);
294: }
295:
296: }
297:
298: public class ContentProvider implements IStructuredContentProvider {
299:
300: ArrayList attachmentsList = new ArrayList();
301:
302: /**
303: * Placeholder used to indicate that a new row may be added to the viewer.
304: */
305:
306: public ContentProvider(boolean provideInputs) {
307: super ();
308: }
309:
310: public Object[] getElements(Object ioElement) {
311: return emailAction.getAttachments();
312: }
313:
314: public void dispose() {
315: }
316:
317: public void inputChanged(Viewer viewer, Object oldInput,
318: Object newInput) {
319: emailAction = (EmailAction) newInput;
320: }
321:
322: }
323:
324: class DeleteAttachmentAction extends Action {
325: public DeleteAttachmentAction() {
326: super ("Delete"); //$NON-NLS-1$
327: }
328:
329: public void run() {
330: removeSelectedAttachment();
331: }
332: }
333:
334: /**
335: * Creates an viewer
336: * @param parent the parent of this viewer.
337: * @param toolkit the form toolkit.
338: * @param ioInfo a description of the inputs/outputs allowed within the action definition.
339: */
340: public EmailAttachmentsTable(Composite parent) {
341: super (WidgetFactory.createTable(parent, SWT.FULL_SELECTION
342: | SWT.BORDER));
343:
344: Table table = getTable();
345: table.setHeaderVisible(true);
346: createTableColumns();
347: createContentProvider();
348: createLabelProvider();
349: createCellEditors();
350: createPopupMenu();
351: int ops = DND.DROP_MOVE | DND.DROP_COPY;
352: dropAdapter = new AttachmentDropAdapter();
353: addDropSupport(ops,
354: new Transfer[] { ActionSequenceParamTransfer
355: .getInstance() }, dropAdapter);
356: }
357:
358: protected void createContentProvider() {
359: contentProvider = new ContentProvider(true);
360: setContentProvider(contentProvider);
361: }
362:
363: /* (non-Javadoc)
364: * @see org.pentaho.designstudio.editors.actionsequence.pages.actions.ActionIOViewer#createLabelProvider()
365: */
366: protected void createLabelProvider() {
367: setLabelProvider(new LabelProvider());
368: }
369:
370: /* (non-Javadoc)
371: * @see org.pentaho.designstudio.editors.actionsequence.pages.actions.ActionIOViewer#createCellEditors()
372: */
373: protected void createCellEditors() {
374: Table table = getTable();
375: attachmentNameCellEditor = new AttachmentNameCellEditor(table);
376: CellEditor[] editors = new CellEditor[] {
377: attachmentNameCellEditor, null };
378: setCellEditors(editors);
379: setCellModifier(new CellModifier());
380: setColumnProperties(new String[] { NAME_COLUMN_PROP,
381: ATTACH_COLUMN_PROP });
382:
383: }
384:
385: /**
386: * Creates the popup menu used by this viewer.
387: */
388: protected void createPopupMenu() {
389: MenuManager popupMenu = new MenuManager();
390: popupMenu.add(deleteAttachmentAction);
391: popupMenu.addMenuListener(new IMenuListener() {
392: public void menuAboutToShow(IMenuManager manager) {
393: updatePopupMenuState(manager);
394: }
395: });
396:
397: Table table = getTable();
398: Menu menu = popupMenu.createContextMenu(table);
399: table.setMenu(menu);
400: }
401:
402: /**
403: * Removes the selected inputs/ouputs from the action definition.
404: */
405: protected void removeSelectedAttachment() {
406: IStructuredSelection selection = (IStructuredSelection) getSelection();
407: remove(selection.toArray());
408: List selections = selection.toList();
409: for (Iterator iter = selections.iterator(); iter.hasNext();) {
410: EmailAttachment emailAttachment = (EmailAttachment) iter
411: .next();
412: emailAttachment.delete();
413: ActionSequenceDocument.fireActionChanged(emailAction);
414: }
415: refresh();
416: }
417:
418: public boolean performDrop(Object data) {
419: boolean result = false;
420: if (data instanceof IActionInputVariable) {
421: IActionInputVariable actionVariable = (IActionInputVariable) data;
422: if (ActionSequenceDocument.CONTENT_TYPE
423: .equals(actionVariable.getType())) {
424: boolean alreadyAttached = false;
425: EmailAttachment[] emailAttachments = emailAction
426: .getAttachments();
427: for (int i = 0; (i < emailAttachments.length)
428: && !alreadyAttached; i++) {
429: ActionInput actionInput = emailAttachments[i]
430: .getContentParam();
431: alreadyAttached = (actionInput != null)
432: && actionInput.getName().equals(
433: actionVariable.getVariableName());
434: }
435: if (!alreadyAttached) {
436: emailAction
437: .addAttachment((IActionInputVariable) data);
438: refresh();
439: }
440: }
441: } else if (data instanceof ActionSequenceResource) {
442: ActionSequenceResource actionSequenceResource = (ActionSequenceResource) data;
443: boolean alreadyAttached = false;
444: EmailAttachment[] emailAttachments = emailAction
445: .getAttachments();
446: for (int i = 0; (i < emailAttachments.length)
447: && !alreadyAttached; i++) {
448: ActionResource actionResource = emailAttachments[i]
449: .getContentResource();
450: if (actionResource != null) {
451: alreadyAttached = actionSequenceResource.getUri()
452: .equals(actionResource.getUri());
453: }
454: }
455: if (!alreadyAttached) {
456: emailAction.addAttachment(actionSequenceResource
457: .getName(), actionSequenceResource.getUri(),
458: actionSequenceResource.getMimeType());
459: refresh();
460: }
461: }
462: return result;
463: }
464:
465: /**
466: * Adds a new input/ouput to the action definition.
467: * @param variableName the input/output name.
468: */
469: protected EmailAttachment addAttachment(
470: IActionInputVariable actionVariable) {
471: boolean alreadyAttached = false;
472: EmailAttachment[] emailAttachments = emailAction
473: .getAttachments();
474: EmailAttachment newAttachment = null;
475: for (int i = 0; (i < emailAttachments.length)
476: && !alreadyAttached; i++) {
477: ActionInput actionInput = emailAttachments[i]
478: .getContentParam();
479: alreadyAttached = (actionInput != null)
480: && actionInput.getName().equals(
481: actionVariable.getVariableName());
482: }
483: if (!alreadyAttached) {
484: newAttachment = emailAction.addAttachment(actionVariable);
485: refresh();
486: }
487: return newAttachment;
488: }
489:
490: protected EmailAttachment addAttachment(String attachmentName,
491: URI uri, String mimeType) {
492: boolean alreadyAttached = false;
493: EmailAttachment[] emailAttachments = emailAction
494: .getAttachments();
495: EmailAttachment newEmailAttachment = null;
496: for (int i = 0; (i < emailAttachments.length)
497: && !alreadyAttached; i++) {
498: ActionResource actionResource = emailAttachments[i]
499: .getContentResource();
500: if (actionResource != null) {
501: alreadyAttached = uri.equals(actionResource.getUri());
502: }
503: }
504: if (!alreadyAttached) {
505: newEmailAttachment = emailAction.addAttachment(
506: attachmentName, uri, mimeType);
507: refresh();
508: }
509: return newEmailAttachment;
510: }
511:
512: /**
513: * Creates the table columns for this table viewer.
514: */
515: protected void createTableColumns() {
516: Table table = getTable();
517: TableColumn tableColumn = new TableColumn(table, SWT.LEFT);
518: tableColumn.setText(NAME_COLUMN_HDR);
519: tableColumn.pack();
520: tableColumn = new TableColumn(table, SWT.LEFT);
521: tableColumn.setText(ATTACH_COLUMN_HDR);
522: tableColumn.pack();
523: }
524:
525: private void updatePopupMenuState(IMenuManager menuMgr) {
526: deleteAttachmentAction.setEnabled(true);
527: }
528:
529: /* (non-Javadoc)
530: * @see org.eclipse.jface.viewers.Viewer#inputChanged(java.lang.Object, java.lang.Object)
531: */
532: protected void inputChanged(Object input, Object oldInput) {
533: emailAction = (EmailAction) input;
534:
535: super .inputChanged(input, oldInput);
536:
537: TableColumn[] columns = getTable().getColumns();
538: for (int i = 0; i < columns.length; i++) {
539: columns[i].pack();
540: }
541:
542: attachmentNameCellEditor.setItems(new String[0]);
543: ArrayList items = new ArrayList();
544: if (emailAction != null) {
545: IActionInputVariable[] availInputs = emailAction
546: .getAvailInputVariables(ActionSequenceDocument.STRING_TYPE);
547: for (int i = 0; i < availInputs.length; i++) {
548: items.add(ActionUtil
549: .parameterToDisplayText(availInputs[i]
550: .getVariableName()));
551: }
552: items.add(0, ""); //$NON-NLS-1$
553: }
554: attachmentNameCellEditor.setItems((String[]) items
555: .toArray(new String[0]));
556: }
557: }
|