001: package org.pentaho.designstudio.controls;
002:
003: import java.util.Comparator;
004: import java.util.Iterator;
005: import java.util.TreeSet;
006:
007: import org.eclipse.swt.SWT;
008: import org.eclipse.swt.events.SelectionEvent;
009: import org.eclipse.swt.events.SelectionListener;
010: import org.eclipse.swt.graphics.Image;
011: import org.eclipse.swt.graphics.Point;
012: import org.eclipse.swt.graphics.Rectangle;
013: import org.eclipse.swt.widgets.Composite;
014: import org.eclipse.swt.widgets.Control;
015: import org.eclipse.swt.widgets.Menu;
016: import org.eclipse.swt.widgets.MenuItem;
017: import org.eclipse.swt.widgets.Text;
018: import org.eclipse.swt.widgets.ToolBar;
019: import org.eclipse.swt.widgets.ToolItem;
020: import org.pentaho.actionsequence.dom.ActionSequenceDocument;
021: import org.pentaho.actionsequence.dom.IActionInputVariable;
022: import org.pentaho.actionsequence.dom.actions.ActionDefinition;
023: import org.pentaho.designstudio.editors.actionsequence.ActionSequenceEditorPlugin;
024: import org.pentaho.designstudio.messages.Messages;
025:
026: public class AddActionInputToolbar implements SelectionListener {
027:
028: static final Image ADD_ICON = ActionSequenceEditorPlugin
029: .getImageDescriptor(
030: Messages
031: .getString("ActionsMasterDetailBlock.ICON_ADD_ACTION")).createImage(); //$NON-NLS-1$
032:
033: protected ToolBar toolBar;
034: protected Text textField;
035: Menu addMenu;
036: protected ActionDefinition actionDefinition;
037:
038: class InputNameComparator implements Comparator {
039:
040: public int compare(Object o1, Object o2) {
041: int result = o1.toString().toUpperCase().compareTo(
042: o2.toString().toUpperCase());
043: if (result == 0) {
044: result = o1.toString().compareTo(o2.toString());
045: }
046: return result;
047: }
048:
049: }
050:
051: public AddActionInputToolbar(Composite parent, Text textField,
052: Object layoutData) {
053: this .textField = textField;
054: toolBar = new ToolBar(parent, SWT.FLAT);
055:
056: ToolItem toolItem = new ToolItem(toolBar, SWT.DROP_DOWN);
057: toolItem.setImage(ADD_ICON);
058: toolItem.setToolTipText(Messages
059: .getString("AddActionInputToolbar.ADD_INPUT_TOOLTIP")); //$NON-NLS-1$
060:
061: addMenu = new Menu(toolBar.getShell());
062: toolItem.addSelectionListener(this );
063: if (layoutData != null) {
064: toolBar.setLayoutData(layoutData);
065: }
066: }
067:
068: public void setActionDefinition(ActionDefinition actionDefinition) {
069: this .actionDefinition = actionDefinition;
070: refresh();
071: }
072:
073: public ActionDefinition getActionDefinition() {
074: return actionDefinition;
075: }
076:
077: public void widgetDefaultSelected(SelectionEvent e) {
078: }
079:
080: public void widgetSelected(SelectionEvent e) {
081: if (e.detail == SWT.ARROW) {
082: ToolItem item = (ToolItem) e.widget;
083: Rectangle rectangle = item.getBounds();
084: Point pt = item.getParent().toDisplay(
085: new Point(rectangle.x, rectangle.y));
086: addMenu.setLocation(pt.x, pt.y + rectangle.height);
087: addMenu.setVisible(true);
088: } else if (e.getSource() instanceof MenuItem) {
089: MenuItem menuItem = (MenuItem) e.getSource();
090: textField.setText(menuItem.getText());
091: }
092: }
093:
094: public void refresh() {
095: MenuItem[] menuItems = addMenu.getItems();
096: for (int i = 0; i < menuItems.length; i++) {
097: menuItems[i].removeSelectionListener(this );
098: menuItems[i].dispose();
099: }
100: if (actionDefinition != null) {
101: TreeSet sortedSet = new TreeSet(new InputNameComparator());
102: IActionInputVariable[] availInputs = actionDefinition
103: .getAvailInputVariables(ActionSequenceDocument.STRING_TYPE);
104: for (int i = 0; i < availInputs.length; i++) {
105: sortedSet.add(ActionUtil
106: .parameterToDisplayText(availInputs[i]
107: .getVariableName()));
108: }
109: MenuItem menuItem = new MenuItem(addMenu, SWT.NONE);
110: menuItem.setText(""); //$NON-NLS-1$
111: menuItem.addSelectionListener(this );
112: for (Iterator iterator = sortedSet.iterator(); iterator
113: .hasNext();) {
114: menuItem = new MenuItem(addMenu, SWT.NONE);
115: menuItem.setText(iterator.next().toString());
116: menuItem.addSelectionListener(this );
117: }
118: }
119: }
120:
121: public Control getControl() {
122: return toolBar;
123: }
124: }
|