[25584] | 1 | /*
|
---|
| 2 | * 10/28/2004
|
---|
| 3 | *
|
---|
| 4 | * DefaultTokenFactory.java - Default token factory.
|
---|
| 5 | *
|
---|
| 6 | * This library is distributed under a modified BSD license. See the included
|
---|
| 7 | * RSyntaxTextArea.License.txt file for details.
|
---|
| 8 | */
|
---|
| 9 | package org.fife.ui.rsyntaxtextarea;
|
---|
| 10 |
|
---|
| 11 | import javax.swing.text.Segment;
|
---|
| 12 |
|
---|
| 13 |
|
---|
| 14 | /**
|
---|
| 15 | * This class generates tokens for a {@link TokenMaker}. This class is here
|
---|
| 16 | * because it reuses tokens when they aren't needed anymore to prevent
|
---|
| 17 | * This class doesn't actually create new tokens every time
|
---|
| 18 | * <code>createToken</code> is called. Instead, it internally keeps a stack of
|
---|
| 19 | * available already-created tokens. When more tokens are needed to properly
|
---|
| 20 | * display a line, more tokens are added to the available stack. This saves
|
---|
| 21 | * from needless repetitive memory allocation. However, it makes it IMPERATIVE
|
---|
| 22 | * that users call <code>resetTokenList</code> when creating a new token list so
|
---|
| 23 | * that the token maker can keep an accurate list of available tokens.<p>
|
---|
| 24 | *
|
---|
| 25 | * NOTE: This class should only be used by {@link TokenMaker}; nobody else
|
---|
| 26 | * needs it!
|
---|
| 27 | *
|
---|
| 28 | * @author Robert Futrell
|
---|
| 29 | * @version 0.1
|
---|
| 30 | */
|
---|
| 31 | class DefaultTokenFactory implements TokenFactory {
|
---|
| 32 |
|
---|
| 33 | private int size;
|
---|
| 34 | private int increment;
|
---|
| 35 | private Token[] tokenList;
|
---|
| 36 | private int currentFreeToken;
|
---|
| 37 |
|
---|
| 38 | protected static final int DEFAULT_START_SIZE = 30;
|
---|
| 39 | protected static final int DEFAULT_INCREMENT = 10;
|
---|
| 40 |
|
---|
| 41 |
|
---|
| 42 | /**
|
---|
| 43 | * Cosnstructor.
|
---|
| 44 | */
|
---|
| 45 | public DefaultTokenFactory() {
|
---|
| 46 | this(DEFAULT_START_SIZE, DEFAULT_INCREMENT);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 |
|
---|
| 50 | /**
|
---|
| 51 | * Constructor.
|
---|
| 52 | *
|
---|
| 53 | * @param size The initial number of tokens in this factory.
|
---|
| 54 | * @param increment How many tokens to increment by when the stack gets
|
---|
| 55 | * empty.
|
---|
| 56 | */
|
---|
| 57 | public DefaultTokenFactory(int size, int increment) {
|
---|
| 58 |
|
---|
| 59 | this.size = size;
|
---|
| 60 | this.increment = increment;
|
---|
| 61 | this.currentFreeToken = 0;
|
---|
| 62 |
|
---|
| 63 | // Give us some tokens to initially work with.
|
---|
| 64 | tokenList = new Token[size];
|
---|
| 65 | for (int i=0; i<size; i++)
|
---|
| 66 | tokenList[i] = createInternalUseOnlyToken();
|
---|
| 67 |
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 |
|
---|
| 71 | /**
|
---|
| 72 | * Adds tokens to the internal token list. This is called whenever a
|
---|
| 73 | * request is made and no more tokens are available.
|
---|
| 74 | */
|
---|
| 75 | private final void augmentTokenList() {
|
---|
| 76 | Token[] temp = new Token[size + increment];
|
---|
| 77 | System.arraycopy(tokenList,0, temp,0, size);
|
---|
| 78 | size += increment;
|
---|
| 79 | tokenList = temp;
|
---|
| 80 | for (int i=0; i<increment; i++) {
|
---|
| 81 | tokenList[size-i-1] = createInternalUseOnlyToken();
|
---|
| 82 | }
|
---|
| 83 | //System.err.println("... size up to: " + size);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 |
|
---|
| 87 | /**
|
---|
| 88 | * Creates a token for use internally by this token factory. This method
|
---|
| 89 | * should NOT be called externally; only by this class and possibly
|
---|
| 90 | * subclasses.
|
---|
| 91 | *
|
---|
| 92 | * @return A token to add to this token factory's internal stack. If a
|
---|
| 93 | * subclass wants to produce a stack of a token other than
|
---|
| 94 | * {@link DefaultToken}, then this method can be overridden to
|
---|
| 95 | * return a new instance of the desired token type.
|
---|
| 96 | */
|
---|
| 97 | protected Token createInternalUseOnlyToken() {
|
---|
| 98 | return new DefaultToken();
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 |
|
---|
| 102 | /**
|
---|
| 103 | * Returns a null token.
|
---|
| 104 | *
|
---|
| 105 | * @return A null token.
|
---|
| 106 | */
|
---|
| 107 | public Token createToken() {
|
---|
| 108 | Token token = tokenList[currentFreeToken];
|
---|
| 109 | token.text = null;
|
---|
| 110 | token.type = Token.NULL;
|
---|
| 111 | token.offset = -1;
|
---|
| 112 | token.setNextToken(null);
|
---|
| 113 | currentFreeToken++;
|
---|
| 114 | if (currentFreeToken==size)
|
---|
| 115 | augmentTokenList();
|
---|
| 116 | return token;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 |
|
---|
| 120 | /**
|
---|
| 121 | * Returns a token.
|
---|
| 122 | *
|
---|
| 123 | * @param line The segment from which to get the token's text.
|
---|
| 124 | * @param beg The starting offset of the token's text in the segment.
|
---|
| 125 | * @param end The ending offset of the token's text in the segment.
|
---|
| 126 | * @param startOffset The offset in the document of the token.
|
---|
| 127 | * @param type The type of token.
|
---|
| 128 | * @return The token.
|
---|
| 129 | */
|
---|
| 130 | public Token createToken(final Segment line, final int beg,
|
---|
| 131 | final int end, final int startOffset, final int type) {
|
---|
| 132 | return createToken(line.array, beg,end, startOffset, type);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 |
|
---|
| 136 | /**
|
---|
| 137 | * Returns a token.
|
---|
| 138 | *
|
---|
| 139 | * @param line The segment from which to get the token's text.
|
---|
| 140 | * @param beg The starting offset of the token's text in the segment.
|
---|
| 141 | * @param end The ending offset of the token's text in the segment.
|
---|
| 142 | * @param startOffset The offset in the document of the token.
|
---|
| 143 | * @param type The type of token.
|
---|
| 144 | * @return The token.
|
---|
| 145 | */
|
---|
| 146 | public Token createToken(final char[] line, final int beg,
|
---|
| 147 | final int end, final int startOffset, final int type) {
|
---|
| 148 | Token token = tokenList[currentFreeToken];
|
---|
| 149 | token.set(line, beg,end, startOffset, type);
|
---|
| 150 | currentFreeToken++;
|
---|
| 151 | if (currentFreeToken==size)
|
---|
| 152 | augmentTokenList();
|
---|
| 153 | return token;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 |
|
---|
| 157 | /**
|
---|
| 158 | * Resets the state of this token maker. This method should be called
|
---|
| 159 | * by the <code>TokenMaker</code> every time a token list is generated for
|
---|
| 160 | * a new line so the tokens can be reused.
|
---|
| 161 | */
|
---|
| 162 | public void resetAllTokens() {
|
---|
| 163 | currentFreeToken = 0;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 |
|
---|
| 167 | } |
---|