KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > ui > DebugPopup


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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.debug.ui;
12
13 import com.ibm.icu.text.MessageFormat;
14
15 import org.eclipse.core.commands.AbstractHandler;
16 import org.eclipse.core.commands.ExecutionEvent;
17 import org.eclipse.core.commands.ExecutionException;
18 import org.eclipse.core.commands.IHandler;
19 import org.eclipse.debug.internal.ui.DebugUIPlugin;
20 import org.eclipse.debug.internal.ui.views.DebugUIViewsMessages;
21 import org.eclipse.jface.dialogs.IDialogSettings;
22 import org.eclipse.jface.dialogs.PopupDialog;
23 import org.eclipse.swt.graphics.Point;
24 import org.eclipse.swt.graphics.Rectangle;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Shell;
28 import org.eclipse.ui.IWorkbench;
29 import org.eclipse.ui.PlatformUI;
30 import org.eclipse.ui.handlers.IHandlerActivation;
31 import org.eclipse.ui.handlers.IHandlerService;
32 import org.eclipse.ui.keys.IBindingService;
33
34 /**
35  * A <code>PopupDialog</code> that is automatically positioned relative
36  * to a specified anchor point. The popup can be dismissed in the same
37  * manor as all popup dialogs, but additionally allows clients the option
38  * of specifying a command id that can be used to persist the contents of
39  * the dialog.
40  * <p>
41  * Clients may subclass this.
42  * @since 3.2
43  */

44 public abstract class DebugPopup extends PopupDialog {
45
46     private Point fAnchor;
47
48     private IHandlerActivation fActivation;
49
50     private IHandlerService fHandlerService;
51
52     private String JavaDoc fCommandId;
53     
54     private boolean fPersisted = false;
55
56     /**
57      * Constructs a new popup dialog of type <code>PopupDialog.INFOPOPUPRESIZE_SHELLSTYLE</code>
58      * @param parent The parent shell
59      * @param anchor point at which to anchor the popup dialog in Display coordinate space.
60      * Since 3.3, <code>null</code> can be specified to use a default anchor point
61      * @param commandId The command id to be used for persistence of
62      * the dialog, or <code>null</code>
63      */

64     public DebugPopup(Shell parent, Point anchor, String JavaDoc commandId) {
65         super(parent, PopupDialog.INFOPOPUPRESIZE_SHELLSTYLE, true, true, true, false, null, null);
66         fAnchor = anchor;
67         fCommandId = commandId;
68     }
69
70     /**
71      * Returns the text to be shown in the popups's information area.
72      * May return <code>null</code>.
73      * <p>
74      * By default, if this dialog has a persistence command associated with it,
75      * the text displayed is of the form "Press {key-sequence} to {action}". The
76      * action text is specified by the method <code>getActionText()</code>.
77      * </p>
78      * @return The text to be shown in the popup's information area or <code>null</code>
79      */

80     protected String JavaDoc getInfoText() {
81         if (getCommandId() != null && getActionText() != null) {
82             IWorkbench workbench = PlatformUI.getWorkbench();
83             IBindingService bindingService = (IBindingService) workbench.getAdapter(IBindingService.class);
84             String JavaDoc formattedBinding = bindingService.getBestActiveBindingFormattedFor(getCommandId());
85             
86             String JavaDoc infoText = null;
87             if (formattedBinding != null) {
88                  infoText = MessageFormat.format(DebugUIViewsMessages.InspectPopupDialog_1, new String JavaDoc[] { formattedBinding, getActionText()});
89             }
90             return infoText;
91         }
92         return null;
93     }
94     
95     /**
96      * Returns the text to be shown as the action performed when this dialog's
97      * persist command is invoked, or <code>null</code>.
98      * <p>
99      * Subclasses should override as necessary.
100      * </p>
101      * @return the text to be shown as the action performed when this dialog's
102      * persist command is invoked
103      */

104     protected String JavaDoc getActionText() {
105         return null;
106     }
107
108     /**
109      * Returns the command id to be used for persisting the contents of the
110      * dialog. If the contents should not be persisted, this method should
111      * return null.
112      *
113      * @return The command id to be used for persisting the contents of the
114      * dialog or <code>null</code>
115      */

116     protected String JavaDoc getCommandId() {
117         return fCommandId;
118     }
119
120     /**
121      * Persists the contents of the dialog. Subclasses should override as required,
122      * but also call super.persist().
123      */

124     protected void persist() {
125         fPersisted = true;
126     }
127     
128     /**
129      * Returns whether the command handler was invoked to persist this popup's result.
130      *
131      * @return whether the command handler was invoked to persist this popup's result
132      */

133     protected boolean wasPersisted() {
134         return fPersisted;
135     }
136
137     /*
138      * (non-Javadoc)
139      * @see org.eclipse.jface.dialogs.PopupDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
140      */

141     protected abstract Control createDialogArea(Composite parent);
142
143
144     /**
145      * Returns the initial location to use for the shell based upon the
146      * current selection in the viewer. Bottom is preferred to top, and
147      * right is preferred to left, therefore if possible the popup will
148      * be located below and to the right of the selection.
149      *
150      * @param initialSize
151      * the initial size of the shell, as returned by
152      * <code>getInitialSize</code>.
153      * @return the initial location of the shell
154      */

155     protected Point getInitialLocation(Point initialSize) {
156         if (fAnchor == null) {
157             return super.getInitialLocation(initialSize);
158         }
159         Point point = fAnchor;
160         Rectangle monitor = getShell().getMonitor().getClientArea();
161         if (monitor.width < point.x + initialSize.x) {
162             point.x = Math.max(0, point.x - initialSize.x);
163         }
164         if (monitor.height < point.y + initialSize.y) {
165             point.y = Math.max(0, point.y - initialSize.y);
166         }
167         return point;
168     }
169
170     /*
171      * (non-Javadoc)
172      * @see org.eclipse.jface.dialogs.PopupDialog#getDialogSettings()
173      */

174     protected IDialogSettings getDialogSettings() {
175         IDialogSettings settings = DebugUIPlugin.getDefault().getDialogSettings();
176         return settings;
177     }
178
179     
180     /* (non-Javadoc)
181      * @see org.eclipse.jface.dialogs.PopupDialog#open()
182      */

183     public int open() {
184         IWorkbench workbench = PlatformUI.getWorkbench();
185         String JavaDoc commandId = getCommandId();
186         if (commandId != null) {
187             IHandler fCloseHandler = new AbstractHandler() {
188                 public Object JavaDoc execute(ExecutionEvent event) throws ExecutionException {
189                     persist();
190                     close();
191                     return null;
192                 }
193             };
194
195             fHandlerService = (IHandlerService) workbench.getAdapter(IHandlerService.class);
196             fActivation = fHandlerService.activateHandler(commandId, fCloseHandler);
197         }
198
199         String JavaDoc infoText = getInfoText();
200         if (infoText != null)
201             setInfoText(infoText);
202         
203         return super.open();
204     }
205
206     /* (non-Javadoc)
207      * @see org.eclipse.jface.dialogs.PopupDialog#close()
208      */

209     public boolean close() {
210         if (fActivation != null)
211             fHandlerService.deactivateHandler(fActivation);
212
213         return super.close();
214     }
215 }
216
Popular Tags