|
Re: How to design for competing use of popup menu on StyledText? [message #494244 is a reply to message #494066] |
Thu, 29 October 2009 17:08 |
Grant Gayed Messages: 2150 Registered: July 2009 |
Senior Member |
|
|
Hi,
There isn't an automated way to merge menus at the swt level. The snippet
below demonstrates how to do it manually, and I think it should be fine for
your case unless someone else is also adding their menu items asynchronously
(seems unlikely).
public class Main0 {
static int counter = 0;
public static void main (String [] args) {
final String ID_SPELLCHECKER = "SPELLCHECKER";
final Display display = new Display ();
final Shell shell = new Shell (display);
shell.setBounds(10,10,200,250);
shell.setLayout(new FillLayout());
final StyledText st = new StyledText(shell, SWT.NONE);
Menu menu = new Menu(st);
new MenuItem(menu, SWT.NONE).setText("static item");
st.setMenu(menu);
st.addListener(SWT.MenuDetect, new Listener() {
public void handleEvent(Event event) {
/* asyncExec so that all other MenuDetects run first */
display.asyncExec(new Runnable() {
public void run() {
if (st.isDisposed()) return;
Menu menu = st.getMenu();
MenuItem item = (MenuItem)menu.getData(ID_SPELLCHECKER);
if (item != null) {
int index = menu.indexOf(item);
if (index != menu.getItemCount() - 1) {
/* move it to the bottom of the menu */
item.dispose();
item = null;
}
}
if (item == null) {
item = new MenuItem(menu, SWT.NONE);
item.setText("check spelling");
menu.setData(ID_SPELLCHECKER, item);
}
}
});
}
});
st.addListener(SWT.MenuDetect, new Listener() {
public void handleEvent(Event event) {
Menu menu = ((Control)event.widget).getMenu();
new MenuItem(menu, SWT.NONE).setText("dynamic menu item " +
counter++);
}
});
shell.open();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
Grant
"Gerald Rosenberg" <no@dr.ess> wrote in message
news:MPG.2552c65e4116a36e9896c7@news.eclipse.org...
> Building a small UI component framework. One of the components is a
> spellchecker. When associated with a StyledText it will add a a right
> click/popup menu. Since this is a framework, I won't have exclusive
> control over other components that might add popup menus to the
> StyledText.
>
> Is there a recommended, or at least practical way to manage the
> potential conflict? Any way to do a contolled merge of popup menu
> contributions?
|
|
|
|
Powered by
FUDForum. Page generated in 0.03481 seconds