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.otherxml; 18 19 import com.android.annotations.NonNull; 20 import com.android.annotations.Nullable; 21 import com.android.ide.eclipse.adt.AdtConstants; 22 import com.android.ide.eclipse.adt.AdtPlugin; 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.sdk.AndroidTargetData; 27 import com.android.resources.ResourceFolderType; 28 29 import org.eclipse.ui.PartInitException; 30 import org.w3c.dom.Document; 31 32 /** 33 * Multi-page form editor for /res/xml XML files. 34 */ 35 public class OtherXmlEditorDelegate extends CommonXmlDelegate { 36 37 public static class Creator implements IDelegateCreator { 38 @Override 39 @SuppressWarnings("unchecked") 40 public OtherXmlEditorDelegate createForFile( 41 @NonNull CommonXmlEditor delegator, 42 @Nullable ResourceFolderType type) { 43 if (ResourceFolderType.XML == type) { 44 return new OtherXmlEditorDelegate(delegator); 45 } 46 47 return null; 48 } 49 } 50 51 /** 52 * Old standalone-editor ID. 53 * Use {@link CommonXmlEditor#ID} instead. 54 */ 55 public static final String LEGACY_EDITOR_ID = 56 AdtConstants.EDITORS_NAMESPACE + ".xml.XmlEditor"; //$NON-NLS-1$ 57 58 /** 59 * Creates the form editor for resources XML files. 60 */ 61 public OtherXmlEditorDelegate(CommonXmlEditor editor) { 62 super(editor, new OtherXmlContentAssist()); 63 editor.addDefaultTargetListener(); 64 } 65 66 // ---- Base Class Overrides ---- 67 68 /** 69 * Create the various form pages. 70 */ 71 @Override 72 public void delegateCreateFormPages() { 73 try { 74 getEditor().addPage(new OtherXmlTreePage(getEditor())); 75 } catch (PartInitException e) { 76 AdtPlugin.log(e, "Error creating nested page"); //$NON-NLS-1$ 77 } 78 79 } 80 /** 81 * Processes the new XML Model, which XML root node is given. 82 * 83 * @param xml_doc The XML document, if available, or null if none exists. 84 */ 85 @Override 86 public void delegateXmlModelChanged(Document xml_doc) { 87 // init the ui root on demand 88 delegateInitUiRootNode(false /*force*/); 89 90 getUiRootNode().loadFromXmlNode(xml_doc); 91 } 92 93 /** 94 * Creates the initial UI Root Node, including the known mandatory elements. 95 * @param force if true, a new UiRootNode is recreated even if it already exists. 96 */ 97 @Override 98 public void delegateInitUiRootNode(boolean force) { 99 // The root UI node is always created, even if there's no corresponding XML node. 100 if (getUiRootNode() == null || force) { 101 Document doc = null; 102 if (getUiRootNode() != null) { 103 doc = getUiRootNode().getXmlDocument(); 104 } 105 106 // get the target data from the opened file (and its project) 107 AndroidTargetData data = getEditor().getTargetData(); 108 109 DocumentDescriptor desc; 110 if (data == null) { 111 desc = new DocumentDescriptor("temp", null /*children*/); 112 } else { 113 desc = data.getXmlDescriptors().getDescriptor(); 114 } 115 116 setUiRootNode(desc.createUiNode()); 117 getUiRootNode().setEditor(getEditor()); 118 119 onDescriptorsChanged(doc); 120 } 121 } 122 123 // ---- Local Methods ---- 124 125 /** 126 * Reloads the UI manifest node from the XML, and calls the pages to update. 127 */ 128 private void onDescriptorsChanged(Document document) { 129 if (document != null) { 130 getUiRootNode().loadFromXmlNode(document); 131 } else { 132 getUiRootNode().reloadFromXmlNode(getUiRootNode().getXmlNode()); 133 } 134 } 135 } 136