Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2000, 2008 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 Martin Karpisek (martin.karpisek@gmail.com) - bug 229474 /
 
 package org.eclipse.ant.internal.ui.launchConfigurations;
 
 
 import java.io.File;
 import java.util.List;
 
 import  org.eclipse.swt.SWT;
 import  org.eclipse.swt.events.SelectionAdapter;
 import  org.eclipse.swt.events.SelectionEvent;
 import  org.eclipse.swt.events.ShellAdapter;
 import  org.eclipse.swt.events.ShellEvent;
 import  org.eclipse.swt.graphics.Font;
 import  org.eclipse.swt.graphics.Image;
 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.Label;
 import  org.eclipse.swt.widgets.Table;
 import  org.eclipse.swt.widgets.TableColumn;
 import  org.eclipse.swt.widgets.Text;
 
Launch configuration tab which allows the user to choose the targets from an Ant buildfile that will be executed when the configuration is launched.
 
 public class AntTargetsTab extends AbstractLaunchConfigurationTab {
 	
 	private AntTargetNode fDefaultTarget = null;
 	private AntTargetNode[] fAllTargetsnull;
 	private List fOrderedTargets = null;
 	
 	private CheckboxTableViewer fTableViewer = null;
 	private Label fSelectionCountLabel = null;
 	private Text fTargetOrderText = null;
 	private Button fOrderButton = null;
 	private Button fFilterInternalTargets;
 	private Button fSortButton;
 	
 	private int fSortDirection= 0;
	private boolean fInitializingfalse;

Sort direction constants.
	public final static int SORT_NONE= 0;
	public final static int SORT_NAME= 1;
	public final static int SORT_NAME_REVERSE= -1;
	public final static int SORT_DESCRIPTION= 2;
	public final static int SORT_DESCRIPTION_REVERSE= -2;

A comparator which can sort targets by name or description, in forward or reverse order.
	private class AntTargetsComparator extends ViewerComparator {
		/* (non-Javadoc)
		 * @see org.eclipse.jface.viewers.ViewerComparator#compare(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
		 */
		public int compare(Viewer viewerObject e1Object e2) {
			if (!(e1 instanceof AntTargetNode && e2 instanceof AntTargetNode)) {
				return super.compare(viewere1e2);
			}
				return 0;
			}
			String string1string2;
			int result= 0;
				string1= ((AntTargetNodee1).getLabel();
				string2= ((AntTargetNodee2).getLabel();
else {
				string1= ((AntTargetNodee1).getTarget().getDescription();
				string2= ((AntTargetNodee2).getTarget().getDescription();
			}
			if (string1 != null && string2 != null) {
				resultgetComparator().compare(string1string2);
else if (string1 == null) {
				result= 1;
else if (string2 == null) {
				result= -1;
			}
			if ( < 0) { // reverse sort
				if (result == 0) {
					result= -1;
else {
					result= -result;
				}
			}
			return result;
		}
	}
	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#createControl(org.eclipse.swt.widgets.Composite)
	 */
	public void createControl(Composite parent) {
		Font font = parent.getFont();
		Composite comp = new Composite(parent, SWT.NONE);
		GridLayout topLayout = new GridLayout();
		comp.setLayout(topLayout);		
		GridData gd = new GridData(GridData.FILL_BOTH);
		comp.setLayoutData(gd);
		comp.setFont(font);
		Composite buttonCompositenew Composite(comp, SWT.NONE);
		GridLayout layoutnew GridLayout();
		layout.verticalSpacing = 0;
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		buttonComposite.setLayout(layout);
		buttonComposite.setFont(font);
		createSortTargets(buttonComposite);
		Dialog.applyDialogFont(parent);
	}

Creates the selection count widget

Parameters:
parent the parent composite
	private void createSelectionCount(Composite parent) {
		 = new Label(parent, SWT.NONE);
		.setFont(parent.getFont());
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		.setLayoutData(gd);
	}

Creates the widgets that display the target order

Parameters:
parent the parent composite
	private void createTargetOrder(Composite parent) {
		Font fontparent.getFont();
		Label label = new Label(parent, SWT.NONE);
		label.setFont(font);
		Composite orderComposite = new Composite(parent, SWT.NONE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		orderComposite.setLayoutData(gd);
		GridLayout layout = new GridLayout(2, false);
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		orderComposite.setLayout(layout);
		orderComposite.setFont(font);
		 = new Text(orderComposite, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.V_SCROLL | SWT.READ_ONLY);
		.setFont(font);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.heightHint = 40;
		.setLayoutData(gd);
		gd = (GridData).getLayoutData();
		gd.verticalAlignment = GridData.BEGINNING;
		.setFont(font);
		.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
			}
		});
	}

Creates the toggle to filter internal targets from the table

Parameters:
parent the parent composite
	private void createFilterInternalTargets(Composite parent) {
		.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
			}
		});
	}

Creates the toggle to sort targets in the table

Parameters:
parent the parent composite
	private void createSortTargets(Composite parent) {
		.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
			}
		});
	}

The filter targets button has been toggled. If it's been turned on, filter out internal targets. Else, restore internal targets to the table.
	private void handleFilterTargetsSelected() {
		boolean filter.getSelection();
		if (filter) {
else {
		}
		// Must refresh before updating selection count because the selection
		// count's "hidden" reporting needs the content provider to be queried
		// first to count how many targets are hidden.
		if (!) {
		}
	}
		if ( == null) {
		}
	}

The button to sort targets has been toggled. Set the tab's sorting as appropriate.
	private void handleSortTargetsSelected() {
		setSort(.getSelection() ?  : );
	}

Sets the sorting of targets in this tab. See the sort constants defined above.

Parameters:
column the column which should be sorted on
	private void setSort(int column) {
		if (!) {
		}
	}

The target order button has been pressed. Prompt the user to reorder the selected targets.
	private void handleOrderPressed() {
		int ok = dialog.open();
		if (ok == .) {
			Object[] targets = dialog.getTargets();
			for (int i = 0; i < targets.lengthi++) {
				.add(targets[i]);
			}
		}
	}

Creates the table which displays the available targets

Parameters:
parent the parent composite
	private void createTargetsTable(Composite parent) {
		Font fontparent.getFont();
		Label label = new Label(parent, SWT.NONE);
		label.setFont(font);
		final Table tablenew Table(parent, SWT.CHECK | SWT.BORDER | SWT.FULL_SELECTION );
		GridData datanew GridData(GridData.FILL_BOTH);
		int availableRowsavailableRows(parent);
		data.heightHint = table.getItemHeight() * (availableRows / 20);
		data.widthHint= 250;
		table.setLayoutData(data);
		table.setFont(font);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);		
		TableLayout tableLayoutnew TableLayout();
		ColumnWeightData weightData = new ColumnWeightData(30, true);
		tableLayout.addColumnData(weightData);
		weightData = new ColumnWeightData(70, true);
		tableLayout.addColumnData(weightData);		
		table.setLayout(tableLayout);
		final TableColumn column1new TableColumn(table, SWT.NULL);
		final TableColumn column2new TableColumn(table, SWT.NULL);
		//TableLayout only sizes columns once. If showing the targets
		//tab as the initial tab, the dialog isn't open when the layout
		//occurs and the column size isn't computed correctly. Need to
		//recompute the size of the columns once all the parent controls 
		//have been created/sized.
		//HACK Bug 139190 
		getShell().addShellListener(new ShellAdapter() {
			public void shellActivated(ShellEvent e) {
				if(!table.isDisposed()) {
					int tableWidth = table.getSize().x;
					if (tableWidth > 0) {
						int c1 = tableWidth / 3;
						column1.setWidth(c1);
						column2.setWidth(tableWidth - c1);
					}
					getShell().removeShellListener(this);
				}
			}
		});
			public void doubleClick(DoubleClickEvent event) {
				ISelection selectionevent.getSelection();
				if (!selection.isEmpty() && selection instanceof IStructuredSelection) {
					Object elementss.getFirstElement();
					boolean checked= !.getChecked(element);
					.setChecked(elementchecked);
					updateOrderedTargets(elementchecked);
				}
			}
		});
			}
		});
		TableColumn[] columns.getTable().getColumns();
		for (int i = 0; i < columns.length; i++) {
			final int indexi;
			columns[index].addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					if (.getSelection()) {
						// index 0 => sort_name (1)
						// index 1 => sort_description (2)
						int columnindex + 1;
						if (column == ) {
							column= -column// invert the sort when the same column is selected twice in a row
						}
						setSort(column);
					}
				}
			});
		}
	}

Return the number of rows available in the current display using the current font.

Parameters:
parent The Composite whose Font will be queried.
Returns:
int The result of the display size divided by the font size.
	private int availableRows(Composite parent) {
		int fontHeight = (parent.getFont().getFontData())[0].getHeight();
		int displayHeight = parent.getDisplay().getClientArea().height;
		return displayHeight / fontHeight;
	}

Updates the ordered targets list in response to an element being checked or unchecked. When the element is checked, it's added to the list. When unchecked, it's removed.

Parameters:
element the element in question
checked whether the element has been checked or unchecked
	private void updateOrderedTargets(Object element , boolean checked) {
		if (checked) {
			 .add(element);
else {
		}	 
	}

Updates the selection count widget to display how many targets are selected (example, "1 out of 6 selected") and filtered.
	private void updateSelectionCount() {
		String numSelected = Integer.toString(checked.length);
		int all == null ? 0 : .;
		int visible.getTable().getItemCount();
		String total = Integer.toString(visible);
		int numHiddenall - visible;
		if (numHidden > 0) {
			.setText(MessageFormat.format(.new String[]{numSelected, String.valueOf(all), String.valueOf(numHidden)}));
else {
		}
		.setEnabled(checked.length > 1);
		StringBuffer buffer = new StringBuffer();
		while (iter.hasNext()) {
			buffer.append(", "); //$NON-NLS-1$
		}
		if (buffer.length() > 2) {
			// remove trailing comma
			buffer.setLength(buffer.length() - 2);
		}
		.setText(buffer.toString());
	}

Returns all targets in the buildfile.

Returns:
all targets in the buildfile
	private AntTargetNode[] getTargets() {
		if ( == null || isDirty()) {
			setDirty(false);
			final String expandedLocationvalidateLocation();
			if (expandedLocation == null) {
				return ;
			}
			final CoreException[] exceptionsnew CoreException[1];
			try {
					/* (non-Javadoc)
					 * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
					 */
					public void run(IProgressMonitor monitor) {
						try {
							 = AntUtil.getTargets(expandedLocation);
catch (CoreException ce) {
							exceptions[0]= ce;
						}
					}
				};
				if (context == null) {
				    contextgetLaunchConfigurationDialog();
				}
				ISchedulingRule rulenull;
				if (!ResourcesPlugin.getWorkspace().isTreeLocked()) {
					//only set a scheduling rule if not in a resource change callback
					rule= AntUtil.getFileForLocation(expandedLocationnull);
				}
				PlatformUI.getWorkbench().getProgressService().runInUI(contextoperationrule);
catch (InvocationTargetException e) {
			    AntUIPlugin.log("Internal error occurred retrieving targets"e.getTargetException()); //$NON-NLS-1$
			    null;
			    return null;
catch (InterruptedException e) {
			    AntUIPlugin.log("Internal error occurred retrieving targets"e); //$NON-NLS-1$
			    null;
			    return null;
			}
			if (exceptions[0] != null) {
				IStatus exceptionStatusexceptions[0].getStatus();
				IStatus[] childrenexceptionStatus.getChildren();
				StringBuffer messagenew StringBuffer(exceptions[0].getMessage());
				for (int i = 0; i < children.lengthi++) {
					message.append(' ');
					IStatus childStatus = children[i];
					message.append(childStatus.getMessage());
				}
				return ;
			}
			if ( == null) {
			    //if an error was not thrown during parsing then having no targets is valid (Ant 1.6.*)
			    return ;
			}
			AntProjectNode projectNodetarget.getProjectNode();
			for (int i=0; i < .i++) {
			    target[i];
				if (target.isDefaultTarget()) {
				}
			}
		}
		return ;
	}
	private void setErrorMessageFromNode(AntElementNode node) {
		if (getErrorMessage() != null) {
			return;
		}
		if (node.isErrorNode() || node.isWarningNode()) {
			String messagenode.getProblemMessage();
		    if (message != null) {
		        setErrorMessage(message);
		    } else {
		    }
		}
	}
	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
	 */
	public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
	}
	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
	 */
	public void initializeFrom(ILaunchConfiguration configuration) {
		configuration;
		setDirty(true);
		boolean hideInternalfalse;
		try {
catch (CoreException e) {
			AntUIPlugin.log(e);
		}
		.setSelection(hideInternal);
		int sort;
		try {
catch (CoreException e) {
			AntUIPlugin.log(e);
		}
		.setSelection(sort != );
		setSort(sort);
		String configTargetsnull;
		String newLocationnull;
		try {
			newLocationconfiguration.getAttribute(., (String)null);
catch (CoreException ce) {
		}
		if (newLocation == null) {
			return
		}
		AntTargetNode[] allTargetNodesgetTargets();
		if (allTargetNodes == null) {
			return
		}
		String[] targetNames= AntUtil.parseRunTargets(configTargets);
		if (targetNames.length == 0) {
			setExecuteInput(allTargetNodes);
			if ( != null) {
			}
			return;
		}
		setExecuteInput(allTargetNodes);
		for (int i = 0; i < targetNames.lengthi++) {
			for (int j = 0; j < .j++) {
				if (targetNames[i].equals([j].getTargetName())) {
				}
			}
		}
	}
	private void initializeForNoTargets() {
	}

Sets the execute table's input to the given input.
	private void setExecuteInput(Object input) {
	}
	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
	 */
	public void performApply(ILaunchConfigurationWorkingCopy configuration) {
		//	attribute added in 3.0, so null must be used instead of false for backwards compatibility
		if (.getSelection()) {
else {
		}
		//attribute added in 3.0, so null must be used instead of 0 for backwards compatibility
else {
		}
		if (.size() == 1) {
			if (item.isDefaultTarget()) {
				return;
			}
else if (.size() == 0) {
			return;
		}
		StringBuffer buffnew StringBuffer();
		String targets = null;
		while (iter.hasNext()) {
			AntTargetNode item = (AntTargetNode)iter.next();
			buff.append(item.getTargetName());
			buff.append(',');
		}
		if (buff.length() > 0) {
			targetsbuff.toString();
		}  
	}
	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName()
	 */
	public String getName() {
	}
	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getImage()
	 */
	public Image getImage() {
	}
	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#isValid(org.eclipse.debug.core.ILaunchConfiguration)
	 */
	public boolean isValid(ILaunchConfiguration launchConfig) {
		if ( == null || isDirty()) {
			if (getErrorMessage() != null && !isDirty()) {
				//error in parsing;
				return false;
			//targets not up to date and no error message...we have not parsed recently
			initializeFrom(launchConfig);
			if (getErrorMessage() != null) {
				//error in parsing;
				return false;
			}
		}
		return super.isValid(launchConfig);
	}
	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.AbstractLaunchConfigurationTab#setDirty(boolean)
	 */
	protected void setDirty(boolean dirty) {
		//provide package visibility
		super.setDirty(dirty);
	}
	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#activated(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
	 */
	public void activated(ILaunchConfigurationWorkingCopy workingCopy) {
		if (isDirty()) {
			super.activated(workingCopy);
		}
	}
	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#deactivated(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
	 */
	public void deactivated(ILaunchConfigurationWorkingCopy workingCopy) {
		if (.size() == 0) {
			//set the dirty flag so that the state will be reinitialized on activation
			setDirty(true);
		}
	}
	private String validateLocation() {
		String expandedLocationnull;
		String locationnull;
		try {
			if (location == null) {
				return null;
			}
			expandedLocationmanager.performStringSubstitution(location);
			if (expandedLocation == null) {
				return null;
			}
			File file = new File(expandedLocation);
			if (!file.exists()) {
				return null;
			}
			if (!file.isFile()) {
				return null;
			}
			return expandedLocation;
catch (CoreException e1) {
			if (location != null) {
				try {
					manager.validateStringVariables(location);
					return null;
catch (CoreException e2) {//invalid variable
					return null;
				}
			}
			return null;
		}
	}
	protected boolean isTargetSelected() {
	}
New to GrepCode? Check out our FAQ X