01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.ui.refactoring;
11:
12: import org.eclipse.core.runtime.Assert;
13:
14: import org.eclipse.swt.SWT;
15: import org.eclipse.swt.events.SelectionAdapter;
16: import org.eclipse.swt.events.SelectionEvent;
17: import org.eclipse.swt.layout.GridData;
18: import org.eclipse.swt.layout.GridLayout;
19: import org.eclipse.swt.widgets.Combo;
20: import org.eclipse.swt.widgets.Composite;
21: import org.eclipse.swt.widgets.Control;
22: import org.eclipse.swt.widgets.Label;
23: import org.eclipse.swt.widgets.Shell;
24:
25: import org.eclipse.jface.dialogs.Dialog;
26:
27: class ComboSelectionDialog extends Dialog {
28:
29: private String fSelection = null;
30: private final String fShellTitle;
31: private final String fLabelText;
32: private final String[] fAllowedStrings;
33: private final int fInitialSelectionIndex;
34:
35: public ComboSelectionDialog(Shell parentShell, String shellTitle,
36: String labelText, String[] comboStrings,
37: int initialSelectionIndex) {
38: super (parentShell);
39: Assert.isNotNull(shellTitle);
40: Assert.isNotNull(labelText);
41: Assert.isTrue(comboStrings.length > 0);
42: Assert.isTrue(initialSelectionIndex >= 0
43: && initialSelectionIndex < comboStrings.length);
44: fShellTitle = shellTitle;
45: fLabelText = labelText;
46: fAllowedStrings = comboStrings;
47: fInitialSelectionIndex = initialSelectionIndex;
48: }
49:
50: String getSelectedString() {
51: return fSelection;
52: }
53:
54: /*
55: * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
56: */
57: protected Control createDialogArea(Composite parent) {
58: getShell().setText(fShellTitle);
59:
60: Composite composite = (Composite) super
61: .createDialogArea(parent);
62: Composite innerComposite = new Composite(composite, SWT.NONE);
63: innerComposite.setLayoutData(new GridData());
64: GridLayout gl = new GridLayout();
65: gl.numColumns = 2;
66: innerComposite.setLayout(gl);
67:
68: Label label = new Label(innerComposite, SWT.NONE);
69: label.setText(fLabelText);
70: label.setLayoutData(new GridData());
71:
72: final Combo combo = new Combo(innerComposite, SWT.READ_ONLY);
73: for (int i = 0; i < fAllowedStrings.length; i++) {
74: combo.add(fAllowedStrings[i]);
75: }
76: combo.select(fInitialSelectionIndex);
77: fSelection = combo.getItem(combo.getSelectionIndex());
78: GridData gd = new GridData();
79: gd.widthHint = convertWidthInCharsToPixels(getMaxStringLength());
80: combo.setLayoutData(gd);
81: combo.addSelectionListener(new SelectionAdapter() {
82: public void widgetSelected(SelectionEvent e) {
83: fSelection = combo.getItem(combo.getSelectionIndex());
84: }
85: });
86: applyDialogFont(composite);
87: return composite;
88: }
89:
90: private int getMaxStringLength() {
91: int max = 0;
92: for (int i = 0; i < fAllowedStrings.length; i++) {
93: max = Math.max(max, fAllowedStrings[i].length());
94: }
95: return max;
96: }
97: }
|