Hello everyone,
I've got another question concerning MVC with a drag and drop interface. Let me recapitulate what I have so far:
I've got an MVC triad managing a simple JFrame in which mouse clicks create coloured dots. In the mouseListener in the controller a mouse click is translated into creating a new triad of MVC objects that represent an individual dot. The model of the dot is stored in a vector, which is managed in the model of the application.
So, in essence, two levels of MVC.
My next problem is how to delete a dot model from the vector. In the dot-level controller I have a mouseListener. How do I reach the vector in the application-level model? Do I have to create a local variable to store a reference to the application model, or is it enough to store a reference to the vector.
And how do I do that? It's not enough to try something that kind of works; I want to do this the proper OO way.
Here is part of the code of the application-level model where the vector is created:
public class ColouredDotsAppModel { private Vector<DotModel> chain; // Constructor public ColouredDotsAppModel() { chain = new Vector<DotModel>(); ... } }
And here is the overridden mouseReleased method in the dot-level controller. The first part of the method is to change the status of the dot (and by that way its color) when it is left-clicked upon without being dragged. The deleting should be done in the second part, when clicked on with the right mouse button.
public void mouseReleased(MouseEvent mE) { // If left mouse button clicked and released... if (!beenDragged) { if ((mE.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) { if(dotModel.getStatus() == 1) dotModel.setStatus(2); else if(dotModel.getStatus() == 2) dotModel.setStatus(3); else if(dotModel.getStatus() == 3) dotModel.setStatus(1); } if ((mE.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) { // TODO: remove dot from vector and from UI } } beenDragged = false; }
If it is too confusing, I can upload my latest working project to an online storage. Just let me know.
Thanks in advance,
Alice