2

I've an ArrayList of Line Objects called 'lines'. I made my own line class to draw lines with some constraints. It involves selecting two points in a panel and a line is drawn connecting the two points. Everytime a line is created, it is added to the 'lines'. The lines are drawn in a panel.

The paint function in my panel looks like this:

   public void paintComponent(Graphics g){      

       super.paintComponent(g);

       for(final Line r:lines){

            r.paint((Graphics2D)g);

       }
    }

And everytime two points are clicked on the panel, a new line is created.

class Board extends JPanel{

 public void placeLine(){
  Point p1,p2;
  JLabel l1,l2;
  ...
  lines.add(new Line(p1,p2,l1,l2));
  this.repaint();
 }
 public void deleteLine(Line l){
  lines.remove(l);
 }
}

I want to create an UndoAbleEdit in this, and everytime i give undo, the undo method must revert to the last action(i.e.creating a line or deleting a line). I've tried undo for events in JTextArea but i couldn't figure out how to build a custom undo for event changes in ArrayLists. Suggest an example for doing this.

And i'm really sorry for not posting it as an SSCCE.. It is a huge project and it is almost impossible to create an SSCCE.

aleroot
  • 71,077
  • 30
  • 176
  • 213
Siva Subramaniam
  • 124
  • 1
  • 3
  • 12

2 Answers2

0

I suggest you read about the Memento Pattern (http://en.wikipedia.org/wiki/Memento_pattern), then search the web for some code samples that use this pattern.

Oded Peer
  • 2,377
  • 18
  • 25
  • Yup.. Thanks!! But I've no idea about design patterns though.. Is there any way we can do this without using design patterns? – Siva Subramaniam Apr 09 '12 at 08:27
  • @SivaSubramaniam design patterns are basics to know. If you have never used them you should start to take a look at them. (I'm sure you've used some of them but you didn't know if was is a design pattern.) – dexametason Apr 09 '12 at 08:29
  • @SivaSubramaniam design patterns are a concept, an idea. Not an actual package to download and run. Think of it as best-practices for software development, as solutions to common software development problems. You need to read about the pattern and think about how to apply it to your use case. – Oded Peer Apr 09 '12 at 08:46
0

I would create and store Runnable objects for making undo changes in some stack structure, popping and running them as needed. For your example:

class Board extends JPanel {
    ArrayList lines = new ArrayList();
    Stack<Runnable> undo = new Stack<Runnable>();

    public void placeLine() {
        Point p1, p2;
        JLabel l1, l2;


        final Line line = new Line(p1, p2, l1, l2);
        lines.add(line);
        undo.push(new Runnable() {
            @Override
            public void run() {
                lines.remove(line);
                this.repaint();
            }
        });

        this.repaint();
    }

    public void deleteLine(final Line l) {
        lines.remove(l);
        undo.push(new Runnable() {
            @Override
            public void run() {
                lines.add(l);
            }
        });
    }


    public void undo() {
        undo.pop().run();
    }
}
yggdraa
  • 2,002
  • 20
  • 23