KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > ui > sourcelookup > CommonSourceNotFoundEditor


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.sourcelookup;
12
13 import org.eclipse.core.runtime.IAdaptable;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.debug.core.DebugPlugin;
16 import org.eclipse.debug.core.ILaunch;
17 import org.eclipse.debug.core.ILaunchesListener2;
18 import org.eclipse.debug.core.model.IDebugElement;
19 import org.eclipse.debug.core.model.ISourceLocator;
20 import org.eclipse.debug.core.sourcelookup.AbstractSourceLookupDirector;
21 import org.eclipse.debug.internal.ui.DebugUIPlugin;
22 import org.eclipse.debug.internal.ui.IDebugHelpContextIds;
23 import org.eclipse.debug.internal.ui.sourcelookup.SourceLookupManager;
24 import org.eclipse.debug.internal.ui.sourcelookup.SourceLookupUIMessages;
25 import org.eclipse.debug.ui.DebugUITools;
26 import org.eclipse.jface.dialogs.Dialog;
27 import org.eclipse.jface.dialogs.IDialogConstants;
28 import org.eclipse.jface.dialogs.MessageDialog;
29 import org.eclipse.jface.resource.JFaceColors;
30 import org.eclipse.jface.window.Window;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.events.SelectionAdapter;
33 import org.eclipse.swt.events.SelectionEvent;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Button;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Text;
39 import org.eclipse.ui.IEditorInput;
40 import org.eclipse.ui.IEditorPart;
41 import org.eclipse.ui.IEditorSite;
42 import org.eclipse.ui.IReusableEditor;
43 import org.eclipse.ui.IWorkbenchPage;
44 import org.eclipse.ui.IWorkbenchWindow;
45 import org.eclipse.ui.PartInitException;
46 import org.eclipse.ui.PlatformUI;
47 import org.eclipse.ui.part.EditorPart;
48
49 /**
50  * Default editor displayed when source is not found. Displays a button to modify
51  * the source lookup path.
52  * <p>
53  * This editor's id is <code>IDebugUIConstants.ID_COMMON_SOURCE_NOT_FOUND_EDITOR</code>
54  * (value <code>org.eclipse.debug.ui.sourcelookup.CommonSourceNotFoundEditor</code>).
55  * </p>
56  * <p>
57  * This class may be instantiated and subclassed.
58  * </p>
59  * @see AbstractSourceLookupDirector
60  * @see CommonSourceNotFoundEditorInput
61  * @since 3.2
62  */

63 public class CommonSourceNotFoundEditor extends EditorPart implements IReusableEditor {
64     
65     /**
66      * Text widgets used for this editor
67      */

68     private Text fText;
69     
70     /**
71      * Launch listener to handle launch events, or <code>null</code> if none
72      */

73     private ILaunchesListener2 fLaunchesListener;
74     
75     /* (non-Javadoc)
76      * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
77      */

78     public void doSave(IProgressMonitor monitor) {
79     }
80     
81     /* (non-Javadoc)
82      * @see org.eclipse.ui.part.EditorPart#doSaveAs()
83      */

84     public void doSaveAs() {
85     }
86     
87     /* (non-Javadoc)
88      * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
89      */

90     public void init(IEditorSite site, IEditorInput input) throws PartInitException {
91         setSite(site);
92         setInput(input);
93         initialize();
94     }
95     
96     /* (non-Javadoc)
97      * @see org.eclipse.ui.part.EditorPart#isDirty()
98      */

99     public boolean isDirty() {
100         return false;
101     }
102     
103     /* (non-Javadoc)
104      * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
105      */

106     public boolean isSaveAsAllowed() {
107         return false;
108     }
109     
110     /* (non-Javadoc)
111      * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
112      */

113     public void createPartControl(Composite parent) {
114         GridLayout topLayout = new GridLayout();
115         GridData data = new GridData();
116         topLayout.numColumns = 1;
117         topLayout.verticalSpacing = 10;
118         parent.setLayout(topLayout);
119         parent.setLayoutData(data);
120         parent.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));
121         
122         fText = new Text(parent,SWT.READ_ONLY|SWT.WRAP);
123         data = new GridData(GridData.FILL_HORIZONTAL);
124         data.grabExcessHorizontalSpace = true;
125         fText.setLayoutData(data);
126         fText.setForeground(JFaceColors.getErrorText(fText.getDisplay()));
127         fText.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));
128         if (getEditorInput() != null) {
129             setInput(getEditorInput());
130         }
131         
132         createButtons(parent);
133         
134         Dialog.applyDialogFont(parent);
135         
136         PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IDebugHelpContextIds.NO_SOURCE_EDITOR);
137     }
138
139     /**
140      * Create buttons to be displayed in this editor
141      *
142      * @param parent composite to create the buttons in.
143      */

144     protected void createButtons(Composite parent) {
145         GridData data;
146         Button button = new Button(parent, SWT.PUSH);
147         data = new GridData();
148         data.grabExcessHorizontalSpace = false;
149         data.grabExcessVerticalSpace = false;
150         button.setLayoutData(data);
151         button.setText(SourceLookupUIMessages.addSourceLocation_addButton2);
152         button.addSelectionListener(new SelectionAdapter() {
153             public void widgetSelected(SelectionEvent evt) {
154                 editSourceLookupPath();
155             }
156         });
157     }
158     
159     /**
160      * Edits the source lookup path associated with the active debug context.
161      * After the path is edited, source lookup is performed again and this
162      * editor is closed.
163      */

164     protected void editSourceLookupPath(){
165         ISourceLocator locator = null;
166         ILaunch launch = null;
167         IAdaptable selection = DebugUITools.getDebugContext();
168         if(selection == null) {
169             new MessageDialog(getSite().getShell(),
170                     SourceLookupUIMessages.CommonSourceNotFoundEditor_0,
171                     null,
172                     SourceLookupUIMessages.CommonSourceNotFoundEditor_1,
173                     MessageDialog.INFORMATION,
174                     new String JavaDoc[] {IDialogConstants.OK_LABEL}, 0).open();
175             return;
176         }
177         if (selection.getAdapter(ILaunch.class) != null ) {
178             launch = (ILaunch) selection.getAdapter(ILaunch.class);
179             locator = launch.getSourceLocator();
180         }
181         else if (selection.getAdapter(IDebugElement.class) != null ) {
182             launch = ((IDebugElement)selection.getAdapter(IDebugElement.class)).getLaunch();
183             locator = launch.getSourceLocator();
184         }
185         else {
186             return; //should not occur
187
}
188         if (locator == null || !(locator instanceof AbstractSourceLookupDirector)) {
189             return;
190         }
191         final SourceLookupDialog dialog = new SourceLookupDialog(DebugUIPlugin.getShell(),(AbstractSourceLookupDirector) locator);
192         if(dialog.open() == Window.OK) {
193             IWorkbenchPage page = getEditorSite().getPage();
194             SourceLookupManager.getDefault().displaySource(getArtifact(), page, true);
195             closeEditor();
196         }
197     }
198     
199     /* (non-Javadoc)
200      * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
201      */

202     public void setFocus() {
203         if (fText != null) {
204             fText.setFocus();
205         }
206     }
207     
208     /* (non-Javadoc)
209      * @see org.eclipse.ui.part.EditorPart#setInput(org.eclipse.ui.IEditorInput)
210      */

211     public void setInput(IEditorInput input) {
212         super.setInput(input);
213         setPartName(input.getName());
214         if (fText != null) {
215             fText.setText(getText());
216         }
217         firePropertyChange(PROP_INPUT);
218     }
219     
220     /**
221      * Return the text to be displayed in this editor. The text is reset each time
222      * the editor input is set.
223      *
224      * @return the text to be displayed in this editor
225      */

226     protected String JavaDoc getText() {
227         return getEditorInput().getToolTipText() + "\n"; //$NON-NLS-1$
228
}
229     
230     /**
231      * Closes this editor.
232      */

233     protected void closeEditor()
234     {
235         final IEditorPart editor = this;
236         DebugUIPlugin.getStandardDisplay().syncExec(new Runnable JavaDoc() {
237             public void run() {
238                 IWorkbenchWindow activeWorkbenchWindow = DebugUIPlugin.getActiveWorkbenchWindow();
239                 if (activeWorkbenchWindow != null) {
240                     IWorkbenchPage activePage = activeWorkbenchWindow.getActivePage();
241                     if (activePage != null) {
242                         activePage.closeEditor(editor, false);
243                     }
244                 }
245             }
246         });
247     }
248     
249     
250     /*
251      * (non-Javadoc)
252      *
253      * @see org.eclipse.ui.IWorkbenchPart#dispose()
254      */

255     public void dispose() {
256         if (fLaunchesListener != null)
257             DebugPlugin.getDefault().getLaunchManager().removeLaunchListener(fLaunchesListener);
258         super.dispose();
259     }
260     
261     /**
262      * Returns the artifact this editor was opened for (i.e. the artifact that source
263      * was not found for), or <code>null</code>
264      *
265      * @return artifact with associated source or <code>null</code>
266      */

267     protected Object JavaDoc getArtifact() {
268         IEditorInput editorInput = getEditorInput();
269         if (editorInput instanceof CommonSourceNotFoundEditorInput) {
270             CommonSourceNotFoundEditorInput input = (CommonSourceNotFoundEditorInput) editorInput;
271             return input.getArtifact();
272         }
273         return null;
274     }
275     
276     /**
277      * Initialize this editor.
278      * Called after <code>init(IEditorSite, IEditorInput)</code>. By default, a launch
279      * listener is added to close this editor when the associated launch terminates.
280      * Subclasses may override.
281      */

282     protected void initialize()
283     {
284         fLaunchesListener = new ILaunchesListener2() {
285             public void launchesTerminated(ILaunch[] launches) {
286                 Object JavaDoc artifact = getArtifact();
287                 if (artifact instanceof IDebugElement) {
288                     IDebugElement element = (IDebugElement)artifact;
289                     for (int i = 0; i < launches.length; i++) {
290                         ILaunch launch = launches[i];
291                         if (launch.equals(element.getLaunch())) {
292                             closeEditor();
293                             return;
294                         }
295                     }
296                 }
297             }
298
299             public void launchesRemoved(ILaunch[] launches) {
300                 launchesTerminated(launches);
301             }
302
303             public void launchesAdded(ILaunch[] launches) {
304             }
305
306             public void launchesChanged(ILaunch[] launches) {
307             }};
308             
309         DebugPlugin.getDefault().getLaunchManager().addLaunchListener(fLaunchesListener);
310     }
311 }
312
313
314
Popular Tags