KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > expression > PopupInformationControl


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.debug.internal.ui.views.expression;
13
14 import java.text.MessageFormat JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.debug.internal.ui.DebugUIMessages;
19 import org.eclipse.debug.internal.ui.DebugUIPlugin;
20 import org.eclipse.jface.dialogs.IDialogSettings;
21 import org.eclipse.jface.text.IInformationControl;
22 import org.eclipse.jface.text.IInformationControlExtension;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.DisposeListener;
25 import org.eclipse.swt.events.FocusListener;
26 import org.eclipse.swt.graphics.Color;
27 import org.eclipse.swt.graphics.Point;
28 import org.eclipse.swt.graphics.Rectangle;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Display;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.Shell;
36 import org.eclipse.ui.IWorkbench;
37 import org.eclipse.ui.PlatformUI;
38 import org.eclipse.ui.commands.AbstractHandler;
39 import org.eclipse.ui.commands.ExecutionException;
40 import org.eclipse.ui.commands.HandlerSubmission;
41 import org.eclipse.ui.commands.ICommand;
42 import org.eclipse.ui.commands.ICommandManager;
43 import org.eclipse.ui.commands.IHandler;
44 import org.eclipse.ui.commands.IKeySequenceBinding;
45 import org.eclipse.ui.commands.IWorkbenchCommandSupport;
46 import org.eclipse.ui.commands.Priority;
47 import org.eclipse.ui.contexts.IWorkbenchContextSupport;
48
49 /**
50  * An information popup window. The window contains a control provided
51  * by subclasses. A label is displayed at the bottom of the
52  * window describing an action used to dismiss the window, which is invoked
53  * with a key binding.
54  * <p>
55  * Clients are intended to subclass this class.
56  * </p>
57  * @see org.eclipse.debug.ui.actions.IPopupInformationControlAdapter
58  * @see org.eclipse.jface.text.information.IInformationPresenter
59  * @see org.eclipse.jface.text.information.IInformationProvider
60  * @since 3.0
61  */

62 public abstract class PopupInformationControl implements IInformationControl, IInformationControlExtension {
63     
64     private static final String JavaDoc HEIGHT_STRING = "_DEBUGPOPUP_HEIGHT"; //$NON-NLS-1$
65
private static final String JavaDoc WIDTH_STRING = "_DEBUGPOPUP_WIDTH"; //$NON-NLS-1$
66

67     /**
68      * The popup window
69      */

70     protected Shell shell;
71     
72     /**
73      * The maximum width of the popup
74      */

75     private int maxWidth = 300;
76     
77     /**
78      * The maximum height of the popup
79      */

80     private int maxHeight = 300;
81         
82     /**
83      * ActionHandler for closeAction
84      */

85     private HandlerSubmission submission;
86     
87     /**
88      * Handler used to close this popup, or <code>null</code> if none
89      */

90     private IHandler closeHandler = null;
91     
92     /**
93      * Command id that provides a key sequence used to invoke this
94      * popup's close handler, or <code>null</code> if none
95      */

96     private String JavaDoc commandId = null;
97     
98     /**
99      * Popup control
100      */

101     private Control control = null;
102             
103     /**
104      * Creates a popup information control. When the specified command
105      * is invoked, the handler is invoked and the popup is closed.
106      *
107      * @param parent the shell to parent the popup
108      * @param labelText label to display at the bottom of the popup window.
109      * The label will be augmented with the key-sequence asscoaited with the
110      * given commandId.
111      * @param commandId command identifier used to bind a key sequence to
112      * close the popup invoking <code>performCommand()</code>
113      */

114     public PopupInformationControl(Shell parent, String JavaDoc labelText, String JavaDoc commandId) {
115         this.closeHandler = new CloseHandler();
116         this.commandId = commandId;
117         
118         shell= new Shell(parent, SWT.RESIZE);
119         Display display = shell.getDisplay();
120         shell.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
121         shell.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
122         
123         GridLayout layout= new GridLayout(1, false);
124         layout.marginHeight= 0;
125         layout.marginWidth= 0;
126         shell.setLayout(layout);
127         shell.setLayoutData(new GridData(GridData.FILL_BOTH));
128         control = createControl(shell);
129         register();
130         
131         ICommandManager commandManager= PlatformUI.getWorkbench().getCommandSupport().getCommandManager();
132         ICommand command = null;
133         if (commandId != null) {
134             command = commandManager.getCommand(commandId);
135         }
136         
137         Label separator= new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL | SWT.LINE_DOT);
138         separator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
139         
140         Label label = new Label(shell, SWT.SHADOW_NONE | SWT.RIGHT);
141         label.setText(labelText);
142         label.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
143         label.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
144         label.setEnabled(false);
145         label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));
146         if (command != null) {
147             List JavaDoc keyBindings = command.getKeySequenceBindings();
148             if (keyBindings != null && keyBindings.size() > 0) {
149                 IKeySequenceBinding keySequenceBinding = (IKeySequenceBinding)keyBindings.get(0);
150                 label.setText(MessageFormat.format(DebugUIMessages.PopupInformationControl_1, new String JavaDoc[] {keySequenceBinding.getKeySequence().format(), labelText})); //$NON-NLS-1$
151
label.getParent().layout();
152             }
153         }
154     }
155     
156     /* (non-Javadoc)
157      * @see org.eclipse.jface.text.IInformationControl#addDisposeListener(DisposeListener)
158      */

159     public void addDisposeListener(DisposeListener listener) {
160         shell.addDisposeListener(listener);
161     }
162     
163     /* (non-Javadoc)
164      * @see org.eclipse.jface.text.IInformationControl#addFocusListener(FocusListener)
165      */

166     public void addFocusListener(FocusListener listener) {
167         shell.addFocusListener(listener);
168     }
169     
170     /* (non-Javadoc)
171      * @see org.eclipse.jface.text.IInformationControl#computeSizeHint()
172      */

173     public Point computeSizeHint() {
174         Point persistedSize = getInitialSize();
175         if (persistedSize != null) {
176             return persistedSize;
177         }
178         
179         Point computedSize = shell.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
180         if (maxWidth > 0 && computedSize.x > maxWidth)
181             computedSize.x = maxWidth;
182         if (maxHeight > 0 && computedSize.y > maxHeight)
183             computedSize.y = maxHeight;
184         return computedSize;
185     }
186     
187     /* (non-Javadoc)
188      * @see org.eclipse.jface.text.IInformationControl#dispose()
189      */

190     public void dispose() {
191         deregister();
192         persistSize();
193         shell= null;
194     }
195     
196     /**
197      * Deregisters this popup's default close action and turns off the
198      * debug popup scope.
199      */

200     private void deregister() {
201         IWorkbench workbench = PlatformUI.getWorkbench();
202         IWorkbenchContextSupport contextSupport = workbench.getContextSupport();
203         IWorkbenchCommandSupport commandSupport = workbench.getCommandSupport();
204         
205         commandSupport.removeHandlerSubmission(submission);
206         contextSupport.unregisterShell(shell);
207     }
208     
209     /**
210      * Registers this popup's default close action and turns on the
211      * debug popup scope.
212      */

213     private void register() {
214         if (closeHandler != null) {
215             IWorkbench workbench = PlatformUI.getWorkbench();
216             
217             IWorkbenchContextSupport contextSupport = workbench.getContextSupport();
218             IWorkbenchCommandSupport commandSupport = workbench.getCommandSupport();
219             
220             submission = new HandlerSubmission(null, shell, null, commandId, closeHandler, Priority.MEDIUM);
221             commandSupport.addHandlerSubmission(submission);
222             
223             contextSupport.registerShell(shell, IWorkbenchContextSupport.TYPE_DIALOG);
224         }
225     }
226     
227     /* (non-Javadoc)
228      * @see org.eclipse.jface.text.IInformationControl#removeDisposeListener(DisposeListener)
229      */

230     public void removeDisposeListener(DisposeListener listener) {
231         shell.removeDisposeListener(listener);
232     }
233     
234     /* (non-Javadoc)
235      * @see org.eclipse.jface.text.IInformationControl#removeFocusListener(FocusListener)
236      */

237     public void removeFocusListener(FocusListener listener) {
238         shell.removeFocusListener(listener);
239     }
240     
241     /* (non-Javadoc)
242      * @see org.eclipse.jface.text.IInformationControl#setBackgroundColor(Color)
243      */

244     public void setBackgroundColor(Color background) {
245         shell.setBackground(background);
246     }
247     
248     /* (non-Javadoc)
249      * @see org.eclipse.jface.text.IInformationControl#setForegroundColor(Color)
250      */

251     public void setForegroundColor(Color foreground) {
252         shell.setForeground(foreground);
253     }
254     
255     /* (non-Javadoc)
256      * @see org.eclipse.jface.text.IInformationControl#setLocation(Point)
257      */

258     public void setLocation(Point location) {
259         Rectangle displayBounds = control.getDisplay().getClientArea();
260         
261         location.x = location.x < 0 ? displayBounds.x + 25 : location.x;
262         location.y = location.y < 0 ? displayBounds.y + 25 : location.y;
263         
264         Point shellSize = shell.getSize();
265         boolean shellSizeChanged = false;
266         if (shellSize.x + location.x > displayBounds.width) {
267             shellSize.x = displayBounds.width - location.x;
268             shellSizeChanged = true;
269         }
270         if (shellSize.y + location.y > displayBounds.height) {
271             shellSize.y = displayBounds.height - location.y;
272             shellSizeChanged = true;
273         }
274         if (shellSizeChanged) {
275             shell.setSize(shellSize);
276         }
277
278         shell.setLocation(location);
279     }
280     
281     /* (non-Javadoc)
282      * @see org.eclipse.jface.text.IInformationControl#setSize(int, int)
283      */

284     public void setSize(int width, int height) {
285         shell.setSize(width, height);
286     }
287     
288     /* (non-Javadoc)
289      * @see org.eclipse.jface.text.IInformationControl#setSizeContraints(int, int)
290      */

291     public void setSizeConstraints(int maxWidth, int maxHeight) {
292         this.maxWidth = maxWidth;
293         this.maxHeight = maxHeight;
294     }
295     
296     /* (non-Javadoc)
297      * @see org.eclipse.jface.text.IInformationControl#setVisible(boolean)
298      */

299     public void setVisible(boolean visible) {
300         shell.setVisible(visible);
301         if (!visible) {
302             deregister();
303             shell.dispose();
304         }
305     }
306             
307     /**
308      * Creates and returns the control for this popup.
309      *
310      * @param parent parent control
311      * @return control
312      */

313     protected abstract Control createControl(Composite parent);
314     
315     /**
316      * Attempts to retrieve the size of the popup when it was last disposed.
317      * @return The size the initial size of the popup if available, otherwise null
318      */

319     protected Point getInitialSize() {
320         Point point = null;
321         try {
322             IDialogSettings settings = getDialogSettings();
323             if (settings != null) {
324                 String JavaDoc key = getClass().getName();
325                 
326                 int height = settings.getInt(key+HEIGHT_STRING);
327                 int width = settings.getInt(key+WIDTH_STRING);
328                 
329                 point = new Point(width, height);
330             }
331         } catch (NumberFormatException JavaDoc e) {
332         }
333         
334         return point;
335     }
336     
337     /**
338      * Returns a dialog settings in which to persist/restore popup control size.
339      *
340      * @return dialog settings
341      */

342     protected IDialogSettings getDialogSettings() {
343         return DebugUIPlugin.getDefault().getDialogSettings();
344     }
345     
346     /**
347      * Attempts to store the current size of the popup in the adapter's IDialogSettings.
348      * Uses the adapters fully qualified class name to create unique keys.
349      */

350     protected void persistSize() {
351         if (shell == null) {
352             return;
353         }
354         
355         IDialogSettings settings = getDialogSettings();
356         if (settings != null) {
357             String JavaDoc key = getClass().getName();
358             Point size = shell.getSize();
359             settings.put(key+WIDTH_STRING, size.x);
360             settings.put(key+HEIGHT_STRING, size.y);
361         }
362     }
363
364     /**
365      * Handler to closes this popup
366      */

367     private class CloseHandler extends AbstractHandler {
368         /* (non-Javadoc)
369          * @see org.eclipse.ui.commands.IHandler#execute(java.lang.Object)
370          */

371         public Object JavaDoc execute(Map JavaDoc parameter) throws ExecutionException {
372             performCommand();
373             if (shell != null) {
374                 shell.dispose();
375             }
376             return null;
377         }
378         
379     }
380     
381     /**
382      * Called when this popup is closed via its command.
383      * Subclasses must override.
384      */

385     protected abstract void performCommand();
386     
387     /**
388      * Returns this popup's shell.
389      *
390      * @return shell
391      */

392     protected Shell getShell() {
393         return shell;
394     }
395     /* (non-Javadoc)
396      * @see org.eclipse.jface.text.IInformationControl#isFocusControl()
397      */

398     public boolean isFocusControl() {
399         return control.isFocusControl();
400     }
401     /* (non-Javadoc)
402      * @see org.eclipse.jface.text.IInformationControl#setFocus()
403      */

404     public void setFocus() {
405         control.setFocus();
406     }
407 }
408
Popular Tags