Você está na página 1de 33

“A PROJECT ON JAVA EDITOR”

Mini Project work Submitted in partial fulfillment of the


Requirements for the award of the degree of
BE(Computer Science & Engineering)
By
M.NATARAJAN
(REG.NO-40708104042)

Under the guidance of


Mrs.B.DURGADEVI B.E.,

Department of Computer Science and Engineering

GKM College of Engineering and Technology


Alapakkam-Mappedu Road,
G.K.M Nagar,
Chennai-600 063.
2010-2011
Department of Computer Science and Engineering

GKM College of Engineering and Technology

Alapakkam-Mappedu Road,G.K.M Nagar,

Chennai-600 063.

BONAFIDE
CERTIFICATE
Certificate that this Mini project report titled “JAVA EDITOR” the bonafide work of
Mr. M.NATARAJAN, REG.NO -40708104042 who carried out the research under my
supervision. Certified further, that to the best of my knowledge the work reported herein does not
form part of any other project report or dissertation on the basis of which
degree or award was conferred on an earlier occasion on this or any other candidate.

Project Guide Head of the Department


Mrs.B.DURGADEVI B.E., Mr.M.BABU M.E.,

Internal Examiner External Examiner


CONTENTS

1. SYNOPSIS.

2. SYSTEM CONFIGURATION.

3. SOFTWARE DESCRIPTION.

• ABOUT JAVA.

4. PROJECT DETAILS.

5. BIBILOGRAPHY.

6. SOURCE CODE.

7. SNAPSHOT.

8. FUTURE ENCHANCEMENTS.

9. CONCLUSION.

SYNOPSIS:
Java Editor is the easy-to-use and it is developed with many powerful features to
improve your productivity and programming experience. Ideal for both the beginner and more
experienced programmer.

Java Editor is very useful developing large scale projects. The developer can
avoid the typing of the commands for each and every operation so he can finish the project and
deliver the user in time.

It reduces the complexity to the programmer and he can concentrate in his concept and do his
work correctly.

SYSTEM CONFIGURATION:

MONITOR : 14” COLOR.

KEYBOARD : 101 KEYBOARD.

MOUSE : LOGITECH MOUSE.

RAM CAPACITY : 512 MB.

HDD CAPACITY : 180 GB.

CD DRIVE : 52X.

SOFTWARE DESCRIPTION:
ABOUT JAVA:

Java is a programming language originally developed by James


Gosling at Sun Microsystems (which is now a subsidiary of Oracle Corporation) and released in
1995 as a core component of Sun Microsystems' Java platform. The language derives much of its
syntax from C and C++ but has a simpler object model and fewer low-level facilities. Java
applications are typically compiled to byte code (class file) that can run on any Java Virtual
Machine (JVM) regardless of computer architecture. Java is a general-purpose, concurrent, class-
based, object-oriented language that is specifically designed to have as few implementation
dependencies as possible. It is intended to let application developers "write once, run anywhere".
Java is currently one of the most popular programming languages in use, and is widely used from
application software to web applications.

The original and reference implementation Java compilers, virtual machines, and class libraries
were developed by Sun from 1995. As of May 2007, in compliance with the specifications of the
Java Community Process, Sun relicensed most of its Java technologies under the GNU General
Public License. Others have also developed alternative implementations of these Sun
technologies, such as the GNU Compiler for Java, GNU Class path, and Dalvik.

James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project in June
1991. Java was originally designed for interactive television, but it was too advanced. The
language was initially called Oak after an oak tree that stood outside Gosling's office; it went by
the name Green later, and was later renamed Java, from a list of random words. Gosling aimed
to implement a virtual machine and a language that had a familiar C/C++ style of notation.

Sun Microsystems released the first public implementation as Java 1.0 in 1995. It promised
"Write Once, Run anywhere" (WORA), providing no-cost run-times on popular platforms. Fairly
secure and featuring configurable security, it allowed network- and file-access restrictions. Major
web browsers soon incorporated the ability to run Java applets within web pages, and Java
quickly became popular. With the advent of Java 2 (released initially as J2SE 1.2 in December
1998–1999), new versions had multiple configurations built for different types of platforms. For
example, J2EE targeted enterprise applications and the greatly stripped-down version J2ME for
mobile applications (Mobile Java). J2SE designated the Standard Edition. In 2006, for marketing
purposes, Sun renamed new J2 versions as Java EE, Java ME, and Java SE, respectively.

In 1997, Sun Microsystems approached the ISO/IEC JTC1 standards body and later the Ecma
International to formalize Java, but it soon withdrew from the process. Java remains a de facto
standard, controlled through the Java Community Process. At one time, Sun made most of its
Java implementations available without charge, despite their proprietary software status. Sun
generated revenue from Java through the selling of licenses for specialized products such as the
Java Enterprise System. Sun distinguishes between its Software Development Kit (SDK) and
Runtime Environment (JRE) (a subset of the SDK); the primary distinction involves the JRE's
lack of the compiler, utility programs, and header file.
PROJECT DETAILS:

MOUDULES:

• MENUS.
• EDIT.
• EXECUTE.

MENUS:

New:

They are used to create a new file.

Open:

They are used to open a already existing file.

Close:

They are used to close the currently active file.

Print:

They are used to print the file.

EDIT:

Cut:

They are used to cut the text from the textarea.

Paste:

They are used to paste the text to the textarea.

Undo:

They are used to perform the undo operation.


EXECUTE:

COMPILE:

A Java compiler is a compiler for the Java programming language. The most
common form of output from a Java compiler are Java class files containing platform-neutral
Java bytecode. There exist also compilers emitting optimized native machine code for a
particular hardware/operating system combination

BUILD:

In java programming language the dataflow between the programs can be identified by
using the build option.

RUN:

In java programming language the java interpreter produces machine code from the
bytecode and run the program.

BIBILOGRAPHY:
• AG98
Ken Arnold and James Gosling, The Java Programming Language, second ed., Addison-
Wesley, 2009.

• Chan98
Patrick Chan, The Java Developers Almanac, Addison-Wesley, 2007.

• CM96
Peter Coad and Mark Mayfield, Java Design: Building Better Apps and Applets, Yourdon
Press, 1996.

• CH97
Gary Cornell and Cay S. Horstmann, Core Java, second ed., SunSoft Press, 1997.

• ELW98
Robert Eckstein and Marc Loy and Dave Wood, Java Swing, O'Reilly, 2009.

• Englander97
Robert Englander, Developing Java Beans, O'Reilly, 2007.

• Flanagan96
David Flanagan, Java in a Nutshell, second ed., O'Reilly, 2006.

• Flanagan99
David Flanagan, Java Foundation Classes in a Nutshell, O'Reilly, 2009.

• Gea99a
David M. Geary, Graphic Java 2: Mastering the JFC, vol. I, AWT, third ed., Sun
Microsystems Press, 2001.

• Gea99b
David M. Geary Graphic Java 2: Mastering the JFC, vol. II, Swing, third ed., Sun
Microsystems Press, 2001.

• CORE JAVA PUBLISHER SUN MICROSYSTEMS ed., 2010.

SOURCE CODE:
import sd.components.tabbedpanel.titledtab.TitledTabProperties;
import sd.components.tabbedpanel.theme.*;
import java.awt.*;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import java.io.File;
import javax.swing.*;
import sd.components.tabbedpanel.*;
public class MainForm extends JFrame implements TabListener {
private TitledTabProperties titledTabProperties = new TitledTabProperties();
public sd.components.SDTabbedPanel mainTab;
public SDMenuBar mnub;
public static java.util.ArrayList commandList=new java.util.ArrayList();
public MainForm() {
super("SDJEditor");
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
int n = JOptionPane.showConfirmDialog(null,"Are You Sure to Close This
Programe","SDJCreator",JOptionPane.YES_NO_OPTION);
if(n==JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
});

Dimension size=Toolkit.getDefaultToolkit().getScreenSize();
setSize(size.width-60,size.height-40);
setLocationRelativeTo(null);
mainTab=new sd.components.SDTabbedPanel();
mainTab.setCloseButton();
TabbedPanelTitledTabTheme themes =new ShapedGradientTheme();
mainTab.getProperties().addSuperObject(themes.getTabbedPanelProperties());
titledTabProperties.addSuperObject(themes.getTitledTabProperties());
mainTab.getProperties().setEnsureSelectedTabVisible(true);
mainTab.getProperties().setTabLayoutPolicy(sd.components.tabbedpanel.TabLayoutPolicy.SCROLLING);
mainTab.getProperties().setTabDropDownListVisiblePolicy(sd.components.tabbedpanel.TabDropDownLi
stVisiblePolicy.MORE_THAN_ONE_TAB);
getContentPane().add(mainTab,BorderLayout.CENTER);
sd.components.swing.StatusBar sb=new sd.components.swing.StatusBar();
sb.show(true);
getContentPane().add(sb,BorderLayout.PAGE_END);
mainTab.setBackground(Color.RED);
mnub=new SDMenuBar();
setJMenuBar(mnub);
mainTab.addTabListener(this);
// new Tray();
} public void addTab(sd.components.tabbedpanel.titledtab.TitledTab tab)
{ tab.setInheritsPopupMenu(true);
tab.getProperties().addSuperObject(titledTabProperties);
mainTab.addTab(tab);
mainTab.setSelectedTab(tab);
} public javax.swing.JTextPane getEditor()
{ if(mainTab.getTabCount()==0)
return null;
if(mainTab==null)
return null;
if(mainTab.getSelectedTab()==null)
return null;
return ((NewFile)mainTab.getSelectedTab().getTag()).getEditor();
//return ((NewFile)mainTab.getSelectedTab().getContentComponent()).getEditor()
} public javax.swing.JTextArea getConsole()
{
if(mainTab.getTabCount()==0)
return null;
if(mainTab==null)
return null;
if(mainTab.getSelectedTab()==null)
return null;
return ((NewFile)mainTab.getSelectedTab().getTag()).getConsole();
} public NewFile getFileDetails()
{
if(mainTab.getTabCount()==0)
return null;
if(mainTab==null)
return null;
if(mainTab.getSelectedTab()==null)
return null;
return ((NewFile)mainTab.getSelectedTab().getTag());
}
public void tabAdded(TabEvent event)
{

}
public void tabRemoved(TabRemovedEvent event)
{
/*sd.components.tabbedpanel.titledtab.TitledTab
tab=(sd.components.tabbedpanel.titledtab.TitledTab)event.getTab();
if(tab.getText().indexOf("*")!=-1)
{
int n = JOptionPane.showConfirmDialog(null,"Do You Want To Save The
Changes","SDJCreator",JOptionPane.YES_NO_CANCEL_OPTION);
if(n==JOptionPane.YES_OPTION)
{

}else if(n==JOptionPane.NO_OPTION)
{
}else
{}}*/ }
public void tabDragged(TabDragEvent event)
{
}
public void tabDropped(TabDragEvent event)
{
}
public void tabDragAborted(TabEvent event)
{
}
public void tabSelected(TabStateChangedEvent event)
{
SDJEditor.frmMain.mnub.setSaveSaveAsMenuItemEnableDisable();
}
public void tabDeselected(TabStateChangedEvent event)
{
}
public void tabHighlighted(TabStateChangedEvent event)
{ }
public void tabDehighlighted(TabStateChangedEvent event)
{ }
public void tabMoved(TabEvent event)
{ }
}
COMPILE:
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.awt.*;
import java.util.*;
class Build extends Thread {

private ArrayList list, errorList;


private JViewport vp;
private JScrollBar scrollBar;
private int option, index;
private JTextArea console;
private boolean lock;
private TextAreaUI myUID;
private boolean compilingSuccess = false;
private File project, file;
private volatile boolean execute = true;
private long startTime = -1,
endTime = -1;

private String stop = "< Press the STOP button on the toolbar to force " +
"termination of current process >",
finish = "Project Building Finished. ",
command, currentDir, fileName;

Build( String currentDir, File project, int index ) {


this.currentDir = currentDir;
this.errorList = new ArrayList();
this.project = project;
this.index = index;
this.currentDir = project.getPath();
}

public void run() {


this.console =SDJEditor.frmMain.getConsole();
this.myUID = (TextAreaUI)console.getUI();

console.append( stop + "\n" );


console.append( "Building Project of Directory: " + currentDir + "\n\n" );

try {

File files[] = project.listFiles();


startTime = System.currentTimeMillis();
for ( int i = 0; i < files.length; i++ ) {

file = (File)files[i];
if ( file.isDirectory() || !file.getName().endsWith(".java") )
continue;

fileName = file.getName();
command = "javac " + fileName;

lock = true;

Runtime rt = Runtime.getRuntime();
SDJEditor.frmMain.commandList.add(rt);
Process p = rt.exec( command, null, project );
SDJEditor.frmMain.commandList.add(p);
BufferedReader reader1 = new BufferedReader(
new InputStreamReader( p.getInputStream() ) );

BufferedReader reader2 = new BufferedReader(


new InputStreamReader( p.getErrorStream() ) );

console.append( ">> " + command + "\n" );


console.setCaretPosition(console.getText().length());

Thread t1 = new Thread( new Print( reader1, fileName ) );


Thread t2 = new Thread( new Print(reader2, fileName ) );
t1.start();
t2.start();

while ( execute ) {
if ( !lock )
break;
else {
try {
Thread.sleep(500);
}
catch (InterruptedException ie ) {}
}
}

p.destroy();
reader1.close();
reader2.close();

if ( !execute )
break;

console.append("\n========================================================");
console.append( "\n" );

} // for

if ( execute ) {
report();
console.append( "\n" + finish +" \n" );
} else {
console.append( "\n" + "The Process has been Terminated !!! \n" );
}

console.append("\n---------------------------------------------------------------------------------------------------\n");
console.setCaretPosition(console.getText().length()); // Alternative
method scrollBar.setValue(scrollBar.getMaximum());

}
catch(Exception e) {
JOptionPane.showMessageDialog( SDJEditor.frmMain,
"Error: Invalid Command!!!", "Warning", JOptionPane.ERROR_MESSAGE);
}

Toolkit.getDefaultToolkit().beep();
} // End run()

public void terminate() {


execute = false;
}

private void report() {


if ( errorList.size() != 0 ) {
console.append( "Following files failed to compile:\n" );
for ( int i = 0; i < errorList.size(); i++ )
console.append( (String)errorList.get(i) + "\n" );
}
else {
console.append( "All files within project directory compiled successfully. \n" );
}
endTime = System.currentTimeMillis() - startTime;
long lg = endTime/1000;
String time = "Time taken: " + lg + "." + (endTime-lg*1000) + " seconds.";
console.append( time + "\n" );
}

class Print implements Runnable {


BufferedReader reader;
String fileName;
int ind;
Print( BufferedReader reader, String fileName ) {
this.reader = reader;
this.fileName = fileName;
}
public void run() {
int i = 0;
try {
String msg;
while ( (msg = reader.readLine()) != null) {
i++;
console.append( msg + "\n" );
console.setCaretPosition(console.getText().length());
int line = console.getLineCount()-1;
if ( msg.indexOf( fileName ) != -1 )
myUID.add( line );
}
lock = false;
} catch ( IOException e ) {
e.printStackTrace();
}
if ( i != 0 )
errorList.add( fileName );
}
}

RUN:
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.awt.*;
import java.util.*;

class Run extends Thread {

private int option, index;


private JTextArea console;
private Boolean Locks[];
private TextAreaUI myUID;
private boolean compilingSuccess = false;
private volatile boolean execute = true;
private long startTime = -1,
endTime = -1;

private String compile = "Process compiling java class started... ",


application = "Process running java applicaiton started... ",
applet = "Process running java applet started... ",
stop = "< Press the Stop Process MenuItem " +
"termination of current process >",
finish = "Process returned.",
commandDialog = "Process running command from command dialog... ",

command, currentDir;
private final String fileName;

public Process proces;


Run( String command, String currentDir, String fileName, int option, int index) {
this.command = command;
this.currentDir = currentDir;
this.fileName = fileName;

this.option = option;
this.index = index;
}

public void run() {

this.console =SDJEditor.frmMain.getConsole();
this.myUID = (TextAreaUI)console.getUI();
this.Locks = new Boolean[2];
Locks[0] = new Boolean(true);
Locks[1] = new Boolean(true);
try {

Runtime rt = Runtime.getRuntime();

startTime = System.currentTimeMillis();
SDJEditor.frmMain.commandList.add(rt);
Process p = rt.exec( command, null, new File( currentDir ) );
SDJEditor.frmMain.commandList.add(p);

BufferedReader reader1 = new BufferedReader(


new InputStreamReader( p.getInputStream() ) );

BufferedReader reader2 = new BufferedReader(


new InputStreamReader( p.getErrorStream() ) );

console.append( stop + "\n" );


console.append(">> " + command + "\n" );
if ( option == 1 )
console.append( compile );
else if ( option == 2 )
console.append( application );
else if ( option == 3 )
console.append( applet );
else if ( option == 4 )
console.append( commandDialog );

console.append( "\n\n" );
console.setCaretPosition(console.getText().length());

Thread t1 = new Thread( new Print(reader1,0) );


Thread t2 = new Thread( new Print(reader2,1) );
t1.start();
t2.start();

while ( execute ) {
if ( Locks[0].equals( Boolean.FALSE ) &&
Locks[1].equals( Boolean.FALSE ) )
break;
else {
try {
Thread.sleep(500);
}
catch (InterruptedException ie ) {}
}
}

reader1.close();
reader2.close();
endTime = System.currentTimeMillis() - startTime;
long lg = endTime/1000;
String time = "\n Time taken: " + lg + "." + (endTime-lg*1000) + "
seconds.";
if ( execute ) {

if ( option == 1 ) {
if ( compilingSuccess )
console.append( "Compiling Java Code
Success. " );
else
console.append( "\n" + "Compiling Java
Code Failed!!! ");
console.append( time + "\n" );
}

if ( option != 1 )
finish = "\n" + finish+"\n";
//console.append( finish );

} else {

console.append( "The Process has been Terminated !!!" );

console.append("\n\n---------------------------------------------------------------------------------------------------\n");
console.setCaretPosition(console.getText().length()); // Alternative
method scrollBar.setValue(scrollBar.getMaximum());
p.destroy();

}
catch(Exception e) {
JOptionPane.showMessageDialog( SDJEditor.frmMain,
"Error: Invalid Command!!!", "Warning", JOptionPane.ERROR_MESSAGE);
}

//editor.updateCommandPanel( list, index ); // Call back method in MyEditor


Toolkit.getDefaultToolkit().beep();

} // End run()

public void terminate() {


execute = false;
}

class Print implements Runnable {


BufferedReader reader;
int ind;
Print( BufferedReader reader, int ind ) {
this.reader = reader;
this.ind = ind;
}
public void run() {
int i = 0;
try {
String msg;
while ( (msg = reader.readLine()) != null ) {
i++;
console.append( msg + "\n" );
console.setCaretPosition(console.getText().length());
final int line = console.getLineCount()-1;
final String str = msg;
// Has to use Runnable thread or else myUID.add() called
// only in the end
SwingUtilities.invokeLater( new Runnable() {
public void run() {
if ( str.indexOf( fileName ) != -1 ) {
myUID.add( line-1 );
}
}
});
} // while
Locks[ind] = new Boolean(false);
} catch ( IOException e ) {
e.printStackTrace();
}
if ( i == 0 && ind == 1 )
compilingSuccess = true;
}
}

}
SYNTAX HIGHLIGHTING:
import java.awt.*;
import java.awt.event.*;
import java.awt.Toolkit.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;

class SyntaxDocument extends DefaultStyledDocument{


private DefaultStyledDocument doc;
private Element rootElement;
private boolean multiLineComment;
private MutableAttributeSet normal;
private MutableAttributeSet keyword;
private MutableAttributeSet comment;
private MutableAttributeSet quote;
private MutableAttributeSet javaClass;
private Hashtable keywords;
private Hashtable javaClasses;
private boolean started;

public SyntaxDocument(){
doc = this;
rootElement = doc.getDefaultRootElement();
putProperty( DefaultEditorKit.EndOfLineStringProperty, "\n" );
normal = new SimpleAttributeSet();
StyleConstants.setForeground(normal, Color.black);
comment = new SimpleAttributeSet();
Color green = new Color(0, 120, 0);
StyleConstants.setForeground(comment, green);
keyword = new SimpleAttributeSet();
Color blue = new Color(0, 0, 140);
StyleConstants.setForeground(keyword, blue);
StyleConstants.setBold(keyword, true);
quote = new SimpleAttributeSet();
Color red = new Color(140,0,0);
StyleConstants.setForeground(quote, red);
Object dummyObject = new Object();

javaClass = new SimpleAttributeSet();


// Color cl=new Color();
StyleConstants.setForeground(javaClass, Color.DARK_GRAY);
StyleConstants.setBold(javaClass, true);

keywords = new Hashtable();


javaClasses=new Hashtable();
keywords.put( "abstract", dummyObject );
keywords.put( "boolean", dummyObject );
keywords.put( "break", dummyObject );
keywords.put( "byte", dummyObject );
keywords.put( "case", dummyObject );
keywords.put( "catch", dummyObject );
keywords.put( "char", dummyObject );
keywords.put( "class", dummyObject );
keywords.put( "continue", dummyObject );
keywords.put( "default", dummyObject );
keywords.put( "do", dummyObject );
keywords.put( "double", dummyObject );
keywords.put( "else", dummyObject );
keywords.put( "extends", dummyObject );
keywords.put( "enum", dummyObject );
keywords.put( "false", dummyObject ); // string literal
keywords.put( "final", dummyObject );
keywords.put( "finally", dummyObject );
keywords.put( "float", dummyObject );
keywords.put( "for", dummyObject );
keywords.put( "goto", dummyObject ); // reserved word
keywords.put( "if", dummyObject );
keywords.put( "implements", dummyObject );
keywords.put( "import", dummyObject );
keywords.put( "instanceof", dummyObject );
keywords.put( "int", dummyObject );
keywords.put( "interface", dummyObject );
keywords.put( "long", dummyObject );
keywords.put( "native", dummyObject );
keywords.put( "new", dummyObject );
keywords.put( "null", dummyObject ); // string literal
keywords.put( "package", dummyObject );
keywords.put( "private", dummyObject );
keywords.put( "protected", dummyObject );
keywords.put( "public", dummyObject );
keywords.put( "return", dummyObject );
keywords.put( "short", dummyObject );
keywords.put( "static", dummyObject );
keywords.put( "strictfp", dummyObject );
keywords.put( "String", dummyObject ); // newbie word
keywords.put( "super", dummyObject );
keywords.put( "System", dummyObject ); // newbie word
keywords.put( "switch", dummyObject );
keywords.put( "synchronized", dummyObject );
keywords.put( "this", dummyObject );
keywords.put( "throw", dummyObject );
keywords.put( "throws", dummyObject );
keywords.put( "transient", dummyObject );
keywords.put( "true", dummyObject );
keywords.put( "try", dummyObject );
keywords.put( "void", dummyObject );
keywords.put( "volatile", dummyObject );
keywords.put( "while", dummyObject );

javaClasses.put("ASCIICaseInsensitiveComparator",dummyObject );
javaClasses.put("ATR",dummyObject );
javaClasses.put("AWTAutoShutdown",dummyObject );
javaClasses.put("AWTCharset",dummyObject );
javaClasses.put("AWTError",dummyObject );
javaClasses.put("AWTEvent",dummyObject );
javaClasses.put("AWTEventMulticaster",dummyObject );
javaClasses.put("AWTException",dummyObject );
javaClasses.put("AWTKeyStroke",dummyObject );
javaClasses.put("AWTPermission",dummyObject );
javaClasses.put("AWTSecurityManager",dummyObject );
javaClasses.put("AbstractAction",dummyObject );
javaClasses.put("AbstractButton",dummyObject );
javaClasses.put("AbstractButtonBeanInfo",dummyObject
);
javaClasses.put("AbstractCellEditor",dummyObject );
javaClasses.put("AbstractCollection",dummyObject );
javaClasses.put("AbstractList",dummyObject );
javaClasses.put("AbstractListModel",dummyObject );
javaClasses.put("AbstractMap",dummyObject );
javaClasses.put("AbstractMethodError",dummyObject );
javaClasses.put("AbstractQueue",dummyObject );
javaClasses.put("AbstractScriptEngine",dummyObject );
javaClasses.put("AbstractSequentialList",dummyObject );
javaClasses.put("AbstractSet",dummyObject );

javaClasses.put("AbstractSpinnerModel",dummyObject );
javaClasses.put("AbstractStringBuilder",dummyObject );
javaClasses.put("AccessControlContext",dummyObject );

javaClasses.put("AccessControlException",dummyObject );
javaClasses.put("AccessController",dummyObject );
javaClasses.put("AccessException",dummyObject );
javaClasses.put("Accessible",dummyObject );
javaClasses.put("AccessibleAction",dummyObject );

javaClasses.put("AccessibleAttributeSequence",dummyObject );
javaClasses.put("AccessibleBundle",dummyObject );
javaClasses.put("AccessibleComponent",dummyObject );
javaClasses.put("AccessibleContext",dummyObject );

javaClasses.put("AccessibleEditableText",dummyObject );

javaClasses.put("AccessibleExtendedComponent",dummyObject );

javaClasses.put("AccessibleExtendedTable",dummyObject );

javaClasses.put("AccessibleExtendedText",dummyObject );
javaClasses.put("AccessibleHyperlink",dummyObject );
javaClasses.put("AccessibleHypertext",dummyObject );
javaClasses.put("AccessibleIcon",dummyObject );
javaClasses.put("AccessibleKeyBinding",dummyObject );
javaClasses.put("AccessibleMethod",dummyObject );
javaClasses.put("AccessibleRelation",dummyObject );
javaClasses.put("AccessibleRelationSet",dummyObject );

javaClasses.put("AccessibleResourceBundle",dummyObject );
javaClasses.put("AccessibleRole",dummyObject );
public void insertString(int offset, String str, AttributeSet a) throws BadLocationException{
if (str.equals("{"))
str = addMatchingBrace(offset);
super.insertString(offset, str, a);
processChangedLines(offset, str.length());
}
/* Override to apply syntax highlighting after the document has been updated */
public void remove(int offset, int length) throws BadLocationException{
super.remove(offset, length);
processChangedLines(offset, 0);
}
/* Determine how many lines have been changed, then apply highlighting to each line */
private void processChangedLines(int offset, int length) throws BadLocationException {
String content = doc.getText(0, doc.getLength());
// The lines affected by the latest document update
int startLine = rootElement.getElementIndex( offset );
int endLine = rootElement.getElementIndex( offset + length );
// Make sure all comment lines prior to the start line are commented
// and determine if the start line is still in a multi line comment
setMultiLineComment( commentLinesBefore( content, startLine ) );
// Do the actual highlighting
for (int i=startLine; i <=endLine; i++)applyHighlighting(content, i);
// Resolve highlighting to the next end multi line delimiter
if (isMultiLineComment()) commentLinesAfter(content, endLine);
else highlightLinesAfter(content, endLine);
}
/* Highlight lines when a multi line comment is still 'open' */
private boolean commentLinesBefore(String content, int line){
int offset = rootElement.getElement( line ).getStartOffset();
// Start of comment not found, nothing to do
int startDelimiter = lastIndexOf( content, getStartDelimiter(), offset-2);
if (startDelimiter < 0)return false;
// Matching start/end of comment found, nothing to do
int endDelimiter = indexOf( content, getEndDelimiter(), startDelimiter );
if (endDelimiter < offset & endDelimiter != -1)return false;
// End of comment not found, highlight the lines
doc.setCharacterAttributes(startDelimiter, offset - startDelimiter + 1, comment, false);
return true;
}
/* Highlight comment lines to matching end delimiter */
private void commentLinesAfter(String content, int line){
int offset = rootElement.getElement( line ).getEndOffset();
// End of comment not found, nothing to do
int endDelimiter = indexOf( content, getEndDelimiter(), offset );
if (endDelimiter < 0) return;
// Matching start/end of comment found, comment the lines
int startDelimiter = lastIndexOf( content, getStartDelimiter(), endDelimiter );
if (startDelimiter < 0 || startDelimiter <= offset){
doc.setCharacterAttributes(offset, endDelimiter - offset + 1, comment, false);
}
}
/* Highlight lines to start or end delimiter */
private void highlightLinesAfter(String content, int line) throws BadLocationException{
int offset = rootElement.getElement( line ).getEndOffset();
// Start/End delimiter not found, nothing to do
int startDelimiter = indexOf( content, getStartDelimiter(), offset );
int endDelimiter = indexOf( content, getEndDelimiter(), offset );
if (startDelimiter < 0) startDelimiter = content.length();
if (endDelimiter < 0)endDelimiter = content.length();
int delimiter = Math.min(startDelimiter, endDelimiter);
if (delimiter < offset)return;
// Start/End delimiter found, reapply highlighting
int endLine = rootElement.getElementIndex( delimiter );
for (int i = line + 1; i < endLine; i++){
Element branch = rootElement.getElement( i );
Element leaf = doc.getCharacterElement( branch.getStartOffset() );
AttributeSet as = leaf.getAttributes();
if ( as.isEqual(comment) ) applyHighlighting(content, i);
}
}
/* Parse the line to determine the appropriate highlighting */
private void applyHighlighting(String content, int line) throws BadLocationException{
int startOffset = rootElement.getElement( line ).getStartOffset();
int endOffset = rootElement.getElement( line ).getEndOffset() - 1;
int lineLength = endOffset - startOffset;
int contentLength = content.length();
if (endOffset >= contentLength)endOffset = contentLength - 1;
// check for multi line comments
// (always set the comment attribute for the entire line)
if( ( endingMultiLineComment(content, startOffset, endOffset) )
||( isMultiLineComment() )
||( startingMultiLineComment(content, startOffset, endOffset) ) ){
doc.setCharacterAttributes(startOffset, endOffset - startOffset + 1, comment, false);
return;
}
// set normal attributes for the line
doc.setCharacterAttributes(startOffset, lineLength, normal, true);
// check for single line comment
int index = content.indexOf(getSingleLineDelimiter(), startOffset);
if ( (index > -1) && (index < endOffset) ){
doc.setCharacterAttributes(index, endOffset - index + 1, comment, false);
endOffset = index - 1;
}
// check for tokens
checkForTokens(content, startOffset, endOffset);
}
/* This line contain the start delimiter */
private boolean startingMultiLineComment(String content, int startOffset, int endOffset) throws
BadLocationException{
int index = indexOf( content, getStartDelimiter(), startOffset );
if( (index < 0) || (index > endOffset) ) return false;
else{
setMultiLineComment( true );
return true;
}
}
/* Does this line contain the end delimiter */
private boolean endingMultiLineComment(String content, int startOffset, int endOffset) throws
BadLocationException{
int index = indexOf( content, getEndDelimiter(), startOffset );
if( (index < 0) || (index > endOffset) ) return false;
else{
setMultiLineComment( false );
return true;
}
}
/* We have found a start delimiter and are still searching for the end delimiter */
private boolean isMultiLineComment(){
return multiLineComment;
}
private void setMultiLineComment(boolean value){
multiLineComment = value;
}

private void checkForTokens(String content, int startOffset, int endOffset){


while (startOffset <= endOffset){
// skip the delimiters to find the start of a new token
while (isDelimiter(content.substring(startOffset, startOffset+1))){
if(startOffset < endOffset) startOffset++;
else return;
}
// Extract and process the entire token
if (isQuoteDelimiter( content.substring(startOffset, startOffset + 1)))
startOffset = getQuoteToken(content, startOffset, endOffset);
else startOffset = getOtherToken(content, startOffset, endOffset);
}
}
private int getQuoteToken(String content, int startOffset, int endOffset){
String quoteDelimiter = content.substring(startOffset, startOffset + 1);
String escapeString = getEscapeString(quoteDelimiter);
int index;
int endOfQuote = startOffset;
// skip over the escape quotes in this quote
index = content.indexOf(escapeString, endOfQuote + 1);
while ( (index > -1) && (index < endOffset) ){
endOfQuote = index + 1;
index = content.indexOf(escapeString, endOfQuote);
}
// now find the matching delimiter
index = content.indexOf(quoteDelimiter, endOfQuote + 1);
if( (index < 0) || (index > endOffset) ) endOfQuote = endOffset;
else endOfQuote = index;
doc.setCharacterAttributes(startOffset, endOfQuote-startOffset+1, quote, false);
return endOfQuote + 1;
}

private int getOtherToken(String content, int startOffset, int endOffset){


int endOfToken = startOffset + 1;
while(endOfToken <= endOffset ){
if(isDelimiter(content.substring(endOfToken, endOfToken+1))) break;
endOfToken++;
}
String token = content.substring(startOffset, endOfToken);
if( isKeyword( token ) )
doc.setCharacterAttributes(startOffset, endOfToken-startOffset, keyword, false);
if( isJavaClasses( token ) )
doc.setCharacterAttributes(startOffset, endOfToken-startOffset, javaClass, false);

return endOfToken + 1;
}
private int indexOf(String content, String needle, int offset){
int index;
while( (index = content.indexOf(needle, offset)) != -1 ){
String text = getLine( content, index ).trim();
if(text.startsWith(needle) || text.endsWith(needle)) break;
else offset = index + 1;
}
return index;
}

private int lastIndexOf(String content, String needle, int offset){


int index;
while ( (index = content.lastIndexOf(needle, offset)) != -1 ){
String text = getLine( content, index ).trim();
if( (text.startsWith(needle)) || (text.endsWith(needle)) ) break;
else offset = index - 1;
}
return index;
}

private String getLine(String content, int offset){


int line = rootElement.getElementIndex( offset );
Element lineElement = rootElement.getElement( line );
int start = lineElement.getStartOffset();
int end = lineElement.getEndOffset();
return content.substring(start, end - 1);
}

protected boolean isDelimiter(String character){


String operands = ";:{}()[]+-/%<=>!&|^~*.,\\\n";
if (Character.isWhitespace( character.charAt(0) ) ||
operands.indexOf(character)!= -1 ) return true;
else return false;
}

protected boolean isQuoteDelimiter(String character){


String quoteDelimiters = "\"'";
if (quoteDelimiters.indexOf(character) < 0) return false;
else return true;
}

protected boolean isKeyword(String token){


Object o = keywords.get( token );
return o == null ? false : true;
} protected boolean isJavaClasses(String token){
Object o = javaClasses.get( token );
return o == null ? false : true;
}
protected String getStartDelimiter(){
return "/*";
}
protected String getEndDelimiter(){
return "*/";
}
protected String getSingleLineDelimiter(){
return "//";
}

protected String getEscapeString(String quoteDelimiter){


return "\\" + quoteDelimiter;
}
protected String addMatchingBrace(int offset) throws BadLocationException{
StringBuffer whiteSpace = new StringBuffer();
int line = rootElement.getElementIndex( offset );
int i = rootElement.getElement(line).getStartOffset();
while(true){
String temp = doc.getText(i, 1);
if (temp.equals(" ") || temp.equals("\t")){
whiteSpace.append(temp);
i++;
}
else break;
} return "{\n" + whiteSpace.toString() + whiteSpace.toString()+"\n"+ whiteSpace.toString()+ "}";}}
SNAPSHOTS:
FUTURE ENCHANCEMENTS:
Considering the future enhancement of this tool, it could be
done to work fine for J2E programming language.

CONCLUSION:
The project work entitled “JAVA EDITOR” was initiated to work in a
more efficient and personal way. The main trademark of the project is many powerful features to
improve your productivity and programming experience. Ideal for both the beginner and more
experienced programmer.

Você também pode gostar