1 11 12 package org.eclipse.jdt.internal.debug.ui; 13 14 import org.eclipse.core.runtime.IStatus; 15 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants; 16 import org.eclipse.jface.dialogs.IDialogConstants; 17 import org.eclipse.jface.dialogs.IDialogSettings; 18 import org.eclipse.jface.dialogs.StatusDialog; 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.events.ModifyEvent; 21 import org.eclipse.swt.events.ModifyListener; 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 31 public class CreateStepFilterDialog extends StatusDialog { 32 33 private static final String DEFAULT_NEW_FILTER_TEXT = ""; 35 private Text text; 36 private Filter filter; 37 private Button okButton; 38 39 private boolean filterValid; 40 private boolean okClicked; 41 private Filter[] existingFilters; 42 43 private CreateStepFilterDialog(Shell parent, Filter filter, Filter[] existingFilters) { 44 super(parent); 45 setShellStyle(getShellStyle() | SWT.RESIZE); 46 this.filter = filter; 47 this.existingFilters = existingFilters; 48 49 setTitle(DebugUIMessages.CreateStepFilterDialog_2); 50 setStatusLineAboveButtons(false); 51 52 } 53 54 static Filter showCreateStepFilterDialog(Shell parent, Filter[] existingFilters) { 55 CreateStepFilterDialog createStepFilterDialog = new CreateStepFilterDialog(parent, new Filter(DEFAULT_NEW_FILTER_TEXT, true), existingFilters); 56 createStepFilterDialog.create(); 57 createStepFilterDialog.open(); 58 59 return createStepFilterDialog.filter; 60 } 61 62 protected void createButtonsForButtonBar(Composite parent) { 63 okButton= createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true); 64 okButton.setEnabled(false); 65 createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false); 66 } 67 68 protected Control createDialogArea(Composite parent) { 69 Composite container = (Composite)super.createDialogArea(parent); 70 71 GridLayout gridLayout = new GridLayout(); 72 gridLayout.numColumns = 2; 73 gridLayout.marginHeight = 15; 74 gridLayout.marginWidth = 15; 75 container.setLayout(gridLayout); 76 77 int textStyles = SWT.SINGLE | SWT.LEFT; 78 Label label= new Label(container, textStyles); 79 label.setText(DebugUIMessages.CreateStepFilterDialog_3); 80 label.setFont(container.getFont()); 81 82 if (!SWT.getPlatform().equals("motif")) { textStyles |= SWT.BORDER; 90 } 91 92 text = new Text(container, textStyles); 93 GridData gridData = new GridData(GridData.FILL_HORIZONTAL); 94 gridData.horizontalSpan=1; 95 gridData.widthHint = 300; 96 text.setLayoutData(gridData); 97 text.setFont(container.getFont()); 98 99 text.addModifyListener(new ModifyListener() { 100 public void modifyText(ModifyEvent e) { 101 validateChange(); 102 if (!filterValid) 103 updateStatus(new StatusInfo(IStatus.ERROR, DebugUIMessages.CreateStepFilterDialog_4)); 104 else if (isDuplicateFilter(text.getText().trim())) { 105 updateStatus(new StatusInfo(IStatus.WARNING, DebugUIMessages.CreateStepFilterDialog_5)); 106 return; 107 } else 108 updateStatus(new StatusInfo()); 109 } 110 }); 111 112 return container; 113 } 114 115 private void validateChange() { 116 String trimmedValue = text.getText().trim(); 117 118 if (trimmedValue.length()>0 && validateInput(trimmedValue)) { 119 okButton.setEnabled(true); 120 filter.setName(text.getText()); 121 filterValid = true; 122 } else { 123 okButton.setEnabled(false); 124 filter.setName(DEFAULT_NEW_FILTER_TEXT); 125 filterValid = false; 126 } 127 } 128 129 private boolean isDuplicateFilter(String trimmedValue) { 130 for (int i=0; i<existingFilters.length; i++) 131 if(existingFilters[i].getName().equals(trimmedValue)) 132 return true; 133 return false; 134 } 135 143 private boolean validateInput(String trimmedValue) { 144 char firstChar= trimmedValue.charAt(0); 145 if (!Character.isJavaIdentifierStart(firstChar)) { 146 if (!(firstChar == '*')) { 147 return false; 148 } 149 } 150 int length= trimmedValue.length(); 151 for (int i= 1; i < length; i++) { 152 char c= trimmedValue.charAt(i); 153 if (!Character.isJavaIdentifierPart(c)) { 154 if (c == '.' && i != (length - 1)) { 155 continue; 156 } 157 if (c == '*' && i == (length - 1)) { 158 continue; 159 } 160 return false; 161 } 162 } 163 return true; 164 } 165 166 171 protected String getDialogSettingsSectionName() { 172 return IJavaDebugUIConstants.PLUGIN_ID + ".CREATE_STEP_FILTER_DIALOG_SECTION"; } 174 175 178 public boolean close() { 179 if (!okClicked) { 180 filterValid = false; 181 filter = null; 182 } 183 return super.close(); 184 } 185 186 189 protected IDialogSettings getDialogBoundsSettings() { 190 IDialogSettings settings = JDIDebugUIPlugin.getDefault().getDialogSettings(); 191 IDialogSettings section = settings.getSection(getDialogSettingsSectionName()); 192 if (section == null) { 193 section = settings.addNewSection(getDialogSettingsSectionName()); 194 } 195 return section; 196 } 197 198 protected void okPressed() { 199 okClicked = true; 200 super.okPressed(); 201 } 202 } 203 | Popular Tags |