Ext GWT. Column event not working. - java

When i add listener column.addListener(Events.CellClick, new Listener<BaseEvent>() it doesnt work on column. But if I add listener to Grid. then its works. How to fire event when user clicks on column?

Ok, i understand what you want. You can fire column event from the grid eventHandler like this:
grid.addListener(Events.CellClick, new Listener<GridEvent<ModelData>>() {
#Override
public void handleEvent(#NotNull GridEvent<ModelData> ge) {
ge.getGrid().getColumnModel().getColumn(ge.getColIndex()).fireEvent(Events.ColumnClick);
}
});
Then before adding column to your grid ColumnModel you should add Listener on it:
final ColumnConfig column = new ColumnConfig();
column.addListener(Events.ColumnClick, new Listener<BaseEvent>() {
#Override
public void handleEvent(#NotNull BaseEvent be) {
GWT.log("I was clicked!!!");
}
});
I don't know is there better way to do it.

Related

How to know if a ComboBox ValueChangeListener is triggered from select or from the user

Suposse that I define a ComboBox like this:
ComboBox myCombo = new ComboBox();
myCombo.addValueChangeListener(event -> {
//Some code
});
And then, after adding items to the ComboBox I select one:
myCombo.select(someItem);
Is there any way from witch I can know if the code inside the value change listener is executed because of a calling to myCombo.select(someItem) or because the user changes the value of the ComboBox?
We simply use a flag such as _internalChange and we check it in the listener function. Not cool enough but it works :) A reusable solution would be to create a new component.
boolean _internalChange = false;
void init(){
ComboBox myCombo = new ComboBox();
myCombo.addValueChangeListener(event -> {
if(_internalChange) {
// do something
} else {
// do something else
}
});
}
void selectMyCombo(Object value){
_internalChange = true;
myCombo.select(stuff)
_internalChange = false;
}
I think all you need to do is this. If you haven't added the ValueChangeListener when you select the initial value the listener won't get triggered.
ComboBox myCombo = new ComboBox();
// add items to the ComboBo
myCombo.select(someItem);
myCombo.addValueChangeListener(event -> {
//Some code
});

ComboBoxCellEditor only setting value on focus lost

Is there a way to set the value in a ComboBoxCellEditor other then when the focus is lost on the cell? I'm using it in each cell of a column in a TreeViewer and the only time that the setValue method is called is when focus is lost on the cell. So when a user makes a selection and doesn't click off of the cell the value is never set to the new selection. I've tried adding listeners on the ComboBoxCellEditor and on the control of the ComboBoxCellEditor but nothing seems to pick up the selection event.
I figured out that I needed to cast the control to a CCombo in order to add the correct type of listener to the ComboBoxCellEditor. Here's what I did:
CCombo combo = (CCombo) cellEditor.getControl();
combo.addSelectionListener(new SelectionListener()
{
#Override
public void widgetSelected(SelectionEvent paramSelectionEvent)
{
//selection code here...
}
#Override
public void
widgetDefaultSelected(SelectionEvent paramSelectionEvent)
{
//do nothing here...
}
});

How to listen to selection changes on TableViewer?

I'm working on an Eclipse RCP application and I'm trying to update an expression value which is provided by MySourceProvider according to selection changes on a TableViewer in MyEditorPart.
MyEditorPart instance defines a TableViewer like this:
public class MyEditorPart extends EditorPart {
#Override
public void createPartControl(Composite parent) {
TableViewer tableviewer = new TableViewer(parent, SWT.CHECK);
tableviewer.setContentProvider(ArrayContentProvider.getInstance());
getSite().setSelectionProvider(tableViewer);
...
MySourceProvider have some expression values like this:
public class MySourceProvider extends AbstractSourceProvider {
public static final String EXPR = "org.xyz.isEntrySelected";
// other expressions
#Override
public String[] getProvidedSourceNames() {
return new String[] { EXPR,
// other expressions
};
}
#Override
public Map getCurrentState() {
HashMap<String, Object> map = new HashMap<String, Object>(1);
map.put(EXPR, expr_value); // expr_value calculated by the listener
// other expressions
return map;
}
I want to change expr_value according to selection changes on TableViewer.
I registered the listener like this:
window.getSelectionService().addPostSelectionListener(MyEditorPartId, selectionListener);
private final ISelectionListener selectionListener = new SelectionListener() {
#Override
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
handleEvent();
}
};
The listener registers successfully but gets notified only once if I clicked somewhere on MyEditorPart (not just TableViewer but the whole editor). To get notified again, I have to click on some other view (or editor) part to lose focus and then click again on MyEditorPart.
1. Why does the listener gets notified only once when MyEditorPart re-gains focus?
2. How to listen only to selection changes to TableViewer rows?
What am I missing here? What is the proper way to listen to selection changes?
Thanks in advance.
What you need is not a SelectionListener, but a SelectionChangedListener.
With this you can write the following code:
viewer.addSelectionChangedListener(new ISelectionChangedListener() {
#Override
public void selectionChanged(SelectionChangedEvent event) {
IStructuredSelection selection = viewer.getStructuredSelection();
Object firstElement = selection.getFirstElement();
// do something with it
}
});
It does appear that this form of addPostSelectionListener only fires when the part becomes active. Use the:
addPostSelectionListener(ISelectionListener listener)
form of the listener which is called for every selection change.
You can then test the IWorkbenchPart id in the listener:
#Override
public void selectionChanged(final IWorkbenchPart part, final ISelection selection)
{
if (MyEditorPartId.equals(part.getSite().getId()))
{
// your code
}
}

Set the checkbox of a CheckboxTableViewer when the row is clicked

I am very new to SWT. Started working on it today actually. I have a table of type CheckboxTableViewer. What i want to be able to do is whenever the user selects the row (i.e clicks anywhere on the row) I want the check box to be checked (ticked). Currently I have a listener on the CheckboxTableViewer as follows:
diagnosesTableViewer.addCheckStateListener(new ICheckStateListener() {
#Override
public void checkStateChanged(CheckStateChangedEvent event) {
Nomenclature changedStateNomenclature = (Nomenclature) event
.getElement();
if (event.getChecked()) {
selectedNomenclatures.add(changedStateNomenclature);
} else {
selectedNomenclatures.remove(changedStateNomenclature);
}
}
});
I am able to select the row by checking on the checkbox. But i want to select the check box even when the user selects the row by clicking anywhere on that row on any column (not just the checkbox).
I guess that logic would go somewhere in the addSelectionChangedListener for the addSelectionChangedListener. But I am not sure how to go about it. Can anyone help me with this?
Use this code: Add selection listener to the table. ctv is the instance of of your CheckboxTableViewer.
Also I assumed CheckboxTableViewer allow only single selection not multi.
ctv.getTable().addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
int df = ctv.getTable().getSelectionIndex();
ctv.setChecked(ctv.getElementAt(df), !ctv.getChecked(ctv.getElementAt(df)));
}
});

GWT/ Textbox- single AND double click handler options? possible?

I have created a popup box that extends DialogBox and uses a cellTable that contains a list of values, one of which will be selected and inserted into a textBox.
-I have an onSelectionChange handler which is fired when one of the rows is clicked.
-I have an onDoubleClick handler which is fired when the same rows are double clicked.
both work when the other is commented out. But when they are both in the live code, whichever one is written first gets overwritten by the other one and no longer gets called.
Any way around this?
Code snipbit:
final SingleSelectionModel<popUpBoxContent> selectionModel= new <popUpBoxContent>();
cellTable.setSelectionModel(selectionModel);
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler(){
public void onSelectionChange (selectionChangeEvent event){
//Do something
}});
final SingleSelectionModel<popUpBoxContent> selectionModel2= new <popUpBoxContent>();
cellTable.setSelectionModel(selectionMode2);
cellTable.addDomHandler(new DoubleClickHandler(){
public void onDoubleClick(final DoubleClickEvent event){
//Do something else
}},
DoubleClickEvent.getType());
Thank you!
Yes they get overwritten from what I can see in the snippet. Assuming "popUpBoxContent" is the data type with which the CellTable (I presume cellTable is a CellTable) is being populated you could try this and see if it works:
final SingleSelectionModel<PopUpBoxContent> selectionModel = new SingleSelectionModel<PopUpBoxContent>();
cellTable.setSelectionModel(selectionModel);
cellTable.addDomHandler(new DoubleClickHandler() {
public void onDoubleClick(final DoubleClickEvent event) {
PopUpBoxContent selected = selectionModel.getSelectedObject();
if (selected != null) {
System.out.println("double clicked");
}
}
},
DoubleClickEvent.getType());
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
public void onSelectionChange(SelectionChangeEvent event) {
System.out.println("clicked");
}
});

Categories