Debian Codegraph @

src/footleg/cavesurvey/gui/swing/SourceDataComponent.java

raw symbols SCIP
1 /**
2 * Copyright (C) 2015-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.Font;
22 import java.io.File;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.ListIterator;
26
27 import javax.swing.JFileChooser;
28
29 import footleg.cavesurvey.converter.CaveConverter;
30 import footleg.cavesurvey.converter.Logger;
31 import footleg.cavesurvey.converter.CaveConverter.SurveyDataInputFormats;
32 import footleg.cavesurvey.tools.UtilityFunctions;
33
34 /**
35 * Component to display and hold cave survey source data for conversions and editing.
36 *
37 * @author Footleg
38 * @version 2017.01.10 (ISO 8601 YYYY.MM.DD)
39 * @since 1.7 (The Java version used)
40 *
41 * @to.do
42 * TODO Add highlighting of lines in file which parser could not interpret
43 */
44 @SuppressWarnings("serial")
45 public class SourceDataComponent extends SurveyDatafileTabPane {
46 private CaveConverter.SurveyDataInputFormats format;
47 private List<String> multifileLineRefs;
48
49 /**
50 * Create TextArea scrollable tab instance with specified font
51 * @param font The font to use to display survey data file contents
52 * @param logger Logging class to output information, warning and error messages to
53 */
54 public SourceDataComponent(Font font, Logger logger) {
55 super( font, logger );
56 }
57
58 /**
59 * @return the format
60 */
61 public CaveConverter.SurveyDataInputFormats getFormat() {
62 return format;
63 }
64
65 /**
66 * Opens a new survey data input file and loads the contents into a source data tab pane
67 * @param dataFile The file to open
68 * @param characterSetEncoding The character set encoding to use for the file being read (e.g. UTF8, Cp1252 (for ANSI) )
69 * @return Returns true if file was opened successfully
70 */
71 public boolean openFile(File dataFile, String characterSetEncoding ) {
72 boolean res = false;
73 //Check whether currently loaded data has been modified
74 int action = promptToSaveChanges( characterSetEncoding );
75
76 //Load new data if prompt check passed
77 if ( action == JFileChooser.APPROVE_OPTION ) {
78 //Reset fields before attempting to load new data
79 setText("");
80 format = null;
81
82 //Determine file type from file extension
83 boolean multiFile = false;
84 String fileName = dataFile.getName();
85 int extnStart = fileName.lastIndexOf('.') + 1;
86 String fileExtn = "";
87 if ( extnStart > 0 ) {
88 fileExtn = fileName.substring( extnStart );
89 }
90 format = CaveConverter.inputFormatFromFileExtn( fileExtn );
91 //Set file filter appropriate to file format (this instance of the file
92 //save dialog is specific to this class instance, so this can be set here)
93 if ( format == SurveyDataInputFormats.Compass ) {
94 fileSaveDialog.setFileFilter( compassFileFilter );
95 }
96 else if ( format == SurveyDataInputFormats.DXF ) {
97 fileSaveDialog.setFileFilter( dxfFileFilter );
98 }
99 else if ( format == SurveyDataInputFormats.PocketTopo ) {
100 fileSaveDialog.setFileFilter( pocketTopoFileFilter );
101 }
102 else if ( format == SurveyDataInputFormats.Survex ) {
103 fileSaveDialog.setFileFilter( survexFileFilter );
104 multiFile = true;
105 multifileLineRefs = new ArrayList<String>();
106 }
107
108 //Read data from file
109 List<String> fileData =
110 UtilityFunctions.readTextFile(dataFile, characterSetEncoding, multiFile, multifileLineRefs, logger);
111
112 //Put data into tab
113 ListIterator<String> dataIter = fileData.listIterator();
114 while ( dataIter.hasNext() ) {
115 append(dataIter.next() + "\n");
116 }
117
118 //Store filename in class property
119 loadedFile = dataFile;
120 //Reset modified flag now file loading is complete
121 setModified( false );
122 res = true;
123
124 }
125 return res;
126 }
127
128 }