How to get node actions to display in IconView - netbeans

In my Netbeans Platform project I have a TopComponent with an IconView element, but when I add a NodeAction to the nodes it wont display. Right-clicking the nodes does not pop up a context menu. How do I get the action to display? This is what I have:
public class ItemDeleteAction extends NodeAction
{
⋮
#Override
protected boolean enable(Node[] nodes)
{
for (int i = 0; i < nodes.length; i++) {
Node node = nodes[i];
if (node instanceof ViewerItemNode) return true;
}
return false;
}
⋮
}
public final class JFlavourViewerTopComponent extends TopComponent implements ExplorerManager.Provider
{
public JFlavourViewerTopComponent()
{
⋮
setLayout(new BorderLayout());
add(new IconView(), BorderLayout.CENTER);
ActionMap actionMap = getActionMap();
actionMap.put("delete", new ItemDeleteAction());
associateLookup(ExplorerUtils.createLookup(explorerManager, actionMap));
}
}
public class ViewerItemNode extends AbstractNode
{
private ViewerItem item;
public ViewerItemNode(ViewerItem item, JFlavourProjectBean activeProject)
{
super (Children.LEAF, Lookups.singleton(item));
this.item = item;
setDisplayName (item.item.getLabel());
}
#Override
public Action[] getActions(boolean context)
{
if (!context) {
return new Action[]
{
SystemAction.get( ItemDeleteAction.class )
};
} else {
return super.getActions(context);
}
}
#Override
public Action getPreferredAction()
{
return SystemAction.get(PlayAudioAction.class);
}
}
I think that's all the relevant code.
I do the same thing in another TopComponent in another module and it works fine, but in that module the nodes are displayed as a BeanTreeView instead of an IconView

Try to remove the context condition:
#Override
public Action[] getActions(boolean context)
{
return new Action[] {
SystemAction.get(ItemDeleteAction.class)
};
}

Related

JavaFX TreeTable select children when parent is selected and remove selection from parent

I have a TreeTable which has maximum depth of 2, e.g.
fooType
-foo
-foo
If I select fooType I want the program to select automatically all child-items AND deselect the parent item. But when I try this, I always get an IndexOutOfBoundsException.
myTreeTable.getSelectionModel().selectedItemProperty().addListener((obs, ov, nv) -> {
if (nv.getValue() instanceof fooType) {
myTreeTable.getSelectionModel().clearSelection(myTreeTable.getSelectionModel().getSelectedIndex());
if (!nv.isExpanded()) {
nv.setExpanded(true);
}
ObservableList<TreeItem<IfooTreeItem>> children = nv.getChildren();
for (TreeItem<IfooTreeItem> item : children) {
annotationsTreeTable.getSelectionModel().select(item);
}
}
});
Multi selection mode is enabled.
Any help appreciated.
In JavaFX, you are not allowed to change an ObservableList while an existing change to that list is being processed. (Whether or not this is a sensible rule is open to debate, nevertheless, it is a rule.)
The selection model keeps ObservableLists of both the selected indices and the selected items. Part of the processing of changes in those lists is to call listeners on the selected item and selected index. Consequently, you can't change the selection from a listener on the selection itself.
The "proper" way to do this would be to provide your own implementation of the selection model. This is a bit of a pain, as there are a lot of methods to implement, and their use is not well documented. Here is an example, though this is intended as a starting point and is not intended to be production quality:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.MultipleSelectionModel;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class TreeSelectionExample extends Application {
#Override
public void start(Stage primaryStage) {
TreeView<String> tree = new TreeView<>();
TreeItem<String> root = new TreeItem<>();
tree.setRoot(root);
tree.setShowRoot(false);
root.getChildren().add(new TreeItem<>("Item 1"));
root.getChildren().add(new TreeItem<>("Item 2"));
root.getChildren().forEach(item ->
Stream.of("A", "B").map(s -> new TreeItem<String>(item.getValue()+s))
.forEach(item.getChildren()::add));
MultipleSelectionModel<TreeItem<String>> defaultSelectionModel = tree.getSelectionModel() ;
defaultSelectionModel.setSelectionMode(SelectionMode.MULTIPLE);
tree.setSelectionModel(new MultipleSelectionModel<TreeItem<String>>() {
{
setSelectionMode(SelectionMode.MULTIPLE);
}
#Override
public ObservableList<Integer> getSelectedIndices() {
return defaultSelectionModel.getSelectedIndices();
}
#Override
public ObservableList<TreeItem<String>> getSelectedItems() {
return defaultSelectionModel.getSelectedItems();
}
#Override
public void selectRange(int start, int end) {
System.out.println("selectRange("+start+", "+end+")");
List<TreeItem<String>> items = new ArrayList<>();
for (int i = start; i < end; i++) {
items.add(tree.getTreeItem(i));
}
for (int i = start ; i > end; i--) {
items.add(tree.getTreeItem(i));
}
items.forEach(this::select);
}
#Override
public void selectIndices(int index, int... indices) {
System.out.println("select("+index+", "+Arrays.toString(indices)+")");
TreeItem<String> item = tree.getTreeItem(index);
if (item.isLeaf()) {
defaultSelectionModel.select(item);;
} else {
List<TreeItem<String>> leaves = new ArrayList<>();
findLeavesAndExpand(item, leaves);
for (TreeItem<String> leaf : leaves) {
defaultSelectionModel.select(leaf);
}
}
for (int i : indices) {
item = tree.getTreeItem(i);
if (item.isLeaf()) {
defaultSelectionModel.select(item);;
} else {
List<TreeItem<String>> leaves = new ArrayList<>();
findLeavesAndExpand(item, leaves);
for (TreeItem<String> leaf : leaves) {
defaultSelectionModel.select(leaf);
}
}
}
}
#Override
public void selectAll() {
System.out.println("selectAll()");
List<TreeItem<String>> leaves = new ArrayList<>();
findLeavesAndExpand(tree.getRoot(), leaves);
for (TreeItem<String> leaf : leaves) {
defaultSelectionModel.select(leaf);
}
}
#Override
public void selectFirst() {
System.out.println("selectFirst()");
TreeItem<String> firstLeaf ;
for (firstLeaf = tree.getRoot(); ! firstLeaf.isLeaf(); firstLeaf = firstLeaf.getChildren().get(0)) ;
defaultSelectionModel.select(firstLeaf);
}
#Override
public void selectLast() {
System.out.println("selectLast()");
TreeItem<String> lastLeaf ;
for (lastLeaf = tree.getRoot(); ! lastLeaf.isLeaf();
lastLeaf = lastLeaf.getChildren().get(lastLeaf.getChildren().size()-1)) ;
defaultSelectionModel.select(lastLeaf);
}
#Override
public void clearAndSelect(int index) {
TreeItem<String> item = tree.getTreeItem(index);
defaultSelectionModel.clearSelection();
if (item.isLeaf()) {
defaultSelectionModel.select(item);
} else {
List<TreeItem<String>> leaves = new ArrayList<>();
findLeavesAndExpand(item, leaves);
for (TreeItem<String> leaf : leaves) {
defaultSelectionModel.select(leaf);
}
}
}
#Override
public void select(int index) {
System.out.println("select("+index+")");
select(tree.getTreeItem(index));
}
#Override
public void select(TreeItem<String> item) {
System.out.println("select("+item.getValue()+")");
if (item.isLeaf()) {
defaultSelectionModel.select(item);
} else {
List<TreeItem<String>> leaves = new ArrayList<>();
findLeavesAndExpand(item, leaves);
for (TreeItem<String> leaf : leaves) {
defaultSelectionModel.select(leaf);
}
}
}
#Override
public void clearSelection(int index) {
defaultSelectionModel.clearSelection(index);
}
#Override
public void clearSelection() {
defaultSelectionModel.clearSelection();
}
#Override
public boolean isSelected(int index) {
return defaultSelectionModel.isSelected(index);
}
#Override
public boolean isEmpty() {
return defaultSelectionModel.isEmpty();
}
#Override
public void selectPrevious() {
// TODO Auto-generated method stub
// not sure on implementation needed here
}
#Override
public void selectNext() {
System.out.println("selectNext()");
// TODO Auto-generated method stub
// not sure on implementation needed here
}
private void findLeavesAndExpand(TreeItem<String> node, List<TreeItem<String>> leaves) {
if (node.isLeaf()) {
leaves.add(node);
} else {
node.setExpanded(true);
for (TreeItem<String> child : node.getChildren()) {
findLeavesAndExpand(child, leaves);
}
}
}
});
primaryStage.setScene(new Scene(new BorderPane(tree), 400, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Eclipse Plugin - How to open a wizard page in a command handler?

I write a plugin for eclipse. I have a button in the toolbar menu. and I want that on pressing on it - a wizard page dialog will be opened. I wrote already a class which extends wizard and implements IWizardPage, and I wrote also all the 5 relevant pages, I only don't find any way to open this in the command handler.
Here is the pieces of my code:
The command handler:
public class AddProjectHandler extends AbstractHandler {
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
return null;
}
}
The wizard page manager:
public class NewProjectWizardManager extends Wizard implements INewWizard {
private NewProjectWizardPage1 _page1;
private NewProjectWizardPage2 _page2;
private NewProjectWizardPage3 _page3;
private NewProjectWizardPage4 _page4;
private NewProjectWizardPage5 _page5;
// constructor
public NewProjectWizardManager() {
super();
setWindowTitle("New Project");
}
#Override
public void init(IWorkbench workbench, IStructuredSelection selection) {
}
#Override
public boolean performCancel() {
return true;
}
#Override
public void addPages() {
super.addPages();
_page1 = new NewProjectWizardPage1();
addPage(_page1);
_page2 = new NewProjectWizardPage2(_page1);
addPage(_page2);
_page3 = new NewProjectWizardPage3(_page1);
addPage(_page3);
_page4 = new NewProjectWizardPage4();
addPage(_page4);
_page5 = new NewProjectWizardPage5(_page1);
addPage(_page5);
}
#Override
public boolean canFinish() {
IWizardContainer container = getContainer();
if (_page5.equals(container.getCurrentPage())) {
return true;
} else {
return false;
}
}
#Override
public IWizardPage getNextPage(IWizardPage page) {
IWizardPage nextPage = super.getNextPage(page);
IWizardContainer container = getContainer();
if (nextPage != null) {
if (_page2.equals(container.getCurrentPage()) && _page2.isCheckFinishChecked())
nextPage = super.getNextPage(super.getNextPage(nextPage));
}
return nextPage;
}
#Override
public boolean performFinish() {
}
}
The plugin.xml pieces:
<command
categoryId="com.commands.category"
description="Add new Project"
id="com.commands.AddProject"
name="Add new Project">
</command>
<handler
class="com.handlers.AddProjectHandler"
commandId="com.commands.AddProject">
</handler>
Do you have any idea?
Use WizardDialog to show a wizard. Something like:
public Object execute(ExecutionEvent event) throws ExecutionException
{
Shell activeShell = HandlerUtil.getActiveShell(event);
IWizard wizard = new NewProjectWizardManager();
WizardDialog dialog = new WizardDialog(activeShell, wizard);
dialog.open();
return null;
}
I found the code below from org.eclipse.jdt.ui.actions.AbstractOpenWizardAction.
Before Eclipse3.4 you can extend this class to create an Action.but action is deprecated now,I wonder if Eclipse.org provide something like AbstractOpenWizardAction to do the same work in command-handler mode. I'v not found it yet.
public void run() {
Shell shell = getShell();
if (!(doCreateProjectFirstOnEmptyWorkspace(shell)))
return;
try {
INewWizard wizard = createWizard();
wizard.init(PlatformUI.getWorkbench(), getSelection());
WizardDialog dialog = new WizardDialog(shell, wizard);
PixelConverter converter = new PixelConverter(JFaceResources.getDialogFont());
dialog.setMinimumPageSize(converter.convertWidthInCharsToPixels(70),
converter.convertHeightInCharsToPixels(20));
dialog.create();
int res = dialog.open();
if ((res == 0) && (wizard instanceof NewElementWizard)) {
this.fCreatedElement = ((NewElementWizard) wizard).getCreatedElement();
}
notifyResult(res == 0);
} catch (CoreException e) {
String title = NewWizardMessages.AbstractOpenWizardAction_createerror_title;
String message = NewWizardMessages.AbstractOpenWizardAction_createerror_message;
ExceptionHandler.handle(e, shell, title, message);
}
}

GXT 3.x RowExpander with ButtonCell

I try to add button into rowExpander content:
so i have:
ButtonCell<Integer> viewButtonCell = new ButtonCell<Integer>();
and row expander
RowExpander<XX> expander = new RowExpander<XX>(identity, new AbstractCell<XX>() {
#Override
public void render(Context context, XX value, SafeHtmlBuilder sb) {
sb.appendHtmlConstant("<span>");
viewButtonCell.render(context, value.getId(), sb);
sb.appendHtmlConstant("</span>");
}
ButtonCell is rendered OK i can see it BUT I cannot click it, no selecthandler from ButtonCell is call :(.
Any ideas how can I make selectHandlerActive for this button ?
Thanks
i created some new RowExpander :
public class MTPRowExpander<M> extends RowExpander<M> {
public static int id = 0;
public static interface WidgetFactory<M> {
public Widget createWidget(M model);
}
private WidgetFactory<M> wf;
private Set<Integer> expandedRows;
public MTPRowExpander(IdentityValueProvider<M> valueProvider,WidgetFactory<M> wf) {
this(valueProvider,GWT.<RowExpanderAppearance<M>> create(RowExpanderAppearance.class),wf);
}
public MTPRowExpander(IdentityValueProvider<M> valueProvider,final RowExpanderAppearance<M> appearance, WidgetFactory<M> wf) {
super(valueProvider, null, appearance);
this.wf = wf;
expandedRows = new HashSet<Integer>();
}
#Override
protected boolean beforeExpand(M model, Element body, XElement row,int rowIndex) {
if (expandedRows.contains(rowIndex)) {
return true;
} else {
expandedRows.add(rowIndex);
return super.beforeExpand(model, body, row, rowIndex);
}
}
#Override
protected String getBodyContent(final M model, int rowIndex) {
final int curentid = id++;
Scheduler.get().scheduleFinally(new ScheduledCommand() {
#Override
public void execute() {
Widget widget = wf.createWidget(model);
com.google.gwt.dom.client.Element item = grid.getElement().childElement(".widget" + curentid);
item.appendChild(widget.getElement());
ComponentHelper.setParent(grid, widget);
}
});
return "<div class='widget" + curentid + "'></div>";
}
}
I know that this solution is not perfect but I didnt know how to resolve problem at more proper way.

GXT: LayoutContainer does not respond to ESC Key or "X" button to close

I have a GXT 2.x application with a Menubar Item that renders a separate LayoutContainer.
Here's the hierarchy
MainUI.java -> MenuBar.java -> ReservationPopUp.java
I have replaced my contents of ReservationPopUp.java with KNOWN working examples of LayoutContainer implementations and they respond to the ESC key and "X" button.
Here's how the MenuItem renders the ReservationPopUp.java
MenuItem mntmReserve = new MenuItem("Reserve");
mntmReserve.addSelectionListener(new SelectionListener<MenuEvent>() {
public void componentSelected(MenuEvent ce) {
RootPanel.get().add(new ReservationPopUp());
}
Here's a slimmed down version of my ReservationPopUp.java
public class ReservationPopUp extends LayoutContainer {
public ReservationPopUp() {
}
#Override
protected void onRender(Element parent, int pos) {
super.onRender(parent, pos);
setSize("1024", "809");
final Window window = new Window();
window.setDraggable(false);
window.setSize(537, 399);
window.setPlain(true);
window.setModal(true);
window.setBlinkModal(true);
window.setHeading("Reserve A Server");
window.setClosable(true);
window.setOnEsc(true);
window.setSize("465", "345");
window.setLayout(new AbsoluteLayout());
LabelField lblfldUsers = new LabelField("Users");
window.add(lblfldUsers, new AbsoluteData(43, 218));
final ComboBox<AsyncUser> userList = new ComboBox<AsyncUser>();
window.add(userList, new AbsoluteData(81, 218));
userList.setEmptyText("Select a User...");
userList.setSize("347px", "24px");
LabelField labelServers = new LabelField("Servers");
window.add(labelServers, new AbsoluteData(32, 6));
final DualListField<AsyncServer> serverList = new DualListField<AsyncServer>();
....
window.add(serverList, new AbsoluteData(81, 6));
serverList.setSize("347px", "206px");
window.addButton(new Button("Cancel", new SelectionListener<ButtonEvent>() {
#Override
public void componentSelected(ButtonEvent ce) {
ReservationPopUp.this.hide();
}
}));
window.addButton(new Button("Reserve", new SelectionListener<ButtonEvent>() {
#Override
public void componentSelected(ButtonEvent ce) {
if (serverList.getToList().getListView().getItemCount() == 0 ) {
MessageBox.alert("Invalid Selection","No Server(s) Selected", null);
} else if ( userList.getValue() == null) {
} else {
// DO some stuff
ReservationPopUp.this.hide();
}
}
}));
window.addWindowListener(new WindowListener() {
#Override
public void windowHide(WindowEvent we) {
ReservationPopUp.this.hide();
}
});
window.setFocusWidget(window.getButtonBar().getItem(0));
add(window);
}
}
Window is a popup, it doesn't need to be (and shouldn't be) added to anything. Extend the Window class instead of the LayoutContainer, and instead of adding the ReservationPopup to the page, just call Window.show().

GWT CEll Browser Real Time Update

has someone been able to correctly to update a cell browser at runtime, i.e. when u remove a node or add a node, the change is reflected immediately in the CEll Browser, because I am using a List and when i am making a change it is not being updated on the spot
You can use ListDataProvider setList(...) method for dynamic updates. Here is an example how I update cell browser via RPC:
private void loadAllData(final ListDataProvider<Data> dataProvider) {
dBservice.getAllData(new AsyncCallback<List<Data>>() {
public void onSuccess(List<Data> result) {
dataProvider.setList(result);
}
public void onFailure(Throwable caught) {
caught.printStackTrace();
}
});
}
to refresh a cellBrowser you have to close all the child on the root node.
anyway something like this
for (int i = 0; i < cellBrowser.getRootTreeNode().getChildCount(); i++) {
cellBrowser.getRootTreeNode().setChildOpen(i, false);
}
the AsyncDataProvider calls refreshes data
private final class Model implements TreeViewModel{
private List<ZonaProxy> zonaList = null;
private List<CategoriaProxy> categoriaList = null;
public void setCategoriaList(List<CategoriaProxy> categoriaList) {
this.categoriaList = categoriaList;
}
public void setListZona(List<ZonaProxy> zonaList) {
this.zonaList = zonaList;
}
#SuppressWarnings({ "unchecked", "rawtypes" })
public <T> NodeInfo<?> getNodeInfo(T value) {
CategoryDataProvider dataProvider1 = new CategoryDataProvider();
return new DefaultNodeInfo(dataProvider1, new CategoriaCell());
}
/**
* Check if the specified value represents a leaf node. Leaf nodes cannot be
* opened.
*/
public boolean isLeaf(Object value) {
if (value instanceof CategoriaProxy){
if (((CategoriaProxy) value).getLivello() == 3) {
return true;
}
}
return false;
}
}
private class CategoryDataProvider extends AsyncDataProvider<CategoriaProxy>
{
#Override
protected void onRangeChanged(HasData<CategoriaProxy> display)
{
requests.categoriaRequest().findAllCategorias(0, 8).with().fire(new Receiver<List<CategoriaProxy>>() {
#Override
public void onSuccess(List<CategoriaProxy> values) {
updateRowCount(values.size(), true);
updateRowData(0, values);
}
});
}
}
it Works.
Apparently it is not enough to change the data provider and refresh it.
You need also to force the affected cell to close and reopen it, as in this example
public void updateCellBrowser(String id) {
TreeNode node = getNode(cellBrowser.getRootTreeNode(),id);
if(node != null && ! node.isDestroyed()) {
TreeNode parent = node.getParent();
int index = node.getIndex();
parent.setChildOpen(index, false,true);
parent.setChildOpen(index, true, true);
}
}
In my particular example the cell ids are pathnames hence the following
implementation of getNode().
private TreeNode getNode(TreeNode node, String id) {
for(int i=0; i < node.getChildCount(); i++)
if(node.isChildOpen(i)) {
Object value = node.getChildValue(i);
if(value instanceof String) {
String nodeId = ((String) value);
if(id.equals(nodeId))
return node.setChildOpen(i, true);
if(id.startsWith(nodeId))
getNode(node.setChildOpen(i, true),id);
}
}
return null;
}