aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/resources/CyclicDependencyValidator.java
blob: a2b13c6a86b932e79a04a3011005ab5bca1e96e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
 *
 * 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.
 */
package com.android.ide.eclipse.adt.internal.resources;

import com.android.annotations.Nullable;
import com.android.ide.eclipse.adt.internal.editors.layout.gle2.IncludeFinder;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.dialogs.IInputValidator;

import java.util.Collection;

/** A validator which checks for cyclic dependencies */
public class CyclicDependencyValidator implements IInputValidator {
    private final Collection<String> mInvalidIds;

    private CyclicDependencyValidator(Collection<String> invalid) {
        this.mInvalidIds = invalid;
    }

    @Override
    public String isValid(String newText) {
        if (mInvalidIds.contains(newText)) {
            return "Cyclic include, not valid";
        }
        return null;
    }

    /**
     * Creates a validator which ensures that the chosen id is not for a layout that is
     * directly or indirectly including the given layout. Used to avoid cyclic
     * dependencies when offering layouts to be included within a given file, etc.
     *
     * @param file the target file that candidate layouts should not directly or
     *            indirectly include
     * @return a validator which checks whether resource ids are valid or whether they
     *         could result in a cyclic dependency
     */
    @Nullable
    public static IInputValidator create(@Nullable IFile file) {
        if (file == null) {
            return null;
        }

        IProject project = file.getProject();
        IncludeFinder includeFinder = IncludeFinder.get(project);
        final Collection<String> invalid =
            includeFinder.getInvalidIncludes(file);

        return new CyclicDependencyValidator(invalid);
    }
}