KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > CreateStepFilterDialog


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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
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 JavaDoc DEFAULT_NEW_FILTER_TEXT = ""; //$NON-NLS-1$
34

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         // create & configure Text widget for editor
83
// Fix for bug 1766. Border behavior on for text fields varies per platform.
84
// On Motif, you always get a border, on other platforms,
85
// you don't. Specifying a border on Motif results in the characters
86
// getting pushed down so that only there very tops are visible. Thus,
87
// we have to specify different style constants for the different platforms.
88
if (!SWT.getPlatform().equals("motif")) { //$NON-NLS-1$
89
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 JavaDoc 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 JavaDoc 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     /**
136      * A valid step filter is simply one that is a valid Java identifier.
137      * and, as defined in the JDI spec, the regular expressions used for
138      * step filtering must be limited to exact matches or patterns that
139      * begin with '*' or end with '*'. Beyond this, a string cannot be validated
140      * as corresponding to an existing type or package (and this is probably not
141      * even desirable).
142      */

143     private boolean validateInput(String JavaDoc 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     /**
167      * Returns the name of the section that this dialog stores its settings in
168      *
169      * @return String
170      */

171     protected String JavaDoc getDialogSettingsSectionName() {
172         return IJavaDebugUIConstants.PLUGIN_ID + ".CREATE_STEP_FILTER_DIALOG_SECTION"; //$NON-NLS-1$
173
}
174     
175     /* (non-Javadoc)
176      * @see org.eclipse.jface.window.Window#close()
177      */

178     public boolean close() {
179         if (!okClicked) {
180             filterValid = false;
181             filter = null;
182         }
183         return super.close();
184     }
185     
186      /* (non-Javadoc)
187      * @see org.eclipse.jface.dialogs.Dialog#getDialogBoundsSettings()
188      */

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