| 1 | /******************************************************************************* |
|---|---|
| 2 | * Copyright (c) 2000, 2017 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 | |
| 17 | import java.util.Objects; |
| 18 | |
| 19 | import org.eclipse.swt.graphics.Image; |
| 20 | |
| 21 | import org.eclipse.jdt.core.dom.IBinding; |
| 22 | |
| 23 | /** |
| 24 | * |
| 25 | */ |
| 26 | public class BindingProperty extends ASTAttribute { |
| 27 | |
| 28 | private final String fName; |
| 29 | private final Binding fParent; |
| 30 | private final ASTAttribute[] fValues; |
| 31 | private final boolean fIsRelevant; |
| 32 | |
| 33 | public BindingProperty(Binding parent, String name, Object value, boolean isRelevant) { |
| 34 | fParent= parent; |
| 35 | if (value instanceof String) { |
| 36 | if (((String) value).length() == 0) { |
| 37 | fName= name + ": (empty string)"; //$NON-NLS-1$ |
| 38 | } else { |
| 39 | fName= name + ": " + Binding.getEscapedStringLiteral((String) value); //$NON-NLS-1$ |
| 40 | } |
| 41 | } else if (value instanceof Character) { |
| 42 | fName= name + ": " + Binding.getEscapedCharLiteral(((Character) value)); //$NON-NLS-1$ |
| 43 | } else { |
| 44 | fName= name + ": " + String.valueOf(value); //$NON-NLS-1$ |
| 45 | } |
| 46 | fValues= null; |
| 47 | fIsRelevant= isRelevant; |
| 48 | } |
| 49 | |
| 50 | public BindingProperty(Binding parent, String name, boolean value, boolean isRelevant) { |
| 51 | fParent= parent; |
| 52 | fName= name + ": " + String.valueOf(value); //$NON-NLS-1$ |
| 53 | fValues= null; |
| 54 | fIsRelevant= isRelevant; |
| 55 | } |
| 56 | |
| 57 | public BindingProperty(Binding parent, String name, int value, boolean isRelevant) { |
| 58 | fParent= parent; |
| 59 | fName= name + ": " + String.valueOf(value); //$NON-NLS-1$ |
| 60 | fValues= null; |
| 61 | fIsRelevant= isRelevant; |
| 62 | } |
| 63 | |
| 64 | public BindingProperty(Binding parent, String name, IBinding[] bindings, boolean isRelevant) { |
| 65 | fParent= parent; |
| 66 | if (bindings == null) { |
| 67 | fName= name + " (null)"; //$NON-NLS-1$ |
| 68 | fValues= null; |
| 69 | } else { |
| 70 | fValues= createBindings(bindings, isRelevant); |
| 71 | fName= name + " (" + fValues.length + ')'; //$NON-NLS-1$ |
| 72 | } |
| 73 | fIsRelevant= isRelevant; |
| 74 | } |
| 75 | |
| 76 | public BindingProperty(Binding parent, String name, ASTAttribute[] children, boolean isRelevant) { |
| 77 | fParent= parent; |
| 78 | if (children == null) { |
| 79 | children= new ASTAttribute[0]; |
| 80 | } |
| 81 | fValues= children; |
| 82 | fName= name + " (" + fValues.length + ')'; //$NON-NLS-1$ |
| 83 | fIsRelevant= isRelevant; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | @Deprecated |
| 88 | public BindingProperty(Binding parent, StringBuffer label, boolean isRelevant) { |
| 89 | fParent= parent; |
| 90 | fName= label.toString(); |
| 91 | fValues= null; |
| 92 | fIsRelevant= isRelevant; |
| 93 | } |
| 94 | |
| 95 | public BindingProperty(Binding parent, StringBuilder label, boolean isRelevant) { |
| 96 | fParent= parent; |
| 97 | fName= label.toString(); |
| 98 | fValues= null; |
| 99 | fIsRelevant= isRelevant; |
| 100 | } |
| 101 | |
| 102 | private Binding[] createBindings(IBinding[] bindings, boolean isRelevant) { |
| 103 | Binding[] res= new Binding[bindings.length]; |
| 104 | for (int i= 0; i < res.length; i++) { |
| 105 | res[i]= new Binding(this, String.valueOf(i), bindings[i], isRelevant); |
| 106 | } |
| 107 | return res; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public Object getParent() { |
| 112 | return fParent; |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public Object[] getChildren() { |
| 117 | if (fValues != null) { |
| 118 | return fValues; |
| 119 | } |
| 120 | return EMPTY; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public String getLabel() { |
| 125 | return fName; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public Image getImage() { |
| 130 | return null; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public String toString() { |
| 135 | return getLabel(); |
| 136 | } |
| 137 | |
| 138 | public boolean isRelevant() { |
| 139 | return fIsRelevant; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * @see java.lang.Object#equals(java.lang.Object) |
| 144 | */ |
| 145 | @Override |
| 146 | public boolean equals(Object obj) { |
| 147 | if (this == obj) |
| 148 | return true; |
| 149 | if (obj == null || !obj.getClass().equals(getClass())) { |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | BindingProperty other= (BindingProperty) obj; |
| 154 | if (!Objects.equals(fParent, other.fParent)) { |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | if (!Objects.equals(fName, other.fName)) { |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * @see java.lang.Object#hashCode() |
| 167 | */ |
| 168 | @Override |
| 169 | public int hashCode() { |
| 170 | return (fParent != null ? fParent.hashCode() : 0) |
| 171 | + (fName != null ? fName.hashCode() : 0); |
| 172 | } |
| 173 | } |
| 174 |
Members