diff options
author | Jenkins <taeyoung2.son@samsung.com> | 2012-09-18 13:27:40 +0900 |
---|---|---|
committer | Jenkins <taeyoung2.son@samsung.com> | 2012-09-18 13:27:40 +0900 |
commit | fec9e294bc016e821e16a07e6be923e17d12f8fe (patch) | |
tree | 261f0f3a6e105010a44904cfd848eaf27725ed59 /org.tizen.common/src/org/tizen/common/util/EFSUtil.java | |
parent | 497d5ab077dbf5ca4146e516535da17e3f7314db (diff) | |
download | common-eplugin-master.tar.gz common-eplugin-master.tar.bz2 common-eplugin-master.zip |
Change-Id: Id40e89a773ceb063b1b0a84fcc651d63cf869b98
Diffstat (limited to 'org.tizen.common/src/org/tizen/common/util/EFSUtil.java')
-rwxr-xr-x | org.tizen.common/src/org/tizen/common/util/EFSUtil.java | 303 |
1 files changed, 303 insertions, 0 deletions
diff --git a/org.tizen.common/src/org/tizen/common/util/EFSUtil.java b/org.tizen.common/src/org/tizen/common/util/EFSUtil.java new file mode 100755 index 000000000..47d33ea06 --- /dev/null +++ b/org.tizen.common/src/org/tizen/common/util/EFSUtil.java @@ -0,0 +1,303 @@ +/* +* Common +* +* Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. +* +* Contact: +* Jihoon Song <jihoon80.song@samsung.com> +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +* Contributors: +* - S-Core Co., Ltd +* +*/ + +package org.tizen.common.util; + +import java.net.URI; + +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.filesystem.IFileStore; +import org.eclipse.core.filesystem.IFileSystem; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.OperationCanceledException; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.internal.ide.dialogs.IDEResourceInfoUtils; +import org.eclipse.ui.internal.ide.dialogs.IFileStoreFilter; +import org.tizen.common.Surrogate; +import org.tizen.common.core.application.Messages; + +/** + * EFSUtil. + * + * Helper related to Eclipse File System + * + * @author Jihoon Song{@literal <jihoon80.song@samsung.com>} (S-Core) + */ +public class EFSUtil { + + public enum DialogChoicer { + YesToAll, + NoToAll, + Yes, + No, + Cancel; + + /** + * Get DialogChoicer's all value + * + * @return String array with DialogChoicer's values + */ + public static String[] toStringArray() { + DialogChoicer[] choicers = DialogChoicer.values(); + String[] values = new String[choicers.length]; + for (DialogChoicer choicer : choicers) { + values[choicer.ordinal()] = choicer.name(); + } + return values; + } + + public static DialogChoicer valueFromOrdinal(int ordinal) { + for (DialogChoicer choicer : values()) { + if (choicer.ordinal() == ordinal) { + return choicer; + } + } + return DialogChoicer.Cancel; + } + } + + private static Surrogate<IFileSystem> fileSystemSurrogate = new Surrogate<IFileSystem>() { + @Override + public IFileSystem getAdapter() { + return EFS.getLocalFileSystem(); + } + }; + + private static final String defaultTask = ""; //$NON-NLS-1$ + private static final int defaultTicks = 100; // TODO : will add automatically calculate method + + private static final IFileStoreFilter defaultFilter = new IFileStoreFilter() { + @Override + public boolean accept(IFileStore store) { + return true; + } + }; + + + // Constructor + private EFSUtil() {} + + public static void setFileSystemSurrogate(Surrogate<IFileSystem> surrogate) { + EFSUtil.fileSystemSurrogate = surrogate; + } + + /** + * Get IFileSystem based on Surrogate<IFileSystem> for this class + * + * @return IFileStoreFilter + */ + protected static IFileSystem getFileSystem() { + return fileSystemSurrogate.getAdapter(); + } + + /** + * Get a default IFileStoreFilter that always returns true + * + * @return IFileStoreFilter + */ + public static IFileStoreFilter getDefaultFileStoreFilter() { + return defaultFilter; + } + + /** + * Detect whether resource exist. + * + * @param fileURI + */ + public static boolean isExistResource(URI fileURI) { + IFileStore fileStore = getFileSystem().getStore(fileURI); + return isExistResource(fileStore); + } + + /** + * Detect whether resource exist. + * + * @param fileStore + */ + public static boolean isExistResource(IFileStore fileStore) { + return (fileStore != null) ? fileStore.fetchInfo().exists() : false; + } + + /** + * Detect whether resource exist. if exist this, open dialog + * + * @param parentShell dialog's parent shell + * @param title dialog title + * @param msg dialog message + * @param previousChoice previous DialogChoicer value + * @param fileStore + * @return in case of previousChoice is Yes or No, return user choice status. any else return previous status + */ + public static DialogChoicer isExistResourceWithDialog(Shell parentShell, + String title, String msg, DialogChoicer previousChoice, IFileStore fileStore) { + + Assert.notNull(parentShell); + Assert.notNull(msg); + Assert.notNull(previousChoice); + Assert.notNull(fileStore); + + switch (previousChoice) { + case Yes : + case No : + break; + case YesToAll : + case NoToAll : + case Cancel : + default : + return previousChoice; + } + + if (isExistResource( fileStore )) { + MessageDialog msgDialog = new MessageDialog(parentShell, + title, null, msg, MessageDialog.NONE, DialogChoicer.toStringArray(), + DialogChoicer.Cancel.ordinal()); + + int msgResult = msgDialog.open(); + + return DialogChoicer.valueFromOrdinal(msgResult); + } + + return DialogChoicer.Yes; + } + + /** + * Copy file or directory using Eclipse File System + * + * @param source the source URI of the copy. If null, NullPointerException occur. + * @param destination the destination URI of the copy. If null, NullPointerException occur. + * @param options bit-wise or of option flag constants ({@link EFS#OVERWRITE} or {@link EFS#SHALLOW}). + * @param monitor a progress monitor, or <code>null</code> if progress reporting and cancellation are not desired + * + * @author Jihoon Song {@literal <jihoon80.song@samsung.com>} (S-Core) + */ + public static void copy(URI source, URI destination, int options, + IProgressMonitor monitor) throws CoreException { + Assert.notNull(source); + Assert.notNull(destination); + + monitor = ObjectUtil.nvl(monitor, new NullProgressMonitor()); + + try { + monitor.beginTask(defaultTask, defaultTicks); + + getFileSystem().getStore(source).copy(getFileSystem().getStore( destination ), options, monitor); + } finally { + monitor.done(); + } + } + + /** + * Copy file or directory using EFS with IFileStoreFilter. + * If file already exist, open selectable message dialog. + * If the user selected Cancel, OperationCanceledException occur + * + * @param source the source URI of the copy. If null, NullPointerException occur. + * @param destination the destination URI of the copy. If null, NullPointerException occur. + * @param fileFilter the child file filter in source, if <code>null</code>, the default file filter is used + * @param monitor a progress monitor, or <code>null</code> if progress reporting and cancellation are not desired + * @throws CoreException + */ + public static DialogChoicer copyWithFilter(URI source, URI destination, IFileStoreFilter fileFilter, + IProgressMonitor monitor) throws CoreException { + return copyWithFilter(source, destination, fileFilter, DialogChoicer.Yes, monitor); + } + + /** + * Copy file or directory using EFS with IFileStoreFilter. + * If file already exist, open selectable message dialog. + * If the user selected Cancel, OperationCanceledException occur + * + * @param source the source URI of the copy. If null, NullPointerException occur. + * @param destination the destination URI of the copy. If null, NullPointerException occur. + * @param fileFilter the child file filter in source, if <code>null</code>, the default file filter is used + * @param previousChoice apply user configuration. if YesToAll or NoToAll, don't ask your choice. + * @param monitor a progress monitor, or <code>null</code> if progress reporting and cancellation are not desired + * @throws CoreException + */ + public static DialogChoicer copyWithFilter(URI source, URI destination, IFileStoreFilter fileFilter, + DialogChoicer previousChoice, IProgressMonitor monitor) throws CoreException { + Assert.notNull(source); + Assert.notNull(destination); + + monitor = ObjectUtil.nvl(monitor, new NullProgressMonitor()); + + try { + monitor.beginTask(defaultTask, defaultTicks); + + // fileFilter must be not null, because IDEResourceInfoUtils don't check null. + if (fileFilter == null) { + fileFilter = getDefaultFileStoreFilter(); + } + + return internalCopyWithFilter(getFileSystem().getStore( source ), + getFileSystem().getStore( destination ), + fileFilter, previousChoice, monitor); + } finally { + monitor.done(); + } + } + + private static DialogChoicer internalCopyWithFilter(IFileStore source, IFileStore destination, + IFileStoreFilter fileFilter, DialogChoicer previousChoice, IProgressMonitor monitor) + throws CoreException { + Assert.notNull(source); + Assert.notNull(destination); + + previousChoice = isExistResourceWithDialog( + ViewUtil.getWorkbenchWindow().getShell(), Messages.FileExistDialogTitle, + Messages.bind( Messages.FileExistMsg, destination.getName() ), + previousChoice, destination); + + switch (previousChoice) { + case YesToAll : + case Yes : + source.copy(destination, EFS.OVERWRITE | EFS.SHALLOW, monitor); + break; + case Cancel : + throw new OperationCanceledException(); + case NoToAll : + case No : + break; + default: + break; + } + monitor.worked(1); + + // traverse child resources + if (source.fetchInfo().isDirectory()) { + IFileStore[] childStores = IDEResourceInfoUtils.listFileStores(source, fileFilter, null); + for (IFileStore childStore : childStores) { + previousChoice = internalCopyWithFilter(childStore, + destination.getChild( childStore.getName() ), + fileFilter, previousChoice, monitor); + } + } + + return previousChoice; + } +} |