1 
2 package com.android.ide.eclipse.adt.internal.editors.layout.gle2;
3 
4 import com.android.ide.common.rendering.api.Capability;
5 import com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditorDelegate;
6 import com.android.ide.eclipse.adt.internal.editors.layout.gle2.IncludeFinder.Reference;
7 
8 import org.eclipse.core.resources.IFile;
9 import org.eclipse.core.resources.IProject;
10 import org.eclipse.jface.action.Action;
11 import org.eclipse.jface.action.ActionContributionItem;
12 import org.eclipse.jface.action.IAction;
13 import org.eclipse.jface.action.Separator;
14 import org.eclipse.swt.widgets.Menu;
15 
16 import java.util.List;
17 
18 /**
19  * Action which creates a submenu for the "Show Included In" action
20  */
21 class ShowWithinMenu extends SubmenuAction {
22     private LayoutEditorDelegate mEditorDelegate;
23 
ShowWithinMenu(LayoutEditorDelegate editorDelegate)24     ShowWithinMenu(LayoutEditorDelegate editorDelegate) {
25         super("Show Included In");
26         mEditorDelegate = editorDelegate;
27     }
28 
29     @Override
addMenuItems(Menu menu)30     protected void addMenuItems(Menu menu) {
31         GraphicalEditorPart graphicalEditor = mEditorDelegate.getGraphicalEditor();
32         IFile file = graphicalEditor.getEditedFile();
33         if (graphicalEditor.renderingSupports(Capability.EMBEDDED_LAYOUT)) {
34             IProject project = file.getProject();
35             IncludeFinder finder = IncludeFinder.get(project);
36             final List<Reference> includedBy = finder.getIncludedBy(file);
37 
38             if (includedBy != null && includedBy.size() > 0) {
39                 for (final Reference reference : includedBy) {
40                     String title = reference.getDisplayName();
41                     IAction action = new ShowWithinAction(title, reference);
42                     new ActionContributionItem(action).fill(menu, -1);
43                 }
44                 new Separator().fill(menu, -1);
45             }
46             IAction action = new ShowWithinAction("Nothing", null);
47             if (includedBy == null || includedBy.size() == 0) {
48                 action.setEnabled(false);
49             }
50             new ActionContributionItem(action).fill(menu, -1);
51         } else {
52             addDisabledMessageItem("Not supported on platform");
53         }
54     }
55 
56     /** Action to select one particular include-context */
57     private class ShowWithinAction extends Action {
58         private Reference mReference;
59 
ShowWithinAction(String title, Reference reference)60         public ShowWithinAction(String title, Reference reference) {
61             super(title, IAction.AS_RADIO_BUTTON);
62             mReference = reference;
63         }
64 
65         @Override
isChecked()66         public boolean isChecked() {
67             Reference within = mEditorDelegate.getGraphicalEditor().getIncludedWithin();
68             if (within == null) {
69                 return mReference == null;
70             } else {
71                 return within.equals(mReference);
72             }
73         }
74 
75         @Override
run()76         public void run() {
77             if (!isChecked()) {
78                 mEditorDelegate.getGraphicalEditor().showIn(mReference);
79             }
80         }
81     }
82 }
83