This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Loading a document  RSS feed

 
Ciara O Brien
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have found a package known as Ostermiller's syntax highlighter on my travels and have modified it to perform text editor functions also, with the goal of creating an IDE eventually however am having spot of bother loading documents. I have included some of the source below however the fileLoader class is from an earlier swing editor I created which inserts the String to a plainDocument. I have tried loads of different suggestions but to no avail. The editor keeps crashing after coloring the first line.
public HighLightedDocument doc;
// .....
// call the file Loader
// file f, document
Thread loader = new FileLoader(f, document);
loader.start();
//......
/*
* Class to load a file
public final class FileLoader extends Thread {
/** Loads a file*/
FileLoader(File f, HighLightedDocument doc) {
this.f = f;
setPriority(4);
this.doc = doc;
try {
in =new BufferedReader(new FileReader(f));
}
catch (FileNotFoundException e){}
}

public void run() {
try {
StringBuffer data = new StringBuffer();
data.ensureCapacity((int)f.length());
}
String inline = new String();
while ((inline = in.readLine()) !=null)
{
data.append(inline);
data.append('\n');
}
documentReader = new DocumentReader(doc);
//setupHighLighter();
in.close();
String test = data.toString();
doc.insertString(0,test, null);
colorAll();


}
catch (IOException e) {
System.err.println(e.toString());
}
catch (BadLocationException e) {
System.err.println(e.getMessage());
}
private BufferedReader in;


/**The document to load the file into*/
private HighLightedDocument doc;

private File f;
}
}//end of file loader class
/**
* the highlightedDocument class
* Just like a DefaultStyledDocument but * intercepts inserts and
* removes to color them.
*/
private class HighLightedDocument extends DefaultStyledDocument {
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
synchronized (doclock){
super.insertString(offs, str, a);
color(offs, str.length());
documentReader.update(offs, str.length());
}
}
public void remove(int offs, int len) throws BadLocationException {
synchronized (doclock){
super.remove(offs, len);
color(offs, -len);
documentReader.update(offs, -len);
}
}
}
The styles are held in a hashtable and are of type SimpleAttributeSet().
The colorAll method colors the text from 0 to the length of the document.
Hope this makes sense
Cheers,
Ciara
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ciara,
Welcome to JavaRanch. I'd be more than happy to take a look at your code, but you seem to have a variety of syntax errors here. Try reposting the code with a version that will compile. Also, if you will use the CODE button and place the code between the tags, it will be easier to read.
Michael Morris
 
Ciara O Brien
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about that. I have included all the appropriate sections of code here I think!
Thanks again
Ciara
 
Ciara O Brien
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry forgot the setuphighlighter method, it was just a method I was testing.
Thanks
Ciara
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again Ciara,
I hate to keep being a pest, but still seem to be missing some code here. If you like you can email me what you have, since it seems to be a lengthy amount of code.
Michael Morris
 
Ciara O Brien
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your time Michael, have emailed them to you there just now.
Ciara
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!