2 * Copyright (C) 2009 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.sdkuilib.internal.widgets;
19 import com.android.sdkuilib.ui.GridDialog;
21 import org.eclipse.jface.dialogs.IDialogConstants;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.SelectionAdapter;
24 import org.eclipse.swt.events.SelectionEvent;
25 import org.eclipse.swt.graphics.Rectangle;
26 import org.eclipse.swt.widgets.Button;
27 import org.eclipse.swt.widgets.Combo;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Display;
31 import org.eclipse.swt.widgets.Label;
32 import org.eclipse.swt.widgets.Monitor;
33 import org.eclipse.swt.widgets.Shell;
36 * Small dialog to let a user choose a screen size (from a fixed list) and a resolution
37 * (as returned by {@link Display#getMonitors()}).
39 * After the dialog as returned, one can query {@link #getDensity()} to get the chosen monitor
42 class ResolutionChooserDialog extends GridDialog {
43 public final static float[] MONITOR_SIZES = new float[] {
44 13.3f, 14, 15.4f, 15.6f, 17, 19, 20, 21, 24, 30,
47 private Button mButton;
48 private Combo mScreenSizeCombo;
49 private Combo mMonitorCombo;
51 private Monitor[] mMonitors;
52 private int mScreenSizeIndex = -1;
53 private int mMonitorIndex = 0;
55 ResolutionChooserDialog(Shell parentShell) {
56 super(parentShell, 2, false);
60 * Returns the pixel density of the user-chosen monitor.
63 float size = MONITOR_SIZES[mScreenSizeIndex];
64 Rectangle rect = mMonitors[mMonitorIndex].getBounds();
66 // compute the density
67 double d = Math.sqrt(rect.width * rect.width + rect.height * rect.height) / size;
68 return (int)Math.round(d);
72 protected void configureShell(Shell newShell) {
73 newShell.setText("Monitor Density");
74 super.configureShell(newShell);
78 protected Control createContents(Composite parent) {
79 Control control = super.createContents(parent);
80 mButton = getButton(IDialogConstants.OK_ID);
81 mButton.setEnabled(false);
86 public void createDialogContent(Composite parent) {
87 Label l = new Label(parent, SWT.NONE);
88 l.setText("Screen Size:");
90 mScreenSizeCombo = new Combo(parent, SWT.DROP_DOWN | SWT.READ_ONLY);
91 for (float size : MONITOR_SIZES) {
92 if (Math.round(size) == size) {
93 mScreenSizeCombo.add(String.format("%.0f\"", size));
95 mScreenSizeCombo.add(String.format("%.1f\"", size));
98 mScreenSizeCombo.addSelectionListener(new SelectionAdapter() {
100 public void widgetSelected(SelectionEvent arg0) {
101 mScreenSizeIndex = mScreenSizeCombo.getSelectionIndex();
102 mButton.setEnabled(mScreenSizeIndex != -1);
106 l = new Label(parent, SWT.NONE);
107 l.setText("Resolution:");
109 mMonitorCombo = new Combo(parent, SWT.DROP_DOWN | SWT.READ_ONLY);
110 mMonitors = parent.getDisplay().getMonitors();
111 for (Monitor m : mMonitors) {
112 Rectangle r = m.getBounds();
113 mMonitorCombo.add(String.format("%d x %d", r.width, r.height));
115 mMonitorCombo.select(mMonitorIndex);
116 mMonitorCombo.addSelectionListener(new SelectionAdapter() {
118 public void widgetDefaultSelected(SelectionEvent arg0) {
119 mMonitorIndex = mMonitorCombo.getSelectionIndex();