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.GC; 21 import org.eclipse.swt.graphics.Point; 22 import org.eclipse.swt.widgets.Combo; 23 import org.eclipse.swt.widgets.Control; 24 import org.eclipse.swt.widgets.Event; 25 import org.eclipse.swt.widgets.Listener; 26 27 import org.eclipse.core.runtime.Assert; 28 29 import org.eclipse.jface.text.BadLocationException; 30 import org.eclipse.jface.text.Document; 31 import org.eclipse.jface.text.IDocument; 32 33 34 35 52 public class ComboContentAssistSubjectAdapter extends AbstractControlContentAssistSubjectAdapter { 53 54 57 private class InternalDocument extends Document { 58 61 private ModifyListener fModifyListener; 62 63 private InternalDocument() { 64 super(fCombo.getText()); 65 fModifyListener= new ModifyListener() { 66 69 public void modifyText(ModifyEvent e) { 70 set(fCombo.getText()); 71 } 72 }; 73 fCombo.addModifyListener(fModifyListener); 74 } 75 76 79 public void replace(int pos, int length, String text) throws BadLocationException { 80 super.replace(pos, length, text); 81 fCombo.removeModifyListener(fModifyListener); 82 fCombo.setText(get()); 83 fCombo.addModifyListener(fModifyListener); 84 } 85 } 86 87 90 private Combo fCombo; 91 private HashMap fModifyListeners; 92 93 98 public ComboContentAssistSubjectAdapter(Combo combo) { 99 Assert.isNotNull(combo); 100 fCombo= combo; 101 fModifyListeners= new HashMap (); 102 } 103 104 107 public Control getControl() { 108 return fCombo; 109 } 110 111 114 public int getLineHeight() { 115 return fCombo.getTextHeight(); 116 } 117 118 121 public int getCaretOffset() { 122 return fCombo.getSelection().y; 123 } 124 125 128 public Point getLocationAtOffset(int offset) { 129 String comboString= fCombo.getText(); 130 GC gc = new GC(fCombo); 131 gc.setFont(fCombo.getFont()); 132 Point extent= gc.textExtent(comboString.substring(0, Math.min(offset, comboString.length()))); 133 int spaceWidth= gc.textExtent(" ").x; gc.dispose(); 135 138 int x= 2 * spaceWidth + fCombo.getClientArea().x + fCombo.getBorderWidth() + extent.x; 139 return new Point(x, fCombo.getClientArea().y); 140 } 141 142 145 public Point getWidgetSelectionRange() { 146 return new Point(fCombo.getSelection().x, Math.abs(fCombo.getSelection().y - fCombo.getSelection().x)); 147 } 148 149 152 public Point getSelectedRange() { 153 return new Point(fCombo.getSelection().x, Math.abs(fCombo.getSelection().y - fCombo.getSelection().x)); 154 } 155 156 159 public IDocument getDocument() { 160 IDocument document= (IDocument)fCombo.getData("document"); if (document == null) { 162 document= new InternalDocument() ; 163 fCombo.setData("document", document); } 165 return document; 166 } 167 168 171 public void setSelectedRange(int i, int j) { 172 fCombo.setSelection(new Point(i, i+j)); 173 } 174 175 178 public void revealRange(int i, int j) { 179 fCombo.setSelection(new Point(i, i+j)); 181 } 182 183 186 public boolean addSelectionListener(final SelectionListener selectionListener) { 187 fCombo.addSelectionListener(selectionListener); 188 Listener listener= new Listener() { 189 192 public void handleEvent(Event e) { 193 selectionListener.widgetSelected(new SelectionEvent(e)); 194 195 } 196 }; 197 fCombo.addListener(SWT.Modify, listener); 198 fModifyListeners.put(selectionListener, listener); 199 return true; 200 } 201 202 205 public void removeSelectionListener(SelectionListener selectionListener) { 206 fCombo.removeSelectionListener(selectionListener); 207 Object listener= fModifyListeners.get(selectionListener); 208 if (listener instanceof Listener) 209 fCombo.removeListener(SWT.Modify, (Listener)listener); 210 } 211 } 212 | Popular Tags |