JET

edu.utep.cs.et.gui.editor
Class DefaultEditor

java.lang.Object
  extended by edu.utep.cs.et.gui.editor.DefaultEditor
All Implemented Interfaces:
Editor
Direct Known Subclasses:
LineNumberedEditor

public class DefaultEditor
extends Object
implements Editor

A simple source code editor that support signle document editing and primitive syntax hightligting.

Author:
Yoonsik Cheon

Nested Class Summary
private  class DefaultEditor.DefaultCaretListener
          A caret listener class for the editor.
private static class DefaultEditor.PasteEnablingAction
          An action class to represent editing actions that enable the paste action.
private  class DefaultEditor.RedoAction
          A class representing redo actions.
private  class DefaultEditor.UndoAction
          A class representing undo actions.
 
Field Summary
private static Action actCopy
          The action for the copy menu.
private static Action actCut
          The action for the cut menu.
private static Action actDelete
          The action for the Delete menu.
private static Action actPaste
          The action for the Paste menu.
private static DefaultEditor.RedoAction actRedo
          The action for the Redo menu.
private static DefaultEditor.UndoAction actUndo
          The action for the Undo menu.
private static DefaultEditorKit defaultEditorKit
          The default editor kit to open and write the document.
private  DocumentListener dirtyFlagManager
          A document listener to set the dirty flag.
private  boolean docModified
          The dirty flag indicating whether the document being edited (doc) was modified or not.
private  SyntaxDocument document
          The styled document that is the model for the textPane
private  List<FileChangeListener> fileChangeListeners
          Registered file change listeners.
private  List<LineChangeListener> lineChangeListeners
          Registered line change listeners.
private  JEditTextArea textPane
          The place where the text is drawn.
private static UndoManager undoManager
          The undo manager that handles undoable edits performed on the editor buffer.
 
Constructor Summary
DefaultEditor()
          Create a new JML editor.
 
Method Summary
 void addFileChangeListener(FileChangeListener l)
          Registers the given file change listener.
 void addLineChangeListener(LineChangeListener l)
          Registers the given line change listener.
protected  JTextPane createTextPane(DefaultStyledDocument doc)
          Creates a new text pane.
private  void disableAll()
          Diables all edit actions.
 SyntaxDocument document()
          Returns the document being edited.
private  CaretListener getCaretListener()
          Returns the caret listener.
 Action getCopy()
          Returns the copy action of this editor.
 Action getCut()
          Returns the cut action of this editor.
 Action getDelete()
          Returns the action for delete menu item.
protected  ImageIcon getIcon(String name)
          Returns an image icon whose image is read from the file named name.
 OptionPanel getOptionPanel()
          Returns the option panel of this editor.
 Action getPaste()
          Returns the action for paste menu item.
 Action getRedo()
          Returns the redo action of this editor.
 Action getUndo()
          Returns the undo action of this editor.
private  UndoManager getUndoManager()
          Creates and returns an undo manger.
 JComponent getView()
          Returns the view of this editor.
 void highlight(int line)
          Highlights the specified line.
 void highlight(int line, int col)
          Highlights the specified line and column.
 boolean isDirty()
          Returns true if there exist any unsaved edits.
 boolean newDocument()
          Creates a new empty document and returns true if the creation was successful.
private  void notifyFileOpened(File file)
          Notifies to the registered file change listeners that the given file was opened by this editor.
private  void notifyLineChanged(int line, int column)
          Notifies to the registered line change listeners that the caret was moved to the given line and column numbers.
 boolean openFile(File file)
          Opens the given file file using this editor, and returns true if the file was successfully opened.
private  void resetUndoManager()
          Resets the undo manager by discarding all edits and disabling both the undo and the redo actions.
 boolean saveFile(File file)
          Saves the document of this editor to the given file file, and returns true if it was successfully saved.
(package private)  void setBackground(Color color)
          Sets the background color of this editor.
 void setCloseViewAction(AbstractAction action)
          Given the action that closes this editor's view, provides an appropriate user interface for it, such as a popup menu.
 void setDropTarget(DropTarget dropTarget)
          Associate a DropTarget with this editor so that the editor receives drops.
(package private)  void setFont(Font font)
          Sets the font.
(package private)  void setForeground(Color color)
          Sets the foreground color of this editor.
(package private)  void setStyles(SyntaxStyle[] styles)
          Sets the syntax styles of this editor.
private  void showMessage(String title, String msg)
          Shows a message dialog with the given title and message.
(package private)  void updateView()
          Updates the view by repaint it.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

textPane

private JEditTextArea textPane
The place where the text is drawn.


document

private SyntaxDocument document
The styled document that is the model for the textPane


docModified

private boolean docModified
The dirty flag indicating whether the document being edited (doc) was modified or not. If it is true, it means that there are unsaved changes; otherwise, no such changes exist.


defaultEditorKit

private static DefaultEditorKit defaultEditorKit
The default editor kit to open and write the document.


dirtyFlagManager

private final DocumentListener dirtyFlagManager
A document listener to set the dirty flag.


lineChangeListeners

private final List<LineChangeListener> lineChangeListeners
Registered line change listeners.


fileChangeListeners

private final List<FileChangeListener> fileChangeListeners
Registered file change listeners.


actCopy

private static Action actCopy
The action for the copy menu.


actCut

private static Action actCut
The action for the cut menu.


actPaste

private static Action actPaste
The action for the Paste menu.


actDelete

private static Action actDelete
The action for the Delete menu.


actUndo

private static DefaultEditor.UndoAction actUndo
The action for the Undo menu.


actRedo

private static DefaultEditor.RedoAction actRedo
The action for the Redo menu.


undoManager

private static UndoManager undoManager
The undo manager that handles undoable edits performed on the editor buffer. Initially both the undo and redo actions are disabled.

Constructor Detail

DefaultEditor

public DefaultEditor()
Create a new JML editor.

Method Detail

setDropTarget

public void setDropTarget(DropTarget dropTarget)
Associate a DropTarget with this editor so that the editor receives drops.

Specified by:
setDropTarget in interface Editor

getView

public JComponent getView()
Returns the view of this editor.

Specified by:
getView in interface Editor

getOptionPanel

public OptionPanel getOptionPanel()
Returns the option panel of this editor.

Specified by:
getOptionPanel in interface Editor

setCloseViewAction

public void setCloseViewAction(AbstractAction action)
Given the action that closes this editor's view, provides an appropriate user interface for it, such as a popup menu.

Specified by:
setCloseViewAction in interface Editor

isDirty

public boolean isDirty()
Returns true if there exist any unsaved edits.

Specified by:
isDirty in interface Editor

newDocument

public boolean newDocument()
Creates a new empty document and returns true if the creation was successful. The created document is not associated with an external file.

Specified by:
newDocument in interface Editor

openFile

public boolean openFile(File file)
Opens the given file file using this editor, and returns true if the file was successfully opened. The contents of the file are read into the editing buffer (i.e., document) of this editor.

Specified by:
openFile in interface Editor

saveFile

public boolean saveFile(File file)
Saves the document of this editor to the given file file, and returns true if it was successfully saved.

Specified by:
saveFile in interface Editor

highlight

public void highlight(int line,
                      int col)
Highlights the specified line and column.

Specified by:
highlight in interface Editor

highlight

public void highlight(int line)
Highlights the specified line.

Specified by:
highlight in interface Editor

addFileChangeListener

public void addFileChangeListener(FileChangeListener l)
Registers the given file change listener.

Specified by:
addFileChangeListener in interface Editor

addLineChangeListener

public void addLineChangeListener(LineChangeListener l)
Registers the given line change listener.

Specified by:
addLineChangeListener in interface Editor

getCopy

public Action getCopy()
Returns the copy action of this editor. Initially the action is disabled; it is enabled whenever something is selected in the editor.

Specified by:
getCopy in interface Editor
See Also:
getCaretListener()

getCut

public Action getCut()
Returns the cut action of this editor. Initially the action is disabled,; it is enabled whenever something is selected in the editor.

Specified by:
getCut in interface Editor
See Also:
getCaretListener()

getPaste

public Action getPaste()
Returns the action for paste menu item. Initially the action is disabled, and gets enabled whenever something is copied or cut by the editor.

Specified by:
getPaste in interface Editor

getDelete

public Action getDelete()
Returns the action for delete menu item. Initially the action is disabled, and gets enabled whenever something is selected on the editor.

Specified by:
getDelete in interface Editor
See Also:
getCaretListener()

getUndo

public Action getUndo()
Returns the undo action of this editor.

Specified by:
getUndo in interface Editor

getRedo

public Action getRedo()
Returns the redo action of this editor.

Specified by:
getRedo in interface Editor

document

public SyntaxDocument document()
Returns the document being edited.


createTextPane

protected JTextPane createTextPane(DefaultStyledDocument doc)
Creates a new text pane. This is a factory method that can be overridden by subclasses to return a different type of text panes.


updateView

void updateView()
Updates the view by repaint it. This method should be called when the editor options (e.g., tab size) have been changed.


setFont

void setFont(Font font)
Sets the font.


setForeground

void setForeground(Color color)
Sets the foreground color of this editor.


setBackground

void setBackground(Color color)
Sets the background color of this editor.


setStyles

void setStyles(SyntaxStyle[] styles)
Sets the syntax styles of this editor.


notifyLineChanged

private void notifyLineChanged(int line,
                               int column)
Notifies to the registered line change listeners that the caret was moved to the given line and column numbers.


notifyFileOpened

private void notifyFileOpened(File file)
Notifies to the registered file change listeners that the given file was opened by this editor. If the argument is null, it means that a new document was created without being associate with an external file.


showMessage

private void showMessage(String title,
                         String msg)
Shows a message dialog with the given title and message.


getUndoManager

private UndoManager getUndoManager()
Creates and returns an undo manger. The undo manager ignores all edits performed by the given thread thr, e.g., side-effects of syntax coloring. Initially both the undo and redo actions are disabled.


resetUndoManager

private void resetUndoManager()
Resets the undo manager by discarding all edits and disabling both the undo and the redo actions.


getCaretListener

private CaretListener getCaretListener()
Returns the caret listener.


disableAll

private void disableAll()
Diables all edit actions.


getIcon

protected ImageIcon getIcon(String name)
Returns an image icon whose image is read from the file named name. A null is returned if reading the file fails.


JET

JET is Copyright (C) 2005-2007 by The University of Texas at El Paso and is distributed under the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.