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 */
16package com.android.ide.eclipse.adt.internal.editors.formatting;
17
18import com.android.ide.eclipse.adt.internal.build.AaptQuickFix;
19import com.android.ide.eclipse.adt.internal.editors.layout.refactoring.RefactoringAssistant;
20import com.android.ide.eclipse.adt.internal.lint.LintFixGenerator;
21
22import org.eclipse.jface.text.contentassist.ICompletionProposal;
23import org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext;
24import org.eclipse.jface.text.quickassist.IQuickAssistProcessor;
25import org.eclipse.jface.text.source.Annotation;
26
27import java.util.ArrayList;
28import java.util.List;
29
30/**
31 * This class implements Quick Assists for XML files. It does not perform any
32 * quick assistance on its own, but it coordinates the various separate quick
33 * assists available for XML such that the order is logical. This is necessary
34 * because without it, the order of suggestions (when more than one assistant
35 * provides suggestions) is not always optimal. There doesn't seem to be a way
36 * from non-Java languages to set the sorting order (see
37 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=229983 ). So instead of
38 * registering our multiple XML quick assistants via the plugin.xml file, we
39 * register <b>just</b> this manager, which delegates to the various XML quick
40 * assistants as appropriate.
41 */
42public class XmlQuickAssistManager implements IQuickAssistProcessor {
43    private final IQuickAssistProcessor[] mProcessors;
44
45    /** Constructs a new {@link XmlQuickAssistManager} which orders the quick fixes */
46    public XmlQuickAssistManager() {
47        mProcessors = new IQuickAssistProcessor[] {
48                new AaptQuickFix(),
49                new LintFixGenerator(),
50                new RefactoringAssistant()
51        };
52    }
53
54    @Override
55    public String getErrorMessage() {
56        return null;
57    }
58
59    @Override
60    public boolean canFix(Annotation annotation) {
61        for (IQuickAssistProcessor processor : mProcessors) {
62            if (processor.canFix(annotation)) {
63                return true;
64            }
65        }
66
67        return false;
68    }
69
70    @Override
71    public boolean canAssist(IQuickAssistInvocationContext invocationContext) {
72        for (IQuickAssistProcessor processor : mProcessors) {
73            if (processor.canAssist(invocationContext)) {
74                return true;
75            }
76        }
77
78        return false;
79    }
80
81    @Override
82    public ICompletionProposal[] computeQuickAssistProposals(
83            IQuickAssistInvocationContext invocationContext) {
84        List<ICompletionProposal> allProposals = null;
85        for (IQuickAssistProcessor processor : mProcessors) {
86            if (processor.canAssist(invocationContext)) {
87                ICompletionProposal[] proposals =
88                        processor.computeQuickAssistProposals(invocationContext);
89                if (proposals != null && proposals.length > 0) {
90                    if (allProposals == null) {
91                        allProposals = new ArrayList<ICompletionProposal>();
92                    }
93                    for (ICompletionProposal proposal : proposals) {
94                        allProposals.add(proposal);
95                    }
96                }
97            }
98        }
99
100        if (allProposals != null) {
101            return allProposals.toArray(new ICompletionProposal[allProposals.size()]);
102        }
103
104        return null;
105    }
106}
107