KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > misc > UIListenerLogging


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.ui.internal.misc;
12
13 import org.eclipse.core.runtime.Platform;
14 import org.eclipse.ui.IPerspectiveDescriptor;
15 import org.eclipse.ui.IWorkbenchPage;
16 import org.eclipse.ui.IWorkbenchPart;
17 import org.eclipse.ui.IWorkbenchPartConstants;
18 import org.eclipse.ui.IWorkbenchPartReference;
19 import org.eclipse.ui.IWorkbenchWindow;
20 import org.eclipse.ui.PlatformUI;
21 import org.eclipse.ui.internal.util.Util;
22
23
24 /**
25  * To whom it may concern: Please stop deleting the logging code. This is important for debugging
26  * event ordering issues.
27  */

28 public class UIListenerLogging {
29     
30     // Types of listeners that can be logged (the names of the options that enable/disable their logging)
31
private final static String JavaDoc LISTENER_EVENTS = PlatformUI.PLUGIN_ID + "/debug"; //$NON-NLS-1$
32
public final static String JavaDoc PAGE_PARTLISTENER_EVENTS = PlatformUI.PLUGIN_ID + "/listeners/IWorkbenchPage.IPartListener"; //$NON-NLS-1$
33
public final static String JavaDoc PAGE_PARTLISTENER2_EVENTS = PlatformUI.PLUGIN_ID + "/listeners/IWorkbenchPage.IPartListener2"; //$NON-NLS-1$
34
private final static String JavaDoc PAGE_PROPERTY_EVENTS = PlatformUI.PLUGIN_ID + "/listeners/IWorkbenchPage.IPropertyChangeListener"; //$NON-NLS-1$
35
private final static String JavaDoc WINDOW_PAGE_EVENTS = PlatformUI.PLUGIN_ID + "/listeners/IWorkbenchWindow.IPageListener"; //$NON-NLS-1$
36
private final static String JavaDoc WINDOW_PERSPECTIVE_EVENTS = PlatformUI.PLUGIN_ID + "/listeners/IWorkbenchWindow.IPerspectiveListener"; //$NON-NLS-1$
37
public final static String JavaDoc WINDOW_PARTLISTENER_EVENTS = PlatformUI.PLUGIN_ID + "/listeners/IWorkbenchWindow.IPartListener"; //$NON-NLS-1$
38
public final static String JavaDoc WINDOW_PARTLISTENER2_EVENTS = PlatformUI.PLUGIN_ID + "/listeners/IWorkbenchWindow.IPartListener2"; //$NON-NLS-1$
39
private final static String JavaDoc PARTREFERENCE_PROPERTY_EVENTS = PlatformUI.PLUGIN_ID + "/listeners/IWorkbenchPartReference"; //$NON-NLS-1$
40

41     public final static boolean enabled = internal_isEnabled(LISTENER_EVENTS);
42     
43     // IPartListener events
44
public final static String JavaDoc PE_ACTIVATED = "partActivated"; //$NON-NLS-1$
45
public final static String JavaDoc PE_PART_BROUGHT_TO_TOP = "partBroughtToTop"; //$NON-NLS-1$
46
public final static String JavaDoc PE_PART_CLOSED = "partClosed"; //$NON-NLS-1$
47
public final static String JavaDoc PE_PART_DEACTIVATED = "partDeactivated"; //$NON-NLS-1$
48
public final static String JavaDoc PE_PART_OPENED = "partOpened"; //$NON-NLS-1$
49

50     // IPartListener2 events
51
public final static String JavaDoc PE2_ACTIVATED = "partActivated"; //$NON-NLS-1$
52
public final static String JavaDoc PE2_PART_VISIBLE = "partVisible"; //$NON-NLS-1$
53
public final static String JavaDoc PE2_PART_HIDDEN = "partHidden"; //$NON-NLS-1$
54
public final static String JavaDoc PE2_PART_BROUGHT_TO_TOP = "partBroughtToTop"; //$NON-NLS-1$
55
public final static String JavaDoc PE2_PART_CLOSED = "partClosed"; //$NON-NLS-1$
56
public final static String JavaDoc PE2_PART_DEACTIVATED = "partDectivated"; //$NON-NLS-1$
57
public final static String JavaDoc PE2_PART_OPENED = "partOpened"; //$NON-NLS-1$
58
public static final String JavaDoc PE2_PART_INPUT_CHANGED = "partInputChanged"; //$NON-NLS-1$
59

60     // IPageListener events
61
public final static String JavaDoc WPE_PAGE_ACTIVATED = "pageActivated"; //$NON-NLS-1$
62
public final static String JavaDoc WPE_PAGE_OPENED = "pageOpened"; //$NON-NLS-1$
63
public final static String JavaDoc WPE_PAGE_CLOSED = "pageClosed"; //$NON-NLS-1$
64

65     // IPerspectiveListener events
66
public static final String JavaDoc PLE_PERSP_PRE_DEACTIVATE = "perspectivePreDeactivate"; //$NON-NLS-1$
67
public static final String JavaDoc PLE_PERSP_DEACTIVATED = "perspectiveDeactivated"; //$NON-NLS-1$
68
public static final String JavaDoc PLE_PERSP_ACTIVATED = "perspectiveActivated"; //$NON-NLS-1$
69
public static final String JavaDoc PLE_PERSP_OPENED = "perspectiveOpened"; //$NON-NLS-1$
70
public static final String JavaDoc PLE_PERSP_CLOSED = "perspectiveClosed"; //$NON-NLS-1$
71
public static final String JavaDoc PLE_PERSP_SAVED_AS = "perspectiveSavedAs"; //$NON-NLS-1$
72

73     private static String JavaDoc getSourceId(Object JavaDoc source) {
74         return Util.safeString(Integer.toString(source.hashCode() % 1000));
75     }
76
77     private static String JavaDoc getWindowId(IWorkbenchWindow source) {
78         return "window " + Util.safeString(Integer.toString(source.hashCode() % 1000)); //$NON-NLS-1$
79
}
80     
81     private static String JavaDoc getPageId(IWorkbenchPage page) {
82         return "page " + Util.safeString(Integer.toString(page.hashCode() % 1000)); //$NON-NLS-1$
83
}
84     
85     private static String JavaDoc getPerspectiveId(IPerspectiveDescriptor descriptor) {
86         return Util.safeString(descriptor.getId());
87     }
88     
89     public static final void logPageEvent(IWorkbenchWindow window, IWorkbenchPage page, String JavaDoc eventId) {
90         if (isEnabled(WINDOW_PAGE_EVENTS)) {
91             System.out.println(WINDOW_PAGE_EVENTS
92                     + " " + getWindowId(window) //$NON-NLS-1$
93
+ " " + eventId + " (" + getPageId(page) + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
94
}
95     }
96
97     public static final void logPerspectiveEvent(IWorkbenchWindow window, IWorkbenchPage page,
98             IPerspectiveDescriptor descriptor, String JavaDoc eventId) {
99         
100         if (isEnabled(WINDOW_PERSPECTIVE_EVENTS)) {
101             System.out.println(WINDOW_PERSPECTIVE_EVENTS
102                     + " " + getWindowId(window) //$NON-NLS-1$
103
+ " " + eventId + " (" + getPageId(page) + ", " + getPerspectiveId(descriptor) + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
104
}
105     }
106
107     public static final void logPerspectiveChangedEvent(IWorkbenchWindow window, IWorkbenchPage page,
108             IPerspectiveDescriptor descriptor, IWorkbenchPartReference ref, String JavaDoc changeId) {
109         
110         if (isEnabled(WINDOW_PERSPECTIVE_EVENTS)) {
111             System.out.println(WINDOW_PERSPECTIVE_EVENTS
112                     + " "+ getWindowId(window) //$NON-NLS-1$
113
+ " perspectiveChanged (" + getPageId(page) + ", " + getPerspectiveId(descriptor) //$NON-NLS-1$ //$NON-NLS-2$
114
+ ", " + getPartId(ref) + ", " + changeId + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
115

116         }
117     }
118
119     public static final void logPerspectiveSavedAs(IWorkbenchWindow window, IWorkbenchPage page,
120             IPerspectiveDescriptor oldDescriptor, IPerspectiveDescriptor newDescriptor) {
121         
122         if (isEnabled(WINDOW_PERSPECTIVE_EVENTS)) {
123             System.out.println(WINDOW_PERSPECTIVE_EVENTS
124                     + " " + getWindowId(window) //$NON-NLS-1$
125
+ " " + PLE_PERSP_SAVED_AS + " (" + getPageId(page) + ", " + getPerspectiveId(oldDescriptor) + ", " + getPerspectiveId(newDescriptor) + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
126
}
127     }
128
129     
130     private static String JavaDoc getPartId(IWorkbenchPart part) {
131         if (part == null) {
132             return "null part"; //$NON-NLS-1$
133
}
134         
135         return Util.safeString(part.getTitle()) + " - " //$NON-NLS-1$
136
+ Util.safeString(part.getSite().getId());
137     }
138     
139     private static String JavaDoc getPartId(IWorkbenchPartReference ref) {
140         if (ref == null) {
141             return "null part"; //$NON-NLS-1$
142
}
143         
144         return Util.safeString(ref.getPartName()) + " - " //$NON-NLS-1$
145
+ Util.safeString(ref.getId());
146     }
147
148     /**
149      * Log a partListener event fired from the workbench window
150      *
151      * @param page
152      * @param eventId
153      */

154     public static final void logPartListenerEvent(String JavaDoc sourceType, Object JavaDoc source, IWorkbenchPart part, String JavaDoc eventId) {
155         if (isEnabled(sourceType)) {
156             System.out.println(sourceType + " " + getSourceId(source) //$NON-NLS-1$
157
+ ", " + eventId + "(" + getPartId(part) + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
158
}
159     }
160
161     /**
162      * Log a partListener2 event fired from the workbench window
163      *
164      * @param page
165      * @param eventId
166      */

167     public static final void logPartListener2Event(String JavaDoc sourceType, Object JavaDoc source, IWorkbenchPartReference part, String JavaDoc eventId) {
168         if (isEnabled(sourceType)) {
169             System.out.println(sourceType + " " + getSourceId(source) //$NON-NLS-1$
170
+ ", " + eventId + "(" + getPartId(part) + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
171
}
172     }
173     
174     /**
175      * Log an event fired from the workbench page
176      *
177      * @param page
178      * @param eventId
179      */

180     public static final void logPartListenerEvent(IWorkbenchPage page, IWorkbenchPart part, String JavaDoc eventId) {
181         if (isEnabled(PAGE_PARTLISTENER_EVENTS)) {
182             System.out.println(PAGE_PARTLISTENER_EVENTS + " page " //$NON-NLS-1$
183
+ Util.safeString(page.getLabel())
184                     + ", " + eventId + "(" + getPartId(part) + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
185
}
186     }
187
188     /**
189      * Log an event fired from the workbench page
190      *
191      * @param page
192      * @param eventId
193      */

194     public static final void logPartListener2Event(IWorkbenchPage page, IWorkbenchPartReference part, String JavaDoc eventId) {
195         if (isEnabled(PAGE_PARTLISTENER2_EVENTS)) {
196             System.out.println(PAGE_PARTLISTENER2_EVENTS + " page " //$NON-NLS-1$
197
+ Util.safeString(page.getLabel())
198                     + ", " + eventId + "(" + getPartId(part) + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
199
}
200     }
201     
202     /**
203      * Log an event fired from the workbench page
204      *
205      * @param page
206      * @param eventId
207      */

208     public static final void logPagePropertyChanged(IWorkbenchPage page, String JavaDoc changeId, Object JavaDoc oldValue, Object JavaDoc newValue) {
209         if (isEnabled(PAGE_PROPERTY_EVENTS)) {
210             System.out.println(PAGE_PROPERTY_EVENTS + " page " //$NON-NLS-1$
211
+ Util.safeString(page.getLabel())
212                     + ", " + changeId + " = " + Util.safeString(newValue.toString()) + "( old value = " + newValue.toString() + " )"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
213
}
214     }
215     
216     public static final void logPartReferencePropertyChange(IWorkbenchPartReference ref, int changeId) {
217         if (isEnabled(PARTREFERENCE_PROPERTY_EVENTS)) {
218             String JavaDoc eventDescription;
219             
220             switch(changeId) {
221                 case IWorkbenchPartConstants.PROP_TITLE: eventDescription = "title"; break; //$NON-NLS-1$
222
case IWorkbenchPartConstants.PROP_DIRTY: eventDescription = "dirty"; break; //$NON-NLS-1$
223
case IWorkbenchPartConstants.PROP_INPUT: eventDescription = "input"; break; //$NON-NLS-1$
224
case IWorkbenchPartConstants.PROP_PART_NAME: eventDescription = "part_name"; break; //$NON-NLS-1$
225
case IWorkbenchPartConstants.PROP_CONTENT_DESCRIPTION: eventDescription = "content_description"; break; //$NON-NLS-1$
226
default:
227                     eventDescription = "unknown event id = " + changeId; //$NON-NLS-1$
228
}
229             
230             System.out.println(PARTREFERENCE_PROPERTY_EVENTS + " " //$NON-NLS-1$
231
+ getPartId(ref)
232                     + ", property " + eventDescription); //$NON-NLS-1$
233
}
234     }
235     
236     private static boolean isEnabled(String JavaDoc eventName) {
237         return enabled && internal_isEnabled(eventName);
238     }
239     
240     private static boolean internal_isEnabled(String JavaDoc eventName) {
241         String JavaDoc option = Platform.getDebugOption(eventName);
242         return option != null && !option.equalsIgnoreCase("false") && !option.equalsIgnoreCase("-1"); //$NON-NLS-1$ //$NON-NLS-2$
243
}
244 }
245
Popular Tags