SWT TableViewer cell does not display image unless clicked on after update - java

I'm currently working with an SWT TableViewer. And I have a given it a StyledCellLabelProvider implementation. I have given it a set of input and it displays it fine. There is use case where a use can select rows from the table to perform certain actions on and then an icon needs to be added to a specific cell on each row that the modification was done on.
The problem I'm running into here is that the image is not showing up unless I click physically inside the tableviewer after performing the modifcation. Please note that the tableviewer's selection remains the same after the modification has been performed as well. I know the update in the StyledCellLabelProvider is being called after I call refresh on the tableviewer.
My question is why is it not showing up without clicking?
MyStyledCellLabelProvider
public void update(ViewerCell cell) {
if (cell.getElement() instanceof Foo && ((Foo)cell.getElement()).hasBeenModified()) {
cell.setImage(myImage);
}
cell.setText("ABCD");
}
TableViewerComposite
#Override
public void notifyModificationMade(Object[] modifiedObjects){
//Update input on tableviewer
...
//Refresh after updating
tableViewer.refresh(); //Triggers the StyledCellLabelProvider to be called
}
Thanks again!
EDIT:
So one piece of information I forgot is that the cell that is being updated will sometimes have a org.eclipse.ui.forms.widgets.Hyperlink already existing inside of it instead of text and will continue to have it after the image has been added. I think this is what is causing the issue. When I removed the hyperlink code and went back to text, it worked as intended. Here is the code I'm using to add the HyperLink to the cell.
public void update(ViewerCell cell) {
if (cell.getElement() instanceof Foo && ((Foo)cell.getElement()).hasBeenModified()) {
cell.setImage(myImage);
final Hyperlink link = new Hyperlink((Composite) cell.getViewerRow().getControl(),
SWT.UNDERLINE_LINK);
link.setUnderlined(true);
link.setBackground(link.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
link.setForeground(link.getDisplay().getSystemColor(SWT.COLOR_BLUE));
link.setText("My url");
link.setHref(url);
link.addHyperlinkListener(new HyperlinkAdapter() {
...
});
TableItem item = (TableItem) cell.getItem();
TableEditor editor = new TableEditor(item.getParent());
editor.grabHorizontal = true;
editor.grabVertical = true;
editor.setEditor(link, item, cell.getColumnIndex());
editor.layout();
}
}

You need to call super.update(cell); after your code inside update
From the source code of StyledCellLabelProvider.java, the update method says
/*
* (non-Javadoc)
*
* #see org.eclipse.jface.viewers.OwnerDrawLabelProvider#update(org.eclipse.jface.viewers.ViewerCell)
*/
public void update(ViewerCell cell) {
// clients must override and configure the cell and call super
super.update(cell); // calls 'repaint' to trigger the paint listener
}
The super class OwnerDrawLabelProvider.java forces a redraw of cell.

Related

Vaadin grid - save/cancel when setEnableEditor= true

I'm new to vaadin and I'm a bit confused with the save and cancel button when setEditorEnabled = true.
Do you need to provide additional codes in order to save the data or it automatically saves all the data to the database when you click on save?
If there are aditional codes, how do I add a listener to the save and cancel buttons?
Thanks!
If you use buffered mode, the edited values are written to the source object when you press the Save button. If you use unbuffered mode, the edition is written instantly, so the Save and Cancel button becomes meaningless.
If you want to write the edited object back to a database, you will need to add that functionality manually. It is practical to use buffered mode in this case and add the database calling method to a method that is called when the save button is pressed.
Besides adding it to a CommitHandler's postCommit method, like Daniel Dubec writes, you can also override the saveEditor() and doCancelEditor() methods of the Grid.
class MyGrid extends Grid {
public MyGrid() {
setEditorEnabled(true);
setEditorBuffered(true);
}
#Override
public void saveEditor() throws CommitException {
super.saveEditor();
// You can persist your data here.
Notification.show("Item " + getEditedItemId() + " was edited.");
}
// Be aware that doCancelEditor() is called whenever super.saveEditor() is called!
#Override
protected void doCancelEditor() {
super.doCancelEditor();
// editedItemId was already set to 'null'.
Notification.show("Cancel button was pressed");
}
}
What super.saveEditor() does is actually calling the commit() method on the editorFieldGroup. But this is only meaningful, if the editable grid is in buffered mode. Read more on Field Buffering here.
So what happens is, when you press the save button, and super.saveEditor() is called, then first the pre-commit event is fired, then the the changes in the editor field values are updated to the data source (that is the commit itself), then the post-commit event is fired. The doCancelEditor() method is called whenever the editor itself is closed, this is why it is called after a save too.
Or use saveListener. I am not sure if this is the best way, but it works for me.
Grid<Bean> grid;
grid.getEditor().addSaveListener(new EditorSaveListener<Bean>() {
#Override
public void onEditorSave(EditorSaveEvent<Bean> event) {
// You can persist your data here
persistBean(event.getBean());
}
});
try to add CommitHandler for FieldGroup
grid.setEditorEnabled(true);
// register save listener
grid.getEditorFieldGroup().addCommitHandler(new CommitHandler() {
#Override
public void preCommit(CommitEvent commitEvent) throws CommitException {
}
#Override
public void postCommit(CommitEvent commitEvent) throws CommitException {
// You can persist your data here
Notification.show("Item " + grid.getEditedItemId() + " was edited.");
}
});

Vaadin 7 ComboBox rendering

So I am experiencing some odd behaviour using Vaadin 7 and the ComboBox component. Essentially, what is happening is that when it first renders the form, it appears to neither have selected the null selection nor any of the items added.
I have attempted to recreate this behaviour with the following code and this demonstrates the issue.
#Override
protected void init(VaadinRequest request) {
final FieldGroup binder;
FormLayout form = new FormLayout();
form.setMargin(true);
setSizeFull();
setContent(form);
final Label output = new Label();
form.addComponent(output);
ComboBox box = new ComboBox("My Dropdown");
final PropertysetItem fields = new PropertysetItem();
fields.addItemProperty("country", new ObjectProperty<String>(""));
binder = new FieldGroup(fields);
binder.bind(box, "country");
box.addItem("aus");
box.setItemCaption("aus", "Australia");
box.addItem("uk");
box.setItemCaption("uk", "United Kingdom");
box.setRequired(true);
box.setImmediate(true);
box.setRequiredError("Country is required field");
Button submit = new Button("Submit", new ClickListener() {
#Override
public void buttonClick(ClickEvent event) {
try {
binder.commit();
output.setValue((String) fields.getItemProperty("country").getValue());
}
catch (CommitException e) {
Notification.show("fail!");
}
}
});
form.addComponent(box);
form.addComponent(submit);
}
By default the ComboBox has allow null selection set to true. So there is a blank entry, which represents a null value selection. However the ComboBox's value when first rendered neither represents the null selection nor one of the items but an empty string.
So when I load the form and click the button the outcome is neither a failure, which it should be because I haven't selected anything yet, nor one of my selections.
This is causing an issue for me in a more advanced UI application but very much the same thing going on here.
Could anybody enlighten me as to what is happening here?
Many thanks,
Joe
So when I load the form and click the button the outcome is neither a
failure, which it should be because I haven't selected anything yet,
nor one of my selections.
Combobox is not empty as you think. It has default property value, that you set as empty string:
fields.addItemProperty("country", new ObjectProperty<String>(""));
Thus form pass validation, because empty string is also a value and empty string != null.
Change this row:
fields.addItemProperty("country", new ObjectProperty<String>(""));
to:
fields.addItemProperty("country", new ObjectProperty<String>(null, String.class));
box.setNullRepresentation("-- Select Country --");

How to ignore the mouse events except the last one in Eclipse RCP

See below for code sample, the method handleMouseDoubleClick method will take seconds to run and open another layout screen containing buttons and links. End users may click many times on one listed item in the table control and create flood of mouse events, how can I handle the last mouse event only?
Table tableControl = (Table) control;
tableControl.addMouseListener(new MouseAdapter()
{
public void mouseDown(MouseEvent e)
{
handleMouseDown(e);
}
public void mouseUp(MouseEvent e)
{
handleMouseUp(e);
}
public void mouseDoubleClick(MouseEvent e)
{
handleMouseDoubleClick(e);
}
}
Create a flag field. Set it to true when handler was called. Initialize it with false.
You just need to check whether your screen is already initialized or not before creating another one.
Set the cursor to hourglass and/or disable the table, resetting these after the new "layout screen" is closed...

how to write lwuit radio button code

I have my form Welcome on this form i have two radio buttons-Verification and enrollment and a OK button .when user select one of radio buttons and press OK then a form will show but i am not able to do that. Please help.
this is my Statemachine class code:
package userclasses;
import generated.StateMachineBase;
import com.sun.lwuit.*;
import com.sun.lwuit.events.*;
import com.sun.lwuit.RadioButton;
import com.sun.lwuit.Form;
import com.sun.lwuit.util.Resources;
public class StateMachine extends StateMachineBase implements ActionListener {
Resources resources;
RadioButton Verification = new RadioButton("Verification");
RadioButton Enrollment = new RadioButton("Enrollment");
StateMachineBase cl = new StateMachineBase() { };
com.sun.lwuit.ButtonGroup bg=new ButtonGroup();
Form fo, f;
public StateMachine(String resFile) {
super(resFile);
// do not modify, write code in initVars and initialize class members there,
// the constructor might be invoked too late due to race conditions that might occur
}
/**
* this method should be used to initialize variables instead of
* the constructor/class scope to avoid race conditions
*/
StateMachine()
{
try {
resources = Resources.open("/NEW AADHAR.res");
}
catch(java.io.IOException err) {
err.printStackTrace();
}
cl.setHomeForm("Welcome");
fo = (Form)cl.startApp(resources,null,true);
f = cl.findWelcome(fo);
//fo.addCommandListener(this);
Verification = cl.findRadioButton1(f);
Enrollment = cl.findRadioButton(f);
f.addComponent(Verification);
f.addComponent(Enrollment);
//fo.addComponent(bg,null);
bg.add(Enrollment);
bg.add(Verification);
Verification.addActionListener(this);
Enrollment.addActionListener(this);
}
protected void initVars() { }
protected void onWelcome_OKAction(Component c, ActionEvent event) { }
public void actionPerformed(ActionEvent ae) {
throw new UnsupportedOperationException("Not supported yet.");
}
protected boolean onWelcomeEXIT() {
// If the resource file changes the names of components this call will break notifying you that you should fix the code
boolean val = super.onWelcomeEXIT();
return val;
}
protected void onWelcome_ButtonAction(Component c, ActionEvent event) {
// If the resource file changes the names of components this call will break notifying you that you should fix the code
super.onWelcome_RadioButton1Action(c, event);
super.onWelcome_RadioButtonAction(c, event);
if(Verification.hasFocus()) {
showForm("Login",null);
}
else if(Enrollment.hasFocus()) {
showForm("Authentication",null);
}
else {
Dialog.show("INFORMATION","Please select option","OK","CANCEL");
}
}
|
When you generate a netbeans project from the GUI builder the src folder will now contain the res file you need to work with. Whenever you modify the GUI code that StateMachineBase will be regenerated so you can just rename the components in the GUI builder (you can do this by clicking on the tree node and pressing F2 or by selecting the name attribute in the properties table).
The properties table allows you to assign an event for every component that supports it (e.g. radio button action events) which will generate the appropriate callback method in the StateMachine class (write your code only in the StateMachine class).
Radio buttons can be associated with one group by giving them the same group name.
The easiest way to do it is to use Resource Editor. Simply run it from LWUIT/util directory.
To create project using this tool follow each step from this video: http://www.youtube.com/watch?v=HOfb8qiySd8. Be sure to watch it to the end.
It will create 4 Netbeans projects (ProjectName, ProjectName_Desktop, ProjectName_MIDP, ProjectName_RIM). Fix depedencies (most important for ProjectName and _MIDP one) and you can start coding.
File StateMachineBase.java will be located in 'generated' package, which means that it will be regenerated every time you change something in Resource Editor.
Implement everything in StateMachine class ('userclasses' package), but don't create new methods there, use Resource Editor to create them for you: Resource Editor -> GUI Builder (tab on left side) -> Select component -> Events (tab on the right).
Now, if you want to do something for example, you want to change TextField value, you will write something like this:
protected boolean onUstawieniaKontoZapisz() {
// If the resource file changes the names of components this call will break notifying you that you should fix the code //this comment was generated
boolean val = super.onUstawieniaKontoZapisz(); //generated
Form current = Display.getInstance().getCurrent();
TextField login = findUstawieniaKontoLoginTextField(current); //TextField name in Editor is: 'UstawieniaKontoLoginTextField' - a bit long I know, but it's unique
TextField password = findUstawieniaKontoHasloTextField(current); //same here, 'UstawieniaKontoHasloTextField' is second's TextField name
Configuration.setEmail(login.getText()); //Configuration class is used to store preferences
Configuration.setPassword(password.getText());
return val; //generated
}
You can find all 'find*' methods inside StateMachineBase class. There is one for each Component you have added using Resource Editor (GUI Builder tab).
For grouping radio buttons into groups use Resource Editor too, select each radio button and on Properties tab find 'Group' property. Set it to the same word on every radio button you want to have in the same group.

Wicket: Can I use a modal Panel with a AjaxFallbackButton?

I have 2 inputs. When I press a button(AjaxFallbackButton), those inputs are saved into database.
If one of the input is greater than 10, when I press the button, I want to show a modal panel, for asking the user if is the sure about his option. But the modal component is not appearing. Any thoughts?
#Override
public void onSubmit(AjaxRequestTarget target) {
if (input < 10) { //save to database
} else {
AskingDialogPanel panel = new AskingDialogPanel("content",
new ResourceModel("asking.title"),
new ResourceModel("asking.message")) {
#Override
public void onOkClick(AjaxRequestTarget target) {
super.onOkClick(target);
//save to database
modalWindow.close(target);
}
#Override
public void onCancelClick(AjaxRequestTarget target) {
super.onCancelClick(target);
modalWindow.close(target);
}
};
panel.setOutputMarkupId(true);
target.addComponent(panel);
modalWindow.setContent(panel);
modalWindow.show(target);
}
Have a look at the documentation for the AjaxRequestTarget.
A component whose markup needs to be
updated should be added to this target
via
AjaxRequestTarget#addComponent(Component)
method. Its body will be rendered and
added to the envelope when the target
is processed, and refreshed on the
client side when the ajax response is
received.
I'm not sure if I remember this correctly (I've had trouble implementing the correct refresh behavior previously), but I believe you could only addComponent components that were previously added to the page, but not rendered / invisible. These will than be updated and/or their visibility re-evaluated.
I could be wrong however.. Does the above work if you substitute a normal Label for the AskingDialogPanel? (Just to verify I'm talking out the wrong end ;))

Categories

Resources