Changeset 77

Show
Ignore:
Timestamp:
07/01/08 16:11:31 (6 months ago)
Author:
mb0
Message:

added templates to as3 file wizard

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • axdt/trunk/org.axdt.as3/src/org/axdt/as3/templates/AS3TemplateCompletionProcessor.java

    r76 r77  
    55import java.util.Comparator; 
    66import java.util.List; 
    7 import java.util.StringTokenizer; 
    87 
    98import org.axdt.as3.AS3Plugin; 
     
    2524public class AS3TemplateCompletionProcessor extends TemplateCompletionProcessor { 
    2625 
     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 
    2735        @Override 
    28         protected TemplateContextType getContextType(ITextViewer viewer, 
    29                         IRegion region) { 
     36        protected TemplateContextType getContextType(ITextViewer viewer, IRegion region) { 
    3037                return AS3Plugin.getInstance().getCotextTypeRegistry().getContextType(AS3ContextType.CONTEXT_TYPE_ID); 
    3138        } 
     
    7481                                continue; 
    7582                        } 
     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                        } 
    7693                        if (template.matches(prefix, context.getContextType().getId()) 
    7794                                && template.getName().startsWith(prefix)) { 
    78                                         template = correctIndention(template,viewer,offset); 
    7995                                        matches.add((TemplateProposal)createProposal(template, context, (IRegion) region, getRelevance(template, prefix))); 
    8096                        } 
     
    89105        } 
    90106 
    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 spaces 
    105                         int end= findEndOfWhiteSpace(d, start, offset); 
    106                         StringBuffer buf= new StringBuffer(delim); 
    107                         if (end > start) { 
    108                                 // append to input 
    109                                 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 preference 
    126                                 if (token.charAt(0) == '\t')  
    127                                         buf.append("    "); 
    128                                 else 
    129                                         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  
    148107} 
  • axdt/trunk/org.axdt.as3/src/org/axdt/as3/wizards/NewAs3Wizard.java

    r75 r77  
    55import java.io.InputStream; 
    66import java.lang.reflect.InvocationTargetException; 
     7import java.util.StringTokenizer; 
    78 
     9import org.axdt.as3.AS3Plugin; 
     10import org.axdt.as3.templates.AS3ContextType; 
     11import org.axdt.as3.templates.AS3TemplateContext; 
    812import org.axdt.as3.util.AS3PreferenceUtil; 
    913import org.eclipse.core.resources.IContainer; 
     
    1822import org.eclipse.core.runtime.Path; 
    1923import org.eclipse.jface.operation.IRunnableWithProgress; 
     24import org.eclipse.jface.text.Document; 
     25import org.eclipse.jface.text.templates.Template; 
     26import org.eclipse.jface.text.templates.TemplateBuffer; 
     27import org.eclipse.jface.text.templates.TemplateContextType; 
     28import org.eclipse.jface.text.templates.persistence.TemplateStore; 
    2029import org.eclipse.ui.IWorkbenchWindow; 
    2130import org.eclipse.ui.PartInitException; 
     
    3847                final String containerName = page.getContainerName(); 
    3948                final String fileName = page.getFileName(); 
     49                final String template = page.getTemplate(); 
    4050                return new IRunnableWithProgress() { 
    4151                        public void run(IProgressMonitor monitor) throws InvocationTargetException { 
    4252                                try { 
    43                                         doFinish(containerName, fileName, monitor); 
     53                                        doFinish(containerName, fileName, template, monitor); 
    4454                                } catch (CoreException e) { 
    4555                                        throw new InvocationTargetException(e); 
     
    5262 
    5363        protected void doFinish(String containerName, String fileString, 
    54                         IProgressMonitor monitor) throws CoreException { 
     64                        String template, IProgressMonitor monitor) throws CoreException { 
    5565                String fileName = fileString + ".as"; 
    5666                monitor.beginTask("Creating " + fileName, 2); 
     
    7383                final IFile file = container.getFile(new Path(fileName)); 
    7484                try { 
    75                         InputStream stream = openContentStream(fileString,packageName); 
     85                        InputStream stream = openContentStream(fileString,packageName, template); 
    7686                        if (file.exists()) { 
    7787                                file.setContents(stream, true, true, monitor); 
     
    115125        } 
    116126 
    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                } 
    123172                return new ByteArrayInputStream(contents.getBytes()); 
    124173        } 
  • axdt/trunk/org.axdt.as3/src/org/axdt/as3/wizards/NewAs3WizardPage.java

    r75 r77  
    1616import org.eclipse.swt.layout.GridData; 
    1717import org.eclipse.swt.layout.GridLayout; 
     18import org.eclipse.swt.layout.RowLayout; 
    1819import org.eclipse.swt.widgets.Button; 
    1920import org.eclipse.swt.widgets.Composite; 
     
    2728        private Text containerText; 
    2829        private Text fileText; 
     30        protected String template = "Class"; 
    2931 
    3032        public NewAs3WizardPage(ISelection selection) { 
     
    4648                GridData gd = new GridData(GridData.FILL_HORIZONTAL); 
    4749                containerText.setLayoutData(gd); 
    48                 containerText.addModifyListener(new ModifyListener(){ 
     50                ModifyListener modifyListener = new ModifyListener(){ 
    4951                        public void modifyText(ModifyEvent e) { 
    5052                                dialogChanged(); 
    5153                        } 
    52                 }); 
     54                }; 
     55                containerText.addModifyListener(modifyListener); 
    5356                 
    5457                Button button = new Button(container, SWT.PUSH); 
     
    6568                gd = new GridData(GridData.FILL_HORIZONTAL); 
    6669                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                                } 
    7081                        } 
    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                 
    72101                initialize(); 
    73102                dialogChanged(); 
     
    160189                return fileText.getText(); 
    161190        } 
     191        public String getTemplate() { 
     192                return template; 
     193        } 
    162194} 
  • axdt/trunk/org.axdt.as3/templates/as3default-templates.xml

    r76 r77  
    165165}</template> 
    166166<template name="interface"  
    167         description="new class"  
     167        description="new interface"  
    168168        id="org.axdt.as3.templates.newInterface"  
    169169        context="org.axdt.as3.templates.as3" enabled="true"