1 11 package org.eclipse.jface.fieldassist; 12 13 import org.eclipse.swt.graphics.GC; 14 import org.eclipse.swt.graphics.Point; 15 import org.eclipse.swt.graphics.Rectangle; 16 import org.eclipse.swt.widgets.Combo; 17 import org.eclipse.swt.widgets.Control; 18 19 26 public class ComboContentAdapter implements IControlContentAdapter { 27 28 33 public String getControlContents(Control control) { 34 return ((Combo) control).getText(); 35 } 36 37 43 public void setControlContents(Control control, String text, 44 int cursorPosition) { 45 ((Combo) control).setText(text); 46 ((Combo) control) 47 .setSelection(new Point(cursorPosition, cursorPosition)); 48 } 49 50 56 public void insertControlContents(Control control, String text, 57 int cursorPosition) { 58 Combo combo = (Combo) control; 59 String contents = combo.getText(); 60 Point selection = combo.getSelection(); 61 StringBuffer sb = new StringBuffer (); 62 sb.append(contents.substring(0, selection.x)); 63 sb.append(text); 64 if (selection.y < contents.length()) { 65 sb.append(contents.substring(selection.y, contents.length())); 66 } 67 combo.setText(sb.toString()); 68 selection.x = selection.x + cursorPosition; 69 selection.y = selection.x; 70 combo.setSelection(selection); 71 } 72 73 78 public int getCursorPosition(Control control) { 79 return ((Combo) control).getSelection().x; 80 } 81 82 87 public Rectangle getInsertionBounds(Control control) { 88 Combo combo = (Combo) control; 89 int position = combo.getSelection().y; 90 String contents = combo.getText(); 91 GC gc = new GC(combo); 92 gc.setFont(combo.getFont()); 93 Point extent = gc.textExtent(contents.substring(0, Math.min(position, 94 contents.length()))); 95 gc.dispose(); 96 return new Rectangle(combo.getClientArea().x + extent.x, combo 97 .getClientArea().y, 1, combo.getClientArea().height); 98 } 99 100 106 public void setCursorPosition(Control control, int index) { 107 ((Combo) control).setSelection(new Point(index, index)); 108 } 109 110 } 111 | Popular Tags |