Mvc4g is a simple framework to implement the MVC pattern of GWT applications.
Main Ideas
The main idea is to reduce developers' work by taking a separate view from the model. This framework is an XML file that allows developers to tell the view what actions they need to perform when launching an event.
How the framework works
The procedure is as follows:
Event
Is to create an active View Controller. The event contains two parts:
Name of the executed action
Object To action
UserBean user = new UserBean();user.setName("John Smith");new Event("CreateUser", user);
Controller
The Controller receives the event and executes the action based on the name of the event action.
For example, if you have the following event triggers:
Event e =newEvent("doOperation","+");
The following mvc4g configuration file:
<actionname="doOperation"class="com.mvc4g.example.client.OperationAction"/>
The Controller then calls the following instance:
com.mvc4g.example.client.OperationAction
To create a view, you need:
Implement com. mvc4g. Client. viewinterface
There is a default Structure
The view can create handleevent functions called by events and fire controllers.
Event e =newEvent("doOperation","+");
controller.handleEvent(e);
Detailed examples
Action
package com.mvc4g.example.client;import com.mvc4g.client.ActionInterface;import com.mvc4g.client.Controller;public class OperationAction implements ActionInterface { private int value = 0; @Override public void execute(Controller controller, Object form) { //Execute action String operation = (String) form; if("+".equals(operation)){ value++; } else if("-".equals(operation)){ value--; } //Update the view ((SimpleCalculatorView)controller.getView("mainView")).updateScreen(Integer.toString(value)); }}
View
package com.mvc4g.example.client;import com.google.gwt.event.dom.client.ClickEvent;import com.google.gwt.event.dom.client.ClickHandler;import com.google.gwt.user.client.ui.Button;import com.google.gwt.user.client.ui.Composite;import com.google.gwt.user.client.ui.HorizontalPanel;import com.google.gwt.user.client.ui.RootPanel;import com.google.gwt.user.client.ui.TextBox;import com.google.gwt.user.client.ui.VerticalPanel;import com.mvc4g.client.Controller;import com.mvc4g.client.Event;import com.mvc4g.client.ViewInterface;public class SimpleCalculatorView extends Composite implements ViewInterface { private Controller controller = null; private TextBox screen = new TextBox(); public SimpleCalculatorView(){ screen.setWidth("50px"); screen.setEnabled(false); screen.setText("0"); Button add = new Button("+1"); add.addClickHandler(new ClickHandler(){ @Override public void onClick(ClickEvent event) { //Create and fire event to the controller Event e = new Event("doOperation", "+"); controller.handleEvent(e); } }); Button less = new Button("-1"); less.addClickHandler(new ClickHandler(){ @Override public void onClick(ClickEvent event) { //Create and fire event to the controller Event e = new Event("doOperation", "-"); controller.handleEvent(e); } }); HorizontalPanel buttons = new HorizontalPanel(); buttons.add(add); buttons.add(less); VerticalPanel mainPanel = new VerticalPanel(); mainPanel.add(screen);mainPanel.add(buttons); RootPanel.get().add(mainPanel); } public void updateScreen(String value){ screen.setText(value); } @Override public void setController(Controller controller) { this.controller = controller; } }
Control Layer
To create an action, you need:
Implement com. mvc4g. Client. actioninterface
There is a default Structure
The action execution function called by the Controller. In this function, you need code action. To update the view, you can retrieve the Controller for the action. The controller calls the getview function. Thank you. To get a correct view, you need to name the view for the action.
For example, if you have the following calls:
controller.getView("mainView")
The following mvc4g configuration file:
<viewname="mainView"class="com.mvc4g.example.client.SimpleCalculatorView"/>
Examples of the following points of view that you will retrieve:
com.mvc4g.example.client.SimpleCalculatorView
Action
package com.mvc4g.example.client;import com.mvc4g.client.ActionInterface;import com.mvc4g.client.Controller;public class OperationAction implements ActionInterface { private int value = 0; @Override public void execute(Controller controller, Object form) { //Execute action String operation = (String) form; if("+".equals(operation)){ value++; } else if("-".equals(operation)){ value--; } //Update the view ((SimpleCalculatorView)controller.getView("mainView")).updateScreen(Integer.toString(value)); }}
View
package com.mvc4g.example.client;import com.google.gwt.event.dom.client.ClickEvent;import com.google.gwt.event.dom.client.ClickHandler;import com.google.gwt.user.client.ui.Button;import com.google.gwt.user.client.ui.Composite;import com.google.gwt.user.client.ui.HorizontalPanel;import com.google.gwt.user.client.ui.RootPanel;import com.google.gwt.user.client.ui.TextBox;import com.google.gwt.user.client.ui.VerticalPanel;import com.mvc4g.client.Controller;import com.mvc4g.client.Event;import com.mvc4g.client.ViewInterface;public class SimpleCalculatorView extends Composite implements ViewInterface { private Controller controller = null; private TextBox screen = new TextBox(); public SimpleCalculatorView(){ screen.setWidth("50px"); screen.setEnabled(false); screen.setText("0"); Button add = new Button("+1"); add.addClickHandler(new ClickHandler(){ @Override public void onClick(ClickEvent event) { //Create and fire event to the controller Event e = new Event("doOperation", "+"); controller.handleEvent(e); } }); Button less = new Button("-1"); less.addClickHandler(new ClickHandler(){ @Override public void onClick(ClickEvent event) { //Create and fire event to the controller Event e = new Event("doOperation", "-"); controller.handleEvent(e); } }); HorizontalPanel buttons = new HorizontalPanel(); buttons.add(add); buttons.add(less); VerticalPanel mainPanel = new VerticalPanel(); mainPanel.add(screen);mainPanel.add(buttons); RootPanel.get().add(mainPanel); } public void updateScreen(String value){ screen.setText(value); } @Override public void setController(Controller controller) { this.controller = controller; } }