1 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 fName; 35 private String fValue; 36 37 private String fTitle; 38 39 private Label fNameLabel; 40 private Text fNameText; 41 private Label fValueLabel; 42 private Text fValueText; 43 44 private String [] fInitialValues; 45 46 public AddPropertyDialog(Shell shell, String title, String [] initialValues) { 47 super(shell); 48 fTitle = title; 49 fInitialValues= initialValues; 50 } 51 52 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 variable = variablesDialog.getVariableExpression(); 114 if (variable != null) { 115 fValueText.insert(variable.trim()); 116 } 117 } 118 } 119 120 124 public String [] getNameValuePair() { 125 return new String [] {fName, fValue}; 126 } 127 128 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 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 160 protected void updateButtons() { 161 String name = fNameText.getText().trim(); 162 String value = fValueText.getText().trim(); 163 getButton(IDialogConstants.OK_ID).setEnabled((name.length() > 0) &&(value.length() > 0)); 164 } 165 166 170 public void create() { 171 super.create(); 172 updateButtons(); 173 } 174 } 175 | Popular Tags |