下面列出了org.eclipse.swt.widgets.MenuItem#setAccelerator ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @return
*/
protected Menu createMenuBar() {
Menu bar = new Menu(shell, SWT.BAR);
// file
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, new Listener() {
public void handleEvent(Event e) {
}
});
item.setText("Select &All\tCtrl+A");
item.setAccelerator(SWT.MOD1 + 'A');
// edit
MenuItem editItem = new MenuItem(bar, SWT.CASCADE);
editItem.setText("&Edit");
// search
MenuItem searchItem = new MenuItem(bar, SWT.CASCADE);
searchItem.setText("&Search");
return bar;
}
private void setMenuItemKeyboardShortcut( MenuItem menuItem, GuiMenuItem guiMenuItem ) {
// See if there's a shortcut worth mentioning...
//
KeyboardShortcut shortcut = GuiRegistry.getInstance().findKeyboardShortcut(
guiMenuItem.getListenerClassName(),
guiMenuItem.getListenerMethod(),
Const.isOSX()
);
if ( shortcut != null ) {
appendShortCut( menuItem, shortcut );
menuItem.setAccelerator( getAccelerator( shortcut ) );
shortcutMap.put( guiMenuItem.getId(), shortcut );
}
}
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');
}
private MenuItem addMenuItem(Menu menu, String text, int accelerator,
Object data, SelectionListener listener) {
MenuItem item = new MenuItem(menu, SWT.NONE);
item.setText(text);
item.addSelectionListener(listener);
if (accelerator != SWT.NONE)
item.setAccelerator(accelerator);
item.setData(data);
return item;
}
private MenuItem addMenuItem(Menu menu, String text, int accelerator,
SelectionListener listener) {
MenuItem item = addMenuItem(menu, text, listener);
if (accelerator != SWT.NONE)
item.setAccelerator(accelerator);
return item;
}
private void updateAccelerator(MenuItem item, String itemText,
char newAccelerator) {
itemText += " \t Ctrl+"
+ (newAccelerator == ' ' ? "[space]" : newAccelerator);
int acc = SWT.MOD1
+ (newAccelerator == ' ' ? SWT.SPACE : newAccelerator);
item.setText(itemText);
item.setAccelerator(acc);
}
/**
* Helper method to set a keyboard accelerator for a MenuItem. If kbInfo is SWT.NONE, no accelerator will be set.
* @param menu SWT MenuItem
* @param kbInfo KeyBindingInfo object, which contains the SWT accelerator value and its display name
*/
private static void setAccelerator(final MenuItem menu, final KeyBindingInfo kbInfo)
{
if ( menu.isDisposed()){
return;
}
if(kbInfo.accelerator != SWT.NONE)
{
menu.setAccelerator(kbInfo.accelerator);
// SWT on OS X now uses native drawing
if(!Constants.isOSX && !menu.getText().endsWith(kbInfo.name))
menu.setText(menu.getText() + kbInfo.name);
}
}
/**
* メニューバーにメニューを追加する
*/
private void setMenuBar() {
// メニューバーに追加する
// フィルターメニュー
final MenuItem filter = new MenuItem(this.opemenu, SWT.PUSH);
filter.setText("フィルター(&F)\tCtrl+F");
filter.setAccelerator(SWT.CTRL + 'F');
filter.addSelectionListener((SelectedListener) (e) -> {
int index = ShipTable.this.tabFolder.getSelectionIndex();
new ShipFilterDialog(this.shell, this.updateFilter(index), this.getFilter(index)).open();
});
}
/**
* Create contents of the dialog.
*/
private void createContents() {
// シェルを作成
this.shell = new Shell(this.getParent(), this.getStyle());
this.shell.setSize(this.getSize());
// ウインドウ位置を復元
LayoutLogic.applyWindowLocation(this.getClass(), this.shell);
// 閉じた時にウインドウ位置を保存
this.shell.addShellListener(new SaveWindowLocationAdapter(this.getClass()));
this.shell.setText(this.getTitle());
this.shell.setLayout(new FillLayout(SWT.HORIZONTAL));
// メニューバー
this.menubar = new Menu(this.shell, SWT.BAR);
this.shell.setMenuBar(this.menubar);
// ツリーテーブル
this.tree = new Tree(this.shell, SWT.BORDER | SWT.FULL_SELECTION | SWT.VIRTUAL | SWT.MULTI);
this.tree.addKeyListener(new TreeKeyShortcutAdapter(this.header, this.tree));
this.tree.setLinesVisible(true);
this.tree.setHeaderVisible(true);
// メニューバーのメニュー
MenuItem operoot = new MenuItem(this.menubar, SWT.CASCADE);
operoot.setText("操作");
this.opemenu = new Menu(operoot);
operoot.setMenu(this.opemenu);
MenuItem reload = new MenuItem(this.opemenu, SWT.NONE);
reload.setText("再読み込み(&R)\tF5");
reload.setAccelerator(SWT.F5);
reload.addSelectionListener((SelectedListener) e -> this.reloadTable());
// テーブル右クリックメニュー
this.tablemenu = new Menu(this.tree);
this.tree.setMenu(this.tablemenu);
MenuItem sendclipbord = new MenuItem(this.tablemenu, SWT.NONE);
sendclipbord.addSelectionListener(new TreeToClipboardAdapter(this.header, this.tree));
sendclipbord.setText("クリップボードにコピー(&C)");
MenuItem reloadtable = new MenuItem(this.tablemenu, SWT.NONE);
reloadtable.setText("再読み込み(&R)");
reloadtable.addSelectionListener((SelectedListener) e -> this.reloadTable());
this.setTableHeader();
this.reloadTable();
}
/**
* Open the dialog.
*/
public void open() {
// シェルを作成
this.shell = new Shell(this.getParent(), this.getStyle());
this.shell.setSize(this.getSize());
// ウインドウ位置を復元
LayoutLogic.applyWindowLocation(this.getClass(), this.shell);
// 閉じた時にウインドウ位置を保存
this.shell.addShellListener(new SaveWindowLocationAdapter(this.getClass()));
this.shell.setText(this.getTitle());
this.shell.setLayout(new FillLayout());
// メニューバー
this.menubar = new Menu(this.shell, SWT.BAR);
this.shell.setMenuBar(this.menubar);
// メニューバーのメニュー
MenuItem fileroot = new MenuItem(this.menubar, SWT.CASCADE);
fileroot.setText("ファイル");
this.filemenu = new Menu(fileroot);
fileroot.setMenu(this.filemenu);
MenuItem savecsv = new MenuItem(this.filemenu, SWT.NONE);
savecsv.setText("CSVファイルに保存(&S)\tCtrl+S");
savecsv.setAccelerator(SWT.CTRL + 'S');
savecsv.addSelectionListener(new TableToCsvSaveAdapter(this.shell, this.getTitle(),
() -> this.getSelectionTable().getTable()));
MenuItem operoot = new MenuItem(this.menubar, SWT.CASCADE);
operoot.setText("操作");
this.opemenu = new Menu(operoot);
operoot.setMenu(this.opemenu);
MenuItem reload = new MenuItem(this.opemenu, SWT.NONE);
reload.setText("再読み込み(&R)\tF5");
reload.setAccelerator(SWT.F5);
reload.addSelectionListener(new TableReloadAdapter());
Boolean isCyclicReload = AppConfig.get().getCyclicReloadMap().get(this.getClass().getName());
MenuItem cyclicReload = new MenuItem(this.opemenu, SWT.CHECK);
cyclicReload.setText("定期的に再読み込み(&A)\tCtrl+F5");
cyclicReload.setAccelerator(SWT.CTRL + SWT.F5);
if ((isCyclicReload != null) && isCyclicReload.booleanValue()) {
cyclicReload.setSelection(true);
}
CyclicReloadAdapter adapter = new CyclicReloadAdapter(cyclicReload);
cyclicReload.addSelectionListener(adapter);
adapter.setCyclicReload(cyclicReload);
MenuItem selectVisible = new MenuItem(this.opemenu, SWT.NONE);
selectVisible.setText("列の表示・非表示(&V)");
selectVisible.addSelectionListener(new SelectVisibleColumnAdapter());
new MenuItem(this.opemenu, SWT.SEPARATOR);
// 閉じた時に設定を保存
this.shell.addShellListener(new ShellAdapter() {
@Override
public void shellClosed(ShellEvent e) {
AppConfig.get().getCyclicReloadMap()
.put(AbstractTableDialogEx.this.getClass().getName(), cyclicReload.getSelection());
}
});
this.createContents();
this.shell.open();
this.shell.layout();
Display display = this.getParent().getDisplay();
while (!this.shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
// タスクがある場合キャンセル
if (this.future != null) {
this.future.cancel(false);
}
}