KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > ComboSelectionDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.refactoring;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.SelectionAdapter;
17 import org.eclipse.swt.events.SelectionEvent;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Combo;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Label;
24 import org.eclipse.swt.widgets.Shell;
25
26 import org.eclipse.jface.dialogs.Dialog;
27
28
29 class ComboSelectionDialog extends Dialog{
30             
31     private String JavaDoc fSelection= null;
32     private final String JavaDoc fShellTitle;
33     private final String JavaDoc fLabelText;
34     private final String JavaDoc[] fAllowedStrings;
35     private final int fInitialSelectionIndex;
36     public ComboSelectionDialog(Shell parentShell, String JavaDoc shellTitle, String JavaDoc labelText, String JavaDoc[] comboStrings, int initialSelectionIndex) {
37         super(parentShell);
38         Assert.isNotNull(shellTitle);
39         Assert.isNotNull(labelText);
40         Assert.isTrue(comboStrings.length > 0);
41         Assert.isTrue(initialSelectionIndex >= 0 && initialSelectionIndex < comboStrings.length);
42         fShellTitle= shellTitle;
43         fLabelText= labelText;
44         fAllowedStrings= comboStrings;
45         fInitialSelectionIndex= initialSelectionIndex;
46     }
47
48     String JavaDoc getSelectedString(){
49         return fSelection;
50     }
51     
52     /*
53      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
54      */

55     protected Control createDialogArea(Composite parent) {
56         getShell().setText(fShellTitle);
57         
58         Composite composite = (Composite)super.createDialogArea(parent);
59         Composite innerComposite = new Composite(composite, SWT.NONE);
60         innerComposite.setLayoutData(new GridData());
61         GridLayout gl= new GridLayout();
62         gl.numColumns= 2;
63         innerComposite.setLayout(gl);
64         
65         Label label= new Label(innerComposite, SWT.NONE);
66         label.setText(fLabelText);
67         label.setLayoutData(new GridData());
68
69         final Combo combo= new Combo(innerComposite, SWT.READ_ONLY);
70         for (int i = 0; i < fAllowedStrings.length; i++) {
71             combo.add(fAllowedStrings[i]);
72         }
73         combo.select(fInitialSelectionIndex);
74         fSelection= combo.getItem(combo.getSelectionIndex());
75         GridData gd= new GridData();
76         gd.widthHint= convertWidthInCharsToPixels(getMaxStringLength());
77         combo.setLayoutData(gd);
78         combo.addSelectionListener(new SelectionAdapter(){
79             public void widgetSelected(SelectionEvent e) {
80                 fSelection= combo.getItem(combo.getSelectionIndex());
81             }
82         });
83         applyDialogFont(composite);
84         return composite;
85     }
86     
87     private int getMaxStringLength() {
88         int max= 0;
89         for (int i= 0; i < fAllowedStrings.length; i++) {
90             max= Math.max(max, fAllowedStrings[i].length());
91         }
92         return max;
93     }
94 }
95
Popular Tags