Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #932

Re: MVC question regardin

From "Rogan Dawes" <rogan.dawes@THRWHITE.remove-dii-this>
Subject Re: MVC question regardin
Message-ID <eqvb9q$ilp$1@wblv-ip-nnrp-1.saix.net> (permalink)
Newsgroups comp.lang.java.gui
References <qhd54dt79c.fsf@ruckus.brouhaha.com>
Date 2011-04-27 15:30 +0000
Organization TDS.net

Show all headers | View raw


  To: comp.lang.java.gui
Eric Smith wrote:
> I'm writing a simulator for the trivial computer defined in Appendix C
> of _Computer Science: An Overview_ (9th Edition) by Brookshear.  The
> computer has 256 bytes of memory.  In my simulator, I have a Memory
> class that represents the main memory, and which supports a
> MemmoryEventListener interface for the GUI to track changes made by the
> simulation.
> 
> I have a MemoryPanel class which extends JPanel and provides display and
> editing of the memory.  It shows each memory location using a subclass
> of JFormattedTextField.  If the user edits a location, a
> DocumentListener will be notified, and will call the Memory object to
> change the memory contents.
> 
> The obvious problem is that without some care, a change to a memory
> location will trigger the MemoryEventListener, which will change the
> JFormattedTextField, which trigger the DocumentListener, which will
> change the memory location, which will trigger the MemoryEventListener,
> etc.
> 
> I can easily imagine several solutions to this problem, none of which
> seem very elegant, and some of which wouldn't be thread safe.
> 
> My question is whether there is any standard pattern used to solve this
> sort of problem.  If not, I'll just roll my own ad hoc solution.
> 
> I originally tried using a JTable for this, with my Memory class
> subclassing AbstractTableModel, but it was really obnoxious that JTable
> understands column headings and not row headings.  I ended up
> having to put row headings in actual cells in my AbstractTableModel, and
> various ugly hacks like that, which seemed to defeat the purpose of
> separating the data model from the view and controller.  I don't
> want my Memory class to have any idea how the GUI works.
> 
> Thanks!
> Eric

The obvious shortcut is to check if the value is different to the value 
to intend to set it to, and if not, do not change it, and therefore, do 
not publish a new change event.

To your JTable question/problem, there are again two possibilities:

1) Implement your own TableModel, which listens to the MemoryEvent. Use 
a formula to calculate which row and column has changed, depending on 
the memory position.

e.g.

int pos = . . . // the memory index that changed (0..255)
int row = pos / 16;
// Add 1 to allow you to specify the row header in column 0
int col = pos % 16 + 1;

And for the get method,

if(col == 0) {
   return rownumber;
} else {
   int position = row * 16 + (col -1);
   return Memory.get(position);
}

For some actual code implementing this, see:

<http://dawes.za.net/gitweb.cgi?p=dawes.za.net/rogan/webscarab/webscarab-ng.git;a=blob;f=src/main/java/org/owasp/webscarab/ui/forms/HexForm.java;h=fea44df53cd5de592e95b5380e5a098513a45d76;hb=HEAD>

Scroll down to the HexTableModel.

2) The second alternative is to implement a proper table header. For 
example code, see: <http://www.chka.de/swing/table/row-headers/>

You'd probably still want to implement a custom TableModel, but you 
wouldn't use 1 column for the row header.

Hope this helps.

Rogan

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

MVC question regarding us "Eric Smith" <eric.smith@THRWHITE.remove-dii-this> - 2011-04-27 15:30 +0000
  Re: MVC question regardin "Rogan Dawes" <rogan.dawes@THRWHITE.remove-dii-this> - 2011-04-27 15:30 +0000
    Re: MVC question regardin "Eric Smith" <eric.smith@THRWHITE.remove-dii-this> - 2011-04-27 15:30 +0000

csiph-web