View Javadoc

1   /* ====================================================================
2    *   Copyright 2003-2004 Fabrizio Giustina.
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   * ====================================================================
16   */
17  package net.sf.commonclipse.preferences;
18  
19  import org.eclipse.core.runtime.IStatus;
20  import org.eclipse.jdt.core.JavaConventions;
21  import org.eclipse.swt.widgets.Composite;
22  
23  /***
24   * ComboFieldEditor with check for valid class.CONSTANT values.
25   * @author fgiust
26   * @version $Revision: 1.3 $ ($Author: fgiust $)
27   */
28  public class ClassConstantFieldEditor extends ComboFieldEditor
29  {
30  
31      /***
32       * @see net.sf.commonclipse.preferences.ComboFieldEditor#ComboFieldEditor(String, String, Composite)
33       */
34      public ClassConstantFieldEditor(String name, String labelText, Composite parent)
35      {
36          super(name, labelText, parent);
37      }
38  
39      /***
40       * Overrides default validation to check for a valid class.CONSTANT value.
41       * @return <code>true</code> if the field value is valid, and <code>false</code> if invalid
42       */
43      protected boolean doCheckState()
44      {
45          String txt = getTextControl().getText();
46  
47          if (txt != null && !"".equals(txt)) //$NON-NLS-1$
48          {
49              int lastDot = txt.lastIndexOf("."); //$NON-NLS-1$
50  
51              if (lastDot == -1)
52              {
53                  return false;
54              }
55  
56              // split package/class - field
57              String typeToken = txt.substring(0, lastDot);
58              String fieldToken = txt.substring(lastDot + 1);
59  
60              // validates tokens
61              IStatus status1 = JavaConventions.validateJavaTypeName(typeToken);
62              IStatus status2 = JavaConventions.validateFieldName(fieldToken);
63  
64              if ((status1.getCode() != IStatus.OK) || (status2.getCode() != IStatus.OK))
65              {
66                  return false;
67              }
68          }
69  
70          return true;
71      }
72  
73  }