download JipeMethodFinder.java
Language: Java
License: GPL
Copyright: (c) 1996-2000 Steve Lawson. E-Mail
LOC: 228
Project Info
Jipe
Server: SourceForge
Type: cvs
...\j\jipe\jipe\jipe\src\jipe\
   BoxComment.java
   BrowserLauncher.java
   ClassBrowser.java
   ClassFilter.java
   DeleteLine.java
   Editor.java
   ExtensionFilter.java
   Find.java
   FindInFiles.java
   HelpConsole.java
   HtmlFilter.java
   IDEOptions.java
   JarFileManager.java
   JavaFilter.java
   JavaOptions.java
   Jipe.java
   JipeCodeCompleter.java
   JipeCompiler.java
   JipeConsole.java
   JipeDecompiler.java
   JipeMethodFinder.java
   JipeProjectManager.java
   JipeUtilities.java
   JSBeautifier.java
   JSFormatter.java
   JSLineBreaker.java
   JspFilter.java
   LeftIndent.java
   ListAllFiles.java
   MasterFilter.java
   Print.java
   PrintManager.java
   ProjectFilter.java
   PropertiesManager.java
   RemoveSpaces.java
   RightIndent.java
   SimpleComment.java
   SpecialFileFilters.java
   StatusBar.java
   StringParser.java
   TextFilter.java
   ToLowerCase.java
   ToUpperCase.java
   WingComment.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/**
 * JipeMethodFinder.java - SpeedBar window.  Copyright (c) 1996-2000 Steve Lawson.  E-Mail 
 * steve@e-i-s.co.uk Latest version found at http://e-i-s.co.uk/jipe
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of 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.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package jipe;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.text.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.net.URL;
import java.util.Date;
import java.text.SimpleDateFormat;


public class JipeMethodFinder implements ListSelectionListener{
    Jipe jipe;
    public JList results;
    public Vector positions;

    public JipeMethodFinder(Jipe parent){
        jipe=parent;
        results = new JList();
        results.setVisibleRowCount(10);
        results.addListSelectionListener(this);
        positions = new Vector();
        results.setFont(new Font(jipe.props.FONTSTYLE, 0, jipe.props.FONTSIZE));
    }
    public void removeList()
    {
        positions.removeAllElements();
        results.setListData(positions);
    }
    protected void parse()
    {
        try
        {
            positions.removeAllElements();
            Vector data = new Vector();
            Element map =
                jipe.activeChild.textarea.getDocument().getDefaultRootElement();
            int lines = map.getElementCount();
            for (int i = 0; i < lines; i++)
            {
                Element lineElement = map.getElement(i);
                int start = lineElement.getStartOffset();
                String lineString = jipe.activeChild.textarea.getText(start,
                                    lineElement.getEndOffset() - start - 1);
                if (isMethod(lineString))
                {
                    String splitstring = SplitString(lineString);
                    data.addElement(splitstring);

                    // data.addElement(lineString.substring(
                    //     getMethodStartIndex(), getMethodEndIndex()));

                    positions.addElement(jipe.activeChild.textarea.getDocument().createPosition
                                         (start + getMethodStartIndex()));
                }
            }
            results.setListData(data);
        }
        catch (BadLocationException ble)
        {
        }
    }

    /**
     * Methods indexes
     */
    private int startIndex;
    private int endIndex;

    /**
     * Braces indexex
     */
    private int parenthesisOpen = -1;
    private int parenthesisClose = -1;

    /**
     * Several booleans needed to determine if our 'method' can be displayed
     */
    private boolean isInComment;
    private boolean isString;
    private boolean isVariable;
    private boolean isInstruction;
    private boolean oneWord;
    private int getMethodStartIndex()
    {
        return startIndex;
    }
    private int getMethodEndIndex()
    {
        return endIndex;
    }
    private boolean isMethod(String data)
    {

        // Used to determine the method start index
        isVariable = isInstruction = oneWord = false;
        parenthesisOpen = parenthesisClose = -1;
        boolean isIndented = false;
        startIndex = endIndex = 0;

        // Current character
        char c;
        char comment = '\0';

        // Last parsed char
        char lastChar = '\0';

        // Index of the comment, if one, on current line See below its usage
        int commentIndex = -1;
out:
        for (int i = 0; i < data.length(); i++)
        {
            switch (c = data.charAt(i))
            {
            case ' ':
            case '\t':                               // needed to
                // remove blanks
                // spaces and
                // ensure we
                if (!isIndented && !isString && !isInComment) // got
                    // at
                    // least
                    // one
                    // word
                    // before
                    // parenthesis
                    startIndex++;
                else if ((i < data.length() - 1) &&
                         data.charAt(i + 1) != '(')
                    oneWord = true;
                lastChar = ' ';
                break;
            case '\"':                               // the user may
                // have declared
                // methods in a
                // String
                if (isInComment)
                    break;                           // we have to
                // avoid these
                // 'declares'
                if (!isIndented)
                    isIndented = true;
                if (!isString)
                    isString = true;
                else
                    isString = false;
                lastChar = '\"';
                break;
            case '/':                                // coders often
                // place
                // method(s) in
                // comments
                if (!isIndented)
                    isIndented = true;               // we have to
                // avoid'em too
                if (isString)
                    break;
                if (lastChar == '/')
                {
                    if (isInComment)
                        break;
                    isInComment = true;
                    commentIndex = i;
                    comment = '/';
                    break out;
                }
                else if (lastChar == '*')
                    isInComment = false;
                else
                    lastChar = '/';
                break;
            case '*':                                // if we
                // encounter
                // this, the
                // comment might
                // ends
                if (!isIndented)
                    isIndented = true;               // here...
                if (isString)
                    break;
                if (lastChar == '/')
                {
                    if (isInComment)
                        break;
                    isInComment = true;
                    commentIndex = i;
                }
                else
                    lastChar = '*';
                break;
            case '(':                                // a method
                // declare is
                // made of a
                // opening
                // parethesis
                if (!isInComment && !isString)       // and of a
                    // closing one
                    parenthesisOpen = i;
                lastChar = '(';
                break;
            case ')':                                // that's what
                // I've just said
                if (!isInComment && !isString)
                    if (parenthesisClose == -1)
                        parenthesisClose = i;
                lastChar = ')';
                break;
            case '=':
            case '?':
            case '!':
            case ':':                                // these chars
                // NEVER appear
                // in a method
                // declare
                if (!isInComment && !isString)
                    isVariable = true;
                lastChar = c;
                break;
            case ';':
            case '|':
            case '&':                                // these chars
                // NEVER appear
                // in a method
                // declare
                if (!isInComment && !isString)
                    isInstruction = true;
                lastChar = ';';
                break;
            default:
                if (!isIndented)
                    isIndented = true;
                if (isString || isInComment)
                    break;
                lastChar = c;
                break;
            }
        }
        if (commentIndex == -1)
            commentIndex = data.length();

        // our String must be out of a String, out of a comment, contain at
        // least one word before the parenthesis, have an opening and a
        // closing parenthesis and the opening parenthesis must be placed
        // before the closing one
        if (!isInComment && !isString && !isVariable && !isInstruction &&
                oneWord && parenthesisOpen != -1 && parenthesisClose != -1 &&
                parenthesisOpen < parenthesisClose && parenthesisClose <=
                commentIndex)
        {

            // Silly hack !! To FIX #################### necessary because
            // methods declares really look like those blocks code
            if (data.indexOf("if") == -1 && data.indexOf("catch") == -1 && data.indexOf
                    ("new") == -1 && data.indexOf("switch") == -1 && data.indexOf(
                        "while") == -1)
            {
                endIndex = parenthesisClose + 1;
                if (comment == '/')
                    isInComment = false;
                return true;
            }
        }
        if (comment == '/')
            isInComment = false;
        return false;
    }
    private String SplitString(String stringname)
    {
        String s = null;
        int pos = stringname.indexOf('(');
        if (pos != -1)
            s = stringname.substring(0, pos);
        StringTokenizer t = new StringTokenizer(s, " ");
        while (t.hasMoreTokens())
            s = t.nextToken().trim();
        return s;
    }
    public void valueChanged(ListSelectionEvent evt)
    {
        if (results.isSelectionEmpty() || evt.getValueIsAdjusting() ||
                jipe.activeChild == null)
            return;
        Position pos = (Position)positions.elementAt(
                           results.getSelectedIndex());
        Element map =
            jipe.activeChild.textarea.getDocument().getDefaultRootElement();
        Element lineElement = map.getElement(map.getElementIndex(
                                                 pos.getOffset()));
        if (lineElement == null)
            return;
        jipe.activeChild.textarea.select(lineElement.getStartOffset(),
                                         lineElement.getEndOffset() - 1);
    }

}

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us