kawigi.editor
Class GenericView

java.lang.Object
  extended by javax.swing.text.View
      extended by javax.swing.text.PlainView
          extended by kawigi.editor.GenericView
All Implemented Interfaces:
javax.swing.SwingConstants, javax.swing.text.TabExpander
Direct Known Subclasses:
CPPView, CSharpView, JavaView, VBView

public class GenericView
extends javax.swing.text.PlainView

Superclass of all language-specific views in KawigiEdit. This class handles the custom tab stop, pair matching, has some shared methods for syntax highlighting, and paints the KawigiEdit watermark in the background of the editor. As many people as have complained about the watermark and asked for a way to remove it, I was surprised when I changed it to blue and people said they don't like it as much as the old one that used "TopCoder" colors.


Field Summary
protected static java.awt.Color classColor
          Colors for various token types.
protected static java.awt.Color commentColor
          Colors for various token types.
protected static java.awt.Color directiveColor
          Colors for various token types.
protected static java.lang.Object highlightKey1
          Keys for the pair-matching highlighter.
protected static java.lang.Object highlightKey2
          Keys for the pair-matching highlighter.
protected static java.awt.Color keywordColor
          Colors for various token types.
protected  int lineIndex
          Set parseIndex to 0 and lineIndex to 1 before calling findIntervals.
protected static java.awt.Color operatorColor
          Colors for various token types.
protected  int parseIndex
          Set parseIndex to 0 and lineIndex to 1 before calling findIntervals.
protected static java.awt.Color stringColor
          Colors for various token types.
protected static java.awt.Color tagColor
          Colors for various token types.
protected static java.awt.Color typeColor
          Colors for various token types.
 
Fields inherited from class javax.swing.text.PlainView
metrics
 
Fields inherited from class javax.swing.text.View
BadBreakWeight, ExcellentBreakWeight, ForcedBreakWeight, GoodBreakWeight, X_AXIS, Y_AXIS
 
Fields inherited from interface javax.swing.SwingConstants
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
 
Constructor Summary
GenericView(javax.swing.text.Element e)
          Just forwards that Element on down.
 
Method Summary
protected  void findIntervals(java.util.ArrayList<Interval> list, java.lang.String text, java.lang.String[] startEnd, boolean block)
          Helper function to help with getIntervals.
protected  Interval findName(java.lang.String text, int index, int startline, int endline, int endindex, java.lang.String[] startEnd, boolean block)
          Searches for a String identifier for a block in the text before the block.
static void getColors()
          initializes all the syntax colors.
 java.util.ArrayList<Interval> getIntervals()
          Returns an ArrayList of Interval objects representing code block intervals in the document.
protected  int getTabSize()
          Programmers don't like 8-space tabstops.
 void paint(java.awt.Graphics g, java.awt.Shape a)
          A little hack to put a "TopCoder-ish" logo and a copycat logo for KawigiEdit in the background of the text pane.
static void readKeywords(java.lang.String name, boolean caseSensitive, java.util.Map<java.lang.String,java.awt.Color> colorHash)
          Reads a keywords file and initializes the map to appropriately color those tokens.
static void resetTabStop()
          Checks the property kawigi.editor.tabstop and sets the tabstop used in GenericView and derivatives to its value.
protected  void updateDamage(javax.swing.event.DocumentEvent changes, java.awt.Shape a, javax.swing.text.ViewFactory f)
          NOTHING BUT A HACK (and a weird, dirty one at that).
 
Methods inherited from class javax.swing.text.PlainView
changedUpdate, damageLineRange, drawLine, drawSelectedText, drawUnselectedText, getLineBuffer, getPreferredSpan, insertUpdate, lineToRect, modelToView, nextTabStop, removeUpdate, setSize, updateMetrics, viewToModel
 
Methods inherited from class javax.swing.text.View
append, breakView, createFragment, forwardUpdate, forwardUpdateToView, getAlignment, getAttributes, getBreakWeight, getChildAllocation, getContainer, getDocument, getElement, getEndOffset, getGraphics, getMaximumSpan, getMinimumSpan, getNextVisualPositionFrom, getParent, getResizeWeight, getStartOffset, getToolTipText, getView, getViewCount, getViewFactory, getViewIndex, getViewIndex, insert, isVisible, modelToView, modelToView, preferenceChanged, remove, removeAll, replace, setParent, updateChildren, updateLayout, viewToModel
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

highlightKey1

protected static java.lang.Object highlightKey1
Keys for the pair-matching highlighter.


highlightKey2

protected static java.lang.Object highlightKey2
Keys for the pair-matching highlighter.


keywordColor

protected static java.awt.Color keywordColor
Colors for various token types.


typeColor

protected static java.awt.Color typeColor
Colors for various token types.


operatorColor

protected static java.awt.Color operatorColor
Colors for various token types.


classColor

protected static java.awt.Color classColor
Colors for various token types.


stringColor

protected static java.awt.Color stringColor
Colors for various token types.


commentColor

protected static java.awt.Color commentColor
Colors for various token types.


directiveColor

protected static java.awt.Color directiveColor
Colors for various token types.


tagColor

protected static java.awt.Color tagColor
Colors for various token types.


parseIndex

protected int parseIndex
Set parseIndex to 0 and lineIndex to 1 before calling findIntervals.


lineIndex

protected int lineIndex
Set parseIndex to 0 and lineIndex to 1 before calling findIntervals.

Constructor Detail

GenericView

public GenericView(javax.swing.text.Element e)
Just forwards that Element on down.

Method Detail

getColors

public static void getColors()
initializes all the syntax colors.


readKeywords

public static void readKeywords(java.lang.String name,
                                boolean caseSensitive,
                                java.util.Map<java.lang.String,java.awt.Color> colorHash)
Reads a keywords file and initializes the map to appropriately color those tokens.


resetTabStop

public static void resetTabStop()
Checks the property kawigi.editor.tabstop and sets the tabstop used in GenericView and derivatives to its value. It sets the property and the tabstop to 4 if the property isn't set.


paint

public void paint(java.awt.Graphics g,
                  java.awt.Shape a)
A little hack to put a "TopCoder-ish" logo and a copycat logo for KawigiEdit in the background of the text pane. I know, it just makes me seem like I need attention or something, but I tried to at least make it look nice and non-intrusive.

Overrides:
paint in class javax.swing.text.PlainView

getTabSize

protected int getTabSize()
Programmers don't like 8-space tabstops. At least I don't, so I set the default to 4. At Ryan's request, this is now configurable.

Overrides:
getTabSize in class javax.swing.text.PlainView

getIntervals

public java.util.ArrayList<Interval> getIntervals()
Returns an ArrayList of Interval objects representing code block intervals in the document. This default implementation matches curly braces, square brackets and parentheses.


findIntervals

protected void findIntervals(java.util.ArrayList<Interval> list,
                             java.lang.String text,
                             java.lang.String[] startEnd,
                             boolean block)
Helper function to help with getIntervals. Puts the intervals into the list in an in-order traversal of curly braces.


findName

protected Interval findName(java.lang.String text,
                            int index,
                            int startline,
                            int endline,
                            int endindex,
                            java.lang.String[] startEnd,
                            boolean block)
Searches for a String identifier for a block in the text before the block. May or may not be particularly useful in many situations. This is called by findIntervals.


updateDamage

protected void updateDamage(javax.swing.event.DocumentEvent changes,
                            java.awt.Shape a,
                            javax.swing.text.ViewFactory f)
NOTHING BUT A HACK (and a weird, dirty one at that). This is a "fix" to the problem where after undoing exceptions occurred causing the cursor to not update its position after edits (which leads to a really strange editing experience after that point, as you might imagine). In the case that it was reaching when this bug occurred, It seems I can force the parent of this class to do something of a double take by spoofing its parent element and calling an update method. Any cleaner hack seemed impossible due to the use of private methods and even package-level access classes. This appears to be a blatant bug in Swing. I think a real fix to this would be in AbstractDocument$BranchElement.getEndOffset(), but the necessary check here might not really solve the problem, just get it out of my way in this case. In this odd case, nchildren is zero, and getEndOffset() tries to access the element at index -1. Reproducing the bug is about as simple as creating a JTextPane that uses a PlainView to render, install a pretty standard UndoManager on it, write some arbitrary text into it, and then select and paste in some other big text or something, then undo (with whatever mechanism you have for that, for me it's a keystroke).

Overrides:
updateDamage in class javax.swing.text.PlainView