I am trying to add a context menu on SWT Table with the Key Name.
Context Menu is coming properly but I am not able to set the key name as we can mention as a "sequence" in Menu Contribution.
I am not using Menu Contribution but using a MenuItem.
Here is my code.
final MenuItem item = new MenuItem(menu, SWT.PUSH);
item.setText(save);
item.addListener(SWT.Selection, new Listener() {
#Override
public void handleEvent(Event event) {
//saveFunction
}
});
This is working but I want to add the Key name also with the name of Menu something like this:
Can anyone please help me as I can not use MenuContribution.
You can use MenuItem.setAccelerator to set the key for the menu item:
item.setAccelerator(SWT.MOD1 | 'S');
Note that this is only active when the menu is shown, if the menu is not shown you would need to use a key listener. I have used the SWT.MOD1 modifier here rather than SWT.CTRL so that the key will be the correct ⌘+S on macOS.
On platforms that don't automatically add the accelerator text you can get set the text using:
String acceleratorText = Action.convertAccelerator(SWT.MOD1 | 'S');
item.setText(save + '\t' + acceleratorText);
Here Action is org.eclipse.jface.action.Action.
In an Eclipse plug-in you should probably be using a retargetable action to integrate with the standard save code.
Related
How can I add a function key (i.e. the F1 to F12 keys) for shortcut key in JavaFX?
I use save button. I don’t need to click save button and it make easy to system
If you are using a Button, let's say saveButton and it is in Scene scene then you can set accelerator(shortcut key) to button as following:
Button saveButton = new Button("save");
scene.getAccelerators().put(new KeyCodeCombination(KeyCode.F1), saveButton::fire);
KeyCodeCombination in above code is used to set accelerators to javaFX contols and It takes, as argument, KeyCode e.g. KeyCode.K, KeyCode.F3 etc. and/or KeyCombination like KeyCombination.SHORTCUT_DOWN etc.
and if you are using MenuItem let's say saveMenu then you can set accelerator(shortcut key) to it as following:
MenuItem saveMenu = new MenuItem("save");
saveMenu.setAccelerator(new KeyCodeCombination(KeyCode.F1));
I´m writing an Eclipse plugin to show the contents of an own file type in an own custom editor.
This file basically consists of 1..n IFile´s from the Project Explorer which are shown in a SWT TreeView.
The entries in this TreeView are some Beans organized in a flat List, but i provide an adapter to convert them to IFile´s.
<extension
point="org.eclipse.core.runtime.adapters">
<factory
adaptableType="de.dstg.delta.collections.model.Collection$Member"
class="de.dstg.delta.collections.CollectionMemberAdapterFactory">
<adapter
type="org.eclipse.ui.views.properties.IPropertySource2">
</adapter>
<adapter
type="org.eclipse.core.resources.IFile">
</adapter>
</factory>
</extension>
Eclipse Screenshot
It´s no problem to show a custom SWT menu but i think the right way is using the Eclipse menu structure, especially by providing default behaviour for Resources.
How can i show the default context popup menu, which is used by right-clicking resources in the Project Explorer, in my SWT TableView?
EDIT second question removed
EDIT2
According to the answer from greg-449, the problem remains that the context menu shows no entries i would like to have for this type of selection.
f.e. i need delete or open with or something like that 'file basics' without implementing it on my own.
We have also some other menus from additional plugins that should be visible here when the appropriate file type is selected. This works in Project Explorer but not in my table view.
I think the problem is the type of the selected element.
Plugin spy shows the following in my table view:
type of the selected element: Collection$Member,
interfaces valid for the selected element: IAdaptable
whereas the Project Explorer shows File and IFile, respectively.
My adapter works generally and delivers the related IFile while being triggered from an unknown Eclipse source.
I think i have to explicitely say, that the context menu should use the IFile type to get the correct menu entries but i dont´t know how.
Or did i miss something?
There isn't really a 'default' menu. Some menu items are defined to be added to all popup menus that define a specific place holder.
To use the place holder you must register your context menu with Eclipse, something like:
private void createContextMenu(Viewer viewer) {
Control menuControl = viewer.getControl();
MenuManager menuMgr = new MenuManager("#PopUp");
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(new IMenuListener() {
#Override
public void menuAboutToShow(IMenuManager mgr) {
fillContextMenu(mgr);
}
});
Menu menu = menuMgr.createContextMenu(menuControl);
menuControl.setMenu(menu);
// register the context menu such that other plugins may contribute to it
getSite().registerContextMenu(menuMgr, viewer);
}
private void fillContextMenu(IMenuManager menu) {
// TODO add your actions
// Standard additions
menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
}
You will probably also need to set your viewer as the 'selection provider':
getSite().setSelectionProvider(viewer);
Many menu items will check this to see what the type of the current selection is.
There is nothing that will automatically give you things like Open With or Delete, you have to add these yourself in the 'fillContextMenu' section.
You can use org.eclipse.ui.actions.OpenWithMenu for Open With.
IFile file = ... currently selected file
IMenuManager submenu = new MenuManager("Open With...");
submenu.add(new OpenWithMenu(getSite().getPage(), file);
menu.append(submenu);
We got the Jmjrst open source project and should configure and work a bit with it.
Now we never had any of the JMenu, JMenuItem and Listener stuff so this is pretty confusing for me.
I already got my new MenuTab implemented and is working.
My problem is that my Task says the following:
Every plugin should be able to be started and configured.
The configuration should only be implemented if the plugin is configurable.
(translated text)
We got a method for checking if it is configurable.
This is my code right now:
int length = pluginList.size();
int i = 1;
for (Plugins plugIn: pluginList) {
pluginMenuItem = new JMenuItem(plug.getMenuText());
if (plugIn.isConfigurable()) {
pluginMenuItem.addActionListener(new MenuListner(m, this));
}
if (i < length) {
pluginsMenu.addSeparator();
}
pluginsMenu.add(plugin);
i++;
}
newList is just a list containing all available plugins. I think my
if (plug.isConfigurable()) is set incorrectly because every menupoint needs to be able to be clicked. But idk how to manage what is happening when the menu point (plugin) is getting clicked and how to seperate running and configuring.
Edit: every plugin has the following methods: run() and configure()
but how do I add them to the Menu and am able to seperate them?
Edit2:
Plugins ==== The Plugins which should be added to the Menu
pluginsMenu ==== the JMenu Object
plugIn ==== the new JMenuButton Object
This will depend on your user interface design, you can have subMenuItems under each plugin-- one for say "config" and other for "execute/Run",
you can have one menuItem has you have now and then listener can popup the dialog to let user select config/execute -- this will not be the best option as users will have to deal with dialog every time they click the plugin
Another way I can think of is have a top level menuItem for "configuration" and so users clearly can see if they want to configure or change they have to go to "config" menu
SubMenu item is a menuItem itself
//a submenu
pluginsMenu.addSeparator();
submenu = new JMenu("Plugin Name");
//add one submenuItem
menuItem = new JMenuItem("Config");
submenu.add(menuItem);
//add another submenuItem
menuItem = new JMenuItem("Run/Execute");
submenu.add(menuItem);
//add submenu to mainPluginMenu
pluginsMenu.add(submenu);
see actual code here menu
I have a jFace wizard, I am using this to create a new project type eclipse plugin. As you can see from image below, I have one treeviewer on left side, and a SWT group on right side. What I want is when ever user selects one of the item from treeviewer, I should be able to create dynamic controls on right side SWT Group. Say user selects Test One, one right side I should be able to create few controls like label, text and few radio buttons on right side, similarly if user selects Test Two I should be able to create dynamic controls on right side.
Currently I tried below code:
tree.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
for (int i = 0; i < selection.length; i++) {
String tempStr = selection[i].toString();
tempStr = tempStr.replaceAll("TreeItem \\{", "");
String finalStr = tempStr.replaceAll("\\}", "");
if (finalStr.equals("Test One")) {
Button btn = new Button(g2, SWT.NONE); //g2 is right side group
btn.setText("Blaaaa");
btn.setVisible(true);
container.redraw();
}
}
But when I run, I see no changes on right group. Can anyone guide me what I am doing wrong? Any pointers would be very appreciated, since I am new to Eclipse development and SWT.
You probably didn't set a layout on the g2 group. This is the common cause for controls not showing up. You can also try using g2.layout() to ensure that the new controls are correctly laid out after you create them.
Additionally you could look at using a StackLayout so that once you create a set of controls you can just hide them all at once instead of destroying when the selection changes. This is often useful so that if the user comes back to a previous selection, they will find the data they entered in the same state when they switched the selection. Here is an example.
I'm writing a simple Swing app. I tried adding a checkbox as listed below. Once I added the actionHandler loadPickers the name Foo disappeared from where it was sitting next to the right of chckbxNewCheckBox. I tried adding a call to setHideActionText(), but now nothing displays.
JCheckBox chckbxNewCheckBox = new JCheckBox("Foo");
chckbxNewCheckBox.setToolTipText("");
chckbxNewCheckBox.setName("");
chckbxNewCheckBox.setHideActionText(true);
chckbxNewCheckBox.setAction(loadPickers);
mainPanel.add(chckbxNewCheckBox, "flowy,cell 0 1");
If I change it to this it works properly. I see the text "Foo".
JCheckBox chckbxNewCheckBox = new JCheckBox("Foo");
chckbxNewCheckBox.setToolTipText("");
chckbxNewCheckBox.setName("");
chckbxNewCheckBox.setHideActionText(true);
chckbxNewCheckBox.setAction(loadPickers);
chckbxNewCheckBox.setText("Foo"); //THIS DOES NOT WORK IF IT COMES BEFORE SET ACTION
mainPanel.add(chckbxNewCheckBox, "flowy,cell 0 1");
I've included the action here for completeness. Why does it work this way? Am I missing something here? Currently I'm using the WindowBuilder plugin for Eclipse with the Mig layout system (which I really like). Unfortunately I haven't figure out if there's a way to make WindowBuilder use the .setText() method instead of using the constructor. Any help on what I'm doing wrong, any insight on why this behavior exists like this, or a good workaround for WindowBuilder would be great.
private class LoadPickers extends AbstractAction {
public LoadPickers() {
//putValue(NAME, "SwingAction_2");
putValue(SHORT_DESCRIPTION, "Some short description");
}
public void actionPerformed(ActionEvent e) {
}
}
As explained in the JavaDoc of AbstractButton.setAction:
Setting the Action results in immediately changing all the properties described in Swing Components Supporting Action. Subsequently, the button's properties are automatically updated as the Action's properties change.
So all the following properties can be impacted by setting an action:
enabled
toolTipText
actionCommand
mnemonic
text
displayedMnemonicIndex
icon (NA for JCheckBox)
accelerator (NA for JCheckBox)
selected