Mercurial > projects > dwt2
comparison org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet005MenuUpdater.d @ 90:6086085e153d
Added databinding snippets. unmodified java sources.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 19 Apr 2009 11:33:55 +0200 |
parents | |
children | 5d5bd660917f |
comparison
equal
deleted
inserted
replaced
89:0ca1aee7decf | 90:6086085e153d |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2006, 2007 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Brad Reynolds - bug 116920 | |
11 *******************************************************************************/ | |
12 package org.eclipse.jface.examples.databinding.snippets; | |
13 | |
14 import java.util.Date; | |
15 import java.util.Iterator; | |
16 | |
17 import org.eclipse.core.databinding.observable.Realm; | |
18 import org.eclipse.core.databinding.observable.list.WritableList; | |
19 import org.eclipse.jface.databinding.swt.SWTObservables; | |
20 import org.eclipse.jface.internal.databinding.provisional.swt.MenuUpdater; | |
21 import org.eclipse.swt.SWT; | |
22 import org.eclipse.swt.widgets.Display; | |
23 import org.eclipse.swt.widgets.Menu; | |
24 import org.eclipse.swt.widgets.MenuItem; | |
25 import org.eclipse.swt.widgets.Shell; | |
26 | |
27 /** | |
28 */ | |
29 public class Snippet005MenuUpdater { | |
30 public static void main(String[] args) { | |
31 final Display display = new Display(); | |
32 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { | |
33 public void run() { | |
34 Shell shell = new Shell(display); | |
35 | |
36 final WritableList menuItemStrings = new WritableList(); | |
37 display.asyncExec(new Runnable() { | |
38 public void run() { | |
39 System.out.println("adding item"); | |
40 menuItemStrings.add(new Date().toString()); | |
41 display.timerExec(5000, this); | |
42 } | |
43 }); | |
44 | |
45 Menu bar = new Menu(shell, SWT.BAR); | |
46 shell.setMenuBar(bar); | |
47 MenuItem fileItem = new MenuItem(bar, SWT.CASCADE); | |
48 fileItem.setText("&Test Menu"); | |
49 final Menu submenu = new Menu(shell, SWT.DROP_DOWN); | |
50 fileItem.setMenu(submenu); | |
51 new MenuUpdater(submenu) { | |
52 protected void updateMenu() { | |
53 System.out.println("updating menu"); | |
54 MenuItem[] items = submenu.getItems(); | |
55 int itemIndex = 0; | |
56 for (Iterator it = menuItemStrings.iterator(); it | |
57 .hasNext();) { | |
58 MenuItem item; | |
59 if (itemIndex < items.length) { | |
60 item = items[itemIndex++]; | |
61 } else { | |
62 item = new MenuItem(submenu, SWT.NONE); | |
63 } | |
64 String string = (String) it.next(); | |
65 item.setText(string); | |
66 } | |
67 while (itemIndex < items.length) { | |
68 items[itemIndex++].dispose(); | |
69 } | |
70 } | |
71 }; | |
72 | |
73 shell.open(); | |
74 | |
75 // The SWT event loop | |
76 while (!shell.isDisposed()) { | |
77 if (!display.readAndDispatch()) { | |
78 display.sleep(); | |
79 } | |
80 } | |
81 } | |
82 }); | |
83 display.dispose(); | |
84 } | |
85 } |