SWT TreeViewer with combobox and checkbox - java

I'm looking for a way to display a tree in the first column of a table/grid with three other columns, one with a combobox and the others with checkboxes. I've been trying to make this work with a TreeViewer but it doesn't quite fit what I'm looking for. The tree goes together fine. The Combobox column where I used the EditorSupport for the column and return a ComboboxCellEditor in the getCellEditor method but you can only see that there is a combobox in the column when you select a cell in that column. Then when you click out of the cell the selected value goes back to the default blank. The same goes for the checkbox columns where it is is only visible when the cell is selected. I'm looking for something that will display my tree with the combobox column and checkbox columns always visible. I've looked at TableViewer but couldn't find a way to force in a tree in the first column. I've looked at Nebula Grid but that doesn't look like it supports comboboxes. Any tips on how to get one of these to work like what I am looking for or if there is some other tree/table/grid I should be looking at. Thanks.
Edit: Here's the code for the EditingSupport class.
private class ComboBoxEditingSupport extends EditingSupport
{
private ComboBoxCellEditor cellEditor;
public ComboBoxEditingSupport(ColumnViewer viewer)
{
super(viewer);
cellEditor =
new ComboBoxCellEditor(((TreeViewer) viewer).getTree(),
new String[] {
"Some String",
"Some other String" }, SWT.READ_ONLY);
}
#Override
protected CellEditor getCellEditor(Object element)
{
if (element instanceof MyObject
{
return cellEditor;
}
return null;
}
#Override
protected boolean canEdit(Object element)
{
if (element instanceof MyObject
{
return true;
}
return false;
}
#Override
protected Object getValue(Object element)
{
return 0;
}
#Override
protected void setValue(Object element, Object value)
{
TreeItem[] ti = treeViewer.getTree().getSelection();
CCombo combo = ((CCombo) cellEditor.getControl());
String str = combo.getItem(combo.getSelectionIndex());
ti[0].setText(1, str);
}
}

Your setValue method must update the value in your model data (the data returned by your content provider). The element parameter to setValue is the particular model data object (MyObject) that you should update.
After updating the value call:
getViewer().update(element, null);
to get the tree to update the display from the model.
Trying to update the TreeItem directly will not work.

Related

ComboBoxItem not showing selected item smartgwt

i've a simple combobox item contained in a Dynamicform made with smartgwt. I've populated the dropdown list with the resultset of some query to the database and it is correctly populated. Then I've set some handler to manage the fact that the combobox is populated based on an other combobox of the for, and it is specifically an onFocus Handler which gives the message to populate at first the first required combo and only then the file combo (the combo in question). The problem is that now i have the populated list and the onfocus event without errors but i cannot select anything in the combo cause whatever i select the row of the selection always remains empty.
This is weird, i don't even get any error in development mode to see if something is wrong.
I'm pasting the code of the combo:
fileComboBox.addFocusHandler(new FocusHandler(){
#Override
public void onFocus(FocusEvent event){
String society = (String) getCompany();
if(society==null || society.equals(EMPTY_STRING)){
SC.say(constants.selezionaSocieta());
}
companyComboBox.focusInItem();
}});
fileComboBox.setTitle(constants.fileUploadMov());
fileComboBox.setName(FILE);
fileComboBox.setValueField(TestataDS.ATTR_ID_UPLOAD);
fileComboBox.setDisplayField(TestataDS.ATTR_NOME_FILE);
fileComboBox.setAutoFetchData(false);
fileComboBox.setFetchMissingValues(false);
fileComboBox.setOptionDataSource(TestataDS.getInstance());
fileComboBox = new ComboBoxItem() {
#Override
protected Criteria getPickListFilterCriteria() {
return getFileCriteria();
}};
public AdvancedCriteria getFileCriteria(){
String society = (String) getCompany();
String societyValue = ( society != null) ? society : "";
Criterion cSociety = new Criterion("codSocGest", OperatorId.EQUALS, societyValue);
return new AdvancedCriteria(OperatorId.AND, new Criterion[]{cSociety});
}
I solved it turning the combobox into a selectItem

JFace - How can I make only one column editable in TreeViewer based on checkbox value from another column in the same row?

I have a TreeViewer which has two columns: ProximityClustersColumn: which has names as String, selectionColumn: which has checkbox as shown in the figure
TreeViewer
I have two questions:
On clicking the selection column's checkbox, the corresponding name of ProximityClustersColumn should become editable.
For eg: When I click on the checkbox corresponding to "Studium organisieren-Formelles", the cell "Studium organisieren-Formelles" should become editable.
Also, as seen in the figure, a check must be made such that only one value in the group, whose checkbox is checked becomes editable.
In other words, for each group, only one category name can be checked and that corresponding name should be editable.
For eg:
If you look at the second group, there are two proximity Cluster names, i.e "Infos für Studis" and "Finanzielles im Studium", along with their respective checkboxes. Now, I can choose one among the two names, by selecting the corresponding checkbox. Suppose, I click on the checkbox corresponding to "Infos für Studis", only that cell should become editable.
The main idea is that : I should be able to select only one name from each group and edit it.
I have tried EditingSupport as suggested by #keyur, but the "canEdit" method is not called at all.
My LabelProvider extends ColumnLabelProvider and implements ITableLabelProvider. My ContentProvider implements ITreeContentProvider.
Is there any reason why EditingSupport will fail?
public class ProximityClustersEditingSupport extends EditingSupport{
private TreeViewer viewer;
private CellEditor editor;
public ProximityClustersEditingSupport(ColumnViewer columnViewer, TreeViewer treeViewer) {
super(columnViewer);
this.viewer = treeViewer;
this.editor = new TextCellEditor(treeViewer.getTree());
}
#Override
protected CellEditor getCellEditor(Object element) {
return new TextCellEditor();
}
#Override
protected boolean canEdit(Object element) {
return true;
}
#Override
protected Object getValue(Object element) {
if(element instanceof ProbeSort)
return ((ProximityClusters)element).proximityClusterNames;
return element;
}
#Override
protected void setValue(Object element, Object value) {
if (element instanceof ProbeSort)
{
((ProximityClusters)element).setProximityClusterNames(String.valueOf(value));
}
viewer.update(element, null);
}
}
I think I need more information to answer it completely. First of all is that Treeviewer or Tableviewer. To me it looks like TableViewer.
What is the expected behavior of the cell when you mean editable. Those cells which are not editable should have disable type foreground colored text and the one which is editable can have normal foreground color say black. Only when user changes to focus (if tab is supported) or by clicking on the cell it is better to show the editable text where user can select the text and change/edit it. And pressing Enter key or Tab Key program can accept the change. Is that what you are looking for?
I guess I didnt get the question. Can you give example from the above figure. Like what is group?
I think EditingSupport will be useful for you.
You can create your concrete EditingSupport class and assign it to your column. It has method "canEdit" through which you can control the editing dynamically.
So what you have to do is to store the boolean flag in the model from checkbox status or read the check box status directly and return false/true value , which will enable/disable editing.
The program in the link shows all possible Tree viewer related implementations. Copy paste the program into a new java class and run as java application. This one example solved all tree related issues i had in my implementation.

GWT: Adding GWTP Tooltip to a CellTable cell

I want to use GWT-Bootstrap Tooltip for cells of a column in the CellTable. Each Tooltip will show description of a cell which is a field in the "MyCustomObject" class used for generating the table.
One of the quick solutions I found in one of the answers on this question is the following.
CellTable<MyCustomObject> table = new CellTable<MyCustomObject>();
table.addCellPreviewHandler(new Handler<MyCustomObject>() {
#Override
public void onCellPreview(CellPreviewEvent<MyCustomObject> event) {
if (event.getNativeEvent().getType().equals("mouseover")){
table.getRowElement(event.getIndex()).getCells()
.getItem(event.getColumn()).setTitle(event.getValue().getDescription());
}
}
}
);
It makes a little sense but I don't see anything when I hover the cursor over the table cells and nothing is attached to the DOM. Can anyone explain how this even works? I want to attach the Tooltip to table cell similar to the following.
Tooltip tt = new Tooltip("Here goes the description");
tt.setAnimation(true);
tt.setWidget(tableColumn);
tt.reconfigure();
The problem is that a CellTable cell is not a widget so it can't be attached in that way.
So is there any workaround for this?
I've faced this issue once and I created my own cell that displays a tooltip, here's how I did it: Note: My cell was displaying an image with a tooltip so I quickly changed the code to display String text... So I did not test it with text.
private class TooltipCell extends AbstractCell<String> {
private String tooltipText = "";
#Override
public void render(Context context, String value, SafeHtmlBuilder sb) {
if (value != null) {
sb.appendHtmlConstant("<div title=\"" + tooltipText + "\">");
sb.append(SafeHtmlUtils.fromString(value));
}
}
public void setTooltip(String tootltipTextToSet){
tooltipText = tootltipTextToSet;
}
}
And in your table use method setTooltip("tooltip text") in method getValue just before returning the cell text.
Hope this helps

SWT , TreeViewer , CellEditor with ComboBox

While using EditingSupport for a treeColumn in a TreeViewer, Is there any way i can just reflect the Changes in the View instead of changing the model and then using getViewer().update(element,null);
Detail:
I want to achieve the following functionality:
Show a tree View with |Object| (ComboBox)property|
Upon selection and clicking on the button i want to show user the summary of changes and then upon clicking confirm i want to apply those changes to the model (Object)
I am using a TreeViewer, Within that i have a column with EditingSupport Enabled.
Whenever I select a value from the ComboBox and click somewhere else (lostFocus kind of ) the Value sets to default.
I have figured out that after SetValue() is called the TreeLabelProvider is again called(using debug points)
Is there any way i can just reflect the Changes in the View instead of changing the model and using getViewer().update(element,null);
Some FYIs :
Package Object contains multiple versions
ContentProvider does the job to fetch the object
LabelProvider gets all the Versions from the package(String[]) and shows the first one.
//Code to Create the UI
// blah
TreeViewerColumn column2 = new TreeViewerColumn(treeViewer, SWT.LEFT);
column2.getColumn().setText("Version");
column2.getColumn().setWidth(130);
treeViewer.setLabelProvider(new PackageUpdateTreeLabelProvider());
EditingSupport exampleEditingSupport = new OperationEditingSupport(
column2.getViewer());
column2.setEditingSupport(exampleEditingSupport);
OperationEditingSupport Class
private class OperationEditingSupport extends EditingSupport {
private ComboBoxCellEditor cellEditor = null;
private OperationEditingSupport(ColumnViewer viewer) {
super(viewer);
cellEditor = new ComboBoxCellEditor(
((TreeViewer) viewer).getTree(), new String[] {},
SWT.READ_ONLY);
}
#Override
protected CellEditor getCellEditor(Object element) {
// TODO Auto-generated method stub
if (element instanceof IPackageInfo) {
IPackageInfo pkg = (IPackageInfo) element;
cellEditor.setItems(PackageMgrUtil.getInstance().getVersions(
(IdmPackage) pkg, false, true));
return cellEditor;
}
return null;
}
#Override
protected boolean canEdit(Object element) {
return true;
}
#Override
protected Object getValue(Object element) {
// TODO Auto-generated method stub
return 0;
}
#Override
protected void setValue(Object element, Object value) {
/* only set new value if it differs from old one */
}
}
***************************************************
When i click on the cell of column2 i get the combo box but when i select something and move the focus somewhere else.It again shows the default Value
on debuging i found that :
it agains calls the label Provider which fetches all the Version of the package and then shows the first one hence I can not see any change.
what i want is that it should keep the selection intact without changing the underlying object.
thanks for the help.
Figured it out.
following code added to the SetValue() method solves it.
m_tree = (Tree)getViewer.getControl();
TreeItem[] ti = m_tree.getSelection();
CCombo c = ((CCombo)cellEditor.getControl());
String str = c.getItem(c.getSelectionIndex());
ti[0].setText(1, str );

Remove HTML from JTable CellEditor

I have a JTable and some editable cells which are dynamically formatted with very specific HTML based on some business rules.
However, when you edit these cells all the HTML is in the CellEditor. I just want the plain text in the CellEditor.
I am trying to do this to the entire table. Here is the code I used. I threw together an extended DefaultCellEditor but its still showing the HTML. I don't even see the debugger entering the getCellEditorValue() method. What do I do?
public class MyTable extends JTable
{
public MyTable()
{
MyTable.setCellEditor(new DefaultCellEditor(new JTextField())
{
#Override
public Object getCellEditorValue() {
// get content of textField
String str = (String) super.getCellEditorValue();
if (str == null) {
return null;
}
if (str.length() == 0) {
return null;
}
//remove HTML and return plain text
return Jsoup.parse(str).text();
}
});
}
}
I'm not sure where things are going awry; a complete example may shed some light. The normal editing sequence is outlined here, suggesting that you should probably create your own renderer and editor:
class MyRenderer extends DefaultTableCellRenderer {…}
class MyEditor extends DefaultCellEditor {…}
and apply them as follows:
table.setDefaultRenderer(String.class, new MyRenderer());
table.setDefaultEditor(String.class, new MyEditor());
Be certain that your TableModel returns the correct type token from getColumnClass().

Categories