| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2004, 2008 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 | * Chris Gross (schtoo@schtoo.com) - support for ILogger added |
| 11 | * (bug 49497 [RCP] JFace dependency on org.eclipse.core.runtime enlarges standalone JFace applications) |
| 12 | *******************************************************************************/ |
| 13 | package org.eclipse.jface.util; |
| 14 | |
| 15 | import java.util.Comparator; |
| 16 | |
| 17 | import org.eclipse.core.runtime.IStatus; |
| 18 | import org.eclipse.core.runtime.Status; |
| 19 | import org.eclipse.jface.dialogs.AnimatorFactory; |
| 20 | import org.eclipse.jface.dialogs.ErrorSupportProvider; |
| 21 | import org.eclipse.swt.events.DisposeEvent; |
| 22 | import org.eclipse.swt.events.DisposeListener; |
| 23 | import org.eclipse.swt.widgets.Display; |
| 24 | |
| 25 | /** |
| 26 | * The Policy class handles settings for behaviour, debug flags and logging |
| 27 | * within JFace. |
| 28 | * |
| 29 | * @since 3.0 |
| 30 | */ |
| 31 | public class Policy { |
| 32 | |
| 33 | /** |
| 34 | * Constant for the the default setting for debug options. |
| 35 | */ |
| 36 | public static final boolean DEFAULT = false; |
| 37 | |
| 38 | /** |
| 39 | * The unique identifier of the JFace plug-in. |
| 40 | */ |
| 41 | public static final String JFACE = "org.eclipse.jface"; //$NON-NLS-1$ |
| 42 | |
| 43 | private static ILogger log; |
| 44 | |
| 45 | private static Comparator viewerComparator; |
| 46 | |
| 47 | private static AnimatorFactory animatorFactory; |
| 48 | |
| 49 | /** |
| 50 | * A flag to indicate whether unparented dialogs should be checked. |
| 51 | */ |
| 52 | public static boolean DEBUG_DIALOG_NO_PARENT = DEFAULT; |
| 53 | |
| 54 | /** |
| 55 | * A flag to indicate whether actions are being traced. |
| 56 | */ |
| 57 | public static boolean TRACE_ACTIONS = DEFAULT; |
| 58 | |
| 59 | /** |
| 60 | * A flag to indicate whether toolbars are being traced. |
| 61 | */ |
| 62 | |
| 63 | public static boolean TRACE_TOOLBAR = DEFAULT; |
| 64 | |
| 65 | private static ErrorSupportProvider errorSupportProvider; |
| 66 | |
| 67 | private static StatusHandler statusHandler; |
| 68 | |
| 69 | /** |
| 70 | * Returns the dummy log to use if none has been set |
| 71 | */ |
| 72 | private static ILogger getDummyLog() { |
| 73 | return new ILogger() { |
| 74 | public void log(IStatus status) { |
| 75 | System.err.println(status.getMessage()); |
| 76 | if (status.getException() != null) { |
| 77 | status.getException().printStackTrace(System.err); |
| 78 | } |
| 79 | } |
| 80 | }; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Sets the logger used by JFace to log errors. |
| 85 | * |
| 86 | * @param logger |
| 87 | * the logger to use, or <code>null</code> to use the default |
| 88 | * logger |
| 89 | * @since 3.1 |
| 90 | */ |
| 91 | public static void setLog(ILogger logger) { |
| 92 | log = logger; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns the logger used by JFace to log errors. |
| 97 | * <p> |
| 98 | * The default logger prints the status to <code>System.err</code>. |
| 99 | * </p> |
| 100 | * |
| 101 | * @return the logger |
| 102 | * @since 3.1 |
| 103 | */ |
| 104 | public static ILogger getLog() { |
| 105 | if (log == null) { |
| 106 | log = getDummyLog(); |
| 107 | } |
| 108 | return log; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Sets the status handler used by JFace to handle statuses. |
| 113 | * |
| 114 | * @param status |
| 115 | * the handler to use, or <code>null</code> to use the default |
| 116 | * one |
| 117 | * @since 3.4 |
| 118 | */ |
| 119 | public static void setStatusHandler(StatusHandler status) { |
| 120 | statusHandler = status; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Returns the status handler used by JFace to handle statuses. |
| 125 | * |
| 126 | * @return the status handler |
| 127 | * @since 3.4 |
| 128 | */ |
| 129 | public static StatusHandler getStatusHandler() { |
| 130 | if (statusHandler == null) { |
| 131 | statusHandler = getDummyStatusHandler(); |
| 132 | } |
| 133 | return statusHandler; |
| 134 | } |
| 135 | |
| 136 | private static StatusHandler getDummyStatusHandler() { |
| 137 | return new StatusHandler() { |
| 138 | private SafeRunnableDialog dialog; |
| 139 | |
| 140 | public void show(final IStatus status, String title) { |
| 141 | Runnable runnable = new Runnable() { |
| 142 | public void run() { |
| 143 | if (dialog == null || dialog.getShell().isDisposed()) { |
| 144 | dialog = new SafeRunnableDialog(status); |
| 145 | dialog.create(); |
| 146 | dialog.getShell().addDisposeListener( |
| 147 | new DisposeListener() { |
| 148 | public void widgetDisposed( |
| 149 | DisposeEvent e) { |
| 150 | dialog = null; |
| 151 | } |
| 152 | }); |
| 153 | dialog.open(); |
| 154 | } else { |
| 155 | dialog.addStatus(status); |
| 156 | dialog.refresh(); |
| 157 | } |
| 158 | } |
| 159 | }; |
| 160 | if (Display.getCurrent() != null) { |
| 161 | runnable.run(); |
| 162 | } else { |
| 163 | Display.getDefault().asyncExec(runnable); |
| 164 | } |
| 165 | } |
| 166 | }; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Return the default comparator used by JFace to sort strings. |
| 171 | * |
| 172 | * @return a default comparator used by JFace to sort strings |
| 173 | */ |
| 174 | private static Comparator getDefaultComparator() { |
| 175 | return new Comparator() { |
| 176 | /** |
| 177 | * Compares string s1 to string s2. |
| 178 | * |
| 179 | * @param s1 |
| 180 | * string 1 |
| 181 | * @param s2 |
| 182 | * string 2 |
| 183 | * @return Returns an integer value. Value is less than zero if |
| 184 | * source is less than target, value is zero if source and |
| 185 | * target are equal, value is greater than zero if source is |
| 186 | * greater than target. |
| 187 | * @exception ClassCastException |
| 188 | * the arguments cannot be cast to Strings. |
| 189 | */ |
| 190 | public int compare(Object s1, Object s2) { |
| 191 | return ((String) s1).compareTo((String) s2); |
| 192 | } |
| 193 | }; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Return the comparator used by JFace to sort strings. |
| 198 | * |
| 199 | * @return the comparator used by JFace to sort strings |
| 200 | * @since 3.2 |
| 201 | */ |
| 202 | public static Comparator getComparator() { |
| 203 | if (viewerComparator == null) { |
| 204 | viewerComparator = getDefaultComparator(); |
| 205 | } |
| 206 | return viewerComparator; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Sets the comparator used by JFace to sort strings. |
| 211 | * |
| 212 | * @param comparator |
| 213 | * comparator used by JFace to sort strings |
| 214 | * @since 3.2 |
| 215 | */ |
| 216 | public static void setComparator(Comparator comparator) { |
| 217 | org.eclipse.core.runtime.Assert.isTrue(viewerComparator == null); |
| 218 | viewerComparator = comparator; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Sets the animator factory used by JFace to create control animator |
| 223 | * instances. |
| 224 | * |
| 225 | * @param factory |
| 226 | * the AnimatorFactory to use. |
| 227 | * @since 3.2 |
| 228 | * @deprecated this is no longer in use as of 3.3 |
| 229 | */ |
| 230 | public static void setAnimatorFactory(AnimatorFactory factory) { |
| 231 | animatorFactory = factory; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Returns the animator factory used by JFace to create control animator |
| 236 | * instances. |
| 237 | * |
| 238 | * @return the animator factory used to create control animator instances. |
| 239 | * @since 3.2 |
| 240 | * @deprecated this is no longer in use as of 3.3 |
| 241 | */ |
| 242 | public static AnimatorFactory getAnimatorFactory() { |
| 243 | if (animatorFactory == null) |
| 244 | animatorFactory = new AnimatorFactory(); |
| 245 | return animatorFactory; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Set the error support provider for error dialogs. |
| 250 | * |
| 251 | * @param provider |
| 252 | * @since 3.3 |
| 253 | */ |
| 254 | public static void setErrorSupportProvider(ErrorSupportProvider provider) { |
| 255 | errorSupportProvider = provider; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Return the ErrorSupportProvider for the receiver. |
| 260 | * |
| 261 | * @return ErrorSupportProvider or <code>null</code> if this has not been |
| 262 | * set |
| 263 | * @since 3.3 |
| 264 | */ |
| 265 | public static ErrorSupportProvider getErrorSupportProvider() { |
| 266 | return errorSupportProvider; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Log the Exception to the logger. |
| 271 | * |
| 272 | * @param exception |
| 273 | * @since 3.4 |
| 274 | */ |
| 275 | public static void logException(Exception exception) { |
| 276 | getLog().log( |
| 277 | new Status(IStatus.ERROR, JFACE, exception |
| 278 | .getLocalizedMessage(), exception)); |
| 279 | |
| 280 | } |
| 281 | |
| 282 | } |