Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.jface.text.TextSelection
java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.jface.text.TextSelection [message #905418] Thu, 30 August 2012 10:55 Go to next message
zhang lei is currently offline zhang leiFriend
Messages: 22
Registered: July 2012
Junior Member
I am learing the Jface in action.in the chapter 5,I get a mistake that annoyed me.I'm gettting the below error, please help me on this.
java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.jface.text.TextSelection
at org.eclipse.jface.text.TextViewer.firePostSelectionChanged(TextViewer.java:2701)
at org.eclipse.jface.text.TextViewer$5.run(TextViewer.java:2682)
at org.eclipse.swt.widgets.Display.runTimer(Unknown Source)
at org.eclipse.swt.widgets.Display.messageProc(Unknown Source)
at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
at org.eclipse.swt.internal.win32.OS.DispatchMessage(Unknown Source)
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
at org.eclipse.jface.window.Window.runEventLoop(Window.java:825)
at org.eclipse.jface.window.Window.open(Window.java:801)
at com.swtjface.Ch2.WidgetWindow.main(WidgetWindow.java:48)

the main class CompViewer

import org.eclipse.jface.window.*;
import org.eclipse.swt.widgets.*;
public class CompViewer extends ApplicationWindow 
{
  public CompViewer() 
  {
    super(null);
  }
  protected Control createContents(Composite parent) 
  {
	  Ch5CompletionEditor cc1 = new Ch5CompletionEditor(parent);
      return parent;
  }
  public static void main(String[] args) 
  {
    CompViewer cv = new CompViewer();
    cv.setBlockOnOpen(true);
    cv.open();
    Display.getCurrent().dispose();
  }
}


import java.util.StringTokenizer;
import org.eclipse.jface.text.*;
import org.eclipse.jface.text.contentassist.ContentAssistant;
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)
    {
      System.out.println("findMostRecentWord()");
      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();
      }
    }


import java.util.*;
public 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 = 
	              (String)wordBuffer.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 = (String)i.next();
	      if( currWord.startsWith(word) )
	      {
	        suggestions.add(currWord);
	      }
	    }
	    return suggestions;
	  }
	}


import java.util.Iterator;
import java.util.List;

import org.eclipse.jface.text.*;
import org.eclipse.jface.text.contentassist.*;

public 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;
    System.out.println("RecentWordContentAssistProcessor()");
  }
  public ICompletionProposal[] computeCompletionProposals(
    ITextViewer textViewer,
    int documentOffset)
  {
	  System.out.println("computeCompletionProposals()");
    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 = (String)i.next();
      proposals[index] = new 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;
		  }
		}
  • Attachment: Ch5.rar
    (Size: 3.13KB, Downloaded 282 times)

[Updated on: Thu, 30 August 2012 10:58]

Report message to a moderator

Re: java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.jface.text.TextSelection [message #905421 is a reply to message #905418] Thu, 30 August 2012 11:03 Go to previous messageGo to next message
zhang lei is currently offline zhang leiFriend
Messages: 22
Registered: July 2012
Junior Member
when the snippet is running,I double click the text viewer,or enter a whitespace,I always get the mistake in the console.I could not understand it very well,could you help me?thanks.
Re: java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.jface.text.TextSelection [message #905475 is a reply to message #905418] Thu, 30 August 2012 12:51 Go to previous messageGo to next message
Oliver Koch is currently offline Oliver KochFriend
Messages: 40
Registered: June 2012
Member
Am 30.08.2012 12:55, schrieb zhang lei:
> I am learing the Jface in action.in the chapter 5,I get a mistake that
> annoyed me.I'm gettting the below error, please help me on this.
> java.lang.NoClassDefFoundError: Could not initialize
> class org.eclipse.jface.text.TextSelection
> at
> org.eclipse.jface.text.TextViewer.firePostSelectionChanged(TextViewer.java:2701)
>
> at org.eclipse.jface.text.TextViewer$5.run(TextViewer.java:2682)
> at org.eclipse.swt.widgets.Display.runTimer(Unknown Source)
> at org.eclipse.swt.widgets.Display.messageProc(Unknown Source)
> at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
> at org.eclipse.swt.internal.win32.OS.DispatchMessage(Unknown Source)
> at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
> at org.eclipse.jface.window.Window.runEventLoop(Window.java:825)
> at org.eclipse.jface.window.Window.open(Window.java:801)
> at com.swtjface.Ch2.WidgetWindow.main(WidgetWindow.java:48)

> the main class CompViewer
>
>
> import org.eclipse.jface.window.*;
> import org.eclipse.swt.widgets.*;
> public class CompViewer extends ApplicationWindow {
> public CompViewer() {
> super(null);
> }
> protected Control createContents(Composite parent) {
> Ch5CompletionEditor cc1 = new Ch5CompletionEditor(parent);
> return parent;
> }
> public static void main(String[] args) {
> CompViewer cv = new CompViewer();
> cv.setBlockOnOpen(true);
> cv.open();
> Display.getCurrent().dispose();
> }
> }
>
>
>
> import java.util.StringTokenizer;
> import org.eclipse.jface.text.*;
> import org.eclipse.jface.text.contentassist.ContentAssistant;
> 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)
> {
> System.out.println("findMostRecentWord()");
> 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();
> }
> }
>
>
>
> import java.util.*;
> public 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 =
> (String)wordBuffer.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 = (String)i.next();
> if( currWord.startsWith(word) )
> {
> suggestions.add(currWord);
> }
> }
> return suggestions;
> }
> }
>
>
>
> import java.util.Iterator;
> import java.util.List;
>
> import org.eclipse.jface.text.*;
> import org.eclipse.jface.text.contentassist.*;
>
> public 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;
> System.out.println("RecentWordContentAssistProcessor()");
> }
> public ICompletionProposal[] computeCompletionProposals(
> ITextViewer textViewer,
> int documentOffset)
> {
> System.out.println("computeCompletionProposals()");
> 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 = (String)i.next();
> proposals[index] = new 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;
> }
> }
>


If this is an eclipse-based application you might want to check if all
bundles can be resolved.

Another way to tackle the problem is to press Ctrl+Shift+T and enter
org.eclipse.jface.text.TextSelection to search for the class in your
target platform. Then you can find the plug-in that you are missing
Re: java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.jface.text.TextSelection [message #905718 is a reply to message #905475] Fri, 31 August 2012 00:14 Go to previous messageGo to next message
zhang lei is currently offline zhang leiFriend
Messages: 22
Registered: July 2012
Junior Member
to dear Oliver Koch:
if I was missing the plug-in,the Eclipse could tell me,and the snippet can not run immediately.
Re: java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.jface.text.TextSelection [message #905887 is a reply to message #905475] Fri, 31 August 2012 09:03 Go to previous message
zhang lei is currently offline zhang leiFriend
Messages: 22
Registered: July 2012
Junior Member
to dear Oliver Koch:
if I was missing the plug-in,the Eclipse could tell me,and the snippet can not run immediately
Previous Topic:Combo unselect item
Next Topic:Making Composite to ScrolledComposite
Goto Forum:
  


Current Time: Thu Apr 25 16:19:15 GMT 2024

Powered by FUDForum. Page generated in 0.03011 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top