2013-09-25 29 views
1

我正在用java編寫拼寫檢查功能。我遇到了一個麻煩, 當我在Text輸入一些文本。從這我怎麼能得到當前輸入或當前修改的單詞,以便我可以 驗證輸入或修改的單詞是否在我的字典中找到。如何從SWT文本框中獲取當前輸入/修改的單詞

我已經實現了突出顯示在字典中沒有找到的用紅色標記的單詞。但是我通過閱讀每個修改的整個文本來實現這一點。

+0

是的。據我所知,處理類似問題的方法就是:儘可能地去......也許你可以逐步做到這一點,並獲得插入位置,然後取出之前的單詞(以及可能繼續)之後的單詞插入符號。 – ppeterka

回答

2

我設法將一些代碼放在一起,不會迭代整個文本。相反,它從當前光標位置向左和向右移動,以搜索單詞的結尾。當兩端都發現,這個詞是輸出:

public static void main(String[] args) 
{ 
    Display display = Display.getDefault(); 
    final Shell shell = new Shell(display); 
    shell.setText("StackOverflow"); 
    shell.setLayout(new GridLayout(2, false)); 

    Text text = new Text(shell, SWT.BORDER); 
    GridData data = new GridData(SWT.FILL, SWT.BEGINNING, true, true); 
    data.horizontalSpan = 2; 
    text.setLayoutData(data); 
    new Label(shell, SWT.NONE).setText("Current word:"); 
    final Label label = new Label(shell, SWT.NONE); 

    text.addListener(SWT.Verify, new Listener() 
    { 
     @Override 
     public void handleEvent(Event e) 
     { 
      Text source = (Text) e.widget; 

      /* Construct the entered text */ 
      String oldString = source.getText(); 
      String textString = oldString.substring(0, e.start) + e.text + oldString.substring(e.end); 

      /* Get all the chars */ 
      char[] text = textString.toCharArray(); 

      /* Get the cursor position */ 
      int position = source.getCaretPosition(); 

      /* Adjust cursor position based on input (necessary for delete operations) */ 
      if(e.text.equals("")) 
       position--; 
      else 
       position++; 

      /* Remember start and end of current word */ 
      int leftBorder = -1; 
      int rightBorder = -1; 

      /* Search for left end of the current word */ 
      for(int i = 1; i < position; i++) 
      { 
       int left = position - i; 

       if(left > 0) 
       { 
        if(!Character.isLetter(text[left])) 
        { 
         leftBorder = left + 1; 
         break; 
        } 
       } 
      } 

      /* Search for right end of the current word */ 
      for(int i = position; i < text.length; i++) 
      { 
       int right = i; 

       if(right < text.length) 
       { 
        if(!Character.isLetter(text[right])) 
        { 
         rightBorder = right; 
         break; 
        } 
       } 
      } 

      /* If the word is the first/last, set border accordingly */ 
      if(leftBorder == -1) 
       leftBorder = 0; 
      if(rightBorder == -1) 
       rightBorder = text.length; 

      StringBuilder result = new StringBuilder(); 
      /* Output the word */ 
      for(int i = leftBorder; i < rightBorder; i++) 
       result.append(text[i]); 

      label.setText(result.toString()); 
      shell.layout(true, true); 
     } 
    }); 

    shell.pack(); 
    shell.setSize(600, 100); 
    shell.open(); 
    while (!shell.isDisposed()) 
    { 
     if (!display.readAndDispatch()) 
      display.sleep(); 
    } 
    display.dispose(); 
} 

這裏有兩張截圖凸顯當前編輯的話:

enter image description here

enter image description here


因此,如果整個文本長度爲n,當前單詞長度爲m,運行時間爲O(m)而不是O(n)

相關問題