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.util.Iterator;
016: import java.util.List;
017:
018: import org.eclipse.jface.action.Action;
019: import org.eclipse.jface.action.IMenuListener;
020: import org.eclipse.jface.action.IMenuManager;
021: import org.eclipse.jface.action.MenuManager;
022: import org.eclipse.jface.viewers.IContentProvider;
023: import org.eclipse.jface.viewers.IStructuredSelection;
024: import org.eclipse.jface.viewers.TableViewer;
025: import org.eclipse.swt.SWT;
026: import org.eclipse.swt.widgets.Composite;
027: import org.eclipse.swt.widgets.Menu;
028: import org.eclipse.swt.widgets.Table;
029: import org.eclipse.swt.widgets.TableColumn;
030: import org.pentaho.actionsequence.dom.AbstractActionIOElement;
031: import org.pentaho.actionsequence.dom.actions.ActionDefinition;
032: import org.pentaho.designstudio.messages.Messages;
033:
034: /**
035: * Abstract table viewer used for viewing and modifying the inputs and outputs of an action
036: * definition element.
037: *
038: * @author Angelo Rodriguez
039: */
040: public abstract class ActionIOTable extends TableViewer {
041:
042: class DeleteIOAction extends Action {
043: public DeleteIOAction() {
044: super (Messages.getString("ActionIOTable.DELETE_MENU_TITLE")); //$NON-NLS-1$
045: }
046:
047: public void run() {
048: removeSelectedIOElement();
049: }
050: }
051:
052: static final int NAME_COLUMN_IDX = 0;
053:
054: static final int TYPE_COLUMN_IDX = 1;
055:
056: static final String NAME_COLUMN_HDR = Messages
057: .getString("ActionIOTable.UI_NAME_HEADER"); //$NON-NLS-1$
058:
059: static final String TYPE_COLUMN_HDR = Messages
060: .getString("ActionIOTable.UI_TYPE_HEADER"); //$NON-NLS-1$
061:
062: static final String NAME_COLUMN_PROP = "NAME"; //$NON-NLS-1$
063:
064: static final String TYPE_COLUMN_PROP = "TYPE"; //$NON-NLS-1$
065:
066: ActionDefinition actionDefinition;
067:
068: DeleteIOAction deleteIOAction = new DeleteIOAction();
069:
070: /**
071: * Creates an viewer
072: * @param parent the parent of this viewer.
073: * @param toolkit the form toolkit.
074: */
075: public ActionIOTable(Composite parent) {
076: super (WidgetFactory.createTable(parent, SWT.FULL_SELECTION
077: | SWT.BORDER));
078:
079: Table table = getTable();
080: table.setHeaderVisible(true);
081: createTableColumns();
082: setContentProvider(createContentProvider());
083: createLabelProvider();
084: createCellEditors();
085: createPopupMenu();
086: }
087:
088: /**
089: * Initializes this viewer with a content provider.
090: * @see IStructuredContentProvider.
091: */
092: protected abstract IContentProvider createContentProvider();
093:
094: /**
095: * Initializes this viewer with a content provider.
096: * @see ITableLabelProvider.
097: */
098: protected abstract void createLabelProvider();
099:
100: /**
101: * Initializes this viewer with the appropriate cell editors.
102: */
103: protected abstract void createCellEditors();
104:
105: /**
106: * Creates the popup menu used by this viewer.
107: */
108: protected void createPopupMenu() {
109: MenuManager popupMenu = new MenuManager();
110: popupMenu.add(deleteIOAction);
111: popupMenu.addMenuListener(new IMenuListener() {
112: public void menuAboutToShow(IMenuManager manager) {
113: updatePopupMenuState(manager);
114: }
115: });
116:
117: Table table = getTable();
118: Menu menu = popupMenu.createContextMenu(table);
119: table.setMenu(menu);
120: }
121:
122: /**
123: * Removes the selected inputs/ouputs from the action definition.
124: */
125: protected void removeSelectedIOElement() {
126: IStructuredSelection selection = (IStructuredSelection) getSelection();
127: remove(selection.toArray());
128: List selections = selection.toList();
129: for (Iterator iter = selections.iterator(); iter.hasNext();) {
130: AbstractActionIOElement selectedElement = (AbstractActionIOElement) iter
131: .next();
132: selectedElement.delete();
133: remove(selectedElement);
134: }
135: }
136:
137: /**
138: * Creates the table columns for this table viewer.
139: */
140: protected void createTableColumns() {
141: Table table = getTable();
142: TableColumn tableColumn = new TableColumn(table, SWT.LEFT);
143: tableColumn.setText(NAME_COLUMN_HDR);
144: tableColumn.setWidth(200);
145: tableColumn = new TableColumn(table, SWT.LEFT);
146: tableColumn.setText(TYPE_COLUMN_HDR);
147: tableColumn.setWidth(200);
148: }
149:
150: private void updatePopupMenuState(IMenuManager menuMgr) {
151: deleteIOAction.setEnabled(true);
152: }
153:
154: /* (non-Javadoc)
155: * @see org.eclipse.jface.viewers.Viewer#inputChanged(java.lang.Object, java.lang.Object)
156: */
157: protected void inputChanged(Object input, Object oldInput) {
158: actionDefinition = (ActionDefinition) input;
159:
160: super.inputChanged(input, oldInput);
161: }
162: }
|