KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > launcher > NameValuePairDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.debug.ui.launcher;
12
13
14 import org.eclipse.jface.dialogs.Dialog;
15 import org.eclipse.jface.dialogs.IDialogConstants;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.ModifyEvent;
18 import org.eclipse.swt.events.ModifyListener;
19 import org.eclipse.swt.graphics.Font;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Label;
25 import org.eclipse.swt.widgets.Shell;
26 import org.eclipse.swt.widgets.Text;
27
28 public class NameValuePairDialog extends Dialog {
29
30     private String JavaDoc fName;
31     private String JavaDoc fValue;
32
33     private String JavaDoc fTitle;
34     private String JavaDoc[] fFieldLabels;
35     private String JavaDoc[] fInitialValues;
36     
37     private Label fNameLabel;
38     private Text fNameText;
39     private Label fValueLabel;
40     private Text fValueText;
41
42     public NameValuePairDialog(Shell shell, String JavaDoc title, String JavaDoc[] fieldLabels, String JavaDoc[] initialValues) {
43         super(shell);
44         fTitle = title;
45         fFieldLabels = fieldLabels;
46         fInitialValues = initialValues;
47     }
48
49     /**
50      * @see Dialog#createDialogArea(Composite)
51      */

52     protected Control createDialogArea(Composite parent) {
53         Font font = parent.getFont();
54         
55         Composite comp = new Composite(parent, SWT.NULL);
56         GridLayout topLayout = new GridLayout();
57         topLayout.numColumns = 2;
58         topLayout.marginHeight =
59             convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
60         topLayout.marginWidth =
61             convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
62         topLayout.verticalSpacing =
63             convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
64         topLayout.horizontalSpacing =
65             convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
66         comp.setLayout(topLayout);
67         comp.setFont(font);
68         GridData gd;
69         
70         fNameLabel = new Label(comp, SWT.NONE);
71         fNameLabel.setText(fFieldLabels[0]);
72         fNameLabel.setFont(font);
73         
74         ModifyListener listener= new ModifyListener() {
75             public void modifyText(ModifyEvent e) {
76                 updateButtons();
77             }
78         };
79         
80         fNameText = new Text(comp, SWT.BORDER | SWT.SINGLE);
81         fNameText.setText(fInitialValues[0]);
82         gd = new GridData(GridData.FILL_HORIZONTAL);
83         gd.widthHint = 300;
84         fNameText.setLayoutData(gd);
85         fNameText.setFont(font);
86         fNameText.addModifyListener(listener);
87         
88         fValueLabel = new Label(comp, SWT.NONE);
89         fValueLabel.setText(fFieldLabels[1]);
90         fValueLabel.setFont(font);
91         
92         fValueText = new Text(comp, SWT.BORDER | SWT.SINGLE);
93         fValueText.setText(fInitialValues[1]);
94         gd = new GridData(GridData.FILL_HORIZONTAL);
95         gd.widthHint = 300;
96         fValueText.setLayoutData(gd);
97         fValueText.setFont(font);
98         fValueText.addModifyListener(listener);
99         
100         applyDialogFont(comp);
101         return comp;
102     }
103     
104     /**
105      * Return the name/value pair entered in this dialog. If the cancel button was hit,
106      * both will be <code>null</code>.
107      */

108     public String JavaDoc[] getNameValuePair() {
109         return new String JavaDoc[] {fName, fValue};
110     }
111     
112     /**
113      * @see Dialog#buttonPressed(int)
114      */

115     protected void buttonPressed(int buttonId) {
116         if (buttonId == IDialogConstants.OK_ID) {
117             fName= fNameText.getText();
118             fValue = fValueText.getText();
119         } else {
120             fName = null;
121             fValue = null;
122         }
123         super.buttonPressed(buttonId);
124     }
125     
126     /**
127      * @see Window#configureShell(Shell)
128      */

129     protected void configureShell(Shell shell) {
130         super.configureShell(shell);
131         if (fTitle != null) {
132             shell.setText(fTitle);
133         }
134     }
135     
136     /**
137      * Enable the OK button if valid input
138      */

139     protected void updateButtons() {
140         String JavaDoc name = fNameText.getText().trim();
141         String JavaDoc value = fValueText.getText().trim();
142         getButton(IDialogConstants.OK_ID).setEnabled((name.length() > 0) &&(value.length() > 0));
143     }
144     
145     /**
146      * Enable the buttons on creation.
147      */

148     public void create() {
149         super.create();
150         updateButtons();
151     }
152 }
153
Popular Tags