update copyright
[fedora-idea.git] / xml / dom-openapi / src / com / intellij / util / xml / ui / DomCollectionWrapper.java
blobb2c25d91d8711b476ddb99054725fd85989447aa
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.
16 package com.intellij.util.xml.ui;
18 import com.intellij.util.ReflectionUtil;
19 import com.intellij.util.xml.DomElement;
20 import com.intellij.util.xml.reflect.DomCollectionChildDescription;
21 import org.jetbrains.annotations.NotNull;
23 import java.lang.reflect.InvocationTargetException;
24 import java.lang.reflect.Method;
25 import java.util.List;
27 /**
28 * @author peter
30 public class DomCollectionWrapper<T> extends DomWrapper<T>{
31 private final DomElement myDomElement;
32 private final DomCollectionChildDescription myChildDescription;
33 private final Method mySetter;
34 private final Method myGetter;
36 public DomCollectionWrapper(final DomElement domElement,
37 final DomCollectionChildDescription childDescription) {
38 this(domElement, childDescription,
39 DomUIFactory.findMethod(ReflectionUtil.getRawType(childDescription.getType()), "setValue"),
40 DomUIFactory.findMethod(ReflectionUtil.getRawType(childDescription.getType()), "getValue"));
43 public DomCollectionWrapper(final DomElement domElement,
44 final DomCollectionChildDescription childDescription,
45 final Method setter,
46 final Method getter) {
47 myDomElement = domElement;
48 myChildDescription = childDescription;
49 mySetter = setter;
50 myGetter = getter;
53 @NotNull
54 public DomElement getExistingDomElement() {
55 return myDomElement;
58 public DomElement getWrappedElement() {
59 final List<? extends DomElement> list = myChildDescription.getValues(myDomElement);
60 return list.isEmpty() ? null : list.get(0);
63 public void setValue(final T value) throws IllegalAccessException, InvocationTargetException {
64 final List<? extends DomElement> list = myChildDescription.getValues(myDomElement);
65 final DomElement domElement;
66 if (list.isEmpty()) {
67 domElement = myChildDescription.addValue(myDomElement);
68 } else {
69 domElement = list.get(0);
71 mySetter.invoke(domElement, value);
74 public T getValue() throws IllegalAccessException, InvocationTargetException {
75 if (!myDomElement.isValid()) return null;
76 final List<? extends DomElement> list = myChildDescription.getValues(myDomElement);
77 return list.isEmpty() ? null : (T)myGetter.invoke(list.get(0));