Improved Templates i.e.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / editor / WikiCompletionProcessor.java
index 533e894..d5d4866 100644 (file)
@@ -15,6 +15,7 @@ import java.util.List;
 
 import net.sourceforge.phpeclipse.wiki.editor.model.WikipediaSection;
 import net.sourceforge.phpeclipse.wiki.editor.model.WikipediaText;
+import net.sourceforge.phpeclipse.wiki.sql.WikipediaDB;
 
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.jface.resource.ImageRegistry;
@@ -40,11 +41,24 @@ import org.eclipse.swt.graphics.Image;
 
 public class WikiCompletionProcessor implements IContentAssistProcessor {
 
-  private static final String TEMPLATE_ICON = "icons/template.gif";
+  static class StringComparator implements Comparator {
+    public int compare(Object o1, Object o2) {
+      String s1 = (String) o1;
+      String s2 = (String) o2;
+      return s1.compareTo(s2);
+      //       return s1.toUpperCase().compareTo(s2.toUpperCase());
+    }
 
-//  private static final String PREPARATION_TEMPLATE_CTX = "net.sourceforge.phpeclipse.wiki.editor.preparation";
+    public boolean equals(Object o) {
+      //      String s = (String) o;
+      return compare(this, o) == 0;
+    }
+  }
 
-  private static final String INGREDIENTS_TEMPLATE_CTX = "net.sourceforge.phpeclipse.wiki.editor.templates";
+  private static final String TEMPLATE_ICON = "icons/template.gif";
+
+  //  private static TreeSet TITLES_SET = null;
+  private static final String WIKIPEDIA_TEMPLATE_CTX = "net.sourceforge.phpeclipse.wiki.editor.templates";
 
   private static final class ProposalComparator implements Comparator {
     public int compare(Object o1, Object o2) {
@@ -60,6 +74,32 @@ public class WikiCompletionProcessor implements IContentAssistProcessor {
     fEditor = editor;
   }
 
+  //  private static void readFile(String filename) {
+  //    TITLES_SET = new TreeSet(new StringComparator());
+  //    FileReader fileReader;
+  //    try {
+  //      fileReader = new FileReader(filename);
+  //      BufferedReader bufferedReader = new BufferedReader(fileReader);
+  //      LineTokenizer lineTokenizer = new LineTokenizer();
+  //      StringBuffer line = new StringBuffer(1024);
+  //      while (lineTokenizer.getToken(line, bufferedReader)) {
+  //        if (line.length() == 0) {
+  //          // this should not happen
+  //        } else {
+  //          TITLES_SET.add(line.toString());
+  //          line.delete(0, line.length());
+  //        }
+  //      }
+  //      bufferedReader.close();
+  //    } catch (FileNotFoundException e) {
+  //      // ignore this
+  //      // TODO DialogBox which asks the user if she/he likes to build new index?
+  //    } catch (IOException e) {
+  //      // TODO Auto-generated catch block
+  //      e.printStackTrace();
+  //    }
+  //  }
+
   public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
     WikipediaSection section = fEditor.getSection();
     if (section == null)
@@ -72,69 +112,79 @@ public class WikiCompletionProcessor implements IContentAssistProcessor {
 
     String prefix = getPrefix(viewer, offset);
     Region region = new Region(offset - prefix.length(), prefix.length() + selection.getLength());
+    List result = new ArrayList();
+
+    if (prefix.length() > 2) {
+      if (WikiEditorPlugin.fWikiDB == null) {
+        try {
+          WikiEditorPlugin.fWikiDB = new WikipediaDB();
+        } catch (Exception ex1) {
+          //        ex1.printStackTrace(); // could not start db
+          WikiEditorPlugin.fWikiDB = null;
+        }
+      }
+      if (WikiEditorPlugin.fWikiDB != null) {
+        try {
+          ArrayList list = WikiEditorPlugin.fWikiDB.queryPrefix(prefix);
+          ICompletionProposal[] titleProposals = computeTitleProposals(list, region, viewer);
+          result.addAll(Arrays.asList(titleProposals));
+        } catch (Exception ex1) {
+          //    ex1.printStackTrace(); // could not start db
+          WikiEditorPlugin.fWikiDB = null;
+        }
+      }
+    }
+    //    if (TITLES_SET==null) {
+    // TODO make this a preference
+    //      readFile("c:\\temp\\titles.txt");
+    //    }
+    //    if (TITLES_SET.size()>0 && prefix.length()>3) {
+    //      SortedSet subSet = TITLES_SET.subSet(prefix, prefix + '\255');
+    //      Iterator iter = subSet.iterator();
+    //      ArrayList list;
+    //      String title;
+    //      int maxProposals = 200;
+    //      while (iter.hasNext()) {
+    //        title = (String) iter.next();
+    //        if (title.toLowerCase().startsWith(prefix.toLowerCase())) {
+    //          result.add(title);
+    //        }
+    //        if (maxProposals-- < 0) {
+    //          break;
+    //        }
+    //      }
+    //    }
 
     ICompletionProposal[] templateProposals = computeTemplateProposals(viewer, region, section, prefix);
-//    ICompletionProposal[] ingredientProposals = computeIngredientsProposals(viewer, region, recipe, prefix);
-//    ICompletionProposal[] titleProposals = computeTitleProposals(viewer, region, recipe, prefix);
-    List result = new ArrayList();
-//    result.addAll(Arrays.asList(ingredientProposals));
+    //    ICompletionProposal[] ingredientProposals = computeIngredientsProposals(viewer, region, recipe, prefix);
+    //    ICompletionProposal[] titleProposals = computeTitleProposals(viewer, region, recipe, prefix);
+
+    //    result.addAll(Arrays.asList(ingredientProposals));
     result.addAll(Arrays.asList(templateProposals));
-//    result.addAll(Arrays.asList(titleProposals));
+    //    result.addAll(Arrays.asList(titleProposals));
 
     return (ICompletionProposal[]) result.toArray(new ICompletionProposal[result.size()]);
   }
 
-  private ICompletionProposal[] computeTitleProposals(ITextViewer viewer, IRegion region, WikipediaText recipe, String prefix) {
-    List props = new ArrayList(2);
-    //         if (recipe.getIngredientsSection() == null)
-    //                 props.add(createTitleProposal("Zutaten:", region, viewer));
-    //         if (recipe.getPreparationSection() == null)
-    //                 props.add(createTitleProposal("Zubereitung:", region, viewer));
-    return (ICompletionProposal[]) props.toArray(new ICompletionProposal[props.size()]);
-  }
-
-  private CompletionProposal createTitleProposal(String name, IRegion region, ITextViewer viewer) {
-    String lineDelimiter = TextUtilities.getDefaultLineDelimiter(viewer.getDocument());
-    return new CompletionProposal(name + lineDelimiter, region.getOffset(), region.getLength(), region.getOffset() + name.length()
-        + lineDelimiter.length(), null, name, null, null);
-  }
-
-  private ICompletionProposal[] computeIngredientsProposals(ITextViewer viewer, IRegion region, WikipediaText recipe, String prefix) {
-    return new ICompletionProposal[0];
-    //         Step[] steps= recipe.getSteps();
-    //         if (steps == null || steps.length == 0)
-    //                 return new ICompletionProposal[0];
-    //         
-    //         int offset= region.getOffset();
-    //         Step first= steps[0];
-    //         if (offset < first.getOffset())
-    //                 return new ICompletionProposal[0];
-    //         
-    //         Ingredient[] ingredients= recipe.getIngredients();
-    //         if (ingredients == null)
-    //                 return new ICompletionProposal[0];
-    //         
-    //         prefix= prefix.toLowerCase();
-    //         
-    //         ArrayList proposals= new ArrayList();
-    //         for (int i= 0; i < ingredients.length; i++) {
-    //                 String ingredient= ingredients[i].getName();
-    //                 if (ingredient.toLowerCase().startsWith(prefix))
-    //                         proposals.add(new CompletionProposal(ingredient, offset, region.getLength(), ingredient.length()));
-    //         }
-    //         return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]);
+  private ICompletionProposal[] computeTitleProposals(ArrayList list, IRegion region, ITextViewer viewer) {
+    ICompletionProposal[] arr = new ICompletionProposal[list.size()];
+    //    String lineDelimiter = TextUtilities.getDefaultLineDelimiter(viewer.getDocument());
+    String temp;
+    for (int i = 0; i < arr.length; i++) {
+      temp = (String) list.get(i);
+      arr[i] = new CompletionProposal(temp, region.getOffset(), region.getLength(), region.getOffset() + temp.length(), null, temp,
+          null, null);
+    }
+    return arr;
   }
 
   private TemplateContextType getContextType(WikipediaSection section, int offset) {
-//    if (recipe.getPreparationSection() != null && recipe.getPreparationSection().getOffset() < offset)
-//      return WikiEditorPlugin.getDefault().getContextTypeRegistry().getContextType(PREPARATION_TEMPLATE_CTX);
-//    else
-      return WikiEditorPlugin.getDefault().getContextTypeRegistry().getContextType(INGREDIENTS_TEMPLATE_CTX);
+    return WikiEditorPlugin.getDefault().getContextTypeRegistry().getContextType(WIKIPEDIA_TEMPLATE_CTX);
   }
 
   /**
-   * Creates a concrete template context for the given region in the document. This involves finding out which context fType is valid
-   * at the given location, and then creating a context of this fType. The default implementation returns a
+   * Creates a concrete template context for the given region in the document. This involves finding out which context fType is
+   * valid at the given location, and then creating a context of this fType. The default implementation returns a
    * <code>DocumentTemplateContext</code> for the context fType at the given location.
    * 
    * @param viewer
@@ -171,10 +221,10 @@ public class WikiCompletionProcessor implements IContentAssistProcessor {
       } catch (TemplateException e) {
         continue;
       }
-      int relevance = getRelevance(template, prefix);
-      if (relevance > 0) {
-        matches.add(new TemplateProposal(template, context, region, getImage(template), relevance));
-      }
+      //      int relevance = getRelevance(template, prefix);
+      //      if (relevance > 0) {
+      matches.add(new TemplateProposal(template, context, region, getImage(template), 1));
+      //      }
     }
 
     Collections.sort(matches, fgProposalComparator);
@@ -208,7 +258,7 @@ public class WikiCompletionProcessor implements IContentAssistProcessor {
     try {
       while (i > 0) {
         char ch = document.getChar(i - 1);
-        if (!Character.isLetterOrDigit(ch))
+        if (!Character.isLetterOrDigit(ch) && (ch != ':') && (ch != '_'))
           break;
         i--;
       }