update copyright
[fedora-idea.git] / plugins / ui-designer / src / com / intellij / uiDesigner / propertyInspector / editors / IntRegexEditor.java
blob446dd064d84b14a756b2f4ce66bb494a93e29a1a
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
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.
17 package com.intellij.uiDesigner.propertyInspector.editors;
19 import com.intellij.uiDesigner.propertyInspector.renderers.LabelPropertyRenderer;
20 import com.intellij.uiDesigner.radComponents.RadComponent;
21 import com.intellij.uiDesigner.radComponents.RadRootContainer;
22 import com.intellij.uiDesigner.UIDesignerBundle;
23 import com.intellij.uiDesigner.FormEditingUtil;
24 import org.jetbrains.annotations.NonNls;
26 import javax.swing.*;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
30 /**
31 * @author yole
33 public class IntRegexEditor<T> extends AbstractTextFieldEditor<T> {
34 @NonNls private final Pattern myPattern;
35 private final Class<T> myValueClass;
36 private final LabelPropertyRenderer<T> myRenderer;
37 private final int[] myMinValues;
39 public IntRegexEditor(Class<T> valueClass, LabelPropertyRenderer<T> renderer, final int[] minValues) {
40 myMinValues = minValues;
41 myValueClass = valueClass;
42 myRenderer = renderer;
44 @NonNls StringBuilder regexBuilder = new StringBuilder("\\[?(-?\\d+)");
45 for(int i=1; i<myMinValues.length; i++) {
46 regexBuilder.append(",\\s*(-?\\d+)");
48 regexBuilder.append("\\]?");
49 myPattern = Pattern.compile(regexBuilder.toString());
52 @Override
53 protected void setValueFromComponent(final RadComponent component, final T value) {
54 RadRootContainer root = (RadRootContainer) FormEditingUtil.getRoot(component);
55 JLabel label = myRenderer.getComponent(root, value, false, false);
56 myTf.setText(label.getText());
59 public T getValue() throws Exception {
60 final Matcher matcher = myPattern.matcher(myTf.getText());
61 if (!matcher.matches()) {
62 throw new Exception("Incorrect dimension format");
65 Class[] paramTypes = new Class[myMinValues.length];
66 Integer[] params = new Integer[myMinValues.length];
67 for(int i=0; i<myMinValues.length; i++) {
68 paramTypes [i] = int.class;
69 final int value = Integer.parseInt(matcher.group(i + 1));
70 if (value < myMinValues [i]) {
71 throw new RuntimeException(UIDesignerBundle.message("error.value.should.not.be.less", myMinValues [i]));
73 params [i] = value;
76 return myValueClass.getConstructor(paramTypes).newInstance(params);