SDA SE Wiki

Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering

User Tools

Site Tools


Code Snippets

A simple SWT/JFace application

You need these Libraries in the Build-Path to execute the app:

  • org.eclipse.swt.win32.win32.x86_3.3.2.v3347a.jar
  • org.eclipse.jface_3.3.1.M20070910-0800b.jar
  • org.eclipse.core.runtime_3.3.100.v20070530.jar
  • org.eclipse.ui.workbench_3.3.1.M20070921-1200.jar
  • org.eclipse.core.commands_3.3.0.I20070605-0010.jar
  • org.eclipse.equinox.common_3.3.0.v20070426.jar

<code xml|h .classpath |h> <?xml version=“1.0” encoding=“UTF-8”?> <classpath>

<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="L:/Work/eclipse3311-xplab2008a/plugins/org.eclipse.swt.win32.win32.x86_3.3.2.v3347a.jar"/>
<classpathentry kind="lib" path="L:/Work/eclipse3311-xplab2008a/plugins/org.eclipse.jface_3.3.1.M20070910-0800b.jar"/>
<classpathentry kind="lib" path="L:/Work/eclipse3311-xplab2008a/plugins/org.eclipse.core.runtime_3.3.100.v20070530.jar"/>
<classpathentry kind="lib" path="L:/Work/eclipse3311-xplab2008a/plugins/org.eclipse.ui.workbench_3.3.1.M20070921-1200.jar"/>
<classpathentry kind="lib" path="L:/Work/eclipse3311-xplab2008a/plugins/org.eclipse.core.commands_3.3.0.I20070605-0010.jar"/>
<classpathentry kind="lib" path="L:/Work/eclipse3311-xplab2008a/plugins/org.eclipse.equinox.common_3.3.0.v20070426.jar"/>
<classpathentry kind="output" path="bin"/>

</classpath>

</Code>

import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Bla extends ApplicationWindow {

	// UI Elements
	Button button;

	public Bla(Shell shell) {
		super(shell);
	}

	@Override
	protected Control createContents(Composite parent) {

		button = new Button(parent, SWT.PUSH);
		button.setText("Click me");

		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				button.setText("Hello World");
			}
		});

		parent.pack();

		return parent;
	}

	public static void main(String args[]) {
		Display display = new Display();
		Shell shell = new Shell(display);
		Bla bla = new Bla(shell);

		bla.setBlockOnOpen(true);
		bla.open();
		display.dispose();
	}
}

A test for it

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;

import junit.framework.TestCase;

public class BlaTest extends TestCase {

	Bla bla;
	private static Display display = new Display();
	
	protected void setUp() throws Exception {
		super.setUp();
		Shell shell = new Shell(display);
		bla = new Bla(shell);
		bla.open();
	}

	public void testButton() {
		String buttonText = bla.button.getText();
		assertEquals("Click me", buttonText);

	}

	public void testButtonChange() {
		bla.button.notifyListeners(SWT.Selection, new Event());
		String buttonText = bla.button.getText();
		assertEquals("Hello World", buttonText);

	}
}

SWT-Stuff

	

	public void sleep(int millis) {
		//Some kind of busy waiting for allowing the SWT to work in parallel
		long endTime = System.currentTimeMillis() + millis;
		while (System.currentTimeMillis() < endTime) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

Query a predicate

To query a predicate use the following pattern:


  
 
  
  List<String> factbaseNames = JTUtils.getPrologInterfaceKeysWithJTransformerSubscription();
  
  PrologInterface pif = JTUtils.getPrologInterface("factbasename");
		
		PrologSession session = null;
		try {
			session = pif.getSession();
			// your code goes here 
		} finally {
			if (session != null)
				session.dispose();
		}

  @author rho
 
 

JUnit Test Snippets

You always have to manually fire an event while performing GUI testing using JUnit.

For instance, the below line of code will not fire the selected event for the combo box by default.

view.comboproject.select(0);

You have to manually write the code below after the above statement in order to explicitly fire a Selection event, for the Combobox as shown in this example.

view.comboproject.notifyListeners(SWT.Selection, new Event());

getEnclosingElements()

Just for Alexis who was bagging for this Code ;-)

    public List<PEFReference> getEnclosingElements() throws PrologInterfaceException {
    	
    	FactbaseAdapter adapter = new FactbaseAdapter();
    	List<Map<String,String>> pefReferences = adapter.queryAll(JTUtils.getKeyForPrologInterface(pif), 
    			Predicates.ENCL_PEFS + "(" + getId() + ",Name,EnclId,EnclKind)");
    	
    	List<PEFReference> refs = new ArrayList<PEFReference>();
    	for (Map<String, String> map : pefReferences) {
			PEFReference ref = new PEFReference(
					map.get("EnclKind"),
					map.get("EnclId"),
					map.get("Name")
				);
			if (!map.get("EnclId").equals(getId()))	refs.add(ref);
		}
    	return refs;
    }

And the Predicat

/**
 * encl_pefs(+Id, -Name, -EnclosingId,-EnclKind) is nondet.
 *
 */
encl_pefs(Id, 'Parent', Parent,Kind) :-
    tree(Id,Parent,_),
    Parent \= null,
    tree(Parent,_,Kind).    
    
encl_pefs(Id, 'Enclosing Method', Encl,methodDefT):-
    enclosing(Id,Encl),
    methodDefT(Encl,_,_,_,_,_,_).    

encl_pefs(Id, 'Enclosing Field', Encl,fieldDefT):-
    enclosing(Id,Encl),
    fieldDefT(Encl,_,_,_,_).    

encl_pefs(Id, 'Enclosing Class', EnclClass, classDefT):-
    enclClass(Id,EnclClass).

encl_pefs(Id, 'Enclosing Package', EnclPackage, packageT):-
    enclPackage(Id,EnclPackage).
teaching/labs/xp/2008a/snippets.txt · Last modified: 2018/05/09 01:59 (external edit)

SEWiki, © 2020