• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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 
17 package com.android.ide.eclipse.adt.internal.editors.descriptors;
18 
19 import com.android.SdkConstants;
20 import com.android.ide.common.api.IAttributeInfo;
21 import com.android.ide.common.api.IAttributeInfo.Format;
22 import com.android.ide.common.resources.platform.AttributeInfo;
23 import com.android.ide.eclipse.adt.internal.editors.ui.ResourceValueCellEditor;
24 import com.android.ide.eclipse.adt.internal.editors.uimodel.UiAttributeNode;
25 import com.android.ide.eclipse.adt.internal.editors.uimodel.UiElementNode;
26 import com.android.ide.eclipse.adt.internal.editors.uimodel.UiResourceAttributeNode;
27 import com.android.resources.ResourceType;
28 
29 import org.eclipse.jface.viewers.CellEditor;
30 import org.eclipse.swt.widgets.Composite;
31 
32 /**
33  * Describes an XML attribute displayed containing a value or a reference to a resource.
34  * It is displayed by a {@link UiResourceAttributeNode}.
35  */
36 public final class ReferenceAttributeDescriptor extends TextAttributeDescriptor {
37 
38     /**
39      * The {@link ResourceType} that this reference attribute can accept. It can be null,
40      * in which case any reference type can be used.
41      */
42     private ResourceType mResourceType;
43 
44     /**
45      * Used by {@link DescriptorsUtils} to create instances of this descriptor.
46      */
47     public static final ITextAttributeCreator CREATOR = new ITextAttributeCreator() {
48         @Override
49         public TextAttributeDescriptor create(String xmlLocalName,
50                 String nsUri, IAttributeInfo attrInfo) {
51             return new ReferenceAttributeDescriptor(
52                     ResourceType.DRAWABLE,
53                     xmlLocalName, nsUri,
54                     new AttributeInfo(xmlLocalName, Format.REFERENCE_SET));
55         }
56     };
57 
58     /**
59      * Creates a reference attributes that can contain any type of resources.
60      * @param xmlLocalName The XML name of the attribute (case sensitive)
61      * @param nsUri The URI of the attribute. Can be null if attribute has no namespace.
62      *              See {@link SdkConstants#NS_RESOURCES} for a common value.
63      * @param attrInfo The {@link IAttributeInfo} of this attribute. Can't be null.
64      */
ReferenceAttributeDescriptor(String xmlLocalName, String nsUri, IAttributeInfo attrInfo)65     public ReferenceAttributeDescriptor(String xmlLocalName, String nsUri,
66             IAttributeInfo attrInfo) {
67         super(xmlLocalName, nsUri, attrInfo);
68     }
69 
70     /**
71      * Creates a reference attributes that can contain a reference to a specific
72      * {@link ResourceType}.
73      * @param resourceType The specific {@link ResourceType} that this reference attribute supports.
74      * It can be <code>null</code>, in which case, all resource types are supported.
75      * @param xmlLocalName The XML name of the attribute (case sensitive)
76      * @param nsUri The URI of the attribute. Can be null if attribute has no namespace.
77      *              See {@link SdkConstants#NS_RESOURCES} for a common value.
78      * @param attrInfo The {@link IAttributeInfo} of this attribute. Can't be null.
79      */
ReferenceAttributeDescriptor(ResourceType resourceType, String xmlLocalName, String nsUri, IAttributeInfo attrInfo)80     public ReferenceAttributeDescriptor(ResourceType resourceType,
81             String xmlLocalName, String nsUri, IAttributeInfo attrInfo) {
82         super(xmlLocalName, nsUri, attrInfo);
83         mResourceType = resourceType;
84     }
85 
86 
87     /** Returns the {@link ResourceType} that this reference attribute can accept.
88      * It can be null, in which case any reference type can be used. */
getResourceType()89     public ResourceType getResourceType() {
90         return mResourceType;
91     }
92 
93     /**
94      * @return A new {@link UiResourceAttributeNode} linked to this reference descriptor.
95      */
96     @Override
createUiNode(UiElementNode uiParent)97     public UiAttributeNode createUiNode(UiElementNode uiParent) {
98         return new UiResourceAttributeNode(mResourceType, this, uiParent);
99     }
100 
101     // ------- IPropertyDescriptor Methods
102 
103     @Override
createPropertyEditor(Composite parent)104     public CellEditor createPropertyEditor(Composite parent) {
105         return new ResourceValueCellEditor(parent);
106     }
107 
108 }
109