Index: plugin.properties =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/plugin.properties,v retrieving revision 1.2 diff -u -r1.2 plugin.properties --- plugin.properties 2 Oct 2006 23:08:05 -0000 1.2 +++ plugin.properties 22 Dec 2006 18:03:50 -0000 @@ -1,2 +1,13 @@ pluginName = FreeMarker IDE -providerName = JBoss, a division of Red Hat \ No newline at end of file +providerName = JBoss, a division of Red Hat + +freemarker.editor=Freemarker Editor +freemarker.library=Use as FreeMarker library +freemarker.context=FreeMarker Context + +freemarker.templates=Freemarker Templates +freemarker.template.type=Freemarker + +# preferencePages +freemarker.preferencePages.templates=Templates + Index: plugin.xml =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/plugin.xml,v retrieving revision 1.1 diff -u -r1.1 plugin.xml --- plugin.xml 8 May 2006 15:09:08 -0000 1.1 +++ plugin.xml 22 Dec 2006 18:03:51 -0000 @@ -9,15 +9,20 @@ extensions="ftl" icon="icons/icon.gif" id="org.jboss.ide.eclipse.freemarker.editor.FreemarkerEditor" - name="Freemarker Editor"> + name="%freemarker.editor"> + id="org.jboss.ide.eclipse.freemarker.preferences.PreferencePage" + name="%freemarker.editor"/> + @@ -30,7 +35,7 @@ class="org.jboss.ide.eclipse.freemarker.editor.actions.AddMacroLibrary" icon="icons/include.png" id="org.jboss.ide.eclipse.freemarker.AddMacroLibrary1" - label="Use as FreeMarker library"/> + label="%freemarker.library"/> + label="%freemarker.library"/> + + + + + Index: META-INF/MANIFEST.MF =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/META-INF/MANIFEST.MF,v retrieving revision 1.2 diff -u -r1.2 MANIFEST.MF --- META-INF/MANIFEST.MF 29 Aug 2006 21:10:59 -0000 1.2 +++ META-INF/MANIFEST.MF 22 Dec 2006 18:03:51 -0000 @@ -3,7 +3,7 @@ Bundle-Name: %pluginName Bundle-SymbolicName: org.jboss.ide.eclipse.freemarker;singleton:=true Bundle-Version: 1.0.3 -Bundle-Activator: org.jboss.ide.eclipse.freemarker.Plugin +Bundle-Activator: org.jboss.ide.eclipse.freemarker.FreemarkerPlugin Bundle-Localization: plugin Require-Bundle: org.eclipse.ui, org.eclipse.core.runtime, Index: src/org/jboss/ide/eclipse/freemarker/ImageManager.java =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/src/org/jboss/ide/eclipse/freemarker/ImageManager.java,v retrieving revision 1.2 diff -u -r1.2 ImageManager.java --- src/org/jboss/ide/eclipse/freemarker/ImageManager.java 2 Oct 2006 23:08:06 -0000 1.2 +++ src/org/jboss/ide/eclipse/freemarker/ImageManager.java 22 Dec 2006 18:03:51 -0000 @@ -51,7 +51,7 @@ public static ImageDescriptor getImageDescriptor(String filename) { if (null == filename) return null; try { - URL url = new URL(Plugin.getInstance().getDescriptor().getInstallURL(), + URL url = new URL(FreemarkerPlugin.getInstance().getDescriptor().getInstallURL(), "icons/" + filename); return ImageDescriptor.createFromURL(url); } catch (MalformedURLException mue) { Index: src/org/jboss/ide/eclipse/freemarker/Plugin.java =================================================================== RCS file: src/org/jboss/ide/eclipse/freemarker/Plugin.java diff -N src/org/jboss/ide/eclipse/freemarker/Plugin.java --- src/org/jboss/ide/eclipse/freemarker/Plugin.java 2 Oct 2006 23:08:06 -0000 1.2 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,127 +0,0 @@ -/* - * JBoss, a division of Red Hat - * Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated - * by the @authors tag. See the copyright.txt in the distribution for a - * full listing of individual contributors. - * - * This is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1 of - * the License, or (at your option) any later version. - * - * This software 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this software; if not, write to the Free - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA - * 02110-1301 USA, or see the FSF site: http://www.fsf.org. - */ -package org.jboss.ide.eclipse.freemarker; - -import java.io.PrintWriter; -import java.io.StringWriter; -import java.util.MissingResourceException; -import java.util.ResourceBundle; - -import org.eclipse.core.resources.IWorkspace; -import org.eclipse.core.resources.ResourcesPlugin; -import org.eclipse.core.runtime.IPluginDescriptor; -import org.eclipse.core.runtime.Preferences; -import org.eclipse.jface.dialogs.MessageDialog; -import org.eclipse.swt.widgets.Display; -import org.eclipse.ui.plugin.AbstractUIPlugin; - -/** - * @author Joe Hudson - */ -public class Plugin extends AbstractUIPlugin { - - public static final String ID = "org.jboss.ide.eclipse.freemarker"; - - //The shared instance. - private static Plugin plugin; - //Resource bundle. - private ResourceBundle resourceBundle; - - public Plugin() { - super(); - plugin = this; - try { - resourceBundle = ResourceBundle.getBundle("org.jboss.ide.eclipse.freemarker.resources"); - } catch (MissingResourceException x) { - resourceBundle = null; - } - } - - /** - * The constructor. - */ - public Plugin(IPluginDescriptor descriptor) { - super(descriptor); - plugin = this; - } - - /** - * Returns the shared instance. - */ - public static Plugin getDefault() { - return plugin; - } - - /** - * Returns the workspace instance. - */ - public static IWorkspace getWorkspace() { - return ResourcesPlugin.getWorkspace(); - } - - public static Plugin getInstance() { - return plugin; - } - - /** - * Returns the plugin's resource bundle, - */ - public ResourceBundle getResourceBundle() { - return resourceBundle; - } - - /** - * Initializes the plugin preferences with default preference values for - * this plug-in. - */ - protected void initializeDefaultPluginPreferences() { - Preferences prefs = getPluginPreferences(); - prefs.setDefault(Constants.HIGHLIGHT_RELATED_ITEMS, true); - prefs.setDefault(Constants.COLOR_COMMENT, "170,0,0"); - prefs.setDefault(Constants.COLOR_TEXT, "0,0,0"); - prefs.setDefault(Constants.COLOR_INTERPOLATION, "255,0,128"); - prefs.setDefault(Constants.COLOR_DIRECTIVE, "0,0,255"); - prefs.setDefault(Constants.COLOR_STRING, "0,128,128"); - prefs.setDefault(Constants.COLOR_XML_COMMENT, "128,128,128"); - prefs.setDefault(Constants.COLOR_XML_TAG, "0,0,128"); - prefs.setDefault(Constants.COLOR_RELATED_ITEM, "255,255,128"); - } - - public static void error (Throwable t) { - StringWriter sw = new StringWriter(); - t.printStackTrace(new PrintWriter(sw)); - MessageDialog.openError(Display.getCurrent().getActiveShell(), t.getMessage(), sw.toString()); - log(t); - } - - public static void log (Throwable t) { - log("Error: " + t.getMessage() + "\n"); - StringWriter sw = new StringWriter(); - t.printStackTrace(new PrintWriter(sw)); - log("Trace: " + sw.toString()); - - } - - public static void log (String s) { - // TODO figure out a good place to log messages - } -} \ No newline at end of file Index: src/org/jboss/ide/eclipse/freemarker/resources.properties =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/src/org/jboss/ide/eclipse/freemarker/resources.properties,v retrieving revision 1.1 diff -u -r1.1 resources.properties --- src/org/jboss/ide/eclipse/freemarker/resources.properties 8 May 2006 15:09:09 -0000 1.1 +++ src/org/jboss/ide/eclipse/freemarker/resources.properties 22 Dec 2006 18:03:51 -0000 @@ -0,0 +1,29 @@ +# OpenResourceDialog Dialog Widget +OpenResourceDialog.title=Select a XML file. +OpenResourceDialog.help=XML file selected will be used into your context. + +# ContextValueDialog dialog Widget +ContextValueDialog.title=Context Value Configuration +ContextValueDialog.contextType.label=Context type: +ContextValueDialog.key.label=Name: +ContextValueDialog.classRadioButton.text=Class +ContextValueDialog.xmlRadioButton.text=XML +ContextValueDialog.browseButton.text=Browse +ContextValueDialog.labelText.CLASS=Class: +ContextValueDialog.labelText.XML=XML: + +# ContextValueDialog dialog messages +ContextValueDialog.context.message=Click OK to save your context. +ContextValueDialog.context.name.required=Choose the reference name! +ContextValueDialog.context.CLASS.required=Choose the class name by using Browse button! +ContextValueDialog.context.XML.required=Choose a XML file by using Browse button! + +# ContextProperties Property Page +ContextProperties.description=Create pre-determined Velocity context variables for all files under this resource + +# OutlinePage +OutlinePage.text=Set context class entry + +# Global Messages +Context.confirm.title=Confirmation +Context.confirm.message=Are you sure you want to delete this context value? \ No newline at end of file Index: src/org/jboss/ide/eclipse/freemarker/configuration/ConfigurationManager.java =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/src/org/jboss/ide/eclipse/freemarker/configuration/ConfigurationManager.java,v retrieving revision 1.2 diff -u -r1.2 ConfigurationManager.java --- src/org/jboss/ide/eclipse/freemarker/configuration/ConfigurationManager.java 2 Oct 2006 23:08:06 -0000 1.2 +++ src/org/jboss/ide/eclipse/freemarker/configuration/ConfigurationManager.java 22 Dec 2006 18:03:51 -0000 @@ -43,7 +43,9 @@ import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.jface.dialogs.InputDialog; import org.eclipse.swt.widgets.Shell; -import org.jboss.ide.eclipse.freemarker.Plugin; +import org.jboss.ide.eclipse.freemarker.FreemarkerPlugin; +import org.jboss.ide.eclipse.freemarker.configuration.context.IContextValue; +import org.jboss.ide.eclipse.freemarker.helper.ContextValueHelper; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -99,10 +101,10 @@ this.macroLibrary.put(namespace, new MacroLibrary(namespace, file)); } catch (CoreException e) { - Plugin.error(e); + FreemarkerPlugin.error(e); } catch (IOException e) { - Plugin.error(e); + FreemarkerPlugin.error(e); } } } @@ -128,10 +130,10 @@ } } catch (CoreException e) { - Plugin.error(e); + FreemarkerPlugin.error(e); } catch (IOException e) { - Plugin.error(e); + FreemarkerPlugin.error(e); } } } @@ -164,17 +166,17 @@ } } catch (Exception e) { - Plugin.log(e); + FreemarkerPlugin.log(e); } } } catch (Exception e) { - Plugin.log(e); + FreemarkerPlugin.log(e); } return map; } - private IFile getFile (String path) { + public IFile getFile (String path) { return project.getFile(path); } @@ -186,9 +188,10 @@ Map map = new HashMap(); try { NodeList nl = element - .getElementsByTagName("resource"); + .getElementsByTagName("resource"); for (int i = 0; i < nl.getLength(); i++) { try { + IContextValue contextValue = null; Node n = nl.item(i); String path = ((Element) n).getAttribute("path"); List contextValues = new ArrayList(); @@ -197,27 +200,45 @@ for (int j = 0; j < nl2.getLength(); j++) { Node n2 = nl2.item(j); String key = ((Element) n2).getAttribute("key"); - Class value = getClass(((Element) n2) - .getAttribute("object-class")); - String singularName = ((Element) n2) - .getAttribute("item-class"); - Class singularClass = null; - if (null != singularName && singularName.trim().length()>0) - singularClass = getClass(singularName); - contextValues.add(new ContextValue(key, value, - singularClass)); + // Instanciate Context value switch class/xml or string + String className = ((Element)n2).getAttribute("object-class"); + if (className != null && className.trim().length() > 0) { + // Class context + String singularName = ((Element)n2).getAttribute("item-class"); + contextValue = getInstanceOfContextValue(IContextValue.CLASS, key, className, singularName); + } + else { + String stringValue = ((Element)n2).getAttribute("string-value"); + if (stringValue != null && stringValue.trim().length() > 0) { + // String context + contextValue = getInstanceOfContextValue(IContextValue.STRING, key, stringValue, null); + } + else { + String xmlFile = ((Element)n2).getAttribute("xml-file"); + if (xmlFile != null && xmlFile.trim().length() > 0) { + // XML context + contextValue = getInstanceOfContextValue(IContextValue.XML, key, xmlFile, null); + } + else { + String dtdFile = ((Element)n2).getAttribute("dtd-file"); + if (dtdFile != null && dtdFile.trim().length() > 0) { + // DTD context + contextValue = getInstanceOfContextValue(IContextValue.DTD, key, dtdFile, null); + } + } + } + } + if (contextValue != null) + contextValues.add(contextValue); } - map.put(path, - contextValues - .toArray(new ContextValue[contextValues - .size()])); + map.put(path, ((Object) (contextValues.toArray(new IContextValue[contextValues.size()])))); } catch (Exception e) { - Plugin.log(e); + FreemarkerPlugin.log(e); } } - } + } catch (Exception e) { - Plugin.log(e); + FreemarkerPlugin.log(e); } return map; } @@ -252,7 +273,7 @@ file.create(new ByteArrayInputStream(sb.toString().getBytes()), true, null); } catch (Exception e) { - Plugin.error(e); + FreemarkerPlugin.error(e); } reload(); } @@ -285,7 +306,7 @@ this.macroLibrary = new HashMap(); macroLibraryArr = (MacroLibrary[]) libraries.toArray(new MacroLibrary[libraries.size()]); } catch (Exception e) { - Plugin.error(e); + FreemarkerPlugin.error(e); } } } @@ -294,27 +315,57 @@ for (Iterator i = contextValues.entrySet().iterator(); i.hasNext();) { Map.Entry entry = (Map.Entry) i.next(); String fileName = (String) entry.getKey(); - ContextValue[] values = (ContextValue[]) entry.getValue(); + IContextValue[] values = (IContextValue[]) entry.getValue(); if (null != values && values.length > 0) { sb.append("\t\t\n"); for (int j = 0; j < values.length; j++) { - sb.append("\t\t\t\n"); + // Manage context value for XML, String or class + IContextValue contextValue = values[j]; + String key = contextValue.getName(); + if (contextValue.getType() == IContextValue.CLASS) { + // Class context + String objClassName = contextValue.getFirstObjectContext().getName(); + String singularClassName = contextValue.getSecondObjectContext().getName(); + sb.append("\t\t\t\n"); + } + else { + if (contextValue.getType() == IContextValue.STRING) { + // String context + /*String stringValue = contextValue.getFirstObjectContext().getName(); + sb.append("\t\t\t\n"); + */ + } + else { + if (contextValue.getType() == IContextValue.XML) { + // XML context + String xmlFile = contextValue.getFirstObjectContext().getName(); + sb.append("\t\t\t\n"); + } + else { + /*if (contextValue.getType() == IContextValue.DTD) { + // XML context + String dtdFile = contextValue.getFirstObjectContext().getName(); + sb.append("\t\t\t\n"); + }*/ + } + } + } } sb.append("\t\t\n"); } } } - public ContextValue[] getContextValues(IResource resource, boolean recurse) { + public IContextValue[] getContextValues(IResource resource, boolean recurse) { Map newValues = new HashMap(); addRootContextValues(resource, newValues, recurse); - return (ContextValue[]) newValues.values().toArray(new ContextValue[newValues.size()]); + return (IContextValue[]) newValues.values().toArray(new IContextValue[newValues.size()]); } private void addRootContextValues(IResource resource, Map newValues, boolean recurse) { @@ -325,29 +376,29 @@ } else key = ""; if (null != resource.getProject()) { - ContextValue[] values = (ContextValue[]) contextValues.get(key); - if (null != values) { - for (int i=0; i= 0) { - ContextValue[] newValues = new ContextValue[values.length - 1]; + IContextValue[] newValues = new IContextValue[values.length - 1]; int j = 0; for (int i = 0; i < values.length; i++) { if (i != index) @@ -392,4 +443,21 @@ save(); } } + + /** + * Return an instance of context value swith contextType (CLASS, XML, ...) + * @param contextType + * @param name + * @param context1 + * @param context2 + * @return + * @throws Exception + */ + public IContextValue getInstanceOfContextValue(int contextType, String name, + String context1, String context2) throws Exception + { + return ContextValueHelper.getInstanceOfContextValue(this, contextType, name, context1, + context2); + } + } \ No newline at end of file Index: src/org/jboss/ide/eclipse/freemarker/configuration/ContextValue.java =================================================================== RCS file: src/org/jboss/ide/eclipse/freemarker/configuration/ContextValue.java diff -N src/org/jboss/ide/eclipse/freemarker/configuration/ContextValue.java --- src/org/jboss/ide/eclipse/freemarker/configuration/ContextValue.java 2 Oct 2006 23:08:06 -0000 1.2 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,38 +0,0 @@ -/* - * JBoss, a division of Red Hat - * Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated - * by the @authors tag. See the copyright.txt in the distribution for a - * full listing of individual contributors. - * - * This is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1 of - * the License, or (at your option) any later version. - * - * This software 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this software; if not, write to the Free - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA - * 02110-1301 USA, or see the FSF site: http://www.fsf.org. - */ -package org.jboss.ide.eclipse.freemarker.configuration; - -/** - * @author Joe Hudson - */ -public class ContextValue { - - public String name; - public Class objClass; - public Class singularClass; - - public ContextValue (String name, Class objClass, Class singularClass) { - this.name = name; - this.objClass = objClass; - this.singularClass = singularClass; - } -} Index: src/org/jboss/ide/eclipse/freemarker/configuration/MacroLibrary.java =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/src/org/jboss/ide/eclipse/freemarker/configuration/MacroLibrary.java,v retrieving revision 1.2 diff -u -r1.2 MacroLibrary.java --- src/org/jboss/ide/eclipse/freemarker/configuration/MacroLibrary.java 2 Oct 2006 23:08:06 -0000 1.2 +++ src/org/jboss/ide/eclipse/freemarker/configuration/MacroLibrary.java 22 Dec 2006 18:03:51 -0000 @@ -32,7 +32,7 @@ import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.Path; -import org.jboss.ide.eclipse.freemarker.Plugin; +import org.jboss.ide.eclipse.freemarker.FreemarkerPlugin; import org.jboss.ide.eclipse.freemarker.model.LibraryMacroDirective; import org.jboss.ide.eclipse.freemarker.model.MacroDirective; import org.jboss.ide.eclipse.freemarker.util.StringUtil; @@ -139,7 +139,7 @@ } catch (Exception e) { macros = new MacroDirective[0]; - Plugin.log(e); + FreemarkerPlugin.log(e); } } Index: src/org/jboss/ide/eclipse/freemarker/dialogs/ContextValueDialog.java =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/src/org/jboss/ide/eclipse/freemarker/dialogs/ContextValueDialog.java,v retrieving revision 1.2 diff -u -r1.2 ContextValueDialog.java --- src/org/jboss/ide/eclipse/freemarker/dialogs/ContextValueDialog.java 2 Oct 2006 23:08:08 -0000 1.2 +++ src/org/jboss/ide/eclipse/freemarker/dialogs/ContextValueDialog.java 22 Dec 2006 18:03:51 -0000 @@ -22,19 +22,25 @@ package org.jboss.ide.eclipse.freemarker.dialogs; +import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.core.search.IJavaSearchScope; import org.eclipse.jdt.core.search.SearchEngine; import org.eclipse.jdt.internal.corext.util.JavaModelUtil; import org.eclipse.jdt.ui.JavaUI; -import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jface.dialogs.StatusDialog; import org.eclipse.jface.window.ApplicationWindow; import org.eclipse.swt.SWT; +import org.eclipse.swt.events.ModifyEvent; +import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.MouseListener; import org.eclipse.swt.graphics.Color; @@ -43,28 +49,47 @@ import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; +import org.eclipse.ui.dialogs.ContainerSelectionDialog; import org.eclipse.ui.dialogs.SelectionDialog; +import org.jboss.ide.eclipse.freemarker.FreemarkerMessages; +import org.jboss.ide.eclipse.freemarker.FreemarkerPlugin; import org.jboss.ide.eclipse.freemarker.configuration.ConfigurationManager; -import org.jboss.ide.eclipse.freemarker.configuration.ContextValue; +import org.jboss.ide.eclipse.freemarker.configuration.context.IContextValue; +import org.jboss.ide.eclipse.freemarker.configuration.context.IObjectContext; /** * @author Joe Hudson */ -public class ContextValueDialog extends Dialog { +public class ContextValueDialog extends StatusDialog { + private final static int NO_CONTEXT_SELECTED = -1; + private IResource resource; - private ContextValue contextValue; + private IContextValue contextValue; private Text keyText; + + private Label labelText; private Text valueText; private Text singleValueText; private Label singleLabel; private Button singleBrowse; - - public ContextValueDialog(Shell parentShell, ContextValue contextValue, IResource resource) { + + // Manage radio button to select a context type + // (Class, XML) + private Button classRadioButton; + private Button xmlRadioButton; + + // Browse Button to select a class or XMl file + private Button browseButton; + + public ContextValueDialog(Shell parentShell, IContextValue contextValue, IResource resource) { super(parentShell); this.resource = resource; this.contextValue = contextValue; @@ -74,7 +99,7 @@ * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell) */ protected void configureShell(Shell newShell) { - newShell.setText("Context Value Configuration"); + newShell.setText(FreemarkerMessages.getString("ContextValueDialog.title")); super.configureShell(newShell); } @@ -82,73 +107,193 @@ Composite composite = new Composite(parent, SWT.NULL); composite.setLayout(new GridLayout(3, false)); + Label label = new Label(composite, 0); + label.setText(FreemarkerMessages.getString("ContextValueDialog.contextType.label")); + final Group group = new Group(composite, SWT.NONE); + final GridLayout gridLayout = new GridLayout(); + gridLayout.numColumns = 4; + group.setLayout(gridLayout); + + classRadioButton = new Button(group, SWT.RADIO); + classRadioButton.setText(FreemarkerMessages.getString("ContextValueDialog.classRadioButton.text")); + classRadioButton.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event event) { + handleRadioButon(true); + } + }); + /*stringRadioButton = new Button(group, SWT.RADIO); + stringRadioButton.setText("String"); + stringRadioButton.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event event) { + handleRadioButon(true); + } + });*/ + xmlRadioButton = new Button(group, SWT.RADIO); + xmlRadioButton.setText(FreemarkerMessages.getString("ContextValueDialog.xmlRadioButton.text")); + xmlRadioButton.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event event) { + handleRadioButon(true); + } + }); + /*dtdRadioButton = new Button(group, SWT.RADIO); + dtdRadioButton.setText("dtd"); + dtdRadioButton.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event event) { + handleRadioButon(true); + } + });*/ + GridData gd = new GridData(768); + gd.horizontalSpan = 2; + gd.widthHint = 200; + group.setLayoutData(gd); + + // Select radio swith context type + if(contextValue != null) { + if (contextValue.getType() == IContextValue.CLASS) + classRadioButton.setSelection(true); + else { + /*if (contextValue.getType() == IContextValue.STRING) + stringRadioButton.setSelection(true); + else {*/ + if (contextValue.getType() == IContextValue.XML) + xmlRadioButton.setSelection(true); + else { + // if (contextValue.getType() == IContextValue.DTD) + // dtdRadioButton.setSelection(true); + } + //} + } + } + else { + // By default class is selected + classRadioButton.setSelection(true); + } + + label = new Label(composite, SWT.NULL); + label.setText(FreemarkerMessages.getString("ContextValueDialog.key.label")); + keyText = new Text(composite, SWT.BORDER); + if (null != contextValue) { + keyText.setText(contextValue.getName()); + keyText.setEnabled(false); + } + gd = new GridData(GridData.FILL_HORIZONTAL); + gd.horizontalSpan = 2; + gd.widthHint = 200; + keyText.setLayoutData(gd); + keyText.addModifyListener(new ModifyListener() { + public void modifyText(ModifyEvent e) { + dialogChanged(); + } + }); + + labelText = new Label(composite, SWT.NULL); + valueText = new Text(composite, SWT.BORDER); + valueText.setEnabled(false); + valueText.setBackground(new Color(null, 255, 255, 255)); + gd = new GridData(GridData.FILL_HORIZONTAL); + gd.widthHint = 250; + valueText.setLayoutData(gd); + // Manage context value for XML, String or class + if(contextValue != null) { + IObjectContext objClass = contextValue.getFirstObjectContext(); + if(objClass != null) { + String objectClassName = objClass.getName(); + if (objectClassName != null) + valueText.setText(objectClassName); + } - Label label = new Label(composite, SWT.NULL); - label.setText("Name:"); - keyText = new Text(composite, SWT.BORDER); - if (null != contextValue) { - keyText.setText(contextValue.name); - keyText.setEnabled(false); - } - GridData gd = new GridData(GridData.FILL_HORIZONTAL); - gd.horizontalSpan = 2; - gd.widthHint = 200; - keyText.setLayoutData(gd); - - label = new Label(composite, SWT.NULL); - label.setText("Type:"); - valueText = new Text(composite, SWT.BORDER); - valueText.setEnabled(false); - valueText.setBackground(new Color(null, 255, 255, 255)); - gd = new GridData(GridData.FILL_HORIZONTAL); - gd.widthHint = 250; - valueText.setLayoutData(gd); - if (null != contextValue && null != contextValue.objClass) valueText.setText(contextValue.objClass.getName()); - Button browse = new Button(composite, 8); - browse.setText("Browse"); - browse.addMouseListener(new MouseListener() { + } + valueText.addModifyListener(new ModifyListener() { + public void modifyText(ModifyEvent e) { + dialogChanged(); + } + }); + + browseButton = new Button(composite, 8); + browseButton.setText(FreemarkerMessages.getString("ContextValueDialog.browseButton.text")); + browseButton.addMouseListener(new MouseListener() { public void mouseDown(MouseEvent e) { try { - IJavaProject javaProject = JavaCore.create(resource.getProject()); - if(javaProject != null) - { - org.eclipse.jdt.core.search.IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(new IJavaElement[]{javaProject}); - SelectionDialog sd = JavaUI.createTypeDialog(getShell(), new ApplicationWindow(getShell()), searchScope, 2, false); - sd.open(); - Object objects[] = sd.getResult(); - if(objects != null && objects.length > 0) + int contextType = getContextTypeSelected(); + if (contextType == IContextValue.CLASS) { + // Context type selected is CLASS, open dialog in order + // to display classes availables. + IJavaProject javaProject = JavaCore.create(resource.getProject()); + if(javaProject != null) { - IType type = (IType)objects[0]; - String fullyQualifiedName = JavaModelUtil.getFullyQualifiedName(type); - valueText.setText(type.getFullyQualifiedName()); - String[] interfaces = type.getSuperInterfaceNames(); - boolean isList = false; - if ("java.lang.Object".equals(fullyQualifiedName)) isList = true; - else { - for (int i=0; i 0) + { + IType type = (IType)objects[0]; + String fullyQualifiedName = JavaModelUtil.getFullyQualifiedName(type); + valueText.setText(type.getFullyQualifiedName()); + String interfaces[] = type.getSuperInterfaceNames(); + boolean isList = false; + if("java.lang.Object".equals(fullyQualifiedName)) + { + isList = true; + } else + { + for(int i = 0; i < interfaces.length; i++) + { + if(!interfaces[i].equals("java.util.Collection") && !interfaces[i].equals("java.util.List") && !interfaces[i].equals("java.util.Set")) + continue; + isList = true; + break; + } + + } + if(isList) + { + singleBrowse.setEnabled(true); + singleLabel.setEnabled(true); + singleValueText.setEnabled(true); + } else + { + singleBrowse.setEnabled(false); + singleLabel.setEnabled(false); + singleValueText.setEnabled(false); + singleValueText.setText(""); + } } + } else + { + MessageDialog.openError(getShell(), "Java Project Error", "The project must be a java project."); } } else { - MessageDialog.openError(getShell(), "Java Project Error", "The project must be a java project."); - } + if (contextType == IContextValue.XML || + contextType == IContextValue.DTD) { + // Context type selected is XML, open dialog in order + // to display XML files availables. + OpenResourceDialog dialog = + new OpenResourceDialog (getShell(), resource.getProject()); + //dialog.setInitialSelections(selectedResources); + if (dialog.open() == ContainerSelectionDialog.OK) + { + Object[] result = dialog.getResult(); + if (result.length == 1) + { + IFile xmlFile = (IFile)(result[0]); + if (xmlFile != null) { + //IFile file = resource.getProject().getFile(xmlFile); + valueText.setText(xmlFile.getProjectRelativePath().toString()); + // Load XML file into DOM + + + //MessageDialog.openError(getShell(), "Java Project Error", "XML file is not valid."); + + } + } + } + } + } } catch(JavaModelException _ex) { } } @@ -164,17 +309,24 @@ }); boolean enabled = false; - if (null != contextValue && null != contextValue.singularClass) enabled = true; - singleLabel = new Label(composite, SWT.NULL); - singleLabel.setEnabled(enabled); - singleLabel.setText("List Entry Type:"); - singleValueText = new Text(composite, SWT.BORDER); - singleValueText.setEnabled(enabled); - singleValueText.setBackground(new Color(null, 255, 255, 255)); - gd = new GridData(GridData.FILL_HORIZONTAL); - gd.widthHint = 250; - singleValueText.setLayoutData(gd); - if (enabled) singleValueText.setText(contextValue.singularClass.getName()); + String singularClassName = null; + // Manage context value for XML, String or class + if (contextValue != null) { + IObjectContext singularClass = contextValue.getSecondObjectContext(); + if(singularClass != null) { + singularClassName = singularClass.getName(); + } + } + singleLabel = new Label(composite, SWT.NULL); + singleLabel.setEnabled(enabled); + singleLabel.setText("List Entry Type:"); + singleValueText = new Text(composite, SWT.BORDER); + singleValueText.setEnabled(enabled); + singleValueText.setBackground(new Color(null, 255, 255, 255)); + gd = new GridData(GridData.FILL_HORIZONTAL); + gd.widthHint = 250; + singleValueText.setLayoutData(gd); + if (enabled) singleValueText.setText(singularClassName); singleBrowse = new Button(composite, 8); singleBrowse.setEnabled(enabled); singleBrowse.setText("Browse"); @@ -185,7 +337,7 @@ IJavaProject javaProject = JavaCore.create(resource.getProject()); if(javaProject != null) { - org.eclipse.jdt.core.search.IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(new IJavaElement[]{javaProject}); + IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(new IJavaElement[]{javaProject}); SelectionDialog sd = JavaUI.createTypeDialog(getShell(), new ApplicationWindow(getShell()), searchScope, 2, false); sd.open(); Object objects[] = sd.getResult(); @@ -211,29 +363,157 @@ } }); - return parent; - } - - protected void okPressed() { - try { - String name = keyText.getText().trim(); - while (name.startsWith("$")) name = name.substring(1, name.length()); - if (name.length() == 0) MessageDialog.openError(getShell(), "Error", "You must choose the reference name"); - String className = valueText.getText().trim(); - if (className.length() == 0) MessageDialog.openError(getShell(), "Error", "You must choose the class name"); - String singularClassName = singleValueText.getText().trim(); - Class singularClass = null; - if (null != singularClassName && singularClassName.trim().length() > 0) singularClass = ConfigurationManager.getInstance(resource.getProject()).getClass(singularClassName); - if (className.length() == 0) singularClassName = null; - contextValue = new ContextValue( - name, - ConfigurationManager.getInstance(resource.getProject()).getClass(className), - singularClass); - ConfigurationManager.getInstance(resource.getProject()).addContextValue(contextValue, resource); - } - catch (Exception e) { - MessageDialog.openError(getShell(), "Error", e.getMessage()); - } - super.okPressed(); - } + // Manage disable/enable for radio button + handleRadioButon(false); + // dialogChanged(); + return parent; + } + + protected void okPressed() { + try { + String name = keyText.getText().trim(); + while (name.startsWith("$")) name = name.substring(1, name.length()); + String className = valueText.getText().trim(); + String singularClassName = singleValueText.getText().trim(); + // Coded added by AZ : + // Manage context value for XML, String or class + // Get context type + int contextType = getContextTypeSelected(); + IContextValue contextValue = ConfigurationManager.getInstance(resource.getProject()). + getInstanceOfContextValue(contextType, name, className, singularClassName); + if (contextValue != null) + ConfigurationManager.getInstance(resource.getProject()).addContextValue(contextValue, resource); + } + catch (Exception e) { + MessageDialog.openError(getShell(), "Error", e.getMessage()); + } + super.okPressed(); + } + + private void handleRadioButon(boolean initialize) { + // 1. manage Key text + // It is enable ONLY if there is not exist context value + keyText.setEnabled(contextValue == null); + if (initialize) { + singleValueText.setEnabled(false); + singleBrowse.setEnabled(false); + singleLabel.setEnabled(false); + } + boolean selected = classRadioButton.getSelection(); + if (selected) { + // Class radio button is selected + valueText.setEnabled(false); + browseButton.setEnabled(true); + labelText.setText(FreemarkerMessages.getString("ContextValueDialog.labelText.CLASS")); + } + else { + //selected = stringRadioButton.getSelection(); + if (selected) { + // String radio button is selected + /*valueText.setEnabled(true); + valueText.setFocus(); + browse.setEnabled(false); + singleBrowse.setEnabled(false); + labelText.setText("Value:");*/ + } + else { + selected = xmlRadioButton.getSelection(); + if (selected) { + // XML radio button is selected + valueText.setEnabled(false); + browseButton.setEnabled(true); + singleBrowse.setEnabled(false); + labelText.setText(FreemarkerMessages.getString("ContextValueDialog.labelText.XML")); + } + else { + /*selected = dtdRadioButton.getSelection(); + if (selected) { + // DTD radio button is selected + valueText.setEnabled(false); + browse.setEnabled(true); + singleBrowse.setEnabled(false); + labelText.setText("DTD:"); + }*/ + } + } + } + if (selected && initialize) { + valueText.setText(""); + singleValueText.setText(""); + } + String key = keyText.getText(); + if (keyText.isEnabled() && + key == null || key.trim().length() < 1) { + keyText.setFocus(); + } + } + + private int getContextTypeSelected() { + if ( classRadioButton.getSelection()) + return IContextValue.CLASS; + //if ( stringRadioButton.getSelection()) + // return IContextValue.STRING; + if ( xmlRadioButton.getSelection()) + return IContextValue.XML; + //if ( dtdRadioButton.getSelection()) + // return IContextValue.DTD; + return NO_CONTEXT_SELECTED; + } + + private void dialogChanged() { + // Key validation + String name = keyText.getText().trim(); + while (name.startsWith("$")) name = name.substring(1, name.length()); + if (name.length() == 0) { + updateMessageStatus(FreemarkerMessages.getString("ContextValueDialog.context.name.required")); + return; + } + + // Value validation + String className = valueText.getText().trim(); + if(className.length() == 0) { + String errorMessage = ""; + int contexType = getContextTypeSelected(); + if (contexType == IContextValue.CLASS) { + // Choose the class name by using Browse button! + errorMessage = FreemarkerMessages.getString("ContextValueDialog.context.CLASS.required"); + } + else { + if (contexType == IContextValue.STRING) { + //errorMessage = "Select a value for the string!"; + } + else { + if (contexType == IContextValue.XML) { + // Choose a XML file by using Browse button!" + errorMessage = FreemarkerMessages.getString("ContextValueDialog.context.XML.required"); + } + else { + /* if (contexType == IContextValue.DTD) { + errorMessage = "Choose a DTD file by using Browse button!"; + }*/ + } + } + } + updateMessageStatus(errorMessage); + return; + } + updateMessageStatus(null); + } + + private void updateMessageStatus(String message) { + IStatus status = null; + if (message != null) { + status = new Status(IStatus.ERROR, FreemarkerPlugin.getId(), IStatus.ERROR, message, null); + } + else { + status = new Status(IStatus.INFO, FreemarkerPlugin.getId(), IStatus.OK, + FreemarkerMessages.getString("ContextValueDialog.context.message"), null); + } + super.updateStatus(status); + } + + public void create() { + super.create(); + dialogChanged(); + } } \ No newline at end of file Index: src/org/jboss/ide/eclipse/freemarker/editor/ColorManager.java =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/src/org/jboss/ide/eclipse/freemarker/editor/ColorManager.java,v retrieving revision 1.3 diff -u -r1.3 ColorManager.java --- src/org/jboss/ide/eclipse/freemarker/editor/ColorManager.java 2 Oct 2006 23:08:06 -0000 1.3 +++ src/org/jboss/ide/eclipse/freemarker/editor/ColorManager.java 22 Dec 2006 18:03:51 -0000 @@ -28,7 +28,7 @@ import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.widgets.Display; -import org.jboss.ide.eclipse.freemarker.Plugin; +import org.jboss.ide.eclipse.freemarker.FreemarkerPlugin; /** * @author Joe Hudson @@ -44,7 +44,7 @@ public Color getColor (String colorName) { RGB rgb = PreferenceConverter.getColor( - Plugin.getDefault().getPreferenceStore(), + FreemarkerPlugin.getDefault().getPreferenceStore(), colorName); return getColor(rgb); } Index: src/org/jboss/ide/eclipse/freemarker/editor/CompletionProcessor.java =================================================================== RCS file: src/org/jboss/ide/eclipse/freemarker/editor/CompletionProcessor.java diff -N src/org/jboss/ide/eclipse/freemarker/editor/CompletionProcessor.java --- src/org/jboss/ide/eclipse/freemarker/editor/CompletionProcessor.java 2 Oct 2006 23:08:06 -0000 1.2 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,188 +0,0 @@ -/* - * JBoss, a division of Red Hat - * Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated - * by the @authors tag. See the copyright.txt in the distribution for a - * full listing of individual contributors. - * - * This is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1 of - * the License, or (at your option) any later version. - * - * This software 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this software; if not, write to the Free - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA - * 02110-1301 USA, or see the FSF site: http://www.fsf.org. - */ -package org.jboss.ide.eclipse.freemarker.editor; - - -import java.util.HashMap; -import java.util.Map; - -import org.eclipse.core.resources.IResource; -import org.eclipse.jface.text.BadLocationException; -import org.eclipse.jface.text.IRegion; -import org.eclipse.jface.text.ITextViewer; -import org.eclipse.jface.text.contentassist.CompletionProposal; -import org.eclipse.jface.text.contentassist.ICompletionProposal; -import org.eclipse.jface.text.contentassist.IContentAssistProcessor; -import org.eclipse.jface.text.source.ISourceViewer; -import org.eclipse.jface.text.templates.Template; -import org.eclipse.jface.text.templates.TemplateCompletionProcessor; -import org.eclipse.jface.text.templates.TemplateContextType; -import org.eclipse.swt.graphics.Image; -import org.jboss.ide.eclipse.freemarker.Plugin; -import org.jboss.ide.eclipse.freemarker.configuration.ConfigurationManager; -import org.jboss.ide.eclipse.freemarker.configuration.ContextValue; -import org.jboss.ide.eclipse.freemarker.model.CompletionDirective; -import org.jboss.ide.eclipse.freemarker.model.CompletionInterpolation; -import org.jboss.ide.eclipse.freemarker.model.CompletionMacroInstance; -import org.jboss.ide.eclipse.freemarker.model.Item; -import org.jboss.ide.eclipse.freemarker.model.ItemSet; -import org.jboss.ide.eclipse.freemarker.model.MacroInstance; - -/** - * @author Joe Hudson - */ -public class CompletionProcessor extends TemplateCompletionProcessor implements IContentAssistProcessor { - - private Editor editor; - - - public CompletionProcessor (Editor editor) { - this.editor = editor; - } - - public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) { - try { - ItemSet directiveSet = editor.getItemSet(); - - Map context = new HashMap(); - ContextValue[] values = ConfigurationManager.getInstance(editor.getProject()).getContextValues(editor.getFile(), true); - for (int i=0; i=topOffset; i--) { - char c = editor.getDocument().getChar(i); - if (c == '>' || c == ']') break; - if (c == '<' || c == '[') { - if (editor.getDocument().getLength() > i) { - char c2 = editor.getDocument().getChar(i+1); - if (c2 == '#') { - CompletionDirective completionDirective = new CompletionDirective( - i, offset - i, editor.getItemSet(), (ISourceViewer) viewer, (IResource) editor.getFile()); - completionDirective.setItemSet(editor.getItemSet()); - return completionDirective.getCompletionProposals(offset, context); - } - else if (c2 == '@') { - CompletionMacroInstance completionMacroInstance = new CompletionMacroInstance( - editor.getDocument().get(i, offset - i), i, editor.getItemSet(), editor.getFile()); - completionMacroInstance.setItemSet(editor.getItemSet()); - return completionMacroInstance.getCompletionProposals(offset, context); - } - else if (c2 == '/') { - if (editor.getDocument().getLength() < i+3 - || editor.getDocument().getChar(i+2) == ' ' - || editor.getDocument().getChar(i+2) == '\r' - || editor.getDocument().getChar(i+2) == '\n') { - Item stackItem = editor.getItemSet().getPreviousStartItem(offset); - StringBuffer value = new StringBuffer(); - if (null != stackItem && stackItem instanceof MacroInstance) - value.append("@"); - else - value.append("#"); - String name = null; - if (null != stackItem) name = stackItem.getFirstToken(); - if (null != name) - value.append(name); - if (c == '<') - value.append('>'); - else - value.append(']'); - ICompletionProposal completionProposal = new CompletionProposal( - value.toString(), offset, 0, offset+value.toString().length()); - return new ICompletionProposal[]{completionProposal}; - } - } - else { - return null; - } - } - } - } - } - catch (BadLocationException e) { - return null; - } - // check for interpolations - try { - for (int i=offset-1; i>=topOffset; i--) { - char c = editor.getDocument().getChar(i); - if (c == '\n') break; - else if (c == '$') { - if (editor.getDocument().getLength() > i) { - char c2 = editor.getDocument().getChar(i+1); - if (c2 == '{') { - int j = offset; - while (editor.getDocument().getLength() > j) { - char c3 = editor.getDocument().getChar(j); - if (Character.isWhitespace(c3) || c3 == '(' || c3 == '.' || c3 == ')' || c3 == '}' || c3 == '?') { - // j = j-1; - break; - } - j++; - } - CompletionInterpolation interpolation = new CompletionInterpolation( - editor.getDocument().get(i, j - i), i, editor.getItemSet(), editor.getFile()); - interpolation.setParentItem(editor.getItemSet().getPreviousStartItem(offset)); - return interpolation.getCompletionProposals(offset, context); - } - } - } - } - } - catch (BadLocationException e) { - return null; - } - } - } - catch (Exception e) { - Plugin.log(e); - } - return null; - } - - protected TemplateContextType getContextType(ITextViewer viewer, IRegion region) { - return null; - } - - protected Image getImage(Template template) { - return null; - } - - protected Template[] getTemplates(String contextTypeId) { - return null; - } - - public char[] getCompletionProposalAutoActivationCharacters() { - return new char[]{'.', '$', '#', '@', '/', '?', '{'}; - } -} \ No newline at end of file Index: src/org/jboss/ide/eclipse/freemarker/editor/Configuration.java =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/src/org/jboss/ide/eclipse/freemarker/editor/Configuration.java,v retrieving revision 1.2 diff -u -r1.2 Configuration.java --- src/org/jboss/ide/eclipse/freemarker/editor/Configuration.java 2 Oct 2006 23:08:06 -0000 1.2 +++ src/org/jboss/ide/eclipse/freemarker/editor/Configuration.java 22 Dec 2006 18:03:52 -0000 @@ -36,6 +36,8 @@ import org.eclipse.jface.text.source.ISourceViewer; import org.eclipse.ui.editors.text.TextSourceViewerConfiguration; import org.jboss.ide.eclipse.freemarker.Constants; +import org.jboss.ide.eclipse.freemarker.contentassist.CompletionProcessor; +import org.jboss.ide.eclipse.freemarker.editor.editorpart.SourceEditor; /** * @author Joe Hudson @@ -44,9 +46,9 @@ private ColorManager colorManager; private XMLTagScanner xmlTagScanner; private DirectiveScanner directiveScanner; - private Editor editor; + private SourceEditor editor; - public Configuration(IPreferenceStore preferenceStore, ColorManager colorManager, Editor editor) { + public Configuration(IPreferenceStore preferenceStore, ColorManager colorManager, SourceEditor editor) { super(preferenceStore); this.editor = editor; this.colorManager = colorManager; Index: src/org/jboss/ide/eclipse/freemarker/editor/Editor.java =================================================================== RCS file: src/org/jboss/ide/eclipse/freemarker/editor/Editor.java diff -N src/org/jboss/ide/eclipse/freemarker/editor/Editor.java --- src/org/jboss/ide/eclipse/freemarker/editor/Editor.java 2 Oct 2006 23:08:06 -0000 1.3 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,493 +0,0 @@ -/* - * JBoss, a division of Red Hat - * Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated - * by the @authors tag. See the copyright.txt in the distribution for a - * full listing of individual contributors. - * - * This is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1 of - * the License, or (at your option) any later version. - * - * This software 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this software; if not, write to the Free - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA - * 02110-1301 USA, or see the FSF site: http://www.fsf.org. - */ -package org.jboss.ide.eclipse.freemarker.editor; - -import java.io.Reader; -import java.io.StringReader; -import java.util.HashMap; -import java.util.Map; - -import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IMarker; -import org.eclipse.core.resources.IProject; -import org.eclipse.core.resources.IResource; -import org.eclipse.jdt.internal.ui.javaeditor.JarEntryEditorInput; -import org.eclipse.jdt.internal.ui.text.JavaPairMatcher; -import org.eclipse.jface.dialogs.MessageDialog; -import org.eclipse.jface.text.BadLocationException; -import org.eclipse.jface.text.IDocument; -import org.eclipse.jface.text.ITextViewerExtension2; -import org.eclipse.jface.text.ITypedRegion; -import org.eclipse.jface.text.source.ISourceViewer; -import org.eclipse.jface.text.source.MatchingCharacterPainter; -import org.eclipse.jface.text.source.SourceViewer; -import org.eclipse.swt.SWT; -import org.eclipse.swt.custom.StyleRange; -import org.eclipse.swt.events.KeyEvent; -import org.eclipse.swt.events.KeyListener; -import org.eclipse.swt.events.MouseEvent; -import org.eclipse.swt.events.MouseListener; -import org.eclipse.swt.widgets.Composite; -import org.eclipse.ui.IFileEditorInput; -import org.eclipse.ui.editors.text.TextEditor; -import org.eclipse.ui.texteditor.ContentAssistAction; -import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds; -import org.eclipse.ui.texteditor.MarkerUtilities; -import org.eclipse.ui.views.contentoutline.IContentOutlinePage; -import org.jboss.ide.eclipse.freemarker.Constants; -import org.jboss.ide.eclipse.freemarker.Plugin; -import org.jboss.ide.eclipse.freemarker.configuration.ConfigurationManager; -import org.jboss.ide.eclipse.freemarker.model.Item; -import org.jboss.ide.eclipse.freemarker.model.ItemSet; -import org.jboss.ide.eclipse.freemarker.outline.OutlinePage; - -import freemarker.core.ParseException; -import freemarker.template.Configuration; -import freemarker.template.Template; - -/** - * @author Joe Hudson - */ -public class Editor extends TextEditor implements KeyListener, MouseListener { - - private OutlinePage fOutlinePage; - private org.jboss.ide.eclipse.freemarker.editor.Configuration configuration; - private ColorManager colorManager = new ColorManager(); - - private ItemSet itemSet; - private Item selectedItem; - private Item[] relatedItems; - private static final char[] VALIDATION_TOKENS = new char[]{'\"', '[', ']', ',', '.', '\n', '4'}; - private boolean readOnly = false; - - private boolean mouseDown = false; - private boolean ctrlDown = false; - private boolean shiftDown = false; - - public Editor() { - super(); - configuration = new org.jboss.ide.eclipse.freemarker.editor.Configuration(getPreferenceStore(), colorManager, this); - setSourceViewerConfiguration(configuration); - setDocumentProvider(new DocumentProvider()); - } - public void dispose() { - ConfigurationManager.getInstance(getProject()).reload(); - super.dispose(); - if(matchingCharacterPainter!=null) { - matchingCharacterPainter.dispose(); - } - } - - public Object getAdapter(Class aClass) { - Object adapter; - if (aClass.equals(IContentOutlinePage.class)) { - if (fOutlinePage == null) { - fOutlinePage = new OutlinePage(this); - if (getEditorInput() != null) { - fOutlinePage.setInput(getEditorInput()); - } - } - adapter = fOutlinePage; - } else { - adapter = super.getAdapter(aClass); - } - return adapter; - } - - protected static final char[] BRACKETS= {'{', '}', '(', ')', '[', ']', '<', '>' }; - private MatchingCharacterPainter matchingCharacterPainter; - public void createPartControl(Composite parent) { - super.createPartControl(parent); - getSourceViewer().getTextWidget().addKeyListener(this); - getSourceViewer().getTextWidget().addMouseListener(this); - //matchingCharacterPainter = new MatchingCharacterPainter( - // getSourceViewer(), - // new JavaPairMatcher(BRACKETS)); - //((SourceViewer) getSourceViewer()).addPainter(matchingCharacterPainter); - } - - protected void createActions() { - super.createActions(); - // Add content assist propsal action - ContentAssistAction action = new ContentAssistAction( - Plugin.getDefault().getResourceBundle(), - "FreemarkerEditor.ContentAssist", this); - action.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS); - setAction("FreemarkerEditor.ContentAssist", action); - action.setEnabled(true); - } - - protected void handleCursorPositionChanged() { - super.handleCursorPositionChanged(); - if (!mouseDown) { - int offset = getCaretOffset(); - Item item = getItemSet().getSelectedItem(offset); - if (null == item && offset > 0) item = getItemSet().getSelectedItem(offset-1); - if (Plugin.getInstance().getPreferenceStore().getBoolean( - Constants.HIGHLIGHT_RELATED_ITEMS)) { - if (null != item && null != item.getRelatedItems() && item.getRelatedItems().length > 0) { - highlightRelatedRegions(item.getRelatedItems(), item); - } - else { - highlightRelatedRegions(null, item); - } - } - if (null == item) { - item = getItemSet().getContextItem(getCaretOffset()); - } - if (null != fOutlinePage) - fOutlinePage.update(item); - } - } - public void mouseDoubleClick(MouseEvent e) { - } - public void mouseDown(MouseEvent e) { - mouseDown = true; - } - public void mouseUp(MouseEvent e) { - mouseDown = false; - handleCursorPositionChanged(); - } - - public void select (Item item) { - selectAndReveal(item.getRegion().getOffset(), item.getRegion().getLength()); - } - - public IDocument getDocument() { - ISourceViewer viewer = getSourceViewer(); - if (viewer != null) { - return viewer.getDocument(); - } - return null; - } - - public void addProblemMarker(String aMessage, int aLine) { - IFile file = ((IFileEditorInput)getEditorInput()).getFile(); - try { - Map attributes = new HashMap(5); - attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR)); - attributes.put(IMarker.LINE_NUMBER, new Integer(aLine)); - attributes.put(IMarker.MESSAGE, aMessage); - attributes.put(IMarker.TEXT, aMessage); - MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM); - } catch (Exception e) { - - } - } - - private synchronized void highlightRelatedRegions (Item[] items, Item selectedItem) { - if (null == items || items.length == 0) { - if (null != relatedItems && relatedItems.length > 0) { - for (int i=0; i= relatedItems[i].getRegion().getOffset() + relatedItems[i].getRegion().getLength()) { - if (null == this.selectedItem || !relatedItems[i].equals(this.selectedItem)) - resetRange(relatedItems[i].getRegion()); - } - } - } - relatedItems = null; - } - if (null != relatedItems) { - for (int i=0; i= relatedItems[i].getRegion().getOffset() + relatedItems[i].getRegion().getLength()) { - if (null == this.selectedItem || !relatedItems[i].equals(this.selectedItem)) - resetRange(relatedItems[i].getRegion()); - } - } - } - if (null != items && items.length > 0) { - for (int i=0; i= items[i].getRegion().getOffset() + items[i].getRegion().getLength() - && !items[i].equals(selectedItem)) { - ITypedRegion region = items[i].getRegion(); - getSourceViewer().getTextWidget().setStyleRange( - new StyleRange(region.getOffset(), - region.getLength(), null, - colorManager.getColor( - Constants.COLOR_RELATED_ITEM))); - } - } - } - relatedItems = items; - this.selectedItem = selectedItem; - } - - private void resetRange (ITypedRegion region) { - if (getSourceViewer() instanceof ITextViewerExtension2) - ((ITextViewerExtension2) getSourceViewer()).invalidateTextPresentation(region.getOffset(), region.getLength()); - else - getSourceViewer().invalidateTextPresentation(); - } - - public Item getSelectedItem (boolean allowFudge) { - int caretOffset = getCaretOffset(); - Item item = getItemSet().getSelectedItem(getCaretOffset()); - if (null == item && caretOffset > 0) item = getItemSet().getSelectedItem(caretOffset - 1); - return item; - } - - public Item getSelectedItem () { - return getItemSet().getSelectedItem(getCaretOffset()); - } - - public int getCaretOffset () { - return getSourceViewer().getTextWidget().getCaretOffset(); - } - - private void clearCache () { - this.itemSet = null; - } - - public ItemSet getItemSet () { - if (null == this.itemSet) { - IResource resource = null; - if (getEditorInput() instanceof IFileEditorInput) { - resource = ((IFileEditorInput) getEditorInput()).getFile(); - } - else if (getEditorInput() instanceof JarEntryEditorInput) { - resource = null; - } - - this.itemSet = new ItemSet( - getSourceViewer(), resource); - } - return this.itemSet; - - } - public OutlinePage getOutlinePage() { - return fOutlinePage; - } - - public void keyPressed(KeyEvent e) { - if (e.keyCode == SWT.CTRL) { - ctrlDown = true; - } - if (e.keyCode == SWT.SHIFT) { - shiftDown = true; - } - if (e.keyCode == ']') { - try { - char c = getDocument().getChar(getCaretOffset()); - if (c == ']') { - // remove this - getDocument().replace(getCaretOffset(), 1, ""); - } - } - catch (BadLocationException e1) {} - } - else if (e.keyCode == '}') { - try { - char c = getDocument().getChar(getCaretOffset()); - if (c == '}') { - // remove this - getDocument().replace(getCaretOffset(), 1, "}"); - } - } - catch (BadLocationException e1) {} - } - } - - public void keyReleased(KeyEvent e) { - if (e.keyCode == SWT.CTRL) { - ctrlDown = false; - } - else if (e.keyCode == SWT.SHIFT) { - shiftDown = false; - } - try { - if (shiftDown && (e.keyCode == '3' || e.keyCode == '2')) { - int offset = getCaretOffset(); - char c = getSourceViewer().getDocument().getChar(offset-2); - if (c == '[' || c == '<') { - // directive - char endChar = Character.MIN_VALUE; - if (c == '[') endChar = ']'; else endChar = '>'; - if (getSourceViewer().getDocument().getLength() > offset) { - if (offset > 0) { - for (int i=offset+1; i offset) { - if (offset > 0) { - for (int i=offset+1; i 0) item = getItemSet().getSelectedItem(offset-1); - if (Plugin.getInstance().getPreferenceStore().getBoolean( - Constants.HIGHLIGHT_RELATED_ITEMS)) { - if (null != item && null != item.getRelatedItems() && item.getRelatedItems().length > 0) { - highlightRelatedRegions(item.getRelatedItems(), item); - } - else { - highlightRelatedRegions(null, item); - } - } - clearCache(); - validateContents(); - if (null != fOutlinePage) - fOutlinePage.update(getSelectedItem()); - } - } - - private void showMessage (String text) { - MessageDialog.openError( - getSourceViewer().getTextWidget().getShell(), - "message", - text); - } - - public static Validator VALIDATOR; - public synchronized void validateContents () { - if (null == VALIDATOR) { - VALIDATOR = new Validator(this); - VALIDATOR.start(); - } - } - - public IProject getProject () { - return ((IFileEditorInput) getEditorInput()).getFile().getProject(); - } - - public IFile getFile () { - return (null != getEditorInput()) ? - ((IFileEditorInput) getEditorInput()).getFile() : null; - } - - private Configuration fmConfiguration; - public class Validator extends Thread { - Editor editor; - public Validator (Editor editor) { - this.editor = editor; - } - public void run () { - try { - if (null != getFile()) { - if (null == fmConfiguration) { - fmConfiguration = new Configuration(); - fmConfiguration.setTagSyntax(Configuration.AUTO_DETECT_TAG_SYNTAX); - } - getFile().deleteMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE); - String pageContents = getDocument().get(); - Reader reader = new StringReader(pageContents); - new Template(getFile().getName(), reader, fmConfiguration); - reader.close(); - } - } - catch (ParseException e) { - if (e.getMessage() != null) { - String errorStr = e.getMessage(); - int errorLine = 0; - try { - errorLine = e.getLineNumber(); - if (errorLine == 0) { - // sometimes they forget to put it in - int index = e.getMessage().indexOf("line: "); - if (index > 0) { - int index2 = e.getMessage().indexOf(" ", index+6); - int index3 = e.getMessage().indexOf(",", index+6); - if (index3 < index2 && index3 > 0) index2 = index3; - String s = e.getMessage().substring(index+6, index2); - try { - errorLine = Integer.parseInt(s); - } - catch (Exception e2) {} - } - } - } catch (NullPointerException npe) { - errorLine = 0; - } - editor.addProblemMarker(errorStr, errorLine); - } - } - catch (Exception e) { - Plugin.log(e); - } - finally { - editor.VALIDATOR = null; - } - } - } - - protected void editorSaved() { - super.editorSaved(); - validateContents(); - } - - public boolean isEditorInputReadOnly() { - return readOnly; - } - public void setReadOnly(boolean readOnly) { - this.readOnly = readOnly; - } -} \ No newline at end of file Index: src/org/jboss/ide/eclipse/freemarker/editor/FreemarkerMultiPageEditor.java =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/src/org/jboss/ide/eclipse/freemarker/editor/FreemarkerMultiPageEditor.java,v retrieving revision 1.3 diff -u -r1.3 FreemarkerMultiPageEditor.java --- src/org/jboss/ide/eclipse/freemarker/editor/FreemarkerMultiPageEditor.java 2 Oct 2006 23:08:06 -0000 1.3 +++ src/org/jboss/ide/eclipse/freemarker/editor/FreemarkerMultiPageEditor.java 22 Dec 2006 18:03:52 -0000 @@ -64,10 +64,14 @@ import org.eclipse.ui.texteditor.IStatusField; import org.eclipse.ui.texteditor.ITextEditor; import org.eclipse.ui.texteditor.ITextEditorExtension; -import org.jboss.ide.eclipse.freemarker.Plugin; +import org.jboss.ide.eclipse.freemarker.FreemarkerMessages; +import org.jboss.ide.eclipse.freemarker.FreemarkerPlugin; import org.jboss.ide.eclipse.freemarker.configuration.ConfigurationManager; -import org.jboss.ide.eclipse.freemarker.configuration.ContextValue; +import org.jboss.ide.eclipse.freemarker.configuration.context.IContextValue; import org.jboss.ide.eclipse.freemarker.dialogs.ContextValueDialog; +import org.jboss.ide.eclipse.freemarker.editor.editorpart.PreviewEditor; +import org.jboss.ide.eclipse.freemarker.editor.editorpart.PreviewEditorInput; +import org.jboss.ide.eclipse.freemarker.editor.editorpart.SourceEditor; /** * @author Joe Hudson @@ -76,7 +80,7 @@ public static final String ID = "org.jboss.ide.eclipse.freemarker.editor.FreemarkerEditor"; - private Editor vEditor; + private SourceEditor vEditor; private String text; private boolean readOnly = false; @@ -86,15 +90,20 @@ protected void createPages() { - createPage0(); + createSourcePage(); if (!vEditor.isEditorInputReadOnly()) createContextPage(); + createPreviewPage(); } - void createPage0() + /** + * Create Source Page + * + */ + void createSourcePage() { try { - vEditor = new Editor(); + vEditor = new SourceEditor(); vEditor.init(getEditorSite(), getEditorInput()); if (readOnly) vEditor.setReadOnly(readOnly); int index = addPage(vEditor, getEditorInput()); @@ -106,7 +115,22 @@ } } - public Editor getEditor () { + /** + * Create Preview Page + * + */ + private void createPreviewPage() { + try { + int index = addPage(new PreviewEditor(), + new PreviewEditorInput(vEditor.getFile(), vEditor.getDocument())); + setPageText(index, "preview"); + } + catch (PartInitException e) { + ErrorDialog.openError(getSite().getShell(), "Error creating nested Preview Editor", null, e.getStatus()); + } + } + + public SourceEditor getEditor () { return vEditor; } @@ -203,18 +227,19 @@ public void reloadContextValues () { try { contextValuesTable.removeAll(); - ContextValue[] values = ConfigurationManager.getInstance( + IContextValue[] values = ConfigurationManager.getInstance( vEditor.getFile().getProject()).getContextValues(vEditor.getFile(), false); for (int i=0; i= 0) { String key = contextValuesTable.getSelection()[0].getText(0); - ContextValue value = ConfigurationManager.getInstance(vEditor.getFile().getProject()).getContextValue(key, vEditor.getFile(), false); + IContextValue value = ConfigurationManager.getInstance(vEditor.getFile().getProject()).getContextValue(key, vEditor.getFile(), false); ContextValueDialog dialog = new ContextValueDialog(new Shell(), value, vEditor.getFile()); if (IDialogConstants.OK_ID == dialog.open()) { reloadContextValues(); @@ -278,16 +303,18 @@ int index = contextValuesTable.getSelectionIndex(); if (index >= 0) { try { - boolean confirm = MessageDialog.openConfirm(new Shell(), "Confirmation", "Are you sure you want to delete this context value?"); + boolean confirm = MessageDialog.openConfirm(new Shell(), + FreemarkerMessages.getString("Context.confirm.title"), + FreemarkerMessages.getString("Context.confirm.message")); if (confirm) { String key = contextValuesTable.getSelection()[0].getText(0); - ContextValue value = ConfigurationManager.getInstance(vEditor.getFile().getProject()).getContextValue(key, vEditor.getFile(), false); - ConfigurationManager.getInstance(vEditor.getFile().getProject()).removeContextValue(value.name, vEditor.getFile()); + IContextValue value = ConfigurationManager.getInstance(vEditor.getFile().getProject()).getContextValue(key, vEditor.getFile(), false); + ConfigurationManager.getInstance(vEditor.getFile().getProject()).removeContextValue(value.getName(), vEditor.getFile()); reloadContextValues(); } } catch (Exception e1) { - Plugin.error(e1); + FreemarkerPlugin.error(e1); } } } Index: src/org/jboss/ide/eclipse/freemarker/editor/MacroHyperlink.java =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/src/org/jboss/ide/eclipse/freemarker/editor/MacroHyperlink.java,v retrieving revision 1.2 diff -u -r1.2 MacroHyperlink.java --- src/org/jboss/ide/eclipse/freemarker/editor/MacroHyperlink.java 2 Oct 2006 23:08:06 -0000 1.2 +++ src/org/jboss/ide/eclipse/freemarker/editor/MacroHyperlink.java 22 Dec 2006 18:03:52 -0000 @@ -28,7 +28,7 @@ import org.eclipse.ui.IEditorPart; import org.eclipse.ui.PartInitException; import org.eclipse.ui.part.FileEditorInput; -import org.jboss.ide.eclipse.freemarker.Plugin; +import org.jboss.ide.eclipse.freemarker.FreemarkerPlugin; import org.jboss.ide.eclipse.freemarker.model.MacroInstance; public class MacroHyperlink implements IHyperlink { @@ -47,14 +47,14 @@ public void open() { try { - IEditorPart editorPart = Plugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage() + IEditorPart editorPart = FreemarkerPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage() .openEditor(new FileEditorInput(file), FreemarkerMultiPageEditor.ID); if (offset >= 0 && length > 0 && editorPart instanceof FreemarkerMultiPageEditor) { ((FreemarkerMultiPageEditor) editorPart).selectAndReveal(offset, length); } } catch (PartInitException e) { - Plugin.error(e); + FreemarkerPlugin.error(e); } } Index: src/org/jboss/ide/eclipse/freemarker/editor/MacroHyperlinkDetector.java =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/src/org/jboss/ide/eclipse/freemarker/editor/MacroHyperlinkDetector.java,v retrieving revision 1.3 diff -u -r1.3 MacroHyperlinkDetector.java --- src/org/jboss/ide/eclipse/freemarker/editor/MacroHyperlinkDetector.java 2 Oct 2006 23:08:06 -0000 1.3 +++ src/org/jboss/ide/eclipse/freemarker/editor/MacroHyperlinkDetector.java 22 Dec 2006 18:03:52 -0000 @@ -31,14 +31,15 @@ import org.eclipse.jface.text.hyperlink.IHyperlinkDetector; import org.jboss.ide.eclipse.freemarker.configuration.ConfigurationManager; import org.jboss.ide.eclipse.freemarker.configuration.MacroLibrary; +import org.jboss.ide.eclipse.freemarker.editor.editorpart.SourceEditor; import org.jboss.ide.eclipse.freemarker.model.Item; import org.jboss.ide.eclipse.freemarker.model.MacroDirective; import org.jboss.ide.eclipse.freemarker.model.MacroInstance; public class MacroHyperlinkDetector implements IHyperlinkDetector { - private Editor editor; - public MacroHyperlinkDetector(ITextViewer textViewer, Editor editor) { + private SourceEditor editor; + public MacroHyperlinkDetector(ITextViewer textViewer, SourceEditor editor) { this.editor = editor; } Index: src/org/jboss/ide/eclipse/freemarker/editor/actions/AddMacroLibrary.java =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/src/org/jboss/ide/eclipse/freemarker/editor/actions/AddMacroLibrary.java,v retrieving revision 1.2 diff -u -r1.2 AddMacroLibrary.java --- src/org/jboss/ide/eclipse/freemarker/editor/actions/AddMacroLibrary.java 2 Oct 2006 23:08:07 -0000 1.2 +++ src/org/jboss/ide/eclipse/freemarker/editor/actions/AddMacroLibrary.java 22 Dec 2006 18:03:52 -0000 @@ -44,7 +44,7 @@ import org.eclipse.swt.widgets.Display; import org.eclipse.ui.IObjectActionDelegate; import org.eclipse.ui.IWorkbenchPart; -import org.jboss.ide.eclipse.freemarker.Plugin; +import org.jboss.ide.eclipse.freemarker.FreemarkerPlugin; import org.jboss.ide.eclipse.freemarker.configuration.ConfigurationManager; /** @@ -109,7 +109,7 @@ } } catch (Exception e) { - Plugin.error(e); + FreemarkerPlugin.error(e); } } } Index: src/org/jboss/ide/eclipse/freemarker/model/AbstractItem.java =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/src/org/jboss/ide/eclipse/freemarker/model/AbstractItem.java,v retrieving revision 1.2 diff -u -r1.2 AbstractItem.java --- src/org/jboss/ide/eclipse/freemarker/model/AbstractItem.java 2 Oct 2006 23:08:05 -0000 1.2 +++ src/org/jboss/ide/eclipse/freemarker/model/AbstractItem.java 22 Dec 2006 18:03:52 -0000 @@ -32,7 +32,7 @@ import org.eclipse.jface.text.ITypedRegion; import org.eclipse.jface.text.contentassist.ICompletionProposal; import org.eclipse.jface.text.source.ISourceViewer; -import org.jboss.ide.eclipse.freemarker.Plugin; +import org.jboss.ide.eclipse.freemarker.FreemarkerPlugin; public abstract class AbstractItem implements Item { @@ -51,7 +51,7 @@ init(region, viewer, resource); } catch (Exception e) { - Plugin.log(e); + FreemarkerPlugin.log(e); } } @@ -153,6 +153,7 @@ boolean doAppend = true; boolean encounteredSpace = false; boolean nextCharSpace = false; + boolean encounteredEscapeVariableName = false; // true for doc["fffff for (int i=0; i 2 && s.charAt(i-1) == '[') { + char lastChar = s.charAt(i-2); + if (lastChar != ' ' && lastChar != '=') { + encounteredEscapeVariableName = true; + doAppendSpecialCharacter = true; + current.append('.'); + offsetCount++; + } + } + else { + if (s.length() > i + 2&& s.charAt(i+1) == ']') { + char nextChar = s.charAt(i+2); + if (encounteredEscapeVariableName == true) + doAppendSpecialCharacter = true; + encounteredEscapeVariableName = false; + } + } + } + if (!doAppendSpecialCharacter) { + current.append(c); + offsetCount++; + } } escape = doEscape; doEscape = false; Index: src/org/jboss/ide/eclipse/freemarker/model/AssignmentDirective.java =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/src/org/jboss/ide/eclipse/freemarker/model/AssignmentDirective.java,v retrieving revision 1.2 diff -u -r1.2 AssignmentDirective.java --- src/org/jboss/ide/eclipse/freemarker/model/AssignmentDirective.java 2 Oct 2006 23:08:05 -0000 1.2 +++ src/org/jboss/ide/eclipse/freemarker/model/AssignmentDirective.java 22 Dec 2006 18:03:52 -0000 @@ -84,18 +84,18 @@ String value = null; if (values.length >= 2) key = values[1]; if (values.length >= 4) value = values[3]; - Class valueClass = null; + Object valueObject = null; if (null != value && value.length() > 0) { - if (value.charAt(0) == '\"') valueClass = String.class; - else if (Character.isDigit(value.charAt(0))) valueClass = Number.class; + if (value.charAt(0) == '\"') valueObject = String.class; + else if (Character.isDigit(value.charAt(0))) valueObject = Number.class; else { CompletionInterpolation completionInterpolation = new CompletionInterpolation("${" + value, 2, getItemSet(), getResource()); - valueClass = completionInterpolation.getReturnClass(context); + valueObject = completionInterpolation.getReturnObject(context); } } if (null != key) { - context.put(key, valueClass); + context.put(key, valueObject); } } super.addToContext(context); Index: src/org/jboss/ide/eclipse/freemarker/model/CompletionDirective.java =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/src/org/jboss/ide/eclipse/freemarker/model/CompletionDirective.java,v retrieving revision 1.2 diff -u -r1.2 CompletionDirective.java --- src/org/jboss/ide/eclipse/freemarker/model/CompletionDirective.java 2 Oct 2006 23:08:05 -0000 1.2 +++ src/org/jboss/ide/eclipse/freemarker/model/CompletionDirective.java 22 Dec 2006 18:03:52 -0000 @@ -30,7 +30,7 @@ import org.eclipse.jface.text.TypedRegion; import org.eclipse.jface.text.contentassist.ICompletionProposal; import org.eclipse.jface.text.source.ISourceViewer; -import org.jboss.ide.eclipse.freemarker.Plugin; +import org.jboss.ide.eclipse.freemarker.FreemarkerPlugin; public class CompletionDirective extends AbstractDirective { @@ -60,7 +60,7 @@ } } catch (Exception e) { - Plugin.log(e); + FreemarkerPlugin.log(e); directive = this; } } @@ -68,7 +68,7 @@ load(region, viewer, resource); } catch (Exception e) { - Plugin.log(e); + FreemarkerPlugin.log(e); } } Index: src/org/jboss/ide/eclipse/freemarker/model/Interpolation.java =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/src/org/jboss/ide/eclipse/freemarker/model/Interpolation.java,v retrieving revision 1.2 diff -u -r1.2 Interpolation.java --- src/org/jboss/ide/eclipse/freemarker/model/Interpolation.java 2 Oct 2006 23:08:05 -0000 1.2 +++ src/org/jboss/ide/eclipse/freemarker/model/Interpolation.java 22 Dec 2006 18:03:52 -0000 @@ -34,6 +34,7 @@ import org.eclipse.jface.text.source.ISourceViewer; import org.jboss.ide.eclipse.freemarker.model.interpolation.BuiltInFragment; import org.jboss.ide.eclipse.freemarker.model.interpolation.Fragment; +import org.jboss.ide.eclipse.freemarker.model.interpolation.IndexedParametersFragment; import org.jboss.ide.eclipse.freemarker.model.interpolation.NameFragment; import org.jboss.ide.eclipse.freemarker.model.interpolation.NullFragment; import org.jboss.ide.eclipse.freemarker.model.interpolation.ParametersFragment; @@ -93,15 +94,15 @@ else break; } if (null != fragment) { - // find the parent class - Class parentClass = null; - for (Iterator i = fragments.iterator(); i.hasNext(); ) { - Fragment fragmentSub = (Fragment) i.next(); - if (fragmentSub.equals(fragment)) break; - else parentClass = fragmentSub.getReturnClass(parentClass, fragments, context, getResource(), getResource().getProject()); - } - return fragment.getCompletionProposals( - subOffset - fragment.getOffset(), offset, parentClass, fragments, getViewer(), context, getResource(), getResource().getProject()); + // find the parent object + Object parentObject = null; + for (Iterator i = fragments.iterator(); i.hasNext(); ) { + Fragment fragmentSub = (Fragment) i.next(); + if (fragmentSub.equals(fragment)) break; + else parentObject = fragmentSub.getReturnObject(parentObject, fragments, context, getResource(), getResource().getProject()); + } + return fragment.getCompletionProposals( + subOffset - fragment.getOffset(), offset, parentObject, fragments, getViewer(), context, getResource(), getResource().getProject()); } else if (getContents().length() == 0 && subOffset == 0) { return new NullFragment().getCompletionProposals( @@ -122,13 +123,14 @@ boolean inBuiltIn = false; boolean inNameFragment = false; boolean inParameters = false; + boolean inIndexedParameters = false; boolean escape = false; boolean doEscape = false; int offset = getOffset(); for (int i=0; i 0 && !inBuiltIn) + fragments.add(new NameFragment(offsetStart, sb.toString())); + inIndexedParameters = true; + offsetStart = i; + sb.delete(0, sb.length()); + } + } else if (c == '"') { if (sb.length() > 0) fragments.add(new NameFragment(offsetStart, sb.toString())); inString = true; @@ -257,26 +301,26 @@ return false; } - public Class getReturnClass (Map context) { - initFragments(); - Class returnClass = null; - for (Iterator i=fragments.iterator(); i.hasNext(); ) { - Fragment fragment = (Fragment) i.next(); - returnClass = fragment.getReturnClass(returnClass, fragments, context, getResource(), getResource().getProject()); - } - return returnClass; - } - - public Class getSingularReturnClass (Map context) { + public Object getReturnObject (Map context) { + initFragments(); + Object returnObject = null; + for (Iterator i=fragments.iterator(); i.hasNext(); ) { + Fragment fragment = (Fragment) i.next(); + returnObject = fragment.getReturnObject(returnObject, fragments, context, getResource(), getResource().getProject()); + } + return returnObject; + } + + public Object getSingularReturnObject (Map context) { initFragments(); - Class returnClass = null; + Object returnObject = null; for (Iterator i=fragments.iterator(); i.hasNext(); ) { Fragment fragment = (Fragment) i.next(); - if (i.hasNext()) - returnClass = fragment.getReturnClass(returnClass, fragments, context, getResource(), getResource().getProject()); - else - returnClass = fragment.getSingularReturnClass(returnClass, fragments, context, getResource(), getResource().getProject()); + if (i.hasNext()) + returnObject = fragment.getReturnObject(returnObject, fragments, context, getResource(), getResource().getProject()); + else + returnObject = fragment.getSingularReturnObject(returnObject, fragments, context, getResource(), getResource().getProject()); } - return returnClass; + return returnObject; } } \ No newline at end of file Index: src/org/jboss/ide/eclipse/freemarker/model/ItemSet.java =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/src/org/jboss/ide/eclipse/freemarker/model/ItemSet.java,v retrieving revision 1.2 diff -u -r1.2 ItemSet.java --- src/org/jboss/ide/eclipse/freemarker/model/ItemSet.java 2 Oct 2006 23:08:05 -0000 1.2 +++ src/org/jboss/ide/eclipse/freemarker/model/ItemSet.java 22 Dec 2006 18:03:52 -0000 @@ -35,7 +35,7 @@ import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.ITypedRegion; import org.eclipse.jface.text.source.ISourceViewer; -import org.jboss.ide.eclipse.freemarker.Plugin; +import org.jboss.ide.eclipse.freemarker.FreemarkerPlugin; public class ItemSet { @@ -164,7 +164,7 @@ } } catch (Exception e) { - Plugin.log(e); + FreemarkerPlugin.log(e); } Collections.sort(macroDefinitions); } Index: src/org/jboss/ide/eclipse/freemarker/model/ListDirective.java =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/src/org/jboss/ide/eclipse/freemarker/model/ListDirective.java,v retrieving revision 1.2 diff -u -r1.2 ListDirective.java --- src/org/jboss/ide/eclipse/freemarker/model/ListDirective.java 2 Oct 2006 23:08:05 -0000 1.2 +++ src/org/jboss/ide/eclipse/freemarker/model/ListDirective.java 22 Dec 2006 18:03:52 -0000 @@ -87,7 +87,7 @@ if (contents.length == 4) { String key = contents[3]; CompletionInterpolation completionInterpolation = new CompletionInterpolation("${" + contents[1], 0, getItemSet(), getResource()); - context.put(key, completionInterpolation.getSingularReturnClass(context)); + context.put(key, completionInterpolation.getSingularReturnObject(context)); } } Index: src/org/jboss/ide/eclipse/freemarker/model/interpolation/AbstractFragment.java =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/src/org/jboss/ide/eclipse/freemarker/model/interpolation/AbstractFragment.java,v retrieving revision 1.2 diff -u -r1.2 AbstractFragment.java --- src/org/jboss/ide/eclipse/freemarker/model/interpolation/AbstractFragment.java 2 Oct 2006 23:08:05 -0000 1.2 +++ src/org/jboss/ide/eclipse/freemarker/model/interpolation/AbstractFragment.java 22 Dec 2006 18:03:52 -0000 @@ -30,6 +30,7 @@ import org.eclipse.core.resources.IResource; import org.eclipse.jface.text.contentassist.CompletionProposal; import org.eclipse.jface.text.contentassist.ICompletionProposal; +import org.jboss.ide.eclipse.freemarker.helper.ClassHelper; /** * @author Joe Hudson @@ -70,15 +71,7 @@ } protected boolean instanceOf (Class test, Class base) { - if (null == test || null == base) return false; - while (null != test) { - for (int i=0; iJoe Hudson @@ -53,147 +55,148 @@ super(offset, content); } - public ICompletionProposal[] getCompletionProposals (int subOffset, int offset, Class parentClass, + public ICompletionProposal[] getCompletionProposals (int subOffset, int offset, Object parentObject, List fragments, ISourceViewer sourceViewer, Map context, IResource file, IProject project) { - if (isStartFragment()) { - // pull from context - String prefix = getContent().substring(0, subOffset); - List proposals = new ArrayList(); - for (Iterator i=context.keySet().iterator(); i.hasNext(); ) { - String key = (String) i.next(); - if (key.startsWith(prefix)) proposals.add(getCompletionProposal( - offset, subOffset, key, getContent())); - } - return completionProposals(proposals); - } - else { - if (null == parentClass) return null; - return getMethodCompletionProposals (subOffset, offset, parentClass, file); - } - } - - private Class returnClass; - public Class getReturnClass (Class parentClass, List fragments, Map context, IResource resource, IProject project){ - if (null == returnClass) { - String content = getContent(); - if (isStartFragment()) { - returnClass = (Class) context.get(content); - } - else { - if (null == parentClass) { - returnClass = Object.class; - } - else { - content = Character.toUpperCase(content.charAt(1)) + content.substring(2, getContent().length()); - String getcontent = "get" + content; - for (int i=0; i 0) { - singulaReturnClass = (Class) pType.getActualTypeArguments()[0]; - break; - } - } - singulaReturnClass = Object.class; - break; - } - } - } - } - } - return singulaReturnClass; - } - - public boolean isStartFragment () { - return !getContent().startsWith("."); + // Test type of context + Object objectContext = context.get(content); + if (ContextValueHelper.isStringContextValue(objectContext)) { + // It's String context + // Completion must be stopped. + return null; + } + + if (objectContext != null && + ContextValueHelper.isStartFragment(content)) { + return objectContext; + } + + if ((ContextValueHelper.isXMLContextValue(objectContext) || + ContextValueHelper.isXMLContextValue(parentObject))) { + // It's XML context + String eltcontent = content; + if (content.startsWith(".")) + eltcontent = content.substring(1, content.length()); + return ContextValueHelper.getChildNode(parentObject, eltcontent); + } + + /* if ((ContextValueHelper.isDTDContextValue(objectContext) || + ContextValueHelper.isDTDContextValue(parentObject))) { + // It's DTD context + String eltcontent = content; + if (content.startsWith(".")) + eltcontent = content.substring(1, content.length()); + return ContextValueHelper.getChildDTDElement(parentObject, eltcontent); + }*/ + if ((ContextValueHelper.isClassContextValue(parentObject))) { + Class parentClass = (Class)parentObject; + content = Character.toUpperCase(content.charAt(1)) + content.substring(2, getContent().length()); + String getcontent = "get" + content; + for (int i=0; i 2) { + content = Character.toUpperCase(content.charAt(1)) + content.substring(2, getContent().length()); + String getcontent = "get" + content; + for (int i=0; i 0) { + singulaReturnObject = (Class) pType.getActualTypeArguments()[0]; + break; + } + } + return Object.class; + } + } + } + } + } + } + } + } + return singulaReturnObject; } public static final String[] invalidMethods = { "clone", "equals", "finalize", "getClass", "hashCode", "notify", "notifyAll", "toString", "wait"}; - public ICompletionProposal[] getMethodCompletionProposals (int subOffset, int offset, Class parentClass, IResource file) { - if (instanceOf(parentClass, String.class) - || instanceOf(parentClass, Number.class) - || instanceOf(parentClass, Date.class) - || instanceOf(parentClass, Collection.class) - || instanceOf(parentClass, List.class) - || instanceOf(parentClass, Map.class)) - return null; - String prefix = getContent().substring(1, subOffset); - List proposals = new ArrayList(); - String pUpper = prefix.toUpperCase(); - try { - BeanInfo bi = Introspector.getBeanInfo(parentClass); - PropertyDescriptor[] pds = bi.getPropertyDescriptors(); - for (int i=0; i 0 && mName.startsWith("get") && mName.toUpperCase().startsWith(pUpper)) { - StringBuffer display = new StringBuffer(); - display.append(mName); - display.append("("); - for (int j=0; j 0) display.append(", "); - display.append(m.getParameterTypes()[j].getName()); - } - display.append(")"); - String actual = mName + "()"; - int tLength = actual.length(); - if (m.getParameterTypes().length > 0) tLength--; - proposals.add(new CompletionProposal(actual, - offset - subOffset + 1, getContent().length()-1, tLength, - null, display.toString() + " - " + m.getReturnType().getName(), null, null)); - } - } - return completionProposals(proposals); - } - catch (IntrospectionException e) { - return null; - } - } + } \ No newline at end of file Index: src/org/jboss/ide/eclipse/freemarker/model/interpolation/ParametersFragment.java =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/src/org/jboss/ide/eclipse/freemarker/model/interpolation/ParametersFragment.java,v retrieving revision 1.2 diff -u -r1.2 ParametersFragment.java --- src/org/jboss/ide/eclipse/freemarker/model/interpolation/ParametersFragment.java 2 Oct 2006 23:08:05 -0000 1.2 +++ src/org/jboss/ide/eclipse/freemarker/model/interpolation/ParametersFragment.java 22 Dec 2006 18:03:52 -0000 @@ -34,16 +34,16 @@ */ public class ParametersFragment extends AbstractFragment { - public ParametersFragment(int offset, String content) { - super(offset, content); - } + public ParametersFragment(int offset, String content) { + super(offset, content); + } - public Class getReturnClass (Class parentClass, List fragments, Map context, IResource resource, IProject project) { - return parentClass; - } + public Object getReturnObject (Object parentObject, List fragments, Map context, IResource resource, IProject project) { + return parentObject; + } - public ICompletionProposal[] getCompletionProposals (int subOffset, int offset, Class parentClass, - List fragments, ISourceViewer sourceViewer, Map context, IResource file, IProject project) { - return null; - } + public ICompletionProposal[] getCompletionProposals (int subOffset, int offset, Object parentClass, + List fragments, ISourceViewer sourceViewer, Map context, IResource file, IProject project) { + return null; + } } Index: src/org/jboss/ide/eclipse/freemarker/model/interpolation/StringFragment.java =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/src/org/jboss/ide/eclipse/freemarker/model/interpolation/StringFragment.java,v retrieving revision 1.2 diff -u -r1.2 StringFragment.java --- src/org/jboss/ide/eclipse/freemarker/model/interpolation/StringFragment.java 2 Oct 2006 23:08:05 -0000 1.2 +++ src/org/jboss/ide/eclipse/freemarker/model/interpolation/StringFragment.java 22 Dec 2006 18:03:52 -0000 @@ -38,11 +38,11 @@ super(offset, content); } - public Class getReturnClass (Class parentClass, List fragments, Map context, IResource resource, IProject project) { - return parentClass; + public Object getReturnObject (Object parentObject, List fragments, Map context, IResource resource, IProject project) { + return parentObject; } - public ICompletionProposal[] getCompletionProposals (int subOffset, int offset, Class parentClass, + public ICompletionProposal[] getCompletionProposals (int subOffset, int offset, Object parentObject, List fragments, ISourceViewer sourceViewer, Map context, IResource file, IProject project) { return null; } Index: src/org/jboss/ide/eclipse/freemarker/outline/OutlineContentProvider.java =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/src/org/jboss/ide/eclipse/freemarker/outline/OutlineContentProvider.java,v retrieving revision 1.2 diff -u -r1.2 OutlineContentProvider.java --- src/org/jboss/ide/eclipse/freemarker/outline/OutlineContentProvider.java 2 Oct 2006 23:08:07 -0000 1.2 +++ src/org/jboss/ide/eclipse/freemarker/outline/OutlineContentProvider.java 22 Dec 2006 18:03:53 -0000 @@ -27,7 +27,8 @@ import org.eclipse.jface.viewers.ITreeContentProvider; import org.eclipse.jface.viewers.Viewer; -import org.jboss.ide.eclipse.freemarker.editor.Editor; +import org.jboss.ide.eclipse.freemarker.editor.editorpart.SourceEditor; +import org.jboss.ide.eclipse.freemarker.editor.editorpart.SourceEditor; import org.jboss.ide.eclipse.freemarker.model.Item; import org.jboss.ide.eclipse.freemarker.model.ItemSet; import org.jboss.ide.eclipse.freemarker.model.MacroDirective; @@ -36,9 +37,9 @@ * @author Joe Hudson */ public class OutlineContentProvider implements ITreeContentProvider { - private Editor fEditor; + private SourceEditor fEditor; - public OutlineContentProvider(Editor anEditor) { + public OutlineContentProvider(SourceEditor anEditor) { fEditor = anEditor; } Index: src/org/jboss/ide/eclipse/freemarker/outline/OutlinePage.java =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/src/org/jboss/ide/eclipse/freemarker/outline/OutlinePage.java,v retrieving revision 1.2 diff -u -r1.2 OutlinePage.java --- src/org/jboss/ide/eclipse/freemarker/outline/OutlinePage.java 2 Oct 2006 23:08:07 -0000 1.2 +++ src/org/jboss/ide/eclipse/freemarker/outline/OutlinePage.java 22 Dec 2006 18:03:53 -0000 @@ -46,10 +46,11 @@ import org.eclipse.swt.widgets.TreeItem; import org.eclipse.ui.dialogs.SelectionDialog; import org.eclipse.ui.views.contentoutline.ContentOutlinePage; -import org.jboss.ide.eclipse.freemarker.Plugin; +import org.jboss.ide.eclipse.freemarker.FreemarkerMessages; +import org.jboss.ide.eclipse.freemarker.FreemarkerPlugin; import org.jboss.ide.eclipse.freemarker.configuration.ConfigurationManager; -import org.jboss.ide.eclipse.freemarker.configuration.ContextValue; -import org.jboss.ide.eclipse.freemarker.editor.Editor; +import org.jboss.ide.eclipse.freemarker.configuration.context.IContextValue; +import org.jboss.ide.eclipse.freemarker.editor.editorpart.SourceEditor; import org.jboss.ide.eclipse.freemarker.model.Interpolation; import org.jboss.ide.eclipse.freemarker.model.Item; import org.jboss.ide.eclipse.freemarker.model.ItemSet; @@ -59,11 +60,11 @@ * @author Joe Hudson */ public class OutlinePage extends ContentOutlinePage implements IDoubleClickListener { - private Editor fEditor; + private SourceEditor fEditor; private Object fInput; private OutlineLabelProvider fLabelProvider; - public OutlinePage(Editor anEditor) { + public OutlinePage(SourceEditor anEditor) { fEditor = anEditor; } @@ -161,7 +162,7 @@ public SetContextEntryAction (OutlinePage outlinePage) { this.outlinePage = outlinePage; - this.setText("Set context class entry"); + this.setText(FreemarkerMessages.getString("OutlinePage.text")); this.setEnabled(true); } @@ -183,17 +184,19 @@ ConfigurationManager configuration = ConfigurationManager.getInstance(interpolation.getResource().getProject()); IType type = (IType) objects[0]; try { - configuration.addContextValue( - new ContextValue(interpolation.getFirstToken(), - configuration.getClass(type.getFullyQualifiedName()), null), interpolation.getResource()); + // TODO : manage other context type (like XML, ...) + int contextType = IContextValue.CLASS; + IContextValue contextValue = configuration.getInstanceOfContextValue(contextType, interpolation.getFirstToken(), + type.getFullyQualifiedName(), null); + configuration.addContextValue(contextValue, interpolation.getResource()); } - catch (ClassNotFoundException e) { - Plugin.log(e); + catch (Exception e) { + FreemarkerPlugin.log(e); } } } catch (JavaModelException jme) { - Plugin.error(jme); + FreemarkerPlugin.error(jme); } } } Index: src/org/jboss/ide/eclipse/freemarker/preferences/ContextProperties.java =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/src/org/jboss/ide/eclipse/freemarker/preferences/ContextProperties.java,v retrieving revision 1.2 diff -u -r1.2 ContextProperties.java --- src/org/jboss/ide/eclipse/freemarker/preferences/ContextProperties.java 2 Oct 2006 23:08:06 -0000 1.2 +++ src/org/jboss/ide/eclipse/freemarker/preferences/ContextProperties.java 22 Dec 2006 18:03:53 -0000 @@ -48,16 +48,17 @@ import org.eclipse.swt.widgets.TableColumn; import org.eclipse.swt.widgets.TableItem; import org.eclipse.ui.dialogs.PropertyPage; -import org.jboss.ide.eclipse.freemarker.Plugin; +import org.jboss.ide.eclipse.freemarker.FreemarkerMessages; +import org.jboss.ide.eclipse.freemarker.FreemarkerPlugin; import org.jboss.ide.eclipse.freemarker.configuration.ConfigurationManager; -import org.jboss.ide.eclipse.freemarker.configuration.ContextValue; +import org.jboss.ide.eclipse.freemarker.configuration.context.IContextValue; import org.jboss.ide.eclipse.freemarker.dialogs.ContextValueDialog; public class ContextProperties extends PropertyPage { public ContextProperties() { super(); - setDescription("Create pre-determined Velocity context variables for all files under this resource"); + setDescription(FreemarkerMessages.getString("ContextProperties.description")); } protected Control createContents(Composite parent) { @@ -170,17 +171,18 @@ public void reloadContextValues() { try { contextValuesTable.removeAll(); - ContextValue[] values = ConfigurationManager.getInstance(getResource().getProject()) + IContextValue[] values = ConfigurationManager.getInstance(getResource().getProject()) .getContextValues(getResource(), false); for (int i = 0; i < values.length; i++) { TableItem item = new TableItem(contextValuesTable, SWT.NULL); - String[] arr = { values[i].name, values[i].objClass.getName() }; + IContextValue contextValue = values[i]; + String[] arr = { contextValue.getName(), contextValue.getFirstObjectContext().getName() }; item.setText(arr); } editContextValueButton.setEnabled(false); deleteContextValueButton.setEnabled(false); } catch (Exception e) { - Plugin.log(e); + FreemarkerPlugin.log(e); } contextValuesTable.redraw(); } @@ -235,7 +237,7 @@ int index = contextValuesTable.getSelectionIndex(); if (index >= 0) { String key = contextValuesTable.getSelection()[0].getText(0); - ContextValue value = ConfigurationManager.getInstance(getResource().getProject()).getContextValue(key, + IContextValue value = ConfigurationManager.getInstance(getResource().getProject()).getContextValue(key, getResource(), false); ContextValueDialog dialog = new ContextValueDialog(new Shell(), value, getResource()); @@ -269,19 +271,19 @@ if (index >= 0) { try { boolean confirm = MessageDialog - .openConfirm(new Shell(), "Confirmation", - "Are you sure you want to delete this context value?"); + .openConfirm(new Shell(), FreemarkerMessages.getString("Context.confirm.title"), + FreemarkerMessages.getString("Context.confirm.message")); if (confirm) { String key = contextValuesTable.getSelection()[0] .getText(0); - ContextValue value = ConfigurationManager.getInstance(getResource().getProject()) + IContextValue contextValue = ConfigurationManager.getInstance(getResource().getProject()) .getContextValue(key, getResource(), false); - ConfigurationManager.getInstance(getResource().getProject()).removeContextValue(value.name, + ConfigurationManager.getInstance(getResource().getProject()).removeContextValue(contextValue.getName(), getResource()); reloadContextValues(); } } catch (Exception e1) { - Plugin.log(e1); + FreemarkerPlugin.log(e1); } } } Index: src/org/jboss/ide/eclipse/freemarker/preferences/PreferencePage.java =================================================================== RCS file: /cvsroot/jboss/jbosside/freemarker/plugins/org.jboss.ide.eclipse.freemarker/src/org/jboss/ide/eclipse/freemarker/preferences/PreferencePage.java,v retrieving revision 1.2 diff -u -r1.2 PreferencePage.java --- src/org/jboss/ide/eclipse/freemarker/preferences/PreferencePage.java 2 Oct 2006 23:08:06 -0000 1.2 +++ src/org/jboss/ide/eclipse/freemarker/preferences/PreferencePage.java 22 Dec 2006 18:03:53 -0000 @@ -28,7 +28,7 @@ import org.eclipse.ui.IWorkbench; import org.eclipse.ui.IWorkbenchPreferencePage; import org.jboss.ide.eclipse.freemarker.Constants; -import org.jboss.ide.eclipse.freemarker.Plugin; +import org.jboss.ide.eclipse.freemarker.FreemarkerPlugin; /** * @author Joe Hudson @@ -39,7 +39,7 @@ public PreferencePage() { super(GRID); - setPreferenceStore(Plugin.getDefault().getPreferenceStore()); + setPreferenceStore(FreemarkerPlugin.getDefault().getPreferenceStore()); setDescription("FreeMarker Settings"); } Index: src/org/jboss/ide/eclipse/freemarker/FreemarkerMessages.java =================================================================== RCS file: src/org/jboss/ide/eclipse/freemarker/FreemarkerMessages.java diff -N src/org/jboss/ide/eclipse/freemarker/FreemarkerMessages.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/jboss/ide/eclipse/freemarker/FreemarkerMessages.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,53 @@ +package org.jboss.ide.eclipse.freemarker; + +import java.text.MessageFormat; +import java.util.MissingResourceException; +import java.util.ResourceBundle; + +/** + * + * Freemarker Messages (see resources.properties). + * + * @version 1.0.4 + * @author Angelo ZERR + * + */ +public class FreemarkerMessages { + + private static final String BUNDLE_NAME = "org.jboss.ide.eclipse.freemarker.resources"; + + private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle + .getBundle(BUNDLE_NAME); + + private FreemarkerMessages() { + } + + public static String getString(String key) { + try { + return RESOURCE_BUNDLE.getString(key); + } catch (MissingResourceException e) { + return '!' + key + '!'; + } + } + + public static String getString(String key, String arg0) { + String[] args = {arg0}; + return getString(key, args); + } + + public static String getString(String key, String arg0, String arg1) { + String[] args = {arg0, arg1}; + return getString(key, args); + } + + public static String getString(String key, String[] args) { + if (args == null) + return getString(key); + try { + return MessageFormat.format(getString(key), args); + } + catch(Exception e) { + return '!' + key + '!'; + } + } +} Index: src/org/jboss/ide/eclipse/freemarker/FreemarkerPlugin.java =================================================================== RCS file: src/org/jboss/ide/eclipse/freemarker/FreemarkerPlugin.java diff -N src/org/jboss/ide/eclipse/freemarker/FreemarkerPlugin.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/jboss/ide/eclipse/freemarker/FreemarkerPlugin.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,176 @@ +/* + * JBoss, a division of Red Hat + * Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.ide.eclipse.freemarker; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.MissingResourceException; +import java.util.ResourceBundle; + +import org.eclipse.core.resources.IWorkspace; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.IPluginDescriptor; +import org.eclipse.core.runtime.Preferences; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jface.text.templates.ContextTypeRegistry; +import org.eclipse.jface.text.templates.persistence.TemplateStore; +import org.eclipse.swt.widgets.Display; +import org.eclipse.ui.editors.text.templates.ContributionContextTypeRegistry; +import org.eclipse.ui.editors.text.templates.ContributionTemplateStore; +import org.eclipse.ui.plugin.AbstractUIPlugin; +import org.jboss.ide.eclipse.freemarker.template.FreemarkerTemplateContextType; + +/** + * @author Joe Hudson + */ +public class FreemarkerPlugin extends AbstractUIPlugin { + + public static final String ID = "org.jboss.ide.eclipse.freemarker"; + + //The shared instance. + private static FreemarkerPlugin plugin; + //Resource bundle. + private ResourceBundle resourceBundle; + + /** + * Template management + */ + private TemplateStore fStore; + private ContributionContextTypeRegistry fRegistry; + + public FreemarkerPlugin() { + super(); + plugin = this; + try { + resourceBundle = ResourceBundle.getBundle("org.jboss.ide.eclipse.freemarker.resources"); + } catch (MissingResourceException x) { + resourceBundle = null; + } + } + + /** + * The constructor. + */ + public FreemarkerPlugin(IPluginDescriptor descriptor) { + super(descriptor); + plugin = this; + } + + /** + * Returns the shared instance. + */ + public static FreemarkerPlugin getDefault() { + return plugin; + } + + /** + * Returns the workspace instance. + */ + public static IWorkspace getWorkspace() { + return ResourcesPlugin.getWorkspace(); + } + + public static FreemarkerPlugin getInstance() { + return plugin; + } + + /** + * Returns the plugin's resource bundle, + */ + public ResourceBundle getResourceBundle() { + return resourceBundle; + } + + /** + * Initializes the plugin preferences with default preference values for + * this plug-in. + */ + protected void initializeDefaultPluginPreferences() { + Preferences prefs = getPluginPreferences(); + prefs.setDefault(Constants.HIGHLIGHT_RELATED_ITEMS, true); + prefs.setDefault(Constants.COLOR_COMMENT, "170,0,0"); + prefs.setDefault(Constants.COLOR_TEXT, "0,0,0"); + prefs.setDefault(Constants.COLOR_INTERPOLATION, "255,0,128"); + prefs.setDefault(Constants.COLOR_DIRECTIVE, "0,0,255"); + prefs.setDefault(Constants.COLOR_STRING, "0,128,128"); + prefs.setDefault(Constants.COLOR_XML_COMMENT, "128,128,128"); + prefs.setDefault(Constants.COLOR_XML_TAG, "0,0,128"); + prefs.setDefault(Constants.COLOR_RELATED_ITEM, "255,255,128"); + } + + public static void error (Throwable t) { + StringWriter sw = new StringWriter(); + t.printStackTrace(new PrintWriter(sw)); + MessageDialog.openError(Display.getCurrent().getActiveShell(), t.getMessage(), sw.toString()); + log(t); + } + + public static void log (Throwable t) { + log("Error: " + t.getMessage() + "\n"); + StringWriter sw = new StringWriter(); + t.printStackTrace(new PrintWriter(sw)); + log("Trace: " + sw.toString()); + + } + + public static void log (String s) { + // TODO figure out a good place to log messages + } + + public static String getId() { + return getDefault().getBundle().getSymbolicName(); + } + + public ContextTypeRegistry getContextTypeRegistry() + { + if(fRegistry == null) + { + fRegistry = new ContributionContextTypeRegistry(); + fRegistry.addContextType(FreemarkerTemplateContextType.FREEMARKER_CONTEXT_TYPE); + } + return fRegistry; + } + + /** + * Return template store for Freemarker plugin. + * @return + */ + public TemplateStore getTemplateStore() + { + if(fStore == null) + { + fStore = new ContributionTemplateStore(getContextTypeRegistry(), getDefault().getPreferenceStore(), + "org.jboss.ide.eclipse.freemarker.templates"); + try + { + fStore.load(); + } + catch(IOException e) + { + log(e); + } + } + return fStore; + } + +} Index: src/org/jboss/ide/eclipse/freemarker/configuration/context/AbstractContextValue.java =================================================================== RCS file: src/org/jboss/ide/eclipse/freemarker/configuration/context/AbstractContextValue.java diff -N src/org/jboss/ide/eclipse/freemarker/configuration/context/AbstractContextValue.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/jboss/ide/eclipse/freemarker/configuration/context/AbstractContextValue.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,45 @@ +package org.jboss.ide.eclipse.freemarker.configuration.context; + +/** + * + * Abstract class for manage context value. + * + * @version 1.0.4 + * @author Angelo ZERR + * + */ +public abstract class AbstractContextValue implements IContextValue { + + private String name; + private IObjectContext[] objectContexts; + + protected AbstractContextValue(String name) { + this.name = name; + this.objectContexts = new IObjectContext[2]; + } + + public String getName() { + return this.name; + } + + public IObjectContext[] getContexts() { + return objectContexts; + } + + protected void setFirstObjectContext(IObjectContext objectContext) { + objectContexts[0] = objectContext; + } + + public IObjectContext getFirstObjectContext() { + return this.objectContexts[0]; + } + + public IObjectContext getSecondObjectContext() { + return this.objectContexts[1]; + } + + protected void setSecondObjectContext(IObjectContext objectContext) { + objectContexts[1] = objectContext; + } + +} Index: src/org/jboss/ide/eclipse/freemarker/configuration/context/ClassContextValue.java =================================================================== RCS file: src/org/jboss/ide/eclipse/freemarker/configuration/context/ClassContextValue.java diff -N src/org/jboss/ide/eclipse/freemarker/configuration/context/ClassContextValue.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/jboss/ide/eclipse/freemarker/configuration/context/ClassContextValue.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,43 @@ +package org.jboss.ide.eclipse.freemarker.configuration.context; + +/** + * Class context value. + * + * @version 1.0.4 + * @author Angelo ZERR + * + */ +public class ClassContextValue extends AbstractContextValue { + + public ClassContextValue(String name, Class objClass, Class singularClass) + { + super(name); + // Object class + setFirstObjectContext(new ClassObjectContext(objClass)); + // Singular class + setSecondObjectContext(new ClassObjectContext(singularClass)); + } + + public int getType() { + return IContextValue.CLASS; + } + + public class ClassObjectContext implements IObjectContext { + + private Class objectClass; + + public ClassObjectContext(Class objectClass) { + this.objectClass = objectClass; + } + + public String getName() { + if (objectClass != null) + return objectClass.getName(); + return null; + } + + public Object getObject() { + return this.objectClass; + } + } +} Index: src/org/jboss/ide/eclipse/freemarker/configuration/context/IContextValue.java =================================================================== RCS file: src/org/jboss/ide/eclipse/freemarker/configuration/context/IContextValue.java diff -N src/org/jboss/ide/eclipse/freemarker/configuration/context/IContextValue.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/jboss/ide/eclipse/freemarker/configuration/context/IContextValue.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,24 @@ +package org.jboss.ide.eclipse.freemarker.configuration.context; + +/** + * + * Interface context value. + * @version 1.0.0 + * @author Angelo ZERR + * + */ +public interface IContextValue { + + public final static int CLASS = 0; + public final static int STRING = 1; + public final static int XML = 2; + public final static int DTD = 3; + + public String getName(); + public int getType(); + public IObjectContext[] getContexts(); + + public IObjectContext getFirstObjectContext(); + public IObjectContext getSecondObjectContext(); + +} Index: src/org/jboss/ide/eclipse/freemarker/configuration/context/IObjectContext.java =================================================================== RCS file: src/org/jboss/ide/eclipse/freemarker/configuration/context/IObjectContext.java diff -N src/org/jboss/ide/eclipse/freemarker/configuration/context/IObjectContext.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/jboss/ide/eclipse/freemarker/configuration/context/IObjectContext.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,7 @@ +package org.jboss.ide.eclipse.freemarker.configuration.context; + +public interface IObjectContext { + + public String getName(); + public Object getObject(); +} Index: src/org/jboss/ide/eclipse/freemarker/configuration/context/XMLContextValue.java =================================================================== RCS file: src/org/jboss/ide/eclipse/freemarker/configuration/context/XMLContextValue.java diff -N src/org/jboss/ide/eclipse/freemarker/configuration/context/XMLContextValue.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/jboss/ide/eclipse/freemarker/configuration/context/XMLContextValue.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,43 @@ +package org.jboss.ide.eclipse.freemarker.configuration.context; + +import org.w3c.dom.Document; + +/** + * XML context value + * + * @version 1.0.0 + * @author Angelo ZERR + * + */ +public class XMLContextValue extends AbstractContextValue { + + + public XMLContextValue(String name, String xmlFile, Document document) { + super(name); + this.setFirstObjectContext(new XMLObjectContext(xmlFile, document)); + } + + public int getType() { + return IContextValue.XML; + } + + public class XMLObjectContext implements IObjectContext { + + private String xmlFile; + private Document document; + + public XMLObjectContext(String xmlFile, Document document) { + this.xmlFile = xmlFile; + this.document = document; + } + + public String getName() { + return xmlFile; + } + + public Object getObject() { + return this.document; + } + } + +} Index: src/org/jboss/ide/eclipse/freemarker/contentassist/CompletionProcessor.java =================================================================== RCS file: src/org/jboss/ide/eclipse/freemarker/contentassist/CompletionProcessor.java diff -N src/org/jboss/ide/eclipse/freemarker/contentassist/CompletionProcessor.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/jboss/ide/eclipse/freemarker/contentassist/CompletionProcessor.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,295 @@ +/* + * JBoss, a division of Red Hat + * Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.ide.eclipse.freemarker.contentassist; + + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.eclipse.core.resources.IResource; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.ITextSelection; +import org.eclipse.jface.text.ITextViewer; +import org.eclipse.jface.text.Region; +import org.eclipse.jface.text.contentassist.CompletionProposal; +import org.eclipse.jface.text.contentassist.ICompletionProposal; +import org.eclipse.jface.text.contentassist.IContentAssistProcessor; +import org.eclipse.jface.text.source.ISourceViewer; +import org.eclipse.jface.text.templates.Template; +import org.eclipse.jface.text.templates.TemplateCompletionProcessor; +import org.eclipse.jface.text.templates.TemplateContext; +import org.eclipse.jface.text.templates.TemplateContextType; +import org.eclipse.jface.text.templates.TemplateException; +import org.eclipse.swt.graphics.Image; +import org.jboss.ide.eclipse.freemarker.FreemarkerPlugin; +import org.jboss.ide.eclipse.freemarker.configuration.ConfigurationManager; +import org.jboss.ide.eclipse.freemarker.configuration.context.IContextValue; +import org.jboss.ide.eclipse.freemarker.editor.editorpart.SourceEditor; +import org.jboss.ide.eclipse.freemarker.model.CompletionDirective; +import org.jboss.ide.eclipse.freemarker.model.CompletionInterpolation; +import org.jboss.ide.eclipse.freemarker.model.CompletionMacroInstance; +import org.jboss.ide.eclipse.freemarker.model.Item; +import org.jboss.ide.eclipse.freemarker.model.ItemSet; +import org.jboss.ide.eclipse.freemarker.model.MacroInstance; +import org.jboss.ide.eclipse.freemarker.template.FreemarkerTemplateContextType; +import org.jboss.ide.eclipse.freemarker.template.FreemarkerTemplateProposal; + +/** + * @author Joe Hudson + */ +public class CompletionProcessor extends TemplateCompletionProcessor implements IContentAssistProcessor { + + private SourceEditor editor; + + + public CompletionProcessor (SourceEditor editor) { + this.editor = editor; + } + + public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) { + // Completion proposal for context + ICompletionProposal[] result = computeContextCompletionProposals(viewer, offset); + // Completion proposal for defined template + ICompletionProposal templates[] = templateComputeCompletionProposals(viewer, offset); + if(result == null) + return templates; + if(templates == null) + { + return result; + } else + { + ICompletionProposal all[] = new ICompletionProposal[result.length + templates.length]; + System.arraycopy(templates, 0, all, 0, templates.length); + System.arraycopy(result, 0, all, templates.length, result.length); + return all; + } + } + + protected TemplateContextType getContextType(ITextViewer viewer, IRegion region) { + return FreemarkerPlugin.getDefault(). + getContextTypeRegistry(). + getContextType(FreemarkerTemplateContextType.FREEMARKER_CONTEXT_TYPE); + } + + protected Image getImage(Template template) { + return null; + } + + public char[] getCompletionProposalAutoActivationCharacters() { + return new char[]{'.', '$', '#', '@', '/', '?', '{'}; + } + + /** + * Completion proposal for context. + * @param viewer + * @param offset + * @return + */ + private ICompletionProposal[] computeContextCompletionProposals(ITextViewer viewer, int offset) { + try { + ItemSet directiveSet = editor.getItemSet(); + + Map context = new HashMap(); + IContextValue[] values = ConfigurationManager.getInstance(editor.getProject()).getContextValues(editor.getFile(), true); + for (int i=0; i=topOffset; i--) { + char c = editor.getDocument().getChar(i); + if (c == '>' || c == ']') break; + if (c == '<' || c == '[') { + if (editor.getDocument().getLength() > i) { + char c2 = editor.getDocument().getChar(i+1); + if (c2 == '#') { + CompletionDirective completionDirective = new CompletionDirective( + i, offset - i, editor.getItemSet(), (ISourceViewer) viewer, (IResource) editor.getFile()); + completionDirective.setItemSet(editor.getItemSet()); + return completionDirective.getCompletionProposals(offset, context); + } + else if (c2 == '@') { + CompletionMacroInstance completionMacroInstance = new CompletionMacroInstance( + editor.getDocument().get(i, offset - i), i, editor.getItemSet(), editor.getFile()); + completionMacroInstance.setItemSet(editor.getItemSet()); + return completionMacroInstance.getCompletionProposals(offset, context); + } + else if (c2 == '/') { + if (editor.getDocument().getLength() < i+3 + || editor.getDocument().getChar(i+2) == ' ' + || editor.getDocument().getChar(i+2) == '\r' + || editor.getDocument().getChar(i+2) == '\n') { + Item stackItem = editor.getItemSet().getPreviousStartItem(offset); + StringBuffer value = new StringBuffer(); + if (null != stackItem && stackItem instanceof MacroInstance) + value.append("@"); + else + value.append("#"); + String name = null; + if (null != stackItem) name = stackItem.getFirstToken(); + if (null != name) + value.append(name); + if (c == '<') + value.append('>'); + else + value.append(']'); + ICompletionProposal completionProposal = new CompletionProposal( + value.toString(), offset, 0, offset+value.toString().length()); + return new ICompletionProposal[]{completionProposal}; + } + } + else { + return null; + } + } + } + } + } + catch (BadLocationException e) { + return null; + } + // check for interpolations + try { + for (int i=offset-1; i>=topOffset; i--) { + char c = editor.getDocument().getChar(i); + if (c == '\n') break; + else if (c == '$') { + if (editor.getDocument().getLength() > i) { + char c2 = editor.getDocument().getChar(i+1); + if (c2 == '{') { + int j = offset; + while (editor.getDocument().getLength() > j) { + char c3 = editor.getDocument().getChar(j); + if (Character.isWhitespace(c3) || c3 == '(' || c3 == '.' || c3 == ')' || c3 == '}' || c3 == '?') { + // j = j-1; + break; + } + j++; + } + CompletionInterpolation interpolation = new CompletionInterpolation( + editor.getDocument().get(i, j - i), i, editor.getItemSet(), editor.getFile()); + interpolation.setParentItem(editor.getItemSet().getPreviousStartItem(offset)); + return interpolation.getCompletionProposals(offset, context); + } + } + } + } + } + catch (BadLocationException e) { + return null; + } + } + } + catch (Exception e) { + FreemarkerPlugin.log(e); + } + return null; + } + + /** + * Completion proposal for defined template + * @param viewer + * @param offset + * @return + */ + private ICompletionProposal[] templateComputeCompletionProposals(ITextViewer viewer, int offset) + { + ITextSelection selection = (ITextSelection)viewer.getSelectionProvider().getSelection(); + if(selection.getOffset() == offset) + offset = selection.getOffset() + selection.getLength(); + String prefix = extractPrefix(viewer, offset); + if(prefix == null || prefix.length() < 1) + return new ICompletionProposal[0]; + String indent = ""; //extractIndent(viewer, offset - prefix.length()); + Region region = new Region(offset - prefix.length(), prefix.length()); + TemplateContext context = createContext(viewer, region); + if(context == null) + return new ICompletionProposal[0]; + context.setVariable("selection", selection.getText()); + Template templates[] = getTemplates(context.getContextType().getId()); + if (templates == null) + return new ICompletionProposal[0]; + List matches = new ArrayList(); + for(int i = 0; i < templates.length; i++) + { + Template template = prepareTemplate(templates[i], indent); + try + { + context.getContextType().validate(template.getPattern()); + } + catch(TemplateException _ex) + { + continue; + } + if(template.getName().startsWith(prefix)) + matches.add(createFreemarkerProposal(template, context, region, getRelevance(template, prefix))); + } + + return (ICompletionProposal[])matches.toArray(new ICompletionProposal[matches.size()]); + } + + private Template prepareTemplate(Template template, String indent) + { + if(indent.length() == 0) + return template; + String newPattern = template.getPattern(); + StringBuffer sb = new StringBuffer(); + int i; + for(i = 0; i < newPattern.length() - 1; i++) + { + char c = newPattern.charAt(i); + char next = newPattern.charAt(i + 1); + if((c == '\n' || c == '\r') && next != '\n' && next != '\r') + sb.append(c).append(indent); + else + sb.append(c); + } + + sb.append(newPattern.charAt(i)); + newPattern = sb.toString(); + Template t = new Template(template.getName(), template.getDescription(), template.getContextTypeId(), + newPattern, template.isAutoInsertable()); + return t; + } + + protected ICompletionProposal createFreemarkerProposal(Template template, TemplateContext context, IRegion region, int relevance) + { + return new FreemarkerTemplateProposal(editor, template, context, region, getImage(template), relevance); + } + + protected Template[] getTemplates(String contextTypeId) { + return FreemarkerPlugin.getDefault().getTemplateStore().getTemplates(); + } +} Index: src/org/jboss/ide/eclipse/freemarker/contentassist/SubCompletionProposal.java =================================================================== RCS file: src/org/jboss/ide/eclipse/freemarker/contentassist/SubCompletionProposal.java diff -N src/org/jboss/ide/eclipse/freemarker/contentassist/SubCompletionProposal.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/jboss/ide/eclipse/freemarker/contentassist/SubCompletionProposal.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,88 @@ +package org.jboss.ide.eclipse.freemarker.contentassist; + +import org.eclipse.jface.text.Assert; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.contentassist.ICompletionProposal; +import org.eclipse.jface.text.contentassist.IContextInformation; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.Point; + +public class SubCompletionProposal implements ICompletionProposal { + + private String fDisplayString; + private String fReplacementString; + private int fReplacementOffset; + private int fReplacementLength; + private int fCursorPosition; + private Image fImage; + private IContextInformation fContextInformation; + private String fAdditionalProposalInfo; + private boolean fRemoveLeftDot; + + public SubCompletionProposal(String replacementString, int replacementOffset, int replacementLength, int cursorPosition) + { + this(replacementString, replacementOffset, replacementLength, cursorPosition, null, null, null, null, false); + } + + public SubCompletionProposal(String replacementString, int replacementOffset, + int replacementLength, int cursorPosition, Image image, + String displayString, IContextInformation contextInformation, + String additionalProposalInfo, boolean removeLeftDot) + { + Assert.isNotNull(replacementString); + Assert.isTrue(replacementOffset >= 0); + Assert.isTrue(replacementLength >= 0); + Assert.isTrue(cursorPosition >= 0); + fReplacementString = replacementString; + fReplacementOffset = replacementOffset; + fReplacementLength = replacementLength; + fCursorPosition = cursorPosition; + fImage = image; + fDisplayString = displayString; + fContextInformation = contextInformation; + fAdditionalProposalInfo = additionalProposalInfo; + fRemoveLeftDot = removeLeftDot; + } + + public void apply(IDocument document) + { + try + { if (fRemoveLeftDot) { + String content = document.get(fReplacementOffset, 1); + if (content.equals(".")) + document.replace(fReplacementOffset, 1, ""); + } + document.replace(fReplacementOffset, fReplacementLength, fReplacementString); + } + catch(BadLocationException _ex) { } + } + + public Point getSelection(IDocument document) + { + return new Point(fReplacementOffset + fCursorPosition, 0); + } + + public IContextInformation getContextInformation() + { + return fContextInformation; + } + + public Image getImage() + { + return fImage; + } + + public String getDisplayString() + { + if(fDisplayString != null) + return fDisplayString; + else + return fReplacementString; + } + + public String getAdditionalProposalInfo() + { + return fAdditionalProposalInfo; + } +} Index: src/org/jboss/ide/eclipse/freemarker/dialogs/OpenResourceDialog.java =================================================================== RCS file: src/org/jboss/ide/eclipse/freemarker/dialogs/OpenResourceDialog.java diff -N src/org/jboss/ide/eclipse/freemarker/dialogs/OpenResourceDialog.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/jboss/ide/eclipse/freemarker/dialogs/OpenResourceDialog.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,28 @@ +package org.jboss.ide.eclipse.freemarker.dialogs; + +import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IResource; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.dialogs.ResourceListSelectionDialog; +import org.jboss.ide.eclipse.freemarker.FreemarkerMessages; + +public class OpenResourceDialog extends ResourceListSelectionDialog { + + /** + * Creates a new instance of the class. + * + * @param parentShell the parent shell + * @param container the container + * @param typesMask the types mask + */ + public OpenResourceDialog(Shell parentShell, IContainer container) { + super(parentShell, container, IResource.FILE); + setTitle(FreemarkerMessages.getString("OpenResourceDialog.title")); + PlatformUI.getWorkbench().getHelpSystem() + .setHelp(parentShell, FreemarkerMessages.getString("OpenResourceDialog.help") + ); + setAllowUserToToggleDerived(false); + setShowDerived(true); + } +} Index: src/org/jboss/ide/eclipse/freemarker/editor/editorpart/PreviewEditor.java =================================================================== RCS file: src/org/jboss/ide/eclipse/freemarker/editor/editorpart/PreviewEditor.java diff -N src/org/jboss/ide/eclipse/freemarker/editor/editorpart/PreviewEditor.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/jboss/ide/eclipse/freemarker/editor/editorpart/PreviewEditor.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,155 @@ +package org.jboss.ide.eclipse.freemarker.editor.editorpart; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.Reader; +import java.io.StringReader; +import java.io.StringWriter; +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.jface.text.IDocument; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.StyledText; +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IEditorSite; +import org.eclipse.ui.IWorkbenchPartSite; +import org.eclipse.ui.PartInitException; +import org.eclipse.ui.part.EditorPart; +import org.jboss.ide.eclipse.freemarker.FreemarkerPlugin; +import org.jboss.ide.eclipse.freemarker.configuration.ConfigurationManager; +import org.jboss.ide.eclipse.freemarker.configuration.context.IContextValue; +import org.w3c.dom.Document; + +import freemarker.ext.dom.NodeModel; +import freemarker.template.Configuration; +import freemarker.template.Template; +import freemarker.template.TemplateException; + +/** + * + * Preview Editor for the ftl template. + * + * @version 1.0.4 + * @author Angelo ZERR + * + */ +public class PreviewEditor extends EditorPart { + + private IEditorSite site; + private PreviewEditorInput previewEditorInput; + private StyledText text; + private Configuration freemarkerConfiguration; + + public PreviewEditor() { + this.freemarkerConfiguration = new Configuration(); + } + + public void doSave(IProgressMonitor arg0) { + } + + public void doSaveAs() { + } + + public void init(IEditorSite site, IEditorInput input) throws PartInitException { + this.site = site; + previewEditorInput = (PreviewEditorInput)input; + } + + public boolean isDirty() { + return false; + } + + public boolean isSaveAsAllowed() { + return false; + } + + public void createPartControl(Composite composite) { + FillLayout layout = new FillLayout(); + composite.setLayout(layout); + text = new StyledText(composite, SWT.H_SCROLL | SWT.V_SCROLL); + text.setEditable(false); + } + + public void setFocus() { + preview(); + } + + public IWorkbenchPartSite getSite() { + return site; + } + + private void preview() { + text.setText(""); + IDocument sourceDocument = previewEditorInput.getSourceDocument(); + IFile contextValuesFile = previewEditorInput.getContextValuesFile(); + String pageContents = sourceDocument.get(); + try { + IProject project = contextValuesFile.getProject(); + /* Prepare Freemarker template with Source tab */ + Reader reader = new StringReader(pageContents); + Template template = new Template("preview.ftl", reader, this.freemarkerConfiguration); + reader.close(); + + /* Prepare Model */ + Map model = new HashMap(); + IContextValue[] contextValues = ConfigurationManager.getInstance( + project).getContextValues(contextValuesFile, false); + if (contextValues != null) { + for (int i = 0; i < contextValues.length; i++) { + IContextValue contextValue = contextValues[i]; + String contextName = contextValue.getName(); + if (IContextValue.STRING == contextValue.getType()) { + String stringValue = (String)contextValue.getFirstObjectContext().getObject(); + model.put(contextName , stringValue); + } + else { + if (IContextValue.XML == contextValue.getType()) { + Document document = (Document)contextValue.getFirstObjectContext().getObject(); + NodeModel nodeModel = NodeModel.wrap(document); + model.put(contextName , nodeModel); + } + else { + if (IContextValue.CLASS == contextValue.getType()) { + Class objectClass = (Class)contextValue.getFirstObjectContext().getObject(); + try { + Object objectInstance = objectClass.newInstance(); + model.put(contextName , objectInstance); + } + catch (Exception e) { + FreemarkerPlugin.log(e); + } + } + } + } + + } + } + + /* Merge data model with template */ + StringWriter out = new StringWriter(); + template.process(model, out); + out.flush(); + + text.setText(out.toString()); + + } catch (TemplateException e) { + StringWriter out = new StringWriter(); + PrintWriter print = new PrintWriter(out); + e.printStackTrace(print); + out.flush(); + text.setText(out.toString()); + FreemarkerPlugin.log(e); + } + catch (IOException e) { + FreemarkerPlugin.log(e); + } + } + + +} Index: src/org/jboss/ide/eclipse/freemarker/editor/editorpart/PreviewEditorInput.java =================================================================== RCS file: src/org/jboss/ide/eclipse/freemarker/editor/editorpart/PreviewEditorInput.java diff -N src/org/jboss/ide/eclipse/freemarker/editor/editorpart/PreviewEditorInput.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/jboss/ide/eclipse/freemarker/editor/editorpart/PreviewEditorInput.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,56 @@ +package org.jboss.ide.eclipse.freemarker.editor.editorpart; + +import org.eclipse.core.resources.IFile; +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.jface.text.IDocument; +import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IPersistableElement; + +/** + * + * Description : + * @version 1.0.0 + * @author Angelo ZERR + * + */ +public class PreviewEditorInput implements IEditorInput { + + private IDocument sourceDocument; + private IFile contextValuesFile; + + public PreviewEditorInput(IFile contextValuesFile, IDocument sourceDocument) { + this.sourceDocument = sourceDocument; + this.contextValuesFile = contextValuesFile; + } + + public Object getAdapter(Class arg0) { + return null; + } + + public boolean exists() { + return false; + } + + public ImageDescriptor getImageDescriptor() { + return null; + } + + public String getName() { + return "PrevEd"; + } + + public IPersistableElement getPersistable() { + return null; + } + + public String getToolTipText() { + return null; + } + public IDocument getSourceDocument() { + return sourceDocument; + } + public IFile getContextValuesFile() { + return contextValuesFile; + } + +} Index: src/org/jboss/ide/eclipse/freemarker/editor/editorpart/SourceEditor.java =================================================================== RCS file: src/org/jboss/ide/eclipse/freemarker/editor/editorpart/SourceEditor.java diff -N src/org/jboss/ide/eclipse/freemarker/editor/editorpart/SourceEditor.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/jboss/ide/eclipse/freemarker/editor/editorpart/SourceEditor.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,495 @@ +/* + * JBoss, a division of Red Hat + * Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.ide.eclipse.freemarker.editor.editorpart; + +import java.io.Reader; +import java.io.StringReader; +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IMarker; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.jdt.internal.ui.javaeditor.JarEntryEditorInput; +import org.eclipse.jdt.internal.ui.text.JavaPairMatcher; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.ITextViewerExtension2; +import org.eclipse.jface.text.ITypedRegion; +import org.eclipse.jface.text.source.ISourceViewer; +import org.eclipse.jface.text.source.MatchingCharacterPainter; +import org.eclipse.jface.text.source.SourceViewer; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.StyleRange; +import org.eclipse.swt.events.KeyEvent; +import org.eclipse.swt.events.KeyListener; +import org.eclipse.swt.events.MouseEvent; +import org.eclipse.swt.events.MouseListener; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.ui.IFileEditorInput; +import org.eclipse.ui.editors.text.TextEditor; +import org.eclipse.ui.texteditor.ContentAssistAction; +import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds; +import org.eclipse.ui.texteditor.MarkerUtilities; +import org.eclipse.ui.views.contentoutline.IContentOutlinePage; +import org.jboss.ide.eclipse.freemarker.Constants; +import org.jboss.ide.eclipse.freemarker.FreemarkerPlugin; +import org.jboss.ide.eclipse.freemarker.configuration.ConfigurationManager; +import org.jboss.ide.eclipse.freemarker.editor.ColorManager; +import org.jboss.ide.eclipse.freemarker.editor.DocumentProvider; +import org.jboss.ide.eclipse.freemarker.model.Item; +import org.jboss.ide.eclipse.freemarker.model.ItemSet; +import org.jboss.ide.eclipse.freemarker.outline.OutlinePage; + +import freemarker.core.ParseException; +import freemarker.template.Configuration; +import freemarker.template.Template; + +/** + * @author Joe Hudson + */ +public class SourceEditor extends TextEditor implements KeyListener, MouseListener { + + private OutlinePage fOutlinePage; + private org.jboss.ide.eclipse.freemarker.editor.Configuration configuration; + private ColorManager colorManager = new ColorManager(); + + private ItemSet itemSet; + private Item selectedItem; + private Item[] relatedItems; + private static final char[] VALIDATION_TOKENS = new char[]{'\"', '[', ']', ',', '.', '\n', '4'}; + private boolean readOnly = false; + + private boolean mouseDown = false; + private boolean ctrlDown = false; + private boolean shiftDown = false; + + public SourceEditor() { + super(); + configuration = new org.jboss.ide.eclipse.freemarker.editor.Configuration(getPreferenceStore(), colorManager, this); + setSourceViewerConfiguration(configuration); + setDocumentProvider(new DocumentProvider()); + } + public void dispose() { + ConfigurationManager.getInstance(getProject()).reload(); + super.dispose(); + if(matchingCharacterPainter!=null) { + matchingCharacterPainter.dispose(); + } + } + + public Object getAdapter(Class aClass) { + Object adapter; + if (aClass.equals(IContentOutlinePage.class)) { + if (fOutlinePage == null) { + fOutlinePage = new OutlinePage(this); + if (getEditorInput() != null) { + fOutlinePage.setInput(getEditorInput()); + } + } + adapter = fOutlinePage; + } else { + adapter = super.getAdapter(aClass); + } + return adapter; + } + + protected static final char[] BRACKETS= {'{', '}', '(', ')', '[', ']', '<', '>' }; + private MatchingCharacterPainter matchingCharacterPainter; + public void createPartControl(Composite parent) { + super.createPartControl(parent); + getSourceViewer().getTextWidget().addKeyListener(this); + getSourceViewer().getTextWidget().addMouseListener(this); + //matchingCharacterPainter = new MatchingCharacterPainter( + // getSourceViewer(), + // new JavaPairMatcher(BRACKETS)); + //((SourceViewer) getSourceViewer()).addPainter(matchingCharacterPainter); + } + + protected void createActions() { + super.createActions(); + // Add content assist propsal action + ContentAssistAction action = new ContentAssistAction( + FreemarkerPlugin.getDefault().getResourceBundle(), + "FreemarkerEditor.ContentAssist", this); + action.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS); + setAction("FreemarkerEditor.ContentAssist", action); + action.setEnabled(true); + } + + protected void handleCursorPositionChanged() { + super.handleCursorPositionChanged(); + if (!mouseDown) { + int offset = getCaretOffset(); + Item item = getItemSet().getSelectedItem(offset); + if (null == item && offset > 0) item = getItemSet().getSelectedItem(offset-1); + if (FreemarkerPlugin.getInstance().getPreferenceStore().getBoolean( + Constants.HIGHLIGHT_RELATED_ITEMS)) { + if (null != item && null != item.getRelatedItems() && item.getRelatedItems().length > 0) { + highlightRelatedRegions(item.getRelatedItems(), item); + } + else { + highlightRelatedRegions(null, item); + } + } + if (null == item) { + item = getItemSet().getContextItem(getCaretOffset()); + } + if (null != fOutlinePage) + fOutlinePage.update(item); + } + } + public void mouseDoubleClick(MouseEvent e) { + } + public void mouseDown(MouseEvent e) { + mouseDown = true; + } + public void mouseUp(MouseEvent e) { + mouseDown = false; + handleCursorPositionChanged(); + } + + public void select (Item item) { + selectAndReveal(item.getRegion().getOffset(), item.getRegion().getLength()); + } + + public IDocument getDocument() { + ISourceViewer viewer = getSourceViewer(); + if (viewer != null) { + return viewer.getDocument(); + } + return null; + } + + public void addProblemMarker(String aMessage, int aLine) { + IFile file = ((IFileEditorInput)getEditorInput()).getFile(); + try { + Map attributes = new HashMap(5); + attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR)); + attributes.put(IMarker.LINE_NUMBER, new Integer(aLine)); + attributes.put(IMarker.MESSAGE, aMessage); + attributes.put(IMarker.TEXT, aMessage); + MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM); + } catch (Exception e) { + + } + } + + private synchronized void highlightRelatedRegions (Item[] items, Item selectedItem) { + if (null == items || items.length == 0) { + if (null != relatedItems && relatedItems.length > 0) { + for (int i=0; i= relatedItems[i].getRegion().getOffset() + relatedItems[i].getRegion().getLength()) { + if (null == this.selectedItem || !relatedItems[i].equals(this.selectedItem)) + resetRange(relatedItems[i].getRegion()); + } + } + } + relatedItems = null; + } + if (null != relatedItems) { + for (int i=0; i= relatedItems[i].getRegion().getOffset() + relatedItems[i].getRegion().getLength()) { + if (null == this.selectedItem || !relatedItems[i].equals(this.selectedItem)) + resetRange(relatedItems[i].getRegion()); + } + } + } + if (null != items && items.length > 0) { + for (int i=0; i= items[i].getRegion().getOffset() + items[i].getRegion().getLength() + && !items[i].equals(selectedItem)) { + ITypedRegion region = items[i].getRegion(); + getSourceViewer().getTextWidget().setStyleRange( + new StyleRange(region.getOffset(), + region.getLength(), null, + colorManager.getColor( + Constants.COLOR_RELATED_ITEM))); + } + } + } + relatedItems = items; + this.selectedItem = selectedItem; + } + + private void resetRange (ITypedRegion region) { + if (getSourceViewer() instanceof ITextViewerExtension2) + ((ITextViewerExtension2) getSourceViewer()).invalidateTextPresentation(region.getOffset(), region.getLength()); + else + getSourceViewer().invalidateTextPresentation(); + } + + public Item getSelectedItem (boolean allowFudge) { + int caretOffset = getCaretOffset(); + Item item = getItemSet().getSelectedItem(getCaretOffset()); + if (null == item && caretOffset > 0) item = getItemSet().getSelectedItem(caretOffset - 1); + return item; + } + + public Item getSelectedItem () { + return getItemSet().getSelectedItem(getCaretOffset()); + } + + public int getCaretOffset () { + return getSourceViewer().getTextWidget().getCaretOffset(); + } + + public void clearCache () { + this.itemSet = null; + } + + public ItemSet getItemSet () { + if (null == this.itemSet) { + IResource resource = null; + if (getEditorInput() instanceof IFileEditorInput) { + resource = ((IFileEditorInput) getEditorInput()).getFile(); + } + else if (getEditorInput() instanceof JarEntryEditorInput) { + resource = null; + } + + this.itemSet = new ItemSet( + getSourceViewer(), resource); + } + return this.itemSet; + + } + public OutlinePage getOutlinePage() { + return fOutlinePage; + } + + public void keyPressed(KeyEvent e) { + if (e.keyCode == SWT.CTRL) { + ctrlDown = true; + } + if (e.keyCode == SWT.SHIFT) { + shiftDown = true; + } + if (e.keyCode == ']') { + try { + char c = getDocument().getChar(getCaretOffset()); + if (c == ']') { + // remove this + getDocument().replace(getCaretOffset(), 1, ""); + } + } + catch (BadLocationException e1) {} + } + else if (e.keyCode == '}') { + try { + char c = getDocument().getChar(getCaretOffset()); + if (c == '}') { + // remove this + getDocument().replace(getCaretOffset(), 1, "}"); + } + } + catch (BadLocationException e1) {} + } + } + + public void keyReleased(KeyEvent e) { + if (e.keyCode == SWT.CTRL) { + ctrlDown = false; + } + else if (e.keyCode == SWT.SHIFT) { + shiftDown = false; + } + try { + if (shiftDown && (e.keyCode == '3' || e.keyCode == '2')) { + int offset = getCaretOffset(); + char c = getSourceViewer().getDocument().getChar(offset-2); + if (c == '[' || c == '<') { + // directive + char endChar = Character.MIN_VALUE; + if (c == '[') endChar = ']'; else endChar = '>'; + if (getSourceViewer().getDocument().getLength() > offset) { + if (offset > 0) { + for (int i=offset+1; i offset) { + if (offset > 0) { + for (int i=offset+1; i 0) item = getItemSet().getSelectedItem(offset-1); + if (FreemarkerPlugin.getInstance().getPreferenceStore().getBoolean( + Constants.HIGHLIGHT_RELATED_ITEMS)) { + if (null != item && null != item.getRelatedItems() && item.getRelatedItems().length > 0) { + highlightRelatedRegions(item.getRelatedItems(), item); + } + else { + highlightRelatedRegions(null, item); + } + } + clearCache(); + validateContents(); + if (null != fOutlinePage) + fOutlinePage.update(getSelectedItem()); + } + } + + private void showMessage (String text) { + MessageDialog.openError( + getSourceViewer().getTextWidget().getShell(), + "message", + text); + } + + public static Validator VALIDATOR; + public synchronized void validateContents () { + if (null == VALIDATOR) { + VALIDATOR = new Validator(this); + VALIDATOR.start(); + } + } + + public IProject getProject () { + return ((IFileEditorInput) getEditorInput()).getFile().getProject(); + } + + public IFile getFile () { + return (null != getEditorInput()) ? + ((IFileEditorInput) getEditorInput()).getFile() : null; + } + + private Configuration fmConfiguration; + public class Validator extends Thread { + SourceEditor editor; + public Validator (SourceEditor editor) { + this.editor = editor; + } + public void run () { + try { + if (null != getFile()) { + if (null == fmConfiguration) { + fmConfiguration = new Configuration(); + fmConfiguration.setTagSyntax(Configuration.AUTO_DETECT_TAG_SYNTAX); + } + getFile().deleteMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE); + String pageContents = getDocument().get(); + Reader reader = new StringReader(pageContents); + new Template(getFile().getName(), reader, fmConfiguration); + reader.close(); + } + } + catch (ParseException e) { + if (e.getMessage() != null) { + String errorStr = e.getMessage(); + int errorLine = 0; + try { + errorLine = e.getLineNumber(); + if (errorLine == 0) { + // sometimes they forget to put it in + int index = e.getMessage().indexOf("line: "); + if (index > 0) { + int index2 = e.getMessage().indexOf(" ", index+6); + int index3 = e.getMessage().indexOf(",", index+6); + if (index3 < index2 && index3 > 0) index2 = index3; + String s = e.getMessage().substring(index+6, index2); + try { + errorLine = Integer.parseInt(s); + } + catch (Exception e2) {} + } + } + } catch (NullPointerException npe) { + errorLine = 0; + } + editor.addProblemMarker(errorStr, errorLine); + } + } + catch (Exception e) { + FreemarkerPlugin.log(e); + } + finally { + editor.VALIDATOR = null; + } + } + } + + protected void editorSaved() { + super.editorSaved(); + validateContents(); + } + + public boolean isEditorInputReadOnly() { + return readOnly; + } + public void setReadOnly(boolean readOnly) { + this.readOnly = readOnly; + } +} Index: src/org/jboss/ide/eclipse/freemarker/helper/ClassHelper.java =================================================================== RCS file: src/org/jboss/ide/eclipse/freemarker/helper/ClassHelper.java diff -N src/org/jboss/ide/eclipse/freemarker/helper/ClassHelper.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/jboss/ide/eclipse/freemarker/helper/ClassHelper.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,31 @@ +package org.jboss.ide.eclipse.freemarker.helper; + +/** + * + * Class Helper + * + * @version 1.0.4 + * @author Angelo ZERR + * + */ +public class ClassHelper { + + /** + * Return true if class test extends Class base and false otherwise + * @param test + * @param base + * @return + */ + public static boolean instanceOf (Class test, Class base) { + if (null == test || null == base) return false; + while (null != test) { + for (int i=0; i 0 && mName.startsWith("get") && mName.toUpperCase().startsWith(pUpper)) { + StringBuffer display = new StringBuffer(); + display.append(mName); + display.append("("); + for (int j=0; j 0) display.append(", "); + display.append(m.getParameterTypes()[j].getName()); + } + display.append(")"); + String actual = mName + "()"; + int tLength = actual.length(); + if (m.getParameterTypes().length > 0) tLength--; + proposals.add(new CompletionProposal(actual, + offset - subOffset + 1, content.length()-1, tLength, + null, display.toString() + " - " + m.getReturnType().getName(), null, null)); + } + } + return proposals; + } + catch (IntrospectionException e) { + return null; + } + } + + /* -------------- XML context helper -------------*/ + + private static List getXMLChildrenCompletionProposals (String content, int subOffset, int offset, Node parentNode, IResource file) { + List proposals = new ArrayList(); + try { + Element element = null; + if (parentNode instanceof Document) { + Document document = (Document)parentNode; + element = document.getDocumentElement(); + String elementName = element.getNodeName(); + proposals.add(getCompletionProposalForElement + (content, subOffset, offset, elementName, "XML Root Element")); + } + else { + // Attribute + NamedNodeMap attributes = parentNode.getAttributes(); + int attributeLength = attributes.getLength(); + for (int j = 0; j < attributeLength; j++) { + Node attribute = attributes.item(j); + String attributeName = attribute.getNodeName(); + attributeName = "@" + attributeName; + proposals.add(new CompletionProposal( + attributeName, + offset - subOffset + 1, + content.length()-1, + attributeName.length(), + null, attributeName + " - XML Attribute" , null, null)); + } + + //Element children + NodeList nodeList = parentNode.getChildNodes(); + int length = nodeList.getLength(); + Map elementsMap = new HashMap(); + for (int i=0; iAngelo ZERR + * + */ +public class ContextValueHelper { + + /** + * Return true if objectContext is Class and false otherwise. + * @param objectContext + * @return + */ + public static boolean isClassContextValue(Object objectContext) { + return (objectContext != null && + objectContext instanceof Class); + } + + /** + * Return true if objectContext is String and false otherwise. + * @param objectContext + * @return + */ + public static boolean isStringContextValue(Object objectContext) { + return (objectContext != null && + objectContext instanceof java.lang.String); + } + + /** + * Return true if objectContext is XML and false otherwise. + * @param objectContext + * @return + */ + public static boolean isXMLContextValue(Object objectContext) { + return (objectContext != null && objectContext instanceof Node); + } + + /** + * Return true if objectContext is DTD and false otherwise. + * @param objectContext + * @return + */ + /*public static boolean isDTDContextValue(Object objectContext) { + return (objectContext != null && + (objectContext instanceof ElementType || + objectContext instanceof DocType)); + }*/ + + /** + * Return child Node Element of parentObject Node with name elementName. + * @param parentObject + * @param elementName + * @return + */ + public static Node getChildNode(Object parentObject, String elementName) { + Node parentNode = (Node)parentObject; + if (parentNode.getNodeName().equals(elementName)) + return parentNode; + NodeList nodeList = parentNode.getChildNodes(); + int length = nodeList.getLength(); + for (int i=0; i 0) { + context1 = context1.trim(); + if (context1.length() > 0) { + objectClass = configurationManager. + getClass(context1); + } + } + if (objectClass == null) + return null; + Class singularClass = null; + if (context2 != null && context2.length() > 0) { + context2 = context2.trim(); + if (context2.length() > 0) { + singularClass = configurationManager.getClass(context2); + } + } + return new ClassContextValue(name, objectClass, singularClass); + } + else { + if (contextType == IContextValue.STRING) { + // String context value + return null; + //return new StringContextValue(name, context1); + } + else { + if (contextType == IContextValue.XML) { + // XML context value + // 1. Get file ressource + IFile file = configurationManager.getFile(context1); + // 2. Load XML document + Document document = DocumentBuilderFactory.newInstance(). + newDocumentBuilder().parse(file.getContents()); + return new XMLContextValue(name, context1, document); + } + else { + /*if (contextType == IContextValue.DTD) { + + // DTD context value + // 1. Get file ressource + IFile file = configurationManager.getFile(context1); + if (!file.exists()) { + throw new FileNotFoundException(context1); + } + + // 2. Load DTD DocType + DTDSource dtdSource = new DTDFileInputStream(context1, file.getContents()); + DTDParser parser = new DTDParser(); + parser.parse(dtdSource); + + DocType doctype = parser.getDocType(); + return new DTDContextValue(name, context1, doctype); + }*/ + } + } + } + return null; + } + + /** + * Return true if content is start fragment and false otherwise. + * eg : if content=doc return true and + * if content=doc.hibernate-mapping return false. + * @param content + * @return + */ + public static boolean isStartFragment (String content) { + return !content.startsWith("."); + } + +} Index: src/org/jboss/ide/eclipse/freemarker/model/interpolation/IndexedParametersFragment.java =================================================================== RCS file: src/org/jboss/ide/eclipse/freemarker/model/interpolation/IndexedParametersFragment.java diff -N src/org/jboss/ide/eclipse/freemarker/model/interpolation/IndexedParametersFragment.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/jboss/ide/eclipse/freemarker/model/interpolation/IndexedParametersFragment.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,42 @@ +package org.jboss.ide.eclipse.freemarker.model.interpolation; + +import java.util.List; +import java.util.Map; + +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; + +/** + * + * @version 1.0.4 + * @author Angelo ZERR + * + */ +public class IndexedParametersFragment extends NameFragment { + + public IndexedParametersFragment(int offset, String content) { + super(offset, content); + } + + public Object getReturnObject (Object parentObject, List fragments, Map context, IResource resource, IProject project) { + return super.getReturnObject(parentObject, fragments, context, resource, project); + } + + public String getContent() { + String content = super.getContent(); + if (content.startsWith("\"")) + content = content.substring(1, content.length()); + if (content.endsWith("\"")) + content = content.substring(0, content.length() -1); + return content; + } + + public boolean isStartFragment () { + return false; + } + + public int getLength() { + return getContent().length(); + } + +} Index: src/org/jboss/ide/eclipse/freemarker/preferences/FreemarkerTemplatePreferencePage.java =================================================================== RCS file: src/org/jboss/ide/eclipse/freemarker/preferences/FreemarkerTemplatePreferencePage.java diff -N src/org/jboss/ide/eclipse/freemarker/preferences/FreemarkerTemplatePreferencePage.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/jboss/ide/eclipse/freemarker/preferences/FreemarkerTemplatePreferencePage.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,36 @@ +package org.jboss.ide.eclipse.freemarker.preferences; + +import org.eclipse.ui.IWorkbenchPreferencePage; +import org.eclipse.ui.texteditor.templates.TemplatePreferencePage; +import org.jboss.ide.eclipse.freemarker.FreemarkerPlugin; + +/** + * + * Manage Freemarker Template. + * @version 1.0.0 + * @author Angelo ZERR + * + */ +public class FreemarkerTemplatePreferencePage extends TemplatePreferencePage + implements IWorkbenchPreferencePage +{ + + public FreemarkerTemplatePreferencePage() + { + setPreferenceStore(org.jboss.ide.eclipse.freemarker.FreemarkerPlugin.getDefault().getPreferenceStore()); + setTemplateStore(FreemarkerPlugin.getDefault().getTemplateStore()); + setContextTypeRegistry(FreemarkerPlugin.getDefault().getContextTypeRegistry()); + } + + protected boolean isShowFormatterSetting() + { + return false; + } + + public boolean performOk() + { + boolean ok = super.performOk(); + FreemarkerPlugin.getDefault().savePluginPreferences(); + return ok; + } +} Index: src/org/jboss/ide/eclipse/freemarker/template/FreemarkerTemplateContextType.java =================================================================== RCS file: src/org/jboss/ide/eclipse/freemarker/template/FreemarkerTemplateContextType.java diff -N src/org/jboss/ide/eclipse/freemarker/template/FreemarkerTemplateContextType.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/jboss/ide/eclipse/freemarker/template/FreemarkerTemplateContextType.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,34 @@ +package org.jboss.ide.eclipse.freemarker.template; + +import org.eclipse.jface.text.templates.TemplateContextType; + +/** + * + * Set template context type for Freemarker Preference. + * + * @version 1.0.4 + * @author Angelo ZERR + * + */ +public class FreemarkerTemplateContextType extends TemplateContextType +{ + public static final String FREEMARKER_CONTEXT_TYPE = "org.jboss.ide.eclipse.freemarker.template.context"; + + public FreemarkerTemplateContextType() + { + addGlobalResolvers(); + } + + private void addGlobalResolvers() + { + addResolver(new org.eclipse.jface.text.templates.GlobalTemplateVariables.Cursor()); + addResolver(new org.eclipse.jface.text.templates.GlobalTemplateVariables.WordSelection()); + addResolver(new org.eclipse.jface.text.templates.GlobalTemplateVariables.LineSelection()); + addResolver(new org.eclipse.jface.text.templates.GlobalTemplateVariables.Dollar()); + addResolver(new org.eclipse.jface.text.templates.GlobalTemplateVariables.Date()); + addResolver(new org.eclipse.jface.text.templates.GlobalTemplateVariables.Year()); + addResolver(new org.eclipse.jface.text.templates.GlobalTemplateVariables.Time()); + addResolver(new org.eclipse.jface.text.templates.GlobalTemplateVariables.User()); + } + +} Index: src/org/jboss/ide/eclipse/freemarker/template/FreemarkerTemplateProposal.java =================================================================== RCS file: src/org/jboss/ide/eclipse/freemarker/template/FreemarkerTemplateProposal.java diff -N src/org/jboss/ide/eclipse/freemarker/template/FreemarkerTemplateProposal.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/jboss/ide/eclipse/freemarker/template/FreemarkerTemplateProposal.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,42 @@ +package org.jboss.ide.eclipse.freemarker.template; + +import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.ITextViewer; +import org.eclipse.jface.text.templates.Template; +import org.eclipse.jface.text.templates.TemplateContext; +import org.eclipse.jface.text.templates.TemplateProposal; +import org.eclipse.swt.graphics.Image; +import org.jboss.ide.eclipse.freemarker.editor.editorpart.SourceEditor; + +/** + * + * Template proposal for Freemarker. + * + * @version 1.0.4 + * @author Angelo ZERR + * + */ +public class FreemarkerTemplateProposal extends TemplateProposal{ + + private SourceEditor sourceEditor; + + public FreemarkerTemplateProposal(SourceEditor sourceEditor, + Template arg0, TemplateContext arg1, IRegion arg2, Image arg3, int arg4) { + super(arg0, arg1, arg2, arg3, arg4); + this.sourceEditor = sourceEditor; + + } + + public FreemarkerTemplateProposal(SourceEditor sourceEditor, + Template arg0, TemplateContext arg1, IRegion arg2, Image arg3) { + super(arg0, arg1, arg2, arg3); + this.sourceEditor = sourceEditor; + + } + + public void apply(ITextViewer viewer, char arg1, int arg2, int arg3) { + super.apply(viewer, arg1, arg2, arg3); + sourceEditor.clearCache(); + } + +} Index: templates/templates.xml =================================================================== RCS file: templates/templates.xml diff -N templates/templates.xml --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ templates/templates.xml 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,22 @@ + + + + + + + + +