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.internal.preferences.AdtPrefs;
22 import com.android.sdklib.AndroidVersion;
23 import com.android.sdklib.IAndroidTarget;
24 
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.graphics.Point;
29 import org.eclipse.swt.graphics.Rectangle;
30 import org.eclipse.swt.widgets.Menu;
31 import org.eclipse.swt.widgets.MenuItem;
32 import org.eclipse.swt.widgets.ToolItem;
33 
34 import java.util.List;
35 import java.util.RandomAccess;
36 
37 /**
38  * The {@linkplain TargetMenuListener} class is responsible for
39  * generating the rendering target menu in the {@link ConfigurationChooser}.
40  */
41 class TargetMenuListener extends SelectionAdapter {
42     private final ConfigurationChooser mConfigChooser;
43     private final IAndroidTarget mTarget;
44     private final boolean mPickBest;
45 
TargetMenuListener( @onNull ConfigurationChooser configChooser, @Nullable IAndroidTarget target, boolean pickBest)46     TargetMenuListener(
47             @NonNull ConfigurationChooser configChooser,
48             @Nullable IAndroidTarget target,
49             boolean pickBest) {
50         mConfigChooser = configChooser;
51         mTarget = target;
52         mPickBest = pickBest;
53     }
54 
55     @Override
widgetSelected(SelectionEvent e)56     public void widgetSelected(SelectionEvent e) {
57         IAndroidTarget target = mTarget;
58         AdtPrefs prefs = AdtPrefs.getPrefs();
59         if (mPickBest) {
60             boolean autoPick = prefs.isAutoPickRenderTarget();
61             autoPick = !autoPick;
62             prefs.setAutoPickRenderTarget(autoPick);
63             if (autoPick) {
64                 target = ConfigurationMatcher.findDefaultRenderTarget(mConfigChooser);
65             } else {
66                 // Turn it off, but keep current target until another one is chosen
67                 return;
68             }
69         } else {
70             // Manually picked some other target: turn off auto-pick
71             prefs.setAutoPickRenderTarget(false);
72         }
73         mConfigChooser.selectTarget(target);
74         mConfigChooser.onRenderingTargetChange();
75     }
76 
show(ConfigurationChooser chooser, ToolItem combo)77     static void show(ConfigurationChooser chooser, ToolItem combo) {
78         Menu menu = new Menu(chooser.getShell(), SWT.POP_UP);
79         Configuration configuration = chooser.getConfiguration();
80         IAndroidTarget current = configuration.getTarget();
81         List<IAndroidTarget> targets = chooser.getTargetList();
82         boolean haveRecent = false;
83 
84         MenuItem menuItem = new MenuItem(menu, SWT.CHECK);
85         menuItem.setText("Automatically Pick Best");
86         menuItem.addSelectionListener(new TargetMenuListener(chooser, null, true));
87         if (AdtPrefs.getPrefs().isAutoPickRenderTarget()) {
88             menuItem.setSelection(true);
89         }
90 
91         @SuppressWarnings("unused")
92         MenuItem separator = new MenuItem(menu, SWT.SEPARATOR);
93 
94         // Process in reverse order: most important targets first
95         assert targets instanceof RandomAccess;
96         for (int i = targets.size() - 1; i >= 0; i--) {
97             IAndroidTarget target = targets.get(i);
98 
99             AndroidVersion version = target.getVersion();
100             if (version.getApiLevel() >= 7) {
101                 haveRecent = true;
102             } else if (haveRecent) {
103                 // Don't show ancient rendering targets; they're pretty broken
104                 // (unless of course all you have are ancient targets)
105                 break;
106             }
107 
108             String title = ConfigurationChooser.getRenderingTargetLabel(target, false);
109             MenuItem item = new MenuItem(menu, SWT.CHECK);
110             item.setText(title);
111 
112             boolean selected = current == target;
113             if (selected) {
114                 item.setSelection(true);
115             }
116 
117             item.addSelectionListener(new TargetMenuListener(chooser, target, false));
118         }
119 
120         Rectangle bounds = combo.getBounds();
121         Point location = new Point(bounds.x, bounds.y + bounds.height);
122         location = combo.getParent().toDisplay(location);
123         menu.setLocation(location.x, location.y);
124         menu.setVisible(true);
125     }
126 }
127