KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > text > link > contentassist > AdditionalInfoController2


1 /*******************************************************************************
2  * Copyright (c) 2000, 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
12 package org.eclipse.jface.internal.text.link.contentassist;
13
14
15 import org.eclipse.swt.events.SelectionEvent;
16 import org.eclipse.swt.events.SelectionListener;
17 import org.eclipse.swt.graphics.Point;
18 import org.eclipse.swt.graphics.Rectangle;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.Table;
21 import org.eclipse.swt.widgets.TableItem;
22
23 import org.eclipse.core.runtime.Assert;
24
25 import org.eclipse.jface.text.AbstractInformationControlManager;
26 import org.eclipse.jface.text.IInformationControl;
27 import org.eclipse.jface.text.IInformationControlCreator;
28 import org.eclipse.jface.text.contentassist.ICompletionProposal;
29 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension3;
30
31
32
33 /**
34  * Displays the additional information available for a completion proposal.
35  *
36  * @since 2.0
37  */

38 class AdditionalInfoController2 extends AbstractInformationControlManager implements Runnable JavaDoc {
39
40     /**
41      * Internal table selection listener.
42      */

43     private class TableSelectionListener implements SelectionListener {
44
45         /*
46          * @see SelectionListener#widgetSelected(SelectionEvent)
47          */

48         public void widgetSelected(SelectionEvent e) {
49             handleTableSelectionChanged();
50         }
51
52         /*
53          * @see SelectionListener#widgetDefaultSelected(SelectionEvent)
54          */

55         public void widgetDefaultSelected(SelectionEvent e) {
56         }
57     }
58
59
60     /** The proposal table */
61     private Table fProposalTable;
62     /** The thread controlling the delayed display of the additional info */
63     private Thread JavaDoc fThread;
64     /** Indicates whether the display delay has been reset */
65     private boolean fIsReset= false;
66     /** Object to synchronize display thread and table selection changes */
67     private final Object JavaDoc fMutex= new Object JavaDoc();
68     /** Thread access lock. */
69     private final Object JavaDoc fThreadAccess= new Object JavaDoc();
70     /** Object to synchronize initial display of additional info */
71     private Object JavaDoc fStartSignal;
72     /** The table selection listener */
73     private SelectionListener fSelectionListener= new TableSelectionListener();
74     /** The delay after which additional information is displayed */
75     private int fDelay;
76
77
78     /**
79      * Creates a new additional information controller.
80      *
81      * @param creator the information control creator to be used by this controller
82      * @param delay time in milliseconds after which additional info should be displayed
83      */

84     AdditionalInfoController2(IInformationControlCreator creator, int delay) {
85         super(creator);
86         fDelay= delay;
87         setAnchor(ANCHOR_RIGHT);
88         setFallbackAnchors(new Anchor[] {ANCHOR_RIGHT, ANCHOR_LEFT, ANCHOR_BOTTOM });
89     }
90
91     /*
92      * @see AbstractInformationControlManager#install(Control)
93      */

94     public void install(Control control) {
95
96         if (fProposalTable == control) {
97             // already installed
98
return;
99         }
100
101         super.install(control);
102
103         Assert.isTrue(control instanceof Table);
104         fProposalTable= (Table) control;
105         fProposalTable.addSelectionListener(fSelectionListener);
106         synchronized (fThreadAccess) {
107             if (fThread != null)
108                 fThread.interrupt();
109             fThread= new Thread JavaDoc(this, ContentAssistMessages.getString("InfoPopup.info_delay_timer_name")); //$NON-NLS-1$
110

111             fStartSignal= new Object JavaDoc();
112             synchronized (fStartSignal) {
113                 fThread.start();
114                 try {
115                     // wait until thread is ready
116
fStartSignal.wait();
117                 } catch (InterruptedException JavaDoc x) {
118                 }
119             }
120         }
121     }
122
123     /*
124      * @see AbstractInformationControlManager#disposeInformationControl()
125      */

126      public void disposeInformationControl() {
127
128         synchronized (fThreadAccess) {
129             if (fThread != null) {
130                 fThread.interrupt();
131                 fThread= null;
132             }
133         }
134
135         if (fProposalTable != null && !fProposalTable.isDisposed()) {
136             fProposalTable.removeSelectionListener(fSelectionListener);
137             fProposalTable= null;
138         }
139
140         super.disposeInformationControl();
141     }
142
143     /*
144      * @see java.lang.Runnable#run()
145      */

146     public void run() {
147         try {
148             while (true) {
149
150                 synchronized (fMutex) {
151
152                     if (fStartSignal != null) {
153                         synchronized (fStartSignal) {
154                             fStartSignal.notifyAll();
155                             fStartSignal= null;
156                         }
157                     }
158
159                     // Wait for a selection event to occur.
160
fMutex.wait();
161
162                     while (true) {
163                         fIsReset= false;
164                         // Delay before showing the popup.
165
fMutex.wait(fDelay);
166                         if (!fIsReset)
167                             break;
168                     }
169                 }
170
171                 if (fProposalTable != null && !fProposalTable.isDisposed()) {
172                     fProposalTable.getDisplay().asyncExec(new Runnable JavaDoc() {
173                         public void run() {
174                             if (!fIsReset)
175                                 showInformation();
176                         }
177                     });
178                 }
179
180             }
181         } catch (InterruptedException JavaDoc e) {
182         }
183
184         synchronized (fThreadAccess) {
185             // only null fThread if it is us!
186
if (Thread.currentThread() == fThread)
187                 fThread= null;
188         }
189     }
190
191     /**
192      *Handles a change of the line selected in the associated selector.
193      */

194     public void handleTableSelectionChanged() {
195
196         if (fProposalTable != null && !fProposalTable.isDisposed() && fProposalTable.isVisible()) {
197             synchronized (fMutex) {
198                 fIsReset= true;
199                 fMutex.notifyAll();
200             }
201         }
202     }
203
204     /*
205      * @see AbstractInformationControlManager#computeInformation()
206      */

207     protected void computeInformation() {
208
209         if (fProposalTable == null || fProposalTable.isDisposed())
210             return;
211
212         TableItem[] selection= fProposalTable.getSelection();
213         if (selection != null && selection.length > 0) {
214
215             TableItem item= selection[0];
216
217             // compute information
218
String JavaDoc information= null;
219             Object JavaDoc d= item.getData();
220
221             if (d instanceof ICompletionProposal) {
222                 ICompletionProposal p= (ICompletionProposal) d;
223                 information= p.getAdditionalProposalInfo();
224             }
225
226             if (d instanceof ICompletionProposalExtension3)
227                 setCustomInformationControlCreator(((ICompletionProposalExtension3) d).getInformationControlCreator());
228             else
229                 setCustomInformationControlCreator(null);
230
231             // compute subject area
232
setMargins(4, -1);
233             Rectangle area= fProposalTable.getBounds();
234             area.x= 0; // subject area is the whole subject control
235
area.y= 0;
236
237             // set information & subject area
238
setInformation(information, area);
239         }
240     }
241
242     /*
243      * @see org.eclipse.jface.text.AbstractInformationControlManager#computeSizeConstraints(Control, IInformationControl)
244      */

245     protected Point computeSizeConstraints(Control subjectControl, IInformationControl informationControl) {
246         Point sizeConstraint= super.computeSizeConstraints(subjectControl, informationControl);
247         Point size= subjectControl.getSize();
248         if (sizeConstraint.x < size.x)
249             sizeConstraint.x= size.x;
250         if (sizeConstraint.y < size.y)
251             sizeConstraint.y= size.y;
252         return sizeConstraint;
253     }
254 }
255
256
257
Popular Tags