Olaf Enulfson

+ Follow
since Jun 18, 2009
Merit badge: grant badges
For More
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Olaf Enulfson

I read Potel's white paper too, but I think it's a commercial for an IBM product. Although his thoughts on MVP are interesting, it's too far ahead of my current problems.

Now, I've found this article on JavaWorld that at first look seems to agree with Jimmy's point of view of a single Model.

I think the difference lies in semantics: whether the Model part of an MVC triad (as pete named them) is a totally independent Model, or is a part of a greater whole that deals exclusively (or not) with its own View and Controller, depends really how you look at the beast.

In the case of my project, I want the Model of each shape to have its own View (a colored circle in ColoredDots) and its own Controller (a mouse controller actually), but be part of a greater whole of objects representing the structure(s) the shape is part of with some of the other shapes, and the rules that allow or forbid relative certain positions of the shapes.

So, if I want to use the Observer/Observable classes Todd Sundsted talks about in his JavaWorld article, I'll need the View to have a reference link to the Model. So I guess this would become my basic code:
Any thoughts on this, please?

Now I have a more practical question: how do I hook up a mouseListener or a mouseAdapter to the Controller instead of to the lPane in the View in my ColoredDots source? I think I can figure it out using events in case of standard controls (a button or a textfield) but what if my user input is the location of a dragging mouse pointer? How do I manage the mouse input in the Controller if Swing has it associated with the visual part of the interface?
I read Martin Fowler's GUI Architectures. Thanks pete. I understand better now what triads I need.

A triad of MVC will manage the application, where the View is the JFrame (size, background, ...). When a user clicks on an empty space of the screen, the Controller will be notified and instantiate a triad of objects from the shape triad, whom's View will only manage the location and color of the shape.

Now when an existing shape is clicked upon (which is supposed to cycle its color in ColoredDots), does the main Controller (the one that manages the application) intervene? Does it send a message to the appropriate shape controller? Or does that controller get its own mouse handler?

I've also read further about the MVP architecture, and I think my project will have the same problem as the variance display flaw, since the shapes change color based of the status of the shape model, including other shapes. I'm reading Potel's white paper on MVP at the moment.

So, pete, how many triad do you think I need for the program described in my previous post? And how do I need to adapt the code in post #1. And Jimmy, according to your take, how does my code look to you?

I've got some more advanced questions in the pipeline, but first I need to know where I stand.

Jimmy Clark wrote:A View is all of your HTML pages or it is the entire desktop application.

pete stein wrote:My take on this is yeah, each MVC triad will likely have one model, but your complex GUI application is going to have multiple such triads each interacting with the other.

The GUI of my application shouldn't be that complex: a static background and simple shapes that can be created, moved about and deleted using point,click&drag, plus a convenient way to scroll around. Positioning and repositioning the shapes around should trigger rule checks that color the shapes to indicate different error levels as to why a position is not good. (With perhaps a log that gives extra information which rules apply, but that is less important). On top of that, and here's the kicker, the possibility to add a cluster of shapes either through combo boxes or a context menu, filled with a possibly great number of precalculated formations of shapes. The latter is probably the most difficult of the logic.

The difficulty is really the rules that will determine which combinations and formations of clusters of shapes are allowed.

Jimmy Clark wrote:In terms of your JFrame, the entire screen is your View implementation. You could have a thousand little things on the screen, this is all part of your implementaion of a View.
Your Controller is responsible to accepting data from the Model and making it available for your View. Code in your View will display it in any way you code it.

So, the piece of code in post #1 could pass as a base class? Or can some things be improved?

Jimmy Clark wrote:And when you are coding, you shouldn't be trying to map or connect things too much to design terminology. There oftenn is no logical direct match between the too.

I'll try to remember that. To be honest, I don't give a rodent's rectum what it's called as long as I get it to do what I need. :-)

pete stein wrote:YMMV.

Well, my vehicule lacks some road testing altogether. ;-)

Jimmy Clark wrote:The role of the Controller is to serve as a delegate between the Model and either one View or multiple View(s). Whether there is one Controller or not depends on the implementation, i.e. if there are multiple Views for example.

Ok, important sub-question then, if I have one JFrame in which the same data is represented in two parts of the screen, one that allows manipulation of the data and one that basically serves as a graphical scroll bar, do I then have one or two Views? And if I have two, I suppose I need two Controllers?

Business applications such as batch data processing application which do not invlove human interaction are not candidates for the MVC pattern.

Yes, of course. That makes sense.

The purpose of the View is to display data and/or input controls for the human user. There typically is a ton of stuff that is happening behind the scenes which the human user never sees, e.g. data manipulation, calculations, processing, connectivity, service invocation, etc.

I don't know if you've looked at my program (ColoredDots). Does that already count as MVC (even though I don't have a controller)? And if not, is it good to model after the MVC calculator example on this page?

And a question for Pete Stein: I see you guys disagree on some points. Are these of major importance concerning my ColoredDots test case program? (Should I be worried?)

Thanks to both of you so far.

Jimmy Clark wrote:One of the more important aspects of understanding the MVC pattern is that the terms "Model", "Controller", and "View" do not translate into a single class for each area. For example, the Struts Controller consists of many, many objects that all interact with each other to "realize" the Controller responsibilities. The View aspect also consists of many parts.

Ok, so I guess it highly depends on the complexity of the real-world problem at hand how many classes each of these aspects are made of? Is there some guide that can help me determine what component-classes I need to address my real-world problem?

Jimmy Clark wrote:In terms of the Model aspect, this represents the business (domain) logic of the application and typically is many, many, objects all working together to handle their respective parts of the business (domain) functionality.

You will only have a single Model layer. If you have coded functionality in multiple classes, then you should also provide the access point to allow the Controller to effectively communicate with the Model (everything in the Model.) Hope this helps.

So, as if the Model layer is really the logical interface between the business logic objects, and the controller for each view? (If there is more than one view).

pete stein wrote:

Jimmy Clark wrote:Business applications that do not have a related GUI are not candidates for the MVC pattern.

I'm not sure about this either as there are ways to view information that do not involve GUI.

I don't totally understand this, but I think it's a very important point for my project. I also have information that will be part of the model, that will not translate directly in a visible screen element, but that nonetheless will allow or impeach manipulation of the main graphical elements.

You see, my intent is to program some kind of theoretic editor that allows the user to manipulate colored shapes, but with a large amount of rules where these shapes can be positioned relative to each other. As I learn more about the real-world behavior, I will need to add rules to the editor/simulator.

You could compare it a bit to a game of Go, where placing and removing certain pieces triggers a number of requirements (not everything is allowed and some placements are better than others). But the program will also need to offer several solutions where pieces may be placed.

My project is not Go. (It's not even a game. It is more complex than Go, not concerning winning strategies, but concerning allowed placements). But it has comparable interface analogies. So I was hoping to build a framework with the graphical interface that would later allow me to add the business logic rules.
Thanks for your help, Jelle and Stephan.

I haven't replied in a while, I'm sorry about that. I haven't I stopped working on my project though. I've got a few follow-up questions in another thread. If you have the time, I'd appreciate your input.

Here is the thread:

Thanks again.

13 years ago
Hello everyone,

I'm still figuring out MVC. In a previous thread (https://coderanch.com/t/512468/Testing/MVC-test-case) I asked some questions about how to rewrite a little program to fit the MVC pattern. But I'm stuck again. Could you please help me some more?

So, here is the little program I wrote in Eclipse that puts colored dots on a small JFrame, lets you move them around and change their colors when clicked upon.
http://www.4 shared.com/file/gzvFJsmJ/ColoredDots.html

I'd like to change it in the way this example program is build:

So I rewrote the main class like this:
My first question is, what if I have more than one model? In my previous program I had a model for the "dot" and a model for the group of dots together. I could also have a complete different type of object that has different functionality and that I want to keep in its own handy model class.

So, how would I go about fusing multiple models? Would I need a controller for each model?

Thank you in advance.

Jelle Klap wrote:I've briefly looked over the code.

Thanks Jelle.

(...) you've clearly separated the view from the model, yet the controller logic remains integrated into the view, and I wonder why that is?

When I read up on MVC tutorials, I understood the Controller to be a link between the Model (the data and the functionality) and the View (the user interface, including input management from the user).

When I later 'discovered' the Observer-Observable framework, I used this as a replacement for the controller-link. (I even found a white paper on MVC demonstrating that, and based my program on it). I understand now that this is incomplete. The controller is really the mouse listeners and event listeners of the user interface, in addition to the message gateway. Right?

I'd start by extracting the controller logic out of the view into a controller, which would interact with both model and view.

So in the Controller I would have to register two observers ( .addObserver(this); ), one to the Model and one to the View? That second one I don't understand. Which changes of the View are to be observed?

And where and how would I connect the mouse listener to the pane (as is currently done in DotsGUI)?

Also, right now you're updating the DotModel for every single dragged event, which triggers a notify/update cycle that repaints the component. I think it'd be more efficient to short-circuit between the controller and view and update the model only when you have a "definitive" fix on the new location, based on the released event.

Ok, but wouldn't the drawn position of the dot be updated only at release? How does the user know where his dot is going to be at release? He needs to see the dot move.

That way it should even be possible to create an unobservable and immutable Dot and a DotModel that keeps track of them - dropping DotChain entirely.

In my big program, I'll need a searchable link between each dot. The relative postition of the dots will have their importance, and will be checked and re-checked at every new dot and every moved dot. So I kind of need DotChain to maintain an easy relationship. If there is a better way to do that, I'm open to all suggestions. At some point I had specific pointers to neighbor dots, but that was overly complicated.

As for removal of dots, that shouldn't be too hard or different from adding new dots (regardless of which model approach you take). Simply manipulate the model from inside the controller when it receives the MouseEvent that should trigger a removal operation (right click). The removal operation on the model should itself trigger a notify/update cycle, which will in trigger the view to (re)paint itself based on the current state of the model. It should take care of itself.

I don't understand this completely. Would there be a Controller for each dot? Or a single Controller managing the entire input interface for the screen and all the dots at once?

... could we take it a little slower? Maybe tackle one of these problems at a time. Your choice is fine to me.
13 years ago

Maneesh Godbole wrote:We have a forum dedicated for testing. Moving thread.

Well, ok. But it's not really a testing issue. Test case means it's a program to try out some techniques. But if it's read and replied to here, I've got no objection. Thanks Maneesh.

Stephan van Hulst wrote:Maybe you can post your code in an attachment. I would be willing to look at it.

Good idea. Thanks for your time, Stephan.

Attachments don't seem to work, so I signed up for this free data storage account and posted my project there:
http://www.4 shared.com/file/gzvFJsmJ/ColoredDots.html

So, this program is a screen on which dots appear when clicked on. Clicking a second time on a dot, changes its color. You can also drag the dots around. Very basic stuff.

The first issue I have is how to remove a dot by right clicking on it. I understand that a second BUTTON_MASK needs to be checked in method mouseReleased in class DotsGUI. But how does class DotChain receive the message to delete that object?

The other long-term issue is how this program can become more MVC. Actually, there is not even a real controller. I don't expect anyone to rewrite it completely. But maybe you can give me some ideas that I can implement and send back for review. Maybe there are some obvious mistakes I made.

Anyway, thanks a lot in advance.

13 years ago
Dear coders,

I've written a small program in preparation to write a large program. The idea was to develop a test case in MVC first. Unfortunately the program I wrote is not very MVC yet. I would like some tips from you guys to make it better before I start working on the big program. Would it be ok if I posted the program here (5 classes, 40 to 100 lines each) so that you guys can comment on the mistakes?

The program runs. I wrote it using Eclipse. It has a simpel graphical Swing UI and I use the Observable class for messaging. I have one problem I cannot solve and several style problems that need to be cleaned up.

If I should post it in another place, please let me know.

Thanks in advance. I'll be back tomorrow.
(And I would understand if 250 lines of code are too much for a post)
13 years ago
Pete, you are incredible.

I came up with your first suggestion too. I feel kind of bad for posting the question so soon. I should have given it more thought first.
But your second post blew me away. You're in a league of your own.

Thanks man. I appreciate it.

14 years ago
Hello coders,

I'm experimenting with mouseListeners and I have an interesting problem (I think). I've searched the forum for similar posts but found none. Sorry if I missed any.

In my program I'm trying to move parts of shapes by dragging one of its points. I've already implemented the mousePressed() and mouseDragged() methods, and I can move single points. But I need to find out how many pixels the mouse pointer moved in X and Y and in which direction, so that I can apply the same vector to the other points.

In the mouseWheelEvent there is a method called getWheelRotation(), which returns the direction of the wheel. I need something alike for X and Y, instead of the actual new position getX() and getY() are returning.

Any thoughts? Thanks a bunch in advance,

Sex is an emotion in motion.
-- Mae West
14 years ago
Yeeha! It works!

Thanks Henry.
(Thanks to everyone else too).

In order to understand recursion, one must first understand recursion.
14 years ago

you have to give access to the inner class (now it's own class).

Yes, and how do I do that?

It is probably easiest to pass the instance of the outer class to the instance of the formerly inner class -- and probably do it during construction. Then the formerly inner class object can access what it use to access directly with the reference that is passed.

Yes yes, that sounds great. What syntax would I need for that?

Would it be using keyword "this"?

Murphy was a pessimist.
14 years ago