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.ArrayList;
016: import java.util.Arrays;
017: import java.util.List;
018:
019: import org.eclipse.jface.action.Action;
020: import org.eclipse.jface.action.IMenuListener;
021: import org.eclipse.jface.action.IMenuManager;
022: import org.eclipse.jface.action.MenuManager;
023: import org.eclipse.jface.viewers.CellEditor;
024: import org.eclipse.jface.viewers.ICellModifier;
025: import org.eclipse.jface.viewers.IStructuredContentProvider;
026: import org.eclipse.jface.viewers.IStructuredSelection;
027: import org.eclipse.jface.viewers.LabelProvider;
028: import org.eclipse.jface.viewers.TableViewer;
029: import org.eclipse.jface.viewers.TextCellEditor;
030: import org.eclipse.jface.viewers.Viewer;
031: import org.eclipse.swt.SWT;
032: import org.eclipse.swt.events.ControlEvent;
033: import org.eclipse.swt.events.ControlListener;
034: import org.eclipse.swt.widgets.Menu;
035: import org.eclipse.swt.widgets.Table;
036: import org.eclipse.swt.widgets.TableColumn;
037: import org.eclipse.swt.widgets.TableItem;
038: import org.pentaho.actionsequence.dom.ActionInputConstant;
039: import org.pentaho.actionsequence.dom.IActionInputValueProvider;
040: import org.pentaho.actionsequence.dom.actions.PrintMapValsAction;
041:
042: /**
043: * Table viewer used for viewing and modifying the inputs and outputs of an action
044: * definition element.
045: *
046: * @author Angelo Rodriguez
047: */
048: public class PrintMapKeysList extends TableViewer {
049:
050: static final String COLUMN_HDR_0 = "Keys";
051:
052: static final String COLUMN_PROP_0 = "Keys"; //$NON-NLS-1$
053:
054: public class ContentProvider implements IStructuredContentProvider {
055:
056: public Object[] getElements(Object ioElement) {
057: return printMapValsAction.getKeys();
058: }
059:
060: public void dispose() {
061: }
062:
063: public void inputChanged(Viewer viewer, Object oldInput,
064: Object newInput) {
065: printMapValsAction = (PrintMapValsAction) newInput;
066: }
067: }
068:
069: class CellModifier implements ICellModifier {
070:
071: public boolean canModify(Object element, String property) {
072: return true;
073: }
074:
075: public Object getValue(Object element, String property) {
076: return ((ActionInputConstant) element).getStringValue();
077: }
078:
079: public void modify(Object element, String property, Object value) {
080: if (value.toString().trim().length() > 0) {
081: TableItem tableItem = (TableItem) element;
082: String origValue = tableItem.getText();
083: IActionInputValueProvider[] origKeys = printMapValsAction
084: .getKeys();
085: ActionInputConstant[] newKeys = new ActionInputConstant[origKeys.length];
086: for (int i = 0; i < origKeys.length; i++) {
087: ActionInputConstant actionInputConstant = (ActionInputConstant) origKeys[i];
088: if (actionInputConstant.getStringValue().equals(
089: origValue)) {
090: newKeys[i] = new ActionInputConstant(value
091: .toString());
092: tableItem.setText(value.toString());
093: } else {
094: newKeys[i] = (ActionInputConstant) origKeys[i];
095: }
096: }
097: printMapValsAction.setKeys(newKeys);
098: }
099: }
100: }
101:
102: protected class DeleteInputAction extends Action {
103: public DeleteInputAction() {
104: super ("Delete"); //$NON-NLS-1$
105: }
106:
107: public void run() {
108: removeSelectedKey();
109: }
110: }
111:
112: protected class AddInputAction extends Action {
113: public AddInputAction() {
114: super ("Add"); //$NON-NLS-1$
115: }
116:
117: public void run() {
118: addKey();
119: }
120: }
121:
122: PrintMapValsAction printMapValsAction;
123: protected DeleteInputAction deleteIOAction = new DeleteInputAction();
124: protected AddInputAction addIOAction = new AddInputAction();
125: private ContentProvider contentProvider;
126:
127: /**
128: * Creates an viewer
129: * @param parent the parent of this viewer.
130: * @param toolkit the form toolkit.
131: */
132: public PrintMapKeysList(Table table) {
133: super (table);
134:
135: table.setHeaderVisible(true);
136: createColumns();
137: contentProvider = new ContentProvider();
138: setContentProvider(contentProvider);
139: setLabelProvider(new LabelProvider() {
140: public String getText(Object element) {
141: String text = ""; //$NON-NLS-1$
142: if (element instanceof ActionInputConstant) {
143: ActionInputConstant actionInputConstant = (ActionInputConstant) element;
144: text = actionInputConstant.getStringValue();
145: }
146: return text;
147: }
148: });
149: setCellModifier(new CellModifier());
150: table.addControlListener(new ControlListener() {
151:
152: public void controlMoved(ControlEvent e) {
153: }
154:
155: public void controlResized(ControlEvent e) {
156: getTable().getColumn(0)
157: .setWidth(getTable().getSize().x);
158: }
159:
160: });
161: createPopupMenu();
162: }
163:
164: /**
165: * Creates the popup menu used by this viewer.
166: */
167: protected void createPopupMenu() {
168: MenuManager popupMenu = new MenuManager();
169:
170: popupMenu.add(addIOAction);
171: popupMenu.add(deleteIOAction);
172: popupMenu.addMenuListener(new IMenuListener() {
173: public void menuAboutToShow(IMenuManager manager) {
174: updatePopupMenuState(manager);
175: }
176: });
177:
178: Table table = getTable();
179: Menu menu = popupMenu.createContextMenu(table);
180: table.setMenu(menu);
181: }
182:
183: protected void updatePopupMenuState(IMenuManager menuMgr) {
184: IStructuredSelection selection = (IStructuredSelection) getSelection();
185: deleteIOAction.setEnabled(selection.size() > 0);
186: }
187:
188: /* (non-Javadoc)
189: * @see org.eclipse.jface.viewers.Viewer#inputChanged(java.lang.Object, java.lang.Object)
190: */
191: protected void inputChanged(Object input, Object oldInput) {
192: printMapValsAction = (PrintMapValsAction) input;
193: super .inputChanged(input, oldInput);
194: }
195:
196: /**
197: * Removes the selected inputs/ouputs from the action definition.
198: */
199: protected void removeSelectedKey() {
200: ArrayList keys = new ArrayList(Arrays.asList(printMapValsAction
201: .getKeys()));
202: List selectedKeys = ((IStructuredSelection) getSelection())
203: .toList();
204: boolean bool = keys.contains(selectedKeys.get(0));
205: keys
206: .removeAll(((IStructuredSelection) getSelection())
207: .toList());
208: printMapValsAction.setKeys((ActionInputConstant[]) keys
209: .toArray(new ActionInputConstant[0]));
210: refresh();
211: }
212:
213: protected void addKey() {
214: ArrayList keys = new ArrayList(Arrays.asList(printMapValsAction
215: .getKeys()));
216: keys.add(new ActionInputConstant(getUniqueKeyName()));
217: printMapValsAction.setKeys((ActionInputConstant[]) keys
218: .toArray(new ActionInputConstant[0]));
219: refresh();
220: }
221:
222: private String getUniqueKeyName() {
223: ArrayList existingKeys = new ArrayList();
224: IActionInputValueProvider[] actionInputConstants = printMapValsAction
225: .getKeys();
226: for (int i = 0; i < actionInputConstants.length; i++) {
227: existingKeys.add(actionInputConstants[i].getStringValue());
228: }
229: int index = 1;
230: while (existingKeys.contains("key" + index)) {
231: index++;
232: }
233: return "key" + index;
234: }
235:
236: private void createColumns() {
237: Table table = getTable();
238: TableColumn tableColumn = new TableColumn(table, SWT.LEFT);
239: tableColumn.setText(COLUMN_HDR_0);
240: tableColumn.setWidth(getTable().getSize().x);
241: tableColumn.setResizable(false);
242: setCellEditors(new CellEditor[] { new TextCellEditor(table) });
243:
244: setColumnProperties(new String[] { COLUMN_PROP_0 });
245:
246: }
247: }
|