added first version of codetemplates (needs to fix some bugs)
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / PHPeclipsePlugin.java
index 1e54b0e..b0b2811 100644 (file)
@@ -12,6 +12,7 @@
 package net.sourceforge.phpeclipse;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -28,16 +29,17 @@ import net.sourceforge.phpdt.core.JavaCore;
 import net.sourceforge.phpdt.core.WorkingCopyOwner;
 import net.sourceforge.phpdt.externaltools.internal.model.ColorManager;
 import net.sourceforge.phpdt.externaltools.internal.model.ExternalToolsPlugin;
-import net.sourceforge.phpdt.externaltools.internal.model.VariableContextManager;
 import net.sourceforge.phpdt.internal.core.BatchOperation;
 import net.sourceforge.phpdt.internal.core.JavaModelManager;
-import net.sourceforge.phpdt.internal.core.util.Util;
+import net.sourceforge.phpdt.internal.corext.template.php.CodeTemplateContextType;
+import net.sourceforge.phpdt.internal.corext.template.php.HTMLContextType;
+import net.sourceforge.phpdt.internal.corext.template.php.JavaContextType;
+import net.sourceforge.phpdt.internal.corext.template.php.JavaDocContextType;
 import net.sourceforge.phpdt.internal.ui.IJavaStatusConstants;
 import net.sourceforge.phpdt.internal.ui.JavaElementAdapterFactory;
 import net.sourceforge.phpdt.internal.ui.ResourceAdapterFactory;
 import net.sourceforge.phpdt.internal.ui.preferences.MembersOrderPreferenceCache;
 import net.sourceforge.phpdt.internal.ui.preferences.MockupPreferenceStore;
-import net.sourceforge.phpdt.internal.ui.preferences.TemplatePreferencePage;
 import net.sourceforge.phpdt.internal.ui.text.PreferencesAdapter;
 import net.sourceforge.phpdt.internal.ui.text.folding.JavaFoldingStructureProviderRegistry;
 import net.sourceforge.phpdt.internal.ui.text.java.hover.JavaEditorTextHoverDescriptor;
@@ -58,7 +60,6 @@ import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr;
 import net.sourceforge.phpeclipse.phpeditor.WorkingCopyManager;
 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
 
-import org.eclipse.core.internal.runtime.InternalPlatform;
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
@@ -78,7 +79,6 @@ import org.eclipse.core.runtime.Path;
 import org.eclipse.core.runtime.Platform;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.jobs.ISchedulingRule;
-import org.eclipse.core.runtime.jobs.Job;
 import org.eclipse.jface.action.GroupMarker;
 import org.eclipse.jface.action.IMenuManager;
 import org.eclipse.jface.action.Separator;
@@ -87,6 +87,8 @@ import org.eclipse.jface.preference.PreferenceConverter;
 import org.eclipse.jface.resource.JFaceResources;
 import org.eclipse.jface.text.BadLocationException;
 import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.templates.ContextTypeRegistry;
+import org.eclipse.jface.text.templates.persistence.TemplateStore;
 import org.eclipse.jface.util.IPropertyChangeListener;
 import org.eclipse.jface.util.PropertyChangeEvent;
 import org.eclipse.swt.graphics.RGB;
@@ -101,6 +103,8 @@ import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.IWorkbenchWindow;
 import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.editors.text.EditorsUI;
+import org.eclipse.ui.editors.text.templates.ContributionContextTypeRegistry;
+import org.eclipse.ui.editors.text.templates.ContributionTemplateStore;
 import org.eclipse.ui.ide.IDE;
 import org.eclipse.ui.plugin.AbstractUIPlugin;
 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
@@ -135,6 +139,28 @@ public class PHPeclipsePlugin extends AbstractUIPlugin implements
    */
   public final static int MAX_PROPOSALS = 200;
 
+  /**
+        * The key to store customized templates. 
+        * @since 3.0
+        */
+       private static final String TEMPLATES_KEY= "net.sourceforge.phpdt.ui.text.custom_templates"; //$NON-NLS-1$
+       /**
+        * The key to store customized code templates. 
+        * @since 3.0
+        */
+       private static final String CODE_TEMPLATES_KEY= "net.sourceforge.phpdt.ui.text.custom_code_templates"; //$NON-NLS-1$
+       /**
+        * The key to store whether the legacy templates have been migrated 
+        * @since 3.0
+        */
+//     private static final String TEMPLATES_MIGRATION_KEY= "net.sourceforge.phpdt.ui.text.templates_migrated"; //$NON-NLS-1$
+       /**
+        * The key to store whether the legacy code templates have been migrated 
+        * @since 3.0
+        */
+//     private static final String CODE_TEMPLATES_MIGRATION_KEY= "net.sourceforge.phpdt.ui.text.code_templates_migrated"; //$NON-NLS-1$
+       
+
   private static ExternalToolsPlugin externalTools;
 
   /**
@@ -174,6 +200,28 @@ public class PHPeclipsePlugin extends AbstractUIPlugin implements
   //The shared instance.
   private static PHPeclipsePlugin plugin;
 
+  /** 
+        * The template context type registry for the java editor. 
+        * @since 3.0
+        */
+       private ContextTypeRegistry fContextTypeRegistry;
+       /** 
+        * The code template context type registry for the java editor. 
+        * @since 3.0
+        */
+       private ContextTypeRegistry fCodeTemplateContextTypeRegistry;
+       
+       /**
+        * The template store for the java editor. 
+        * @since 3.0
+        */
+       private TemplateStore fTemplateStore;
+       /**
+        * The coded template store for the java editor. 
+        * @since 3.0
+        */
+       private TemplateStore fCodeTemplateStore;
+       
   /** Windows 9x */
   private static final int WINDOWS_9x = 6;
 
@@ -225,7 +273,7 @@ public class PHPeclipsePlugin extends AbstractUIPlugin implements
 
   /**
    * The extension point registry for the
-   * <code>org.eclipse.jdt.ui.javaFoldingStructureProvider</code> extension
+   * <code>net.sourceforge.phpdt.ui.javaFoldingStructureProvider</code> extension
    * point.
    * 
    * @since 3.0
@@ -781,7 +829,7 @@ public class PHPeclipsePlugin extends AbstractUIPlugin implements
     //    store.setDefault(RESOURCE_BUNDLE_DE, "false");
     //    store.setDefault(RESOURCE_BUNDLE_FR, "false");
     //    store.setDefault(RESOURCE_BUNDLE_ES, "false");
-    TemplatePreferencePage.initDefaults(store);
+//    TemplatePreferencePage.initDefaults(store);
     //this will initialize the static fields in the syntaxrdr class
     new PHPSyntaxRdr();
     JavaCore.initializeDefaultPluginPreferences();
@@ -1137,47 +1185,46 @@ public class PHPeclipsePlugin extends AbstractUIPlugin implements
       //                       manager.loadVariablesAndContainers();
 
       final IWorkspace workspace = ResourcesPlugin.getWorkspace();
-      workspace.addResourceChangeListener(manager.deltaState,
-          IResourceChangeEvent.PRE_BUILD | IResourceChangeEvent.POST_BUILD
-              | IResourceChangeEvent.POST_CHANGE
-              | IResourceChangeEvent.PRE_DELETE
-              | IResourceChangeEvent.PRE_CLOSE);
-
-      //                       startIndexing();
-
-        // process deltas since last activated in indexer thread so that indexes
-        // are up-to-date.
-        // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=38658
-//        Job processSavedState = new Job(Util.bind("savedState.jobName")) { //$NON-NLS-1$
-//          protected IStatus run(IProgressMonitor monitor) {
-//            try {
-//              // add save participant and process delta atomically
-//              // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=59937
-//              workspace.run(new IWorkspaceRunnable() {
-//                public void run(IProgressMonitor progress) throws CoreException {
-//                  ISavedState savedState = workspace.addSaveParticipant(
-//                      PHPeclipsePlugin.this, manager);
-//                  if (savedState != null) {
-//                    // the event type coming from the saved state is always
-//                    // POST_AUTO_BUILD
-//                    // force it to be POST_CHANGE so that the delta processor
-//                    // can
-//                    // handle it
-//                    manager.deltaState.getDeltaProcessor().overridenEventType = IResourceChangeEvent.POST_CHANGE;
-//                    savedState.processResourceChangeEvents(manager.deltaState);
-//                  }
-//                }
-//              }, monitor);
-//            } catch (CoreException e) {
-//              return e.getStatus();
-//            }
-//            return Status.OK_STATUS;
-//          }
-//        };
-//        processSavedState.setSystem(true);
-//        processSavedState.setPriority(Job.SHORT); // process asap
-//        processSavedState.schedule();
-//      }
+               workspace.addResourceChangeListener(
+                       manager.deltaState,
+                       IResourceChangeEvent.PRE_BUILD
+                               | IResourceChangeEvent.POST_BUILD
+                               | IResourceChangeEvent.POST_CHANGE
+                               | IResourceChangeEvent.PRE_DELETE
+                               | IResourceChangeEvent.PRE_CLOSE);
+
+//             startIndexing();
+               ISavedState savedState = workspace.addSaveParticipant(PHPeclipsePlugin.this, manager);
+
+               // process deltas since last activated in indexer thread so that indexes are up-to-date.
+               // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=38658
+//             Job processSavedState = new Job(Util.bind("savedState.jobName")) { //$NON-NLS-1$
+//                     protected IStatus run(IProgressMonitor monitor) {
+//                             try {
+//                                     // add save participant and process delta atomically
+//                                     // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=59937
+//                                     workspace.run(
+//                                             new IWorkspaceRunnable() {
+//                                                     public void run(IProgressMonitor progress) throws CoreException {
+//                                                             ISavedState savedState = workspace.addSaveParticipant(PHPeclipsePlugin.this, manager);
+//                                                             if (savedState != null) {
+//                                                                     // the event type coming from the saved state is always POST_AUTO_BUILD
+//                                                                     // force it to be POST_CHANGE so that the delta processor can handle it
+//                                                                     manager.deltaState.getDeltaProcessor().overridenEventType = IResourceChangeEvent.POST_CHANGE;
+//                                                                     savedState.processResourceChangeEvents(manager.deltaState);
+//                                                             }
+//                                                     }
+//                                             },
+//                                             monitor);
+//                             } catch (CoreException e) {
+//                                     return e.getStatus();
+//                             }
+//                             return Status.OK_STATUS;
+//                     }
+//             };
+//             processSavedState.setSystem(true);
+//             processSavedState.setPriority(Job.SHORT); // process asap
+//             processSavedState.schedule();
     } catch (RuntimeException e) {
       manager.shutdown();
       throw e;
@@ -1330,7 +1377,7 @@ public class PHPeclipsePlugin extends AbstractUIPlugin implements
 
   /**
    * Returns the registry of the extensions to the
-   * <code>org.eclipse.jdt.ui.javaFoldingStructureProvider</code> extension
+   * <code>net.sourceforge.phpdt.ui.javaFoldingStructureProvider</code> extension
    * point.
    * 
    * @return the registry of contributed
@@ -1427,4 +1474,96 @@ public class PHPeclipsePlugin extends AbstractUIPlugin implements
           monitor);
     }
   }
+  
+  /**
+        * Returns the template context type registry for the java plugin.
+        * 
+        * @return the template context type registry for the java plugin
+        * @since 3.0
+        */
+       public ContextTypeRegistry getTemplateContextRegistry() {
+               if (fContextTypeRegistry == null) {
+                       fContextTypeRegistry= new ContributionContextTypeRegistry();
+                       
+                       fContextTypeRegistry.addContextType(new JavaContextType());
+                       fContextTypeRegistry.addContextType(new JavaDocContextType());
+                       fContextTypeRegistry.addContextType(new HTMLContextType());
+               }
+
+               return fContextTypeRegistry;
+       }
+       
+       /**
+        * Returns the template store for the java editor templates.
+        * 
+        * @return the template store for the java editor templates
+        * @since 3.0
+        */
+       public TemplateStore getTemplateStore() {
+               if (fTemplateStore == null) {
+//                     boolean alreadyMigrated= getPreferenceStore().getBoolean(TEMPLATES_MIGRATION_KEY);
+//                     if (alreadyMigrated)
+                               fTemplateStore= new ContributionTemplateStore(getTemplateContextRegistry(), getPreferenceStore(), TEMPLATES_KEY);
+//                     else {
+//                             fTemplateStore= new CompatibilityTemplateStore(getTemplateContextRegistry(), getPreferenceStore(), TEMPLATES_KEY, Templates.getInstance());
+//                             getPreferenceStore().setValue(TEMPLATES_MIGRATION_KEY, true);
+//                     }
+
+                       try {
+                               fTemplateStore.load();
+                       } catch (IOException e) {
+                               log(e);
+                       }
+               }
+               
+               return fTemplateStore;
+       }
+
+       /**
+        * Returns the template context type registry for the code generation
+        * templates.
+        * 
+        * @return the template context type registry for the code generation
+        *         templates
+        * @since 3.0
+        */
+       public ContextTypeRegistry getCodeTemplateContextRegistry() {
+               if (fCodeTemplateContextTypeRegistry == null) {
+                       fCodeTemplateContextTypeRegistry= new ContributionContextTypeRegistry();
+                       
+                       CodeTemplateContextType.registerContextTypes(fCodeTemplateContextTypeRegistry);
+               }
+
+               return fCodeTemplateContextTypeRegistry;
+       }
+       
+       /**
+        * Returns the template store for the code generation templates.
+        * 
+        * @return the template store for the code generation templates
+        * @since 3.0
+        */
+       public TemplateStore getCodeTemplateStore() {
+               if (fCodeTemplateStore == null) {
+//                     boolean alreadyMigrated= getPreferenceStore().getBoolean(CODE_TEMPLATES_MIGRATION_KEY);
+//                     if (alreadyMigrated)
+                               fCodeTemplateStore= new ContributionTemplateStore(getCodeTemplateContextRegistry(), getPreferenceStore(), CODE_TEMPLATES_KEY);
+//                     else {
+//                             fCodeTemplateStore= new CompatibilityTemplateStore(getCodeTemplateContextRegistry(), getPreferenceStore(), CODE_TEMPLATES_KEY, CodeTemplates.getInstance());
+//                             getPreferenceStore().setValue(CODE_TEMPLATES_MIGRATION_KEY, true);
+//                     }
+
+                       try {
+                               fCodeTemplateStore.load();
+                       } catch (IOException e) {
+                               log(e);
+                       }
+                       
+                       // compatibility / bug fixing code for duplicated templates
+                       // TODO remove for 3.0
+//                     CompatibilityTemplateStore.pruneDuplicates(fCodeTemplateStore, true);
+               }
+               
+               return fCodeTemplateStore;
+       }
 }
\ No newline at end of file