1 11 package org.eclipse.jface.contentassist; 12 13 import java.util.HashMap ; 14 15 import org.eclipse.swt.SWT; 16 import org.eclipse.swt.events.ModifyEvent; 17 import org.eclipse.swt.events.ModifyListener; 18 import org.eclipse.swt.events.SelectionEvent; 19 import org.eclipse.swt.events.SelectionListener; 20 import org.eclipse.swt.graphics.Point; 21 import org.eclipse.swt.widgets.Control; 22 import org.eclipse.swt.widgets.Event; 23 import org.eclipse.swt.widgets.Listener; 24 import org.eclipse.swt.widgets.Text; 25 26 import org.eclipse.core.runtime.Assert; 27 28 import org.eclipse.jface.text.BadLocationException; 29 import org.eclipse.jface.text.Document; 30 import org.eclipse.jface.text.IDocument; 31 32 33 41 public class TextContentAssistSubjectAdapter extends AbstractControlContentAssistSubjectAdapter { 42 43 46 private class InternalDocument extends Document { 47 48 51 private ModifyListener fModifyListener; 52 53 private InternalDocument() { 54 super(fText.getText()); 55 fModifyListener= new ModifyListener() { 56 59 public void modifyText(ModifyEvent e) { 60 set(fText.getText()); 61 } 62 }; 63 fText.addModifyListener(fModifyListener); 64 } 65 66 69 public void replace(int pos, int length, String text) throws BadLocationException { 70 super.replace(pos, length, text); 71 fText.removeModifyListener(fModifyListener); 72 fText.setText(get()); 73 fText.addModifyListener(fModifyListener); 74 } 75 } 76 77 78 private Text fText; 79 80 private HashMap fModifyListeners= new HashMap (); 81 82 87 public TextContentAssistSubjectAdapter(Text text) { 88 Assert.isNotNull(text); 89 fText= text; 90 } 91 92 95 public Control getControl() { 96 return fText; 97 } 98 99 102 public int getLineHeight() { 103 return fText.getLineHeight(); 104 } 105 106 109 public int getCaretOffset() { 110 return fText.getCaretPosition(); 111 } 112 113 116 public Point getLocationAtOffset(int offset) { 117 Point caretLocation= fText.getCaretLocation(); 118 121 caretLocation.y += 2; 122 return caretLocation; 123 } 124 125 128 public Point getWidgetSelectionRange() { 129 return new Point(fText.getSelection().x, Math.abs(fText.getSelection().y - fText.getSelection().x)); 130 } 131 132 135 public Point getSelectedRange() { 136 return new Point(fText.getSelection().x, Math.abs(fText.getSelection().y - fText.getSelection().x)); 137 } 138 139 142 public IDocument getDocument() { 143 IDocument document= (IDocument)fText.getData("document"); if (document == null) { 145 document= new InternalDocument() ; 146 fText.setData("document", document); } 148 return document; 149 } 150 151 154 public void setSelectedRange(int i, int j) { 155 fText.setSelection(new Point(i, i+j)); 156 } 157 158 161 public void revealRange(int i, int j) { 162 fText.setSelection(new Point(i, i+j)); 164 } 165 166 169 public boolean addSelectionListener(final SelectionListener selectionListener) { 170 fText.addSelectionListener(selectionListener); 171 Listener listener= new Listener() { 172 175 public void handleEvent(Event e) { 176 selectionListener.widgetSelected(new SelectionEvent(e)); 177 178 } 179 }; 180 fText.addListener(SWT.Modify, listener); 181 fModifyListeners.put(selectionListener, listener); 182 return true; 183 } 184 185 188 public void removeSelectionListener(SelectionListener selectionListener) { 189 fText.removeSelectionListener(selectionListener); 190 Object listener= fModifyListeners.get(selectionListener); 191 if (listener instanceof Listener) 192 fText.removeListener(SWT.Modify, (Listener)listener); 193 } 194 } 195 | Popular Tags |