KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > ide > undo > CreateFileOperation


1 /*******************************************************************************
2  * Copyright (c) 2006, 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
12 package org.eclipse.ui.ide.undo;
13
14 import java.io.ByteArrayInputStream JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.net.URI JavaDoc;
17
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.ui.internal.ide.undo.ContainerDescription;
21 import org.eclipse.ui.internal.ide.undo.FileDescription;
22 import org.eclipse.ui.internal.ide.undo.IFileContentDescription;
23
24 /**
25  * A CreateFileOperation represents an undoable operation for creating a file in
26  * the workspace. If a link location is specified, the folder is considered to
27  * be linked to the file at the specified location. If a link location is not
28  * specified, the file will be created in the location specified by the handle,
29  * and the entire containment path of the file will be created if it does not
30  * exist. Clients may call the public API from a background thread.
31  *
32  * This class is intended to be instantiated and used by clients. It is not
33  * intended to be subclassed by clients.
34  *
35  * @since 3.3
36  *
37  */

38 public class CreateFileOperation extends AbstractCreateResourcesOperation {
39
40     /**
41      * Create a CreateFileOperation
42      *
43      * @param fileHandle
44      * the file to be created
45      * @param linkLocation
46      * the location of the file if it is to be linked
47      * @param contents
48      * the initial contents of the file, or null if there is to be no
49      * contents
50      * @param label
51      * the label of the operation
52      */

53     public CreateFileOperation(IFile fileHandle, URI JavaDoc linkLocation,
54             InputStream JavaDoc contents, String JavaDoc label) {
55         super(null, label);
56         ResourceDescription resourceDescription;
57         if (fileHandle.getParent().exists()) {
58             resourceDescription = new FileDescription(fileHandle, linkLocation,
59                     createFileContentDescription(fileHandle, contents));
60         } else {
61             // must first ensure descriptions for the parent folders are
62
// created
63
ContainerDescription containerDescription = ContainerDescription
64                     .fromContainer(fileHandle.getParent());
65             containerDescription.getFirstLeafFolder()
66                     .addMember(
67                             new FileDescription(fileHandle, linkLocation,
68                                     createFileContentDescription(fileHandle,
69                                             contents)));
70             resourceDescription = containerDescription;
71         }
72         setResourceDescriptions(new ResourceDescription[] { resourceDescription });
73
74     }
75
76     /*
77      * Create a file state that represents the desired contents and attributes
78      * of the file to be created. Used to mimic file history when a resource is
79      * first created.
80      */

81     private IFileContentDescription createFileContentDescription(
82             final IFile file, final InputStream JavaDoc contents) {
83         return new IFileContentDescription() {
84             /*
85              * (non-Javadoc)
86              *
87              * @see org.eclipse.ui.internal.ide.undo.IFileContentDescription#getContents()
88              */

89             public InputStream JavaDoc getContents() {
90                 if (contents != null) {
91                     return contents;
92                 }
93                 return new ByteArrayInputStream JavaDoc(new byte[0]);
94             }
95
96             /*
97              * (non-Javadoc)
98              *
99              * @see org.eclipse.ui.internal.ide.undo.IFileContentDescription#getCharset()
100              */

101             public String JavaDoc getCharset() {
102                 try {
103                     return file.getCharset(false);
104                 } catch (CoreException e) {
105                     return null;
106                 }
107             }
108
109             /*
110              * (non-Javadoc)
111              *
112              * @see org.eclipse.ui.internal.ide.undo.IFileContentDescription#exists()
113              */

114             public boolean exists() {
115                 return true;
116             }
117         };
118     }
119 }
120
Popular Tags