org.symbian.tools.wrttools.previewer/src/org/symbian/tools/tmw/previewer/internal/PreviewerUtil.java
changeset 468 a05c6e5cc7d9
parent 308 c521df56b15d
equal deleted inserted replaced
467:5a2901872fcf 468:a05c6e5cc7d9
       
     1 /**
       
     2  * Copyright (c) 2010 Symbian Foundation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Symbian Foundation - initial contribution.
       
    11  * Contributors:
       
    12  * Description:
       
    13  * Overview:
       
    14  * Details:
       
    15  * Platforms/Drives/Compatibility:
       
    16  * Assumptions/Requirement/Pre-requisites:
       
    17  * Failures and causes:
       
    18  */
       
    19 package org.symbian.tools.tmw.previewer.internal;
       
    20 
       
    21 import java.util.Collection;
       
    22 import java.util.HashMap;
       
    23 import java.util.HashSet;
       
    24 import java.util.Map;
       
    25 
       
    26 import org.eclipse.core.resources.IFile;
       
    27 import org.eclipse.core.resources.IProject;
       
    28 import org.eclipse.core.resources.IResource;
       
    29 import org.eclipse.core.resources.IResourceDelta;
       
    30 import org.eclipse.core.resources.IResourceDeltaVisitor;
       
    31 import org.eclipse.core.runtime.CoreException;
       
    32 import org.eclipse.core.runtime.IPath;
       
    33 import org.symbian.tools.tmw.previewer.core.IApplicationLayoutProvider;
       
    34 import org.symbian.tools.wrttools.previewer.PreviewerPlugin;
       
    35 
       
    36 public class PreviewerUtil {
       
    37     public static final class ChangedResourcesCollector implements IResourceDeltaVisitor {
       
    38         public final Collection<IFile> files = new HashSet<IFile>();
       
    39         public final Collection<IProject> deleted = new HashSet<IProject>();
       
    40         public final Map<IProject, IPath> renamed = new HashMap<IProject, IPath>();
       
    41 
       
    42         public boolean visit(IResourceDelta delta) throws CoreException {
       
    43             IResource resource = delta.getResource();
       
    44             switch (resource.getType()) {
       
    45             case IResource.PROJECT:
       
    46                 if (delta.getKind() == IResourceDelta.REMOVED) {
       
    47                     if ((delta.getFlags() & IResourceDelta.MOVED_TO) != 0) {
       
    48                         renamed.put(resource.getProject(), delta.getMovedToPath());
       
    49                     } else {
       
    50                         deleted.add((IProject) resource);
       
    51                     }
       
    52                     return false;
       
    53                 }
       
    54                 break;
       
    55             case IResource.FILE:
       
    56                 IApplicationLayoutProvider layoutProvider = PreviewerPlugin.getExtensionsManager().getLayoutProvider(
       
    57                         resource.getProject());
       
    58                 if (layoutProvider != null) {
       
    59                     if (layoutProvider.getResourcePath((IFile) resource) != null) {
       
    60                         final boolean kind = delta.getKind() == IResourceDelta.ADDED
       
    61                                 | delta.getKind() == IResourceDelta.REMOVED;
       
    62                         final boolean flag = (delta.getFlags() & (IResourceDelta.CONTENT | IResourceDelta.ENCODING
       
    63                                 | IResourceDelta.LOCAL_CHANGED | IResourceDelta.REPLACED | IResourceDelta.SYNC)) != 0;
       
    64                         if (kind || flag) {
       
    65                             files.add((IFile) resource);
       
    66                         }
       
    67                     }
       
    68                 }
       
    69                 break;
       
    70             }
       
    71             return true;
       
    72         }
       
    73     }
       
    74 
       
    75     public static boolean isRelevantResource(IResource resource) {
       
    76         if (resource.exists() && resource.getType() == IResource.FILE) {
       
    77             return !resource.getFullPath().segment(1).equalsIgnoreCase("preview")
       
    78                     && !"wgz".equalsIgnoreCase(resource.getFileExtension())
       
    79                     && !(PreviewerPlugin.PLUGIN_ID + ".xml").equals(resource.getName());
       
    80         }
       
    81         return false;
       
    82     }
       
    83 
       
    84     public static IFile[] getWebChanges(final IResourceDelta delta) {
       
    85         ChangedResourcesCollector visitor = collectResourceChanges(delta);
       
    86         final Collection<IFile> files = visitor.files;
       
    87         return files.toArray(new IFile[files.size()]);
       
    88     }
       
    89 
       
    90     public static ChangedResourcesCollector collectResourceChanges(final IResourceDelta delta) {
       
    91         ChangedResourcesCollector visitor = new ChangedResourcesCollector();
       
    92         try {
       
    93             delta.accept(visitor);
       
    94         } catch (CoreException e) {
       
    95             PreviewerPlugin.log(e);
       
    96         }
       
    97         return visitor;
       
    98     }
       
    99 }