update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInsight / unwrap / JavaElseUnwrapperBase.java
blobbbfbde94b4e38538888d2efa57a5348268508fbe
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.codeInsight.unwrap;
18 import com.intellij.psi.PsiElement;
19 import com.intellij.psi.PsiIfStatement;
20 import com.intellij.psi.PsiStatement;
21 import com.intellij.util.IncorrectOperationException;
23 import java.util.Set;
25 public abstract class JavaElseUnwrapperBase extends JavaUnwrapper {
26 public JavaElseUnwrapperBase(String description) {
27 super(description);
30 public boolean isApplicableTo(PsiElement e) {
31 return (isElseBlock(e) || isElseKeyword(e)) && isValidConstruct(e);
34 private boolean isElseKeyword(PsiElement e) {
35 PsiElement p = e.getParent();
36 return p instanceof PsiIfStatement && e == ((PsiIfStatement)p).getElseElement();
39 private boolean isValidConstruct(PsiElement e) {
40 return ((PsiIfStatement)e.getParent()).getElseBranch() != null;
43 @Override
44 public void collectElementsToIgnore(PsiElement element, Set<PsiElement> result) {
45 PsiElement parent = element.getParent();
47 while (parent instanceof PsiIfStatement) {
48 result.add(parent);
49 parent = parent.getParent();
53 @Override
54 protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException {
55 PsiStatement elseBranch;
57 if (isElseKeyword(element)) {
58 elseBranch = ((PsiIfStatement)element.getParent()).getElseBranch();
60 else {
61 elseBranch = (PsiStatement)element;
64 unwrapElseBranch(elseBranch, element.getParent(), context);
67 protected abstract void unwrapElseBranch(PsiStatement branch, PsiElement parent, Context context) throws IncorrectOperationException;