Debian Codegraph @

src/footleg/cavesurvey/gui/swing/CaveModelVisualiser.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.gui.swing;
20
21 import java.awt.Dimension;
22 import java.awt.Font;
23 import java.util.Calendar;
24 import java.util.Date;
25 import java.util.List;
26 import java.util.TimeZone;
27
28 import javax.swing.JScrollPane;
29 import javax.swing.JSplitPane;
30 import javax.swing.JTextArea;
31 import javax.swing.JTree;
32 import javax.swing.ScrollPaneConstants;
33 import javax.swing.event.TreeSelectionEvent;
34 import javax.swing.event.TreeSelectionListener;
35
36 import footleg.cavesurvey.converter.CaveConverter;
37 import footleg.cavesurvey.converter.CaveConverter.BearingUnit;
38 import footleg.cavesurvey.converter.CaveConverter.GradientUnit;
39 import footleg.cavesurvey.converter.CaveConverter.LengthUnit;
40 import footleg.cavesurvey.converter.CaveConverter.SurveyDataOutputFormats;
41 import footleg.cavesurvey.converter.Logger;
42 import footleg.cavesurvey.data.model.CaveSurvey;
43 import footleg.cavesurvey.data.model.SurveyLeg;
44 import footleg.cavesurvey.data.model.SurveySeries;
45 import footleg.cavesurvey.data.writer.SurvexWriter;
46 import footleg.cavesurvey.data.writer.TopoRobotWriter;
47 import footleg.cavesurvey.tools.SurveyProcessing;
48
49 /**
50 * Component to display the cave data model
51 *
52 * @author Footleg
53 * @version 2017.01.09 (ISO 8601 YYYY.MM.DD)
54 * @since 1.7 (The Java version used)
55 *
56 * @to.do
57 * TODO Add proper visualisation of data model
58 */
59 @SuppressWarnings("serial")
60 public class CaveModelVisualiser extends JSplitPane {
61
62 public class SelectionListener implements TreeSelectionListener {
63
64 public void valueChanged(TreeSelectionEvent se) {
65 JTree jTree = (JTree) se.getSource();
66 Object selectedNode = (Object) jTree.getLastSelectedPathComponent();
67 boolean enableSeriesProcessing = false;
68
69 if (selectedNode instanceof CaveSurvey) {
70 enableSeriesProcessing = false;
71 CaveSurvey survey = (CaveSurvey) selectedNode;
72 taCaveLog.append( ReportCaveSurveyStats( survey ) );
73 }
74 else if (selectedNode instanceof SurveySeries ) {
75 enableSeriesProcessing = true;
76 SurveySeries selectedSeries = (SurveySeries) selectedNode;
77 taCaveLog.setText( ReportSeriesStats(selectedSeries) );
78 }
79 else if (selectedNode instanceof SurveyLeg) {
80 SurveyLeg selectedLeg = (SurveyLeg) selectedNode;
81 taCaveLog.setText( ReportLegStats(selectedLeg) );
82 }
83 //Enable or disable survey series actions
84 parent.setSurveySeriesActionsEnabled( enableSeriesProcessing );
85 }
86 }
87
88 private MainForm parent;
89 private JTextArea taCaveLog;
90 private CaveSurvey caveModel;
91 private JTree tree;
92 private Logger logger;
93
94 /**
95 * Create TextArea scrollable tab instance and display model summary. This is a very basic start for a
96 * full visualiser for the cave data model.
97 * @param parentForm Object reference to the parent form owning this item (so actions can be accessed)
98 * @param model The cave survey model to build a visualisation of
99 * @param font The font to use in the text area pane
100 */
101 public CaveModelVisualiser(MainForm parentForm, CaveSurvey model, Font font) {
102 super();
103 caveModel = model;
104 parent = parentForm;
105 logger = parentForm.getLogger();
106
107 //Create scrollable text area for right pane
108 taCaveLog = new JTextArea();
109 taCaveLog.setFont( font );
110 JScrollPane spCaveLog = new JScrollPane (taCaveLog, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
111 spCaveLog.setViewportView(taCaveLog);
112
113 //Create tree for left pane, using the CaveSurvey class as the TreeModel
114 tree = new JTree( caveModel );
115 tree.addTreeSelectionListener( new SelectionListener() );
116 JScrollPane spSurveyTree = new JScrollPane (tree, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
117
118 //Configure split pane
119 spCaveLog.setPreferredSize(new Dimension(250, 400));
120 spSurveyTree.setPreferredSize(new Dimension(250, 400));
121 this.setTopComponent( spSurveyTree );
122 this.setBottomComponent( spCaveLog );
123
124 renderCaveModel();
125 }
126
127 public CaveSurvey getCaveModel() {
128 return caveModel;
129 }
130
131 /**
132 * To pass a new cave model to an existing visualiser tab
133 * @param caveModel The cave survey model to build a visualisation of
134 */
135 public void setCaveModel(CaveSurvey caveModel) {
136 this.caveModel = caveModel;
137
138 //Pass the new model to the jTree
139 tree.setModel( getCaveModel() );
140
141 renderCaveModel();
142 }
143
144 /**
145 * @return the split pane
146 */
147 public JSplitPane getTabPaneComponent() {
148 return this;
149 }
150
151 /**
152 * Generates a visualisation of the cave data (currently a bare bones text summary, but intended to be developed).
153 */
154 private void renderCaveModel() {
155 taCaveLog.setText("This is a bare bones implementation of the visualisation of the cave model." + CaveConverter.newline +
156 "Currently it just reports some stats to let you know your data was loaded successfully." + CaveConverter.newline +
157 "This visualisation will improve with later releases to enable you to view your cave model" + CaveConverter.newline +
158 "more effectively." + CaveConverter.newline +
159 "-----" + CaveConverter.newline + CaveConverter.newline );
160
161 taCaveLog.append( ReportCaveSurveyStats( caveModel ) );
162 }
163
164 /**
165 * Generates LRUD data for stations from splay legs in each series
166 */
167 public void generateLRUDFromSplays() {
168 //Generate LRUD if model contains data
169 if ( ( caveModel != null ) && ( caveModel.isEmpty() == false ) ) {
170 caveModel.generateLRUDfromSplays();
171 taCaveLog.append( "LRUD data generated from splays in cave model." + CaveConverter.newline);
172 }
173 else {
174 taCaveLog.append( "No data in cave model, so no LRUD generation is possible." + CaveConverter.newline);
175 }
176 }
177
178 /**
179 * Generates new survey series from the names of the stations in the selected series
180 */
181 public void generateSeriesFromFullPathStationNames() {
182 //Generate LRUD if model contains data
183 if ( ( caveModel != null ) && ( caveModel.isEmpty() == false ) ) {
184 //Get selected series
185 SurveySeries selectedSeries = getSelectedSeries();
186
187 //Process series
188 if ( SurveyProcessing.generateSeriesFromFullPathStationNames(selectedSeries) ) {;
189 //Reload model
190 CaveSurvey model = caveModel;
191 setCaveModel( model );
192 }
193 else {
194 taCaveLog.append( "No changes were made to the series." + CaveConverter.newline);
195 }
196 }
197 else {
198 taCaveLog.append( "No data in cave model, so no processing is possible." + CaveConverter.newline);
199 }
200 }
201
202 /**
203 * Returns data file content for a specified survey format for the current cave model
204 * @param outputFormat The format to export
205 * @return Data file content for the model in the specified format
206 */
207 public List<String> exportModel( CaveConverter.SurveyDataOutputFormats outputFormat ) {
208 List<String> outputData = null;
209
210 if ( outputFormat == SurveyDataOutputFormats.Survex ) {
211 //Set options flag for splays (default to true)
212 SurvexWriter.SplayFormats outputSplays = SurvexWriter.SplayFormats.Flagged;
213 //Generate Survex format data
214 SurvexWriter writer = new SurvexWriter(logger);
215 outputData = writer.generateSurvexData( caveModel, outputSplays );
216 }
217 else if ( outputFormat == SurveyDataOutputFormats.Toporobot ) {
218 //Set options flag for splays (default to false)
219 boolean outputSplays = false;
220 //Set date to todays datetime
221 Calendar cal = Calendar.getInstance(TimeZone.getDefault());
222 Date today = cal.getTime();
223 //Generate Toporobot format data
224 TopoRobotWriter writer = new TopoRobotWriter(logger);
225 outputData = writer.generateToporobotData( caveModel, today, outputSplays );
226 }
227
228 return outputData;
229 }
230
231 /**
232 * Generates a summary report for the survey
233 * @param survey
234 * @return Report text
235 */
236 private String ReportCaveSurveyStats(CaveSurvey survey) {
237 String report = "Cave Survey: ";
238
239 if ( survey == null ) {
240 report = "No Survey set!";
241 }
242 else {
243 report += survey.getSurveyName() + CaveConverter.newline;
244
245 if ( caveModel.size() == 0 ) {
246 report += "No data was loaded into the cave model!" + CaveConverter.newline;
247 }
248 else {
249 report += "Cave model contains " + caveModel.size() + " top level series." + CaveConverter.newline;
250 for (int i = 0; i < caveModel.size(); i++ ) {
251 report += ReportSeriesStats( caveModel.get(i) );
252 }
253 }
254 }
255
256 return report;
257 }
258
259 /**
260 * Generates a summary report for the survey series
261 * @param series
262 * @return Report text
263 */
264 private String ReportSeriesStats(SurveySeries series) {
265 String report = "Series: " + series.getSeriesName() + CaveConverter.newline;
266 report += "Date: " + series.getSurveyDate() + CaveConverter.newline;
267 report += "Tape units: " + series.getLengthUnit() + CaveConverter.newline;
268 report += "Declination: " + series.getDeclination() + CaveConverter.newline;
269 if ( series.getTapeCalibration(LengthUnit.Metres) != 0 ) {
270 report += "Tape calibration: " + series.getTapeCalibration( series.getLengthUnit() ) + CaveConverter.newline;
271 }
272 if ( series.getCompassCalibration(BearingUnit.Degrees) != 0 ) {
273 report += "Compass calibration: " + series.getCompassCalibration( series.getBearingUnit() ) + CaveConverter.newline;
274 }
275 if ( series.getClinoCalibration(GradientUnit.Degrees) != 0 ) {
276 report += "Clino calibration: " + series.getClinoCalibration( series.getGradientUnit() ) + CaveConverter.newline;
277 }
278
279 if ( series.legCount() > 0 ) {
280 report += "Contains " + series.legCount() + " legs. ";
281 }
282 if ( series.innerSeriesCount() > 0 ) {
283 report += "(Contains " + series.innerSeriesCount() + " child series).";
284 }
285 report += CaveConverter.newline;
286
287 //Recursively log details of inner series
288 for (int i = 0; i < series.innerSeriesCount(); i++ ) {
289 report += ReportSeriesStats( series.getInnerSeries(i) );
290 }
291
292 return report;
293 }
294
295 /**
296 * Generates a summary report for the survey leg
297 * @param leg
298 * @return Report text
299 */
300 private String ReportLegStats(SurveyLeg leg) {
301 String report = leg.toString();
302
303 return report;
304 }
305
306 /**
307 * Gets the selected series in the tree (if multiple nodes are selected then it gets
308 * the first one which was selected)
309 * @return The selected series, or null if selected node is not a SurveySeries
310 */
311 private SurveySeries getSelectedSeries() {
312 SurveySeries selectedSeries = null;
313
314 Object selectedNode = tree.getSelectionPath().getLastPathComponent();
315
316 if (selectedNode instanceof SurveySeries ) {
317 selectedSeries = (SurveySeries) selectedNode;
318 }
319
320 return selectedSeries;
321 }
322 }