下面列出了org.eclipse.swt.widgets.MenuItem#addListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Create the menu with "Delete", "Modify" Item.
*
* @param parent
* @return
*/
protected Menu createMenu(Control parent) {
Menu menu = new Menu(parent);
// "Delete" menu item.
deleteItem = new MenuItem(menu, SWT.NONE);
deleteItem.setText(resources.getString(PICTURE_CONTROL_DELETE));
deleteItem.addListener(SWT.Selection, e -> {
// Delete the image.
AbstractPictureControl.this.handleDeleteImage();
});
// "Modify" menu item.
final MenuItem modifyItem = new MenuItem(menu, SWT.NONE);
modifyItem.setText(resources.getString(PICTURE_CONTROL_MODIFY));
modifyItem.addListener(SWT.Selection, e -> {
// Modify the image.
AbstractPictureControl.this.handleModifyImage();
});
return menu;
}
public Menu getDefaultMenu() {
Menu menu = new Menu(sText.getShell());
MenuItem cut = new MenuItem(menu, SWT.NONE);
cut.setText(XViewerText.get("menu.cut")); //$NON-NLS-1$
cut.addListener(SWT.Selection, e-> {
sText.cut();
sText.redraw();
});
MenuItem copy = new MenuItem(menu, SWT.NONE);
copy.setText(XViewerText.get("menu.copy")); //$NON-NLS-1$
copy.addListener(SWT.Selection, e-> sText.copy());
MenuItem paste = new MenuItem(menu, SWT.NONE);
paste.setText(XViewerText.get("menu.paste")); //$NON-NLS-1$
paste.addListener(SWT.Selection, e-> {
sText.paste();
sText.redraw();
});
return menu;
}
@Override
protected void insertAction(final Type type, Menu parentMenu) {
MenuItem actionItem = new MenuItem(parentMenu, SWT.CHECK);
actionItem.setText(type.getName());
if (type.equals(editor.getAnnotationMode()))
actionItem.setSelection(true);
actionItem.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event e) {
for (IModeMenuListener listener : listeners) {
listener.modeChanged(type);
}
}
});
}
@Override
public void fill(Menu parentMenu, int index) {
CAS cas = casEditor.getDocument().getCAS();
for (Iterator<CAS> it = cas.getViewIterator(); it.hasNext(); ) {
CAS casView = it.next();
final String viewName = casView.getViewName();
final MenuItem actionItem = new MenuItem(parentMenu, SWT.CHECK);
actionItem.setText(viewName);
// TODO: Disable non-text views, check mime-type
try {
actionItem.setEnabled(cas.getDocumentText() != null);
} catch (Throwable t) {
// TODO: Not nice, discuss better solution on ml
actionItem.setEnabled(false);
}
// TODO: Add support for non text views, editor has
// to display some error message
if (cas.getViewName().equals(viewName))
actionItem.setSelection(true);
// TODO: move this to an action
actionItem.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event e) {
// Trigger only if view is really changed
// TODO: Move this check to the document itself ...
if(!casEditor.getDocument().getCAS().getViewName().equals(viewName)) {
casEditor.showView(viewName);
}
}
});
}
}
@Override
protected void insertAction(final Type type, Menu parentMenu) {
final MenuItem actionItem = new MenuItem(parentMenu, SWT.CHECK);
actionItem.setText(type.getName());
// TODO: find another way to select the annotation mode also
if (editorAnnotationMode != null && editorAnnotationMode.equals(type)) {
actionItem.setSelection(true);
}
if (typesToDisplay.contains(type)) {
actionItem.setSelection(true);
}
// TODO: move this to an action
// do not access mTypesToDisplay directly !!!
actionItem.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event e) {
if (actionItem.getSelection()) {
typesToDisplay.add(type);
} else {
typesToDisplay.remove(type);
}
fireChanged();
}
});
}
public static void addFileMenuItems(Menu menu) {
new MenuItem(menu, SWT.SEPARATOR);
MenuItem preferences = new MenuItem(menu, SWT.PUSH);
preferences.setText("Preferences...");
preferences.addListener(SWT.Selection, preferencesListener);
MenuItem exit = new MenuItem(menu, SWT.PUSH);
exit.setText("Exit " + appName);
exit.addListener(SWT.Selection, exitListener);
}
public void createMenu(Shell shell) {
Menu bar = new Menu(shell, SWT.BAR);
shell.setMenuBar(bar);
MenuItem fileItem = new MenuItem(bar, SWT.CASCADE);
fileItem.setText("&File");
Menu submenu = new Menu(shell, SWT.DROP_DOWN);
fileItem.setMenu(submenu);
MenuItem item = new MenuItem(submenu, SWT.PUSH);
item.addListener(SWT.Selection, e -> Runtime.getRuntime().halt(0));
item.setText("E&xit\tCtrl+W");
item.setAccelerator(SWT.MOD1 + 'W');
}
/**
* Creates the header of the calendar. The header contains the label
* displaying the current month and year, and the two buttons for navigation :
* previous and next month.
*/
private void createHeader() {
monthPanel = new Composite(this, SWT.NONE);
GridLayoutFactory.fillDefaults().numColumns(3).spacing(HEADER_SPACING, 0).margins(HEADER_SPACING, 2).applyTo(monthPanel);
GridDataFactory.fillDefaults().applyTo(monthPanel);
monthPanel.addListener(SWT.MouseDown, listener);
prevMonth = new Button(monthPanel, SWT.ARROW | SWT.LEFT | SWT.FLAT);
prevMonth.addListener(SWT.MouseUp, listener);
prevMonth.addListener(SWT.FocusIn, listener);
currentMonth = new Label(monthPanel, SWT.CENTER);
currentMonth.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
currentMonth.addListener(SWT.MouseDown, listener);
nextMonth = new Button(monthPanel, SWT.ARROW | SWT.RIGHT | SWT.FLAT);
nextMonth.addListener(SWT.MouseUp, listener);
nextMonth.addListener(SWT.FocusIn, listener);
monthsMenu = new Menu(getShell(), SWT.POP_UP);
currentMonth.setMenu(monthsMenu);
for (int i = 0; i < 12; i++) {
final MenuItem item = new MenuItem(monthsMenu, SWT.PUSH);
item.addListener(SWT.Selection, listener);
item.setData(new Integer(i));
}
monthsMenu.addListener(SWT.Show, listener);
}
@Override
protected void
addMenuItems(
Menu menu )
{
new MenuItem( menu, SWT.SEPARATOR );
MenuItem mi_reset = new MenuItem( menu, SWT.PUSH );
mi_reset.setText( MessageText.getString( "label.clear.history" ));
mi_reset.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event e) {
try{
this_mon.enter();
nbValues = 0;
currentPosition = 0;
for ( int i=0;i<all_values.length;i++ ){
all_values[i] = new int[all_values[i].length];
}
startTime = -1;
}finally{
this_mon.exit();
}
refresh( true );
}
});
}
public static void addFileMenuItems(Menu menu) {
new MenuItem(menu, SWT.SEPARATOR);
MenuItem preferences = new MenuItem(menu, SWT.PUSH);
preferences.setText("Preferences...");
preferences.addListener(SWT.Selection, preferencesListener);
MenuItem exit = new MenuItem(menu, SWT.PUSH);
exit.setText("Exit " + appName);
exit.addListener(SWT.Selection, exitListener);
}
void onContextualMenu(final Event event) {
final int x = event.x;
final int y = event.y;
for (int i = 0; i < itemCount; i++) {
final ParameterExpandItem item = items[i];
final boolean hover = item.x <= x && x < item.x + item.width && item.y <= y && y < item.y + bandHeight;
if (!hover) {
continue;
}
if (underlyingObjects != null) {
ignoreMouseUp = true;
final Point p = toDisplay(x, y);
final Map<String, Runnable> menuContents = underlyingObjects.handleMenu(item.getData(), p.x, p.y);
if (menuContents == null) {
return;
} else {
final Menu menu = new Menu(getShell(), SWT.POP_UP);
for (final Map.Entry<String, Runnable> entry : menuContents.entrySet()) {
final MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
menuItem.setText(entry.getKey());
menuItem.addListener(SWT.Selection, e -> entry.getValue().run());
}
menu.setLocation(p.x, p.y);
menu.setVisible(true);
while (!menu.isDisposed() && menu.isVisible()) {
if (!WorkbenchHelper.getDisplay().readAndDispatch()) {
WorkbenchHelper.getDisplay().sleep();
}
}
menu.dispose();
}
}
}
}
@Override
protected void
addMenuItems(
Menu menu )
{
new MenuItem( menu, SWT.SEPARATOR );
MenuItem mi_reset = new MenuItem( menu, SWT.PUSH );
mi_reset.setText( MessageText.getString( "label.clear.history" ));
mi_reset.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event e) {
try{
this_mon.enter();
nbValues = 0;
currentPosition = 0;
for ( int i=0;i<all_values.length;i++ ){
all_values[i] = new int[all_values[i].length];
}
}finally{
this_mon.exit();
}
refresh( true );
}
});
}
public void createViewSelectedCellMenuItem(Menu popupMenu) {
setupActions();
final MenuItem item = new MenuItem(popupMenu, SWT.CASCADE);
item.setText(XViewerText.get("menu.copy_celldata")); //$NON-NLS-1$
item.addListener(SWT.Selection, e->copySelectedCell.run());
final MenuItem item1 = new MenuItem(popupMenu, SWT.CASCADE);
item1.setText(XViewerText.get("menu.view_celldata")); //$NON-NLS-1$
item1.addListener(SWT.Selection, e->viewSelectedCell.run());
}
public Menu pageOverviewGetPopup() {
Menu menu = new Menu(b.getShell());
MenuItem item = new MenuItem(menu, SWT.NONE);
item.setText(XViewerText.get("HtmlDialog.menu.view_source")); //$NON-NLS-1$
item.addListener(SWT.Selection, e-> {
String file = System.getProperty("user.home") + File.separator + "out.html"; //$NON-NLS-1$ //$NON-NLS-2$
try {
XViewerLib.writeStringToFile(html, new File(file));
} catch (IOException ex) {
XViewerLog.logAndPopup(Activator.class, Level.SEVERE, ex);
}
Program.launch(file);
});
return menu;
}
/**
* Populates the client's menu bar
* @param locales
* @param parent
*/
private static void createViewInfoMenuItem(Menu parent,
UISWTViewBuilderCore builder) {
MenuItem item = new MenuItem(parent, SWT.NULL);
item.setText(builder.getInitialTitle());
item.setData("ViewID", builder.getViewID());
item.addListener(SWT.Selection, e -> {
UIFunctionsSWT uiFunctions = UIFunctionsManagerSWT.getUIFunctionsSWT();
if (uiFunctions != null) {
uiFunctions.openPluginView(builder, true);
}
});
}
@Override
protected void
addMenuItems(
Menu menu )
{
new MenuItem( menu, SWT.SEPARATOR );
MenuItem mi_reset = new MenuItem( menu, SWT.PUSH );
mi_reset.setText( MessageText.getString( "label.clear.history" ));
mi_reset.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event e) {
try{
this_mon.enter();
nbValues = 0;
currentPosition = 0;
for ( int i=0;i<all_values.length;i++ ){
all_values[i] = new int[all_values[i].length];
}
}finally{
this_mon.exit();
}
refresh( true );
}
});
}
/**
* Creates a link menu item. Adds a listener to the item, the listener
* adds a child node to the applicationLayout. If <code>createChild</code> is
* true then a child will be added to the currently selected node.
*
* @param menu
* the parent menu item
* @param label
* the label of the menu item
* @param def
* the FacesDefinition of the TreeNode represented by the menu item
* @param createChild
* a flag that determines whether or not the TreeNode being inserted should be inserted as a
* child of another TreeNode or as a child of the applicationLayout tag.
*/
protected void addRulesMemuItem(Menu menu, final String label, final FacesDefinition def, final boolean createChild) {
MenuItem item = new MenuItem(menu, SWT.PUSH);
item.setText(label);
item.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
Object newObject = null;
Object parentObj = null;
DataNode dn = DCUtils.findDataNode(leftChild, true);
IMember member = dn.getMember("rules"); // $NON-NLS-1$
ILoader loader = dn.getLoader();
IClassDef classDef;
try {
classDef = loader.loadClass(def.getNamespaceUri(), def.getTagName());
parentObj = dn.getCurrentObject();
newObject = classDef.newInstance(parentObj);
// if we're creating a child node, get the parent from the tree selection
if(createChild){
// this DN is set when selection changes in the tree
DataNode dnParent = DCUtils.findDataNode(cmplxComposite, true);
// find the first member of type "ITreeNode"
IMember parentMember = RedirectRulesPanel.this.findRulesNodeMember(dnParent.getCurrentObject(), loader, null);
// and change whose collection we'll add to
if (parentMember instanceof ICollection) {
parentObj = dnParent.getCurrentObject();
member = parentMember;
}
}
IObjectCollection col = loader.getObjectCollection(parentObj, (ICollection) member);
col.add(col.size(), newObject, null);
// add a default label if appropriate
if(def.getProperty(EXT_LIB_ATTR_LABEL) != null && newObject instanceof Element){
//TODO DAN - Should use data node here.. should avoid instanceof Element if possible.
if(def.getProperty(IExtLibAttrNames.EXT_LIB_ATTR_VAR) != null){
String var = getLinkDisplayName(def);
if(StringUtil.isEmpty(var)){
var = "var"; // $NON-NLS-1$
}
else{
char[] cs = var.toCharArray();
if(cs != null){
StringBuffer buff = new StringBuffer();
for(char c : cs){
if(!Character.isSpaceChar(c)){
buff.append(c);
}
}
var = buff.toString();
}
}
String[] vars = XPagesDOMUtil.getVars(((Element)newObject).getOwnerDocument(), null);
var = XPagesDOMUtil.generateUniqueVar(Arrays.asList(vars), (Element)newObject, var);
XPagesDOMUtil.setAttribute((Element)newObject, IExtLibAttrNames.EXT_LIB_ATTR_VAR, var);
}
String newLabel = generateNewLabel(def);
XPagesDOMUtil.setAttribute((Element)newObject, EXT_LIB_ATTR_LABEL, newLabel);
}
} catch (NodeException e) {
if (ExtLibToolingLogger.EXT_LIB_TOOLING_LOGGER.isErrorEnabled()) {
ExtLibToolingLogger.EXT_LIB_TOOLING_LOGGER.errorp(this, "addLinkMenuItem", e, "Failed to create new {0}", // $NON-NLS-1$ $NLE-AbstractTreeNodePanel.Failedtocreatenew0-2$
"rules"); // $NON-NLS-1$
}
}
if(newObject != null){
ruleViewer.refresh();
if (null != parentObj){
ruleViewer.setExpandedState(parentObj, true);
}
ruleViewer.setSelection(new StructuredSelection(newObject));
}
}
});
}
public void createClearAllSortingMenuItem(Menu popupMenu) {
final MenuItem item = new MenuItem(popupMenu, SWT.CASCADE);
item.setText(XViewerText.get("menu.clear_sorts")); //$NON-NLS-1$
item.addListener(SWT.Selection, e-> xViewer.getCustomizeMgr().clearSorter());
}
/**
* Creates a link menu item. Adds a listener to the item, the listener
* adds a child node to the applicationLayout. If <code>createChild</code> is
* true then a child will be added to the currently selected node.
*
* @param menu
* the parent menu item
* @param label
* the label of the menu item
* @param def
* the FacesDefinition of the TreeNode represented by the menu item
* @param createChild
* a flag that determines whether or not the TreeNode being inserted should be inserted as a
* child of another TreeNode or as a child of the applicationLayout tag.
*/
protected void addLinkMenuItem(Menu menu, final String label, final FacesDefinition def, final boolean createChild) {
MenuItem item = new MenuItem(menu, SWT.PUSH);
item.setText(label);
item.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
Object newObject = null;
Object parentObj = null;
DataNode dn = DCUtils.findDataNode(_leftChild, true);
IMember member = dn.getMember(_descriptor.propertyName);
ILoader loader = dn.getLoader();
IClassDef classDef;
try {
classDef = loader.loadClass(def.getNamespaceUri(), def.getTagName());
parentObj = dn.getCurrentObject();
newObject = classDef.newInstance(parentObj);
// if we're creating a child node, get the parent from the tree selection
if(createChild){
// this DN is set when selection changes in the tree
DataNode dnParent = DCUtils.findDataNode(_complexComposite, true);
// find the first member of type "ITreeNode"
IMember parentMember = AbstractTreeNodePanel.this.findTreeNodeMember(dnParent.getCurrentObject(), loader, null);
// and change whose collection we'll add to
if (parentMember instanceof ICollection) {
parentObj = dnParent.getCurrentObject();
member = parentMember;
}
}
IObjectCollection col = loader.getObjectCollection(parentObj, (ICollection) member);
col.add(col.size(), newObject, null);
// add a default label if appropriate
if(def.getProperty(EXT_LIB_ATTR_LABEL) != null && newObject instanceof Element){
//TODO DAN - Should use data node here.. should avoid instanceof Element if possible.
if(def.getProperty(IExtLibAttrNames.EXT_LIB_ATTR_VAR) != null){
String var = getLinkDisplayName(def, false);
if(StringUtil.isEmpty(var)){
var = "var"; // $NON-NLS-1$
}
else{
char[] cs = var.toCharArray();
if(cs != null){
StringBuffer buff = new StringBuffer();
for(char c : cs){
if(!Character.isSpaceChar(c)){
buff.append(c);
}
}
var = buff.toString();
}
}
String[] vars = XPagesDOMUtil.getVars(((Element)newObject).getOwnerDocument(), null);
var = XPagesDOMUtil.generateUniqueVar(Arrays.asList(vars), (Element)newObject, var);
XPagesDOMUtil.setAttribute((Element)newObject, IExtLibAttrNames.EXT_LIB_ATTR_VAR, var);
}
String newLabel = generateNewLabel(def);
XPagesDOMUtil.setAttribute((Element)newObject, EXT_LIB_ATTR_LABEL, newLabel);
}
} catch (NodeException e) {
if (ExtLibToolingLogger.EXT_LIB_TOOLING_LOGGER.isErrorEnabled()) {
ExtLibToolingLogger.EXT_LIB_TOOLING_LOGGER.errorp(this, "addLinkMenuItem", e, "Failed to create new {0}", // $NON-NLS-1$ $NLE-AbstractTreeNodePanel.Failedtocreatenew0-2$
_descriptor.propertyName);
}
}
if(newObject != null){
_linkViewer.refresh();
if (null != parentObj){
_linkViewer.setExpandedState(parentObj, true);
}
_linkViewer.setSelection(new StructuredSelection(newObject));
}
}
});
}
public void createCopyCellsMenuItem(Menu popupMenu) {
final MenuItem item = new MenuItem(popupMenu, SWT.CASCADE);
item.setText(XViewerText.get("menu.copy_column")); //$NON-NLS-1$
item.addListener(SWT.Selection, e->performCopyColumnCells());
}