001: /*******************************************************************************
002: * Copyright (c) 2004, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Chris Gross (schtoo@schtoo.com) - support for ILogger added
011: * (bug 49497 [RCP] JFace dependency on org.eclipse.core.runtime enlarges standalone JFace applications)
012: *******************************************************************************/package org.eclipse.jface.util;
013:
014: import java.util.Comparator;
015:
016: import org.eclipse.core.runtime.IStatus;
017: import org.eclipse.jface.dialogs.AnimatorFactory;
018: import org.eclipse.jface.dialogs.ErrorSupportProvider;
019:
020: /**
021: * The Policy class handles settings for behaviour, debug flags and logging
022: * within JFace.
023: *
024: * @since 3.0
025: */
026: public class Policy {
027:
028: /**
029: * Constant for the the default setting for debug options.
030: */
031: public static final boolean DEFAULT = false;
032:
033: /**
034: * The unique identifier of the JFace plug-in.
035: */
036: public static final String JFACE = "org.eclipse.jface";//$NON-NLS-1$
037:
038: private static ILogger log;
039:
040: private static Comparator viewerComparator;
041:
042: private static AnimatorFactory animatorFactory;
043:
044: /**
045: * A flag to indicate whether unparented dialogs should be checked.
046: */
047: public static boolean DEBUG_DIALOG_NO_PARENT = DEFAULT;
048:
049: /**
050: * A flag to indicate whether actions are being traced.
051: */
052: public static boolean TRACE_ACTIONS = DEFAULT;
053:
054: /**
055: * A flag to indicate whether toolbars are being traced.
056: */
057:
058: public static boolean TRACE_TOOLBAR = DEFAULT;
059:
060: private static ErrorSupportProvider errorSupportProvider;
061:
062: /**
063: * Returns the dummy log to use if none has been set
064: */
065: private static ILogger getDummyLog() {
066: return new ILogger() {
067: public void log(IStatus status) {
068: System.err.println(status.getMessage());
069: if (status.getException() != null) {
070: status.getException().printStackTrace(System.err);
071: }
072: }
073: };
074: }
075:
076: /**
077: * Sets the logger used by JFace to log errors.
078: *
079: * @param logger
080: * the logger to use, or <code>null</code> to use the default
081: * logger
082: * @since 3.1
083: */
084: public static void setLog(ILogger logger) {
085: log = logger;
086: }
087:
088: /**
089: * Returns the logger used by JFace to log errors.
090: * <p>
091: * The default logger prints the status to <code>System.err</code>.
092: * </p>
093: *
094: * @return the logger
095: * @since 3.1
096: */
097: public static ILogger getLog() {
098: if (log == null) {
099: log = getDummyLog();
100: }
101: return log;
102: }
103:
104: /**
105: * Return the default comparator used by JFace to sort strings.
106: *
107: * @return a default comparator used by JFace to sort strings
108: */
109: private static Comparator getDefaultComparator() {
110: return new Comparator() {
111: /**
112: * Compares string s1 to string s2.
113: *
114: * @param s1
115: * string 1
116: * @param s2
117: * string 2
118: * @return Returns an integer value. Value is less than zero if
119: * source is less than target, value is zero if source and
120: * target are equal, value is greater than zero if source is
121: * greater than target.
122: * @exception ClassCastException
123: * the arguments cannot be cast to Strings.
124: */
125: public int compare(Object s1, Object s2) {
126: return ((String) s1).compareTo((String) s2);
127: }
128: };
129: }
130:
131: /**
132: * Return the comparator used by JFace to sort strings.
133: *
134: * @return the comparator used by JFace to sort strings
135: * @since 3.2
136: */
137: public static Comparator getComparator() {
138: if (viewerComparator == null) {
139: viewerComparator = getDefaultComparator();
140: }
141: return viewerComparator;
142: }
143:
144: /**
145: * Sets the comparator used by JFace to sort strings.
146: *
147: * @param comparator
148: * comparator used by JFace to sort strings
149: * @since 3.2
150: */
151: public static void setComparator(Comparator comparator) {
152: org.eclipse.core.runtime.Assert
153: .isTrue(viewerComparator == null);
154: viewerComparator = comparator;
155: }
156:
157: /**
158: * Sets the animator factory used by JFace to create control animator
159: * instances.
160: *
161: * @param factory
162: * the AnimatorFactory to use.
163: * @since 3.2
164: * @deprecated this is no longer in use as of 3.3
165: */
166: public static void setAnimatorFactory(AnimatorFactory factory) {
167: animatorFactory = factory;
168: }
169:
170: /**
171: * Returns the animator factory used by JFace to create control animator
172: * instances.
173: *
174: * @return the animator factory used to create control animator instances.
175: * @since 3.2
176: * @deprecated this is no longer in use as of 3.3
177: */
178: public static AnimatorFactory getAnimatorFactory() {
179: if (animatorFactory == null)
180: animatorFactory = new AnimatorFactory();
181: return animatorFactory;
182: }
183:
184: /**
185: * Set the error support provider for error dialogs.
186: *
187: * @param provider
188: * @since 3.3
189: */
190: public static void setErrorSupportProvider(
191: ErrorSupportProvider provider) {
192: errorSupportProvider = provider;
193: }
194:
195: /**
196: * Return the ErrorSupportProvider for the receiver.
197: *
198: * @return ErrorSupportProvider or <code>null</code> if this has not been set
199: * @since 3.3
200: */
201: public static ErrorSupportProvider getErrorSupportProvider() {
202: return errorSupportProvider;
203: }
204:
205: }
|