2 * Copyright 2003-2005 Dave Griffith
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com
.siyeh
.ig
.naming
;
18 import com
.intellij
.openapi
.util
.InvalidDataException
;
19 import com
.intellij
.ui
.DocumentAdapter
;
20 import com
.siyeh
.HardcodedMethodConstants
;
21 import com
.siyeh
.InspectionGadgetsBundle
;
22 import com
.siyeh
.ig
.BaseInspection
;
23 import com
.siyeh
.ig
.RegExFormatter
;
24 import com
.siyeh
.ig
.RegExInputVerifier
;
25 import com
.siyeh
.ig
.ui
.FormattedTextFieldMacFix
;
26 import org
.jdom
.Element
;
27 import org
.jetbrains
.annotations
.NonNls
;
30 import javax
.swing
.event
.DocumentEvent
;
31 import javax
.swing
.event
.DocumentListener
;
32 import javax
.swing
.text
.Document
;
33 import javax
.swing
.text
.InternationalFormatter
;
35 import java
.text
.NumberFormat
;
36 import java
.text
.ParseException
;
37 import java
.util
.regex
.Matcher
;
38 import java
.util
.regex
.Pattern
;
40 public abstract class ConventionInspection
extends BaseInspection
{
43 * public fields for the DefaultJDomExternalizer
44 * @noinspection PublicField
46 public String m_regex
= getDefaultRegex();
47 /** @noinspection PublicField*/
48 public int m_minLength
= getDefaultMinLength();
49 /** @noinspection PublicField*/
50 public int m_maxLength
= getDefaultMaxLength();
51 protected Pattern m_regexPattern
= Pattern
.compile(m_regex
);
53 @NonNls protected abstract String
getDefaultRegex();
55 protected abstract int getDefaultMinLength();
57 protected abstract int getDefaultMaxLength();
59 protected String
getRegex() {
63 protected int getMinLength() {
67 protected int getMaxLength() {
71 protected boolean isValid(String name
) {
72 final int length
= name
.length();
73 if (length
< m_minLength
) {
76 if (length
> m_maxLength
) {
79 if (HardcodedMethodConstants
.SERIAL_VERSION_UID
.equals(name
)) {
82 final Matcher matcher
= m_regexPattern
.matcher(name
);
83 return matcher
.matches();
86 public void readSettings(Element element
) throws InvalidDataException
{
87 super.readSettings(element
);
88 m_regexPattern
= Pattern
.compile(m_regex
);
91 private static final int REGEX_COLUMN_COUNT
= 25;
93 public JComponent
createOptionsPanel() {
94 final GridBagLayout layout
= new GridBagLayout();
95 final JPanel panel
= new JPanel(layout
);
97 final JLabel patternLabel
= new JLabel(
98 InspectionGadgetsBundle
.message("convention.pattern.option"));
99 patternLabel
.setHorizontalAlignment(SwingConstants
.TRAILING
);
100 final JLabel minLengthLabel
= new JLabel(
101 InspectionGadgetsBundle
.message("convention.min.length.option"));
102 minLengthLabel
.setHorizontalAlignment(SwingConstants
.TRAILING
);
103 final JLabel maxLengthLabel
= new JLabel(
104 InspectionGadgetsBundle
.message("convention.max.length.option"));
105 maxLengthLabel
.setHorizontalAlignment(SwingConstants
.TRAILING
);
107 final NumberFormat numberFormat
= NumberFormat
.getIntegerInstance();
108 numberFormat
.setParseIntegerOnly(true);
109 numberFormat
.setMinimumIntegerDigits(1);
110 numberFormat
.setMaximumIntegerDigits(2);
111 final InternationalFormatter formatter
=
112 new InternationalFormatter(numberFormat
);
113 formatter
.setAllowsInvalid(false);
114 formatter
.setCommitsOnValidEdit(true);
116 final JFormattedTextField minLengthField
=
117 new JFormattedTextField(formatter
);
118 final Font panelFont
= panel
.getFont();
119 minLengthField
.setFont(panelFont
);
120 minLengthField
.setValue(m_minLength
);
121 minLengthField
.setColumns(2);
122 FormattedTextFieldMacFix
.apply(minLengthField
);
124 final JFormattedTextField maxLengthField
=
125 new JFormattedTextField(formatter
);
126 maxLengthField
.setFont(panelFont
);
127 maxLengthField
.setValue(m_maxLength
);
128 maxLengthField
.setColumns(2);
129 FormattedTextFieldMacFix
.apply(maxLengthField
);
131 final JFormattedTextField regexField
=
132 new JFormattedTextField(new RegExFormatter());
133 regexField
.setFont(panelFont
);
134 regexField
.setValue(m_regexPattern
);
135 regexField
.setColumns(REGEX_COLUMN_COUNT
);
136 regexField
.setInputVerifier(new RegExInputVerifier());
137 regexField
.setFocusLostBehavior(JFormattedTextField
.COMMIT
);
138 FormattedTextFieldMacFix
.apply(regexField
);
139 final DocumentListener listener
= new DocumentAdapter() {
140 public void textChanged(DocumentEvent evt
) {
142 regexField
.commitEdit();
143 minLengthField
.commitEdit();
144 maxLengthField
.commitEdit();
145 m_regexPattern
= (Pattern
) regexField
.getValue();
146 m_regex
= m_regexPattern
.pattern();
147 m_minLength
= ((Number
) minLengthField
.getValue()).intValue();
148 m_maxLength
= ((Number
) maxLengthField
.getValue()).intValue();
149 } catch (ParseException e
) {
154 final Document regexDocument
= regexField
.getDocument();
155 regexDocument
.addDocumentListener(listener
);
156 final Document minLengthDocument
= minLengthField
.getDocument();
157 minLengthDocument
.addDocumentListener(listener
);
158 final Document maxLengthDocument
= maxLengthField
.getDocument();
159 maxLengthDocument
.addDocumentListener(listener
);
161 final GridBagConstraints constraints
= new GridBagConstraints();
162 constraints
.gridx
= 0;
163 constraints
.gridy
= 0;
164 constraints
.weightx
= 1.0;
165 constraints
.anchor
= GridBagConstraints
.EAST
;
166 constraints
.fill
= GridBagConstraints
.HORIZONTAL
;
167 panel
.add(patternLabel
, constraints
);
169 constraints
.gridx
= 1;
170 constraints
.gridy
= 0;
171 constraints
.gridwidth
= 3;
172 constraints
.anchor
= GridBagConstraints
.WEST
;
173 panel
.add(regexField
, constraints
);
175 constraints
.gridx
= 0;
176 constraints
.gridy
= 1;
177 constraints
.gridwidth
= 1;
178 constraints
.anchor
= GridBagConstraints
.EAST
;
179 panel
.add(minLengthLabel
, constraints
);
181 constraints
.gridx
= 1;
182 constraints
.gridy
= 1;
183 constraints
.anchor
= GridBagConstraints
.WEST
;
184 panel
.add(minLengthField
, constraints
);
186 constraints
.gridx
= 2;
187 constraints
.gridy
= 1;
188 constraints
.anchor
= GridBagConstraints
.EAST
;
189 panel
.add(maxLengthLabel
, constraints
);
191 constraints
.gridx
= 3;
192 constraints
.gridy
= 1;
193 constraints
.anchor
= GridBagConstraints
.WEST
;
194 panel
.add(maxLengthField
, constraints
);