1 /* 2 * Copyright 2000-2005 JetBrains s.r.o. 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 package com.intellij.lang.javascript.psi.impl; 17 18 import com.intellij.lang.ASTNode; 19 import com.intellij.lang.javascript.JSElementTypes; 20 import com.intellij.lang.javascript.JSTokenTypes; 21 import com.intellij.lang.javascript.psi.JSElementVisitor; 22 import com.intellij.lang.javascript.psi.JSExpression; 23 import com.intellij.lang.javascript.psi.JSProperty; 24 import com.intellij.psi.PsiElement; 25 import com.intellij.psi.PsiElementVisitor; 26 import com.intellij.util.Icons; 27 import com.intellij.util.IncorrectOperationException; 28 29 import javax.swing.*; 30 31 /** 32 * Created by IntelliJ IDEA. 33 * User: max 34 * Date: Jan 30, 2005 35 * Time: 11:39:23 PM 36 * To change this template use File | Settings | File Templates. 37 */ 38 public class JSPropertyImpl extends JSElementImpl implements JSProperty { 39 public JSPropertyImpl(final ASTNode node) { 40 super(node); 41 } 42 43 public String getName() { 44 final ASTNode node = findNameIdentifier(); 45 return node != null ? node.getText() : null; 46 } 47 48 private ASTNode findNameIdentifier() { 49 return getNode().findChildByType(JSTokenTypes.IDENTIFIER); 50 } 51 52 public PsiElement setName(String name) throws IncorrectOperationException { 53 final ASTNode nameElement = JSChangeUtil.createNameIdentifier(getProject(), name); 54 getNode().replaceChild(getNode().getFirstChildNode(), nameElement); 55 return this; 56 } 57 58 public JSExpression getValue() { 59 final ASTNode[] nodes = getNode().getChildren(JSElementTypes.EXPRESSIONS); 60 return (JSExpression)(nodes.length == 1 ? nodes[0].getPsi() : null); 61 } 62 63 public void accept(PsiElementVisitor visitor) { 64 if (visitor instanceof JSElementVisitor) { 65 ((JSElementVisitor)visitor).visitJSProperty(this); 66 } 67 else { 68 visitor.visitElement(this); 69 } 70 } 71 72 public int getTextOffset() { 73 final ASTNode name = findNameIdentifier(); 74 return name != null ? name.getStartOffset() : super.getTextOffset(); 75 } 76 77 public Icon getIcon(int flags) { 78 return Icons.PROPERTY_ICON; 79 } 80 81 public String toString() { 82 return "JSProperty"; 83 } 84 } 85