Debian Codegraph @

src/footleg/cavesurvey/gui/swing/SurveyDatafileTabPane.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.BufferedWriter;
23 import java.io.File;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.OutputStreamWriter;
27 import java.io.Writer;
28
29 import javax.swing.JFileChooser;
30 import javax.swing.JOptionPane;
31 import javax.swing.JScrollPane;
32 import javax.swing.JTextArea;
33 import javax.swing.ScrollPaneConstants;
34 import javax.swing.event.DocumentEvent;
35 import javax.swing.event.DocumentListener;
36 import javax.swing.filechooser.FileNameExtensionFilter;
37
38 import footleg.cavesurvey.converter.Logger;
39
40 /**
41 * Component to display and hold cave survey data.
42 *
43 * @author Footleg
44 * @version 2017.01.10 (ISO 8601 YYYY.MM.DD)
45 * @since 1.7 (The Java version used)
46 *
47 * @to.do
48 * TODO The tab should indicate it's role (as the source data) by colour or other visual indicator.
49 */
50 @SuppressWarnings("serial")
51 public class SurveyDatafileTabPane extends JTextArea {
52 private JScrollPane scrollPane;
53 protected JFileChooser fileSaveDialog;
54 protected FileNameExtensionFilter compassFileFilter = new FileNameExtensionFilter("Compass Data Files","dat");
55 protected FileNameExtensionFilter dxfFileFilter = new FileNameExtensionFilter("DXF Files","dxf");
56 protected FileNameExtensionFilter pocketTopoFileFilter = new FileNameExtensionFilter("PocketTopo Text Files","txt");
57 protected FileNameExtensionFilter survexFileFilter = new FileNameExtensionFilter("Survex Files","svx");
58 protected FileNameExtensionFilter toporobotFileFilter = new FileNameExtensionFilter("Toporobot Data Files","text");
59 private boolean modified;
60 protected String title = "";
61 protected File loadedFile;
62 protected Logger logger;
63
64 public boolean isModified() {
65 return modified;
66 }
67
68 protected void setModified(boolean modified) {
69 boolean oldModified = this.modified;
70 this.modified = modified;
71 firePropertyChange( "modified", oldModified, modified );
72 }
73
74 /**
75 * Create TextArea scrollable tab instance with specified font
76 * @param font The font to use to display survey data file contents
77 * @param logger Logging class to output information, warning and error messages to
78 */
79 public SurveyDatafileTabPane(Font font, Logger logger) {
80 super();
81 this.setFont(font);
82 this.logger = logger;
83 modified = false;
84 scrollPane = new JScrollPane (this, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
85 scrollPane.setViewportView(this);
86
87 //Add listener for document change events
88 this.getDocument().addDocumentListener(new DocumentListener() {
89
90 @Override
91 public void removeUpdate(DocumentEvent e) {
92 //Update modified state
93 setModified(true);
94 }
95
96 @Override
97 public void insertUpdate(DocumentEvent e) {
98 //Update modified state
99 setModified(true);
100 }
101
102 @Override
103 public void changedUpdate(DocumentEvent e) {
104 //Update modified state
105 setModified(true);
106 }
107 });
108
109 //Initialise file save dialog
110 fileSaveDialog = new JFileChooser(".");
111 fileSaveDialog.setFileSelectionMode( JFileChooser.FILES_ONLY );
112 fileSaveDialog.addChoosableFileFilter( compassFileFilter );
113 fileSaveDialog.addChoosableFileFilter( dxfFileFilter );
114 fileSaveDialog.addChoosableFileFilter( pocketTopoFileFilter );
115 fileSaveDialog.addChoosableFileFilter( survexFileFilter );
116 fileSaveDialog.addChoosableFileFilter( toporobotFileFilter );
117 }
118
119 /**
120 * @return the scrollPane
121 */
122 public JScrollPane getTabPaneComponent() {
123 return scrollPane;
124 }
125
126 /**
127 * Prompts user to save changes if data has been modified. Returns a JFileChooser option value.
128 * @param characterSetEncoding The character set encoding to use for the file being written (e.g. UTF8, Cp1252 (for ANSI) )
129 * @return APPROVE_OPTION if saved, ERROR_OPTION if failed, CANCEL_OPTION if user aborted save
130 */
131 public int promptToSaveChanges(String characterSetEncoding) {
132 int action = JOptionPane.NO_OPTION;
133 int saveRes = JFileChooser.APPROVE_OPTION;
134
135 //Check whether currently loaded data has been modified
136 if ( isModified() ) {
137 //Ask whether to save before loading new data
138 action = JOptionPane.showConfirmDialog( this.getParent(), "Save changes to survey data file " + title + "?",
139 "Save changes?", JOptionPane.YES_NO_CANCEL_OPTION);
140 if ( action == JOptionPane.YES_OPTION ) {
141 //Save file before proceeding
142 saveRes = saveData( false, characterSetEncoding );
143 }
144 else if ( action == JOptionPane.CANCEL_OPTION ) {
145 //Change result to Cancel if user cancelled save
146 saveRes = JFileChooser.CANCEL_OPTION;
147 }
148 }
149
150 return saveRes;
151 }
152
153 /**
154 * Save survey data to file. Returns JFileChooser Option responses (APPROVE_OPTION, ERROR_OPTION, CANCEL_OPTION)
155 * @param saveAs If true then show file chooser even if a filename is already set
156 * @param characterSetEncoding The character set encoding to use for the file being written (e.g. UTF8, Cp1252 (for ANSI) )
157 * @return Returns JFileChooser Option responses: APPROVE_OPTION if saved, ERROR_OPTION if failed, CANCEL_OPTION if user aborted save
158 */
159 public int saveData( boolean saveAs, String characterSetEncoding ) {
160 int action = JFileChooser.APPROVE_OPTION;
161 File saveFile = loadedFile;
162
163 if ( saveAs || loadedFile == null ) {
164 //Set file chooser to current filename
165 fileSaveDialog.setSelectedFile( loadedFile );
166 //Enable new filename to be specified
167 boolean overwriteCheckDone = false;
168 while ( overwriteCheckDone == false ) {
169 //Show file save dialog for user to specify file path
170 action = fileSaveDialog.showSaveDialog( this );
171 if ( action == JFileChooser.APPROVE_OPTION ) {
172 //Check whether specified file already exists and warn of overwrite
173 int overwriteConfirm = JOptionPane.YES_OPTION;
174 if ( fileSaveDialog.getSelectedFile().exists() ) {
175 overwriteConfirm = JOptionPane.showConfirmDialog( this.getParent(),
176 "File " + fileSaveDialog.getSelectedFile().getPath() + " already exists. Do you want to replace this file?",
177 "Overwrite file?", JOptionPane.YES_NO_OPTION);
178 }
179 if ( overwriteConfirm == JOptionPane.YES_OPTION ) {
180 saveFile = fileSaveDialog.getSelectedFile();
181 overwriteCheckDone = true;
182 }
183 }
184 else {
185 overwriteCheckDone = true;
186 }
187 }
188 }
189
190 if ( action == JFileChooser.APPROVE_OPTION ) {
191 //Save File
192 BufferedWriter textWriter = null;
193 try {
194 FileOutputStream fos = new FileOutputStream( saveFile );
195 Writer out = new OutputStreamWriter(fos, characterSetEncoding);
196 textWriter = new BufferedWriter(out);
197 textWriter.write( this.getText() );
198 //Update file reference for new saved file
199 loadedFile = saveFile;
200 //Reset modified status
201 setModified( false );
202 }
203 catch (Exception e) {
204 JOptionPane.showMessageDialog( this, "Error writing file: " + e.getMessage(),
205 "Error", JOptionPane.ERROR_MESSAGE);
206 action = JFileChooser.ERROR_OPTION;
207 }
208 finally {
209 if ( textWriter != null ) {
210 try {
211 textWriter.close();
212 }
213 catch (IOException ex) {
214 JOptionPane.showMessageDialog( this, "Error closing file.",
215 "Error", JOptionPane.ERROR_MESSAGE);
216 action = JFileChooser.ERROR_OPTION;
217 }
218 }
219 }
220 }
221 return action;
222 }
223
224 }