Debian Codegraph @

src/footleg/cavesurvey/data/model/CaveSurvey.java

raw symbols SCIP
1 /**
2 * Copyright (C) 2009-2017 Paul Fretwell - aka 'Footleg' ([email protected])
3 *
4 * This file is part of Cave Converter.
5 *
6 * Cave Converter is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Cave Converter is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Cave Converter. If not, see <http://www.gnu.org/licenses/>.
18 */
19 package footleg.cavesurvey.data.model;
20
21 import java.util.ArrayList;
22 import java.util.EventListener;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.ListIterator;
26
27 import javax.swing.event.EventListenerList;
28 import javax.swing.event.TreeModelEvent;
29 import javax.swing.event.TreeModelListener;
30 import javax.swing.tree.TreeModel;
31 import javax.swing.tree.TreePath;
32
33 import footleg.cavesurvey.converter.Logger;
34
35 /**
36 * Class representing a complete cave survey data model. This can consist of
37 * one or more cave surveys.
38 *
39 * @author Footleg
40 * @version 2017.01.09 (ISO 8601 YYYY.MM.DD)
41 * @since 1.6 (The Java version used)
42 */
43 public class CaveSurvey implements TreeModel {
44 private String surveyName;
45 private List<SurveySeries> survey;
46 private EventListenerList listenerList = new EventListenerList();
47 private Logger logger;
48
49 /**
50 * Class constructor
51 * @param logger Logging class to output information, warning and error messages to
52 */
53 public CaveSurvey( Logger logger ) {
54 super();
55 this.logger = logger;
56 //Create new list of survey series to hold data
57 survey = new ArrayList<SurveySeries>();
58 }
59
60 public int size() {
61 return survey.size();
62 }
63
64 public String getSurveyName() {
65 return surveyName;
66 }
67
68 public void setSurveyName(String surveyName) {
69 this.surveyName = surveyName;
70 }
71
72 public boolean isEmpty() {
73 return survey.isEmpty();
74 }
75
76 public boolean contains(Object o) {
77 return survey.contains(o);
78 }
79
80 public Iterator<SurveySeries> iterator() {
81 return survey.listIterator();
82 }
83
84 public boolean add(SurveySeries e) {
85 boolean result = survey.add(e);
86 fireTreeStructureChanged( this );
87 return result;
88 }
89
90 public boolean remove(SurveySeries o) {
91 return survey.remove(o);
92 }
93
94 public void clear() {
95 survey.clear();
96 }
97
98 public SurveySeries get(int index) {
99 return survey.get(index);
100 }
101
102 public SurveySeries set(int index, SurveySeries element) {
103 return survey.set(index, element);
104 }
105
106 public void add(int index, SurveySeries element) {
107 survey.add(index, element);
108 }
109
110 public SurveySeries remove(int index) {
111 return survey.remove(index);
112 }
113
114 public ListIterator<SurveySeries> listIterator() {
115 return survey.listIterator();
116 }
117
118 public ListIterator<SurveySeries> listIterator(int index) {
119 return survey.listIterator(index);
120 }
121
122 /**
123 * Generates LRUD data from splays for all legs in all series
124 */
125 public void generateLRUDfromSplays() {
126 //Loop through all series
127 ListIterator<SurveySeries> seriesIterator = survey.listIterator();
128 while ( seriesIterator.hasNext() ) {
129 SurveySeries series = seriesIterator.next();
130
131 //Process series and then recursively call inner series
132 processLRUDfromSplays( series );
133 }
134 }
135
136 /**
137 * Process the legs in this series to generate LRUD data from splays,
138 * then loop through all inner series recursively to process them too.
139 * @param series The survey series to process
140 */
141 private void processLRUDfromSplays( SurveySeries series ) {
142 //Process series and then recursively call inner series
143 series.generateLRUDFromSplays( logger );
144
145 //TODO Fix the way the option to remove splays used for LRUD is triggered, and don't do it by default.
146 //series.removeSplaysUsedForLRUD();
147
148 //Loop through all inner series
149 ListIterator<SurveySeries> seriesIterator = series.getInnerSeriesList().listIterator();
150 while ( seriesIterator.hasNext() ) {
151 SurveySeries innerSeries = seriesIterator.next();
152 processLRUDfromSplays( innerSeries );
153 }
154 }
155
156 /*
157 * TreeModel interface methods
158 */
159
160 /*
161 * (non-Javadoc)
162 * @see javax.swing.tree.TreeModel#getRoot()
163 */
164 @Override
165 public Object getRoot() {
166 //Root is the cave survey class itself
167 return this;
168 }
169
170 @Override
171 public Object getChild(Object parent, int index) {
172 //Return inner series at the index specified of the parent series passed in
173 if ( parent instanceof CaveSurvey ) {
174 return survey.get(index);
175 }
176 else if ( parent instanceof SurveySeries ) {
177 SurveySeries series = (SurveySeries)parent;
178 //Determine whether this index points to an inner series or a leg
179 if ( index < series.innerSeriesCount() ) {
180 //Return an inner series
181 return series.getInnerSeries(index);
182 }
183 else {
184 //Return a survey leg
185 int legIdx = index - series.innerSeriesCount();
186 return series.getLegRaw( legIdx );
187 }
188 }
189 else {
190 return null;
191 }
192 }
193
194 @Override
195 public int getChildCount(Object parent) {
196 //Return count of inner series plus surveyed legs for the parent series passed in
197 if ( parent instanceof CaveSurvey ) {
198 return size();
199 }
200 else if ( parent instanceof SurveySeries ) {
201 SurveySeries series = (SurveySeries)parent;
202 return series.getInnerSeriesList().size() + series.legCount();
203 }
204 else {
205 return 0;
206 }
207 }
208
209 @Override
210 public boolean isLeaf(Object node) {
211 //Return true if node is a survey leg
212 boolean leaf = false;
213
214 if ( node instanceof SurveyLeg ) {
215 leaf = true;
216 }
217 return leaf;
218 }
219
220 @Override
221 public void valueForPathChanged(TreePath path, Object newValue) {
222 // TODO Auto-generated method stub
223 Object[] p = path.getPath();
224 Object[] pp = p;
225 Object node;
226 int index;
227
228 if ( p.length == 1 ) {
229 //Editing root node
230 setSurveyName( (String)newValue );
231 node = this;
232 index = -1;
233 }
234 else {
235 //Editing a Survey series or leg inside a series
236 node = p[p.length - 1];
237 SurveySeries parent = (SurveySeries)p[p.length - 2];
238 index = parent.getIndexOfChild( node );
239 //EDIT HERE
240 if ( node instanceof SurveySeries ) {
241 ((SurveySeries) node).setSeriesName( (String)newValue );
242 }
243 }
244
245 // int[] ci = new int[] { index };
246 // Object[] cc = new Object[] { node };
247 // fireTreeNodesChanged();
248 }
249
250 @Override
251 public int getIndexOfChild(Object parent, Object child) {
252 //Look up index of the specified child class instance in the specified parent instance
253 if ( parent instanceof SurveySeries ) {
254 return ((SurveySeries)parent).getIndexOfChild( child );
255 }
256 else {
257 return -1;
258 }
259 }
260
261 @Override
262 public void addTreeModelListener(TreeModelListener l) {
263 listenerList.add(TreeModelListener.class, l);
264 }
265
266 @Override
267 public void removeTreeModelListener(TreeModelListener l) {
268 listenerList.remove(TreeModelListener.class, l);
269 }
270
271 protected void fireTreeStructureChanged(Object oldRoot) {
272 TreeModelEvent event = new TreeModelEvent(this, new Object[] { oldRoot });
273 EventListener[] listeners = listenerList.getListeners(TreeModelListener.class);
274 for (int i = 0; i < listeners.length; i++)
275 ((TreeModelListener) listeners[i]).treeStructureChanged(event);
276 }
277
278 /**
279 * Provides a string representation of a cave survey to display in a tree view of the data model
280 * @return String representation of Cave Survey class
281 */
282 public String toString() {
283 String text = getSurveyName();
284 if ( ( text == null ) || ( text.equals("") ) ) {
285 text = "Cave Survey";
286 }
287 return text;
288 }
289 }