I am trying to write an MVC based application from scratch using Java 8 and Swing... I am having some difficulties with this.
I want to know if I am going the correct way about this...
My controller code is:
public class Controllerv2 {
private final Map<String, View> views = new HashMap<>();
private final Map<String, Command> commands = new HashMap<>();
private final ModelContainer model = new ModelContainer(this);
public void addView(String key, View view) {
views.put(key, view);
}
public View getView(String key) {
return views.get(key);
}
public void showView(String key) {
getView(key).showForm();
}
public void addCommand(String commandString, Command cmd) {
commands.put(commandString, cmd);
}
public void executeControllerCommand(String commandString) {
commands.get(commandString).execute();
}
}
public abstract class View {
protected final JDialog dialog = new JDialog();
protected final GUIAction guiAction = new GUIAction();
protected final Controllerv2 controls;
Views are subclassed from this class:
public View(Controllerv2 suppliedController) {
this.controller = suppliedController;
}
// Fires up the form
protected abstract void initComponents();
// Package up the fields into an object of type T and return
public abstract <T> T readFields();
// Self explanatory functions
public void showForm() {dialog.setVisible(true);}
public void hideForm() {dialog.setVisible(false);}
// For fields that need updating
public abstract void updateFields(String[] msg);
}
This is an example of a concrete View:
public final class VBasexLoginR extends View {
private JButton connectButton;
private JLabel connectionLabel;
private JTextField hostField;
private JLabel hostLabel;
private JLabel passwordLabel;
private JButton pingButton;
private JTextField portField;
private JLabel portLabel;
private JPasswordField pwField;
private JLabel titleLabel;
private JTextField uidField;
private JLabel userId;
public VBasexLoginR(Controllerv2 c) {
super(c);
initComponents();
}
@Override
protected void initComponents() {// Boilerplate to setup the form}
@Override
public BaseXCredentials readFields() {
return new BaseXCredentials(hostField.getText(),portField.getText(),uidField.getText(),pwField.getPassword());
}
@Override
public void updateFields(String[] msg) {
connectionLabel.setText(msg[0]);
}
}
Am I going in the right direction with this?
Also, I think I may need different models, one for running queries on BaseX and then ones for working with the results. Thus far I have...
public class ModelContainer {
private final Map<String, Model> models = new HashMap<>();
private final Map<String, ModelCommand> commands = new HashMap<>();
private final Controllerv2 controller;
ModelContainer(Controllerv2 suppliedController) {
this.controller = suppliedController;
}
public void addModel(String key, Model model) {
models.put(key, model);
}
public Model getModel(String key) {
return models.get(key);
}
public void addCommand(String key, ModelCommand cmd) {
commands.put(key, cmd);
}
public void executeModelCommand(String key) {
commands.get(key).execute();
}
}
Views can be got at through the getView
method in controls
Is this correct or is this overkill?
EDIT -- Taking into account Mat's Mug response, changed the code to reflect the naming conventions suggested.