001: package net.xoetrope.builder.editor.helper;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.IOException;
006:
007: import java.awt.BorderLayout;
008: import java.awt.Dimension;
009: import javax.swing.BorderFactory;
010: import javax.swing.JPanel;
011:
012: import net.xoetrope.builder.editor.syntaxhighlight.JEditTextArea;
013: import net.xoetrope.builder.editor.syntaxhighlight.JavaTokenMarker;
014: import net.xoetrope.builder.editor.syntaxhighlight.SyntaxDocument;
015: import net.xoetrope.builder.editor.syntaxhighlight.TextAreaDefaults;
016: import java.awt.Color;
017: import net.xoetrope.builder.editor.plugin.XEditorPlugin;
018:
019: /**
020: * <p>Copyright (c) Xoetrope 2002-2003, All rights reserved </p>
021: * <p>$Revision: 1.2 $ </p>
022: * <p>License: see license.txt </p>
023: */
024: public class Buffer extends JPanel {
025: private JEditTextArea textArea;
026: private static boolean isReading = false;
027: private File sourceFile;
028: private long originalHash;
029:
030: public Buffer() {
031: TextAreaDefaults defs = TextAreaDefaults.getDefaults();
032: textArea = new JEditTextArea(defs);
033: try {
034: defs.document = new SyntaxDocument();
035: textArea.setDocument(defs.document);
036: textArea.setTokenMarker(new JavaTokenMarker());
037: } catch (Exception ex) {
038: ex.printStackTrace();
039: }
040:
041: JPanel buffer = new JPanel();
042: buffer.setLayout(new BorderLayout());
043: buffer.add(textArea, BorderLayout.CENTER);
044:
045: setLayout(new BorderLayout());
046: add(buffer, BorderLayout.CENTER);
047: }
048:
049: public synchronized void openFile(File file) throws IOException {
050: sourceFile = file;
051: isReading = true;
052: FileInputStream fis = new FileInputStream(sourceFile);
053: int buflen = 1024;
054: byte charBuffer[] = new byte[buflen];
055: int count = 0;
056: StringBuffer buffer = new StringBuffer();
057:
058: do {
059: buffer.append(new String(charBuffer, 0, count));
060: count = fis.read(charBuffer, 0, buflen);
061: } while (count >= 0);
062: isReading = false;
063:
064: String javaSrc = buffer.toString();
065: textArea.setText(javaSrc);
066: originalHash = javaSrc.hashCode();
067: textArea.setCaretPosition(0);
068: }
069:
070: public void setText(String javaSrc) {
071: textArea.setText(javaSrc);
072: originalHash = javaSrc.hashCode();
073: }
074:
075: public synchronized boolean isPerformingIO() {
076: return isReading;
077: }
078:
079: public boolean isDirty() {
080: return (originalHash == textArea.getText().hashCode());
081: }
082:
083: public JEditTextArea getTextArea() {
084: return textArea;
085: }
086:
087: public String getPath() {
088: try {
089: return sourceFile.getCanonicalPath();
090: } catch (IOException ex) {
091: return null;
092: }
093: }
094:
095: public void reload(XEditorPlugin plugin) {
096: try {
097: openFile(sourceFile);
098: } catch (IOException ex) {
099: ex.printStackTrace();
100: }
101: }
102:
103: public String getName() {
104: return sourceFile.getName();
105: }
106: }
|