1 /* 2 * Copyright (C) 2012 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 17 package com.android.ide.eclipse.adt.internal.editors.layout.configuration; 18 19 import com.android.annotations.NonNull; 20 import com.android.annotations.Nullable; 21 import com.android.ide.eclipse.adt.AdtPlugin; 22 import com.android.ide.eclipse.adt.internal.editors.manifest.ManifestInfo; 23 import com.android.ide.eclipse.adt.internal.resources.ResourceHelper; 24 25 import org.eclipse.core.resources.IProject; 26 import org.eclipse.jdt.ui.ISharedImages; 27 import org.eclipse.jdt.ui.JavaUI; 28 import org.eclipse.swt.SWT; 29 import org.eclipse.swt.events.SelectionAdapter; 30 import org.eclipse.swt.events.SelectionEvent; 31 import org.eclipse.swt.graphics.Image; 32 import org.eclipse.swt.graphics.Point; 33 import org.eclipse.swt.graphics.Rectangle; 34 import org.eclipse.swt.widgets.Menu; 35 import org.eclipse.swt.widgets.MenuItem; 36 import org.eclipse.swt.widgets.ToolItem; 37 38 import java.util.ArrayList; 39 import java.util.HashSet; 40 import java.util.List; 41 import java.util.Set; 42 43 /** 44 * The {@linkplain ActivityMenuListener} class is responsible for 45 * generating the activity menu in the {@link ConfigurationChooser}. 46 */ 47 class ActivityMenuListener extends SelectionAdapter { 48 private static final int ACTION_OPEN_ACTIVITY = 1; 49 private static final int ACTION_SELECT_ACTIVITY = 2; 50 51 private final ConfigurationChooser mConfigChooser; 52 private final int mAction; 53 private final String mFqcn; 54 55 ActivityMenuListener( 56 @NonNull ConfigurationChooser configChooser, 57 int action, 58 @Nullable String fqcn) { 59 mConfigChooser = configChooser; 60 mAction = action; 61 mFqcn = fqcn; 62 } 63 64 @Override 65 public void widgetSelected(SelectionEvent e) { 66 switch (mAction) { 67 case ACTION_OPEN_ACTIVITY: { 68 Configuration configuration = mConfigChooser.getConfiguration(); 69 String fqcn = configuration.getActivity(); 70 AdtPlugin.openJavaClass(mConfigChooser.getProject(), fqcn); 71 break; 72 } 73 case ACTION_SELECT_ACTIVITY: { 74 mConfigChooser.selectActivity(mFqcn); 75 mConfigChooser.onSelectActivity(); 76 break; 77 } 78 default: assert false : mAction; 79 } 80 } 81 82 static void show(ConfigurationChooser chooser, ToolItem combo) { 83 // TODO: Allow using fragments here as well? 84 Menu menu = new Menu(chooser.getShell(), SWT.POP_UP); 85 ISharedImages sharedImages = JavaUI.getSharedImages(); 86 Configuration configuration = chooser.getConfiguration(); 87 String current = configuration.getActivity(); 88 89 if (current != null) { 90 MenuItem item = new MenuItem(menu, SWT.PUSH); 91 String label = ConfigurationChooser.getActivityLabel(current, true);; 92 item.setText( String.format("Open %1$s...", label)); 93 Image image = sharedImages.getImage(ISharedImages.IMG_OBJS_CUNIT); 94 item.setImage(image); 95 item.addSelectionListener( 96 new ActivityMenuListener(chooser, ACTION_OPEN_ACTIVITY, null)); 97 98 @SuppressWarnings("unused") 99 MenuItem separator = new MenuItem(menu, SWT.SEPARATOR); 100 } 101 102 IProject project = chooser.getProject(); 103 Image image = sharedImages.getImage(ISharedImages.IMG_OBJS_CLASS); 104 105 // Add activities found to be relevant to this layout 106 String layoutName = ResourceHelper.getLayoutName(chooser.getEditedFile()); 107 String pkg = ManifestInfo.get(project).getPackage(); 108 List<String> preferred = ManifestInfo.guessActivities(project, layoutName, pkg); 109 current = addActivities(chooser, menu, current, image, preferred); 110 111 // Add all activities 112 List<String> activities = ManifestInfo.getProjectActivities(project); 113 if (preferred.size() > 0) { 114 // Filter out the activities we've already listed above 115 List<String> filtered = new ArrayList<String>(activities.size()); 116 Set<String> remove = new HashSet<String>(preferred); 117 for (String fqcn : activities) { 118 if (!remove.contains(fqcn)) { 119 filtered.add(fqcn); 120 } 121 } 122 activities = filtered; 123 } 124 125 if (activities.size() > 0) { 126 if (preferred.size() > 0) { 127 @SuppressWarnings("unused") 128 MenuItem separator = new MenuItem(menu, SWT.SEPARATOR); 129 } 130 131 addActivities(chooser, menu, current, image, activities); 132 } 133 134 Rectangle bounds = combo.getBounds(); 135 Point location = new Point(bounds.x, bounds.y + bounds.height); 136 location = combo.getParent().toDisplay(location); 137 menu.setLocation(location.x, location.y); 138 menu.setVisible(true); 139 } 140 141 private static String addActivities(ConfigurationChooser chooser, Menu menu, String current, 142 Image image, List<String> activities) { 143 for (final String fqcn : activities) { 144 String title = ConfigurationChooser.getActivityLabel(fqcn, false); 145 MenuItem item = new MenuItem(menu, SWT.CHECK); 146 item.setText(title); 147 item.setImage(image); 148 149 boolean selected = title.equals(current); 150 if (selected) { 151 item.setSelection(true); 152 current = null; // Only show the first occurrence as selected 153 // such that we don't show it selected again in the full activity list 154 } 155 156 item.addSelectionListener(new ActivityMenuListener(chooser, 157 ACTION_OPEN_ACTIVITY, fqcn)); 158 } 159 160 return current; 161 } 162 } 163