001: /*******************************************************************************
002: * Copyright (c) 2006, 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: *******************************************************************************/package org.eclipse.ui.ide.undo;
011:
012: import java.io.ByteArrayInputStream;
013: import java.io.InputStream;
014: import java.net.URI;
015:
016: import org.eclipse.core.resources.IFile;
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.ui.internal.ide.undo.ContainerDescription;
019: import org.eclipse.ui.internal.ide.undo.FileDescription;
020: import org.eclipse.ui.internal.ide.undo.IFileContentDescription;
021:
022: /**
023: * A CreateFileOperation represents an undoable operation for creating a file in
024: * the workspace. If a link location is specified, the folder is considered to
025: * be linked to the file at the specified location. If a link location is not
026: * specified, the file will be created in the location specified by the handle,
027: * and the entire containment path of the file will be created if it does not
028: * exist. Clients may call the public API from a background thread.
029: *
030: * This class is intended to be instantiated and used by clients. It is not
031: * intended to be subclassed by clients.
032: *
033: * @since 3.3
034: *
035: */
036: public class CreateFileOperation extends
037: AbstractCreateResourcesOperation {
038:
039: /**
040: * Create a CreateFileOperation
041: *
042: * @param fileHandle
043: * the file to be created
044: * @param linkLocation
045: * the location of the file if it is to be linked
046: * @param contents
047: * the initial contents of the file, or null if there is to be no
048: * contents
049: * @param label
050: * the label of the operation
051: */
052: public CreateFileOperation(IFile fileHandle, URI linkLocation,
053: InputStream contents, String label) {
054: super (null, label);
055: ResourceDescription resourceDescription;
056: if (fileHandle.getParent().exists()) {
057: resourceDescription = new FileDescription(fileHandle,
058: linkLocation, createFileContentDescription(
059: fileHandle, contents));
060: } else {
061: // must first ensure descriptions for the parent folders are
062: // created
063: ContainerDescription containerDescription = ContainerDescription
064: .fromContainer(fileHandle.getParent());
065: containerDescription.getFirstLeafFolder().addMember(
066: new FileDescription(fileHandle, linkLocation,
067: createFileContentDescription(fileHandle,
068: contents)));
069: resourceDescription = containerDescription;
070: }
071: setResourceDescriptions(new ResourceDescription[] { resourceDescription });
072:
073: }
074:
075: /*
076: * Create a file state that represents the desired contents and attributes
077: * of the file to be created. Used to mimic file history when a resource is
078: * first created.
079: */
080: private IFileContentDescription createFileContentDescription(
081: final IFile file, final InputStream contents) {
082: return new IFileContentDescription() {
083: /*
084: * (non-Javadoc)
085: *
086: * @see org.eclipse.ui.internal.ide.undo.IFileContentDescription#getContents()
087: */
088: public InputStream getContents() {
089: if (contents != null) {
090: return contents;
091: }
092: return new ByteArrayInputStream(new byte[0]);
093: }
094:
095: /*
096: * (non-Javadoc)
097: *
098: * @see org.eclipse.ui.internal.ide.undo.IFileContentDescription#getCharset()
099: */
100: public String getCharset() {
101: try {
102: return file.getCharset(false);
103: } catch (CoreException e) {
104: return null;
105: }
106: }
107:
108: /*
109: * (non-Javadoc)
110: *
111: * @see org.eclipse.ui.internal.ide.undo.IFileContentDescription#exists()
112: */
113: public boolean exists() {
114: return true;
115: }
116: };
117: }
118: }
|