Skip to content

Commit 635a87e

Browse files
committed
progress
1 parent a411470 commit 635a87e

File tree

3 files changed

+77
-57
lines changed

3 files changed

+77
-57
lines changed

RSyntaxTextArea/src/main/java/org/fife/ui/rsyntaxtextarea/TextEditorPane.java

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,6 @@ public void load(FileLocation loc) throws IOException {
434434
load(loc, (String) null);
435435
}
436436

437-
438437
/**
439438
* Loads the specified file in this editor. This method fires a property
440439
* change event of type {@link #FULL_PATH_PROPERTY}.
@@ -447,61 +446,61 @@ public void load(FileLocation loc) throws IOException {
447446
* is used.
448447
* @throws IOException If an IO error occurs.
449448
* @see #load(FileLocation)
450-
* @see #load(FileLocation, String)
449+
* @see #load(FileLocation, Charset)
451450
* @see #save()
452451
* @see #saveAs(FileLocation)
453452
*/
454-
public void load(FileLocation loc, Charset defaultEnc) throws IOException {
455-
load(loc, defaultEnc == null ? null : defaultEnc.name());
453+
public void load(FileLocation loc, String defaultEnc) throws IOException {
454+
load(loc, defaultEnc == null ? Charset.defaultCharset() : Charset.forName(defaultEnc));
456455
}
457456

458-
459457
/**
460458
* Loads the specified file in this editor. This method fires a property
461459
* change event of type {@link #FULL_PATH_PROPERTY}.
462460
*
463-
* @param loc The location of the file to load. This cannot be
464-
* <code>null</code>.
465-
* @param defaultEnc The encoding to use when loading/saving the file.
466-
* This encoding will only be used if the file is not Unicode.
467-
* If this value is <code>null</code>, the system default encoding
468-
* is used.
461+
* @param loc The location of the file to load. This cannot be
462+
* <code>null</code>.
463+
* @param defaultCharset The encoding to use when loading/saving the file.
464+
* This encoding will only be used if the file is not Unicode.
465+
* If this value is <code>null</code>, the system default encoding
466+
* is used.
469467
* @throws IOException If an IO error occurs.
470468
* @see #load(FileLocation)
471-
* @see #load(FileLocation, Charset)
469+
* @see #load(FileLocation, String)
472470
* @see #save()
473471
* @see #saveAs(FileLocation)
474472
*/
475-
public void load(FileLocation loc, String defaultEnc) throws IOException {
476-
RSyntaxDocument doc = createEditableDocument(loc, defaultEnc);
477-
loadDocument(loc, doc);
473+
public void load(FileLocation loc, Charset defaultCharset) throws IOException {
474+
Charset fileLocationCharset = getFileLocationCharset(loc, defaultCharset);
475+
RSyntaxDocument doc = createEditableDocument(loc, fileLocationCharset);
476+
loadDocument(loc, doc, defaultCharset);
478477
}
479478

480479
public void loadLocalFileInReadOnlyDocument(File file, Charset defaultCharset) throws IOException {
481480
FileLocation fileLocation = FileLocation.create(file);
482-
RSyntaxDocument doc = createLazyLoadDocument(fileLocation, defaultCharset);
481+
Charset fileLocationCharset = getFileLocationCharset(fileLocation, defaultCharset);
482+
RSyntaxDocument doc = createLazyLoadDocument(fileLocation, fileLocationCharset);
483+
loadDocument(fileLocation, doc, defaultCharset);
484+
}
483485

484-
loadDocument(fileLocation, doc);
486+
private Charset getFileLocationCharset(FileLocation fileLocation, Charset defaultCharset) throws IOException {
487+
try( InputStream is = fileLocation.getInputStream() ){
488+
UnicodeReader ur = new UnicodeReader(is, defaultCharset);
489+
defaultCharset = Charset.forName(ur.getEncoding());
490+
}
491+
return defaultCharset;
485492
}
486493

487-
private RSyntaxDocument createEditableDocument(FileLocation loc, String defaultEnc) throws IOException {
494+
private RSyntaxDocument createEditableDocument(FileLocation loc, Charset charset) throws IOException {
488495
RSyntaxDocument doc = createDefaultModel();
489496
// For new local files, just go with it.
490-
if( loc.isLocal() && !loc.isLocalAndExists() ){
491-
this.charSet = defaultEnc != null ? defaultEnc : getDefaultEncoding();
492-
} else{
493-
// Old local files and remote files, load 'em up. UnicodeReader will
494-
// check for BOMs and handle them correctly in all cases, then pass
495-
// rest of stream down to InputStreamReader.
496-
UnicodeReader ur = new UnicodeReader(loc.getInputStream(), defaultEnc);
497-
charSet = ur.getEncoding();
498-
499-
try( BufferedReader r = new BufferedReader(ur) ){
500-
RTextAreaEditorKit kit = (RTextAreaEditorKit)getUI().getEditorKit(this);
501-
try {
497+
if( !loc.isLocal() || loc.isLocalAndExists() ){
498+
try( InputStreamReader r = new InputStreamReader(loc.getInputStream(), charset) ){
499+
RTextAreaEditorKit kit = (RTextAreaEditorKit) getUI().getEditorKit(this);
500+
try{
502501
// NOTE: Resets the "line separator" property.
503502
kit.read(r, doc, 0);
504-
} catch ( BadLocationException e) {
503+
} catch( BadLocationException e ){
505504
throw new IOException(e.getMessage());
506505
}
507506
}
@@ -510,20 +509,15 @@ private RSyntaxDocument createEditableDocument(FileLocation loc, String defaultE
510509
return doc;
511510
}
512511

513-
private RSyntaxDocument createLazyLoadDocument(FileLocation fileLocation, Charset defaultCharset) throws IOException {
512+
private RSyntaxDocument createLazyLoadDocument(FileLocation fileLocation, Charset charSet) throws IOException {
514513
if( !fileLocation.isLocalAndExists() ){
515514
throw new FileNotFoundException("ReadOnlyDucument supports only files from local file system");
516515
}
517-
518-
try (InputStream is = fileLocation.getInputStream()) {
519-
UnicodeReader ur = new UnicodeReader(is, defaultCharset);
520-
charSet = ur.getEncoding();
521-
return new ReadOnlyDocument(getTokenMarkerFactory(), getSyntaxEditingStyle(), new ReadOnlyContent(new File(fileLocation.getFileFullPath()), Charset.forName(charSet)));
522-
}
523-
516+
return new ReadOnlyDocument(getTokenMarkerFactory(), getSyntaxEditingStyle(), new ReadOnlyContent(new File(fileLocation.getFileFullPath()), charSet));
524517
}
525518

526-
public void loadDocument(FileLocation fileLocation, RSyntaxDocument doc) {
519+
public void loadDocument(FileLocation fileLocation, RSyntaxDocument doc, Charset charSet) {
520+
this.charSet = charSet.name();
527521
String oldFileFullPath = getFileFullPath();
528522
setDocument(doc);
529523
this.loc = fileLocation;

RSyntaxTextArea/src/main/java/org/fife/ui/rtextarea/readonly/ReadOnlyContent.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class ReadOnlyContent implements ReadOnlyContentInterface {
2020
private static final int NOT_INITIALIZED_BUFFER_OFFSET = -1;
2121

2222
private final File file;
23-
private final Charset fixedSizeCharset;
23+
private final Charset charset;
2424
private final List<Integer> offsets;
2525
private final int bufferSize;
2626
private final int fileSize;
@@ -31,16 +31,13 @@ public class ReadOnlyContent implements ReadOnlyContentInterface {
3131
private int startBufferOffset = NOT_INITIALIZED_BUFFER_OFFSET;
3232

3333

34-
public ReadOnlyContent(File file, Charset fixedSizeCharset) {
35-
this.fixedSizeCharset = fixedSizeCharset;
36-
34+
public ReadOnlyContent(File file, Charset charset) {
35+
this.file = file;
36+
this.charset = charset;
3737
fileSize = (int) file.length();
3838
bufferSize = Math.min(fileSize, MAX_BUFFER_SIZE);
3939
buffer = new char[bufferSize];
40-
41-
42-
this.file = file;
43-
offsets = FileOffsetLineDiscover.getOffsets(file.toPath(), fixedSizeCharset);
40+
offsets = FileOffsetLineDiscover.getOffsets(file.toPath(), charset);
4441
}
4542

4643

@@ -98,13 +95,13 @@ private void reloadBuffer(int startReadOffset) throws BadLocationException {
9895
read(startReadOffset, bufferSize);
9996
}
10097

101-
private void read(int startReadOffset, int bytes) throws BadLocationException {
102-
try( BufferedReader bufferedReader = Files.newBufferedReader(file.toPath(), fixedSizeCharset); ){
98+
private void read(int startReadOffset, int charsToRead) throws BadLocationException {
99+
try( BufferedReader bufferedReader = Files.newBufferedReader(file.toPath(), charset); ){
103100
bufferedReader.skip(startReadOffset);
104-
int bytesRead = bufferedReader.read(buffer, 0, bytes);
101+
int foundCharsCount = bufferedReader.read(buffer, 0, charsToRead);
105102

106-
if( bytes != bytesRead ){
107-
System.out.println("AAAA startReadOffset = " + startReadOffset + ", bytes = " + bytes);
103+
if( charsToRead != foundCharsCount ){
104+
throw new BadLocationException("Not all required chars are available", startReadOffset);
108105
}
109106
} catch( Exception e ){
110107
throw new ReadOnlyBadLocationException("Unable to read file", startReadOffset, e);
@@ -160,7 +157,7 @@ public int getElementIndex(int offset) {
160157
}
161158

162159
private int getCharsCount() {
163-
try( BufferedReader bufferedReader = Files.newBufferedReader(file.toPath(), fixedSizeCharset); ){
160+
try( BufferedReader bufferedReader = Files.newBufferedReader(file.toPath(), charset); ){
164161
char[] charBuffer = new char[1024 * 1024];
165162
int count = 2;
166163
int readChars = 0;

RSyntaxTextArea/src/test/java/org/fife/ui/rtextarea/readonly/ReadOnlyDocumentLauncher.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77

88
import javax.swing.*;
99
import javax.swing.text.BadLocationException;
10+
import javax.swing.text.Document;
1011
import javax.swing.text.Highlighter;
1112
import java.awt.Dimension;
1213
import java.awt.event.ActionEvent;
14+
import java.beans.PropertyChangeEvent;
15+
import java.beans.PropertyChangeListener;
1316
import java.io.File;
1417
import java.io.IOException;
1518
import java.nio.charset.Charset;
1619
import java.nio.charset.StandardCharsets;
20+
import java.util.concurrent.ExecutionException;
1721

1822
import static org.fife.ui.rsyntaxtextarea.SyntaxConstants.SYNTAX_STYLE_XML;
1923

@@ -54,14 +58,39 @@ private void buildContent(JFrame frame) throws IOException {
5458
FileBuilderTestUtil.writeTestFileWithAsciiChars(documentFile.toPath(), charset, "\n", 1024);
5559

5660
TextEditorPane textArea = new TextEditorPane();
61+
5762
textArea.loadLocalFileInReadOnlyDocument(documentFile, charset);
5863
textArea.setSyntaxEditingStyle(SYNTAX_STYLE_XML);
5964
RTextScrollPane scrollPane = new RTextScrollPane(textArea, true);
6065
frame.getContentPane().add(scrollPane);
6166
JMenuBar menubar = new JMenuBar();
6267

6368
menubar.add(creteFileChooserAction(frame, (selectedFile) -> textArea.load(FileLocation.create(selectedFile)), "Open editable document"));
64-
menubar.add(creteFileChooserAction(frame, file -> textArea.loadLocalFileInReadOnlyDocument(file, charset), "Open read only document"));
69+
menubar.add(creteFileChooserAction(frame, file -> {
70+
71+
JProgressBar progressBar = new JProgressBar();
72+
progressBar.setIndeterminate(true);
73+
JDialog progressDialog = new JDialog(frame);
74+
progressDialog.add(progressBar);
75+
progressDialog.setSize(400,400);
76+
progressDialog.setVisible(true);
77+
SwingWorker<Object, Object> swingWorker = new SwingWorker<>() {
78+
@Override
79+
protected ReadOnlyDocument doInBackground() {
80+
ReadOnlyDocument tokens = new ReadOnlyDocument(null, SYNTAX_STYLE_XML, new ReadOnlyContent(file, StandardCharsets.UTF_8));
81+
textArea.loadDocument(FileLocation.create(file), tokens, StandardCharsets.UTF_8);
82+
return null;
83+
}
84+
85+
@Override
86+
protected void done() {
87+
progressDialog.dispose();
88+
}
89+
};
90+
91+
swingWorker.execute();
92+
93+
}, "Open read only document"));
6594

6695
frame.setJMenuBar(menubar);
6796
}
@@ -82,7 +111,7 @@ private static void onActionPerformed(JFrame frame, LoadFileAction fileAction) {
82111
File selectedFile = fc.getSelectedFile();
83112
try{
84113
fileAction.onFileSelected(selectedFile);
85-
} catch( IOException ex ){
114+
} catch( Exception ex ){
86115
throw new RuntimeException(ex);
87116
}
88117
}
@@ -91,7 +120,7 @@ private static void onActionPerformed(JFrame frame, LoadFileAction fileAction) {
91120
@FunctionalInterface
92121
private interface LoadFileAction {
93122

94-
void onFileSelected(File file) throws IOException;
123+
void onFileSelected(File file) throws IOException, ExecutionException, InterruptedException;
95124

96125
}
97126

0 commit comments

Comments
 (0)