KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > filesystem > Policy


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.core.internal.filesystem;
12
13 import java.io.*;
14 import java.util.Date JavaDoc;
15 import org.eclipse.core.internal.runtime.RuntimeLog;
16 import org.eclipse.core.runtime.*;
17
18 /**
19  * Grab bag of utility methods for the file system plugin
20  */

21 public class Policy {
22
23     /**
24      * General debug flag for the plugin
25      */

26     public static boolean DEBUG = false;
27
28     public static final String JavaDoc PI_FILE_SYSTEM = "org.eclipse.core.filesystem"; //$NON-NLS-1$
29

30     public static void checkCanceled(IProgressMonitor monitor) {
31         if (monitor.isCanceled())
32             throw new OperationCanceledException();
33     }
34
35     /**
36      * Print a debug message to the console.
37      * Pre-pend the message with the current date and the name of the current thread.
38      */

39     public static void debug(String JavaDoc message) {
40         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
41         buffer.append(new Date JavaDoc(System.currentTimeMillis()));
42         buffer.append(" - ["); //$NON-NLS-1$
43
buffer.append(Thread.currentThread().getName());
44         buffer.append("] "); //$NON-NLS-1$
45
buffer.append(message);
46         System.out.println(buffer.toString());
47     }
48
49     public static void error(int code, String JavaDoc message) throws CoreException {
50         error(code, message, null);
51     }
52
53     public static void error(int code, String JavaDoc message, Throwable JavaDoc exception) throws CoreException {
54         int severity = code == 0 ? 0 : 1 << (code % 100 / 33);
55         throw new CoreException(new Status(severity, PI_FILE_SYSTEM, code, message, exception));
56     }
57
58     public static void log(int severity, String JavaDoc message, Throwable JavaDoc t) {
59         if (message == null)
60             message = ""; //$NON-NLS-1$
61
RuntimeLog.log(new Status(severity, PI_FILE_SYSTEM, 1, message, t));
62     }
63
64     public static IProgressMonitor monitorFor(IProgressMonitor monitor) {
65         return monitor == null ? new NullProgressMonitor() : monitor;
66     }
67
68     /**
69      * Closes a stream and ignores any resulting exception.
70      */

71     public static void safeClose(InputStream in) {
72         try {
73             if (in != null)
74                 in.close();
75         } catch (IOException e) {
76             //ignore
77
}
78     }
79
80     /**
81      * Closes a stream and ignores any resulting exception.
82      */

83     public static void safeClose(OutputStream out) {
84         try {
85             if (out != null)
86                 out.close();
87         } catch (IOException e) {
88             //ignore
89
}
90     }
91
92     public static IProgressMonitor subMonitorFor(IProgressMonitor monitor, int ticks) {
93         if (monitor == null)
94             return new NullProgressMonitor();
95         if (monitor instanceof NullProgressMonitor)
96             return monitor;
97         return new SubProgressMonitor(monitor, ticks);
98     }
99 }
100
Popular Tags