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.editors.layout.gle2.RenderPreviewMode; 22 import com.android.ide.eclipse.adt.internal.wizards.newxmlfile.AddTranslationDialog; 23 24 import org.eclipse.core.resources.IProject; 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.Image; 29 import org.eclipse.swt.graphics.Point; 30 import org.eclipse.swt.graphics.Rectangle; 31 import org.eclipse.swt.widgets.Menu; 32 import org.eclipse.swt.widgets.MenuItem; 33 import org.eclipse.swt.widgets.Shell; 34 import org.eclipse.swt.widgets.ToolItem; 35 36 import java.util.List; 37 38 /** 39 * The {@linkplain LocaleMenuListener} class is responsible for generating the locale 40 * menu in the {@link ConfigurationChooser}. 41 */ 42 class LocaleMenuListener extends SelectionAdapter { 43 private static final int ACTION_SET_LOCALE = 1; 44 private static final int ACTION_ADD_TRANSLATION = 2; 45 46 private final ConfigurationChooser mConfigChooser; 47 private final int mAction; 48 private final Locale mLocale; 49 50 LocaleMenuListener( 51 @NonNull ConfigurationChooser configChooser, 52 int action, 53 @Nullable Locale locale) { 54 mConfigChooser = configChooser; 55 mAction = action; 56 mLocale = locale; 57 } 58 59 @Override 60 public void widgetSelected(SelectionEvent e) { 61 switch (mAction) { 62 case ACTION_SET_LOCALE: { 63 mConfigChooser.selectLocale(mLocale); 64 mConfigChooser.onLocaleChange(); 65 break; 66 } 67 case ACTION_ADD_TRANSLATION: { 68 IProject project = mConfigChooser.getProject(); 69 Shell shell = mConfigChooser.getShell(); 70 AddTranslationDialog dialog = new AddTranslationDialog(shell, project); 71 dialog.open(); 72 break; 73 } 74 default: assert false : mAction; 75 } 76 } 77 78 static void show(final ConfigurationChooser chooser, ToolItem combo) { 79 Menu menu = new Menu(chooser.getShell(), SWT.POP_UP); 80 Configuration configuration = chooser.getConfiguration(); 81 List<Locale> locales = chooser.getLocaleList(); 82 Locale current = configuration.getLocale(); 83 84 for (Locale locale : locales) { 85 String title = ConfigurationChooser.getLocaleLabel(chooser, locale, false); 86 MenuItem item = new MenuItem(menu, SWT.CHECK); 87 item.setText(title); 88 Image image = locale.getFlagImage(); 89 item.setImage(image); 90 91 boolean selected = current == locale; 92 if (selected) { 93 item.setSelection(true); 94 } 95 96 LocaleMenuListener listener = new LocaleMenuListener(chooser, ACTION_SET_LOCALE, 97 locale); 98 item.addSelectionListener(listener); 99 } 100 101 if (locales.size() > 1) { 102 @SuppressWarnings("unused") 103 MenuItem separator = new MenuItem(menu, SWT.SEPARATOR); 104 105 ConfigurationMenuListener.addTogglePreviewModeAction(menu, 106 "Preview All Locales", chooser, RenderPreviewMode.LOCALES); 107 } 108 109 @SuppressWarnings("unused") 110 MenuItem separator = new MenuItem(menu, SWT.SEPARATOR); 111 112 MenuItem item = new MenuItem(menu, SWT.PUSH); 113 item.setText("Add New Translation..."); 114 LocaleMenuListener listener = new LocaleMenuListener(chooser, 115 ACTION_ADD_TRANSLATION, null); 116 item.addSelectionListener(listener); 117 118 Rectangle bounds = combo.getBounds(); 119 Point location = new Point(bounds.x, bounds.y + bounds.height); 120 location = combo.getParent().toDisplay(location); 121 menu.setLocation(location.x, location.y); 122 menu.setVisible(true); 123 } 124 } 125