SWT Completion Editor : Editor « SWT JFace Eclipse « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » SWT JFace Eclipse » EditorScreenshots 
SWT Completion Editor

/*
SWT/JFace in Action
GUI Design with Eclipse 3.0
Matthew Scarpino, Stephen Holder, Stanford Ng, and Laurent Mihalkovic

ISBN: 1932394273

Publisher: Manning
*/


import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextListener;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.TextEvent;
import org.eclipse.jface.text.TextViewer;
import org.eclipse.jface.text.contentassist.CompletionProposal;
import org.eclipse.jface.text.contentassist.ContentAssistant;
import org.eclipse.jface.text.contentassist.ContextInformationValidator;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.contentassist.IContextInformationValidator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;

public class Ch5CompletionEditor extends Composite {
  private TextViewer textViewer;

  private WordTracker wordTracker;

  private static final int MAX_QUEUE_SIZE = 200;

  public Ch5CompletionEditor(Composite parent) {
    super(parent, SWT.NULL);
    wordTracker = new WordTracker(MAX_QUEUE_SIZE);
    buildControls();
  }

  private void buildControls() {
    setLayout(new FillLayout());
    textViewer = new TextViewer(this, SWT.MULTI | SWT.V_SCROLL);

    textViewer.setDocument(new Document());

    final ContentAssistant assistant = new ContentAssistant();
    assistant.setContentAssistProcessor(
        new RecentWordContentAssistProcessor(wordTracker),
        IDocument.DEFAULT_CONTENT_TYPE);

    assistant.install(textViewer);

    textViewer.getControl().addKeyListener(new KeyAdapter() {
      public void keyPressed(KeyEvent e) {
        switch (e.keyCode) {
        case SWT.F1:
          assistant.showPossibleCompletions();
          break;
        default:
        //ignore everything else
        }
      }
    });

    textViewer.addTextListener(new ITextListener() {
      public void textChanged(TextEvent e) {
        if (isWhitespaceString(e.getText())) {
          wordTracker.add(findMostRecentWord(e.getOffset() 1));
        }
      }
    });
  }

  protected String findMostRecentWord(int startSearchOffset) {
    int currOffset = startSearchOffset;
    char currChar;
    String word = "";
    try {
      while (currOffset > 0
          && !Character.isWhitespace(currChar = textViewer
              .getDocument().getChar(currOffset))) {
        word = currChar + word;
        currOffset--;
      }
      return word;
    catch (BadLocationException e) {
      e.printStackTrace();
      return null;
    }
  }

  protected boolean isWhitespaceString(String string) {
    StringTokenizer tokenizer = new StringTokenizer(string);
    //if there is at least 1 token, this string is not whitespace
    return !tokenizer.hasMoreTokens();
  }

}

class WordTracker {
  private int maxQueueSize;

  private List wordBuffer;

  private Map knownWords = new HashMap();

  public WordTracker(int queueSize) {
    maxQueueSize = queueSize;
    wordBuffer = new LinkedList();
  }

  public int getWordCount() {
    return wordBuffer.size();
  }

  public void add(String word) {
    if (wordIsNotKnown(word)) {
      flushOldestWord();
      insertNewWord(word);
    }
  }

  private void insertNewWord(String word) {
    wordBuffer.add(0, word);
    knownWords.put(word, word);
  }

  private void flushOldestWord() {
    if (wordBuffer.size() == maxQueueSize) {
      String removedWord = (StringwordBuffer.remove(maxQueueSize - 1);
      knownWords.remove(removedWord);
    }
  }

  private boolean wordIsNotKnown(String word) {
    return knownWords.get(word== null;
  }

  public List suggest(String word) {
    List suggestions = new LinkedList();
    for (Iterator i = wordBuffer.iterator(); i.hasNext();) {
      String currWord = (Stringi.next();
      if (currWord.startsWith(word)) {
        suggestions.add(currWord);
      }
    }
    return suggestions;
  }

}

class RecentWordContentAssistProcessor implements IContentAssistProcessor {
  private String lastError = null;

  private IContextInformationValidator contextInfoValidator;

  private WordTracker wordTracker;

  public RecentWordContentAssistProcessor(WordTracker tracker) {
    super();
    contextInfoValidator = new ContextInformationValidator(this);
    wordTracker = tracker;
  }

  public ICompletionProposal[] computeCompletionProposals(
      ITextViewer textViewer, int documentOffset) {
    IDocument document = textViewer.getDocument();
    int currOffset = documentOffset - 1;

    try {
      String currWord = "";
      char currChar;
      while (currOffset > 0
          && !Character.isWhitespace(currChar = document
              .getChar(currOffset))) {
        currWord = currChar + currWord;
        currOffset--;
      }

      List suggestions = wordTracker.suggest(currWord);
      ICompletionProposal[] proposals = null;
      if (suggestions.size() 0) {
        proposals = buildProposals(suggestions, currWord,
            documentOffset - currWord.length());
        lastError = null;
      }
      return proposals;
    catch (BadLocationException e) {
      e.printStackTrace();
      lastError = e.getMessage();
      return null;
    }
  }

  private ICompletionProposal[] buildProposals(List suggestions,
      String replacedWord, int offset) {
    ICompletionProposal[] proposals = new ICompletionProposal[suggestions
        .size()];
    int index = 0;
    for (Iterator i = suggestions.iterator(); i.hasNext();) {
      String currSuggestion = (Stringi.next();
      proposals[indexnew CompletionProposal(currSuggestion, offset,
          replacedWord.length(), currSuggestion.length());
      index++;
    }
    return proposals;
  }

  public IContextInformation[] computeContextInformation(
      ITextViewer textViewer, int documentOffset) {
    lastError = "No Context Information available";
    return null;
  }

  public char[] getCompletionProposalAutoActivationCharacters() {
    //we always wait for the user to explicitly trigger completion
    return null;
  }

  public char[] getContextInformationAutoActivationCharacters() {
    //we have no context information
    return null;
  }

  public String getErrorMessage() {
    return lastError;
  }

  public IContextInformationValidator getContextInformationValidator() {
    return contextInfoValidator;
  }
}

           
       
Related examples in the same category
1. Java Source code ViewerJava Source code Viewer
2. SWT Text Editor DemoSWT Text Editor Demo
3. Basic Editor 2Basic Editor 2
4. Basic EditorBasic Editor
5. Basic Editor 3Basic Editor 3
6. Script MultiTextEdit
7. SWT Editor
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.