Home | History | Annotate | Download | only in configuration
      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.ide.eclipse.adt.internal.editors.layout.gle2.SubmenuAction;
     20 import com.android.resources.NightMode;
     21 import com.android.resources.ScreenOrientation;
     22 import com.android.resources.UiMode;
     23 import com.android.sdklib.devices.Device;
     24 import com.android.sdklib.devices.State;
     25 
     26 import org.eclipse.jface.action.Action;
     27 import org.eclipse.jface.action.ActionContributionItem;
     28 import org.eclipse.jface.action.IAction;
     29 import org.eclipse.jface.action.MenuManager;
     30 import org.eclipse.jface.action.Separator;
     31 import org.eclipse.swt.graphics.Point;
     32 import org.eclipse.swt.graphics.Rectangle;
     33 import org.eclipse.swt.widgets.Menu;
     34 import org.eclipse.swt.widgets.ToolItem;
     35 
     36 import java.util.List;
     37 
     38 /**
     39  * Action which creates a submenu that shows the available orientations as well
     40  * as some related options for night mode and dock mode
     41  */
     42 class OrientationMenuAction extends SubmenuAction {
     43     // Constants used to indicate what type of menu is being shown, such that
     44     // the submenus can lazily construct their contents
     45     private static final int MENU_NIGHTMODE = 1;
     46     private static final int MENU_UIMODE = 2;
     47 
     48     private final ConfigurationChooser mConfigChooser;
     49     /** Type of menu; one of the constants {@link #MENU_NIGHTMODE} etc */
     50     private final int mType;
     51 
     52     OrientationMenuAction(int type, String title, ConfigurationChooser configuration) {
     53         super(title);
     54         mType = type;
     55         mConfigChooser = configuration;
     56     }
     57 
     58     static void showMenu(ConfigurationChooser configChooser, ToolItem combo) {
     59         MenuManager manager = new MenuManager();
     60 
     61         // Show toggles for all the available states
     62 
     63         Configuration configuration = configChooser.getConfiguration();
     64         Device device = configuration.getDevice();
     65         State current = configuration.getDeviceState();
     66         if (device != null) {
     67             List<State> states = device.getAllStates();
     68 
     69             if (states.size() > 1 && current != null) {
     70                 State flip = configuration.getNextDeviceState(current);
     71                 String flipName = flip != null ? flip.getName() : current.getName();
     72                 manager.add(new DeviceConfigAction(configChooser,
     73                         String.format("Switch to %1$s", flipName), flip, false, true));
     74                 manager.add(new Separator());
     75             }
     76 
     77             for (State config : states) {
     78                 manager.add(new DeviceConfigAction(configChooser, config.getName(),
     79                         config, config == current, false));
     80             }
     81             manager.add(new Separator());
     82         }
     83         manager.add(new OrientationMenuAction(MENU_UIMODE, "UI Mode", configChooser));
     84         manager.add(new Separator());
     85         manager.add(new OrientationMenuAction(MENU_NIGHTMODE, "Night Mode", configChooser));
     86 
     87         Menu menu = manager.createContextMenu(configChooser.getShell());
     88         Rectangle bounds = combo.getBounds();
     89         Point location = new Point(bounds.x, bounds.y + bounds.height);
     90         location = combo.getParent().toDisplay(location);
     91         menu.setLocation(location.x, location.y);
     92         menu.setVisible(true);
     93     }
     94 
     95     @Override
     96     protected void addMenuItems(Menu menu) {
     97         switch (mType) {
     98             case MENU_NIGHTMODE: {
     99                 NightMode selected = mConfigChooser.getConfiguration().getNightMode();
    100                 for (NightMode mode : NightMode.values()) {
    101                     boolean checked = mode == selected;
    102                     SelectNightModeAction action = new SelectNightModeAction(mode, checked);
    103                     new ActionContributionItem(action).fill(menu, -1);
    104 
    105                 }
    106                 break;
    107             }
    108             case MENU_UIMODE: {
    109                 UiMode selected = mConfigChooser.getConfiguration().getUiMode();
    110                 for (UiMode mode : UiMode.values()) {
    111                     boolean checked = mode == selected;
    112                     SelectUiModeAction action = new SelectUiModeAction(mode, checked);
    113                     new ActionContributionItem(action).fill(menu, -1);
    114                 }
    115                 break;
    116             }
    117         }
    118     }
    119 
    120 
    121     private class SelectNightModeAction extends Action {
    122         private final NightMode mMode;
    123 
    124         private SelectNightModeAction(NightMode mode, boolean checked) {
    125             super(mode.getLongDisplayValue(), IAction.AS_RADIO_BUTTON);
    126             mMode = mode;
    127             if (checked) {
    128                 setChecked(true);
    129             }
    130         }
    131 
    132         @Override
    133         public void run() {
    134             Configuration configuration = mConfigChooser.getConfiguration();
    135             configuration.setNightMode(mMode, false);
    136             mConfigChooser.notifyFolderConfigChanged();
    137         }
    138     }
    139 
    140     private class SelectUiModeAction extends Action {
    141         private final UiMode mMode;
    142 
    143         private SelectUiModeAction(UiMode mode, boolean checked) {
    144             super(mode.getLongDisplayValue(), IAction.AS_RADIO_BUTTON);
    145             mMode = mode;
    146             if (checked) {
    147                 setChecked(true);
    148             }
    149         }
    150 
    151         @Override
    152         public void run() {
    153             Configuration configuration = mConfigChooser.getConfiguration();
    154             configuration.setUiMode(mMode, false);
    155         }
    156     }
    157 
    158     private static class DeviceConfigAction extends Action {
    159         private final ConfigurationChooser mConfiguration;
    160         private final State mState;
    161 
    162         private DeviceConfigAction(ConfigurationChooser configuration, String title,
    163                 State state, boolean checked, boolean flip) {
    164             super(title, IAction.AS_RADIO_BUTTON);
    165             mConfiguration = configuration;
    166             mState = state;
    167             if (checked) {
    168                 setChecked(true);
    169             }
    170             ScreenOrientation orientation = configuration.getOrientation(state);
    171             setImageDescriptor(configuration.getOrientationImage(orientation, flip));
    172         }
    173 
    174         @Override
    175         public void run() {
    176             mConfiguration.selectDeviceState(mState);
    177             mConfiguration.onDeviceConfigChange();
    178         }
    179     }
    180 }
    181