| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 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 |
|
| 76 |
|
| 77 |
|
| 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 |
|
| 88 |
this.getDocument().addDocumentListener(new DocumentListener() { |
| 89 |
|
| 90 |
@Override |
| 91 |
public void removeUpdate(DocumentEvent e) { |
| 92 |
|
| 93 |
setModified(true); |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public void insertUpdate(DocumentEvent e) { |
| 98 |
|
| 99 |
setModified(true); |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public void changedUpdate(DocumentEvent e) { |
| 104 |
|
| 105 |
setModified(true); |
| 106 |
} |
| 107 |
}); |
| 108 |
|
| 109 |
|
| 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 |
|
| 121 |
|
| 122 |
public JScrollPane getTabPaneComponent() { |
| 123 |
return scrollPane; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public int promptToSaveChanges(String characterSetEncoding) { |
| 132 |
int action = JOptionPane.NO_OPTION; |
| 133 |
int saveRes = JFileChooser.APPROVE_OPTION; |
| 134 |
|
| 135 |
|
| 136 |
if ( isModified() ) { |
| 137 |
|
| 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 |
|
| 142 |
saveRes = saveData( false, characterSetEncoding ); |
| 143 |
} |
| 144 |
else if ( action == JOptionPane.CANCEL_OPTION ) { |
| 145 |
|
| 146 |
saveRes = JFileChooser.CANCEL_OPTION; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
return saveRes; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 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 |
|
| 165 |
fileSaveDialog.setSelectedFile( loadedFile ); |
| 166 |
|
| 167 |
boolean overwriteCheckDone = false; |
| 168 |
while ( overwriteCheckDone == false ) { |
| 169 |
|
| 170 |
action = fileSaveDialog.showSaveDialog( this ); |
| 171 |
if ( action == JFileChooser.APPROVE_OPTION ) { |
| 172 |
|
| 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 |
|
| 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 |
|
| 199 |
loadedFile = saveFile; |
| 200 |
|
| 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 |
} |