Monintor resolution/size chooser dialog

This simple dialog lets the user choose a screen size (from a fixed list) and
a resolution (as returned by SWT's Display class).

This is used by the AVD start options dialog to help users figure out
the pixel density of their monitor.

Change-Id: I3eec0e7fac850c82e836d2bc0291a491f29f516e
This commit is contained in:
Xavier Ducrohet
2009-10-02 14:30:43 -07:00
parent d13d440d43
commit d280d2363e
2 changed files with 160 additions and 4 deletions

View File

@@ -22,6 +22,7 @@ import com.android.sdkuilib.internal.repository.SettingsController;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
@@ -83,7 +84,7 @@ final class AvdStartDialog extends Dialog {
private String mSkinDisplay;
private boolean mEnableScaling = true;
protected AvdStartDialog(Shell parentShell, AvdInfo avd, String sdkLocation,
AvdStartDialog(Shell parentShell, AvdInfo avd, String sdkLocation,
SettingsController settingsController) {
super(parentShell);
mAvd = avd;
@@ -111,7 +112,7 @@ final class AvdStartDialog extends Dialog {
}
@Override
protected Control createDialogArea(Composite parent) {
protected Control createDialogArea(final Composite parent) {
GridData gd;
// create a composite with standard margins and spacing
@@ -150,7 +151,7 @@ final class AvdStartDialog extends Dialog {
scaleGroup.setLayoutData(gd = new GridData(GridData.FILL_HORIZONTAL));
gd.horizontalIndent = 30;
gd.horizontalSpan = 2;
scaleGroup.setLayout(new GridLayout(2, false));
scaleGroup.setLayout(new GridLayout(3, false));
l = new Label(scaleGroup, SWT.NONE);
l.setText("Screen Size (in):");
@@ -172,12 +173,16 @@ final class AvdStartDialog extends Dialog {
onScaleChange();
}
});
// empty composite, only 2 widgets on this line.
new Composite(scaleGroup, SWT.NONE).setLayoutData(gd = new GridData());
gd.widthHint = gd.heightHint = 0;
l = new Label(scaleGroup, SWT.NONE);
l.setText("Monitor dpi:");
mMonitorDpi = new Text(scaleGroup, SWT.BORDER);
mMonitorDpi.setText(Integer.toString(getMonitorDpi()));
mMonitorDpi.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
mMonitorDpi.setLayoutData(gd = new GridData(GridData.FILL_HORIZONTAL));
gd.widthHint = 50;
mMonitorDpi.addVerifyListener(new VerifyListener() {
public void verifyText(VerifyEvent event) {
// check for digit only.
@@ -196,6 +201,19 @@ final class AvdStartDialog extends Dialog {
}
});
Button button = new Button(scaleGroup, SWT.PUSH | SWT.FLAT);
button.setText("?");
button.setToolTipText("Click to figure out your monitor's pixel density");
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent arg0) {
ResolutionChooserDialog dialog = new ResolutionChooserDialog(parent.getShell());
if (dialog.open() == Window.OK) {
mMonitorDpi.setText(Integer.toString(dialog.getDensity()));
}
}
});
scaleGroup.setEnabled(defaultState);
mScaleButton.addSelectionListener(new SelectionAdapter() {

View File

@@ -0,0 +1,138 @@
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.sdkuilib.internal.widgets;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
/**
* Small dialog to let a user choose a screen size (from a fixed list) and a resolution
* (as returned by {@link Display#getMonitors()}).
* After the dialog as returned, one can query {@link #getDensity()} to get the chosen monitor
* pixel density.
*/
class ResolutionChooserDialog extends Dialog {
public final static float[] MONITOR_SIZES = new float[] {
13.3f, 14, 15.4f, 15.6f, 17, 19, 20, 21, 24, 30,
};
private Button mButton;
private Combo mScreenSizeCombo;
private Combo mMonitorCombo;
private Monitor[] mMonitors;
private int mScreenSizeIndex = -1;
private int mMonitorIndex = 0;
ResolutionChooserDialog(Shell parentShell) {
super(parentShell);
}
/**
* Returns the pixel density of the user-chosen monitor.
*/
int getDensity() {
float size = MONITOR_SIZES[mScreenSizeIndex];
Rectangle rect = mMonitors[mMonitorIndex].getBounds();
// compute the density
double d = Math.sqrt(rect.width * rect.width + rect.height * rect.height) / size;
return (int)Math.round(d);
}
@Override
protected void configureShell(Shell newShell) {
newShell.setText("Monitor Density");
super.configureShell(newShell);
}
@Override
protected Control createContents(Composite parent) {
Control control = super.createContents(parent);
mButton = getButton(IDialogConstants.OK_ID);
mButton.setEnabled(false);
return control;
}
@Override
protected Control createDialogArea(Composite parent) {
// create a composite with standard margins and spacing
Composite composite = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout(2, false);
layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
layout.horizontalSpacing = convertHorizontalDLUsToPixels(
IDialogConstants.HORIZONTAL_SPACING);
composite.setLayout(layout);
composite.setLayoutData(new GridData(GridData.FILL_BOTH));
Label l = new Label(composite, SWT.NONE);
l.setText("Screen Size:");
mScreenSizeCombo = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
for (float size : MONITOR_SIZES) {
if (Math.round(size) == size) {
mScreenSizeCombo.add(String.format("%.0f\"", size));
} else {
mScreenSizeCombo.add(String.format("%.1f\"", size));
}
}
mScreenSizeCombo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent arg0) {
mScreenSizeIndex = mScreenSizeCombo.getSelectionIndex();
mButton.setEnabled(mScreenSizeIndex != -1);
}
});
l = new Label(composite, SWT.NONE);
l.setText("Resolution:");
mMonitorCombo = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
mMonitors = parent.getDisplay().getMonitors();
for (Monitor m : mMonitors) {
Rectangle r = m.getBounds();
mMonitorCombo.add(String.format("%d x %d", r.width, r.height));
}
mMonitorCombo.select(mMonitorIndex);
mMonitorCombo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetDefaultSelected(SelectionEvent arg0) {
mMonitorIndex = mMonitorCombo.getSelectionIndex();
}
});
applyDialogFont(composite);
return composite;
}
}