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
.util
.xml
.actions
.generate
;
19 import com
.intellij
.openapi
.editor
.Editor
;
20 import com
.intellij
.openapi
.project
.Project
;
21 import com
.intellij
.psi
.PsiFile
;
22 import com
.intellij
.psi
.xml
.XmlElement
;
23 import com
.intellij
.util
.ReflectionUtil
;
24 import com
.intellij
.util
.xml
.DomElement
;
25 import com
.intellij
.util
.xml
.ui
.actions
.generate
.GenerateDomElementProvider
;
26 import com
.intellij
.util
.xml
.reflect
.DomCollectionChildDescription
;
27 import org
.jetbrains
.annotations
.Nullable
;
29 import java
.util
.List
;
32 * User: Sergey.Vasiliev
34 public abstract class DefaultGenerateElementProvider
<T
extends DomElement
> extends GenerateDomElementProvider
<T
> {
35 private final Class
<?
extends DomElement
> myChildElementClass
;
37 public DefaultGenerateElementProvider(final String name
, Class
<T
> childElementClass
) {
40 myChildElementClass
= childElementClass
;
45 public T
generate(final Project project
, final Editor editor
, final PsiFile file
) {
46 return generate(getParentDomElement(project
, editor
, file
), editor
);
50 protected abstract DomElement
getParentDomElement(final Project project
, final Editor editor
, final PsiFile file
);
53 public T
generate(@Nullable final DomElement parent
, final Editor editor
) {
55 final List
<?
extends DomCollectionChildDescription
> list
= parent
.getGenericInfo().getCollectionChildrenDescriptions();
57 for (DomCollectionChildDescription childDescription
: list
) {
58 if (ReflectionUtil
.getRawType(childDescription
.getType()).isAssignableFrom(myChildElementClass
)) {
59 int index
= getCollectionIndex(parent
, childDescription
, editor
);
61 return index
< 0 ?
(T
)childDescription
.addValue(parent
, myChildElementClass
) : (T
)childDescription
.addValue(parent
, myChildElementClass
, index
) ;
68 private static int getCollectionIndex(final DomElement parent
, final DomCollectionChildDescription childDescription
, final Editor editor
) {
69 int offset
= editor
.getCaretModel().getOffset();
71 for (int i
= 0; i
< childDescription
.getValues(parent
).size(); i
++) {
72 DomElement element
= childDescription
.getValues(parent
).get(i
);
73 XmlElement xmlElement
= element
.getXmlElement();
74 if (xmlElement
!= null && xmlElement
.getTextRange().getStartOffset() >= offset
) {