| //------------------------------------------------------------------------------ |
| // Copyright (c) 2005, 2006 IBM Corporation and others. |
| // All rights reserved. This program and the accompanying materials |
| // are made available under the terms of the Eclipse Public License v1.0 |
| // which accompanies this distribution, and is available at |
| // http://www.eclipse.org/legal/epl-v10.html |
| // |
| // Contributors: |
| // IBM Corporation - initial implementation |
| //------------------------------------------------------------------------------ |
| package org.eclipse.epf.authoring.ui.dialogs; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| import org.eclipse.epf.uma.MethodElement; |
| import org.eclipse.jface.dialogs.Dialog; |
| import org.eclipse.jface.dialogs.IDialogConstants; |
| import org.eclipse.jface.viewers.ArrayContentProvider; |
| import org.eclipse.jface.viewers.ComboViewer; |
| import org.eclipse.jface.viewers.DoubleClickEvent; |
| import org.eclipse.jface.viewers.IDoubleClickListener; |
| import org.eclipse.jface.viewers.ILabelProvider; |
| import org.eclipse.jface.viewers.ISelectionChangedListener; |
| import org.eclipse.jface.viewers.IStructuredContentProvider; |
| import org.eclipse.jface.viewers.IStructuredSelection; |
| import org.eclipse.jface.viewers.SelectionChangedEvent; |
| import org.eclipse.jface.viewers.StructuredSelection; |
| import org.eclipse.jface.viewers.StructuredViewer; |
| import org.eclipse.jface.viewers.TableViewer; |
| import org.eclipse.jface.viewers.TreeViewer; |
| import org.eclipse.jface.viewers.Viewer; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.events.ModifyEvent; |
| import org.eclipse.swt.events.ModifyListener; |
| import org.eclipse.swt.layout.GridData; |
| import org.eclipse.swt.layout.GridLayout; |
| import org.eclipse.swt.widgets.Button; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.swt.widgets.Control; |
| import org.eclipse.swt.widgets.Label; |
| import org.eclipse.swt.widgets.Shell; |
| import org.eclipse.swt.widgets.Text; |
| |
| /** |
| * This dialog acts as a input dialog during command execution |
| * based on user input and inputType dialog behaviour changes. |
| * @author skannoor |
| * |
| */ |
| public class UserInteractionDialog extends Dialog implements |
| ISelectionChangedListener, IDoubleClickListener { |
| |
| ILabelProvider labelProvider; |
| IStructuredContentProvider contentProvider; |
| List objectsToSelect; |
| List initialSelection; |
| private String title; |
| private String msg; |
| List selected; |
| private boolean multiple = false; |
| private StructuredViewer viewer; |
| private Object input; |
| private int inputType = InputType.TEXT; |
| private Text text; |
| private String inputLabel; |
| |
| /** |
| * Constructor |
| * @param parentShell |
| * @param objectsToSelect |
| * @param labelProvider |
| * @param multiple |
| * @param title |
| * @param msg |
| */ |
| public UserInteractionDialog(Shell parentShell, List objectsToSelect, |
| ILabelProvider labelProvider, boolean multiple, |
| String title, String msg){ |
| super(parentShell); |
| this.labelProvider = labelProvider; |
| this.objectsToSelect = objectsToSelect; |
| this.title = title; |
| this.msg = msg; |
| this.multiple = multiple; |
| } |
| |
| /** |
| * Constructor |
| * @param parentShell |
| * @param objectsToSelect |
| */ |
| public UserInteractionDialog(Shell parentShell, List objectsToSelect){ |
| super(parentShell); |
| this.objectsToSelect = objectsToSelect; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent) |
| */ |
| public void selectionChanged(SelectionChangedEvent event) { |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.jface.viewers.IDoubleClickListener#doubleClick(org.eclipse.jface.viewers.DoubleClickEvent) |
| */ |
| public void doubleClick(DoubleClickEvent event) { |
| // TODO Auto-generated method stub |
| IStructuredSelection s = (IStructuredSelection) event.getSelection(); |
| Object element = s.getFirstElement(); |
| if(selected != null){ |
| selected.add(element); |
| } |
| close(); |
| } |
| |
| /** |
| * Saves the selection in inputdialog |
| */ |
| public void saveSelection(){ |
| if(viewer != null){ |
| IStructuredSelection selection = (IStructuredSelection) viewer |
| .getSelection(); |
| if (selection.size() > 0) { |
| Object[] objectArr = selection.toArray(); |
| for (int i = 0; i < objectArr.length; i++) { |
| Object obj = objectArr[i]; |
| //if(obj instanceof MethodElement){ |
| if(selected != null){ |
| selected.add(obj); |
| } |
| //} |
| } |
| } |
| }else{ |
| selected.add(text.getText()); |
| } |
| // Persist the dialog settings. |
| // saveLastSettings(); |
| //getShell().get |
| } |
| /** |
| * |
| * @param initialSelection |
| */ |
| public void setInitialSelection(List initialSelection){ |
| this.initialSelection = initialSelection; |
| } |
| /** |
| * |
| * @return |
| */ |
| public List getResult(){ |
| return this.selected; |
| } |
| /** |
| * |
| * @param labelProvider |
| */ |
| public void setLabelProvider(ILabelProvider labelProvider){ |
| this.labelProvider = labelProvider; |
| } |
| /** |
| * |
| * @param title |
| */ |
| public void setTitle(String title){ |
| this.title = title; |
| } |
| /** |
| * |
| * @param msg |
| */ |
| public void setMessage(String msg){ |
| this.msg = msg; |
| } |
| |
| /** |
| * |
| * @param multiple |
| */ |
| public void setIsMultiple(boolean multiple){ |
| this.multiple = multiple; |
| } |
| /** |
| * |
| */ |
| protected void createTreeViewer(Composite parent){ |
| if (!multiple) { |
| viewer = new TreeViewer(parent, SWT.SINGLE | SWT.H_SCROLL |
| | SWT.V_SCROLL | SWT.BORDER); |
| } else { |
| viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL |
| | SWT.V_SCROLL | SWT.BORDER); |
| } |
| |
| GridData spec = new GridData(GridData.FILL_BOTH); |
| { |
| spec.widthHint = 200; |
| spec.heightHint = 200; |
| spec.horizontalSpan = 3; |
| viewer.getControl().setLayoutData(spec); |
| } |
| } |
| |
| protected void createTableViewer(Composite parent){ |
| viewer = new TableViewer(parent); |
| } |
| |
| protected void createText(Composite parent){ |
| text = new Text(parent, SWT.SINGLE | SWT.BORDER); |
| text.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
| | GridData.HORIZONTAL_ALIGN_FILL)); |
| text.addModifyListener(new ModifyListener() { |
| public void modifyText(ModifyEvent e) { |
| // TODO: check inpute |
| } |
| }); |
| } |
| |
| protected void setSelection(Viewer viewer) { |
| if (initialSelection != null && initialSelection.size() > 0) { |
| StructuredSelection selection = new StructuredSelection( |
| initialSelection.toArray()); |
| viewer.setSelection(selection, true); |
| } |
| |
| } |
| |
| /* |
| * |
| */ |
| protected Label createMessageArea(Composite composite) { |
| Label label = new Label(composite,SWT.WRAP); |
| if(msg != null){ |
| label.setText(msg); |
| } |
| GridData gd= new GridData(GridData.FILL_HORIZONTAL); |
| label.setLayoutData(gd); |
| applyDialogFont(label); |
| return label; |
| } |
| |
| protected void createComboViewer(Composite composite) { |
| // TODO Auto-generated method stub |
| viewer = new ComboViewer(composite); |
| } |
| |
| protected Control createDialogArea(Composite parent) { |
| // TODO Auto-generated method stub |
| Composite composite = (Composite) super.createDialogArea(parent); |
| composite.setFont(parent.getFont()); |
| |
| GridLayout layout = (GridLayout) composite.getLayout(); |
| layout.marginWidth = 10; |
| layout.marginHeight = 10; |
| GridData gD = (GridData) composite.getLayoutData(); |
| layout.numColumns = 2; |
| //gD.grabExcessHorizontalSpace = true; |
| gD.widthHint = 150; |
| gD.heightHint = 150; |
| |
| createMessageArea(composite); |
| Label emptylabel = new Label(composite, SWT.NONE); |
| emptylabel.setText(""); |
| |
| if(inputLabel != null || inputLabel != ""){ |
| Label label = new Label(composite, SWT.WRAP); |
| label.setText(inputLabel); |
| GridData data = new GridData(GridData.FILL_BOTH |GridData.FILL_HORIZONTAL); |
| label.setLayoutData(data); |
| } |
| |
| if(inputType == InputType.TREE){ |
| createTreeViewer(composite); |
| }else if(inputType == InputType.LIST){ |
| createTableViewer(composite); |
| }else if (inputType == InputType.COMBO){ |
| createComboViewer(composite); |
| }else if(inputType == InputType.TEXT){ |
| createText(composite); |
| } |
| // |
| if(viewer != null){ |
| viewer.setLabelProvider(labelProvider); |
| if(contentProvider != null){ |
| viewer.setContentProvider(contentProvider); |
| }else{ |
| viewer.setContentProvider(new ArrayContentProvider()); |
| } |
| viewer.setUseHashlookup(true); |
| |
| if (input != null) { |
| viewer.setInput(input); |
| }else{ |
| viewer.setInput(objectsToSelect); |
| } |
| viewer.addSelectionChangedListener(this); |
| viewer.addDoubleClickListener(this); |
| viewer.getControl().setFont(parent.getFont()); |
| setSelection(viewer); |
| // TODO: treeViewer Sorter and Expand/Collapse |
| } |
| return composite; |
| |
| } |
| |
| /* |
| * Update OK button based on selection |
| * |
| */ |
| protected void updateOkButton() { |
| Button okButton = getButton(IDialogConstants.OK_ID); |
| if (okButton != null) { |
| okButton.setEnabled(getResult().size() > 0); |
| } |
| } |
| /* |
| * (non-Javadoc) |
| * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell) |
| */ |
| protected void configureShell(Shell newShell) { |
| // TODO Auto-generated method stub |
| super.configureShell(newShell); |
| if (this.title != null) { |
| newShell.setText(this.title); |
| } |
| } |
| /* |
| * (non-Javadoc) |
| * @see org.eclipse.jface.dialogs.Dialog#okPressed() |
| */ |
| protected void okPressed() { |
| selected = new ArrayList(); |
| saveSelection(); |
| super.okPressed(); |
| } |
| /* |
| * (non-Javadoc) |
| * @see org.eclipse.jface.dialogs.Dialog#cancelPressed() |
| */ |
| protected void cancelPressed() { |
| selected = null; |
| super.cancelPressed(); |
| } |
| |
| |
| |
| public void setInputType(int inputType) { |
| this.inputType = inputType; |
| } |
| |
| /** |
| * @return the input |
| */ |
| public Object getInput() { |
| return input; |
| } |
| |
| /** |
| * @param input the input to set |
| */ |
| public void setInput(Object input) { |
| this.input = input; |
| } |
| |
| /** |
| * @param inputLabel the inputLabel to set |
| */ |
| public void setInputLabel(String inputLabel) { |
| this.inputLabel = inputLabel; |
| } |
| |
| } |