How to Pop a Codenameone Form to show previous Form - java

So I was trying to build a custom UI among which there was back arrow (not the one that can be used from Form's toolbar in Codenameone) like this:
Now the issue is when I want to implement the actionListener of the back button, I don't seem to find the method to pop itself.
Yeah there is native support for back button with toolbar something similar to this :
form.getToolbar().setBackCommand(title, evt -> menuCard.getComponentForm().showBack());
But i want to do something like this :
backButton.addActionListener {
form.dispose()
print("the form should dispose")
}
where I want to hook the logic to dispose / dismiss the form to an actionListener of a Button I can't do that as dispose() on Formis package private .
Then I did a hack
package com.codename1.ui;
import com.codename1.ui.layouts.Layout
class CustomForm constructor(contentPaneLayout: Layout) : Form(contentPaneLayout) {
public override fun dispose() {
super.dispose()
}
}
But calling dispose() on this new Form does not dismiss the form either.
What is the right way to dismiss/dispose/pop a form from its's screen to show the previous form?

Related

GWT: How to Use Place Tokens

As a little example: Take a website where you land on a login page and after a successful login getting directed to the main page where on the left side you'd have a menu and on the right side a content are where everything gets loaded when clicking a link in the menu. In the menu I'd have multiple stores where each store would have its own settings. Those settings would be initialized with an id to determine which content the view should display.
For the history mapper I guess one would have something like this:
/#LoginPlace:login
/#MainPlace:home
/#MainPlace:storeSettings?id=<id>
/#MainPlace:userSettings
etc..
In [2] it says
"In order to be accessible via a URL, an Activity needs a corresponding Place."
Which sounds to me like I should have a LoginActivity and a MainActiviy since after all LoginActivity is the first Place I arrive when I come to my website and MainActivity is the place I go when I am successfully logged in.
I'd have someting like this:
public class LoginActivity extends AbstractActivity implements LoginView.Presenter {
// ..
private void onLoginSuccess() {
this.clientFactory.getPlaceController().goTo(new MainPlace());
}
}
public class MainActivity extends AbstractActivity implements MainView.Presenter {
// ..
}
and of course have respective view LoginView and MainView.
This is how AppActivityMapper.getActivity() would look like:
#Override
public Activity getActivity(Place place) {
if (place instanceof LoginPlace) {
return new LoginActivity((LoginPlace) place, clientFactory);
} else if (place instanceof MainPlace) {
return new MainActivity((MainPlace) place, clientFactory);
}
return null;
}
So far so good but how would I implement the MenuView and the MainContentView?
I want to be able to click on menu items in the MenuView and update MainContentView and generate place tokens accordingly like:
/#MainPlace:home
/#MainPlace:storeSettings?id=<id>
/#MainPlace:userSettings
But I have no idea how to do that and how I would for example initialize my activity StoreSettingsActivity with the given id. I believe that I would need another MainActivityMapper extends ActivityMapper that now should control the place changes initiated by clicking on menu links but I just can't figure out how that could work.
I hope my question is clear, please let me know if you need some more information.
I think I once did something similar, it was not perfect because I had to deal with an existing architecture but it worked.
Here is what I did:
When a link from the menu is click execute placeController.goTo(new MainPlace(id));
In your ActivityMapper, return your MainActivity in which you set the id of the MainPlace.
In your MainActivity, in the start method call a method that will initiate your MainContentView depending on the id of MainPlace you set earlier in the MainActivity.
I am pretty sure there is a better way of doing this, but it works and it can be a start that might help you to find a better solution.

Should action listeners be in the class they apply to?

I'm programming a web browser with multiple classes. One class is "navbar" which holds most of the buttons such as search, back, forward, etc.
The navbar class has action listeners which, when the search button is pressed, become active, and lead to the production of a URL, which needs to be passed to the JEditorPane, but the editor pane is in a different class, "editor".
It does not make sense for an editor to be instantiated inside navbar, so how can i pass the variable from the navbar class to the editor class?
Is it okay to use statics in this situation?
Why not simply give the nav bar a reference to the editor? This could be done in the constructor, if the editor is instantiated first, or via getter/setter pair (actually, only the setter is really needed...). If the nav bar has such a reference, it can call the appropriate methods from within the listener implementation.
class Editor { ... }
class NavBar implements ActionListener
{
public NavBar(Editor editor)
{
myEditor = editor;
}
public void actionPerformed (ActionEvent e)
{
// call methods of myEditor
}
private Editor myEditor;
}
Editor theEditor = new Editor();
NavBar theNavBar = new NavBar(theEditor);
You can use some kind of intermediate interface between these, typically a service like layer. Which is basically what the observer pattern is as well. The editor could list events the navbar produces, but that also means later on you could also catch these events in other components

How to disable DatePicker in GWT?

As You know com.google.gwt.user.datepicker.client.DatePicker haven't method setEnabled(boolean). I have DatePicker with ValueChangeHandler and all what I need is disable datepicker (code in onValueChange shouldn't work).
Of course I can do:
boolean disable;
datePicker.addValueChangeHandler(new ValueChangeHandler() {
#Override
public void onValueChange(ValueChangeEvent<Date> event) {
if(!disable) {
// my code here
}
}
});
but I won't do this. I want write something like that:
datePicker.setEnabled(false);
Any ideas?
GWT 2.3.0
I haven't used GWT in a while, so this is more of a pointer than an answer, but I wanted to include several links so used the answer box ;).
DatePicker is a Composite, so I don't think you can't enable/disable it directly. But you can add a preview handler to disable events, or throw a panel over it (and grey it out, for instance). See this answer for info on that: Disable user interaction in a GWT container?
As a general note, everything that extends from FocusWidget is enable/disable-able, but composites are collections of other widgets so they work differently.

Is it possible to add a message bar/ toolbar at the bottom of a titleareadialog?

With my TitleAreaDialog is it possible to add a area or a bar across the bottom, below the buttons. That a message can be displayed to the users, when a operation is taking place.
Here is a example of what I am referring to
AFAIK, this is not possible for JFace Dialogs. Depending on what exactly you are doing, you might want to have a look at JFace ApplicationWindow. This class has a method addStatusLine(). You would have to override the following method:
#Override
protected StatusLineManager createStatusLineManager() {
StatusLineManager statusLineManager = new StatusLineManager();
statusLineManager.setMessage(null, "YOUR_MESSAGE");
return statusLineManager;
}
You can change the text with:
getStatusLineManager().setMessage("YOUR_NEW_MESSAGE");
Here is an excellent overview of the ApplicationWindow class.

AbstractAction as WindowListener

I'm trying to separate function from state in my GUI application by the use of Action objects. I've been successful in using these to create menu items and buttons that have the same functionality.
My problem is this: I want to have the same Action for both the 'exit' item in my menu and the close button of the frame.
Currently I've been able to solve it by adding the following WindowListener to the frame:
private class MainWindowListener extends WindowAdapter {
#Override
public void windowClosing(WindowEvent e) {
new ExitAction(model).actionPerformed(new ActionEvent(e.getSource(), e.getID(), "Exit"));
}
}
Isn't there a simpler more straightforward way to do this?
Forwarding events is handy, but you can also use dispatchEvent(), as shown here.
Addendum: More examples that use Action are shown below.
LinePanel which connects buttons and keys.
ScrollAction which leverages existing Swing actions.
KeyPadPanel which illustrates forwarding actions.
GraphPanel which shows graph editor actions in a tool bar.

Categories

Resources