blob: ffaaef545a073adcc76d90f9c5cc6d48a4305721 [file] [log] [blame]
//------------------------------------------------------------------------------
// 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.Iterator;
import java.util.List;
import org.eclipse.epf.library.edit.command.UserInput;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
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.TableViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
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.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 Shashidhar Kannoori
*
*/
public class UserInputsDialog extends Dialog {
IStructuredContentProvider contentProvider;
ILabelProvider labelProvider;
private String title;
private String message;
private List userInputs;
private boolean result = false;
public UserInputsDialog(Shell parentShell, List userInputs,
String title, String message){
super(parentShell);
this.title = title;
this.message = message;
this.userInputs = userInputs;
}
/**
*
* @param title
*/
public void setTitle(String title){
this.title = title;
}
/**
*
* @param msg
*/
public void setMessage(String message){
this.message = message;
}
/**
*
*/
protected void createTreeViewer(Composite parent, UserInput userInput){
TreeViewer viewer;
if (!userInput.isMultiple()) {
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);
}
viewer.addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent event) {
// TODO Auto-generated method stub
}
});
if(viewer != null){
viewer.setLabelProvider(labelProvider);
if(contentProvider != null){
viewer.setContentProvider(contentProvider);
}else{
viewer.setContentProvider(new ArrayContentProvider());
}
viewer.setUseHashlookup(true);
viewer.setInput(userInput.getChoices());
viewer.getControl().setFont(parent.getFont());
// TODO: treeViewer Sorter and Expand/Collapse
}
}
protected void createTableViewer(Composite parent, UserInput userInput){
TableViewer viewer = new TableViewer(parent);
viewer.addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent event) {
// TODO Auto-generated method stub
}
});
}
protected void createText(Composite parent, UserInput userInput){
final UserInput localinput = userInput;
final Text 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) {
}
});
text.addFocusListener(new FocusAdapter() {
public void focusLost(FocusEvent e) {
localinput.setInput(text.getText());
}
});
}
protected void setInput(UserInput input, Object obj){
input.setInput(obj);
}
protected void createComboViewer(Composite composite, UserInput userInput) {
// TODO Auto-generated method stub
final UserInput localInput = userInput;
ComboViewer viewer = new ComboViewer(composite);
viewer.setContentProvider(new ArrayContentProvider());
viewer.setInput(userInput.getChoices());
viewer.addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent event) {
IStructuredSelection selection = (StructuredSelection)event.getSelection();
Object obj = selection.getFirstElement();
localInput.setInput(obj);
}
});
}
/*
*
*/
protected Label createMessageArea(Composite composite) {
Label label = new Label(composite,SWT.WRAP);
if(message != null){
label.setText(message);
}
GridData gd= new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = 2;
label.setLayoutData(gd);
applyDialogFont(label);
return label;
}
/*
* (non-Javadoc)
* @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.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.widthHint = 300;
gD.heightHint = 300;
createMessageArea(composite);
Label emptylabel = new Label(composite, SWT.NONE);
emptylabel.setText("");
GridData emptyData = new GridData(GridData.FILL_HORIZONTAL);
emptyData.horizontalSpan =2;
emptylabel.setLayoutData(emptyData);
// Create controls based on UserInputs
if(userInputs != null && userInputs.size() > 0){
for(Iterator iterator = userInputs.iterator(); iterator.hasNext();){
Object object = iterator.next();
if(object instanceof UserInput){
UserInput userInput = (UserInput)object;
//Composite pane = new Composite(composite, SWT.NONE);
//GridLayout paneLayout = (GridLayout) pane.getLayout();
//paneLayout.marginWidth = 10;
//paneLayout.marginHeight = 10;
//paneLayout.numColumns = 2;
// Create a Label
Label label = new Label(composite, SWT.NONE);
label.setText(userInput.getLabel());
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
label.setLayoutData(gridData);
if(userInput.getType() == UserInput.TEXT){
createText(composite, userInput );
}
else if(userInput.getType() == UserInput.SELECTION){
createComboViewer(composite, userInput);
}
//pane.addPaintListener(new Bord)
Label emptyLabel = new Label(composite, SWT.NONE);
emptyLabel.setText("");
emptyData = new GridData(GridData.FILL_HORIZONTAL);
emptyData.horizontalSpan =2;
emptyLabel.setLayoutData(emptyData);
}
}
}
return composite;
}
/*
* (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() {
result = true;
super.okPressed();
}
/*
* (non-Javadoc)
* @see org.eclipse.jface.dialogs.Dialog#cancelPressed()
*/
protected void cancelPressed() {
if(userInputs != null && userInputs.size() > 0){
for(Iterator iterator = userInputs.iterator(); iterator.hasNext();){
Object object = iterator.next();
if(object instanceof UserInput){
UserInput userInput = (UserInput)object;
userInput.setInput(null);
}
}
}
super.cancelPressed();
}
public boolean getResult(){
return result ;
}
}