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:
018: import org.eclipse.swt.SWT;
019: import org.eclipse.swt.events.SelectionEvent;
020: import org.eclipse.swt.events.SelectionListener;
021: import org.eclipse.swt.graphics.Image;
022: import org.eclipse.swt.graphics.Point;
023: import org.eclipse.swt.graphics.Rectangle;
024: import org.eclipse.swt.widgets.Composite;
025: import org.eclipse.swt.widgets.Control;
026: import org.eclipse.swt.widgets.Menu;
027: import org.eclipse.swt.widgets.MenuItem;
028: import org.eclipse.swt.widgets.ToolBar;
029: import org.eclipse.swt.widgets.ToolItem;
030: import org.pentaho.actionsequence.dom.AbstractIOElement;
031: import org.pentaho.actionsequence.dom.ActionOutput;
032: import org.pentaho.actionsequence.dom.ActionSequenceDocument;
033: import org.pentaho.actionsequence.dom.ActionSequenceInput;
034: import org.pentaho.actionsequence.dom.IActionInputVariable;
035: import org.pentaho.designstudio.editors.actionsequence.ActionSequenceEditorPlugin;
036: import org.pentaho.designstudio.messages.Messages;
037:
038: public class TemplateMsgInputsTableToolbar {
039:
040: ToolBar toolBar;
041: TemplateMsgInputsTable actionInputsList;
042: ToolItem addToolItem;
043: ToolItem deleteToolItem;
044: Menu addInputMenu;
045:
046: static final Image ADD_INPUT_ICON = ActionSequenceEditorPlugin
047: .getImageDescriptor(
048: Messages
049: .getString("ActionsMasterDetailBlock.ICON_ADD_ACTION")).createImage(); //$NON-NLS-1$
050: static final Image REMOVE_INPUT_ICON = ActionSequenceEditorPlugin
051: .getImageDescriptor(
052: Messages
053: .getString("ActionsMasterDetailBlock.ICON_REMOVE_ACTION")).createImage(); //$NON-NLS-1$
054:
055: class AddInputMenuItem {
056: AbstractIOElement actionSequenceIO;
057: MenuItem menuItem;
058:
059: AddInputMenuItem(Menu parent, AbstractIOElement actSeqIO) {
060: menuItem = new MenuItem(parent, SWT.NONE);
061: actionSequenceIO = actSeqIO;
062: if (actionSequenceIO instanceof ActionOutput) {
063: menuItem.setText(((ActionOutput) actionSequenceIO)
064: .getPublicName());
065: } else {
066: menuItem.setText(actionSequenceIO.getName());
067: }
068: menuItem.addSelectionListener(new SelectionListener() {
069: public void widgetDefaultSelected(SelectionEvent e) {
070: }
071:
072: public void widgetSelected(SelectionEvent e) {
073: actionInputsList.addNewInput(actionSequenceIO
074: .getName());
075: }
076: });
077: }
078: }
079:
080: public TemplateMsgInputsTableToolbar(Composite parent,
081: TemplateMsgInputsTable inputsTable) {
082: actionInputsList = inputsTable;
083:
084: // ToolBar toolBar = new ToolBar(coolBar, SWT.NONE);
085: // toolkit.adapt(toolBar, false, false);
086: //
087: // CoolItem coolItem = new CoolItem(coolBar, SWT.NULL);
088: // coolItem.setControl(toolBar);
089: // Point pt = toolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT);
090: // pt = coolItem.computeSize(pt.x, pt.y);
091: // coolItem.setSize(pt);
092:
093: toolBar = new ToolBar(parent, SWT.FLAT);
094:
095: addToolItem = new ToolItem(toolBar, SWT.DROP_DOWN);
096: addToolItem.setImage(ADD_INPUT_ICON);
097: addToolItem.setToolTipText(Messages
098: .getString("ActionsMasterDetailBlock.ADD_NEW_INPUT")); //$NON-NLS-1$
099:
100: addToolItem.addSelectionListener(new SelectionListener() {
101: public void widgetDefaultSelected(SelectionEvent e) {
102: // TODO Auto-generated method stub
103:
104: }
105:
106: public void widgetSelected(SelectionEvent e) {
107: ToolItem item = (ToolItem) e.widget;
108: Rectangle rectangle = item.getBounds();
109: Point pt = item.getParent().toDisplay(
110: new Point(rectangle.x, rectangle.y));
111: addInputMenu.setLocation(pt.x, pt.y + rectangle.height);
112: addInputMenu.setVisible(true);
113: }
114:
115: });
116:
117: deleteToolItem = new ToolItem(toolBar, SWT.NULL);
118: deleteToolItem.setImage(REMOVE_INPUT_ICON);
119: deleteToolItem
120: .setToolTipText(Messages
121: .getString("ActionsMasterDetailBlock.REMOVE_SELECTED_INPUT")); //$NON-NLS-1$
122: deleteToolItem.addSelectionListener(new SelectionListener() {
123: public void widgetDefaultSelected(SelectionEvent e) {
124: }
125:
126: public void widgetSelected(SelectionEvent e) {
127: actionInputsList.removeSelectedInput();
128: }
129: });
130: }
131:
132: public void refresh() {
133: if (addInputMenu != null) {
134: addInputMenu.dispose();
135: }
136:
137: addInputMenu = new Menu(actionInputsList.getTable().getShell(),
138: SWT.POP_UP);
139: IActionInputVariable[] availInputs = actionInputsList.actionDefinition
140: .getAvailInputVariables(ActionSequenceDocument.STRING_TYPE);
141: ArrayList availInputNames = new ArrayList();
142: ArrayList expectedInputs = new ArrayList(Arrays
143: .asList(actionInputsList.actionDefinition
144: .getReservedInputNames()));
145: for (int i = 0; i < availInputs.length; i++) {
146: if (availInputs[i] instanceof ActionOutput) {
147: ActionOutput actionOutput = (ActionOutput) availInputs[i];
148: if (!expectedInputs.contains(actionOutput
149: .getPublicName())
150: && !availInputNames.contains(actionOutput
151: .getPublicName())) {
152: new AddInputMenuItem(addInputMenu, actionOutput);
153: availInputNames.add(actionOutput.getPublicName());
154: }
155: } else if (availInputs[i] instanceof ActionSequenceInput) {
156: ActionSequenceInput actionSequenceInput = (ActionSequenceInput) availInputs[i];
157: if (!expectedInputs.contains(actionSequenceInput
158: .getName())
159: && !availInputNames
160: .contains(actionSequenceInput.getName())) {
161: new AddInputMenuItem(addInputMenu,
162: actionSequenceInput);
163: availInputNames.add(actionSequenceInput.getName());
164: }
165: }
166: }
167: }
168:
169: public Control getControl() {
170: return toolBar;
171: }
172:
173: }
|