KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > filtereditor > StyledTextContentAssistSubjectAdapter


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.browser.common.filtereditor;
22
23
24 import java.util.HashMap JavaDoc;
25
26 import org.eclipse.jface.contentassist.AbstractControlContentAssistSubjectAdapter;
27 import org.eclipse.jface.text.Assert;
28 import org.eclipse.jface.text.IDocument;
29 import org.eclipse.jface.text.ITextViewer;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.custom.StyledText;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.events.SelectionListener;
34 import org.eclipse.swt.graphics.Point;
35 import org.eclipse.swt.widgets.Control;
36 import org.eclipse.swt.widgets.Event;
37 import org.eclipse.swt.widgets.Listener;
38
39
40 public class StyledTextContentAssistSubjectAdapter extends AbstractControlContentAssistSubjectAdapter
41 {
42
43     private StyledText styledText;
44
45     private ITextViewer viewer;
46
47     private HashMap JavaDoc modifyListeners;
48
49
50     public StyledTextContentAssistSubjectAdapter( ITextViewer viewer )
51     {
52         Assert.isNotNull( viewer );
53         this.styledText = viewer.getTextWidget();
54         this.viewer = viewer;
55         this.modifyListeners = new HashMap JavaDoc();
56     }
57
58
59     public Control getControl()
60     {
61         return styledText;
62     }
63
64
65     public int getLineHeight()
66     {
67         return styledText.getLineHeight();
68     }
69
70
71     public int getCaretOffset()
72     {
73         return styledText.getCaretOffset();
74     }
75
76
77     public Point getLocationAtOffset( int offset )
78     {
79         return styledText.getLocationAtOffset( offset );
80     }
81
82
83     public Point getWidgetSelectionRange()
84     {
85         return new Point( styledText.getSelection().x, Math.abs( styledText.getSelection().y
86             - styledText.getSelection().x ) );
87     }
88
89
90     public Point getSelectedRange()
91     {
92         return new Point( styledText.getSelection().x, Math.abs( styledText.getSelection().y
93             - styledText.getSelection().x ) );
94     }
95
96
97     public void setSelectedRange( int i, int j )
98     {
99         styledText.setSelection( new Point( i, i + j ) );
100     }
101
102
103     public void revealRange( int i, int j )
104     {
105         styledText.setSelection( new Point( i, i + j ) );
106     }
107
108
109     public IDocument getDocument()
110     {
111         return viewer.getDocument();
112     }
113
114
115     public boolean addSelectionListener( final SelectionListener selectionListener )
116     {
117         styledText.addSelectionListener( selectionListener );
118         Listener listener = new Listener()
119         {
120             public void handleEvent( Event e )
121             {
122                 selectionListener.widgetSelected( new SelectionEvent( e ) );
123             }
124         };
125         styledText.addListener( SWT.Modify, listener );
126         modifyListeners.put( selectionListener, listener );
127         return true;
128     }
129
130
131     public void removeSelectionListener( SelectionListener selectionListener )
132     {
133         styledText.removeSelectionListener( selectionListener );
134         Object JavaDoc listener = modifyListeners.get( selectionListener );
135         if ( listener instanceof Listener )
136         {
137             styledText.removeListener( SWT.Modify, ( Listener ) listener );
138         }
139     }
140
141 }
142
Popular Tags