Apply and OK Button in Eclipse PreferencePage - java

What is the standard and recommended way the apply and ok button in eclipse preference page should work.
I checked and found that performOK() method is called when we click apply or ok button. It means if I have some computations or let say thread starting on in performOK() and the user first click on apply and then ok button it will be executed twice and if the user clicks on apply and cancel the changes will be applied anyways?
Is there a way to not execute the code twice if the user clicks on apply and then ok ?
#Override
protected void performApply() {
this.performOk();
}
#Override
public boolean performOk() {
PreferencesUtil.savePreferences();
return super.performOk();
}
Thanks

It is up to you to remember that Apply has been run by overriding performApply and setting a flag. You can then test the flag in performOk and skip doing the same thing.
Be sure to clear the flag if the user changes something after pressing Apply.
So something like:
private boolean saveDone = false;
public boolean performOk() {
if (!saveDone) {
saveDone = true;
store.setValue(Constants.ENABLE_DEFAULT_COLOR, this.defaultColoringCheckBox.getSelection());
PreferencesUtil.addToPreferenceStore(viewer.getTable());
PreferencesUtil.savePreferences();
}
return super.performOk();
}
Set saveDone = false if anything is changed in the page.

I think it would make sense to extract the functionality to be executed while the user wishes to apply the changes in a separate method. This method is called from apply AND ok.
I would not call "performOK" from within "performApply". In performOk the additional closing of the preferences Dialog is then performed by the super method, I suppose.

Related

How can I show a confirm dialog with Vaadin 23 when the user clicks on back button?

how can I show a dialog to stay or leave the current page with Vaadin 23, when a user clicks back button on browser?
Regards
It depends what you wish to achieve.
See this older discussion: Vaadin onbeforeunload event
Generally: use the onBeforeUnload javascript even for this
https://www.w3schools.com/tags/ev_onbeforeunload.asp
This is executed when the user would go away from your vaadin app, but not when using the back button inside your vaadin app.
For these you can use the navigation lifecycle events as documented here
https://vaadin.com/docs/latest/routing/lifecycle
Not sure if it also catches, when a user leaves your app...
Assuming you mean, that is it possible to prevent the navigation happening, you simply can't do that. If disabling back button is important for you, the only way is to enforce your users to use the application via desktop shortcut which starts the app using --app paramater (if using Chrome). This is not a limitation in Vaadin, but a general restriction in browser behavior.
There is already a possibility to handle Browser Back Button Event on Vaadin (https://vaadin.com/docs/v14/flow/routing/tutorial-routing-lifecycle):
public class SignupForm extends Div implements BeforeLeaveObserver {
#Override
public void beforeLeave(BeforeLeaveEvent event) {
if (this.hasChanges()) {
ContinueNavigationAction action = event.postpone();
ConfirmDialog.build("Are you sure you want"+
" to leave this page?")
.ifAccept(action::proceed)
.show();
}
}
private boolean hasChanges() {
// no-op implementation
return true;
}
}
This code works once but when you click on Cancel on Confirm Dialog so that you want to stay on current page and click again on Back Button on Browser, than you don't see any Confirm Dialog again... I can not understand, why...

How do I overwrite back button of BSImagePicker gallery?

I'm new new in Java and working on an old project that uses bsImagePicker. There's a bug in my current project that when the user wants to click the below back button of the gallery it takes the user to home page.
This shouldn't be the default behavior, rather it should take the user 1 step back. Please, How do I overwrite the method of this back button? Unfortunately, I couldn't find any xml file that has referenced this button neither there's any previous overwritten method.
Thanks in advance.
Edit: I searched more and figured out maybe it has to do something with getSupportActionBar().setDisplayHomeAsUpEnabled() but I don't know how to set its android:parentActivityName attribute.
To override onBackPressed() method and implement your own functionality or behavior.
#Override
public void onBackPressed() {
//implement your own functionality in this
}
But since you are trying to implement it for back button shown in attached image and you don't have reference of it. Either you can create a reference by own or you can try on onCancelled method as provided in BSImagePicker gallary github repo.
//Optional
#Override
public void onCancelled(boolean isMultiSelecting, String tag) {
//Do whatever you want when user cancelled
}

App behaves different after exit and re-enter

Cordova, Version 3.5.0-0.2.6
<body><script>
alert("documentready");
document.addEventListener("deviceready", function() {
alert("deviceready");
}, false);
</script></body>
I enter the app after deploying, I get 'documentready' and 'deviceready' alerted.
I leave the app with the back button.
I get 'documentready' only.
When I force-close the app with the taskmanager or re-deploy it, I get both alerts.
I want this behaviour to occur also after normal re-entering the app.
I would prefer a solution where there is no evidence left that the app has been opened before, after I leave it. Nothing restored from garbage collection etc. Ideally executing the same log as the force-close method from the task manager.
OK: I want, when I close my app via back button, that exactly the same happens, as when I open the task manager and force my app to close. Is this at least theoretically possible?
Alternatively, I would like the app, when left via back-button, to be in a 'hibernate-like' state, that if I re-enter it it behaves absolutely like it has never been left (call same logic as when the menu/home button is pressed).
QUESTION STILL OPEN - 50RS BOUNTY TO EARN
Add this to your mainActivity.java (whatever it is called in your project):
#Override
public void onBackPressed() {
finish();
}
I'm not sure why you'd want such behavior, but you can kill the app on back press (or on finish).
Calling android.os.Process.killProcess(android.os.Process.myPid()) is just like a forcing a stop from the task manager.
Add this to your CordovaActivity and it should kill everything without any remains:
#Override
public void onBackPressed() {
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
I've implemented the same thing, I used the backbutton event from cordova. You can find more about the backbutton event here: link. I can't find documentation about the app.exitApp() function but I do know it is only available for android and amazon-fireos.
See code snippet below, you only need to know when you want to exit, but you might know that from the document.location object or something. Hope it helps.
document.addEventListener("backbutton", function (evt) {
// replace this with some logic (maybe document.location) to now if you are on the main page or not
if (true) {
// Check if methods exists
if (typeof navigator.app !== "undefined" && typeof navigator.app.exitApp !== "undefined") {
evt.preventDefault();
navigator.app.exitApp();
}
} else {
history.back();
}
}, false);
This should work:
In my_app_dir->config.xml add
<preference name="KeepRunning" value="false" />
And below
document.addEventListener("deviceready", function() {...
add
document.addEventListener('backbutton', function() {
navigator.app.exitApp();
}, false);
Afterwards open cmd, go to your project folder and run cordova build android; cordova run --device android;
What works for me, the problem might be that the DOM is not ready when you add the event listener. And may be caused by a faster load due to the app being cached. Use a self executing function to add the event listener and you will be sure the DOM is loaded.
function domIsReady() {
alert('DOM is ready')
}
function deviceIsReady() {
alert('Device is Ready')
}
( function() {
if (document.readyState === "complete") {
domIsReady();
} else {
if (window.addEventListener) {
window.addEventListener('DOMContentLoaded', domIsReady, false);
} else {
window.attachEvent('onload', domIsReady);
}
};
document.addEventListener("deviceready", deviceIsReady, true);
}());

Any workarounds to lack of PreSelection events in SWT/JFace?

In my application I want the user to save any changes before he leaves a tab (implemented as CTabFolder).
I tried to handle SelectionEvent, but it fires after the tab has been changed (so why does it even have a doit field? Does it fire before change for some other controls?)
Looking on Bugzilla, I've found https://bugs.eclipse.org/bugs/show_bug.cgi?id=193453 and https://bugs.eclipse.org/bugs/show_bug.cgi?id=193064, neither of which is fixed.
Since this requirement is probably common, does anybody have a workaround?
I have a workaround that works with org.eclipse.ui.part.MultiPageEditorPart which is backed by a CTabFolder. I'll adapt it for a straight CTabFolder implementation.
First off use the selection listener:
tabFolder.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
pageChange(tabFolder.indexOf((CTabItem) e.item));
}
});
Then I implement pageChange() like this:
protected void pageChange(int newPageIndex) {
boolean changingPages = this.changingPages;
this.changingPages = true;
int oldPageIndex = tabFolder.getSelectionIndex();
if (isDirty() && !changingPages) {
tabFolder.setSelection(oldPageIndex);
if (canChangePages()) {
tabFolder.setSelection(newPageIndex);
}
}
this.changingPages = false;
}
In canChangePages() I pop up a do you want to save dialog and give the user an opportunity to select yes, no, or cancel. Yes saves the info and returns true. No reverts the info to the last saved state and returns true. Cancel simply returns false. You may simply want to try saving and return false only if the save fails.
It may look weird that I switch back to the old page before calling canChangePages(). This call executes quickly so it gives the illusion the tab never switched. No matter how long canChangePages() takes the user will not see a tab change unless it is approved by that method.

In GWT, how to reset the URL when the user hits "Cancel" in the navigation confirmation dialog?

In my GWT application, I want to ask a user confirmation when he navigates out of the current application, i.e. by entering a URL or closing the browser. This is typically done by registering a ClosingHandler and setting the desired dialog message in the onWindowClosing method. This seems to work well.
However, if the user tries to navigate say to http://www.gmail.com (by typing it in the URL bar) and hits Cancel to indicate he doesn't want to navigate, then my app keeps running but the browser's URL bar keeps indicating http://www.gmail.com. This causes a number of problems later in my application and will give the wrong result if the user bookmarks the page.
Is there a way to automatically reset the URL when the user presses Cancel?
Or, alternatively, is there a way to detect the user pressed the Cancel button? If so, is there a way to set the URL without triggering a ValueChangeEvent? (I could add some logic to prevent this, but I'd rather use a built-in mechanism if it exists.)
Not sure if this works but did you try: History.newItem(History.getToken(), false); to reset the URL? It does set the history token without triggering a new history item.
I managed to do this. It looks like GWT DeferredCommand are executed after the confirmation window has been closed. This, combined with Hilbrand's answer above, give me exactly what I want. Here is exactly what I do:
public final void onWindowClosing(Window.ClosingEvent event) {
event.setMessage(onLeaveQuestion);
DeferredCommand.addCommand( new Command() {
public void execute() {
Window.Location.replace(currentLocation);
}
});
}
Where currentLocation is obtained by calling Window.Location.getHref() every time the history token changes.
I solved this by creating a custom PlaceController and replacing the token in the url. Not an ideal solution but it works!
if (warning == null || Window.confirm(warning)) {
where = newPlace;
eventBus.fireEvent(new PlaceChangeEvent(newPlace));
currentToken = History.getToken();
} else {
// update the url when user clicks cancel in confirm popup.
History.replaceItem(currentToken, false);
}

Categories