| 1 | /******************************************************************************* |
|---|---|
| 2 | * Copyright (c) 2000, 2015 IBM Corporation and others. |
| 3 | * |
| 4 | * This program and the accompanying materials |
| 5 | * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | * which accompanies this distribution, and is available at |
| 7 | * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | * |
| 9 | * SPDX-License-Identifier: EPL-2.0 |
| 10 | * |
| 11 | * Contributors: |
| 12 | * IBM Corporation - initial API and implementation |
| 13 | *******************************************************************************/ |
| 14 | package org.eclipse.jdt.astview.views; |
| 15 | |
| 16 | import java.util.List; |
| 17 | |
| 18 | import org.eclipse.swt.graphics.Image; |
| 19 | |
| 20 | import org.eclipse.jdt.core.dom.ASTNode; |
| 21 | import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor; |
| 22 | |
| 23 | public class NodeProperty extends ASTAttribute { |
| 24 | |
| 25 | private ASTNode fParent; |
| 26 | private StructuralPropertyDescriptor fProperty; |
| 27 | |
| 28 | public NodeProperty(ASTNode parent, StructuralPropertyDescriptor property) { |
| 29 | fParent= parent; |
| 30 | fProperty= property; |
| 31 | } |
| 32 | |
| 33 | @Override |
| 34 | public Object getParent() { |
| 35 | return fParent; |
| 36 | } |
| 37 | |
| 38 | @Override |
| 39 | public Object[] getChildren() { |
| 40 | Object child= getNode(); |
| 41 | if (child instanceof List) { |
| 42 | return ((List<?>) child).toArray(); |
| 43 | } else if (child instanceof ASTNode) { |
| 44 | return new Object[] { child }; |
| 45 | } |
| 46 | return EMPTY; |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public String getLabel() { |
| 51 | StringBuilder buf= new StringBuilder(); |
| 52 | buf.append(getPropertyName()); |
| 53 | |
| 54 | if (fProperty.isSimpleProperty()) { |
| 55 | buf.append(": "); //$NON-NLS-1$ |
| 56 | Object node= getNode(); |
| 57 | if (node != null) { |
| 58 | buf.append('\''); |
| 59 | buf.append(getNode().toString()); |
| 60 | buf.append('\''); |
| 61 | } else { |
| 62 | buf.append("null"); //$NON-NLS-1$ |
| 63 | } |
| 64 | } else if (fProperty.isChildListProperty()) { |
| 65 | List<?> node= (List<?>) getNode(); |
| 66 | buf.append(" (").append(node.size()).append(')'); //$NON-NLS-1$ |
| 67 | } else { // child property |
| 68 | if (getNode() == null) { |
| 69 | buf.append(": null"); //$NON-NLS-1$ |
| 70 | } |
| 71 | } |
| 72 | return buf.toString(); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public Image getImage() { |
| 77 | return null; |
| 78 | } |
| 79 | |
| 80 | public Object getNode() { |
| 81 | return fParent.getStructuralProperty(fProperty); |
| 82 | } |
| 83 | |
| 84 | public String getPropertyName() { |
| 85 | return toConstantName(fProperty.getId()); |
| 86 | } |
| 87 | |
| 88 | private static String toConstantName(String string) { |
| 89 | StringBuilder buf= new StringBuilder(); |
| 90 | for (int i= 0; i < string.length(); i++) { |
| 91 | char ch= string.charAt(i); |
| 92 | if (i != 0 && Character.isUpperCase(ch)) { |
| 93 | buf.append('_'); |
| 94 | } |
| 95 | buf.append(Character.toUpperCase(ch)); |
| 96 | } |
| 97 | return buf.toString(); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public boolean equals(Object o) { |
| 102 | if (this == o) { |
| 103 | return true; |
| 104 | } |
| 105 | if (o == null || !o.getClass().equals(getClass())) { |
| 106 | return false; |
| 107 | } |
| 108 | NodeProperty castedObj= (NodeProperty) o; |
| 109 | return fParent.equals(castedObj.fParent) && (fProperty == castedObj.fProperty); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public int hashCode() { |
| 114 | return fParent.hashCode() * 31 + fProperty.hashCode(); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public String toString() { |
| 119 | return getLabel(); |
| 120 | |
| 121 | } |
| 122 | } |
| 123 |
Members