Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "Scout/Tutorial/3.8/SVG Field"
(→Verify the result) |
m (→Add a static SVG Image) |
||
Line 61: | Line 61: | ||
protected void execInitField() throws ProcessingException { | protected void execInitField() throws ProcessingException { | ||
String xml = | String xml = | ||
− | + | "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" + | |
− | + | "<svg width=\"400\" height=\"400\" viewBox=\"0 0 400 400\" \n" + | |
− | + | " xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" \n" + | |
− | + | " contentScriptType=\"text/ecmascript\" zoomAndPan=\"magnify\" \n" + | |
− | + | " contentStyleType=\"text/css\" version=\"1.1\" xml:space=\"preserve\" \n" + | |
− | + | " preserveAspectRatio=\"xMidYMid meet\"\n" + | |
− | + | ">\n" + | |
− | + | " <a xlink:href=\"http://local/polygon\">\n" + | |
− | + | " <polygon id=\"poly\" points=\"220,100 300,210 170,250 123,234\" \n" + | |
− | + | " style=\"fill:#f5950d;stroke:red;stroke-width:3\" />\n" + | |
− | + | " </a>\n" + | |
− | + | " <a xlink:href=\"http://local/circle\">\n" + | |
− | + | " <circle id=\"circle\" cx=\"200\" cy=\"100\" r=\"40\" \n" + | |
− | + | " stroke=\"blue\" stroke-width=\"2\" fill=\"#00678c\" />\n" + | |
− | + | " </a>\n" + | |
− | + | "</svg>"; | |
SVGDocument doc = SVGUtility.readSVGDocument(new ByteArrayInputStream(xml.getBytes())); | SVGDocument doc = SVGUtility.readSVGDocument(new ByteArrayInputStream(xml.getBytes())); | ||
setSvgDocument(doc); | setSvgDocument(doc); |
Revision as of 16:03, 20 March 2012
The Scout documentation has been moved to https://eclipsescout.github.io/.
Contents
- 1 Introduction
- 2 Getting started
- 3 Create a Scout Application with SVG Fields
Introduction
With the upcoming Scout Release 3.8 a SVG field will be introduced as an additional UI component. The new SVG field enhances Eclipse Scout with the ability to display interactive diagrams and graphics that work on desktop as well as on web environment.
This tutorial demonstrate the use of the SVG field for a view only field as well as an interactive field.
Getting started
The AbstractSvgField comes as part of Scout Runtime Release 3.8 that will be integrated into Eclipse Juno builds. The latest nightly build of Scout Runtime 3.8 can be downloaded from http://download.eclipse.org/scout/nightly/update/.
Create a Scout Application with SVG Fields
Create a new Application
As a first step create a new Scout application in the Scout perspective. For this, choose one of the following options
- Use menu File, New, Project ..., Scout Project
- In the Scout Explorer on the top levle node use context menu New Scout Project ...
In the New Scout Project dialog
- Enter org.eclipse.scout.svgtest into field Project Name
- Untick node org.eclipse.scout.svgtest.ui.rap
- Click Finish
More details can be found The Scout documentation has been moved to https://eclipsescout.github.io/..
Add SVG support to the application
For this, do the following
- Go to the projects top level node in the Scout Explorer
- Go to the Scout Property View and tick the check box Scalable Vector Graphics (SVG) as indicated by the red arrow.
Add a SVG field to the DesktopForm
To add a SVG field to the DesktopForm do the following
- Go to the Scout Explorer
- Open the node Client and the folder Forms
- Expand the DesktopForm and click on node MainBox as indicated by the red arrow
- Use context menu New Form Field ... on node MainBox
- Select SvgField as shown below, click Next > and enter StaticSVG as class name for the field
Add a static SVG Image
- In the Scout Explorer expand the MainBox and click on the newly created node StaticSvgField
- Go to the Scout Object Properties
- Enter 4 in filed Grid H to provide a decent height for the SVG field
- Click on the green cross next to operation ExecInitField to add the init method
- Add the following implementation for to method execInitField()
@Override protected void execInitField() throws ProcessingException { String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" + "<svg width=\"400\" height=\"400\" viewBox=\"0 0 400 400\" \n" + " xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" \n" + " contentScriptType=\"text/ecmascript\" zoomAndPan=\"magnify\" \n" + " contentStyleType=\"text/css\" version=\"1.1\" xml:space=\"preserve\" \n" + " preserveAspectRatio=\"xMidYMid meet\"\n" + ">\n" + " <a xlink:href=\"http://local/polygon\">\n" + " <polygon id=\"poly\" points=\"220,100 300,210 170,250 123,234\" \n" + " style=\"fill:#f5950d;stroke:red;stroke-width:3\" />\n" + " </a>\n" + " <a xlink:href=\"http://local/circle\">\n" + " <circle id=\"circle\" cx=\"200\" cy=\"100\" r=\"40\" \n" + " stroke=\"blue\" stroke-width=\"2\" fill=\"#00678c\" />\n" + " </a>\n" + "</svg>"; SVGDocument doc = SVGUtility.readSVGDocument(new ByteArrayInputStream(xml.getBytes())); setSvgDocument(doc); }
Verify the result
To verify the resulting SVG field server and client can be started
- In the Scout Explorer go to the projects top level node
- Go to the Scout Object Properties to section Product Launchers
- Start the server (lower red arrow)
- Start the client (upper red arrow)
Create a scalable vector graphics file
Alternatively, we create a graphic file that will be loaded into the SVG field later. The graphic file is named "SampleGraphic.svg". First we create a folder "svg" under svgfieldtutorial.client/resources, where the graphic file will be stored.
The SVG graphic file contains a dark grey circle and a light grey polygon.
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <svg width="400" height="400" viewBox="0 0 400 400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" contentScriptType="text/ecmascript" zoomAndPan="magnify" contentStyleType="text/css" version="1.1" xml:space="preserve" preserveAspectRatio="xMidYMid meet"> <a xlink:href="http://local/polygon"> <polygon id="poly" points="220,100 300,210 170,250 123,234" style="fill:#cccccc;stroke:#000000;stroke-width:1"/> </a> <a xlink:href="http://local/circle"> <circle id="circle" cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="grey" /> </a> </svg>
The created SVG file can be loaded into the SVG field by using the following code snippet:
@Override protected void execInitField() throws ProcessingException { try { URL url = Activator.getDefault().getBundle().getResource("/resources/svg/SampleGraphic.svg"); InputStream is = url.openStream(); SVGDocument svgDoc = SVGUtility.readSVGDocument(is); setSvgDocument(svgDoc); } catch (IOException e) { e.printStackTrace(); throw new ProcessingException("Exception occured while reading svg file", e); } }
Adding interaction to the SVG field
Based on the SVG field and vector graphic file created in sections [1] and [2] some interaction will be added to the SVG field.
Add click interaction to the SVG field
The click event is fired whenever the SVG item is clicked. In this example a simple handler for the click event will be shown. Upon a click event a message box will be popped up and displays the xy-coordinates of the point that was clicked on.
- In the Scout Explorer expand the MainBox and click on the previous created node StaticSvgField
- Go to the Scout Object Properties
- Click on the green cross next to operation Exec Click to add the method based on the click event
- Add the following implementation for to method execClick()
@Override protected void execClicked(SvgFieldEvent e) throws ProcessingException { MessageBox.showOkMessage("Click Event", null, "Clicked on: [" + e.getPoint().getX() + "," + e.getPoint().getY() + "] "); }
Add hyperlink interaction to the SVG field
The hyperlink event is fired when an element containing an hyperlink is clicked on. The URL could contain GET parameters. A hyperlink event handler could parse this URL and these parameters and open a new form for this entity. E.g. the hyperlink could be http://local/person_nr?1234. The event handler could open the PersonForm for the person with the ID 1234 when the hyperlink event is fired.
In our example the polygon and the circle are declared with the links http://local/polygon and http://local/circle respectively. Based on the hyperlink our simple event handler processes the corresponding SVG element. If the polygon is clicked, one of its corner will be shifted to the right. If the circle is clicked, it will be moved to the right.
- In the Scout Explorer expand the MainBox and click on the previous created node StaticSvgField
- Go to the Scout Object Properties
- Click on the green cross next to operation Exec Hyperlink under Advanced Operations to add the method based on the hyperlink event
- Add the following implementation for to method execHyperlink()
@Override protected void execHyperlink(SvgFieldEvent e) throws ProcessingException { if (e.getURL() == null) return; //create a copy... SVGDocument doc = (SVGDocument) getSvgDocument().cloneNode(true); String url = e.getURL().toExternalForm(); if ("http://local/polygon".equals(url)) { //...and move a corner of the polygon SVGPolygonElement poly = (SVGPolygonElement) doc.getElementById("poly"); SVGPointList pointList = poly.getPoints(); SVGPoint p = pointList.getItem(2); p.setX(p.getX() + 10); } else if ("http://local/circle".equals(url)) { // ...and move the circle to the right SVGCircleElement circle = (SVGCircleElement) doc.getElementById("circle"); SVGLength cx = circle.getCx().getBaseVal(); cx.setValue(cx.getValue() + 10); } else { throw new ProcessingException("Unsupported Link: " + url); } setSvgDocument(doc); }