Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2004, 2007 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 API and implementation Brock Janiczak (brockj@tpg.com.au) - Bug 145736 /
 
 
 package org.eclipse.ant.internal.ui.antsupport.inputhandler;
 
 
 import  org.apache.tools.ant.BuildException;
 import  org.apache.tools.ant.input.DefaultInputHandler;
 import  org.apache.tools.ant.input.InputRequest;
 import  org.apache.tools.ant.input.MultipleChoiceInputRequest;
 import  org.eclipse.swt.SWT;
 import  org.eclipse.swt.events.ModifyEvent;
 import  org.eclipse.swt.events.ModifyListener;
 import  org.eclipse.swt.events.SelectionAdapter;
 import  org.eclipse.swt.events.SelectionEvent;
 import  org.eclipse.swt.graphics.FontMetrics;
 import  org.eclipse.swt.graphics.GC;
 import  org.eclipse.swt.graphics.Point;
 import  org.eclipse.swt.layout.GridData;
 import  org.eclipse.swt.layout.GridLayout;
 import  org.eclipse.swt.widgets.Button;
 import  org.eclipse.swt.widgets.Combo;
 import  org.eclipse.swt.widgets.Composite;
 import  org.eclipse.swt.widgets.Control;
 import  org.eclipse.swt.widgets.Display;
 import  org.eclipse.swt.widgets.Event;
 import  org.eclipse.swt.widgets.Label;
 import  org.eclipse.swt.widgets.Listener;
 import  org.eclipse.swt.widgets.Shell;
 import  org.eclipse.swt.widgets.Text;
 
 public class SWTInputHandler extends DefaultInputHandler {
 	
 	private Text fText;
 	private Combo fCombo;
 	private Text fErrorMessageText;
 	private Button fOkButton;
 	private Shell fDialog;
 	private FontMetrics fFontMetrics;
 	protected InputRequest fRequest;
     private boolean fFirstValidationtrue;
 	
 	/* (non-Javadoc)
 	 * @see org.apache.tools.ant.input.InputHandler#handleInput(org.apache.tools.ant.input.InputRequest)
 	 */
 	public void handleInput(InputRequest requestthrows BuildException {
 		if (System.getProperty("eclipse.ant.noInput") != null) { //$NON-NLS-1$
 			throw new BuildException(RemoteAntMessages.getString("SWTInputHandler.0")); //$NON-NLS-1$
 		}
 		request;
 		BuildException[] problemnew BuildException[1];
 		Runnable runnablegetHandleInputRunnable(problem);
 		Display.getDefault().syncExec(runnable);
 		if (problem[0] != null) {
 			throw problem[0];
 		}
 	}
 	
 	protected Runnable getHandleInputRunnable(final BuildException[] problem) {
 		return new Runnable() {
 			public void run() {
 				String prompt;
 				if ( instanceof MultipleChoiceInputRequest) {
 					prompt = .getPrompt();
 				} else {
 					prompt = getPrompt();
 				}
 		       	String title= RemoteAntMessages.getString("SWTInputHandler.1"); //$NON-NLS-1$
 		       	boolean[] result = new boolean[1];
 				open(titlepromptresult);
 		
 				if (!result[0]) {
 					problem[0]= new BuildException(RemoteAntMessages.getString("SWTInputHandler.2")); //$NON-NLS-1$
 				}
 			}
 		};
 	}
 	
 	protected void open(String titleString promptboolean[] result) {
 		createDialog(titlepromptresult);
 		.open();
 
 		while (!.isDisposed()) {
 			if (!.getDisplay().readAndDispatch()) .getDisplay().sleep();
 		}
 		Display.getDefault().dispose();
	}
    private void createDialog(String titleString promptboolean[] result) {
        Display display= Display.getDefault();
		 = new Shell(display, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.RESIZE);
		.setLayout(new GridLayout());
		GridData gdnew GridData(SWT.FILL);
		gd.horizontalSpan= 2;
		.setLayoutData(gd);
		.setText(title);
		Label labelnew Label(, SWT.WRAP);
		label.setText(prompt);
		GridData data = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_CENTER);
        
        data.widthHint = convertHorizontalDLUsToPixels(300); //from IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH
        label.setLayoutData(data);
        label.setFont(.getFont());
        
        if ( instanceof MultipleChoiceInputRequest) {
        	 = new Combo(, SWT.BORDER | SWT.READ_ONLY);
        	.add(""); //$NON-NLS-1$
        	for (Iterator i = ((MultipleChoiceInputRequest)).getChoices().iterator();i.hasNext();) {
        		.add((String)i.next());
        		.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL| GridData.HORIZONTAL_ALIGN_FILL));
        		.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent e) {
					}
				});
        	}
        } else {
	         = new Text(, SWT.SINGLE | SWT.BORDER);
	        .setLayoutData(new GridData(GridData.GRAB_HORIZONTAL| GridData.HORIZONTAL_ALIGN_FILL));
	        .addModifyListener(new ModifyListener() {
	            public void modifyText(ModifyEvent e) {
	                validateInput();
	            }
	        });
        }
    	String value = null;
		try {
			.getClass().getMethod("getDefaultValue"new Class[0]); //$NON-NLS-1$
			value = .getDefaultValue();
catch (SecurityException e) {
catch (NoSuchMethodException e) {
			//pre Ant 1.7.0
		}
		 = new Text(, SWT.READ_ONLY);
        .setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
        .setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
        
		if (value != null) {
			if ( != null) {
				.select(.indexOf(value));
else {
				.setText(value);
				.selectAll();
			}
		}
		.pack();
    }
    protected void setErrorMessage(String errorMessage) {
        .setText(errorMessage == null ? "" : errorMessage); //$NON-NLS-1$
        .setEnabled(errorMessage == null);
        .getParent().update();
    }
    
    protected void validateInput() {
        String errorMessage = null;
        if ( instanceof MultipleChoiceInputRequest) {
        	.setInput(.getText());
        } else {
        	.setInput(.getText());
        }
        if (!.isInputValid()) {
            if () {
                errorMessage""//$NON-NLS-1$
                false;
            } else {
                errorMessage= RemoteAntMessages.getString("SWTInputHandler.3"); //$NON-NLS-1$
            } 
       }
       
        setErrorMessage(errorMessage); 
    }
    
    protected Control createButtonBar(Composite parentboolean[] result) {
        Composite composite = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout();
        layout.numColumns = 2; 
        layout.makeColumnsEqualWidth = true;
        composite.setLayout(layout);
        GridData data = new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.VERTICAL_ALIGN_CENTER);
        composite.setLayoutData(data);
        composite.setFont(parent.getFont());
       
        createButtonsForButtonBar(compositeresult);
        return composite;
    }
    
    protected void createButtonsForButtonBar(Composite parentfinal boolean[] result) {
    	 = new Button(parent, SWT.PUSH);
		.setText(RemoteAntMessages.getString("SWTInputHandler.4")); //$NON-NLS-1$
		Button cancel = new Button(parent, SWT.PUSH);
		cancel.setText(RemoteAntMessages.getString("SWTInputHandler.5")); //$NON-NLS-1$
		Listener listener = new Listener() {
			public void handleEvent(Event event) {
				result[0] = event.widget == ;
				.close();
			}
		};
		.addListener(SWT.Selection, listener);
		.setDefaultButton();
		cancel.addListener(SWT.Selection, listener);
        //do this here because setting the text will set enablement on the ok button
		if ( instanceof MultipleChoiceInputRequest) {
			.setFocus();
else {
			.setFocus();
		}
    }
    
    private void setButtonLayoutData(Button button) {
		GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		int widthHint = convertHorizontalDLUsToPixels(61); //from IDialogConstants.BUTTON_WIDTH
		Point minSize = button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
		data.widthHint = Math.max(widthHintminSize.x);
		button.setLayoutData(data);
	}
	private int convertHorizontalDLUsToPixels(int dlus) {
        // round to the nearest pixel
        return (.getAverageCharWidth() * dlus + 4 / 2) / 4;
	}
	protected void initializeDialogUnits(Control control) {
        // Compute and store a font metric
        GC gc = new GC(control);
        gc.setFont(control.getFont());
         = gc.getFontMetrics();
        gc.dispose();
    }
New to GrepCode? Check out our FAQ X