KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > preferences > AddPropertyDialog


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.ant.internal.ui.preferences;
12
13 import org.eclipse.ant.internal.ui.IAntUIHelpContextIds;
14 import org.eclipse.debug.ui.StringVariableSelectionDialog;
15 import org.eclipse.jface.dialogs.Dialog;
16 import org.eclipse.jface.dialogs.IDialogConstants;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.ModifyEvent;
19 import org.eclipse.swt.events.ModifyListener;
20 import org.eclipse.swt.events.SelectionAdapter;
21 import org.eclipse.swt.events.SelectionEvent;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.widgets.Button;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.swt.widgets.Text;
30 import org.eclipse.ui.PlatformUI;
31
32 public class AddPropertyDialog extends Dialog {
33
34     private String JavaDoc fName;
35     private String JavaDoc fValue;
36
37     private String JavaDoc fTitle;
38     
39     private Label fNameLabel;
40     private Text fNameText;
41     private Label fValueLabel;
42     private Text fValueText;
43     
44     private String JavaDoc[] fInitialValues;
45
46     public AddPropertyDialog(Shell shell, String JavaDoc title, String JavaDoc[] initialValues) {
47         super(shell);
48         fTitle = title;
49         fInitialValues= initialValues;
50     }
51
52     /* (non-Javadoc)
53      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
54      */

55     protected Control createDialogArea(Composite parent) {
56         Composite comp= (Composite) super.createDialogArea(parent);
57         ((GridLayout) comp.getLayout()).numColumns = 2;
58         
59         fNameLabel = new Label(comp, SWT.NONE);
60         fNameLabel.setText(AntPreferencesMessages.AddPropertyDialog__Name__1);
61         fNameLabel.setFont(comp.getFont());
62         
63         fNameText = new Text(comp, SWT.BORDER | SWT.SINGLE);
64         fNameText.setText(fInitialValues[0]);
65         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
66         gd.widthHint = 300;
67         fNameText.setLayoutData(gd);
68         fNameText.setFont(comp.getFont());
69         fNameText.addModifyListener(new ModifyListener() {
70             public void modifyText(ModifyEvent e) {
71                 updateButtons();
72             }
73         });
74         
75         fValueLabel = new Label(comp, SWT.NONE);
76         fValueLabel.setText(AntPreferencesMessages.AddPropertyDialog__Value__2);
77         fValueLabel.setFont(comp.getFont());
78         
79         fValueText = new Text(comp, SWT.BORDER | SWT.SINGLE);
80         fValueText.setText(fInitialValues[1]);
81         gd = new GridData(GridData.FILL_HORIZONTAL);
82         gd.widthHint = 300;
83         fValueText.setLayoutData(gd);
84         fValueText.setFont(comp.getFont());
85         fValueText.addModifyListener(new ModifyListener() {
86             public void modifyText(ModifyEvent e) {
87                 updateButtons();
88             }
89         });
90         
91         Button variablesButton = new Button(comp, SWT.PUSH);
92         variablesButton.setText(AntPreferencesMessages.AddPropertyDialog_2);
93         gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
94         gd.horizontalSpan = 2;
95         int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
96         gd.widthHint = Math.max(widthHint, variablesButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
97         variablesButton.setLayoutData(gd);
98         variablesButton.setFont(comp.getFont());
99         
100         variablesButton.addSelectionListener(new SelectionAdapter() {
101             public void widgetSelected(SelectionEvent se) {
102                 getVariable();
103             }
104         });
105         
106         return comp;
107     }
108     
109     protected void getVariable() {
110         StringVariableSelectionDialog variablesDialog = new StringVariableSelectionDialog(getShell());
111         int returnCode = variablesDialog.open();
112         if (returnCode == IDialogConstants.OK_ID) {
113             String JavaDoc variable = variablesDialog.getVariableExpression();
114             if (variable != null) {
115                 fValueText.insert(variable.trim());
116             }
117         }
118     }
119
120     /**
121      * Return the name/value pair entered in this dialog. If the cancel button was hit,
122      * both will be <code>null</code>.
123      */

124     public String JavaDoc[] getNameValuePair() {
125         return new String JavaDoc[] {fName, fValue};
126     }
127     
128     /* (non-Javadoc)
129      * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
130      */

131     protected void buttonPressed(int buttonId) {
132         if (buttonId == IDialogConstants.OK_ID) {
133             fName= fNameText.getText();
134             fValue = fValueText.getText();
135         } else {
136             fName = null;
137             fValue = null;
138         }
139         super.buttonPressed(buttonId);
140     }
141     
142     /* (non-Javadoc)
143      * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
144      */

145     protected void configureShell(Shell shell) {
146         super.configureShell(shell);
147         if (fTitle != null) {
148             shell.setText(fTitle);
149         }
150         if (fInitialValues[0].length() == 0) {
151             PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, IAntUIHelpContextIds.ADD_PROPERTY_DIALOG);
152         } else {
153             PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, IAntUIHelpContextIds.EDIT_PROPERTY_DIALOG);
154         }
155     }
156     
157     /**
158      * Enable the OK button if valid input
159      */

160     protected void updateButtons() {
161         String JavaDoc name = fNameText.getText().trim();
162         String JavaDoc value = fValueText.getText().trim();
163         getButton(IDialogConstants.OK_ID).setEnabled((name.length() > 0) &&(value.length() > 0));
164     }
165     
166     /**
167      * Enable the buttons on creation.
168      * @see org.eclipse.jface.window.Window#create()
169      */

170     public void create() {
171         super.create();
172         updateButtons();
173     }
174 }
175
Popular Tags