aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Adding syntax highlighting to existing application. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding syntax highlighting to existing application." Watch "Adding syntax highlighting to existing application." New topic
Author

Adding syntax highlighting to existing application.

Ger Sweeney
Greenhorn

Joined: Jan 29, 2003
Posts: 11
Hi,
I have developed an editor and am hoping to add syntax highlighting with out changing
to much of my code.
The main method of the editor is given below
private static JFrame mainFrame=new JFrame();
public static void main(String[] args) {
MyEditor edit=new MyEditor();

mainFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
mainFrame.setTitle(resources.getString(title)+" - "+resources.getString(newFile));
mainFrame.setBackground(Color.lightGray);
mainFrame.getContentPane().setLayout(new BorderLayout());
mainFrame.getContentPane().add("Center", edit);
mainFrame.addWindowListener(edit.new AppCloser());
mainFrame.pack();

File f =null;
if (args!=null && args.length>0) f = new File(args[0]);
basicEdit.getPrefereneces(f);

}
}
the getPreferences method basically loads your last saved
preferences and opens any file passed from the main() method.
I am in the process of adding Syntax highlighting and have found
source which I hope to add.

The main method of the syntax highlighter is

public static void main(String[] args) {
//create the demo
SyntaxHighLighter frame = new SyntaxHighLighter();
}
and the Synatxhighlighter() method basically creates the
frame, starts the coloring thread and loads in a piece of
sample souce.
Can someone please tell me the best way to join the
two applications without changing too much of my original
source.
Thanks,
Ger
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
hi Ger,
make the SyntaxHighlighter a subclass of JPanel and add it to the content pane of the main frame.
chantal
Ger Sweeney
Greenhorn

Joined: Jan 29, 2003
Posts: 11
Hi,
Still cant figure it out, its driving me cracked. I have made most of(ie the ones needed)the classes in the SyntaxHighlighter public yet when I call it in my own class(the editor class) it cannot recognise the variables.
As always any help much appreciated.
Ger
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
hi Ger,
what do you mean with "it cannot recognise the variables".
in oo programming the goal should be to encapsulate your implementation so that you access your classes only through methods.
are you getting an error from the compiler or the jvm? it would help, if you would paste it together with the code that is mentioned in the error/exception.
Chantal
Ger Sweeney
Greenhorn

Joined: Jan 29, 2003
Posts: 11
Hi,
This is what i have added to my code, these are the methods in the SyntaxHighlighter along with my own
import etc.
import SyntaxHighlighter;
public class BasicEditor extends JPanel {
private BasicEditor() {
super(true);

setBorder (BorderFactory.createEmptyBorder(5,5,5,5));
setLayout(new BorderLayout());

editor = buildEditor();
editor.setBorder(BorderFactory.createEmptyBorder (3,3,3,3));

editor.getDocument().addUndoableEditListener (undoHandler);
editor.setDropTarget(new MyDropHandler());


editor.addMouseListener(new MyMouseListener());
// install the command table
commands = new Hashtable();

Action[] actions = getActions();

for (int i = 0; i < actions.length; i++) {
Action a = actions[i];
commands.put(a.getValue(Action.NAME), a);
}

JScrollPane scroller = new JScrollPane();

JViewport port = scroller.getViewport();

port.add(editor);

menubar = createMenubar();
add("North", menubar);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add("North",createToolbar());
panel.add("Center", scroller);
JPanel sp = new JPanel();
statusLabel=new JLabel(getResourceString (newFile),JLabel.CENTER);
sp.setLayout(new GridLayout(1,1));
sp.add(statusLabel);
panel.add("South",sp);
add("Center", panel);
/* in the SyntaxHighlighter class */
document = new HighLightedDocument();
/* in the SyntaxHighlighter class */
// Start the thread that does the coloring
colorer = new Colorer();
colorer.start();
/* in the SyntaxHighlighter class */
// Set up the hash table that contains
//the styles.
initStyles();

/* in the SyntaxHighlighter class */
// create the new document.
documentReader = new DocumentReader(document);

/* in the SyntaxHighlighter class */
// Put the initial text into the text pane and
// set it's initial coloring style.
initDocument();
currentFileName="Untitled";
}

One such error im getting is
BasicEditor.java:76: cannot resolve symbol
symbol : method initStyles ()
location: class BasicEditor
initStyles();
I have kept my main method as in the original thread and have changed the following line
private static SyntaxHighlighter mainFrame=new SyntaxHighlighter();
Also in the main method of the SyntaxHighlighter it creates a new Highlighter
public static void main(String[] arg){
Syntaxhighlighter frame = new SyntaxHighlighter()
}
Within the Syntaxhighlighter() method it basically starts the methods I have added above and also creates a simple frame onto which s sample program is loaded.
Hope this makes sense to you.
Thanks again
Ger
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
hi Ger,
what do you mean with

/* in the SyntaxHighlighter class */
document = new HighLightedDocument();

??
Is this part of the code located in the SyntaxHighlighter class or in BasicEditor?
if it is in BasicEditor - why?
Why is BasicEditor a panel if SyntaxHighlighter creates the frame? Frame is the uppermost in the component hierarchy. you are adding components to it, but you are not adding a frame to another component.
IMHO you should start with a little test program, where you create a frame and add panels to it to learn how they work together. The Swing tutorial gives lots of examples.
What concerns the main methods: only one main method is necessary to start the program. any additional main methods are most likely for testing purposes.
Chantal
Ger Sweeney
Greenhorn

Joined: Jan 29, 2003
Posts: 11
Hi Chantal,
I have been able to sort out most of the problems, but am still having difficulty. My application is a more advanced version of the sample program Notepad given in the demo section of JDK1.3. The methods mentioned in the last thread are all part of the syntax highlighting class and are initialised in the BasicEditor class.
Then in the main method I have added the line
BasicEditor be = new BasicEditor();
The problem is that it compiles fine but when it is run it still isn't highlighting the syntax. The problem lies in the file the Editor loads.
For example when you load a new file the source is
public class NewAction extends AbstractAction {
NewAction() {
super(newAction);
}
NewAction(String nm) {
super(nm);
}
public void actionPerformed(ActionEvent e) {
Document oldDoc = getEditor().getDocument();
if(oldDoc != null)
oldDoc.removeUndoableEditListener(undoHandler);
getEditor().setDocument(new PlainDocument());
getEditor().getDocument().addUndoableEditListener(undoHandler);
resetUndoManager();
revalidate();
}
}
I need to change the line getEditor.setDocument(new PlainDocument());
I have tried a range of changes but the are all throwing nullpointer exceptions.
Sorry, this is probably very vague but Im lost.
Thanks for your time,
Ger
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding syntax highlighting to existing application.