Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Software Engineering 2:
Tutorial 7: additional hints
Ekkart Kindler
Overview
Ekkart Kindler
Eclipse
Dialogs
Tricks
Resources (files) and loading them
Java3D
Loading 3D models and textures
Using textures: normals
(lighting: ambient, diffuse, specular)
Picking (of 3D objects): PickCanvas, MouseAdapter
EMF
Listeners (Adapters)
.edit: Label Providers, Icons
SE 2 (02162 e16), T07
2
Overview
Ekkart Kindler
OCL
Interactive OCL console
GMF
Decorators
ePNK
Helper functions
FlatAccess (access a net as if there were no pages)
SE 2 (02162 e16), T07
3
Ekkart Kindler
1. Eclipse
SE 2 (02162 e16), T07
4
1.1 Dialogs
Ekkart Kindler
if (MessageDialog.openQuestion(shell,
“Disconnect?”,
“Do you really want to disconnect from the running simulation?")) {
…
new InputDialog(shell, "Name?", "Please enter a name.", "system1", null).open();
new ResourceDialog(shell,
"Select deployment", SWT.OPEN | SWT.SINGLE).open();
SE 2 (02162 e16), T07
5
More Examples
Ekkart Kindler
List<ViewerFilter> filters = new ArrayList<ViewerFilter>();
filters.add(new ViewerFilter() {
@Override
public boolean select(Viewer viewer,
Object parentElement, Object element) {
if (element instanceof IFile) {
return ((IFile)element).
getFileExtension().equals("deployment");
} else return true;
}
});
WorkspaceResourceDialog.openFileSelection(shell,
"Select files", "Please select input file(s)",
false, null, filters);
Filters for the files
Suggested path
WorkspaceResourceDialog.openNewFile(shell, "Save file",
"Please select output file", null, null);
SE 2 (02162 e16), T07
6
Yet More Examples
Ekkart Kindler
String[] buttons = new String[]{
"Overwrite",
"Save as '" + fileAlternative.getName() + "'",
"Cancel“
};
MessageDialog dialog = new MessageDialog(shell,
"File already exists", image,
"'" + file.getName() + "' already exists.\nWhat do you want to do?",
MessageDialog.WARNING, buttons, 1);
switch (dialog.open()) {
case 0: return file;
case 1: return fileAlternative;
default: return null;
}
SE 2 (02162 e16), T07
7
Dialogs: Which one?!
Ekkart Kindler
Does a dialog already exist for my purpose?
Quick Type Hierarchy
Yes? Use it!
with Ctrl + t
No? Pick the closet one, inherit from it, and adjust it
for your needs!
How do I find existing dialogs?
Type Hierarchy of existing dialogs
Plug-in Spy on open dialogs
Open with
Alt + Shift + F1
SE 2 (02162 e16), T07
8
Dialogs: References
Ekkart Kindler
Eclipse Help (Help Help Contents):
Platform Plug-in Developer Guide > Programmer's
Guide > Dialogs and wizards
JavaDoc
JFace Snippets:
http://wiki.eclipse.org/JFaceSnippets
SE 2 (02162 e16), T07
9
1.2 Eclipse Tricks
Ekkart Kindler
Plug-in Spy: See some interesting feature when
using Eclipse find our where and how it is
implemented:
Alt + Shift + F1
(on the respective window see slide 8)
Inspect type hierarchy:
Ctrl + t
(on the respective class: see slide 8)
SE 2 (02162 e16), T07
10
1.3 Resources in Eclipse
C:\se2\workspace\
.metadata\
dk.dtu.imm.se2.petrinet\
model\
petrinet.ecore
petrinet.genmodel
src\
java.io.File
java.io.File
java.io.File
java.io.File
java.io.File
java.io.File
java.io.File
…
java.io.File
bin\
…
META-INF\
MANIFEST.MF
.project
plugin.xml
java.io.File
java.io.File
java.io.File
java.io.File
How do you access
these items in Java?
SE 2 (02162 e16), T07
Ekkart Kindler
IWorkspace
(metadata directory)
IProject
IFolder
IFile
IFile
IFolder
(source files)
IFolder
(compiled files)
IFolder
IFile
(IProjectDescription)
IFile
Images are also stored as IFiles ;-)
11
Resources: Class hierarchy
Ekkart Kindler
How to navigate?
[brief excerpt from the JavaDoc]
IResource.exists(): boolean
IResource.getFullPath(): IPath
IResource.getLocationURI(): URI
Check whether the resource exists
Returns the full, absolute path of this
resource relative to the workspace.
Get the URI of this ressource
IContainer.members(): IResource[]
IContainer.getFile(String file): IFile
Get handles to all resources contained here
Get a handle to a file contained here
IFolder.create(…): void
IFile.create(…): void
Create this folder (assuming it does not exist)
Create this file (assuming it does not exist)
Open that dialog with ”Ctrl + t”
SE 2 (02162 e16), T07
12
Resources: Handles
Ekkart Kindler
Access to a resource is managed by a ”handle”:
petrinetModel: IFile
fullPath = ”/dk.dtu.imm.se2.petrinet/model/Petrinet.ecore”
”There is no guarantee or requirement that the
resource itself exists until you try to do something
with the handle.“
[Eclipse help]
IFile petrinetModel = modelFolder.getFile("Petinet.ecore");
MessageDialog.openInformation(shell, "petrinet model path",
petrinetModel.getFullPath().toString() + "\n" +
"File exists: " + petrinetModel.exists());
What will be
shown here?
SE 2 (02162 e16), T07
13
Resources: URIs
Ekkart Kindler
Syntax for Uniform Resource Identifiers
Platform independent!
<URI scheme>:<path>#<fragment>
Examples:
http://www.compute.dtu.dk/~ekki/
platform:/resource/dk.dtu.imm.se2.petrinet/plugin.xml
platform:/plugin/org.eclipse.emf.ecore/model/Ecore.ecore#//EReference
Resource is
located in a
running plugin
Resource is
located in the
workspace
Reference to an
EClass within the
EMF meta model
URIs also in your models (as XMI):
wiper.componentdefinition
wipersystem.deployment
test.petrinet
SE 2 (02162 e16), T07
14
Load a Resource
Ekkart Kindler
A resource can be loaded with a given URI
via the bundle of the local plugin activator:
platform:/plugin/dk.dtu.imm.se2.dashboard/icons/dashboard.gif
final URL imageURL =
PluginActivator.getDefault().getBundle().getEntry("icons/dashboard.gif");
final Image image = ImageDescriptor.createFromURL(imageURL).createImage();
EMF provides simple methods for loading models:
1. Create resource set
2. ask it for a resource using a URI
final ResourceSet resourceSet = new ResourceSetImpl();
final URI uri = URI.createURI(
"platform:/resource/CASETool-Examples/WiperSystem/controller.componentdefinition"
);
final Resource resource = resourceSet.getResource(uri, true);
final EObject compDef = resource.getContents().get(0);
Located in
workspace
SE 2 (02162 e16), T07
Project
Folder
File
15
Alternatives
Ekkart Kindler
See Java3D part (slide 22)
SE 2 (02162 e16), T07
16
Resources: References
Ekkart Kindler
Eclipse help (Help Help Contents):
Platform Plug-in Developer Guide Programmer’s Guide
Resources overview Resources and the file system
Uniform Resource Identifier (URI) – Generic Syntax:
http://labs.apache.org/webarch/uri/rfc/rfc3986.html
SE
17 2 (02162 e16), T07
Ekkart Kindler
2. Java3D
SE 2 (02162 e16), T07
18
2.1 Loading Models / Textures
SE 2 (02162 e16), T07
Ekkart Kindler
19
Loading a (3D) model
Ekkart Kindler
private Node model = null;
private Node getModel() {
if (model != null) {
return model.cloneTree();
}
ObjectFile file = new ObjectFile(
ObjectFile.RESIZE | ObjectFile.TRIANGULATE |
ObjectFile.STRIPIFY );
URL filePath = null;
URL basePath = null;
...
SE 2 (02162 e16), T07
20
Loading a model
Ekkart Kindler
try {
if (geometry != null) {
URI baseURI = geometry.eResource().getURI().
trimSegments(1).appendSegment("models");
basePath = new URL(baseURI.toString());
basePath = FileLocator.resolve(basePath);
URI fileURI = baseURI.appendSegment("model.obj");
filePath = new URL(fileURI.toString());
filePath = FileLocator.resolve(filePath);
} else {
filePath = Platform.getBundle(
"dk.dtu.imm.se.vis3d.enigne").
getEntry("models/model.obj");
filePath = FileLocator.resolve(filePath);
basePath = Platform.getBundle(
"dk.dtu.imm.se.vis3d.enigne").getEntry("models/");
basePath = FileLocator.resolve(basePath);
SE 2 (02162 e16), T07
21
Loading a model
Ekkart Kindler
}
} catch (Exception e) {
System.err.println(e);
}
Scene scene = null;
if (filePath != null) {
try {
file.setBasePath(basePath.toExternalForm());
scene = file.load(filePath);
} catch (Exception e) {
e.printStackTrace();
}
if (scene != null) {
SE 2 (02162 e16), T07
22
Loading a model
Ekkart Kindler
BranchGroup branchGroup = scene.getSceneGroup();
TransformGroup transformGroup = new TransformGroup();
Transform3D transform = new Transform3D();
transform.rotZ(Math.PI/2);
...
transform.setScale(0.5);
transformGroup.setTransform(transform);
transformGroup.addChild(branchGroup);
model = transformGroup;
}
if (model != null) {
return model.cloneTree();
} else {
return new ColorCube(0.2);
}
}
SE 2 (02162 e16), T07
23
Loading a texture
Ekkart Kindler
URL filePath = null;
try {
filePath = Platform.getBundle(
"dk.dtu.imm.se.vis3d.enigne").
getEntry("textures/track.png");
filePath = FileLocator.resolve(filePath);
} catch (Exception e) {
System.err.println(e);
}
TextureLoader textureLoader = new TextureLoader(
filePath, frame);
Texture texture = textureLoader.getTexture();
javax.media.j3d.Appearance appearance = new
javax.media.j3d.Appearance();
SE 2 (02162 e16), T07
24
Loading a texture
Ekkart Kindler
TextureAttributes textureAttributes = new TextureAttributes();
textureAttributes.setTextureMode(TextureAttributes.MODULATE);
appearance.setTextureAttributes(textureAttributes);
Material material = new Material();
material.setAmbientColor(new Color3f(0.2f,0.2f,0.2f));
material.setDiffuseColor(new Color3f(1.0f,1.0f,1.0f));
material.setSpecularColor(new Color3f(0.3f,0.3f,0.3f));
material.setEmissiveColor(new Color3f(0f,0f,0f));
appearance.setMaterial(material);
appearance.setTexture(texture);
SE 2 (02162 e16), T07
25
2.3 Creating area with texture
Ekkart Kindler
Texture texture = appearance.getTexture();
float f
Point3f
Point3f
Point3f
Point3f
= ... // aspect ratio of texture
p0 = ...; // points of area
p1 = ...;
p2 = ...;
p3 = ...;
QuadArray plane = new QuadArray(4, QuadArray.COORDINATES
| QuadArray.TEXTURE_COORDINATE_2 | QuadArray.NORMALS );
plane.setCoordinate(0,
plane.setCoordinate(1,
plane.setCoordinate(2,
plane.setCoordinate(3,
SE 2 (02162 e16), T07
p0);
p3);
p2);
p1);
26
Creating area with texture
Ekkart Kindler
float[] norms2 = { 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f };
plane.setNormals(0, norms2);
q = new TexCoord2f();
q.set(0.0f, f);
plane.setTextureCoordinate(0,
q.set(0.0f, 0.0f);
plane.setTextureCoordinate(0,
q.set(1.0f, 0.0f);
plane.setTextureCoordinate(0,
q.set(1.0f, f);
plane.setTextureCoordinate(0,
0, q);
3, q);
2, q);
1, q);
track = new Shape3D(plane, appearance);
SE 2 (02162 e16), T07
27
2.3 Picking Objects (trigger)
Ekkart Kindler
public class PickListener extends MouseAdapter {
private Vis3DEngine engine;
private PickCanvas pickCanvas;
public PickListener(Vis3DEngine engine,
PickCanvas pickCanvas) {
super();
this.engine = engine;
this.pickCanvas = pickCanvas;
}
SE 2 (02162 e16), T07
28
Picking Objects (trigger)
Ekkart Kindler
public void mouseClicked(MouseEvent e) {
pickCanvas.setShapeLocation(e);
PickResult result = pickCanvas.pickClosest();
if (result != null) {
Node node = result.getObject();
if (node != null) {
// notify engine etc about the object
// that was picked
}
}
}
}
SE 2 (02162 e16), T07
29
Install PickListener
Ekkart Kindler
PickCanvas pickCanvas = new PickCanvas(canvas, scene);
pickCanvas.setMode(PickCanvas.BOUNDS);
canvas.addMouseListener(
new PickListener(this,pickCanvas));
SE 2 (02162 e16), T07
30
Ekkart Kindler
3. EMF
SE 2 (02162 e16), T07
31
3.1 Listener
Ekkart Kindler
You can install a listener on and any EMF object
(EObject), so that you are notified, whenever some
of its features change.
In EMF, listeneres/observers are called adapters
(since they serve some additional purposes, which
we do not discuss here).
See org.eclipse.emf.common.notify.Adapter
interface
Most relevan method notifyChanged()
SE 2 (02162 e16), T07
32
Install an Adapter
Ekkart Kindler
object.eAdapters().add(new Adapter() {
public void notifyChanged(Notification notification) {
// TODO override this with whatever you need to do
}
public Notifier getTarget() {
// TODO Auto-generated method stub
return null; }
public void setTarget(Notifier newTarget) {
// TODO Auto-generated method stub
}
public boolean isAdapterForType(Object type) {
// TODO Auto-generated method stub
return false;} });
SE 2 (02162 e16), T07
33
3.2 Label Providers
Ekkart Kindler
Some labels in the automatically generated tree
editor are not so reasonable (e.g. Point 10.0 should
rather be Point (10.0, 10.0) )
You can change how the labels of an object looks in
the .edit project by changing the respective
...ItemProviders.
Change the implementation of the getText()
method (make sure you mark this with @generated
NOT)
SE 2 (02162 e16), T07
34
3.3 Custom Icons
Ekkart Kindler
You can also change the icons for the respective
elements in the .edit project
Place your individual .gif files in the icon folder (for
tools and for the objects themselves)
SE 2 (02162 e16), T07
35
Ekkart Kindler
4. ePNK
SE 2 (02162 e16), T07
36
4.1 Helper functions
Ekkart Kindler
The ePNK provides some convenience classes:
NetFunctions with static methods
getPetriNet(EObject object) for obtaining the Petri net in
which the object is contained
getPetriNetType(EObject object) for obtaining the type
(PNTD) of the Petri net in which the object is contained
getAllNetObjects(PetriNet net) for obtaining all Petri net
objects of the net
...
SE 2 (02162 e16), T07
37
4.2 FlatAccess
Ekkart Kindler
FlatAccess provides efficient acess to the net
structure which ignores pages (flattens the net).
To obtain a flatAccess object for a net, call
FlatAccess flatAccess = FlatAccess.getFlatAccess(net);
Moreover, FlatAccess allows accessing all
places and transitions of the net directly
...
SE 2 (02162 e16), T07
38
Overview
Ekkart Kindler
Eclipse
Dialogs
Tricks
Resources (files) and loading them
Java3D
Loading 3D models and textures
Using textures: normals
(lighting: ambient, diffuse, specular)
Picking (of 3D objects): PickCanvas, MouseAdapter
EMF
Listeners (Adapters)
.edit: Label Providers, Icons
SE 2 (02162 e16), T07
39
Overview
Ekkart Kindler
OCL
Interactive OCL console
GMF
Decorators
ePNK
Helper functions
FlatAccess (access a net as if there were no pages)
SE 2 (02162 e16), T07
40