| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
import gls.CompilableTestSupport |
| 20 |
|
| 21 |
class DesignPatternsTest extends CompilableTestSupport { |
| 22 |
|
| 23 |
void testSimpleExample() { |
| 24 |
shouldCompile ''' |
| 25 |
// tag::simple_example[] |
| 26 |
import groovy.swing.SwingBuilder |
| 27 |
import java.awt.BorderLayout as BL |
| 28 |
|
| 29 |
count = 0 |
| 30 |
new SwingBuilder().edt { |
| 31 |
frame(title: 'Frame', size: [300, 300], show: true) { |
| 32 |
borderLayout() |
| 33 |
textlabel = label(text: 'Click the button!', constraints: BL.NORTH) |
| 34 |
button(text:'Click Me', |
| 35 |
actionPerformed: {count++; textlabel.text = "Clicked ${count} time(s)."; println "clicked"}, constraints:BL.SOUTH) |
| 36 |
} |
| 37 |
} |
| 38 |
// end::simple_example[] |
| 39 |
''' |
| 40 |
} |
| 41 |
|
| 42 |
void testMoreInvolvedExample() { |
| 43 |
shouldCompile ''' |
| 44 |
// tag::more_involved_example[] |
| 45 |
import groovy.swing.SwingBuilder |
| 46 |
import javax.swing.* |
| 47 |
import java.awt.* |
| 48 |
|
| 49 |
def swing = new SwingBuilder() |
| 50 |
|
| 51 |
def sharedPanel = { |
| 52 |
swing.panel() { |
| 53 |
label("Shared Panel") |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
count = 0 |
| 58 |
swing.edt { |
| 59 |
frame(title: 'Frame', defaultCloseOperation: JFrame.EXIT_ON_CLOSE, pack: true, show: true) { |
| 60 |
vbox { |
| 61 |
textlabel = label('Click the button!') |
| 62 |
button( |
| 63 |
text: 'Click Me', |
| 64 |
actionPerformed: { |
| 65 |
count++ |
| 66 |
textlabel.text = "Clicked ${count} time(s)." |
| 67 |
println "Clicked!" |
| 68 |
} |
| 69 |
) |
| 70 |
widget(sharedPanel()) |
| 71 |
widget(sharedPanel()) |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
// end::more_involved_example[] |
| 76 |
''' |
| 77 |
} |
| 78 |
|
| 79 |
void testObservableBindingExample() { |
| 80 |
shouldCompile ''' |
| 81 |
// tag::observable_binding_example[] |
| 82 |
import groovy.swing.SwingBuilder |
| 83 |
import groovy.beans.Bindable |
| 84 |
|
| 85 |
class MyModel { |
| 86 |
@Bindable int count = 0 |
| 87 |
} |
| 88 |
|
| 89 |
def model = new MyModel() |
| 90 |
new SwingBuilder().edt { |
| 91 |
frame(title: 'Java Frame', size: [100, 100], locationRelativeTo: null, show: true) { |
| 92 |
gridLayout(cols: 1, rows: 2) |
| 93 |
label(text: bind(source: model, sourceProperty: 'count', converter: { v -> v? "Clicked $v times": ''})) |
| 94 |
button('Click me!', actionPerformed: { model.count++ }) |
| 95 |
} |
| 96 |
} |
| 97 |
// end::observable_binding_example[] |
| 98 |
''' |
| 99 |
} |
| 100 |
} |