KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > fieldassist > ComboContentAdapter


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
20  * An {@link IControlContentAdapter} for SWT Combo controls. This is a
21  * convenience class for easily creating a {@link ContentProposalAdapter} for
22  * combo fields.
23  *
24  * @since 3.2
25  */

26 public class ComboContentAdapter implements IControlContentAdapter {
27
28     /*
29      * (non-Javadoc)
30      *
31      * @see org.eclipse.jface.dialogs.taskassistance.IControlContentAdapter#getControlContents(org.eclipse.swt.widgets.Control)
32      */

33     public String JavaDoc getControlContents(Control control) {
34         return ((Combo) control).getText();
35     }
36
37     /*
38      * (non-Javadoc)
39      *
40      * @see org.eclipse.jface.fieldassist.IControlContentAdapter#setControlContents(org.eclipse.swt.widgets.Control,
41      * java.lang.String, int)
42      */

43     public void setControlContents(Control control, String JavaDoc text,
44             int cursorPosition) {
45         ((Combo) control).setText(text);
46         ((Combo) control)
47                 .setSelection(new Point(cursorPosition, cursorPosition));
48     }
49
50     /*
51      * (non-Javadoc)
52      *
53      * @see org.eclipse.jface.fieldassist.IControlContentAdapter#insertControlContents(org.eclipse.swt.widgets.Control,
54      * java.lang.String, int)
55      */

56     public void insertControlContents(Control control, String JavaDoc text,
57             int cursorPosition) {
58         Combo combo = (Combo) control;
59         String JavaDoc contents = combo.getText();
60         Point selection = combo.getSelection();
61         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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     /*
74      * (non-Javadoc)
75      *
76      * @see org.eclipse.jface.fieldassist.IControlContentAdapter#getCursorPosition(org.eclipse.swt.widgets.Control)
77      */

78     public int getCursorPosition(Control control) {
79         return ((Combo) control).getSelection().x;
80     }
81
82     /*
83      * (non-Javadoc)
84      *
85      * @see org.eclipse.jface.fieldassist.IControlContentAdapter#getInsertionBounds(org.eclipse.swt.widgets.Control)
86      */

87     public Rectangle getInsertionBounds(Control control) {
88         Combo combo = (Combo) control;
89         int position = combo.getSelection().y;
90         String JavaDoc 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     /*
101      * (non-Javadoc)
102      *
103      * @see org.eclipse.jface.fieldassist.IControlContentAdapter#setCursorPosition(org.eclipse.swt.widgets.Control,
104      * int)
105      */

106     public void setCursorPosition(Control control, int index) {
107         ((Combo) control).setSelection(new Point(index, index));
108     }
109
110 }
111
Popular Tags