Home | History | Annotate | Download | only in resources
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.eclipse.org/org/documents/epl-v10.php
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.ide.eclipse.adt.internal.resources;
     17 
     18 import com.android.annotations.Nullable;
     19 import com.android.ide.eclipse.adt.internal.editors.layout.gle2.IncludeFinder;
     20 
     21 import org.eclipse.core.resources.IFile;
     22 import org.eclipse.core.resources.IProject;
     23 import org.eclipse.jface.dialogs.IInputValidator;
     24 
     25 import java.util.Collection;
     26 
     27 /** A validator which checks for cyclic dependencies */
     28 public class CyclicDependencyValidator implements IInputValidator {
     29     private final Collection<String> mInvalidIds;
     30 
     31     private CyclicDependencyValidator(Collection<String> invalid) {
     32         this.mInvalidIds = invalid;
     33     }
     34 
     35     @Override
     36     public String isValid(String newText) {
     37         if (mInvalidIds.contains(newText)) {
     38             return "Cyclic include, not valid";
     39         }
     40         return null;
     41     }
     42 
     43     /**
     44      * Creates a validator which ensures that the chosen id is not for a layout that is
     45      * directly or indirectly including the given layout. Used to avoid cyclic
     46      * dependencies when offering layouts to be included within a given file, etc.
     47      *
     48      * @param file the target file that candidate layouts should not directly or
     49      *            indirectly include
     50      * @return a validator which checks whether resource ids are valid or whether they
     51      *         could result in a cyclic dependency
     52      */
     53     @Nullable
     54     public static IInputValidator create(@Nullable IFile file) {
     55         if (file == null) {
     56             return null;
     57         }
     58 
     59         IProject project = file.getProject();
     60         IncludeFinder includeFinder = IncludeFinder.get(project);
     61         final Collection<String> invalid =
     62             includeFinder.getInvalidIncludes(file);
     63 
     64         return new CyclicDependencyValidator(invalid);
     65     }
     66 }
     67