Design a ContextMenu with a TreeView functionality - java

I want to design a ContextMenu with a TreeView functionality. I didnt found anything how can be done. I want to design a custom TableMenu (that button at the end of table header which can show hide the columns), using a treeView structure in ContextMenu, so the user can show/hide a group of columns, like 1-3, 4-6, and so on, or any kind of grouping, but still keeping the one by one show/hiding. The best way would be a tree structure, so if the user hides the parent all of its children are hidden and vice-versa.
Is there any possibility to adapt somehow the TreeView into ContextMenu?
I would like something like this with CheckBoxes for instance:
Each parent node would be an item that represents a group of columns, and each child would be a column.
P.S.
I know how to create and add a custom ContextMenu as tableMenu (I have got it from here : https://gist.github.com/Roland09/d92829cdf5e5fee6fee9) , I'm interested in how to set a tree structure to context menu instead of the list structure.
I also know that I can add the parent like a "simple" item to the context menu, then handle as a parent, but then I Have to implement every functionality of a TreeItem, but I prefer a much simpler way if it exists.

Related

Uniquely representing tree elements

I want to make my TreeView remember the selected items after refresh/setInput. (Let's pretend the Java TreeView doesn't remember the state). Since the TreeView will get setInput, so the element reference won't persist.
I am thinking to store the selected element by its full path in an ArrayList<String>. (e.g. it looks like ["root_name", "2nd_level_name", "3rd_level_name", "node_name"]). With the full path, I can locate the same element in the refreshed TreeView again.
I was wondering if there's any better way doing this? Or any existing libraries in Java can do this?

How to create custom list widget?

I want to create custom widget, resembling MS File Explorer thumbnail file view or similar.
The questions are:
1) Should I use/extend Item class? It is not prohibited to extend this class, but simultaneously it is said, that custom SWT widgets are made either of Composite or Canvas. If I want to put image above it's caption, then I probably am to use Composite with table layout. This way I will be unable to extend Item. If I extend Item somehow, then when to decide, how to draw it?
2) Should I implement all input handlers? I.e. so that selection can be moved by keyboard and mouse, multiple selection with Ctrl etc. There is too many code. Can I reuse some premade code for this?

Filtering a ListView in wicket using 2 drop down boxes

I am trying to filter a list that is placed into a listview through the use of 2 drop down boxes.
The first dropdown box is titled price and the second is owner.
I want to be able to select a value in one or more of these drop down boxes and then have the List view re-render with the filtered results.
The trouble is I do not know how to begin this task, would someone be so kind as to enlighten me :D
Thanks in advance!
Your best starting point is probably this example: (Source code also available on this page, ChoicePage.java is the name)
First of all, you have to use a dynamic model in your ListView that generates the list of items depending on what you had selected in the dropdown boxes.
Then the basic idea is that you add an AjaxFormComponentUpdatingBehavior to the components that control the updates (your two dropdown boxes in your case), and in the onUpdate() method of this behaviour you should add the component you want to update to that AjaxRequestTarget passed.

Custom Java JTree implementation

There are 2 JTree: JTree1 and JTree2. Note that the nodes (country, city, colors, blue ...) all will be implemented as JCheckboxes so that user can select particular colors for each city or for the whole country by selecting their corresponding checkboxes.
Problem:
Q1. I want that each country or city can have its own colors selected. Means if a user wants city1.1 to have colors blue and violet and city2.1 to have colors red, then he first have to select the city1.1 checkbox and then select blue and violet, and after that when he selects city2.1, then the checkboxes blue and violet are deselected automatically so that user can select the colors for city2.1. But when the user selects the city1.1 again, then the JTree2should show the selected colors (bule and violet) for city1.1.
So for this purpose, Is the JTree (with its nodes as checkboxes) correct option to implement or I should use some other JComponent?
If JTree is a correct option, then how can I remember the colors of each city?
So for this purpose, Is the JTree
(with its nodes as checkboxes) correct
option to implement or I should use
some other JComponent?
Not exactly sure what you meant, but I, personally, would not use a JTree to present the options on the right hand side. I think it is much simpler to present a JPanel that contains the options in this particular case. Left side seems fine for your example, although I don't really know what sort of data is going into the tree.
If JTree is a correct option, then how
can I remember the colors of each
city?
Note, I'm going to make a couple of assumptions:
The left side that contains your countries and cities remains a JTree and the right hand side can still be a JTree or a JPanel.
You want the options to appear exactly as the user last set it before they select a different node on the left hand side.
The simplest way of achieving this is to add a TreeSelectionListener to the tree's (the one containing the countries and cities) selection model. The TreeSelectionListener is provided with a TreeSelectionEvent which provides the node that was selected and the node that will become selected. This will provide you with the opportunity to extract the colour settings that were set for the node that the selection is changing from to the one that the selection is changing to. The TreeSelectionListener should be added to the TreeSelectionModel that is obtained from the JTree, by calling its getSelectionModel method.
If you use this technique, when you to perform the operation with the last selected options, you'll need to get the options one more time before you perform the operation. For example, if you had a "Save" button, you should check extract the colour settings for which node is selected on the left. This is to capture any changes that the user may have made that the listener has not captured (since the listener is triggered only when the left hand selection changes).
If you need an example, I've written one at http://www.box.net/shared/hgbet4uf6k.

GUI: Changing panels based on value of combo box

I have a question about GUI design, specifically with Java Swing and creating clean separation between presentation and model.
It's a bit difficult to describe, but essentially we have lots of reference data in our system (i.e. that would correspond to lookup tables in the DB). We want people to be able to edit them all from one screen.
So, in an ideal world what we'd like is a combo box in the top-left corner with a list of 'types' of reference data (so each corresponding to one table in the DB).
Then, when selected, a list of the data is populated below, also a filter (or search box). When one of these items is selected, the panel to the right is activated which will allow the actual data to be edited.
Now, here's the problem: each type of data we need to edit is different, so it has different fields etc. We could go with a generic solution but I'm not really a fan of them - there are lots of different validation rules for each etc, even for different clients, and it would be a nightmare to manage.
We're using the Presentation Model pattern to achieve some degree of separation between GUI code and the model but I can't think of a clean way of doing this which doesn't somehow blur the line of responsibilities a bit.
What are the ways you have solved problems like this?
[Note: apologies for the long question, hope it's understandable, I can re-phrase if necessary]
You could use the Factory Pattern to create a UI widget for the element that you are selecting. You could also use it to create a validation rule object depending on the type. This would give you some of the flexibility you desire.
So you can have something like:
IWidget widget = UIFactory.createFor(myObject.getType())
That can be invoked on the selection event to create the right widget to edit the selected element.
The IWidget could have methods such as:
validateData()
refreshData()
setDataElement(IDataElement element)
That would allow you to treat all UI widgets generically, but still have a special UI widget for each element type. I am assuming that the elements that you are selecting from the table all implement some IDataElement interface that contains the getType() method.
I used this solution tied together with the Eclipse Extension mechanism to plug-in new UI elements into my "base" solution, to have an extensible core and a high level of reuse. You could achieve something similar by injecting types and widgets into your factory, either manually or with Spring.
If you dont want to go down the generic path, you could have your model hold a mapping of combobox item -> panel name for use with a CardLayout. You could then create custom panels for the editing each of the reference data types. When the combo box selection is changed, you can save the current state in your model, request the panel name of the current selection, prepare your next panel for display and then have your CardLayout show it.

Categories