Vaadin grid - save/cancel when setEnableEditor= true - java

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.");
}
});

Related

how to clear the fields in a jface Wizard on pressing the back button

I have created a JFace wizard PCWizard extending Wizard and have four pages
PCPageOne ,PCPageTwo, PCPageThree and PCPageFour extending WizardPage.
When I reach the last page I want the back and cancel button disabled.
when I press the back button on other pages I want the data in the widgets of the page to get cleared and when I press next again I want the text fields to be empty so that the next button doesn't get activated .
I have also captured the data collected in another class, if u want me to override the WizardDialog class and do the action how do I do it . I'm new to java and SWT a more elaborate explanation would be fine.Thanx in advance
Override the WizardPage setVisible method to clear fields when the page becomes active:
#Override
public void setVisible(final boolean visible)
{
super.setVisible(visible);
if (visible) {
// TODO clear your fields
}
}
To disable the back button do the following in the last page:
#Override
public IWizardPage getPreviousPage() {
// prevent going back
return null;
}
Also clearing the input in pages might be done in a IPageChangedListener:
WizardDialog dialog = new WizardDialog(Display.getCurrent().getActiveShell(), wizard);
dialog.addPageChangedListener(new IPageChangedListener() {
public void pageChanged(PageChangedEvent event) {
// this is just a suggestion..
IClearablePage page = (IClearablePage)event.getSelectedPage();
page.clear();
}
});
Where IClearablePage is your own interface with a clear(), and all your pages implement IClearablePage.
EDIT: override setVisible as greg stated in his answer is probably more convenient.

Save on change of CommandStack

probably a really easy newbie question but I can't manage to get it for already a pair of days, so I'll aks it here. My scope: I have an RCP application with some Graphical Editors (extensions of EditorPart). In my editor I want to catch any changes and save them directly. For that I catch the moment in which my CommandStack gets changed and start a doSave method. The problem is that my save method calls a CommadStack change event and if i skip this call than my changes are saved but my editor has still got a dirty flag. My wished behaviour would be that the dirty flag is away(like a normal save behaviour).
Both my CommandStack change and my doSave method are below. Could you please help me?
#Override
public void commandStackChanged(EventObject event) {
firePropertyChange(IEditorPart.PROP_DIRTY);
doSave(null);
setDirty(false);
}
public void doSave(final IProgressMonitor progressMonitor) {
editorSaving = true;
SafeRunner.run(new SafeRunnable() {
public void run() throws Exception {
IFile targetFile = getFile();
List<GraphicalEditPart> editParts = DiagramUtil.getAllEditParts(NetEditor.this);
Rectangle offsetBounds = DiagramUtil.getBounds(editParts);
saveDiagramProperties();
List<INetTransition> transitionsToExpand = saveSubdiagramGroups();
FileUtil.saveDiagram(getNetDiagram(), targetFile,
NetEditor.this);
getCommandStack().markSaveLocation();
for (INetTransition trans : transitionsToExpand) {
trans.setExpanded(true);
}
}
});
setDirty(false);
editorSaving = false;
}
With getCommandStack().markSaveLocation() I call once more the commandStackChanged and get in a loop. How can I solve this problem? Without it my editor remains dirty after save.
Thanks and Greets,
Jeff
How about adding IPropertyListener to your Editor and listen for IEditorPart.PROP_DIRTY. For the listener implementation perform save if editor is dirty otherwise do nothing.

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 Subscribe to GUI Events in Other JFrames

What is the best practice for subscribing to events from another JFrame? For example, I have a "settings" form, and when the user presses okay on the settings form, I want the main form to know about this so it can retrieve the settings.
Thanks.
Here is my ideal interface:
public void showSettingsButton_Click() {
frmSettings sForm = new sForm(this._currentSettings);
//sForm.btnOkay.Click = okayButtonClicked; // What to do here?
sForm.setVisible(true);
}
public void okayButtonClicked(frmSettings sForm) {
this._currentSettings = sForm.getSettings();
}
Someone publishes an Event, that something has changed, here the settings. A subscriber that registered for this specifig event, gets notified about it and can do his work, here get the settings. This is called publisher/subscriber.
For this you can use Eventbus or implementing something smaller on your own.
One approach is to have only a single JFrame. All the other 'free floating top level containers' could be modal dialogs. Access the the main GUI will be blocked until the current dialog is dismissed, and the code in the main frame can check the settings of the dialog after it is dismissed.
For anyone interested, here is what I ended up going with. I'm not sure if it's the best way, but it is working for my purposes.
// Method called when the "Show Settings" button is pressed from the main JFrame
private void showSettingsButton_Click() {
// Create new settings form and populate with my settings
frmSettings sForm = new frmSettings(this.mySettings);
// Get the "Save" button and register for its click event...
JButton btnSave = sForm.getSaveButton();
btnSave.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent evt) {
SaveSettings(sForm);
}
});
// Show the settings form
sForm.setVisible(true);
}
// Method called whenever the save button is clicked on the settings form
private void SaveSettings(frmSettings sForm) {
// Get the new settings and assign them to the local member
Settings newSettings = sForm.getSettings();
this.mySettings = newSettings;
}
And if, like me, you are coming from a .NET perspective, here is the C# version:
private void showSettingsButton_Click(object sender, EventArgs e)
{
frmSettings sForm = new frmSettings(this.mySettings);
sForm.btnSave += new EventHandler(SaveSettings);
sForm.Show();
}
private void SaveSettings(object sender, EventArgs e)
{
frmSettings sForm = (frmSettings)sender; // This isn't the exact cast you need..
Settings newSettings = sForm.Settings;
this.mySettings = newSettings;
}

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