Changeset 77
- Timestamp:
- 07/01/08 16:11:31 (6 months ago)
- Files:
-
- axdt/trunk/org.axdt.as3/src/org/axdt/as3/templates/AS3TemplateCompletionProcessor.java (modified) (4 diffs)
- axdt/trunk/org.axdt.as3/src/org/axdt/as3/templates/AS3TemplateContext.java (added)
- axdt/trunk/org.axdt.as3/src/org/axdt/as3/wizards/NewAs3Wizard.java (modified) (6 diffs)
- axdt/trunk/org.axdt.as3/src/org/axdt/as3/wizards/NewAs3WizardPage.java (modified) (5 diffs)
- axdt/trunk/org.axdt.as3/templates/as3default-templates.xml (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
axdt/trunk/org.axdt.as3/src/org/axdt/as3/templates/AS3TemplateCompletionProcessor.java
r76 r77 5 5 import java.util.Comparator; 6 6 import java.util.List; 7 import java.util.StringTokenizer;8 7 9 8 import org.axdt.as3.AS3Plugin; … … 25 24 public class AS3TemplateCompletionProcessor extends TemplateCompletionProcessor { 26 25 26 protected TemplateContext createContext(ITextViewer viewer, IRegion region) { 27 TemplateContextType contextType= getContextType(viewer, region); 28 if (contextType != null) { 29 IDocument document= viewer.getDocument(); 30 return new AS3TemplateContext(contextType, document, region.getOffset(), region.getLength()); 31 } 32 return null; 33 } 34 27 35 @Override 28 protected TemplateContextType getContextType(ITextViewer viewer, 29 IRegion region) { 36 protected TemplateContextType getContextType(ITextViewer viewer, IRegion region) { 30 37 return AS3Plugin.getInstance().getCotextTypeRegistry().getContextType(AS3ContextType.CONTEXT_TYPE_ID); 31 38 } … … 74 81 continue; 75 82 } 83 if (template.getName().equals("package")) { 84 IDocument document = viewer.getDocument(); 85 if (document.getNumberOfLines()>5) 86 continue; 87 try { 88 if (document.get(0, document.getLength()).contains("package")) 89 continue; 90 } catch (BadLocationException e) { 91 } 92 } 76 93 if (template.matches(prefix, context.getContextType().getId()) 77 94 && template.getName().startsWith(prefix)) { 78 template = correctIndention(template,viewer,offset);79 95 matches.add((TemplateProposal)createProposal(template, context, (IRegion) region, getRelevance(template, prefix))); 80 96 } … … 89 105 } 90 106 91 private Template correctIndention(Template template, ITextViewer viewer, int offset) {92 IDocument d = viewer.getDocument();93 if (offset == -1 || d.getLength() == 0)94 return template;95 String pattern = template.getPattern();96 String delim = d.getLegalLineDelimiters()[0];97 for (String nl:d.getLegalLineDelimiters()) if (nl.equals("\n")) delim = nl;98 if (pattern.indexOf('\n')<0&&pattern.indexOf('\r')<0)99 return template;100 try {101 int p= (offset == d.getLength() ? offset - 1 : offset);102 IRegion info= d.getLineInformationOfOffset(p);103 int start= info.getOffset();104 // find white spaces105 int end= findEndOfWhiteSpace(d, start, offset);106 StringBuffer buf= new StringBuffer(delim);107 if (end > start) {108 // append to input109 buf.append(d.get(start, end - start));110 }111 String indent= buf.toString();112 buf = new StringBuffer();113 StringTokenizer tokenizer = new StringTokenizer(pattern, "\r\n\t", true);114 char lastnl = 0;115 while (tokenizer.hasMoreTokens()) {116 String token = tokenizer.nextToken();117 char c = token.charAt(0);118 if (c == '\n'||c == '\r') {119 if (lastnl == 0 || lastnl == c)120 buf.append(indent);121 lastnl = c;122 continue;123 }124 lastnl = 0;125 // TODO: tab2space preference126 if (token.charAt(0) == '\t')127 buf.append(" ");128 else129 buf.append(token);130 }131 return new Template(template.getName(), template.getDescription(), template.getContextTypeId(), buf.toString(), template.isAutoInsertable());132 } catch (BadLocationException e) {133 }134 return template;135 }136 137 private int findEndOfWhiteSpace(IDocument document, int offset, int end) throws BadLocationException {138 while (offset < end) {139 char c= document.getChar(offset);140 if (c != ' ' && c != '\t') {141 return offset;142 }143 offset++;144 }145 return end;146 }147 148 107 } axdt/trunk/org.axdt.as3/src/org/axdt/as3/wizards/NewAs3Wizard.java
r75 r77 5 5 import java.io.InputStream; 6 6 import java.lang.reflect.InvocationTargetException; 7 import java.util.StringTokenizer; 7 8 9 import org.axdt.as3.AS3Plugin; 10 import org.axdt.as3.templates.AS3ContextType; 11 import org.axdt.as3.templates.AS3TemplateContext; 8 12 import org.axdt.as3.util.AS3PreferenceUtil; 9 13 import org.eclipse.core.resources.IContainer; … … 18 22 import org.eclipse.core.runtime.Path; 19 23 import org.eclipse.jface.operation.IRunnableWithProgress; 24 import org.eclipse.jface.text.Document; 25 import org.eclipse.jface.text.templates.Template; 26 import org.eclipse.jface.text.templates.TemplateBuffer; 27 import org.eclipse.jface.text.templates.TemplateContextType; 28 import org.eclipse.jface.text.templates.persistence.TemplateStore; 20 29 import org.eclipse.ui.IWorkbenchWindow; 21 30 import org.eclipse.ui.PartInitException; … … 38 47 final String containerName = page.getContainerName(); 39 48 final String fileName = page.getFileName(); 49 final String template = page.getTemplate(); 40 50 return new IRunnableWithProgress() { 41 51 public void run(IProgressMonitor monitor) throws InvocationTargetException { 42 52 try { 43 doFinish(containerName, fileName, monitor);53 doFinish(containerName, fileName, template, monitor); 44 54 } catch (CoreException e) { 45 55 throw new InvocationTargetException(e); … … 52 62 53 63 protected void doFinish(String containerName, String fileString, 54 IProgressMonitor monitor) throws CoreException {64 String template, IProgressMonitor monitor) throws CoreException { 55 65 String fileName = fileString + ".as"; 56 66 monitor.beginTask("Creating " + fileName, 2); … … 73 83 final IFile file = container.getFile(new Path(fileName)); 74 84 try { 75 InputStream stream = openContentStream(fileString,packageName );85 InputStream stream = openContentStream(fileString,packageName, template); 76 86 if (file.exists()) { 77 87 file.setContents(stream, true, true, monitor); … … 115 125 } 116 126 117 private InputStream openContentStream(String typeName, String packageName) { 118 String contents = 119 "package "+ packageName +"{\n" 120 +"\tpublic class "+ typeName +" {\n" 121 +"\t\t\n" 122 +"\t}\n}"; 127 private InputStream openContentStream(String typeName, String packageName, String tempType) { 128 String contents = ""; 129 AS3Plugin plugin = AS3Plugin.getInstance(); 130 TemplateStore templateStore = plugin.getTemplateStore(); 131 TemplateContextType contextType = plugin.getCotextTypeRegistry().getContextType(AS3ContextType.CONTEXT_TYPE_ID); 132 AS3TemplateContext context = new AS3TemplateContext(contextType, new Document(" "), 0, 0); 133 try { 134 StringBuffer buf = new StringBuffer(""); 135 if (!tempType.equals("Simple")) { 136 String pattern; 137 if (tempType.equals("Example")) { 138 pattern = "import flash.display.*;\nimport flash.text.*;\n" 139 +"public class ${type_name} extends Sprite {\n" 140 +"\tpublic function ${type_name}() {\n" 141 +"\t\tvar txt:TextField = new TextField();\n" 142 +"\t\ttxt.width = 400;\n" 143 +"\t\ttxt.height = 400;\n" 144 +"\t\ttxt.multiline = true;\n" 145 +"\t\ttxt.wordWrap = true;\n" 146 +"\t\ttxt.defaultTextFormat = new TextFormat(\"_sans\", 40, 0xff9900);\n" 147 +"\t\ttxt.htmlText = '<p><b>Hello ${user}!</b></p>'\n" 148 +"\t\t\t+ '<p><i>Please</i> contribute some of your time to '\n" 149 +"\t\t\t+ '<u><b><a href=\"http://axdt.org\">AXDT</a></b></u>!</p>'\n" 150 +"\t\t\t+ '<p>Have fun!</p>';\n" 151 +"\t\taddChild(txt);\n\t}\n}"; 152 } else { 153 Template t1 = templateStore.findTemplateById("org.axdt.as3.templates.new"+tempType); 154 buf.append("public "); 155 pattern = t1.getPattern(); 156 } 157 StringTokenizer tokenizer = new StringTokenizer(pattern, "\r\n", true); 158 if (tokenizer.hasMoreElements()) buf.append(tokenizer.nextToken()); 159 while (tokenizer.hasMoreTokens()) buf.append("\t"+tokenizer.nextToken()); 160 } 161 Template t = templateStore.findTemplateById("org.axdt.as3.templates.newPackage"); 162 Template template = new Template(t.getName(),t.getDescription(),t.getContextTypeId(), 163 t.getPattern().replace("${cursor}", buf.toString()) 164 .replace("${package_name}", packageName) 165 .replace("${type_name}", typeName) 166 ,false); 167 TemplateBuffer templateBuffer = context.evaluate(template); 168 contents = templateBuffer.getString(); 169 } catch (Exception e1) { 170 contents = "package "+ packageName +"{\n\t// template error \n}"; 171 } 123 172 return new ByteArrayInputStream(contents.getBytes()); 124 173 } axdt/trunk/org.axdt.as3/src/org/axdt/as3/wizards/NewAs3WizardPage.java
r75 r77 16 16 import org.eclipse.swt.layout.GridData; 17 17 import org.eclipse.swt.layout.GridLayout; 18 import org.eclipse.swt.layout.RowLayout; 18 19 import org.eclipse.swt.widgets.Button; 19 20 import org.eclipse.swt.widgets.Composite; … … 27 28 private Text containerText; 28 29 private Text fileText; 30 protected String template = "Class"; 29 31 30 32 public NewAs3WizardPage(ISelection selection) { … … 46 48 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 47 49 containerText.setLayoutData(gd); 48 containerText.addModifyListener(new ModifyListener(){50 ModifyListener modifyListener = new ModifyListener(){ 49 51 public void modifyText(ModifyEvent e) { 50 52 dialogChanged(); 51 53 } 52 }); 54 }; 55 containerText.addModifyListener(modifyListener); 53 56 54 57 Button button = new Button(container, SWT.PUSH); … … 65 68 gd = new GridData(GridData.FILL_HORIZONTAL); 66 69 fileText.setLayoutData(gd); 67 fileText.addModifyListener(new ModifyListener() { 68 public void modifyText(ModifyEvent e) { 69 dialogChanged(); 70 fileText.addModifyListener(modifyListener); 71 new Label(container, SWT.NULL); 72 label = new Label(container, SWT.NULL); 73 label.setText("&Template:"); 74 75 SelectionAdapter adapter = new SelectionAdapter() { 76 public void widgetSelected(SelectionEvent e) { 77 if (e.widget instanceof Button) { 78 Button b = (Button) e.widget; 79 template = b.getText(); 80 } 70 81 } 71 }); 82 }; 83 84 Composite group = new Composite(container,SWT.NONE); 85 group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 86 group.setLayout(new RowLayout()); 87 button = new Button(group, SWT.RADIO); 88 button.setText("Simple"); 89 button.addSelectionListener(adapter); 90 button = new Button(group, SWT.RADIO); 91 button.setText("Class"); 92 button.setSelection(true); 93 button.addSelectionListener(adapter); 94 button = new Button(group, SWT.RADIO); 95 button.setText("Interface"); 96 button.addSelectionListener(adapter); 97 button = new Button(group, SWT.RADIO); 98 button.setText("Example"); 99 button.addSelectionListener(adapter); 100 72 101 initialize(); 73 102 dialogChanged(); … … 160 189 return fileText.getText(); 161 190 } 191 public String getTemplate() { 192 return template; 193 } 162 194 } axdt/trunk/org.axdt.as3/templates/as3default-templates.xml
r76 r77 165 165 }</template> 166 166 <template name="interface" 167 description="new class"167 description="new interface" 168 168 id="org.axdt.as3.templates.newInterface" 169 169 context="org.axdt.as3.templates.as3" enabled="true"
