1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 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.sdkuilib.ui; 18 19 import org.eclipse.jface.dialogs.Dialog; 20 import org.eclipse.jface.dialogs.IDialogConstants; 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.layout.GridData; 23 import org.eclipse.swt.layout.GridLayout; 24 import org.eclipse.swt.widgets.Composite; 25 import org.eclipse.swt.widgets.Control; 26 import org.eclipse.swt.widgets.Shell; 27 28 /** 29 * JFace-based dialog that properly sets up a {@link GridLayout} top composite with the proper 30 * margin. 31 * <p/> 32 * Implementing dialog must create the content of the dialog in 33 * {@link #createDialogContent(Composite)}. 34 * <p/> 35 * A JFace dialog is perfect if you want a typical "OK | cancel" workflow, with the OK and 36 * cancel things all handled for you using a predefined layout. If you want a different set 37 * of buttons or a different layout, consider {@link SwtBaseDialog} instead. 38 */ 39 public abstract class GridDialog extends Dialog { 40 41 private final int mNumColumns; 42 private final boolean mMakeColumnsEqualWidth; 43 44 /** 45 * Creates the dialog 46 * @param parentShell the parent {@link Shell}. 47 * @param numColumns the number of columns in the grid 48 * @param makeColumnsEqualWidth whether or not the columns will have equal width 49 */ 50 public GridDialog(Shell parentShell, int numColumns, boolean makeColumnsEqualWidth) { 51 super(parentShell); 52 mNumColumns = numColumns; 53 mMakeColumnsEqualWidth = makeColumnsEqualWidth; 54 } 55 56 /** 57 * Creates the content of the dialog. The <var>parent</var> composite is a {@link GridLayout} 58 * created with the <var>numColumn</var> and <var>makeColumnsEqualWidth</var> parameters 59 * passed to {@link #GridDialog(Shell, int, boolean)}. 60 * @param parent the parent composite. 61 */ 62 public abstract void createDialogContent(Composite parent); 63 64 @Override 65 protected Control createDialogArea(Composite parent) { 66 Composite top = new Composite(parent, SWT.NONE); 67 GridLayout layout = new GridLayout(mNumColumns, mMakeColumnsEqualWidth); 68 layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN); 69 layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN); 70 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); 71 layout.horizontalSpacing = convertHorizontalDLUsToPixels( 72 IDialogConstants.HORIZONTAL_SPACING); 73 top.setLayout(layout); 74 top.setLayoutData(new GridData(GridData.FILL_BOTH)); 75 76 createDialogContent(top); 77 78 applyDialogFont(top); 79 return top; 80 } 81 } 82