Source Code Cross Referenced for TemplateEngine.java in  » IDE-Eclipse » jdt » org » eclipse » jdt » internal » ui » text » template » contentassist » 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 » IDE Eclipse » jdt » org.eclipse.jdt.internal.ui.text.template.contentassist 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*******************************************************************************
002:         * Copyright (c) 2000, 2007 IBM Corporation and others.
003:         * All rights reserved. This program and the accompanying materials
004:         * are made available under the terms of the Eclipse Public License v1.0
005:         * which accompanies this distribution, and is available at
006:         * http://www.eclipse.org/legal/epl-v10.html
007:         *
008:         * Contributors:
009:         *     IBM Corporation - initial API and implementation
010:         *******************************************************************************/package org.eclipse.jdt.internal.ui.text.template.contentassist;
011:
012:        import java.util.ArrayList;
013:        import java.util.HashMap;
014:        import java.util.Iterator;
015:        import java.util.Map;
016:        import java.util.Map.Entry;
017:
018:        import org.eclipse.core.runtime.Assert;
019:
020:        import org.eclipse.swt.graphics.Image;
021:        import org.eclipse.swt.graphics.Point;
022:
023:        import org.eclipse.jface.text.BadLocationException;
024:        import org.eclipse.jface.text.IDocument;
025:        import org.eclipse.jface.text.IRegion;
026:        import org.eclipse.jface.text.ITextViewer;
027:        import org.eclipse.jface.text.Position;
028:        import org.eclipse.jface.text.Region;
029:        import org.eclipse.jface.text.templates.GlobalTemplateVariables;
030:        import org.eclipse.jface.text.templates.Template;
031:        import org.eclipse.jface.text.templates.TemplateContextType;
032:
033:        import org.eclipse.jdt.core.ICompilationUnit;
034:
035:        import org.eclipse.jdt.internal.corext.template.java.CompilationUnitContext;
036:        import org.eclipse.jdt.internal.corext.template.java.CompilationUnitContextType;
037:        import org.eclipse.jdt.internal.corext.template.java.SWTContextType;
038:
039:        import org.eclipse.jdt.internal.ui.JavaPlugin;
040:        import org.eclipse.jdt.internal.ui.JavaPluginImages;
041:
042:        public class TemplateEngine {
043:
044:            private static final String $_LINE_SELECTION = "${" + GlobalTemplateVariables.LineSelection.NAME + "}"; //$NON-NLS-1$ //$NON-NLS-2$
045:            private static final String $_WORD_SELECTION = "${" + GlobalTemplateVariables.WordSelection.NAME + "}"; //$NON-NLS-1$ //$NON-NLS-2$
046:
047:            /** The context type. */
048:            private TemplateContextType fContextType;
049:            /** The result proposals. */
050:            private ArrayList fProposals = new ArrayList();
051:            /** Positions created on the key documents to remove in reset. */
052:            private final Map fPositions = new HashMap();
053:
054:            /**
055:             * Creates the template engine for the given <code>contextType</code>.
056:             * <p>
057:             * The <code>JavaPlugin.getDefault().getTemplateContextRegistry()</code>
058:             * defines the supported context types.</p>
059:             *
060:             * @param contextType the context type 
061:             */
062:            public TemplateEngine(TemplateContextType contextType) {
063:                Assert.isNotNull(contextType);
064:                fContextType = contextType;
065:            }
066:
067:            /**
068:             * Empties the collector.
069:             */
070:            public void reset() {
071:                fProposals.clear();
072:                for (Iterator it = fPositions.entrySet().iterator(); it
073:                        .hasNext();) {
074:                    Entry entry = (Entry) it.next();
075:                    IDocument doc = (IDocument) entry.getKey();
076:                    Position position = (Position) entry.getValue();
077:                    doc.removePosition(position);
078:                }
079:                fPositions.clear();
080:            }
081:
082:            /**
083:             * Returns the array of matching templates.
084:             * 
085:             * @return the template proposals
086:             */
087:            public TemplateProposal[] getResults() {
088:                return (TemplateProposal[]) fProposals
089:                        .toArray(new TemplateProposal[fProposals.size()]);
090:            }
091:
092:            /**
093:             * Inspects the context of the compilation unit around <code>completionPosition</code>
094:             * and feeds the collector with proposals.
095:             * @param viewer the text viewer
096:             * @param completionPosition the context position in the document of the text viewer
097:             * @param compilationUnit the compilation unit (may be <code>null</code>)
098:             */
099:            public void complete(ITextViewer viewer, int completionPosition,
100:                    ICompilationUnit compilationUnit) {
101:                IDocument document = viewer.getDocument();
102:
103:                if (!(fContextType instanceof  CompilationUnitContextType))
104:                    return;
105:
106:                Point selection = viewer.getSelectedRange();
107:                Position position = new Position(completionPosition,
108:                        selection.y);
109:
110:                // remember selected text
111:                String selectedText = null;
112:                if (selection.y != 0) {
113:                    try {
114:                        selectedText = document.get(selection.x, selection.y);
115:                        document.addPosition(position);
116:                        fPositions.put(document, position);
117:                    } catch (BadLocationException e) {
118:                    }
119:                }
120:
121:                CompilationUnitContext context = ((CompilationUnitContextType) fContextType)
122:                        .createContext(document, position, compilationUnit);
123:                context.setVariable("selection", selectedText); //$NON-NLS-1$
124:                int start = context.getStart();
125:                int end = context.getEnd();
126:                IRegion region = new Region(start, end - start);
127:
128:                Template[] templates = JavaPlugin.getDefault()
129:                        .getTemplateStore().getTemplates();
130:
131:                if (selection.y == 0) {
132:                    for (int i = 0; i != templates.length; i++)
133:                        if (context.canEvaluate(templates[i]))
134:                            fProposals.add(new TemplateProposal(templates[i],
135:                                    context, region, getImage()));
136:
137:                } else {
138:
139:                    if (context.getKey().length() == 0)
140:                        context.setForceEvaluation(true);
141:
142:                    boolean multipleLinesSelected = areMultipleLinesSelected(viewer);
143:
144:                    for (int i = 0; i != templates.length; i++) {
145:                        Template template = templates[i];
146:                        if (context.canEvaluate(template)
147:                                && template.getContextTypeId().equals(
148:                                        context.getContextType().getId())
149:                                && (!multipleLinesSelected
150:                                        && template.getPattern().indexOf(
151:                                                $_WORD_SELECTION) != -1 || (multipleLinesSelected && template
152:                                        .getPattern().indexOf($_LINE_SELECTION) != -1))) {
153:                            fProposals.add(new TemplateProposal(templates[i],
154:                                    context, region, getImage()));
155:                        }
156:                    }
157:                }
158:            }
159:
160:            private Image getImage() {
161:                if (SWTContextType.ID.equals(fContextType.getId()))
162:                    return JavaPluginImages
163:                            .get(JavaPluginImages.IMG_OBJS_SWT_TEMPLATE);
164:                else
165:                    return JavaPluginImages
166:                            .get(JavaPluginImages.IMG_OBJS_TEMPLATE);
167:            }
168:
169:            /**
170:             * Returns <code>true</code> if one line is completely selected or if multiple lines are selected.
171:             * Being completely selected means that all characters except the new line characters are
172:             * selected.
173:             *
174:             * @param viewer the text viewer 
175:             * @return <code>true</code> if one or multiple lines are selected
176:             * @since 2.1
177:             */
178:            private boolean areMultipleLinesSelected(ITextViewer viewer) {
179:                if (viewer == null)
180:                    return false;
181:
182:                Point s = viewer.getSelectedRange();
183:                if (s.y == 0)
184:                    return false;
185:
186:                try {
187:
188:                    IDocument document = viewer.getDocument();
189:                    int startLine = document.getLineOfOffset(s.x);
190:                    int endLine = document.getLineOfOffset(s.x + s.y);
191:                    IRegion line = document.getLineInformation(startLine);
192:                    return startLine != endLine
193:                            || (s.x == line.getOffset() && s.y == line
194:                                    .getLength());
195:
196:                } catch (BadLocationException x) {
197:                    return false;
198:                }
199:            }
200:        }
w___ww_._j__a_v_a2___s.c__o___m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.