Home | History | Annotate | Download | only in drawable
      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 
     17 package com.android.ide.eclipse.adt.internal.editors.drawable;
     18 
     19 import static com.android.ide.eclipse.adt.AdtConstants.EDITORS_NAMESPACE;
     20 
     21 import com.android.annotations.NonNull;
     22 import com.android.annotations.Nullable;
     23 import com.android.ide.eclipse.adt.internal.editors.common.CommonXmlDelegate;
     24 import com.android.ide.eclipse.adt.internal.editors.common.CommonXmlEditor;
     25 import com.android.ide.eclipse.adt.internal.editors.descriptors.DocumentDescriptor;
     26 import com.android.ide.eclipse.adt.internal.editors.descriptors.ElementDescriptor;
     27 import com.android.ide.eclipse.adt.internal.sdk.AndroidTargetData;
     28 import com.android.resources.ResourceFolderType;
     29 
     30 import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
     31 import org.w3c.dom.Document;
     32 import org.w3c.dom.Element;
     33 import org.w3c.dom.Node;
     34 
     35 /**
     36  * Editor for /res/drawable XML files.
     37  */
     38 @SuppressWarnings("restriction")
     39 public class DrawableEditorDelegate extends CommonXmlDelegate {
     40 
     41     public static class Creator implements IDelegateCreator {
     42         @Override
     43         @SuppressWarnings("unchecked")
     44         public DrawableEditorDelegate createForFile(
     45                 @NonNull CommonXmlEditor delegator,
     46                 @Nullable ResourceFolderType type) {
     47             if (ResourceFolderType.DRAWABLE == type) {
     48                 return new DrawableEditorDelegate(delegator);
     49             }
     50 
     51             return null;
     52         }
     53     }
     54 
     55     /**
     56      * Old standalone-editor ID.
     57      * Use {@link CommonXmlEditor#ID} instead.
     58      */
     59     public static final String LEGACY_EDITOR_ID =
     60         EDITORS_NAMESPACE + ".drawable.DrawableEditor"; //$NON-NLS-1$
     61 
     62     /** The tag used at the root */
     63     private String mRootTag;
     64 
     65     /**
     66      * Creates the form editor for resources XML files.
     67      */
     68     private DrawableEditorDelegate(CommonXmlEditor editor) {
     69         super(editor, new DrawableContentAssist());
     70         editor.addDefaultTargetListener();
     71     }
     72 
     73     @Override
     74     public void delegateCreateFormPages() {
     75         /* Disabled for now; doesn't work quite right
     76         try {
     77             addPage(new DrawableTreePage(this));
     78         } catch (PartInitException e) {
     79             AdtPlugin.log(IStatus.ERROR, "Error creating nested page"); //$NON-NLS-1$
     80             AdtPlugin.getDefault().getLog().log(e.getStatus());
     81         }
     82         */
     83      }
     84 
     85     @Override
     86     public void delegateXmlModelChanged(Document xmlDoc) {
     87         Element rootElement = xmlDoc.getDocumentElement();
     88         if (rootElement != null) {
     89             mRootTag = rootElement.getTagName();
     90         }
     91 
     92         delegateInitUiRootNode(false /*force*/);
     93 
     94         if (mRootTag != null
     95                 && !mRootTag.equals(getUiRootNode().getDescriptor().getXmlLocalName())) {
     96             AndroidTargetData data = getEditor().getTargetData();
     97             if (data != null) {
     98                 ElementDescriptor descriptor =
     99                     data.getDrawableDescriptors().getElementDescriptor(mRootTag);
    100                 // Replace top level node now that we know the actual type
    101 
    102                 // Disconnect from old
    103                 getUiRootNode().setEditor(null);
    104                 getUiRootNode().setXmlDocument(null);
    105 
    106                 // Create new
    107                 setUiRootNode(descriptor.createUiNode());
    108                 getUiRootNode().setXmlDocument(xmlDoc);
    109                 getUiRootNode().setEditor(getEditor());
    110             }
    111         }
    112 
    113         if (getUiRootNode().getDescriptor() instanceof DocumentDescriptor) {
    114             getUiRootNode().loadFromXmlNode(xmlDoc);
    115         } else {
    116             getUiRootNode().loadFromXmlNode(rootElement);
    117         }
    118     }
    119 
    120     @Override
    121     public void delegateInitUiRootNode(boolean force) {
    122         // The manifest UI node is always created, even if there's no corresponding XML node.
    123         if (getUiRootNode() == null || force) {
    124             ElementDescriptor descriptor;
    125             boolean reload = false;
    126             AndroidTargetData data = getEditor().getTargetData();
    127             if (data == null) {
    128                 descriptor = new DocumentDescriptor("temp", null /*children*/);
    129             } else {
    130                 descriptor = data.getDrawableDescriptors().getElementDescriptor(mRootTag);
    131                 reload = true;
    132             }
    133             setUiRootNode(descriptor.createUiNode());
    134             getUiRootNode().setEditor(getEditor());
    135 
    136             if (reload) {
    137                 onDescriptorsChanged();
    138             }
    139         }
    140     }
    141 
    142     private void onDescriptorsChanged() {
    143         IStructuredModel model = getEditor().getModelForRead();
    144         if (model != null) {
    145             try {
    146                 Node node = getEditor().getXmlDocument(model).getDocumentElement();
    147                 getUiRootNode().reloadFromXmlNode(node);
    148             } finally {
    149                 model.releaseFromRead();
    150             }
    151         }
    152     }
    153 }
    154