com.chez.powerteam.java.tools
Class JextTextAreaLite

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--javax.swing.JComponent
                    |
                    +--javax.swing.text.JTextComponent
                          |
                          +--javax.swing.JTextArea
                                |
                                +--com.chez.powerteam.java.tools.JextTextAreaLite

public class JextTextAreaLite
extends javax.swing.JTextArea
implements javax.swing.event.UndoableEditListener, javax.swing.event.DocumentListener

This component extends a JTextArea. This component provides its own methods to read and save files (even to zip them).

Version:
1.1
See Also:
Serialized Form

Inner classes inherited from class javax.swing.JTextArea
javax.swing.JTextArea.AccessibleJTextArea
 
Inner classes inherited from class javax.swing.text.JTextComponent
javax.swing.text.JTextComponent.AccessibleJTextComponent, javax.swing.text.JTextComponent.KeyBinding
 
Inner classes inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Field Summary
static int OPEN
          This constant defines an open dialog box.
static int SAVE
          This constant defines a save dialog box.
 
Fields inherited from class javax.swing.text.JTextComponent
DEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEY
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Constructor Summary
JextTextAreaLite()
          The constructor add the necessary listeners, set some stuffs (caret color, borers, fonts...).
 
Method Summary
 void beginCompoundEdit()
          Used for ReplaceAll.
 void beginOperation()
          When an operation has began, setChanged() cannot be called.
 void changedUpdate(javax.swing.event.DocumentEvent e)
          When a modification is made in the text, we turn the 'to_be_saved' flag to true.
static java.lang.String chooseFile(java.awt.Component owner, int mode)
          Display a file chooser dialog box.
 void clean()
          Called after having saved or created a new document to ensure the content isn't 'dirty'.
 void discard()
          Discard all edits contained in the UndoManager.
 void endCompoundEdit()
          See beginCompoundEdit().
 void endOperation()
          Calling this will allow the DocumentListener to use setChanged().
 boolean find(java.lang.String searchStr, int start, boolean ignoreCase)
          Return the result of a string search.
 int getAnchor()
          Return the anchor position.
 java.lang.String getCurrentFile()
          Return the full path of the opened file.
 java.lang.String getFontName()
          Return current font's name
 int getFontSize()
          Return current font's size
 int getFontStyle()
          Return current font's style (bold, italic...)
 int getLength()
          Return the lentgh of the text in the area.
 int getLineStartOffset(int off)
          This is just to reduce code size of other classes.
 boolean getOperation()
          Return true if we can use the setChanged() method, false otherwise.
 javax.swing.undo.UndoManager getUndo()
          Useful for the GUI.
 void insertUpdate(javax.swing.event.DocumentEvent e)
          When a modification is made in the text, we turn the 'to_be_saved' flag to true.
 boolean isDirty()
          Return true if area content has changed, false otherwise.
 boolean isEmpty()
          Return true if area is empty, false otherwise.
 boolean isNew()
          Return true if current text is new, false otherwise.
 void newFile()
          Set a new file.
 void open(java.lang.String path)
          Called to load a new file in the text area.
 void removeUpdate(javax.swing.event.DocumentEvent e)
          When a modification is made in the text, we turn the 'to_be_saved' flag to true.
 boolean replaceAll(java.lang.String searchStr, java.lang.String replaceStr, int start, int end, boolean ignoreCase)
          Return the result of a string replace.
 void save(java.lang.String file)
          Store the text in a specified file.
 void saveContent()
          Called to save this component's content.
 void setAnchor(int offset)
          Set the anchor postion.
 void setDirty()
          Called when the content of the area has changed.
 void setFontName(java.lang.String name)
          Set the font which has to be used.
 void setFontSize(int size)
          Set the size of the font.
 void setFontStyle(int style)
          Set the style of the font.
static void showError(java.lang.String message)
          Display an error message in a dialog box.
static void showMessage(java.lang.String message)
          Display a sample message in a dialog box.
 java.lang.String toString()
          Return a String representation of this object.
 void undoableEditHappened(javax.swing.event.UndoableEditEvent e)
          When an undoable event is fired, we add it to the undo/redo list.
 void zip(java.lang.String zipFile)
          Zip text area content into specified file.
 void zipContent()
          Called to save current content in specified zip file.
 
Methods inherited from class javax.swing.JTextArea
append, createDefaultModel, getAccessibleContext, getColumns, getColumnWidth, getLineCount, getLineEndOffset, getLineOfOffset, getLineWrap, getPreferredScrollableViewportSize, getPreferredSize, getRowHeight, getRows, getScrollableTracksViewportWidth, getScrollableUnitIncrement, getTabSize, getUIClassID, getWrapStyleWord, insert, isManagingFocus, paramString, processComponentKeyEvent, replaceRange, setColumns, setFont, setLineWrap, setRows, setTabSize, setWrapStyleWord
 
Methods inherited from class javax.swing.text.JTextComponent
addCaretListener, addKeymap, copy, cut, fireCaretUpdate, getActions, getCaret, getCaretColor, getCaretPosition, getDisabledTextColor, getDocument, getFocusAccelerator, getHighlighter, getInputMethodRequests, getKeymap, getKeymap, getMargin, getScrollableBlockIncrement, getScrollableTracksViewportHeight, getSelectedText, getSelectedTextColor, getSelectionColor, getSelectionEnd, getSelectionStart, getText, getText, getUI, isEditable, isFocusTraversable, isOpaque, loadKeymap, modelToView, moveCaretPosition, paste, processInputMethodEvent, read, removeCaretListener, removeKeymap, removeNotify, replaceSelection, select, selectAll, setCaret, setCaretColor, setCaretPosition, setDisabledTextColor, setDocument, setEditable, setEnabled, setFocusAccelerator, setHighlighter, setKeymap, setMargin, setOpaque, setSelectedTextColor, setSelectionColor, setSelectionEnd, setSelectionStart, setText, setUI, updateUI, viewToModel, write
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getAlignmentX, getAlignmentY, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getGraphics, getHeight, getInsets, getInsets, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getVisibleRect, getWidth, getX, getY, grabFocus, hasFocus, isDoubleBuffered, isFocusCycleRoot, isLightweightComponent, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, processFocusEvent, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDoubleBuffered, setForeground, setMaximumSize, setMinimumSize, setNextFocusableComponent, setPreferredSize, setRequestFocusEnabled, setToolTipText, setUI, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getLayout, insets, invalidate, isAncestorOf, layout, list, list, locate, minimumSize, paintComponents, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setLayout, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addPropertyChangeListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, getBackground, getBounds, getColorModel, getComponentOrientation, getCursor, getDropTarget, getFont, getFontMetrics, getForeground, getInputContext, getLocale, getLocation, getLocationOnScreen, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hide, imageUpdate, inside, isDisplayable, isEnabled, isLightweight, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, processComponentEvent, processMouseEvent, remove, removeComponentListener, removeFocusListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, transferFocus
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

OPEN

public static final int OPEN
This constant defines an open dialog box.

SAVE

public static final int SAVE
This constant defines a save dialog box.
Constructor Detail

JextTextAreaLite

public JextTextAreaLite()
The constructor add the necessary listeners, set some stuffs (caret color, borers, fonts...).
Method Detail

chooseFile

public static java.lang.String chooseFile(java.awt.Component owner,
                                          int mode)
Display a file chooser dialog box.
Parameters:
owner - Component which 'owns' the dialog
mode - Can be either LOAD or SAVE
Returns:
The path to selected file, null otherwise

showMessage

public static void showMessage(java.lang.String message)
Display a sample message in a dialog box.
Parameters:
message - The message to display

showError

public static void showError(java.lang.String message)
Display an error message in a dialog box.
Parameters:
message - The message to display

getFontName

public java.lang.String getFontName()
Return current font's name

getFontSize

public int getFontSize()
Return current font's size

getFontStyle

public int getFontStyle()
Return current font's style (bold, italic...)

setFontName

public void setFontName(java.lang.String name)
Set the font which has to be used.
Parameters:
name - The name of the font

setFontSize

public void setFontSize(int size)
Set the size of the font.
Parameters:
size - The new font's size

setFontStyle

public void setFontStyle(int style)
Set the style of the font.
Parameters:
style - The new style to apply

getCurrentFile

public java.lang.String getCurrentFile()
Return the full path of the opened file.

beginOperation

public void beginOperation()
When an operation has began, setChanged() cannot be called. This is very important when we need to insert or remove some parts of the texte without turning on the 'to_be_saved' flag.

endOperation

public void endOperation()
Calling this will allow the DocumentListener to use setChanged().

getOperation

public boolean getOperation()
Return true if we can use the setChanged() method, false otherwise.

newFile

public void newFile()
Set a new file. We first ask the user if he'd like to save its changes (if some have been made).

getLineStartOffset

public int getLineStartOffset(int off)
This is just to reduce code size of other classes.
Parameters:
off - The line index
Returns:
The offset in the text where the line begins
Overrides:
getLineStartOffset in class javax.swing.JTextArea

zipContent

public void zipContent()
Called to save current content in specified zip file. Call zip(String file) but asks user for overwriting if file already exists.

zip

public void zip(java.lang.String zipFile)
Zip text area content into specified file.
Parameters:
zipFile - The file name where to zip the text

saveContent

public void saveContent()
Called to save this component's content. Call save(String file) but let the user choosing a file name. In the case the user choosed an existing file, we ask him if he really wants to overwrite it.

save

public void save(java.lang.String file)
Store the text in a specified file.
Parameters:
file - The file in wich we'll write the text

open

public void open(java.lang.String path)
Called to load a new file in the text area. Determines which line separator (\n, \r\n...) are used in the file to open. Convert'em into Swing line separator (\n).
Parameters:
path - The path of the file to be loaded

isNew

public boolean isNew()
Return true if current text is new, false otherwise.

isEmpty

public boolean isEmpty()
Return true if area is empty, false otherwise.

isDirty

public boolean isDirty()
Return true if area content has changed, false otherwise.

setDirty

public void setDirty()
Called when the content of the area has changed.

clean

public void clean()
Called after having saved or created a new document to ensure the content isn't 'dirty'.

discard

public void discard()
Discard all edits contained in the UndoManager.

getUndo

public javax.swing.undo.UndoManager getUndo()
Useful for the GUI.

getAnchor

public int getAnchor()
Return the anchor position.

setAnchor

public void setAnchor(int offset)
Set the anchor postion.
Parameters:
offset - The new anchor's position

getLength

public int getLength()
Return the lentgh of the text in the area.

beginCompoundEdit

public void beginCompoundEdit()
Used for ReplaceAll. This merges all text changes made between the beginCompoundEdit() and the endCompoundEdit() calls into only one undo event.

endCompoundEdit

public void endCompoundEdit()
See beginCompoundEdit().

find

public boolean find(java.lang.String searchStr,
                    int start,
                    boolean ignoreCase)
Return the result of a string search.
Parameters:
searchStr - The string to be found
start - The search's start offset
ignoreCase - Set to true, we'll ignore the text case
Returns:
True if searchStr has been found, false otherwise

replaceAll

public boolean replaceAll(java.lang.String searchStr,
                          java.lang.String replaceStr,
                          int start,
                          int end,
                          boolean ignoreCase)
                   throws gnu.regexp.REException
Return the result of a string replace.
Parameters:
searchStr - The string to be found
replaceStr - The string which will replace searchStr
start - The search's start offset
end - The search's end offset
ignoreCase - Set to true, we'll ignore the text case
Returns:
True if the replace has been successfully done, false otherwise

undoableEditHappened

public void undoableEditHappened(javax.swing.event.UndoableEditEvent e)
When an undoable event is fired, we add it to the undo/redo list.
Specified by:
undoableEditHappened in interface javax.swing.event.UndoableEditListener

changedUpdate

public void changedUpdate(javax.swing.event.DocumentEvent e)
When a modification is made in the text, we turn the 'to_be_saved' flag to true.
Specified by:
changedUpdate in interface javax.swing.event.DocumentListener

insertUpdate

public void insertUpdate(javax.swing.event.DocumentEvent e)
When a modification is made in the text, we turn the 'to_be_saved' flag to true.
Specified by:
insertUpdate in interface javax.swing.event.DocumentListener

removeUpdate

public void removeUpdate(javax.swing.event.DocumentEvent e)
When a modification is made in the text, we turn the 'to_be_saved' flag to true.
Specified by:
removeUpdate in interface javax.swing.event.DocumentListener

toString

public java.lang.String toString()
Return a String representation of this object.
Overrides:
toString in class java.awt.Component