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.editors.actionsequence.pages.parameters;
014:
015: import java.util.ArrayList;
016: import java.util.Arrays;
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.CellEditor;
023: import org.eclipse.jface.viewers.ICellModifier;
024: import org.eclipse.jface.viewers.IStructuredContentProvider;
025: import org.eclipse.jface.viewers.LabelProvider;
026: import org.eclipse.jface.viewers.TableViewer;
027: import org.eclipse.jface.viewers.TextCellEditor;
028: import org.eclipse.jface.viewers.Viewer;
029: import org.eclipse.swt.SWT;
030: import org.eclipse.swt.events.ControlEvent;
031: import org.eclipse.swt.events.ControlListener;
032: import org.eclipse.swt.widgets.Menu;
033: import org.eclipse.swt.widgets.Table;
034: import org.eclipse.swt.widgets.TableColumn;
035: import org.eclipse.swt.widgets.TableItem;
036: import org.pentaho.actionsequence.dom.ActionSequenceDocument;
037: import org.pentaho.actionsequence.dom.ActionSequenceInput;
038: import org.pentaho.designstudio.messages.Messages;
039:
040: /**
041: * Viewer used for managing input string lists within an action sequence.
042: * @author Angelo Rodriguez
043: */
044: public class StringListViewer extends TableViewer {
045:
046: static final String ADD_ITEM_PLACEHOLDER = Messages
047: .getString("StringListLabelProvider.UI_ADD_ITEM_LABEL"); //$NON-NLS-1$
048:
049: public class ContentProvider implements IStructuredContentProvider {
050:
051: static final String LIST_ITEM_PATH = ActionSequenceDocument.DEFAULT_VAL_NAME
052: + "/list-item"; //$NON-NLS-1$
053:
054: /* (non-Javadoc)
055: * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
056: */
057: public Object[] getElements(Object ioElement) {
058: ArrayList defStrings = new ArrayList(stringList);
059: defStrings.add(ADD_ITEM_PLACEHOLDER);
060: return defStrings.toArray();
061: }
062:
063: public void dispose() {
064: }
065:
066: public void inputChanged(Viewer viewer, Object oldInput,
067: Object newInput) {
068: }
069:
070: }
071:
072: class CellModifier implements ICellModifier {
073:
074: public boolean canModify(Object element, String property) {
075: return true;
076: }
077:
078: public Object getValue(Object element, String property) {
079: return element;
080: }
081:
082: public void modify(Object element, String property, Object value) {
083: TableItem tableItem = (TableItem) element;
084: int index = getTable().indexOf(tableItem);
085: if (index == getTable().getItemCount() - 1) {
086: if (!ADD_ITEM_PLACEHOLDER.equals(value)) {
087: stringList.add(value);
088: actionSequenceInput
089: .setDefaultValue((String[]) stringList
090: .toArray(new String[0]));
091: refresh();
092: }
093: } else {
094: stringList.set(index, value);
095: actionSequenceInput
096: .setDefaultValue((String[]) stringList
097: .toArray(new String[0]));
098: refresh();
099: }
100: }
101: }
102:
103: class DeleteRowAction extends Action {
104: public DeleteRowAction() {
105: super (Messages
106: .getString("StringListViewer.UI_DELETE_ITEM_MENU")); //$NON-NLS-1$
107: }
108:
109: public void run() {
110: removeSelectedRows();
111: }
112: }
113:
114: static final int NAME_COLUMN_IDX = 0;
115:
116: static final String COLUMN_HDR_0 = Messages
117: .getString("StringListViewer.UI_LIST_VALUES_COL_HEADER"); //$NON-NLS-1$
118:
119: static final String COLUMN_PROP_0 = "Values"; //$NON-NLS-1$
120:
121: ActionSequenceInput actionSequenceInput;
122: ArrayList stringList = new ArrayList();
123:
124: DeleteRowAction deleteRowAction = new DeleteRowAction();
125:
126: ContentProvider contentProvider;
127:
128: ArrayList tableColumns = new ArrayList();
129:
130: /**
131: * Creates a string list viewer.
132: * @param parent the parent of this viewer
133: * @param table the table to be managed by this viewer
134: */
135: public StringListViewer(Table table) {
136: super (table);
137:
138: table.setHeaderVisible(true);
139: table.setLinesVisible(true);
140: createColumns();
141: createContentProvider();
142: createLabelProvider();
143: setCellModifier(new CellModifier());
144: table.addControlListener(new ControlListener() {
145:
146: public void controlMoved(ControlEvent e) {
147: }
148:
149: public void controlResized(ControlEvent e) {
150: getTable().getColumn(0)
151: .setWidth(getTable().getSize().x);
152: }
153:
154: });
155: createPopupMenu();
156: }
157:
158: private void createColumns() {
159: Table table = getTable();
160: TableColumn tableColumn = new TableColumn(table, SWT.LEFT);
161: tableColumn.setText(COLUMN_HDR_0);
162: tableColumn.setWidth(getTable().getSize().x);
163: tableColumn.setResizable(false);
164: setCellEditors(new CellEditor[] { new TextCellEditor(table) });
165:
166: setColumnProperties(new String[] { COLUMN_PROP_0 });
167:
168: }
169:
170: private void createContentProvider() {
171: contentProvider = new ContentProvider();
172: setContentProvider(contentProvider);
173: }
174:
175: private void createLabelProvider() {
176: setLabelProvider(new LabelProvider());
177: }
178:
179: private void createPopupMenu() {
180: MenuManager popupMenu = new MenuManager();
181: popupMenu.add(deleteRowAction);
182: popupMenu.addMenuListener(new IMenuListener() {
183: public void menuAboutToShow(IMenuManager manager) {
184: updatePopupMenuState(manager);
185: }
186: });
187:
188: Table table = getTable();
189: Menu menu = popupMenu.createContextMenu(table);
190: table.setMenu(menu);
191: }
192:
193: private void removeSelectedRows() {
194: ArrayList modifiedElements = new ArrayList();
195: int[] selectedIndeces = getTable().getSelectionIndices();
196: for (int i = 0; i < stringList.size(); i++) {
197: boolean indexFound = false;
198: for (int j = 0; j < selectedIndeces.length && !indexFound; j++) {
199: indexFound = (i == selectedIndeces[j]);
200: }
201: if (!indexFound) {
202: modifiedElements.add(stringList.get(i));
203: }
204: }
205: stringList.clear();
206: stringList.addAll(modifiedElements);
207: actionSequenceInput.setDefaultValue((String[]) stringList
208: .toArray(new String[0]));
209: refresh();
210: }
211:
212: private void updatePopupMenuState(IMenuManager menuMgr) {
213: int[] selectedIndeces = getTable().getSelectionIndices();
214: int numElements = contentProvider.getElements(null).length;
215: boolean enabled = selectedIndeces.length > 0;
216: for (int i = 0; i < selectedIndeces.length && enabled; i++) {
217: enabled = (selectedIndeces[i] != (numElements - 1));
218: }
219: deleteRowAction.setEnabled(enabled);
220: }
221:
222: /* (non-Javadoc)
223: * @see org.eclipse.jface.viewers.Viewer#inputChanged(java.lang.Object, java.lang.Object)
224: */
225: protected void inputChanged(Object input, Object oldInput) {
226: actionSequenceInput = (ActionSequenceInput) input;
227: Object defaultValue = actionSequenceInput.getDefaultValue();
228: stringList.clear();
229: if (defaultValue instanceof String[]) {
230: stringList.addAll(Arrays.asList((String[]) defaultValue));
231: }
232: super.inputChanged(input, oldInput);
233: }
234: }
|