[ANU] [DCS] [COMP2100/2500] [Description] [Schedule] [Lectures] [Labs] [Homework] [Assignments] [COMP2500] [Assessment] [PSP] [Java] [Reading] [Help]
COMP2100/2500
Assignment 2 Hints and FAQHints
I recommend that you place each class you modify under version control with RCS (lecture and lab in Week 10), so that you can keep track of the changes you make and go back if you mess up something that was working. I have already put RCS tags into the header. Make sure that you check each file in and out after each significant lot of changes. If you use Emacs, this is built in: just type C-x v v to check in or out, and C-c C-c to tell it when you've finished typing your log message. Take the time to put meaningful log messages, so that you can choose the correct version more easily if you have to go back.
Actually, this is a bit tough with the pair assignment since the only way you can give your partner access is to give everyone access, which you don't want. This is something I need to look into fixing up for next year: we need to put something in place where partners can have their own repositories (using RCS, CVS, subversion or some similar version control tool) with access only to the partners and not to anyone else.
Frequently asked questions
You've commented out the line in the main class that creates a Controller object. Why?
Because the Controller doesn't do anything at the moment.
In fact you probably won't want a separate Controller at all. I've left it there, just in case, together with the command to create it, but I don't think you'll need them.
I have been reading a little more about how Java implements the Model-View-Controller architecture, and the way they describe it is as a “modified” M-V-C architecture, with the View and the Controller merged together. The way I'm now thinking about it, class View creates all the GUI components and puts them on the screen (as the View should in M-V-C). It also creates the various listeners and links them to the GUI components. The listeners collectively take on the Controller role of M-V-C. You could do this with a separate Controller class creating all the listeners, but I think it would be a bit contrived, and make linking them up with the GUI components a bit awkward. Maybe class View should now be named “View and Controller” or perhaps just “GUI”, but I think it's probably OK the way it is. M-V-C is useful as a mental framework for organising the functions of an application, but we don't necessarily have to have a separate class for each part.
When I modified assignment 2 to allow loading of new documents, I noticed that the panel was not repainting itself as it should. This is because there is a call to setChanged() missing prior to the call to notifyObservers(). From my reading of the API, it seems that the notifyObservers() method only notifies its observers if a call to hasChanged() returns true, and an observable object doesn't automatically know that it has been changed. In short, I found that adding “setChanged();” before “notifyObservers();” will cause the observer(s) to actually be notified of the change and repaint themselves.
Yes, that's right. I forgot about it when I coded up the starting version. If you look at the MVC versions of the clock program, you'll see that they do setChanged() before notifyObservers() too.
I'm unclear about exactly what you want us to do in Q1(a). The instructions say “Add a scrollbar so that users can see all of long documents.”
What I want is the same behaviour as most web browsers: the width of the underlying panel is the same as the width of the window; the height of the underlying panel is the maximum of the height of the window and the height that is needed to fit the entire document.
Actually, being really, really fussy, the width of the panel should be the maximum of the width of the window and the width of the longest word. Words should be wrapped to the window width, but if there is a word that won't fit, the panel should really extend across to take it, and a horizontal scroll bar should appear.
I don't mind if the vertical scroll bar is always there, or only there when the entire document doesn't fit in the window.
If you use a JTextPane this should work by itself when you put the JTextPane inside a JScrollPane inside the JFrame (which it didn't do with a JPanel).
What should we do if the user selects Cancel in the file chooser after having no command line arguments? This is a problem for my program as there is no open document and no valid filename getting passed back to the original open method. I thought of a couple of options:
Keep displaying the dialog box (even if they hit cancel) until they select a valid file.
Assume they don't want to use the program after all, and simply exit.
Are either of these OK?
I think your second option is the best. I don't like the idea of leaving the dialog box up until they select a valid file. What if they don't have any suitable documents, and started the application by mistake?
It would be really nice to be able to modify the toString() method of classes XmlContainerElement and XmlDataElement. Are we allowed to do this? Or would it be best to make a wrapper class for these two classes that has a modified toString() method? Thanks!
Check whether those toString() methods are used in Assignment 1. The rule is that the original command-line version of the program (invoked by running “java comp2100.oops.Converter”) should still work.
Actually a quick check of the Javadoc documentation shows that those toString() methods are just the ones inherited from Object; that is, they haven't been overridden. Neither are they called anywhere in the system. So you're free to write new toString() methods for the tree classes if you wish.
I'm encountering a funny situation in Question 2. Whenever I open a file from an open file dialog, it will only repaint the part of the window that was covered by the dialog box.
I think this is my fault. Here's what is happening:
When the file chooser closes, a call to repaint() the text panel is generated automatically, with the rectangle the dialog box was covering getting passed to it. That bit gets redrawn.
Whatever is listening to the file chooser gets hold of the file name and passes it to the model. The model opens the file, scans it, parses it, builds the tree etc, then calls notifyObservers(). (You can see this in the code for open() in class Model.) Unfortunately there is a bug in that code. There should be a call to setChanged() first. (See the MVC versions of the clock example.) As it is, notifyObservers() thinks nothing has changed, and never calls update() on its observers, hence the behaviour you're seeing.
If you're getting really weird results, then perhaps the second of those is happening before the first one...
In any case, the solution is simple: add a call to setChanged() immediately before the call to notifyObservers() at the end of open() in class Model.
Can you be more specific as to exactly what is (and isn't) considered a Heading? The four you seem to specify are: Title, Author's Name, Primary Heading, and Secondary Heading. Are there any more?
The four you listed are all elements of type text:p with those values for the style name attribute (or its ancestor style). There are also elements of type text:h in these documents. (There are examples in sample7.sxw and sample8.sxw.) For these you need to look at the attribute text:level. If the level is 1, treat it the same as a Primary Head. If it's 2 or greater, treat it the same as a Secondary Head.
You say that all headings must be preceded by a blank line of the appropriate size. Does this include the first thing on the page? (In sample1.sxw, the element that reads “Title”.)
Yes. It's nice to have a little white space above the title, and it also means you don't have to worry about special cases.
Indenting by “Ems” isn't really practical using the JTextPane/StyledDocument pair. Is it good enough to set the indent to something that's roughly an “Em”, as opposed to the actual hard-coded spacing value for an ‘M’ character?
Yes, no problem. As long as you do something reasonable so that the indent for the first line of paragraphs with style “Text Body” is roughly 2 ems, and the left and right margins for the abstract and blockquotes are moved in by roughly 4 ems, I'll be happy. (Although I still don't see why it isn't possible to do it exactly...)
Old frequently asked questions
These are some questions and answers from 2004 and earlier that might be relevant and/or helpful.
What does a tree widget look like?
Check out the screenshots in Lecture 20.
You said to do the tree widget using a new visitor, but I can't figure out how to make that work. What do you suggest?
Strange, isn't it? It looks at first as though it should be easy, but when you try to fit this operation into the VISITOR framework, it just doesn't seem to go. I think it might be easier to do this using an "Expression Version 3"-style implementation. Try adding a feature to XML_ELEMENT and its subclasses that returns a GTK_TREE_ITEM representing that element and anything below it. (A bit like the pretty_print feature.)
This question and answer are from last year, when students were using Eiffel and EXG/GTK, so my answer may not apply to this year's Java/Swing assignment. But on the other hand it might. Worth keeping in mind, anyway.
[ANU] [DCS] [COMP2100/2500] [Description] [Schedule] [Lectures] [Labs] [Homework] [Assignments] [COMP2500] [Assessment] [PSP] [Java] [Reading] [Help]
Copyright © 2005, Ian Barnes, The Australian National University
Version 2005.4, Wednesday, 18 May 2005, 14:55:10 +1000
Feedback & Queries to
comp2100@cs.anu.edu.au