KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > display > DisplayView


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 package org.eclipse.jdt.internal.debug.ui.display;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.ResourceBundle JavaDoc;
20
21 import org.eclipse.core.commands.AbstractHandler;
22 import org.eclipse.core.commands.ExecutionEvent;
23 import org.eclipse.core.commands.ExecutionException;
24 import org.eclipse.core.commands.IHandler;
25 import org.eclipse.core.commands.operations.IUndoContext;
26 import org.eclipse.debug.ui.DebugUITools;
27 import org.eclipse.debug.ui.IDebugUIConstants;
28 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants;
29 import org.eclipse.jdt.internal.debug.ui.EvaluationContextManager;
30 import org.eclipse.jdt.internal.debug.ui.IJavaDebugHelpContextIds;
31 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
32 import org.eclipse.jdt.internal.debug.ui.JDISourceViewer;
33 import org.eclipse.jdt.ui.text.IJavaPartitions;
34 import org.eclipse.jdt.ui.text.JavaTextTools;
35 import org.eclipse.jface.action.IAction;
36 import org.eclipse.jface.action.IMenuListener;
37 import org.eclipse.jface.action.IMenuManager;
38 import org.eclipse.jface.action.IToolBarManager;
39 import org.eclipse.jface.action.MenuManager;
40 import org.eclipse.jface.action.Separator;
41 import org.eclipse.jface.text.BadLocationException;
42 import org.eclipse.jface.text.Document;
43 import org.eclipse.jface.text.DocumentEvent;
44 import org.eclipse.jface.text.IDocument;
45 import org.eclipse.jface.text.IDocumentListener;
46 import org.eclipse.jface.text.IFindReplaceTarget;
47 import org.eclipse.jface.text.ITextInputListener;
48 import org.eclipse.jface.text.ITextOperationTarget;
49 import org.eclipse.jface.text.ITextSelection;
50 import org.eclipse.jface.text.ITextViewer;
51 import org.eclipse.jface.text.IUndoManager;
52 import org.eclipse.jface.text.IUndoManagerExtension;
53 import org.eclipse.jface.text.source.ISourceViewer;
54 import org.eclipse.jface.viewers.ISelectionChangedListener;
55 import org.eclipse.jface.viewers.SelectionChangedEvent;
56 import org.eclipse.swt.SWT;
57 import org.eclipse.swt.widgets.Composite;
58 import org.eclipse.swt.widgets.Menu;
59 import org.eclipse.ui.IActionBars;
60 import org.eclipse.ui.IMemento;
61 import org.eclipse.ui.IPerspectiveDescriptor;
62 import org.eclipse.ui.IPerspectiveListener2;
63 import org.eclipse.ui.IViewReference;
64 import org.eclipse.ui.IViewSite;
65 import org.eclipse.ui.IWorkbenchActionConstants;
66 import org.eclipse.ui.IWorkbenchPage;
67 import org.eclipse.ui.IWorkbenchPartReference;
68 import org.eclipse.ui.PartInitException;
69 import org.eclipse.ui.PlatformUI;
70 import org.eclipse.ui.XMLMemento;
71 import org.eclipse.ui.actions.ActionFactory;
72 import org.eclipse.ui.console.actions.ClearOutputAction;
73 import org.eclipse.ui.handlers.IHandlerActivation;
74 import org.eclipse.ui.handlers.IHandlerService;
75 import org.eclipse.ui.operations.OperationHistoryActionHandler;
76 import org.eclipse.ui.operations.RedoActionHandler;
77 import org.eclipse.ui.operations.UndoActionHandler;
78 import org.eclipse.ui.part.ViewPart;
79 import org.eclipse.ui.texteditor.FindReplaceAction;
80 import org.eclipse.ui.texteditor.IAbstractTextEditorHelpContextIds;
81 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
82 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
83 import org.eclipse.ui.texteditor.IUpdate;
84 import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds;
85
86 public class DisplayView extends ViewPart implements ITextInputListener, IPerspectiveListener2 {
87         
88     class DataDisplay implements IDataDisplay {
89         /**
90          * @see IDataDisplay#clear()
91          */

92         public void clear() {
93             IDocument document= fSourceViewer.getDocument();
94             if (document != null) {
95                 document.set(""); //$NON-NLS-1$
96
}
97         }
98         
99         /**
100          * @see IDataDisplay#displayExpression(String)
101          */

102         public void displayExpression(String JavaDoc expression) {
103             IDocument document= fSourceViewer.getDocument();
104             int offset= document.getLength();
105             try {
106                 // add a cariage return if needed.
107
if (offset != document.getLineInformationOfOffset(offset).getOffset()) {
108                     expression= System.getProperty("line.separator") + expression.trim(); //$NON-NLS-1$
109
}
110                 fSourceViewer.getDocument().replace(offset, 0, expression);
111                 fSourceViewer.setSelectedRange(offset + expression.length(), 0);
112                 fSourceViewer.revealRange(offset, expression.length());
113             } catch (BadLocationException ble) {
114                 JDIDebugUIPlugin.log(ble);
115             }
116         }
117         
118         /**
119          * @see IDataDisplay#displayExpressionValue(String)
120          */

121         public void displayExpressionValue(String JavaDoc value) {
122             value= System.getProperty("line.separator") + '\t' + value; //$NON-NLS-1$
123
ITextSelection selection= (ITextSelection)fSourceViewer.getSelection();
124
125             int offset= selection.getOffset() + selection.getLength();
126             int length= value.length();
127             try {
128                 fSourceViewer.getDocument().replace(offset, 0, value);
129             } catch (BadLocationException ble) {
130                 JDIDebugUIPlugin.log(ble);
131             }
132             fSourceViewer.setSelectedRange(offset + length, 0);
133             fSourceViewer.revealRange(offset, length);
134         }
135     }
136         
137     protected IDataDisplay fDataDisplay= new DataDisplay();
138     protected IDocumentListener fDocumentListener= null;
139     
140     protected JDISourceViewer fSourceViewer;
141     protected IAction fClearDisplayAction;
142     protected DisplayViewAction fContentAssistAction;
143
144     protected Map JavaDoc fGlobalActions= new HashMap JavaDoc(4);
145     protected List JavaDoc fSelectionActions= new ArrayList JavaDoc(3);
146
147     protected String JavaDoc fRestoredContents= null;
148     /**
149      * This memento allows the Display view to save and restore state
150      * when it is closed and opened within a session. A different
151      * memento is supplied by the platform for persistance at
152      * workbench shutdown.
153      */

154     private static IMemento fgMemento;
155     private IHandlerActivation fHandlerActivation;
156     
157     /**
158      * @see ViewPart#createChild(IWorkbenchPartContainer)
159      */

160     public void createPartControl(Composite parent) {
161         
162         int styles= SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI | SWT.FULL_SELECTION;
163         fSourceViewer= new JDISourceViewer(parent, null, styles);
164         fSourceViewer.configure(new DisplayViewerConfiguration());
165         fSourceViewer.getSelectionProvider().addSelectionChangedListener(getSelectionChangedListener());
166         IDocument doc= getRestoredDocument();
167         fSourceViewer.setDocument(doc);
168         fSourceViewer.addTextInputListener(this);
169         fRestoredContents= null;
170         createActions();
171         createUndoRedoActions();
172         initializeToolBar();
173
174         // create context menu
175
MenuManager menuMgr = new MenuManager("#PopUp"); //$NON-NLS-1$
176
menuMgr.setRemoveAllWhenShown(true);
177         menuMgr.addMenuListener(new IMenuListener() {
178             public void menuAboutToShow(IMenuManager mgr) {
179                 fillContextMenu(mgr);
180             }
181         });
182         
183         Menu menu = menuMgr.createContextMenu(fSourceViewer.getTextWidget());
184         fSourceViewer.getTextWidget().setMenu(menu);
185         getSite().registerContextMenu(menuMgr, fSourceViewer.getSelectionProvider());
186         
187         getSite().setSelectionProvider(fSourceViewer.getSelectionProvider());
188         PlatformUI.getWorkbench().getHelpSystem().setHelp(fSourceViewer.getTextWidget(), IJavaDebugHelpContextIds.DISPLAY_VIEW);
189         getSite().getWorkbenchWindow().addPerspectiveListener(this);
190     }
191
192     protected IDocument getRestoredDocument() {
193         IDocument doc= null;
194         if (fRestoredContents != null) {
195             doc= new Document(fRestoredContents);
196         } else {
197             doc= new Document();
198         }
199         JavaTextTools tools= JDIDebugUIPlugin.getDefault().getJavaTextTools();
200         tools.setupJavaDocumentPartitioner(doc, IJavaPartitions.JAVA_PARTITIONING);
201         fDocumentListener= new IDocumentListener() {
202             /**
203              * @see IDocumentListener#documentAboutToBeChanged(DocumentEvent)
204              */

205             public void documentAboutToBeChanged(DocumentEvent event) {
206             }
207             /**
208              * @see IDocumentListener#documentChanged(DocumentEvent)
209              */

210             public void documentChanged(DocumentEvent event) {
211                 updateAction(ActionFactory.FIND.getId());
212             }
213         };
214         doc.addDocumentListener(fDocumentListener);
215         
216         return doc;
217     }
218     
219     /* (non-Javadoc)
220      * @see org.eclipse.ui.IWorkbenchPart#setFocus()
221      */

222     public void setFocus() {
223         if (fSourceViewer != null) {
224             fSourceViewer.getControl().setFocus();
225         }
226     }
227     
228     /**
229      * Initialize the actions of this view
230      */

231     protected void createActions() {
232                 
233         fClearDisplayAction= new ClearOutputAction(fSourceViewer);
234         
235         IAction action= new DisplayViewAction(this, ITextOperationTarget.CUT);
236         action.setText(DisplayMessages.DisplayView_Cut_label);
237         action.setToolTipText(DisplayMessages.DisplayView_Cut_tooltip);
238         action.setDescription(DisplayMessages.DisplayView_Cut_description);
239         setGlobalAction(ActionFactory.CUT.getId(), action);
240         
241         action= new DisplayViewAction(this, ITextOperationTarget.COPY);
242         action.setText(DisplayMessages.DisplayView_Copy_label);
243         action.setToolTipText(DisplayMessages.DisplayView_Copy_tooltip);
244         action.setDescription(DisplayMessages.DisplayView_Copy_description);
245         setGlobalAction(ActionFactory.COPY.getId(), action);
246         
247         action= new DisplayViewAction(this, ITextOperationTarget.PASTE);
248         action.setText(DisplayMessages.DisplayView_Paste_label);
249         action.setToolTipText(DisplayMessages.DisplayView_Paste_tooltip);
250         action.setDescription(DisplayMessages.DisplayView_Paste_Description);
251         setGlobalAction(ActionFactory.PASTE.getId(), action);
252         
253         action= new DisplayViewAction(this, ITextOperationTarget.SELECT_ALL);
254         action.setText(DisplayMessages.DisplayView_SelectAll_label);
255         action.setToolTipText(DisplayMessages.DisplayView_SelectAll_tooltip);
256         action.setDescription(DisplayMessages.DisplayView_SelectAll_description);
257         setGlobalAction(ActionFactory.SELECT_ALL.getId(), action);
258         
259         //TODO: Still using "old" resource access
260
ResourceBundle JavaDoc bundle= ResourceBundle.getBundle("org.eclipse.jdt.internal.debug.ui.display.DisplayResourceBundleMessages"); //$NON-NLS-1$
261
FindReplaceAction findReplaceAction = new FindReplaceAction(bundle, "find_replace_action_", this); //$NON-NLS-1$
262
findReplaceAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.FIND_REPLACE);
263         setGlobalAction(ActionFactory.FIND.getId(), findReplaceAction);
264         
265         fSelectionActions.add(ActionFactory.CUT.getId());
266         fSelectionActions.add(ActionFactory.COPY.getId());
267         fSelectionActions.add(ActionFactory.PASTE.getId());
268         
269         fContentAssistAction= new DisplayViewAction(this, ISourceViewer.CONTENTASSIST_PROPOSALS);
270         fContentAssistAction.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
271         fContentAssistAction.setText(DisplayMessages.DisplayView_Co_ntent_Assist_Ctrl_Space_1);
272         fContentAssistAction.setDescription(DisplayMessages.DisplayView_Content_Assist_2);
273         fContentAssistAction.setToolTipText(DisplayMessages.DisplayView_Content_Assist_2);
274         fContentAssistAction.setImageDescriptor(DebugUITools.getImageDescriptor(IDebugUIConstants.IMG_ELCL_CONTENT_ASSIST));
275         fContentAssistAction.setHoverImageDescriptor(DebugUITools.getImageDescriptor(IDebugUIConstants.IMG_LCL_CONTENT_ASSIST));
276         fContentAssistAction.setDisabledImageDescriptor(DebugUITools.getImageDescriptor(IDebugUIConstants.IMG_DLCL_CONTENT_ASSIST));
277         getViewSite().getActionBars().updateActionBars();
278
279         IHandler handler = new AbstractHandler() {
280             public Object JavaDoc execute(ExecutionEvent event) throws ExecutionException {
281                 fContentAssistAction.run();
282                 return null;
283             }
284             
285         };
286
287         IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class);
288         fHandlerActivation = handlerService.activateHandler(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS, handler);
289     }
290     
291     /**
292      * Creates this editor's undo/redo actions.
293      * <p>
294      * Subclasses may override or extend.</p>
295      *
296      * @since 3.2
297      */

298     protected void createUndoRedoActions() {
299         IUndoContext undoContext= getUndoContext();
300         if (undoContext != null) {
301             // Use actions provided by global undo/redo
302

303             // Create the undo action
304
OperationHistoryActionHandler undoAction= new UndoActionHandler(getSite(), undoContext);
305             PlatformUI.getWorkbench().getHelpSystem().setHelp(undoAction, IAbstractTextEditorHelpContextIds.UNDO_ACTION);
306             undoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.UNDO);
307             setGlobalAction(ITextEditorActionConstants.UNDO, undoAction);
308
309             // Create the redo action.
310
OperationHistoryActionHandler redoAction= new RedoActionHandler(getSite(), undoContext);
311             PlatformUI.getWorkbench().getHelpSystem().setHelp(redoAction, IAbstractTextEditorHelpContextIds.REDO_ACTION);
312             redoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.REDO);
313             setGlobalAction(ITextEditorActionConstants.REDO, redoAction);
314         }
315     }
316     
317     /**
318      * Returns this editor's viewer's undo manager undo context.
319      *
320      * @return the undo context or <code>null</code> if not available
321      * @since 3.2
322      */

323     private IUndoContext getUndoContext() {
324         IUndoManager undoManager= fSourceViewer.getUndoManager();
325         if (undoManager instanceof IUndoManagerExtension)
326             return ((IUndoManagerExtension)undoManager).getUndoContext();
327         return null;
328     }
329
330     protected void setGlobalAction(String JavaDoc actionID, IAction action) {
331         IActionBars actionBars = getViewSite().getActionBars();
332         fGlobalActions.put(actionID, action);
333         actionBars.setGlobalActionHandler(actionID, action);
334     }
335
336     /**
337      * Configures the toolBar.
338      */

339     private void initializeToolBar() {
340         IToolBarManager tbm = getViewSite().getActionBars().getToolBarManager();
341         tbm.add(new Separator(IJavaDebugUIConstants.EVALUATION_GROUP));
342         tbm.add(fClearDisplayAction);
343         getViewSite().getActionBars().updateActionBars();
344     }
345
346     /**
347      * Adds the context menu actions for the display view.
348      */

349     protected void fillContextMenu(IMenuManager menu) {
350         
351         if (fSourceViewer.getDocument() == null) {
352             return;
353         }
354         menu.add(new Separator(IJavaDebugUIConstants.EVALUATION_GROUP));
355         if (EvaluationContextManager.getEvaluationContext(this) != null) {
356             menu.add(fContentAssistAction);
357         }
358         menu.add(new Separator());
359         menu.add((IAction) fGlobalActions.get(ActionFactory.CUT.getId()));
360         menu.add((IAction) fGlobalActions.get(ActionFactory.COPY.getId()));
361         menu.add((IAction) fGlobalActions.get(ActionFactory.PASTE.getId()));
362         menu.add((IAction) fGlobalActions.get(ActionFactory.SELECT_ALL.getId()));
363         menu.add(new Separator());
364         menu.add((IAction) fGlobalActions.get(ActionFactory.FIND.getId()));
365         menu.add(fClearDisplayAction);
366         menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
367     }
368
369     /* (non-Javadoc)
370      * @see org.eclipse.ui.part.WorkbenchPart#getAdapter(Class)
371      */

372     public Object JavaDoc getAdapter(Class JavaDoc required) {
373             
374         if (ITextOperationTarget.class.equals(required)) {
375             return fSourceViewer.getTextOperationTarget();
376         }
377         
378         if (IFindReplaceTarget.class.equals(required)) {
379             return fSourceViewer.getFindReplaceTarget();
380         }
381             
382         if (IDataDisplay.class.equals(required)) {
383             return fDataDisplay;
384         }
385         if (ITextViewer.class.equals(required)) {
386             return fSourceViewer;
387         }
388         
389         return super.getAdapter(required);
390     }
391     
392     protected void updateActions() {
393         Iterator JavaDoc iterator = fSelectionActions.iterator();
394         while (iterator.hasNext()) {
395             IAction action = (IAction) fGlobalActions.get(iterator.next());
396             if (action instanceof IUpdate) {
397                  ((IUpdate) action).update();
398             }
399         }
400     }
401     
402     /**
403      * Saves the contents of the display view and the formatting.
404      *
405      * @see org.eclipse.ui.IViewPart#saveState(IMemento)
406      */

407     public void saveState(IMemento memento) {
408         if (fSourceViewer != null) {
409             String JavaDoc contents= getContents();
410             if (contents != null) {
411                 memento.putTextData(contents);
412             }
413         } else if (fRestoredContents != null) {
414             memento.putTextData(fRestoredContents);
415         }
416     }
417     
418     /**
419      * Restores the contents of the display view and the formatting.
420      *
421      * @see org.eclipse.ui.IViewPart#init(IViewSite, IMemento)
422      */

423     public void init(IViewSite site, IMemento memento) throws PartInitException {
424         init(site);
425         if (fgMemento != null) {
426             memento= fgMemento;
427         }
428         if (memento != null) {
429             fRestoredContents= memento.getTextData();
430         }
431     }
432     
433     /**
434      * Returns the entire trimmed contents of the current document.
435      * If the contents are "empty" <code>null</code> is returned.
436      */

437     private String JavaDoc getContents() {
438         if (fSourceViewer != null) {
439             IDocument doc= fSourceViewer.getDocument();
440             if (doc != null) {
441                 String JavaDoc contents= doc.get().trim();
442                 if (contents.length() > 0) {
443                     return contents;
444                 }
445             }
446         }
447         return null;
448     }
449     
450     protected final ISelectionChangedListener getSelectionChangedListener() {
451         return new ISelectionChangedListener() {
452                 public void selectionChanged(SelectionChangedEvent event) {
453                     updateSelectionDependentActions();
454                 }
455             };
456     }
457     
458     protected void updateSelectionDependentActions() {
459         Iterator JavaDoc iterator= fSelectionActions.iterator();
460         while (iterator.hasNext())
461             updateAction((String JavaDoc)iterator.next());
462     }
463
464
465     protected void updateAction(String JavaDoc actionId) {
466         IAction action= (IAction)fGlobalActions.get(actionId);
467         if (action instanceof IUpdate) {
468             ((IUpdate) action).update();
469         }
470     }
471     /**
472      * @see ITextInputListener#inputDocumentAboutToBeChanged(IDocument, IDocument)
473      */

474     public void inputDocumentAboutToBeChanged(IDocument oldInput, IDocument newInput) {
475     }
476
477     /**
478      * @see ITextInputListener#inputDocumentChanged(IDocument, IDocument)
479      */

480     public void inputDocumentChanged(IDocument oldInput, IDocument newInput) {
481         oldInput.removeDocumentListener(fDocumentListener);
482     }
483     /* (non-Javadoc)
484      * @see org.eclipse.ui.IWorkbenchPart#dispose()
485      */

486     public void dispose() {
487         getSite().getWorkbenchWindow().removePerspectiveListener(this);
488         if (fSourceViewer != null) {
489             fSourceViewer.dispose();
490             fSourceViewer = null;
491         }
492         
493         IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class);
494         handlerService.deactivateHandler(fHandlerActivation);
495         
496         super.dispose();
497     }
498
499     /* (non-Javadoc)
500      * @see org.eclipse.ui.IPerspectiveListener2#perspectiveChanged(org.eclipse.ui.IWorkbenchPage, org.eclipse.ui.IPerspectiveDescriptor, org.eclipse.ui.IWorkbenchPartReference, java.lang.String)
501      */

502     public void perspectiveChanged(IWorkbenchPage page, IPerspectiveDescriptor perspective, IWorkbenchPartReference partRef, String JavaDoc changeId) {
503         if (partRef instanceof IViewReference && changeId.equals(IWorkbenchPage.CHANGE_VIEW_HIDE)) {
504             String JavaDoc id = ((IViewReference) partRef).getId();
505             if (id.equals(getViewSite().getId())) {
506                 // Display view closed. Persist contents.
507
String JavaDoc contents= getContents();
508                 if (contents != null) {
509                     fgMemento= XMLMemento.createWriteRoot("DisplayViewMemento"); //$NON-NLS-1$
510
fgMemento.putTextData(contents);
511                 }
512             }
513         }
514     }
515
516     /* (non-Javadoc)
517      * @see org.eclipse.ui.IPerspectiveListener#perspectiveActivated(org.eclipse.ui.IWorkbenchPage, org.eclipse.ui.IPerspectiveDescriptor)
518      */

519     public void perspectiveActivated(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
520     }
521
522     /* (non-Javadoc)
523      * @see org.eclipse.ui.IPerspectiveListener#perspectiveChanged(org.eclipse.ui.IWorkbenchPage, org.eclipse.ui.IPerspectiveDescriptor, java.lang.String)
524      */

525     public void perspectiveChanged(IWorkbenchPage page, IPerspectiveDescriptor perspective, String JavaDoc changeId) {
526     }
527
528 }
529
Popular Tags