PageRenderTime 75ms CodeModel.GetById 30ms RepoModel.GetById 5ms app.codeStats 0ms

/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/descriptors/TextAttributeDescriptor.java

https://github.com/CyanogenMod/android_sdk
Java | 290 lines | 169 code | 40 blank | 81 comment | 35 complexity | 462dd43a330034e408c6d894604b731d MD5 | raw file
  1. /*
  2. * Copyright (C) 2007 The Android Open Source Project
  3. *
  4. * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
  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.android.ide.eclipse.adt.internal.editors.descriptors;
  17. import com.android.SdkConstants;
  18. import com.android.annotations.NonNull;
  19. import com.android.annotations.Nullable;
  20. import com.android.ide.common.api.IAttributeInfo;
  21. import com.android.ide.common.api.IAttributeInfo.Format;
  22. import com.android.ide.eclipse.adt.internal.editors.ui.TextValueCellEditor;
  23. import com.android.ide.eclipse.adt.internal.editors.uimodel.UiAttributeNode;
  24. import com.android.ide.eclipse.adt.internal.editors.uimodel.UiElementNode;
  25. import com.android.ide.eclipse.adt.internal.editors.uimodel.UiTextAttributeNode;
  26. import org.eclipse.jface.viewers.CellEditor;
  27. import org.eclipse.jface.viewers.ILabelProvider;
  28. import org.eclipse.swt.widgets.Composite;
  29. import org.eclipse.swt.widgets.Control;
  30. import org.eclipse.ui.views.properties.IPropertyDescriptor;
  31. import java.util.EnumSet;
  32. import java.util.Locale;
  33. /**
  34. * Describes a textual XML attribute.
  35. * <p/>
  36. * Such an attribute has a tooltip and would typically be displayed by
  37. * {@link UiTextAttributeNode} using a label widget and text field.
  38. * <p/>
  39. * This is the "default" kind of attribute. If in doubt, use this.
  40. */
  41. public class TextAttributeDescriptor extends AttributeDescriptor implements IPropertyDescriptor {
  42. public static final String DEPRECATED_CATEGORY = "Deprecated";
  43. private String mUiName;
  44. private String mTooltip;
  45. private boolean mRequired;
  46. /**
  47. * Creates a new {@link TextAttributeDescriptor}
  48. *
  49. * @param xmlLocalName The XML name of the attribute (case sensitive)
  50. * @param nsUri The URI of the attribute. Can be null if attribute has no namespace.
  51. * See {@link SdkConstants#NS_RESOURCES} for a common value.
  52. * @param attrInfo The {@link IAttributeInfo} of this attribute. Can't be null.
  53. */
  54. public TextAttributeDescriptor(
  55. String xmlLocalName,
  56. String nsUri,
  57. IAttributeInfo attrInfo) {
  58. super(xmlLocalName, nsUri, attrInfo);
  59. }
  60. /**
  61. * @return The UI name of the attribute. Cannot be an empty string and cannot be null.
  62. */
  63. @NonNull
  64. public String getUiName() {
  65. if (mUiName == null) {
  66. IAttributeInfo info = getAttributeInfo();
  67. if (info != null) {
  68. mUiName = DescriptorsUtils.prettyAttributeUiName(info.getName());
  69. if (mRequired) {
  70. mUiName += "*"; //$NON-NLS-1$
  71. }
  72. } else {
  73. mUiName = getXmlLocalName();
  74. }
  75. }
  76. return mUiName;
  77. }
  78. /**
  79. * Sets the UI name to be associated with this descriptor. This is usually
  80. * computed lazily from the {@link #getAttributeInfo()} data, but for some
  81. * hardcoded/builtin descriptor this is manually initialized.
  82. *
  83. * @param uiName the new UI name to be used
  84. * @return this, for constructor setter chaining
  85. */
  86. public TextAttributeDescriptor setUiName(String uiName) {
  87. mUiName = uiName;
  88. return this;
  89. }
  90. /**
  91. * Sets the tooltip to be associated with this descriptor. This is usually
  92. * computed lazily from the {@link #getAttributeInfo()} data, but for some
  93. * hardcoded/builtin descriptor this is manually initialized.
  94. *
  95. * @param tooltip the new tooltip to be used
  96. * @return this, for constructor setter chaining
  97. */
  98. public TextAttributeDescriptor setTooltip(String tooltip) {
  99. mTooltip = tooltip;
  100. return this;
  101. }
  102. /**
  103. * Sets whether this attribute is required
  104. *
  105. * @param required whether this attribute is required
  106. * @return this, for constructor setter chaining
  107. */
  108. public TextAttributeDescriptor setRequired(boolean required) {
  109. mRequired = required;
  110. return this;
  111. }
  112. /**
  113. * Returns whether this attribute is required
  114. *
  115. * @return whether this attribute is required
  116. */
  117. public boolean isRequired() {
  118. return mRequired;
  119. }
  120. /**
  121. * The tooltip string is either null or a non-empty string.
  122. * <p/>
  123. * The tooltip is based on the Javadoc of the attribute and already processed via
  124. * {@link DescriptorsUtils#formatTooltip(String)} to be displayed right away as
  125. * a UI tooltip.
  126. * <p/>
  127. * An empty string is converted to null, to match the behavior of setToolTipText() in
  128. * {@link Control}.
  129. *
  130. * @return A non-empty tooltip string or null
  131. */
  132. @Nullable
  133. public String getTooltip() {
  134. if (mTooltip == null) {
  135. IAttributeInfo info = getAttributeInfo();
  136. if (info == null) {
  137. mTooltip = "";
  138. return mTooltip;
  139. }
  140. String tooltip = null;
  141. String rawTooltip = info.getJavaDoc();
  142. if (rawTooltip == null) {
  143. rawTooltip = "";
  144. }
  145. String deprecated = info.getDeprecatedDoc();
  146. if (deprecated != null) {
  147. if (rawTooltip.length() > 0) {
  148. rawTooltip += "@@"; //$NON-NLS-1$ insert a break
  149. }
  150. rawTooltip += "* Deprecated";
  151. if (deprecated.length() != 0) {
  152. rawTooltip += ": " + deprecated; //$NON-NLS-1$
  153. }
  154. if (deprecated.length() == 0 || !deprecated.endsWith(".")) { //$NON-NLS-1$
  155. rawTooltip += "."; //$NON-NLS-1$
  156. }
  157. }
  158. // Add the known types to the tooltip
  159. EnumSet<Format> formats_list = info.getFormats();
  160. int flen = formats_list.size();
  161. if (flen > 0) {
  162. StringBuilder sb = new StringBuilder();
  163. if (rawTooltip != null && rawTooltip.length() > 0) {
  164. sb.append(rawTooltip);
  165. sb.append(" "); //$NON-NLS-1$
  166. }
  167. if (sb.length() > 0) {
  168. sb.append("@@"); //$NON-NLS-1$ @@ inserts a break before the types
  169. }
  170. sb.append("["); //$NON-NLS-1$
  171. boolean isFirst = true;
  172. for (Format f : formats_list) {
  173. if (isFirst) {
  174. isFirst = false;
  175. } else {
  176. sb.append(", ");
  177. }
  178. sb.append(f.toString().toLowerCase(Locale.US));
  179. }
  180. // The extra space at the end makes the tooltip more readable on Windows.
  181. sb.append("]"); //$NON-NLS-1$
  182. if (mRequired) {
  183. // Note: this string is split in 2 to make it translatable.
  184. sb.append(".@@"); //$NON-NLS-1$ @@ inserts a break and is not translatable
  185. sb.append("* Required.");
  186. }
  187. // The extra space at the end makes the tooltip more readable on Windows.
  188. sb.append(" "); //$NON-NLS-1$
  189. rawTooltip = sb.toString();
  190. tooltip = DescriptorsUtils.formatTooltip(rawTooltip);
  191. }
  192. if (tooltip == null) {
  193. tooltip = DescriptorsUtils.formatTooltip(rawTooltip);
  194. }
  195. mTooltip = tooltip;
  196. }
  197. return mTooltip.isEmpty() ? null : mTooltip;
  198. }
  199. /**
  200. * @return A new {@link UiTextAttributeNode} linked to this descriptor.
  201. */
  202. @Override
  203. public UiAttributeNode createUiNode(UiElementNode uiParent) {
  204. return new UiTextAttributeNode(this, uiParent);
  205. }
  206. // ------- IPropertyDescriptor Methods
  207. @Override
  208. public CellEditor createPropertyEditor(Composite parent) {
  209. return new TextValueCellEditor(parent);
  210. }
  211. @Override
  212. public String getCategory() {
  213. if (isDeprecated()) {
  214. return DEPRECATED_CATEGORY;
  215. }
  216. ElementDescriptor parent = getParent();
  217. if (parent != null) {
  218. return parent.getUiName();
  219. }
  220. return null;
  221. }
  222. @Override
  223. public String getDescription() {
  224. return getTooltip();
  225. }
  226. @Override
  227. public String getDisplayName() {
  228. return getUiName();
  229. }
  230. @Override
  231. public String[] getFilterFlags() {
  232. return null;
  233. }
  234. @Override
  235. public Object getHelpContextIds() {
  236. return null;
  237. }
  238. @Override
  239. public Object getId() {
  240. return this;
  241. }
  242. @Override
  243. public ILabelProvider getLabelProvider() {
  244. return AttributeDescriptorLabelProvider.getProvider();
  245. }
  246. @Override
  247. public boolean isCompatibleWith(IPropertyDescriptor anotherProperty) {
  248. return anotherProperty == this;
  249. }
  250. }