Anda di halaman 1dari 45

JAVA SOURCE CODE EDITOR

A PROJECT REPORT Submitted by

Chandra Shekhar Sharma Sovit Tyagi Abhinav Khandelwal Anshul Goyal


in partial fulfillment for the award of the degree of

BACHELOR OF ENGINEERING
IN

COMPUTER ENGINERING VIVEKANANDA INSTITUTE OF TECHNOLOGY JAIPUR

RAJASTHAN TECHNICAL UNIVERSITY, KOTA


November - 2011

BONAFIED CERTIFICATE

Certified that this project report JAVA SOURCE CODE EDITOR is the bonafide work of Chandra Shekhar Sharma, Abhinav Khandelwal, Sovit Tyagi, Anshul Goyal who carried out the project work under my supervision.

SIGNATURE

SIGNATURE

Dr. Baldev Singh HEAD OF THE DEPARTMENT Computer Science VIT Campus Sisyawas, NRI Road Jagatpura Jaipur

Mr. Pawan Patidar SUPERVISIOR Computer Science VIT Campus Sisyawas, NRI Road Jagatpura Jaipur

TABLE OF CONTENTS
CHAPTER TITLE
ABSTRACT LIST OF FIGURE LIST OF ABBREVATIONS 1. INTORDUCTION 1.1 OBJECTIVE 1.2 TOOLS AND TECHNOLOGY 1.2.1 JAVA STANDARD EDITION 1.7 1.2.2 JAVA SWING AND AWT PACAKGE 1.3 NEED OF SOURCE CODE EDITOR 1.4 PROJECT REQUIREMENTS SYSTEM DESIGN SPECIFICATION 2.1 ENTITY RELATIONSHIP DIAGRAM 2.2 DATA FLOW DIAGRAM 2.3 USE CASE DIAGRAM 2.4 CLASS DIAGRAM PRODUCT SPECIFICATIONS 3.1 BASIC FEATURES 3.1.1 FILE OPERATIONS 3.1.2 EDIT OPERATIONS 3.1.3 SEARCH OPERATIONS 3.1.4 VIEW OPERATIONS 3.2 ADVANCE FEATURES SYNTAX COLORING AND HIGHLIGHTING 4.1 STRATEGIES 4.1.1 CHANGE INSERTSTRING 4.1.2 ADDING STRING SUPPORT 4.1.3 ADDING NUMBER SUPPORT 4.1.4 ADDING COMMENT SUPPORT 4.1.5 PUTTING IT ALLTOGETHER SNAPSHOTS OF PROJECT CONCLUSION BIBILIOGRAPHY

PAGE NO.

2.

3.

4.

5. 6. 7.

ABSTRACT

JFox (Java Source Code Editor) is a source code editor and notepad replacement. It supports Java programming language and provides lot of exclusive features for user convenience. JFox is a powerful editor targeted towards programmers and to focus on dynamic implementation of a source code editor. JFox is completely written in Java programming language so it provides better UI (User Interface) and programming facilities. JFox provides all basic editor features (i.e. cut, copy, paste, undo, redo) as well as advanced features like syntax highlighting and line numbers while writing. It successfully runs on any operating system with a Java 2 version 1.3 or higher virtual machine - this includes MacOS X, OS/2, Unix, VMS and Windows. JFox provides quite effective "Search and replace" method, which is capable of searching both exact words and patterns. This is more than a editor as it provides compile and run features for Java programmers.

LIST OF ABBREVATIONS
JDK = Java Development kit GUI = Graphics user interface JVM = JAVA VIRTUAL MACHINE JFC = JAVA FOUNDATION CLASSES AWT = ABSTRACT WINDOWS TOOLKIT GPL = General public license DFD = DATA FLOW DIAGRAM ER = ENTITY RELATIONSHIP JavaCC = Java Compiler Compiler JIT = Just In Time I/O = Input - output

INTRODUCTION

Objective : To design a powerful and user friendly source code editor for Java programmers
which will be capable of editing Java source code as well compile and run this code. Speed and robustness should be the key factors.

Tools and Technology:

J2SE (Java Standard Edition 1.7)


Java is a programming language originally developed by James Gosling at Sun Microsystems (which has since merged into 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 bytecode (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, particularly for client-server 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 and GNU Classpath. The syntax of Java is largely derived from C++. Unlike C++, which combines the syntax for structured, generic, and object-oriented programming, Java was built almost exclusively as an object-oriented language. All code is written inside a class, and everything is an object, with the exception of the primitive data types (integers, floating-point numbers, boolean values, and characters),wh classes for performance reasons. Unlike C++, Java does not support operator overloading or multiple inheritance for classes. This simplifies the language and aids in preventing potential errors and anti-pattern design. Java uses similar commenting methods to C++. There are three different styles of comments: a single line style marked with two slashes (//), a multiple line style opened with /* and closed with */, and the Javadoc commenting style opened with /** and closed with */. The Javadoc style of commenting allows the user to run the Javadoc executable to compile documentation for the program.

Java Swing and AWT package:


Swing is the primary Java GUI widget toolkit. It is part of Oracle's Java Foundation Classes (JFC) an API for providing a graphical user interface (GUI) for Java programs. Swing was developed to provide a more sophisticated set of GUI components than the earlier Abstract Window Toolkit. Swing provides a native look and feel that emulates the look and feel of several platforms, and also supports a pluggable look and feel that allows applications to have a look and feel unrelated to the underlying platform. It has more powerful and flexible components than AWT. In addition to familiar components such as buttons, check box and labels, Swing provides several advanced components such as tabbed panel, scroll panes, trees, tables and lists.Unlike AWT components, Swing components are not implemented by platformspecific code. Instead they are written entirely in Java and therefore are platform-independent. The term "lightweight" is used to describe such an element. Swing is a highly partitioned architecture, which allows for the "plugging" of various custom implementations of specified framework interfaces: Users can provide their own custom implementation(s) of these components to override the default implementations.

The Abstract Window Toolkit (AWT) is Java's original platform-independent windowing, graphics, and user-interface widget toolkit. The AWT is now part of the Java Foundation Classes (JFC) the standard API for providing a graphical user interface (GUI) for a Java program.AWT is also the GUI toolkit for a number of Java ME profiles. For example, Connected Device Configuration profiles require Java runtimes on mobile telephones to support AWT

Needs of Source Code Editor:


Apart of the ordinary notepad users, There is an another group of people who actually work with other important side of notepad editor, they are computer programmers. Computer programmers need to have a powerful programming interface where they should be capable of writing code with ease of access and convenience. So there is always a requirement of good source code editor which provide reliable, fast, powerful, and multi featured user interface i.e. syntax coloring (highlighting), source code parsing, and line numbers of code searching. There is few other thing which a programmer may want in source code editor, which is compiling and run facility. So Jfox is a source code editor which is having all those requirements. JFox is basically designed for those programmers which works on Java technology, as it provide syntax highlighting for Java programming language.

Project Requirements: Software Requirements Hardware Requirements

SYSTEM DESIGN DIAGRAMS

Entity-Relationship (ER) Diagrams

In software engineering, an entity-relationship model (ERM) is an abstract and conceptual representation of data. Entity-relationship modeling is a database modeling method, used to produce a type of conceptual schema or semantic data model of a system, often a relational database, and its requirements in a top-down fashion. Diagrams created by this process are called entity-relationship diagrams, ER diagrams, or ERDs.
y The building blocks: entities, relationships, and attributes:

An entity may be defined as a thing which is recognized as being capable of an independent existence and which can be uniquely identified. An entity is an abstraction from the complexities of some domain. When we speak of an entity we normally speak of some aspect of the real world which can be distinguished from other aspects of the real world. A relationship captures how two or more entities are related to one another. Relationships can be thought of as verbs, linking two or more nouns. Examples: an owns relationship between a company and a computer, a supervises relationship between an employee and a department, a performs

relationship between an artist and a song, a proved relationship between a mathematician and a theorem. Entities and relationships can both have attributes.

Diagramming conventions:

Entity sets are drawn as rectangles, relationship sets as diamonds. If an entity set participates in a relationship set, they are connected with a line. Attributes are drawn as ovals and are connected with a line to exactly one entity or relationship set. An entity may be a physical object such as a house or a car, an event such as a house sale or a car service, or a concept such as a customer transaction or order. Although the term entity is the one most commonly used, following Chen we should really distinguish between an entity and an entity-type. An entity-type is a category. An entity, strictly speaking, is an instance of a given entity-type. There are usually many instances of an entity-type. Because the term entity-type is somewhat cumbersome, most people tend to use the term entity as a synonym for this term. Entities can be thought of as nouns. Examples: a computer, an employee, a song, a mathematical theorem. Entity-relationship diagrams don't show single entities or single instances of relations. Rather, they show entity sets and relationship sets. Example: a particular song is an entity. The collection of all songs in a database is an entity set. The eaten relationship between a child and her lunch is a single relationship. The set of all such child-lunch relationships in a database is a relationship set.

E R Diagram for JFox Editor


Figure 1

Data flow diagram:

A data-flow diagram (DFD) is a graphical representation of the "flow" of data through an information system. DFDs can also be used for the visualization of data processing (structured design). On a DFD, data items flow from an external data source or an internal data store to an internal data store or an external data sink, via an internal process. A DFD provides no information about the timing of processes, or about whether processes will operate in sequence or in parallel. It is therefore quite different from a flowchart, which shows the flow of control through an algorithm, allowing a reader to determine what operations will be performed, in what order, and under what circumstances, but not what kinds of data will be input to and output from the system, nor where the data will come from and go to, nor where the data will be stored (all of which are shown on a DFD). The Level 1 DFD shows how the system is divided into sub-systems (processes), each of which deals with one or more of the data flows to or from an external agent, and which together provide all of the functionality of the system as a whole. It also identifies internal data stores that must be present in order for the system to do its job, and shows the flow of data between the various parts of the system. Data-flow diagrams were invented by Larry Constantine, the original developer of structured design, based on Martin and Estrin's "data-flow graph" model of computation. Data-flow diagrams (DFDs) are one of the three essential perspectives of the structured-systems analysis and design method SSADM. The sponsor of a project and the end users will need to be briefed and consulted throughout all stages of a system's evolution. With a data-flow diagram, users are able to visualize how the system will operate, what the system will accomplish, and how the system will be implemented. The old system's dataflow diagrams can be drawn up and

compared with the new system's data-flow diagrams to draw comparisons to implement a more efficient system. Data-flow diagrams can be used to provide the end user with a physical idea of where the data they input ultimately has an effect upon the structure of the whole system from order to dispatch to report. How any system is developed can be determined through a data-flow diagram. In the course of developing a set of leveled data-flow diagrams the analyst/designers is forced to address how the system may be decomposed into component sub-systems, and to identify the transaction data in the data model. There are different notations to draw data-flow diagrams, defining different visual representations for processes, data stores, data flow, and external entities. Data flow diagram ("bubble charts") are directed graphs in which the nodes specify processing activities and the arcs specify data items transmitted between processing nodes. DFD is also a virtually designable diagram that technically or diagrammatically describes the inflow and outflow of data or information that is provided by the external entity.

Level 0 DFD:
 Level 0 and level 1 are probably the easiest for someone to create, because they are done at such a high level view.  Shows the systems major processes, data flows, and data stores at a high level of abstraction.  Level 0 DFD shows how the system is divided into sub-systems(processes).  It also identifies internal data stores that must be present in order for the system to do its job.  Shows the flow of data between the various parts of the system.  When the Context Diagram is expanded into DFDlevel-0, all the connections that flow into and out of process 0 needs to be retained.

0 Level DFD
Figure 2

Level 1 DFD: The next stage is to create the Level 1 Data Flow Diagram. This highlights the main functions carried out by the system. As a rule, we try to describe the system using between two and seven functions - two being a simple system and seven being a complicated system. This enables us to keep the model manageable on screen or paper.

Level 1 DFD
Figure 3

Use Case:
A use case diagram in the Unified Modeling Language (UML) is a type of behavioral diagram defined by and created from a Use-case analysis. Its purpose is to present a graphical overview of the functionality provided by a system in terms of actors, their goals (represented as use cases), and any dependencies between those use cases. Use cases are a software modeling technique that helps developers determine which features to implement and how to gracefully resolve errors. Within systems engineering, use cases are used at a higher level than within software engineering, often representing missions or stakeholder goals. The detailed requirements may then be captured in SysML requirement diagrams or similar mechanisms. The main purpose of a use case diagram is to show what system functions are performed for which actor.

Actors:
An actor is a person, organization, or external system that plays a role in one or more interactions with the system. The same person using the system may be represented as different actors because they are playing different roles. For example, user "Joe" could be playing the role of a Customer when using an Automated Teller Machine to withdraw cash, or playing the role of a Bank Teller when using the system to restock the cash drawer.

Types of Actors: Primary Actor - Fulfills the user goals by using the services of the SuD (System Under
Discussion). Ex: 'Cashier' in a Process Sale System.

Secondary Actor - Provides a service to the SuD. Ex: 'Automated payment authorization
service'.

Offstage Actor - Has an interest in the behavior of the system but is not primary or secondary.
Ex: A government tax agency.

Actor Generalization
One popular relationship between Actors is Generalization/Specialization. This is useful in defining overlapping roles between actors. The notation is a solid line ending in a hollow triangle drawn from the specialized to the more general actor.

Use Case Relationships


Four relationships among use cases are used often in practice.

Include
In one form of interaction, a given use case may include another. "Include is a Directed Relationship between two use cases, implying that the behavior of the included use case is inserted into the behavior of the including use case". The first use case often depends on the outcome of the included use case. This is useful for extracting truly common behaviors from multiple use cases into a single description. The notation is a dashed arrow from the including to the included use case, with the label "include". This usage resembles a macro expansion where the included use case behavior is placed inline in the base use case behavior. There are no parameters or return values. To specify the location in a flow of events in which the base use case includes the behavior of another, you simply write include followed by the name of use case you want to include.

Extend
In another form of interaction, a given use case (the extension) may extend another. The relationship indicates that the behavior of the extension use case may be inserted in the extended use case under some conditions. The notation is a dashed arrow from the extension to the extended use case, with the label "extend". The notes or constraints may be associated with this relationship to illustrate the conditions under which this behavior will be executed. Modelers use the extend relationship to indicate use cases that are "optional" to the base use case. Depending on the modeler's approach "optional" may mean "potentially not executed with the base use case" or it may mean "not required to achieve the base use case goal".

Generalization
In the third form of relationship among use cases, a generalization/specialization relationship exists. A given use case may have common behaviors, requirements, constraints, and assumptions with a more general use case. In this case, describe them once, and deal with it in the same way, describing any differences in the specialized cases. The notation is a solid line ending in a hollow triangle drawn from the specialized to the more general use case (following the standard generalization notation).

Associations
Associations between actors and use cases are indicated in use case diagrams by solid lines. An association exists whenever an actor is involved with an interaction described by a use case. Associations are modeled as lines connecting use cases and actors to one another, with an optional arrowhead on one end of the line. The arrowhead is often used to indicate the direction of the initial invocation of the relationship or to indicate the primary actor within the use case. The arrowheads imply control flow and should not be confused with data flow.

USE CASE DIAGRAM Figure 4

Class Diagram:
The class diagram is the main building block of object oriented modeling. It is used both for general conceptual modeling of the systematic of the application, and for detailed modeling translating the models into programming code. Class diagrams can also be used for data modeling. The classes in a class diagram represent both the main objects and or interactions in the application and the objects to be programmed. In the class diagram these classes are represented with boxes which contain three parts: .  The upper part holds the name of the class  The middle part contains the attributes of the class  The bottom part gives the methods or operations the class can take or undertake

In the system design of a system, a number of classes are identified and grouped together in a class diagram which helps to determine the static relations between those objects. With detailed modeling, the classes of the conceptual design are often split into a number of subclasses. In order to further describe the behavior of systems, these class diagrams can be complemented by state diagram or UML state machine. Also instead of class diagrams Object role modeling can be used if you just want to model the classes and their relationships.

Visibility:
To specify the visibility of a class member (i.e., any attribute or method) there are the following notations that must be placed before the member's name

+ Public Private # Protected ~ Package / Derived Underline Static

Some important concepts in class diagram are given below:


y Association:
An association represents a family of links. Binary associations (with two ends) are normally represented as a line, with each end connected to a class box.

y Aggregation:
Aggregation is a variant of the "has a" or association relationship; aggregation is more specific than association. It is an association that represents a part-whole or part-of relationship.

y Composition:
Composition usually has a strong life cycle dependency between instances of the container class and instances of the contained class(es): If the container is destroyed, normally every instance that it contains is destroyed as well.

y Generalization:
The Generalization relationship ("is a") indicates that one of the two related classes (the subclass) is considered to be a specialized form of the other (the super type) and super class is considered as 'Generalization' of subclass.

Class Diagram
Figure 5

PRODUCT SPECIFICATIONS
JFox Source Code Editor is having number of interactive features which are mentioned here respectively

Basic Features:
1. File Operations:
JFox Source Code Editor supports a number of file operations, which allows a user to write code with convenience. Some of these operations are given here :

(a) Copy: Copying the content of document being written in the editor.
(b) Paste: Putting the content in text area which has been previously copied or cut by the user.

(c) New: Opening a new file in text editor to edit or write code. (d) Print: To print the document currently being edited in the source code editor. (e) Rename: To rename the document currently being edited in the source code editor. (f) Open: To Open any document for editing or as a new file. (g) Close: To close the current document or all the document opened in source code
editor. (h) Save: To Save the current file permanently into the disk (with a valid name as per the operating system). (i) Save As: To Save the current file with different file name or as a duplicate copy of the current document.

2. Edit Operations:

(a) (b) (c) (d) (e) (f)

Undo: To revert the changes made just before last action (CTRL+Z) Redo: To get the previous state like it was before the UNDO operation (CTRL+Y) Convert Case: To convert the currently selected text in to UPPER Case or
LOWER case, as per the option being chosen

Select All: To Select each and every character of the text area in that editor. Set Read Only: To convert the text area into a non editable canvas/text display,
where code will not be allowed to change or delete.

Delete: To delete the selected text in that text area. this can be done via back space
key.

3. Search Operations:

(a) Search: To search any text or pattern in whole document this supports the exact key
value as well as text pattern.

(b) Replace: To replace any text or pattern in whole document this takes an input and
replace it by given string.

(c)Go to: This brings the focus/control on to any specific line number given as input to this.

4. View Operations:

(a) Themes: A number of themes are provided to user so he could change the User Interface and color scheme as per his choice. Mainly 4 user themes are provided , i.e. Classic, Advanced, Metal, Nimbus. (b) Zoom: To change the font size and view of the editor. it either increase the text size or decrease the text size in text editor. Zoom In is used to increase the text size and Zoom Out is used to decrease the text Size. (c) Word Wrap: It wraps the long lines into small ones. (d) Change Background: To change the background color of the text editor area. (e) Change Foreground: To change the Foreground color of the text editor area.

Advanced Features:
1. Execution of code: This consist of two major features which makes that editor
more useful. they are compiling, and run facilities.

Compile: In this state editor checks for the syntactical and Symantec of
the source code. It is connected to the Java Compiler which enables all the parsing and optimization features.

2. Run: After successful compilation of the source code it's sent to the java run
time environment and this enables the execution of byte code created by JavaCC (Java Compiler).

3. Line Number: When ever user is dealing with a huge file containing more than
1000 or more lines then seeking to desired line in editor is always very difficult. so JFox source code editor provides line numbering to make thing easier for programmer. These line numbers let the user know where exactly he is writing right now.

4. Help: Help set is provided to user to understand the Java manual and JFox
manual. It has basically 3 major options which are given here (i) JDK help: takes the user to java development Kit user manual , where he can find all the answer regarding java syntax and provided classes. (ii) Tip of the day: Gives any random tips about JFox editor or Java Development. (iii) About: Gives information about JFox developer team and their contact ways.

Syntax Coloring and Highlighting


Syntax highlighting is a feature of some text editors that display textespecially source code in different colors and fonts according to the category of terms. This feature eases writing in a structured language such as a programming language or a markup language as both structures and syntax errors are visually distinct. Highlighting does not affect the meaning of the text itself; it's made only for human readers/editors. Syntax highlighting is a form of secondary notation, since the highlights are not part of the text meaning, but serve to reinforce it. Some editors also integrate syntax highlighting with other features, such as spell checking or code folding, as aids to editing which are external to the language. Syntax highlighting is one strategy to improve the readability and context of the text; especially for code that spans several pages. The reader can easily ignore large sections of comments or code, depending on what one desires. Syntax highlighting also helps programmers find errors in their program. For example, most editors highlight string literals in a different color. Consequently, spotting a missing delimiter is much easier because of the contrasting color of the text. Brace matching is another important feature with many popular editors. This makes it simple to see if a brace has been left out or locate the match of the brace the cursor is on by highlighting the pair in a different color Most editors with syntax highlighting allow different colors and text styles to be given to dozens of different lexical sub-elements of syntax. These include keywords, comments, control-flow statements, variables, and other elements. Programmers often heavily customize their settings in an attempt to show as much useful information as possible without making the code difficult to read.

Strategies for Syntax highlighting in text Area:


To start things out let's try and use what we have so far. If we look at the code below we can see that plugging the Document we've created into a JTextPane component is quite easy. Defining the keywords is also very easy.

JTextPane editor = new JTextPane(); CodeDocument doc = new CodeDocument(); Vector keywords = new Vector(); keywords.addElement("abstract"); keywords.addElement("boolean"); ... doc.setKeywords(keywords); editor.setDocument(doc);

We could have just created the CodeDocument on the fly and passed it in as an argument to the constructor of the JTextPane class, but then the keywords for the Document wouldn't have been set and we'd have had to retrieve the Document and then add them.

Changing the insertString Method


Some of you may have noticed that the Document Model code we developed in the previous article worked only if you actually typed in the text manually. If we had called the insertString method and passed in a string with a length longer than one character, the syntax highlighting wouldn't have worked. To fix that we'll make a simple change, moving the code that actually did the comparing to another method, and changing the insertString method to support any size string. We'll create a method called processChar that will actually do the work of checking the kind of character we're dealing with and, in turn, call the correct method to handle that character.

To make the whole thing work, we can then just loop through each of the characters in the string that's passed into the insertString method, starting at the offset position (the offs variable passed into the insertString method) and ending at the offset plus the length of the string passed in. For each character we find we can call the processChar method, and voila! We can now handle any size string. To get an idea of the code, take a look below.

public void insertString(int offs, String str, AttributeSet a) throws BadLocationException{ super.insertString(offs, str, normal); int strLen = str.length(); int endpos = offs + strLen; int strpos; for (int i=offs;i<endpos;i++){ currentPos = i; strpos = i - offs; processChar(str.charAt(strpos)); } currentPos = offs; }

The only problem with the solution above is that when reading large chunks of text you may notice a slowdown in JTextPane's performance while it loads and parses all the text. A possible solution would be to use a thread to handle any text not immediately visible to the user, parse it in the background and then...well, that'll be the subject of a future article!

Adding String Support

Now that we can support entered text of any size, let's add highlighting support for strings. Adding string support isn't too hard now that we have the basic pieces in place. We'll add a variable that we'll use to keep track of whether we're entering a string. If this flag is turned on, any text we enter following the double quote ("") will be colored in a different foreground color. Also, if this flag is turned on, no keyword processing will occur. A carriage return will automatically shut the flag off. We'll need to keep track of the start position where the flag was first turned on. This will allow us to type in some text representing a string, hit the carriage return and type other stuff, and then put the caret back on the line where the string was and resume entering the string text all the while keeping the formats correct. We'll also need a variable that represents the style attribute we want for strings. In addition to variables we'll need some new methods. For strings we'll create a checkForString() method that will determine whether we're inside a string. If it finds that we are, the mode variable will be set appropriately (this will be explained in more detail a few paragraphs down). We'll also need a method called insertTextString() that will actually reinsert the properly formatted string. Adding Number Support Adding support for numbers is similar to adding support for strings. We'll add a variable to hold the style attribute for numbers, and also add two more methods: checkForNumber() and insertNumberString(). Like strings, the checkForNumber() method determines whether we're actually entering valid digits. If we are, it sets the CodeDocument's mode accordingly. Like insertTextString, insertNumberString inserts the properly formatted text as a number.

Adding Comment Support Comments are handled in a similar manner. Two new methods are needed, checkForComment() and insertCommentString(). Like numbers and text, the checkForComments method determines if a comment block has been started (it checks for the "/*" combination to start a comment and the "*/" to end a comment block). If it has, it changes the attributes of the entered text by calling the insertCommentString() method. Again, like numbers and strings, the insertCommentString() changes the formatting attri-butes of the string accordingly, and then inserts into the document. Another private variable is needed to hold the formatting attributes for comments. Putting It All Together Now it's time to get down and dirty. We're finally going to look at the whole process, first in general terms and then in more detail using code examples. As we mentioned earlier, the original insertString method of the CodeDocument class was changed, and much of the logic was moved to a new method called processChar(), thus allowing us to handle not only single keystrokes, but also to programmatically insert multiple character strings. ProcessChar() works by making certain assumptions about what characters will follow other characters. Based on this, it sets the insert mode for the current position of incoming text. Based on this mode (which is simply a private integer variable), it can then determine which insertXXXString() method to call. For example, when it encounters the character '9', it figures there's a very good chance that this is the start of a number or that a number is currently in the process of being entered. To verify this it calls the checkForNumber() method. If checkForNumber() determines that we're still entering a number, it sets the mode to the number entry mode. If it discovers any other character present, however, such as a space, a parenthesis or a letter, it sets the mode to the default text entry mode (the number entry mode is represented by the static constant NUMBER_MODE, while the text entry mode is represented by the static constant TEXT_MODE). The other checkForXXX() methods work in a similar fashion. After the checkForXXX() method has returned, the mode will have been set correctly and, based on this, the proper insertXXXString() method can be called. If the mode is in TEXT_MODE, the formatting is left alone.

Now look at the code in Listing 1 for method processChar. The first thing the method does is to check if we're in COMMENT_MODE. Because comments can include anything (aside from the comment-terminating characters), we'll let the mode default to COMMENT_MODE; otherwise we'll change it to default to TEXT_MODE, which is the standard text entry mode (no formatting). Next, a switch statement is created based on the character passed to the processChar method. As mentioned before, the method works on the assumption that the character being entered belongs to one of five groups (generic text, keywords, strings, numbers or comments), and that the case statements are grouped accordingly. Characters equaling the numbers '1' through '9' cause a check for numbers; characters equaling '*' or '/' suggest the possible start or end of a comment block, and a check is made; and so on. Once the switch statement is finished executing, a final check is made for quoted strings if we're still in TEXT_MODE. Finally, depending on which mode we're in, a call is made to the appropriate insertXXXString method. There are four check methods: checkForComment(), checkForKeyword(), checkForNumber() and checkForString(). While there are some differences from the original checkForKeywords() method, the basic idea, as discussed last month, is the same. We retrieve the current element, get the text from it, find our position in the element and then, starting at the end, we walk backward until a delimiter of some sort is found and then set the mode accordingly. Let's look at some of the checkForString() method's code to examine this more closely. We'll assume we already have the correct offset from the Document's element.
int quoteCount = 0; if ((offs >= 0) && (offs <= strLen-1)){ i = offs; while (i >0){ //the while loop walks back until we hit a delimiter

char charAt = elementText.charAt(i); if ((charAt == '"')){ quoteCount ++; } i--; }

int rem = quoteCount % 2; mode = (rem == 0) ? TEXT_MODE: STRING_MODE; }

The code is fairly simple: we loop backward until we run out of characters to process, each time comparing a character from the element text. If the character is a double quote (" "), we add one to the local variable called quoteCount. Once we're done with the loop, we check the remainder of the quoteCount divided by two and assign the results to another local variable called rem. Why do this? Again, this is another assumption about the way words are put together. Since quotes always come in pairs, if our remainder isn't equal to zero, we know we're "inside" a string quotation. Otherwise we can safely assume that we're "outside" and are just entering normal text. And Finally... I think at this point I've probably run out of column space. We now have a syntaxhighlighting Document class that supports any user-defined set keywords, plus string, number and comment highlighting (see Listings 2 and 3). We've also seen how easy it is to incorporate our Document class into the JTextPane control to test our results as we go along. In the next article we'll add some more user-definable properties so you can change the color of highlighted string, comments, keywords and so on. We'll also look at adding support for a kind of "smart editor" la Borland's Code Insight features in their IDE editors.

SNAPSHOTS OF PROJECT

HOME SCREEN

OPEN A FILE

PRINT THE DOCUMENT

SELECTALL

FIND AND REPLACE

ABOUT JFOX

THEME DISPLAY NIMBUS

CHANGING BACKGORUND COLOR

CONCLUSION
The primary goal of this report is to identify action initiatives that make up the information details regarding JFox - java source code editor. The application is made in all possible way to meet the user requirements using latest version of available software and hardware. But as user requirements and operating environment keep changing further extensions can be made on this. The project that has been developed is a part of programmer's developing tool , so it can be used and extended the scope and domain of project.

BIBLOGRAPHY
y Java the Complete Reference, Seventh Edition(Herbert Scheldt) y http://java.sys-con.com/node/36509 y http://www.oracle.com/technetwork/java/index.html y http://www.java.com

Anda mungkin juga menyukai