Source Code Cross Referenced for LexerFrame.java in  » Scripting » groovy-1.0 » org » codehaus » groovy » antlr » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Scripting » groovy 1.0 » org.codehaus.groovy.antlr 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.codehaus.groovy.antlr;
002:
003:        import java.awt.*;
004:        import java.awt.event.*;
005:        import java.io.*;
006:        import java.lang.reflect.*;
007:        import java.util.Hashtable;
008:
009:        import javax.swing.*;
010:        import javax.swing.border.Border;
011:        import javax.swing.text.BadLocationException;
012:        import org.codehaus.groovy.antlr.parser.*;
013:
014:        import antlr.*;
015:
016:        /**
017:         * @author Santhosh Kumar T
018:         * @version 1.0
019:         */
020:
021:        public class LexerFrame extends JFrame implements  ActionListener {
022:            JSplitPane jSplitPane1 = new JSplitPane();
023:            JScrollPane jScrollPane1 = new JScrollPane();
024:            JScrollPane jScrollPane2 = new JScrollPane();
025:            JTextPane tokenPane = new HScrollableTextPane();
026:            JButton jbutton = new JButton("open");
027:            JPanel mainPanel = new JPanel(new BorderLayout());
028:            JTextArea scriptPane = new JTextArea();
029:            Border border1;
030:            Border border2;
031:
032:            Class lexerClass;
033:
034:            public LexerFrame(Class lexerClass, Class tokenTypesClass) {
035:                super ("Token Steam Viewer");
036:                this .lexerClass = lexerClass;
037:                try {
038:                    jbInit();
039:                    setSize(500, 500);
040:                    listTokens(tokenTypesClass);
041:
042:                    final JPopupMenu popup = new JPopupMenu();
043:                    popup.add(loadFileAction);
044:
045:                    jbutton.setSize(30, 30);
046:                    jbutton.addMouseListener(new MouseAdapter() {
047:                        public void mouseReleased(MouseEvent e) {
048:                            //if(e.isPopupTrigger())
049:                            popup.show(scriptPane, e.getX(), e.getY());
050:                        }
051:                    });
052:                    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
053:                } catch (Exception e) {
054:                    e.printStackTrace();
055:                }
056:            }
057:
058:            Hashtable tokens = new Hashtable();
059:
060:            private void listTokens(Class tokenTypes) throws Exception {
061:                Field field[] = tokenTypes.getDeclaredFields();
062:                for (int i = 0; i < field.length; i++)
063:                    tokens.put(field[i].get(null), field[i].getName());
064:            }
065:
066:            public void actionPerformed(ActionEvent ae) {
067:                Token token = (Token) ((JComponent) ae.getSource())
068:                        .getClientProperty("token");
069:                if (token.getType() == Token.EOF_TYPE) {
070:                    scriptPane.select(0, 0);
071:                    return;
072:                }
073:                try {
074:                    int start = scriptPane
075:                            .getLineStartOffset(token.getLine() - 1)
076:                            + token.getColumn() - 1;
077:                    scriptPane.select(start, start + token.getText().length());
078:                    scriptPane.requestFocus();
079:                } catch (BadLocationException ex) {
080:                }
081:            }
082:
083:            private Action loadFileAction = new AbstractAction("Open File...") {
084:                public void actionPerformed(ActionEvent ae) {
085:                    JFileChooser jfc = new JFileChooser();
086:                    int response = jfc.showOpenDialog(LexerFrame.this );
087:                    if (response != JFileChooser.APPROVE_OPTION)
088:                        return;
089:                    try {
090:                        scanScript(jfc.getSelectedFile());
091:                    } catch (Exception ex) {
092:                        ex.printStackTrace();
093:                    }
094:                }
095:            };
096:
097:            private void scanScript(File file) throws Exception {
098:                scriptPane.read(new FileReader(file), null);
099:
100:                // create lexer
101:                Constructor constructor = lexerClass
102:                        .getConstructor(new Class[] { InputStream.class });
103:                CharScanner lexer = (CharScanner) constructor
104:                        .newInstance(new Object[] { new FileInputStream(file) });
105:
106:                tokenPane.setEditable(true);
107:                tokenPane.setText("");
108:
109:                int line = 1;
110:                ButtonGroup bg = new ButtonGroup();
111:                Token token = null;
112:
113:                while (true) {
114:                    token = lexer.nextToken();
115:                    JToggleButton tokenButton = new JToggleButton(
116:                            (String) tokens.get(new Integer(token.getType())));
117:                    bg.add(tokenButton);
118:                    tokenButton.addActionListener(this );
119:                    tokenButton.setToolTipText(token.getText());
120:                    tokenButton.putClientProperty("token", token);
121:                    tokenButton.setMargin(new Insets(0, 1, 0, 1));
122:                    tokenButton.setFocusPainted(false);
123:                    if (token.getLine() > line) {
124:                        tokenPane.getDocument()
125:                                .insertString(
126:                                        tokenPane.getDocument().getLength(),
127:                                        "\n", null);
128:                        line = token.getLine();
129:                    }
130:                    insertComponent(tokenButton);
131:                    if (token.getType() == Token.EOF_TYPE)
132:                        break;
133:                }
134:
135:                tokenPane.setEditable(false);
136:                tokenPane.setCaretPosition(0);
137:            }
138:
139:            private void insertComponent(JComponent comp) {
140:                try {
141:                    tokenPane.getDocument().insertString(
142:                            tokenPane.getDocument().getLength(), " ", null);
143:                } catch (BadLocationException ex1) {
144:                }
145:                try {
146:                    tokenPane.setCaretPosition(tokenPane.getDocument()
147:                            .getLength() - 1);
148:                } catch (Exception ex) {
149:                    tokenPane.setCaretPosition(0);
150:                }
151:                tokenPane.insertComponent(comp);
152:            }
153:
154:            private void jbInit() throws Exception {
155:                border1 = BorderFactory.createEmptyBorder();
156:                border2 = BorderFactory.createEmptyBorder();
157:                jSplitPane1.setOrientation(JSplitPane.VERTICAL_SPLIT);
158:                tokenPane.setEditable(false);
159:                tokenPane.setText("");
160:                scriptPane.setFont(new java.awt.Font("DialogInput", 0, 12));
161:                scriptPane.setEditable(false);
162:                scriptPane.setMargin(new Insets(5, 5, 5, 5));
163:                scriptPane.setText("");
164:                jScrollPane1.setBorder(border1);
165:                jScrollPane2.setBorder(border1);
166:                jSplitPane1.setMinimumSize(new Dimension(800, 600));
167:                mainPanel.add(jSplitPane1, BorderLayout.CENTER);
168:                mainPanel.add(jbutton, BorderLayout.NORTH);
169:                this .getContentPane().add(mainPanel);
170:                jSplitPane1.add(jScrollPane1, JSplitPane.LEFT);
171:                jScrollPane1.getViewport().add(tokenPane, null);
172:                jSplitPane1.add(jScrollPane2, JSplitPane.RIGHT);
173:                jScrollPane2.getViewport().add(scriptPane, null);
174:
175:                jScrollPane1.setColumnHeaderView(new JLabel(" Token Stream:"));
176:                jScrollPane2.setColumnHeaderView(new JLabel(" Input Script:"));
177:                jSplitPane1.setResizeWeight(0.5);
178:            }
179:
180:            public static void main(String[] args) throws Exception {
181:                try {
182:                    UIManager.setLookAndFeel(UIManager
183:                            .getSystemLookAndFeelClassName());
184:                } catch (Exception ignore) {
185:                }
186:                new LexerFrame(GroovyLexer.class, GroovyTokenTypes.class)
187:                        .setVisible(true);
188:            }
189:        }
190:
191:        class HScrollableTextPane extends JTextPane {
192:            public boolean getScrollableTracksViewportWidth() {
193:                return (getSize().width < getParent().getSize().width);
194:            }
195:
196:            public void setSize(Dimension d) {
197:                if (d.width < getParent().getSize().width) {
198:                    d.width = getParent().getSize().width;
199:                }
200:                super.setSize(d);
201:            }
202:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.