Index: LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/ui/editor/TemplatedJavaSourceViewerConfiguration.java diff -u /dev/null LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/ui/editor/TemplatedJavaSourceViewerConfiguration.java:1.1 --- /dev/null Wed Jun 20 12:40:40 2007 +++ LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/ui/editor/TemplatedJavaSourceViewerConfiguration.java Wed Jun 20 12:40:35 2007 @@ -0,0 +1,152 @@ +/* *##% + * Copyright (C) 2007 Code Lutin + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +package org.codelutin.eclipse.generator.ui.editor; + +import org.codelutin.eclipse.generator.LutinGeneratorPlugin; +import org.eclipse.jdt.ui.text.IColorManager; +import org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration; +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.jface.text.TextAttribute; +import org.eclipse.jface.text.presentation.IPresentationReconciler; +import org.eclipse.jface.text.presentation.PresentationReconciler; +import org.eclipse.jface.text.rules.BufferedRuleBasedScanner; +import org.eclipse.jface.text.rules.DefaultDamagerRepairer; +import org.eclipse.jface.text.rules.Token; +import org.eclipse.jface.text.source.ISourceViewer; + +/** + * TemplateSourceViewerConfiguration + * + * Configuration de l'editeur + * + * @author chatellier + * @version $Revision: 1.1 $ + * + * Last update : $Date: 2007/06/20 12:40:35 $ + * By : $Author: chatellier $ + */ +public class TemplatedJavaSourceViewerConfiguration extends + JavaSourceViewerConfiguration { + + /** + * Constructor. + * + * @param colorManager + * colorManager + * @param preferenceStore + * preferenceStore + * @param editor + * editor + * @param java_partitioning + * java_partitioning + * + * @see org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration + * @see org.eclipse.jdt.ui.text.IColorManager; + * @see org.eclipse.jface.preference.IPreferenceStore + */ + public TemplatedJavaSourceViewerConfiguration(IColorManager colorManager, + IPreferenceStore preferenceStore, TemplatedJavaEditor editor, + String java_partitioning) { + super(colorManager, preferenceStore, editor, java_partitioning); + } + + /** + * Classe utilisée lorsque l'on ne désire pas affiner la coloration + * syntaxique (ie. toute la partition de la même couleur). Utilisée pour les + * partitions de templates + * + * TODO unicolor pour l'instant, peut etre colore suivant le type par la + * suite + */ + static class SingleTokenScanner extends BufferedRuleBasedScanner { + + /** + * Constructeur. Définit le token renvoyé. + * + * @param attribute + * Attribut du Token. + */ + public SingleTokenScanner(TextAttribute attribute) { + setDefaultReturnToken(new Token(attribute)); + } + } + + /** + * Met en place la coloration syntaxique. Attribue des damagers et des + * repairers à chaque type de partition. + * + * Recupere les damagers et repaires Java et ajoute ceux de gestion des + * templates. + * + * @param sourceViewer + * SourceViewer pour lequel on configure le reconciler. + * @return Le reconciler à utiliser avec les damager et les repairers + * configurés. + * + * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getPresentationReconciler + */ + @Override + public IPresentationReconciler getPresentationReconciler( + ISourceViewer sourceViewer) { + TemplatedJavaTextAttributeProvider provider = LutinGeneratorPlugin.getDefault() + .getTextAttributeProvider(); + + // recupere celui de java par defaut + PresentationReconciler reconciler = (PresentationReconciler) super + .getPresentationReconciler(sourceViewer); + // change le type de document + reconciler + .setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer)); + + // Crée le damager/repairer pour les commentaires + DefaultDamagerRepairer dr = new DefaultDamagerRepairer( + //new SingleTokenScanner( + // provider + // .getAttribute(TemplatedJavaTextAttributeProvider.XXX_JSP_CODE)) + new TemplatedJavaCodeScanner() + ); + reconciler.setDamager(dr, + TemplatedJavaPartitions.TEMPLATED_JAVA_XXX_JSP); + reconciler.setRepairer(dr, + TemplatedJavaPartitions.TEMPLATED_JAVA_XXX_JSP); + + return reconciler; + } + + /** + * Partitions types + * + * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getConfiguredContentTypes(org.eclipse.jface.text.source.ISourceViewer) + */ + @Override + public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) { + return TemplatedJavaPartitions.TEMPLATED_JAVA_PARTITION_TYPES; + } + + + /** + * Document partionning. + * + * @see org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration#getConfiguredDocumentPartitioning(org.eclipse.jface.text.source.ISourceViewer) + */ + @Override + public String getConfiguredDocumentPartitioning(ISourceViewer sourceViewer) { + return TemplatedJavaPartitions.TEMPLATED_JAVA_PARTITIONNING; + } +} Index: LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/ui/editor/TemplatedJavaEditor.java diff -u /dev/null LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/ui/editor/TemplatedJavaEditor.java:1.1 --- /dev/null Wed Jun 20 12:40:40 2007 +++ LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/ui/editor/TemplatedJavaEditor.java Wed Jun 20 12:40:35 2007 @@ -0,0 +1,124 @@ +/* *##% + * Copyright (C) 2007 Code Lutin + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +package org.codelutin.eclipse.generator.ui.editor; + +import org.eclipse.jdt.internal.ui.JavaPlugin; +import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor; +import org.eclipse.jdt.ui.text.JavaTextTools; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.ui.IEditorInput; + +/** + * TemplatedJavaEditor + * + * Editeur de fichier java templatés. + * + * Ne traite que les fichier java partionné du type + * {@link TemplatedJavaPartitions.TEMPLATED_JAVA_PARTITIONNING}. + * + * @author chatellier + * @version $Revision: 1.1 $ + * + * Last update : $Date: 2007/06/20 12:40:35 $ + * By : $Author: chatellier $ + */ +public class TemplatedJavaEditor extends CompilationUnitEditor { + + /** + * Editor configuration + */ + private TemplatedJavaSourceViewerConfiguration svc; + + /** + * Constructor + */ + public TemplatedJavaEditor() { + super(); + setDocumentProvider(new TemplatedJavaDocumentProvider()); + } + + /** + * Build the configuration when needed + * @return a configuration + */ + public TemplatedJavaSourceViewerConfiguration getTemplatedJavaSourceViewerConfiguration() { + if(svc == null) { + // TODO voir s'il ya moyen de le pas utiliser de classe interne + JavaTextTools jTools = JavaPlugin.getDefault().getJavaTextTools(); + svc = new TemplatedJavaSourceViewerConfiguration( + jTools.getColorManager(), JavaPlugin.getDefault() + .getPreferenceStore(), this, + TemplatedJavaPartitions.TEMPLATED_JAVA_PARTITIONNING); + } + return svc; + } + + /** + * Méthode appelée lors de l'initialisation de l'éditeur. Positionne le + * SourceViewer (en charge de la coloration syntaxique, de la complétion, + * etc.). + * + * @see org.eclipse.ui.editors.text.TextEditor#initializeEditor() + */ + protected void initializeEditor() { + + super.initializeEditor(); + + setSourceViewerConfiguration(getTemplatedJavaSourceViewerConfiguration()); + } + + /** + * A redefinir + * + * Je ne sais pas trop pourquoi, createPartControl est appele + * avant que TemplatedJavaSourceViewerConfiguration soit initialisé, + * il est null est en creer un par defaut version "java" + * + * @see org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor#createPartControl(org.eclipse.swt.widgets.Composite) + */ + @Override + public void createPartControl(Composite parent) { + + // lui en donne un avant qu'il decide dans creer un par defaut + setSourceViewerConfiguration(getTemplatedJavaSourceViewerConfiguration()); + + // poursuit l'appel par defaut + super.createPartControl(parent); + } + + /* (non-Javadoc) + * @see org.eclipse.ui.texteditor.StatusTextEditor#updatePartControl(org.eclipse.ui.IEditorInput) + */ + @Override + public void updatePartControl(IEditorInput input) { + // TODO Auto-generated method stub + super.updatePartControl(input); + } + + /* (non-Javadoc) + * @see org.eclipse.jdt.internal.ui.javaeditor.JavaEditor#setPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) + */ + /*protected void setPreferenceStore(IPreferenceStore store) { + super.setPreferenceStore(store); + if (getSourceViewerConfiguration() instanceof TemplatedJavaSourceViewerConfiguration) { + JavaTextTools textTools= JavaPlugin.getDefault().getJavaTextTools(); + setSourceViewerConfiguration(new TemplatedJavaSourceViewerConfiguration(textTools.getColorManager(), store, this, TemplatedJavaPartitions.TEMPLATED_JAVA_PARTITIONNING)); + } + }*/ +} Index: LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/ui/editor/TemplatedJavaDocumentSetupParticipant.java diff -u /dev/null LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/ui/editor/TemplatedJavaDocumentSetupParticipant.java:1.1 --- /dev/null Wed Jun 20 12:40:40 2007 +++ LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/ui/editor/TemplatedJavaDocumentSetupParticipant.java Wed Jun 20 12:40:35 2007 @@ -0,0 +1,66 @@ +/* *##% + * Copyright (C) 2007 Code Lutin + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +package org.codelutin.eclipse.generator.ui.editor; + +import org.codelutin.eclipse.generator.LutinGeneratorPlugin; +import org.eclipse.core.filebuffers.IDocumentSetupParticipant; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IDocumentExtension3; +import org.eclipse.jface.text.IDocumentPartitioner; +import org.eclipse.jface.text.rules.FastPartitioner; + +/** + * TemplateDocumentSetupParticipant. + * + * Point d'entree du plugin. "Participe" au partitionnement des fichiers java. + * + * A ajouter en extention a "org.eclipse.filebuffers.documentSetup" + * dans le manifest.mf + * + * @author chatellier + * @version $Revision: 1.1 $ + * + * Last update : $Date: 2007/06/20 12:40:35 $ + * By : $Author: chatellier $ + */ +public class TemplatedJavaDocumentSetupParticipant implements IDocumentSetupParticipant { + + /* (non-Javadoc) + * @see org.eclipse.core.filebuffers.IDocumentSetupParticipant#setup(org.eclipse.jface.text.IDocument) + */ + public void setup(IDocument document) { + + // notion de partitionnement multiple + if(document instanceof IDocumentExtension3) { + + IDocumentExtension3 document3 = (IDocumentExtension3)document; + + IDocumentPartitioner partitioner = new FastPartitioner( + LutinGeneratorPlugin.getDefault().getTemplatePartitionScanner(), + TemplatedJavaPartitions.TEMPLATED_JAVA_PARTITION_TYPES + ); + + document3.setDocumentPartitioner(TemplatedJavaPartitions.TEMPLATED_JAVA_PARTITIONNING, partitioner); + + // attach the document to the partitionner + partitioner.connect(document); + } + } + +} Index: LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/ui/editor/TemplatedJavaDocumentProvider.java diff -u /dev/null LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/ui/editor/TemplatedJavaDocumentProvider.java:1.1 --- /dev/null Wed Jun 20 12:40:41 2007 +++ LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/ui/editor/TemplatedJavaDocumentProvider.java Wed Jun 20 12:40:35 2007 @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2000, 2006 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ + +package org.codelutin.eclipse.generator.ui.editor; + +import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitDocumentProvider; +import org.eclipse.ui.editors.text.ForwardingDocumentProvider; +import org.eclipse.ui.editors.text.TextFileDocumentProvider; +import org.eclipse.ui.texteditor.IDocumentProvider; + +/** + * CompilationUnitDocumentProvider + * + * Sans cette classe, l'editeur ne colore pas le document au demarrage + * juste a la modification du passage. + * + * Redefini les partition gere dans le document. + * + * @author chatellier + * @version $Revision: 1.1 $ + * + * Last update : $Date: 2007/06/20 12:40:35 $ + * By : $Author: chatellier $ + */ +public class TemplatedJavaDocumentProvider extends CompilationUnitDocumentProvider { + + public TemplatedJavaDocumentProvider() { + + super(); + + IDocumentProvider provider= new TextFileDocumentProvider(); + provider= new ForwardingDocumentProvider(TemplatedJavaPartitions.TEMPLATED_JAVA_PARTITIONNING, new TemplatedJavaDocumentSetupParticipant(), provider); + setParentDocumentProvider(provider); + } +} Index: LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/ui/editor/TemplatedJavaTextAttributeProvider.java diff -u /dev/null LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/ui/editor/TemplatedJavaTextAttributeProvider.java:1.1 --- /dev/null Wed Jun 20 12:40:41 2007 +++ LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/ui/editor/TemplatedJavaTextAttributeProvider.java Wed Jun 20 12:40:35 2007 @@ -0,0 +1,102 @@ +/* *##% + * Copyright (C) 2007 Code Lutin + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +package org.codelutin.eclipse.generator.ui.editor; + +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.jface.text.TextAttribute; +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.RGB; +import org.eclipse.swt.widgets.Display; + +/** + * TemplateTextAtributeProvider. + * + * Defini les couleurs de coloration des partitions + * + * @author chatellier + * @version $Revision: 1.1 $ + * + * Last update : $Date: 2007/06/20 12:40:35 $ By : $Author: chatellier $ + */ +public class TemplatedJavaTextAttributeProvider { + + /** + * Template Partition + */ + public static final String XXX_JSP_CODE = "__template_xxx_jsp_code_attribute"; + + /** + * Jsp tags + */ + public static final String JSP_TAG = "__template_jsp_tag_attribute"; + + /** + * Template specific tags + */ + public static final String TEMPLATE_TAG = "__template_template_tag_attribute"; + + /** + * Partition name -> Color attribute + */ + private Map fAttributes = new HashMap(); + + /** + * Contructor. + * + * Initialise Map + */ + public TemplatedJavaTextAttributeProvider() { + + //bleu clair pas gras + fAttributes.put(XXX_JSP_CODE, new TextAttribute( + new Color(Display.getCurrent(), new RGB(57, 125, 123)), // fore + new Color(Display.getCurrent(), new RGB(240, 255, 224)), // back + SWT.NONE // style + )); + // rouge clair + fAttributes.put(JSP_TAG, new TextAttribute( + new Color(Display.getCurrent(), new RGB(189, 93, 57)), // fore + null, // back + SWT.BOLD // style + )); + // bleu clair gras + fAttributes.put(TEMPLATE_TAG, new TextAttribute( + new Color(Display.getCurrent(), new RGB(57, 125, 123)), // fore + new Color(Display.getCurrent(), new RGB(240, 255, 224)), // back + SWT.BOLD // style + )); + } + + /** + * Accessor + * + * @param type partition type + * @return color attribute + */ + public TextAttribute getAttribute(String type) { + TextAttribute attr = (TextAttribute) fAttributes.get(type); + if (attr == null) { + attr = (TextAttribute) fAttributes.get(XXX_JSP_CODE); + } + return attr; + } +} Index: LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/ui/editor/FastTemplatedJavaPartitionScanner.java diff -u /dev/null LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/ui/editor/FastTemplatedJavaPartitionScanner.java:1.1 --- /dev/null Wed Jun 20 12:40:41 2007 +++ LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/ui/editor/FastTemplatedJavaPartitionScanner.java Wed Jun 20 12:40:35 2007 @@ -0,0 +1,689 @@ +/* *##% + * Copyright (C) 2007 Code Lutin + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +package org.codelutin.eclipse.generator.ui.editor; + +/** + * FastTemplatedJavaPartitionScanner. + * + * Reutilise le code de la classe FastJavaPartitionScanner fournit par le JDT + * en lui ajoutant la gestion des templates. + * + * Les delimitations des templates sont : + * /*{ ... }*) + * + * et le code Java a l'intérieur par : + * <%= ... %> + * + * @author chatellier + * @version $Revision: 1.1 $ + * + * Last update : $Date: 2007/06/20 12:40:35 $ + * By : $Author: chatellier $ + */ +import org.eclipse.jdt.internal.ui.text.BufferedDocumentScanner; +import org.eclipse.jdt.ui.text.IJavaPartitions; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.rules.ICharacterScanner; +import org.eclipse.jface.text.rules.IPartitionTokenScanner; +import org.eclipse.jface.text.rules.IToken; +import org.eclipse.jface.text.rules.Token; + + +/** + * This scanner recognizes the JavaDoc comments, Java multi line comments, Java single line comments, + * Java strings and Java characters. + * + * And LutinGenerator templates. + */ +public class FastTemplatedJavaPartitionScanner implements IPartitionTokenScanner, IJavaPartitions, TemplatedJavaPartitions { + + // states + private static final int JAVA= 0; + private static final int SINGLE_LINE_COMMENT= 1; + private static final int MULTI_LINE_COMMENT= 2; + private static final int JAVADOC= 3; + private static final int CHARACTER= 4; + private static final int STRING= 5; + + // lutingenerator specific + private static final int TEMPLATE= 6; + private static final int JAVA_INSIDE_TEMPLATE= 7; + + // beginning of prefixes and postfixes + private static final int NONE= 0; + private static final int BACKSLASH= 1; // postfix for STRING and CHARACTER + private static final int SLASH= 2; // prefix for SINGLE_LINE or MULTI_LINE or JAVADOC + private static final int SLASH_STAR= 3; // prefix for MULTI_LINE_COMMENT or JAVADOC + private static final int SLASH_STAR_STAR= 4; // prefix for MULTI_LINE_COMMENT or JAVADOC + private static final int STAR= 5; // postfix for MULTI_LINE_COMMENT or JAVADOC + private static final int CARRIAGE_RETURN=6; // postfix for STRING, CHARACTER and SINGLE_LINE_COMMENT + + // lutingenerator specific + private static final int LOWERTHAN=11; + private static final int LOWERTHAN_PERCENT=12; + private static final int PERCENT=13; + private static final int CLOSE_ACCO=14; + private static final int CLOSE_ACCO_STAR=15; + + /** The scanner. */ + private final BufferedDocumentScanner fScanner= new BufferedDocumentScanner(1000); // faster implementation + + /** The offset of the last returned token. */ + private int fTokenOffset; + /** The length of the last returned token. */ + private int fTokenLength; + + /** The state of the scanner. */ + private int fState; + /** The last significant characters read. */ + private int fLast; + /** The amount of characters already read on first call to nextToken(). */ + private int fPrefixLength; + + // emulate JavaPartitionScanner + private boolean fEmulate= false; + private int fJavaOffset; + private int fJavaLength; + + private final IToken[] fTokens= new IToken[] { + new Token(null), + new Token(JAVA_SINGLE_LINE_COMMENT), + new Token(JAVA_MULTI_LINE_COMMENT), + new Token(JAVA_DOC), + new Token(JAVA_CHARACTER), + new Token(JAVA_STRING), + new Token(TEMPLATED_JAVA_XXX_JSP), + new Token(null) // normal java (but inside template) + }; + + public FastTemplatedJavaPartitionScanner(boolean emulate) { + fEmulate= emulate; + } + + public FastTemplatedJavaPartitionScanner() { + this(false); + } + + /* + * @see org.eclipse.jface.text.rules.ITokenScanner#nextToken() + */ + public IToken nextToken() { + + // emulate JavaPartitionScanner + if (fEmulate) { + if (fJavaOffset != -1 && fTokenOffset + fTokenLength != fJavaOffset + fJavaLength) { + fTokenOffset += fTokenLength; + return fTokens[JAVA]; + } else { + fJavaOffset= -1; + fJavaLength= 0; + } + } + + fTokenOffset += fTokenLength; + fTokenLength= fPrefixLength; + + while (true) { + final int ch= fScanner.read(); + + // characters + switch (ch) { + case ICharacterScanner.EOF: + if (fTokenLength > 0) { + fLast= NONE; // ignore last + return preFix(fState, JAVA, NONE, 0); + + } else { + fLast= NONE; + fPrefixLength= 0; + return Token.EOF; + } + + case '\r': + // emulate JavaPartitionScanner + if (!fEmulate && fLast != CARRIAGE_RETURN) { + fLast= CARRIAGE_RETURN; + fTokenLength++; + continue; + + } else { + + switch (fState) { + case SINGLE_LINE_COMMENT: + case CHARACTER: + case STRING: + if (fTokenLength > 0) { + IToken token= fTokens[fState]; + + // emulate JavaPartitionScanner + if (fEmulate) { + fTokenLength++; + fLast= NONE; + fPrefixLength= 0; + } else { + fLast= CARRIAGE_RETURN; + fPrefixLength= 1; + } + + fState= JAVA; + return token; + + } else { + consume(); + continue; + } + + default: + consume(); + continue; + } + } + + case '\n': + switch (fState) { + case SINGLE_LINE_COMMENT: + case CHARACTER: + case STRING: + // assert(fTokenLength > 0); + return postFix(fState); + + default: + consume(); + continue; + } + + default: + if (!fEmulate && fLast == CARRIAGE_RETURN) { + switch (fState) { + case SINGLE_LINE_COMMENT: + case CHARACTER: + case STRING: + + int last; + int newState; + switch (ch) { + case '/': + last= SLASH; + newState= JAVA; + break; + + case '*': + last= STAR; + newState= JAVA; + break; + + case '\'': + last= NONE; + newState= CHARACTER; + break; + + case '"': + last= NONE; + newState= STRING; + break; + + case '\r': + last= CARRIAGE_RETURN; + newState= JAVA; + break; + + case '\\': + last= BACKSLASH; + newState= JAVA; + break; + + default: + last= NONE; + newState= JAVA; + break; + } + + fLast= NONE; // ignore fLast + return preFix(fState, newState, last, 1); + + default: + break; + } + } + } + + // states + switch (fState) { + case JAVA: + switch (ch) { + case '/': + if (fLast == SLASH) { + if (fTokenLength - getLastLength(fLast) > 0) { + return preFix(JAVA, SINGLE_LINE_COMMENT, NONE, 2); + } else { + preFix(JAVA, SINGLE_LINE_COMMENT, NONE, 2); + fTokenOffset += fTokenLength; + fTokenLength= fPrefixLength; + break; + } + + } else { + fTokenLength++; + fLast= SLASH; + break; + } + + case '*': + if (fLast == SLASH) { + if (fTokenLength - getLastLength(fLast) > 0) + return preFix(JAVA, MULTI_LINE_COMMENT, SLASH_STAR, 2); + else { + preFix(JAVA, MULTI_LINE_COMMENT, SLASH_STAR, 2); + fTokenOffset += fTokenLength; + fTokenLength= fPrefixLength; + break; + } + + } else { + consume(); + break; + } + + case '\'': + fLast= NONE; // ignore fLast + if (fTokenLength > 0) + return preFix(JAVA, CHARACTER, NONE, 1); + else { + preFix(JAVA, CHARACTER, NONE, 1); + fTokenOffset += fTokenLength; + fTokenLength= fPrefixLength; + break; + } + + case '"': + fLast= NONE; // ignore fLast + if (fTokenLength > 0) + return preFix(JAVA, STRING, NONE, 1); + else { + preFix(JAVA, STRING, NONE, 1); + fTokenOffset += fTokenLength; + fTokenLength= fPrefixLength; + break; + } + + default: + consume(); + break; + } + break; + + case SINGLE_LINE_COMMENT: + consume(); + break; + + case JAVADOC: + switch (ch) { + case '/': + switch (fLast) { + case SLASH_STAR_STAR: + return postFix(MULTI_LINE_COMMENT); + + case STAR: + return postFix(JAVADOC); + + default: + consume(); + break; + } + break; + + case '*': + fTokenLength++; + fLast= STAR; + break; + + default: + consume(); + break; + } + break; + + case MULTI_LINE_COMMENT: + switch (ch) { + case '*': + if (fLast == SLASH_STAR) { + fLast= SLASH_STAR_STAR; + fTokenLength++; + fState= JAVADOC; + } else { + fTokenLength++; + fLast= STAR; + } + break; + + // template addition (lutingenerator) + case '{': + if (fLast == SLASH_STAR) { + fTokenLength++; + fState= TEMPLATE; + //return fTokens[TEMPLATE]; + } else { + consume(); + break; + } + break; + + case '/': + if (fLast == STAR) { + return postFix(MULTI_LINE_COMMENT); + } else { + consume(); + break; + } + + default: + consume(); + break; + } + break; + + case STRING: + switch (ch) { + case '\\': + fLast= (fLast == BACKSLASH) ? NONE : BACKSLASH; + fTokenLength++; + break; + + case '\"': + if (fLast != BACKSLASH) { + return postFix(STRING); + + } else { + consume(); + break; + } + + default: + consume(); + break; + } + break; + + case CHARACTER: + switch (ch) { + case '\\': + fLast= (fLast == BACKSLASH) ? NONE : BACKSLASH; + fTokenLength++; + break; + + case '\'': + if (fLast != BACKSLASH) { + return postFix(CHARACTER); + + } else { + consume(); + break; + } + + default: + consume(); + break; + } + break; + +// template addition (lutingenerator) + case TEMPLATE: + switch (ch) { + case '%': + if (fLast == LOWERTHAN) { + fLast= LOWERTHAN_PERCENT; + fTokenLength++; + //fPrefixLength++; + //fState= JAVA_INSIDE_TEMPLATE; + //return preFix(TEMPLATE,JAVA_INSIDE_TEMPLATE,NONE,2); + } else { + consume(); + break; + } + break; + case '=': + if (fLast == LOWERTHAN_PERCENT) { + //fPrefixLength++; + fTokenLength += 3; // pour l'instant + // <%= fait partit du java templated, et non du block + // suivant + return preFix(TEMPLATE,JAVA_INSIDE_TEMPLATE,NONE,0); + } else { + consume(); + break; + } + //break; + case '/': + if (fLast == CLOSE_ACCO_STAR) { + return postFix(TEMPLATE); + } else { + consume(); + break; + } + case '*': + if (fLast == CLOSE_ACCO) { + fLast= CLOSE_ACCO_STAR; + fTokenLength++; + } else { + consume(); + break; + } + break; + case '}': + fTokenLength++; + fLast= CLOSE_ACCO; + break; + case '<': + fTokenLength++; + fLast= LOWERTHAN; + break; + default: + if (fLast == LOWERTHAN_PERCENT) { + //fPrefixLength++; + fTokenLength += 2; // pour l'instant + // <%= fait partit du java templated, et non du block + // suivant + //fTokenOffset--; + return preFix(TEMPLATE,JAVA_INSIDE_TEMPLATE,NONE,1); + } else { + consume(); + break; + } + } + break; +// template addition (lutingenerator) + case JAVA_INSIDE_TEMPLATE: + switch (ch) { + + case '>': + if (fLast == PERCENT) { + //fPrefixLength++; + return preFix(JAVA_INSIDE_TEMPLATE,TEMPLATE,NONE,2); + //IToken t = postFix(JAVA_INSIDE_TEMPLATE); + //fState = TEMPLATE; + //return t; + } else { + consume(); + break; + } + // break + case '%': + fTokenLength++; + fLast= PERCENT; + break; + + default: + consume(); + break; + } + break; + } + } + } + + private static final int getLastLength(int last) { + switch (last) { + default: + return -1; + + case NONE: + return 0; + + case CARRIAGE_RETURN: + case BACKSLASH: + case SLASH: + case STAR: + case LOWERTHAN: // template addition (lutingenerator) + case PERCENT: // template addition (lutingenerator) + case CLOSE_ACCO: // template addition (lutingenerator) + return 1; + + case SLASH_STAR: + case LOWERTHAN_PERCENT:// template addition (lutingenerator) + case CLOSE_ACCO_STAR:// template addition (lutingenerator) + return 2; + + case SLASH_STAR_STAR: + return 3; + } + } + + private final void consume() { + fTokenLength++; + fLast= NONE; + } + + private final IToken postFix(int state) { + fTokenLength++; + fLast= NONE; + fState= JAVA; + fPrefixLength= 0; + return fTokens[state]; + } + + private final IToken preFix(int state, int newState, int last, int prefixLength) { + // emulate JavaPartitionScanner + if (fEmulate && state == JAVA && (fTokenLength - getLastLength(fLast) > 0)) { + fTokenLength -= getLastLength(fLast); + fJavaOffset= fTokenOffset; + fJavaLength= fTokenLength; + fTokenLength= 1; + fState= newState; + fPrefixLength= prefixLength; + fLast= last; + return fTokens[state]; + + } else { + fTokenLength -= getLastLength(fLast); + fLast= last; + fPrefixLength= prefixLength; + IToken token= fTokens[state]; + fState= newState; + return token; + } + } + + private static int getState(String contentType) { + + if (contentType == null) + return JAVA; + + else if (contentType.equals(JAVA_SINGLE_LINE_COMMENT)) + return SINGLE_LINE_COMMENT; + + else if (contentType.equals(JAVA_MULTI_LINE_COMMENT)) + return MULTI_LINE_COMMENT; + + else if (contentType.equals(JAVA_DOC)) + return JAVADOC; + + else if (contentType.equals(JAVA_STRING)) + return STRING; + + else if (contentType.equals(JAVA_CHARACTER)) + return CHARACTER; + +// template addition (lutingenerator) + else if (contentType.equals(TEMPLATED_JAVA_XXX_JSP)) + return TEMPLATE; + // JAVA_INSIDE_TEMPLATE , JAVA => else + + else + return JAVA; + } + + /* + * @see IPartitionTokenScanner#setPartialRange(IDocument, int, int, String, int) + */ + public void setPartialRange(IDocument document, int offset, int length, String contentType, int partitionOffset) { + + fScanner.setRange(document, offset, length); + fTokenOffset= partitionOffset; + fTokenLength= 0; + fPrefixLength= offset - partitionOffset; + fLast= NONE; + + if (offset == partitionOffset) { + // restart at beginning of partition + fState= JAVA; + } else { + fState= getState(contentType); + } + + // emulate JavaPartitionScanner + if (fEmulate) { + fJavaOffset= -1; + fJavaLength= 0; + } + } + + /* + * @see ITokenScanner#setRange(IDocument, int, int) + */ + public void setRange(IDocument document, int offset, int length) { + + fScanner.setRange(document, offset, length); + fTokenOffset= offset; + fTokenLength= 0; + fPrefixLength= 0; + fLast= NONE; + fState= JAVA; + + // emulate JavaPartitionScanner + if (fEmulate) { + fJavaOffset= -1; + fJavaLength= 0; + } + } + + /* + * @see ITokenScanner#getTokenLength() + */ + public int getTokenLength() { + return fTokenLength; + } + + /* + * @see ITokenScanner#getTokenOffset() + */ + public int getTokenOffset() { + return fTokenOffset; + } + +} Index: LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/ui/editor/TemplatedJavaCodeScanner.java diff -u /dev/null LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/ui/editor/TemplatedJavaCodeScanner.java:1.1 --- /dev/null Wed Jun 20 12:40:43 2007 +++ LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/ui/editor/TemplatedJavaCodeScanner.java Wed Jun 20 12:40:35 2007 @@ -0,0 +1,212 @@ +/* *##% + * Copyright (C) 2007 Code Lutin + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +package org.codelutin.eclipse.generator.ui.editor; + +import java.util.ArrayList; +import java.util.List; + +import org.codelutin.eclipse.generator.LutinGeneratorPlugin; +import org.eclipse.jface.text.rules.BufferedRuleBasedScanner; +import org.eclipse.jface.text.rules.ICharacterScanner; +import org.eclipse.jface.text.rules.IRule; +import org.eclipse.jface.text.rules.IToken; +import org.eclipse.jface.text.rules.Token; + +/** + * TemplatedJavaCodeScanner + * + * Cette classe analyse le code dans la partition des templates seulement. + * + * Pour l'instant, elle ne colore que les tags JSP. + * + * TODO coloration suivant le type ? + * + * @author chatellier + * @version $Revision: 1.1 $ + * + * Last update : $Date: 2007/06/20 12:40:35 $ + * By : $Author: chatellier $ + */ +public class TemplatedJavaCodeScanner extends BufferedRuleBasedScanner { + + /** + * Rule to detect template tags. + * <%= and %> + */ + protected class JspTagRule implements IRule { + + /** Token to return for this rule */ + private final IToken fToken; + + /** + * Creates a new operator rule. + * + * @param token Token to use for this rule + */ + public JspTagRule(IToken token) { + fToken= token; + } + + /* + * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner) + */ + public IToken evaluate(ICharacterScanner scanner) { + + int character= scanner.read(); + + switch(character) { + case '<': + if(scanner.read()=='%') { + if(scanner.read()=='=') { + //scanner.unread(); + return fToken; + } + else { + // unread parce qu'on a tente de lire <%= + // alors que c'est seulement un <% + scanner.unread(); + return fToken; + } + } + else { + scanner.unread(); + } + break; + + case '%': + if(scanner.read()=='>') { + //scanner.unread(); + return fToken; + } + else { + scanner.unread(); + } + break; + } + + scanner.unread(); + return Token.UNDEFINED; + } + } + + /** + * Rule to detect template tags. + * "/*{" and "}*"+"/" + */ + protected class TemplateTagRule implements IRule { + + /** Token to return for this rule */ + private final IToken fToken; + + /** + * Creates a new operator rule. + * + * @param token Token to use for this rule + */ + public TemplateTagRule(IToken token) { + fToken= token; + } + + /* + * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner) + */ + public IToken evaluate(ICharacterScanner scanner) { + + int character= scanner.read(); + + switch(character) { + case '/': + if(scanner.read()=='*') { + if(scanner.read()=='{') { + return fToken; + } + else { + scanner.unread(); + } + } + else { + scanner.unread(); + } + break; + + case '}': + if(scanner.read()=='*') { + if(scanner.read()=='/') { + return fToken; + } + else { + scanner.unread(); + } + } + else { + scanner.unread(); + } + break; + } + + scanner.unread(); + return Token.UNDEFINED; + } + } + + /** + * Constructor, initialize rule set + */ + public TemplatedJavaCodeScanner() { + initialize(); + } + + /** + * Init scanner rules + */ + protected void initialize() { + initializeRules(); + } + + /** + * + */ + protected void initializeRules() { + + // rules list + List rules = new ArrayList(); + + // color provider + TemplatedJavaTextAttributeProvider provider = LutinGeneratorPlugin.getDefault().getTextAttributeProvider(); + + // token list used here + IToken undefined = new Token(provider.getAttribute(TemplatedJavaTextAttributeProvider.XXX_JSP_CODE)); + IToken jsptagToken = new Token(provider.getAttribute(TemplatedJavaTextAttributeProvider.JSP_TAG)); + IToken templatetagToken = new Token(provider.getAttribute(TemplatedJavaTextAttributeProvider.TEMPLATE_TAG)); + + // regle par defaut + setDefaultReturnToken(undefined); + + // undefined = la couleur par defaut de tout le reste + // ca serait a changer donc... + + rules.add(new JspTagRule(jsptagToken)); + rules.add(new TemplateTagRule(templatetagToken)); + + // Conversion de la List en tableau pour la passer à la méthode setRules + IRule[] param = new IRule[rules.size()]; + rules.toArray(param); + setRules(param); + } +} Index: LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/ui/editor/TemplatedJavaPartitions.java diff -u /dev/null LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/ui/editor/TemplatedJavaPartitions.java:1.1 --- /dev/null Wed Jun 20 12:40:43 2007 +++ LutinGeneratorEclipsePlugin/src/org/codelutin/eclipse/generator/ui/editor/TemplatedJavaPartitions.java Wed Jun 20 12:40:35 2007 @@ -0,0 +1,65 @@ +/* *##% + * Copyright (C) 2007 Code Lutin + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +package org.codelutin.eclipse.generator.ui.editor; + +import org.eclipse.jdt.ui.text.IJavaPartitions; +import org.eclipse.jface.text.IDocument; + +/** + * ITemplatedJavaPartition + * + * Definition des constantes sur les partionnements et les partitions des + * fichers "Java Templaté". + * + * Etend les type java + * @see org.eclipse.jdt.ui.text.IJavaPartitions + * + * @author chatellier + * @version $Revision: 1.1 $ + * + * Last update : $Date: 2007/06/20 12:40:35 $ + * By : $Author: chatellier $ + */ +public interface TemplatedJavaPartitions { + + /** + * Partitionning name + */ + public static String TEMPLATED_JAVA_PARTITIONNING = "__codelutin_lutingeneratorplugin_templatedjava_partionning"; + + /** + * Partition template name + */ + public static String TEMPLATED_JAVA_XXX_JSP = "__codelutin_lutingeneratorplugin_templatedjava_xxx_jsp_part"; + + /** + * Templated java file partition type + */ + public static String[] TEMPLATED_JAVA_PARTITION_TYPES = { + // default java types + IDocument.DEFAULT_CONTENT_TYPE, + IJavaPartitions.JAVA_STRING, + IJavaPartitions.JAVA_CHARACTER, + IJavaPartitions.JAVA_MULTI_LINE_COMMENT, + IJavaPartitions.JAVA_SINGLE_LINE_COMMENT, + IJavaPartitions.JAVA_DOC, + // template type + TEMPLATED_JAVA_XXX_JSP + }; +}