| GridPointsSet.java |
1 /*
2 *
3 * Copyright (C) 2007,2008 Markko Merzin (markko.merzin@ut.ee)
4 *
5 * This file is part of ImageJ plugin Volumest.
6 *
7 * Volumest plugin is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 *****************************************************************************
21 */
22package ee.ut.mrz.volumest;
23
24import java.util.ArrayList;
25import java.util.Iterator;
26import java.util.ArrayList;
27
28/**
29 * Model for set of points on one grid. Implements undo and redo functionality.
30 * @author Markko Merzin, markko.merzin@ut.ee
31 *
32 */
33public class GridPointsSet extends ArrayList {
34
35 private int activeSet = -1;
36
37 /** Creates a new instance of GridPointsSet */
38 public GridPointsSet() {
39 super();
40 }
41
42 /**
43 * Adds object to end of undo-chain.
44 * @param o Objet to be added.
45 * @return See: java.util.ArrayList.add()
46 * @see java.util.ArrayList#add
47 *
48 */
49 @Override
50 @SuppressWarnings("unchecked")
51 public boolean add(Object o) {
52 // remove entries after current pointer
53 while (this.size() > activeSet + 1) {
54 this.remove(this.size() - 1);
55 }
56 // call is perfectly safe.
57 boolean ret = super.add(o);
58 activeSet++;
59 return ret;
60 }
61
62 /**
63 * Returns ArrayList with currently active set of points.
64 * @return ArrayList with active points.
65 *
66 */
67 @SuppressWarnings("unchecked")
68 public ArrayList<GridPoint> getCurrentObj() {
69 // cast is perfectly safe.
70 return (ArrayList<GridPoint>) this.get(this.activeSet);
71 }
72
73 /**
74 * Steps back in undo-chain.
75 * @return true if there is something to do.
76 */
77 public boolean undoStep() {
78 if (this.activeSet > 0) {
79 this.activeSet--;
80 return true;
81 } else {
82 return false;
83 }
84 }
85
86 /**
87 * Steps forward on undo-chain.
88 * @return true if there is something to do.
89 */
90 public boolean redoStep() {
91 if (this.activeSet < this.size() - 1) {
92 this.activeSet++;
93 return true;
94 } else {
95 return false;
96 }
97 }
98
99 /**
00 * Counts length of undo-chain.
01 * @return length of undo-chain.
02 */
03 public int getPossibleUndoStepsCount() {
04 return this.activeSet;
05 }
06
07 /**
08 * Counts possible redo-steps.
09 * @return Number of possible redo-steps.
10 */
11 public int getPossibleRedoStepsCount() {
12 return this.size() - this.activeSet - 1;
13 }
14
15 /** Copies last entry and adds it to the end of set and makes active for changes. */
16 public void copyLastSet() {
17 ArrayList<GridPoint> prev = new ArrayList<GridPoint>();
18 // Iterator iter = this.gridPoints.iterator();
19 Iterator iter = this.getCurrentObj().iterator();
20 while (iter.hasNext()) {
21 GridPoint gp = new GridPoint((GridPoint) iter.next());
22 prev.add(gp);
23 }
24
25 // add copy to undobuffer
26 this.add(prev);
27
28 }
29}
30