KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > history > TypedBufferedContent


1 /*******************************************************************************
2  * Copyright (c) 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.team.internal.ui.history;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.lang.reflect.InvocationTargetException JavaDoc;
16
17 import org.eclipse.compare.ITypedElement;
18 import org.eclipse.compare.ResourceNode;
19 import org.eclipse.core.resources.IFile;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.jface.operation.IRunnableWithProgress;
23 import org.eclipse.ui.PlatformUI;
24 import org.eclipse.ui.progress.IProgressService;
25
26
27 public class TypedBufferedContent extends ResourceNode {
28     public TypedBufferedContent(IFile resource) {
29         super(resource);
30     }
31     protected InputStream JavaDoc createStream() throws CoreException {
32         return ((IFile)getResource()).getContents();
33     }
34     public void setContent(byte[] contents) {
35         if (contents == null) contents = new byte[0];
36         final InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(contents);
37         IRunnableWithProgress runnable = new IRunnableWithProgress() {
38             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
39                 try {
40                     IFile file = (IFile) getResource();
41                     if (is != null) {
42                         if (!file.exists()) {
43                             file.create(is, false, monitor);
44                         } else {
45                             file.setContents(is, false, true, monitor);
46                         }
47                     } else {
48                         file.delete(false, true, monitor);
49                     }
50                 } catch (CoreException e) {
51                     throw new InvocationTargetException JavaDoc(e);
52                 }
53             }
54         };
55         try {
56             IProgressService progressService= PlatformUI.getWorkbench().getProgressService();
57             progressService.run(false,false, runnable);
58         } catch (InvocationTargetException JavaDoc e) {
59             //TODO: Handle
60
} catch (InterruptedException JavaDoc e) {
61             // Ignore
62
}
63         fireContentChanged();
64     }
65     public ITypedElement replace(ITypedElement child, ITypedElement other) {
66         return null;
67     }
68     public void fireChange() {
69         fireContentChanged();
70     }
71 }
72
Popular Tags