Skip to content

Commit b385e10

Browse files
ptzieglerazoitl
authored andcommitted
Move Draw2D examples to doc project and convert to AsciiDoc
1 parent 5623826 commit b385e10

File tree

18 files changed

+274
-344
lines changed

18 files changed

+274
-344
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
**/bin/
22
**/target/
3-
**/guide/*.html
3+
**/guide/**/*.html
44
/gef-updates
55
*~
66
*.rej
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
ifdef::env-github[]
2+
:imagesdir: ../../guide/demos/
3+
endif::[]
4+
5+
= Example 1 - Hello World
6+
7+
image:images/helloworld.gif[image]
8+
9+
In this example, a simple label is created that covers the whole canvas.
10+
11+
[source,java]
12+
----
13+
import org.eclipse.swt.widgets.Shell;
14+
import org.eclipse.swt.widgets.Display;
15+
import org.eclipse.draw2d.*;
16+
17+
public class HelloWorld {
18+
public static void main(String args[]){
19+
Shell shell = new Shell();
20+
shell.open();
21+
shell.setText("Draw2d Hello World");
22+
LightweightSystem lws = new LightweightSystem(shell);
23+
Label label = new Label("Hello World");
24+
lws.setContents(label);
25+
26+
Display display = Display.getDefault();
27+
while (!shell.isDisposed ()) {
28+
if (!display.readAndDispatch ()) {
29+
display.sleep ();
30+
}
31+
}
32+
}
33+
}
34+
----
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
ifdef::env-github[]
2+
:imagesdir: ../../guide/demos/
3+
endif::[]
4+
5+
= Example 2 - Layout Managers
6+
7+
image:images/demo2.gif[image]
8+
9+
In this example, a panel is created with a FlowLayout for its layout manager.
10+
11+
A flow layout positions children left-to-right in rows going from top-to-bottom.
12+
13+
The children are sized according to their preferred size.
14+
15+
[source,java]
16+
----
17+
import org.eclipse.swt.widgets.Shell;
18+
import org.eclipse.swt.widgets.Display;
19+
import org.eclipse.draw2d.*;
20+
21+
public class Demo2 {
22+
public static void main(String args[]){
23+
Shell shell = new Shell();
24+
shell.open();
25+
shell.setText("Draw2d");
26+
LightweightSystem lws = new LightweightSystem(shell);
27+
IFigure panel = new Figure();
28+
panel.setLayoutManager(new FlowLayout());
29+
lws.setContents(panel);
30+
31+
Clickable button = new Button("Click me");
32+
Clickable checkbox = new CheckBox("Check box");
33+
34+
Shape ellipse = new Ellipse();
35+
ellipse.setBackgroundColor(ColorConstants.yellow);
36+
ellipse.setSize(64, 36);
37+
38+
Shape rectangle = new RectangleFigure();
39+
rectangle.setBackgroundColor(ColorConstants.lightBlue);
40+
rectangle.setSize(64, 36);
41+
42+
panel.add(button);
43+
panel.add(checkbox);
44+
panel.add(ellipse);
45+
panel.add(rectangle);
46+
47+
Display display = Display.getDefault();
48+
while (!shell.isDisposed ()) {
49+
if (!display.readAndDispatch ()) {
50+
display.sleep ();
51+
}
52+
}
53+
}
54+
}
55+
----
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
ifdef::env-github[]
2+
:imagesdir: ../../guide/demos/
3+
endif::[]
4+
5+
= Example 3 - Clicking and Scrolling
6+
7+
image:images/demo3.gif[image]
8+
9+
In this example, a Button is used to create CheckBoxes inside a ScrollPane.
10+
11+
The button and scrollpane are placed absolutely inside the contents figure by
12+
simply setting their bounds; no layout manager is used there. However, the
13+
_view_ must have a Layout, or its preferred size will not get calculated, and
14+
scrolling will not work correctly.
15+
16+
The button's action listener will get called each time the user clicks on the
17+
button. The _listener_ will create a new CheckBox and add it to the view. A
18+
vertical scrollbar will appear when the checkboxes cannot all be displayed at
19+
once in the pane.
20+
21+
[source,java]
22+
----
23+
import org.eclipse.swt.widgets.Shell;
24+
import org.eclipse.swt.widgets.Display;
25+
import org.eclipse.draw2d.*;
26+
import org.eclipse.draw2d.geometry.*;
27+
28+
public class Demo3 {
29+
30+
static int count = 1;
31+
32+
public static void main(String args[]) {
33+
Shell shell = new Shell();
34+
shell.setSize(350,350);
35+
shell.open();
36+
shell.setText("Demo 3");
37+
LightweightSystem lws = new LightweightSystem(shell);
38+
IFigure panel = new Figure();
39+
lws.setContents(panel);
40+
ScrollPane scrollpane = new ScrollPane();
41+
scrollpane.setBounds(new Rectangle(30,30,210,200));
42+
scrollpane.getViewport().setBorder(new GroupBoxBorder("Viewport"));
43+
scrollpane.setBorder(new GroupBoxBorder("ScrollPane"));
44+
45+
final Figure view = new Figure();
46+
view.setBorder(new GroupBoxBorder("The View"));
47+
view.setLayoutManager(new FlowLayout(false));
48+
scrollpane.setContents(view);
49+
50+
Clickable button = new Button("Create checkbox");
51+
button.setBounds(new Rectangle(30,250,140,35));
52+
53+
button.addActionListener(new ActionListener(){
54+
public void actionPerformed(ActionEvent e){
55+
view.add(new CheckBox("Checkbox "+count++));
56+
}
57+
});
58+
59+
panel.add(button);
60+
panel.add(scrollpane);
61+
62+
Display display = Display.getDefault();
63+
while (!shell.isDisposed()) {
64+
if (!display.readAndDispatch()) {
65+
display.sleep ();
66+
}
67+
}
68+
}
69+
}
70+
----
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
ifdef::env-github[]
2+
:imagesdir: ../../guide/demos/
3+
endif::[]
4+
5+
= Example 4 - Connections and Anchors
6+
7+
image:images/demo4.gif[image]
8+
9+
This example shows how to use Connections and ConnectionAnchors. A connection is
10+
a figure that visually connects two anchors. The anchors are usually places on
11+
figures which the connection is linking.
12+
13+
The red and blue "nodes" can be dragged around with the mouse. The connection
14+
will re-route itself automatically. The connections children, the "Midpoint"
15+
label and the arrowhead, will also update.
16+
17+
The connection implementation used here is a PolylineConnection. This
18+
implementation supports decorations, such as arrowheads. PolygonDecorations
19+
added to the connection will be rotated correctly by a Locator. Other
20+
decorations can be added with their own locators, such as the "Midpoint" label
21+
in the demo.
22+
23+
[source,java]
24+
----
25+
import org.eclipse.swt.widgets.Shell;
26+
import org.eclipse.swt.widgets.Display;
27+
import org.eclipse.draw2d.*;
28+
import org.eclipse.draw2d.geometry.*;
29+
30+
public class Demo4 {
31+
32+
public static void main(String args[]) {
33+
Shell shell = new Shell();
34+
shell.setSize(350,350);
35+
shell.open();
36+
shell.setText("Demo 4");
37+
LightweightSystem lws = new LightweightSystem(shell);
38+
IFigure panel = new Figure();
39+
lws.setContents(panel);
40+
RectangleFigure node1 = new RectangleFigure();
41+
RectangleFigure node2 = new RectangleFigure();
42+
node1.setBackgroundColor(ColorConstants.red);
43+
node1.setSize(64, 36);
44+
node2.setBackgroundColor(ColorConstants.blue);
45+
node2.setBounds(new Rectangle(100, 100, 64, 36));
46+
47+
PolylineConnection conn = new PolylineConnection();
48+
conn.setSourceAnchor(new ChopboxAnchor(node1));
49+
conn.setTargetAnchor(new ChopboxAnchor(node2));
50+
conn.setTargetDecoration(new PolygonDecoration());
51+
52+
Label label = new Label("Midpoint");
53+
label.setOpaque(true);
54+
label.setBackgroundColor(ColorConstants.buttonLightest);
55+
label.setBorder(new LineBorder());
56+
conn.add(label, new MidpointLocator(conn, 0));
57+
58+
panel.add(node1);
59+
panel.add(node2);
60+
panel.add(conn);
61+
new Dragger(node1);
62+
new Dragger(node2);
63+
Display display = Display.getDefault();
64+
while (!shell.isDisposed()) {
65+
if (!display.readAndDispatch()) {
66+
display.sleep ();
67+
}
68+
}
69+
}
70+
71+
static class Dragger extends MouseMotionListener.Stub implements MouseListener {
72+
public Dragger(IFigure figure){
73+
figure.addMouseMotionListener(this);
74+
figure.addMouseListener(this);
75+
}
76+
Point last;
77+
public void mouseReleased(MouseEvent e){}
78+
public void mouseClicked(MouseEvent e){}
79+
public void mouseDoubleClicked(MouseEvent e){}
80+
public void mousePressed(MouseEvent e){
81+
last = e.getLocation();
82+
}
83+
public void mouseDragged(MouseEvent e){
84+
Point p = e.getLocation();
85+
Dimension delta = p.getDifference(last);
86+
last = p;
87+
Figure f = ((Figure)e.getSource());
88+
f.setBounds(f.getBounds().getTranslated(delta.width, delta.height));
89+
}
90+
};
91+
}
92+
----
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
ifdef::env-github[]
2+
:imagesdir: ../../guide/demos/
3+
endif::[]
4+
5+
= Draw2d Examples
6+
7+
- xref:demo1.adoc[Hello World]
8+
- xref:demo2.adoc[Layout Managers]
9+
- xref:demo3.adoc[Clicking and Scrolling]
10+
- xref:demo4.adoc[Connections and Anchors]

org.eclipse.draw2d.doc.isv/guide-src/overview.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ composed via a parent-child relationship. Every figure has a rectangular
1313
*_bounds_* inside which it, and its children, paint. A layout manager
1414
can be used to place children based on their index and/or constraint.
1515

16-
image:images/system.gif[image]
16+
image:images/draw2d.gif[image]
1717

1818
A `LightweightSystem` associates a figure composition with an SWT
1919
Canvas. The lightweight system hooks listeners for most SWT events, and
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)