MVC Architecture and Modal Dialog Windows - java

I am developing a project in a MVC architecture. It should be a simple application to manage some customers.
There are MainModel, MainView and MainController classes which make the main window to show the content of the customers tables and to let the user insert, remove or edit customers.
My problem is that the insert and edit buttons should show some dialog windows to let the user insert and edit some text values and I have some doubts.
I would like to ask you some questions:
Should I use the MVC architecture for each dialog window?
If yes, I have tried doing it but my dialog windows are modal, so my code runs the model, runs the view but it gets blocked in the view and it doesn't run the controller class. How could I solve it?
For example here it gets blocked in the "new InsertCustomerController..." instruction:
CustomerModel customerModel = new CustomerModel();
InsertCustomerView insertCustomerView = new insertCustomerView(customerModel);
new InsertCustomerController(insertCustomerView, customerModel);
Thank you very much.

Irrespective of modality, you can use the observer pattern to keep your dialogs synchronized with the application's model. This example uses a PropertyChangeListener; other approaches are mentioned here.

Although I can't fully tell from your post, I'm not sure you're thinking of MVC the right way. But let's say you have a Customer, CustomerView, and CustomerController class..
Customer could contain all of the business logic related to being a customer-- so it might have methods like setBalance(int newBalance), getBalance(), etc.
The CustomerView class could essentially be a subclass of JPanel or JFrame (since it looks like you're using Swing from your question's tags). This class would be representative of one Customer instance. Maybe you could have a private Customer class variable. This class's responsibilities should only consist of displaying the data to the user contained in its Customer instance as well as allowing them to modify it.
It would be tough to say what the CustomerController would do since I don't know anything about your application, but it could potentially contain ActionListeners and that sort of thing that help to route input and output to the different parts of your model and view.
I did some googling and found a pretty straightforward example you may want to check out: http://www.austintek.com/mvc/
Good luck. Hope this helps.

Related

Revert (Undo) implementation in GWT

We are trying to build a GUI framework using GWT. We are finding it hard to implement the cancel functionality in the framework.
Required feature is this:
We have CRUD screens which have pop-ups, grids and so on. When the user changes anything in the GUI and then clicks on cancel() he should be given a notification message saying that something has changed.
Approach that we have tried:
Currently we are trying to keep a hashmap of key vs value of the entire pojo object and trying to compare it against the model which gets updated as and when user changes something. But this is adding lot of unwanted code in every pojo and not working as expected when user adds data directly from the backend.
Is there any elegant way in achieving this functionality? Kindly note that *we are not using Editor framework of GWT *(https://developers.google.com/web-toolkit/doc/latest/DevGuideUiEditors) in our application.
Example:
Suppose I have a pojo like this:
public class Person {
List<Address> address;
PhoneNumber phoneData;
// and so on along with getters and setters
}
How will I write a generic clone method for this? And even if I manage to do that somehow that will lead to lot of code in every pojo (our application has hundreds of them) which doesn't seem right.
Please note that, our pojo gets updated as soon as something is changed in GUI to achieve live binding.
So you have "Save" and "Cancel" buttons in your form?
I would recommend you to change the concept. Update your object properties immediately as user edit them (as in GMail, JIRA and many other modern applications) in an OnChange event handler.
Save all updates to the session stack as UpdateAction objects and let the user undo every single property modification calling UpdateAction.undo() method.
The benefits are:
this design is much more user friendly than "Click "Edit" - update - click "Save"" scenario.
You don't need separate view/edit forms/popup dialogs - just a single form for both viewing and editing.

Java: Structure a users control system using OOP

I hope I can explain.
I don't have a problem, just I want to know how to build my users system control and I want to know if my idea is correct:
My idea:
*One class (file 1) to functions, methods and operations
*Other class (file 2) to swing elements, buttons, textbox, etc.
*But I don't know how connect these two classes.
For example:: I have 2 textbox (user, password) and one button, then when press the button, send data of each textbox(file 2) to a function (file 1) and process.
If you have any other choice, I'd read it.
Thank you in advance
You should use the Model-View-Controller pattern to solve your problem. Basically, your view (the swing files) only know how to display content, and let the user of the class (the controller) subscribe to receive notifications when the content is changed (this is done with swing listeners). When the content is changed the controller can act on it, modify the model if necessary and also update the view (again, using methods provided by the view).
The GUI should never involve functionality. It should only have code that is used to display what you want. This helps create a better architecture.

Unsure about using the MVC design pattern

I'm trying to use the MVC design pattern for my project but I'm sort of unsure about how to decouple my program into the classes. The first part of my program is a Login screen which requires the user to enter a their username and password and a start button which checks the details, and there is a button to go to a page where you can add a new user. So I was thinking for my MVC design:
loginpanelView : Just the GUI with the text boxes, labels, buttons etc
loginpanelController:
- implement the actionlistener for the start button here and have a reference to the method checkLogin
- implement actionlistener for add user button here and have reference to a method which switches the panels
loginModel:
- defines the actual method which checks the login
switchpanelModel:
- defines a method which creates a cardlayout system and switches the panels
My understanding is that the controller just makes very general references to what needs to be done i.e. sort of what the user wants to happen, then the model defines the exact method of how to handle this? Would someone mind verifying/ correcting my understanding please? I've read a lot about this design pattern but unfortunately I still don't feel like I have a clear understanding of it. Any help would be much appreciated!
p.s. Sorry! I forgot to include that I'm programming in Java
It sometimes helps to think of MVC in terms of dependencies.
The model repesents what your application does. It has no dependencies on anything. It is what makes your application unique.
The view displays information to the user. This information comes from the model. Therefore, the view has a dependency on the model.
The controller's function is to accept input from the user, dispatch that request to the appropriate model functionality, and (normally) accept the return value and supply it for a view to render. Thus, the controller is usually very tightly coupled to the view(s) that it serves. It also has dependencies on the model.
In this case, the model is your authentication scheme. (In reality, this is not all that much of a model but an entry point in your application, your overall model is something like "process payments", "generate report", "request to create widget", etc.)
You have two views, one to enter authentication information and a second for when an authentication succeeds. The first really does not have any model information, it is solely to collect input (however its design will be based on whatever the authentication model needs, so there is still a dependency here). The second will undoubtedly display a list of available features your application offers or display a landing page etc.
It is the controller's responsibility to mediate these interactions. Therefore, information sent from the first view is received by the controller, dispatched to the authentication model, authentication succeeds or fails, and then the controller chooses the appropriate view to render based on the result.
With such a basic "functional design" it's hard to help you exactly, but you might want to think more about the big picture about what you want.
A user model - database model for a user. Contains a "check login"
method
A login-page View - Form, layout etc
A login controller - Gets the stuff out of the form, tries to log someone in with the method from the user object, and create said user
object
The page view/controllers can be split up ofcourse in several sub-parts, but this might not be a bad place to start.
It seems to me that LoginModel and SwitchPaneModel are not models at all. Model is what you store somewhere. So you will have UserModel and PaneModel. And your controller will implement switchPane method and login method. It's good idea to decouple this method in some separate classes there are lots of methods to perform this task. But I strongly recommend you to find ready solution. Don't invent the bicycle.
A good place to start is here. This is a special case of MVC called Passive View. The first important idea is that the view and the model do not communicate with each other at all. The view only tells the controller about events, and the controller manipulates both the view and the model. A controller can even create new controllers and views (such as for complex modal dialogs). And finally, the model does not communicate with anyone!
So you have the right idea: your loginpanelController listens for button events from the loginpanelView, and then calls the right methods in the model to set the data and validate it.
I think one place you may be having a problem with is switchpanelModel. I don't think you need this. If your loginpanelView is the view with the cards in it, then your loginpanelController should be the one switching the cards.
I think models should be restricted to methods working with its own data, but must have no reference to any GUI element anywhere. Models do not drive the program; controllers do.
Rather then thinking in terms of 'defining' a method, perhaps it is better to think in terms of what is being encapsulated.
For example, loosely, in MVC a view encapsulates primarily the user interface of your program (a login form), a model encapsulates some part of your domain logic (password authentication) and a controller encapsulates the logic that connects a view with a model (it depends there are variation of MVC architecture). The controller is often to some extent coupled to a view (especially if you start adding overtly specific ActionListeners etc) however the model should be quite reusable/exchangable (changing how you validate should not mean you have to change any view/controller that uses it)

Java action listener question

I am creating a custom JPanel element (a login form).
I want to allow people who use my panel to subscribe/listen to an event called "loginSuccessful".
What is the best way to implement this in my JPanel object?
UPDATE: oh and i also want to add that when that action is triggered, i also want to return a "User" object containing the person that was just logged in
I normally prefer EventBus for those kinds of Events.
Library and Examples can be found here
Moreover you should consider to keep businesslogic out of your viewclass (panel) and create some kind of LoginController for your loginbusinesslogic. There are plenty of good examples out there.
EDIT: You can send an UserObject within an EventBusEvent as well.
I would start by separating your code form widget code. Don't extend where you don't need to. Where classes are focussed on a particular job, everything becomes so much easier.

More swing design & actions

Im pretty new to gui programming so i've been reading through every post on this site about swing and design. Whats been answered over and over again is that one should have a multiton class for the actions. Like this: (GUI being some JFrame)
alt text http://img341.imageshack.us/img341/255/skjermdump.png
Now, this works great for one-way actions, like OpenDialog. But the actions for buttons in DialogA and B will have to have access to all the components (there will be many) in its dialog, and the controller. This is where im stuck.
The only sane way i can see is to put it in DialogA/B but i would then need to pass the controller all the way down, through classes that dont even need it, and it'll get all spaghetti. Really dont want that.
Someone must have encountered this problem before. So where should i put this Action? Or should i just drop the whole design?
Edit: got a good answer from elsewhere. Resolved.
In MVC the controller and the view access each other, the controller shields the view from the model. The best thing to do is to put your ActionHandler as anonymous class and have it simply call back to your view that in turn calls the controller.
If you really want you could have a Controller superclass that has generic messages to send a message and pass in a HashMap, that gives you good separation of code but, adds complexity and removes type checking.

Categories