root/incubator/SimpleGesture/WebContent/WEB-INF/src/com/itmill/incubator/simplegesture/SimpleGestureDemo.java

Revision 7521, 8.2 KB (checked in by marc.englund@…, 11 months ago)

SimpleGesture?: initial commit.

Line 
1/*
2 * Copyright 2009 Marc Englund <marc.englundATitmill.com>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.itmill.incubator.simplegesture;
18
19import com.itmill.incubator.image.Image;
20import com.itmill.incubator.simplegesture.SimpleGesture.GestureEvent;
21import com.itmill.toolkit.Application;
22import com.itmill.toolkit.data.Property;
23import com.itmill.toolkit.data.Property.ValueChangeEvent;
24import com.itmill.toolkit.data.util.IndexedContainer;
25import com.itmill.toolkit.terminal.ClassResource;
26import com.itmill.toolkit.terminal.ExternalResource;
27import com.itmill.toolkit.ui.Button;
28import com.itmill.toolkit.ui.Embedded;
29import com.itmill.toolkit.ui.HorizontalLayout;
30import com.itmill.toolkit.ui.Label;
31import com.itmill.toolkit.ui.Link;
32import com.itmill.toolkit.ui.NativeSelect;
33import com.itmill.toolkit.ui.Panel;
34import com.itmill.toolkit.ui.TextField;
35import com.itmill.toolkit.ui.VerticalLayout;
36import com.itmill.toolkit.ui.Window;
37import com.itmill.toolkit.ui.Button.ClickEvent;
38
39public class SimpleGestureDemo extends Application {
40
41        Image img = new Image(
42                        "http://farm1.static.flickr.com/77/206644103_5bbbb9751d_o.jpg",
43                        true);
44
45        Command CMD_FLIP_HORIZ = new Command("Flip Horizontal", img) {
46                public void run() {
47                        ((Image) this.target).flipHorizontal();
48                }
49        };
50        Command CMD_TOGGLE_THEME = new Command("Toggle theme",
51                        SimpleGestureDemo.this) {
52                public void run() {
53                        if ("mork".equals(((Application) this.target).getTheme())) {
54                                ((Application) this.target).setTheme(null);
55                        } else {
56                                ((Application) this.target).setTheme("mork");
57                        }
58                }
59        };
60        Command CMD_REVERT = new Command("Revert", img) {
61                public void run() {
62                        ((Image) this.target).revert();
63                }
64        };
65        Command CMD_ROTATE_CW = new Command("Rotate CW", img) {
66                public void run() {
67                        ((Image) this.target).rotate(45, false);
68                }
69        };
70        Command CMD_ROTATE_CCW = new Command("Rotate CCW", img) {
71                public void run() {
72                        ((Image) this.target).rotate(-45, false);
73                }
74        };
75        Command CMD_ROUND = new Command("Round", img) {
76                public void run() {
77                        ((Image) this.target).roundCorners(50);
78                }
79        };
80        Command CMD_SHADOW = new Command("Shadow", img) {
81                public void run() {
82                        ((Image) this.target).shadow(135, 5);
83                }
84        };
85
86        @Override
87        public void init() {
88                setMainWindow(new MainWindow());
89                setTheme("mork");
90        }
91
92        public class MainWindow extends Window {
93                SimpleGesture sg;
94                // ClassResource res = new
95                // ClassResource("image.jpg",SimpleGestureDemo.this);
96
97                IndexedContainer commands = new IndexedContainer();
98
99                public MainWindow() {
100                        super("SimpleGesture demo");
101                        setSizeFull();
102                        VerticalLayout vl = ((VerticalLayout) getLayout());
103                        // vl.setSpacing(true);
104                        vl.setMargin(false);
105                        vl.setSizeFull();
106
107                        //
108                        commands.addItem(CMD_TOGGLE_THEME);
109                        commands.addItem(CMD_FLIP_HORIZ);
110                        commands.addItem(CMD_REVERT);
111                        commands.addItem(CMD_ROTATE_CW);
112                        commands.addItem(CMD_ROTATE_CCW);
113                        commands.addItem(CMD_ROUND);
114                        commands.addItem(CMD_SHADOW);
115
116                        sg = new SimpleGesture();
117                        addComponent(sg);
118
119                        Panel p = new Panel();
120                        p.setStyleName(Panel.STYLE_LIGHT);
121                        p.setWidth("100%");
122                        addComponent(p);
123                        final HorizontalLayout l = new HorizontalLayout();
124                        l.setWidth("100%");
125                        p.setLayout(l);
126                        // l.setMargin(true);
127                        // l.setSpacing(true);
128
129                        final Button add = new Button("Add gesture",
130                                        new Button.ClickListener() {
131                                                public void buttonClick(ClickEvent event) {
132                                                        addGesture();
133                                                }
134                                        });
135                        p.addComponent(add);
136
137                        final Button record = new Button("Record gesture",
138                                        new Button.ClickListener() {
139                                                public void buttonClick(ClickEvent event) {
140                                                        sg.setRecordMode(true);
141                                                        l.setEnabled(false);
142                                                }
143                                        });
144                        p.addComponent(record);
145                        commands.addItem(new Command("Record", img) {
146                                public void run() {
147                                        sg.setRecordMode(true);
148                                        l.setEnabled(false);
149                                }
150                        });
151
152                        Embedded hint = new Embedded();
153                        hint
154                                        .setSource(new ClassResource("hint.png",
155                                                        SimpleGestureDemo.this));
156                        p.addComponent(hint);
157                        l.setExpandRatio(hint, 1);
158                        l.setComponentAlignment(hint, "middle center");
159
160                        Link flickr = new Link("flickr", new ExternalResource(
161                                        "http://www.flickr.com/photos/emarc/206644103/"));
162                        p.addComponent(flickr);
163                        l.setComponentAlignment(flickr, "middle center");
164
165                        final Button reset = new Button("Reset gestures",
166                                        new Button.ClickListener() {
167                                                public void buttonClick(ClickEvent event) {
168                                                        showNotification("Gestures reset");
169                                                        reset();
170                                                }
171
172                                        });
173                        p.addComponent(reset);
174
175                        final Button restart = new Button("Restart application",
176                                        new Button.ClickListener() {
177                                                public void buttonClick(ClickEvent event) {
178                                                        SimpleGestureDemo.this.close();
179                                                }
180
181                                        });
182                        p.addComponent(restart);
183
184                        sg.addListener(new SimpleGesture.GestureListener() {
185
186                                public void gesture(GestureEvent event) {
187                                        if (sg.isRecordMode()) {
188                                                sg.setRecordMode(false);
189                                                l.setEnabled(true);
190                                                addGesture(event.getMoved(),
191                                                                (event.getDistance() < 10000 ? ""
192                                                                                + event.getData() : null));
193                                        } else {
194                                                showNotification(event.getData().toString());
195                                                ((Command) event.getData()).run();
196
197                                        }
198
199                                }
200
201                        });
202
203                        addComponent(img);
204                        vl.setExpandRatio(img, 1.0f);
205                        vl.setComponentAlignment(img, "center middle");
206
207                        reset();
208
209                }
210
211                private void reset() {
212                        sg.clear();
213                        sg.addGesture("5443", CMD_ROTATE_CCW);
214                        sg.addGesture("7001", CMD_ROTATE_CW);
215                        sg.addGesture("321076543", CMD_ROUND);
216                        sg.addGesture("43210134", CMD_SHADOW);
217                }
218
219                private void addGesture() {
220                        addGesture(null, null);
221                }
222
223                private void addGesture(String gesture, String close) {
224                        final Window w = new Window("Add gesture");
225                        w.setWidth("400px");
226                        w.setHeight("270px");
227                        addWindow(w);
228                        w.center();
229
230                        ((VerticalLayout) w.getLayout()).setMargin(true);
231                        ((VerticalLayout) w.getLayout()).setSpacing(true);
232                        ((VerticalLayout) w.getLayout()).setSizeFull();
233
234                        if (close != null) {
235                                w.addComponent(new Label(
236                                                "Note that this gesture is quite similar to \"" + close
237                                                                + "\""));
238                        }
239
240                        HorizontalLayout controls = new HorizontalLayout();
241                        controls.setSpacing(true);
242                        w.addComponent(controls);
243                        final TextField g = new TextField("Gesture", gesture);
244                        g.setNullRepresentation("");
245                        controls.addComponent(g);
246                        final NativeSelect c = new NativeSelect("Command", commands);
247                        c.setNullSelectionAllowed(false);
248                        c.setImmediate(true);
249                        controls.addComponent(c);
250
251                        HorizontalLayout buttons = new HorizontalLayout();
252                        buttons.setMargin(true, false, false, false);
253                        buttons.setSpacing(true);
254                        buttons.setWidth("100%");
255                        w.addComponent(buttons);
256                        Embedded hint = new Embedded();
257                        hint
258                                        .setSource(new ClassResource("directions.png",
259                                                        SimpleGestureDemo.this));
260                        buttons.addComponent(hint);
261                        buttons.setExpandRatio(hint, 1);
262
263                        Button cancel = new Button("Cancel", new Button.ClickListener() {
264                                public void buttonClick(ClickEvent event) {
265                                        MainWindow.this.removeWindow(w);
266                                }
267                        });
268                        cancel.setStyleName(Button.STYLE_LINK);
269                        buttons.addComponent(cancel);
270                        buttons.setComponentAlignment(cancel, "bottom");
271
272                        final Button add = new Button("Add", new Button.ClickListener() {
273
274                                public void buttonClick(ClickEvent event) {
275                                        MainWindow.this.removeWindow(w);
276                                        sg.addGesture((String) g.getValue(), c.getValue());
277                                }
278
279                        });
280                        buttons.addComponent(add);
281                        buttons.setComponentAlignment(add, "bottom");
282                        add.setEnabled(false);
283                        c.addListener(new Property.ValueChangeListener() {
284                                public void valueChange(ValueChangeEvent event) {
285                                        add.setEnabled(true);
286                                }
287                        });
288
289                }
290
291        }
292
293        abstract class Command {
294                protected String name;
295                protected Object target;
296
297                Command(String name, Object target) {
298                        this.name = name;
299                        this.target = target;
300                }
301
302                public String toString() {
303                        return name;
304                }
305
306                abstract public void run();
307        }
308
309}
Note: See TracBrowser for help on using the browser.