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.color; 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.ElementDescriptor; 26 import com.android.ide.eclipse.adt.internal.sdk.AndroidTargetData; 27 import com.android.resources.ResourceFolderType; 28 29 import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; 30 import org.w3c.dom.Document; 31 import org.w3c.dom.Element; 32 import org.w3c.dom.Node; 33 34 /** 35 * Editor for /res/color XML files. 36 */ 37 @SuppressWarnings("restriction") 38 public class ColorEditorDelegate extends CommonXmlDelegate { 39 40 public static class Creator implements IDelegateCreator { 41 @Override 42 @SuppressWarnings("unchecked") 43 public ColorEditorDelegate createForFile( 44 @NonNull CommonXmlEditor delegator, 45 @Nullable ResourceFolderType type) { 46 if (ResourceFolderType.COLOR == type) { 47 return new ColorEditorDelegate(delegator); 48 } 49 50 return null; 51 } 52 } 53 54 /** 55 * Old standalone-editor ID. 56 * Use {@link CommonXmlEditor#ID} instead. 57 */ 58 public static final String LEGACY_EDITOR_ID = 59 EDITORS_NAMESPACE + ".color.ColorEditor"; //$NON-NLS-1$ 60 61 62 private ColorEditorDelegate(CommonXmlEditor editor) { 63 super(editor, new ColorContentAssist()); 64 editor.addDefaultTargetListener(); 65 } 66 67 @Override 68 public void delegateCreateFormPages() { 69 /* Disabled for now; doesn't work quite right 70 try { 71 addPage(new ColorTreePage(this)); 72 } catch (PartInitException e) { 73 AdtPlugin.log(IStatus.ERROR, "Error creating nested page"); //$NON-NLS-1$ 74 AdtPlugin.getDefault().getLog().log(e.getStatus()); 75 } 76 */ 77 } 78 79 @Override 80 public void delegateXmlModelChanged(Document xmlDoc) { 81 // create the ui root node on demand. 82 delegateInitUiRootNode(false /*force*/); 83 84 Element rootElement = xmlDoc.getDocumentElement(); 85 getUiRootNode().loadFromXmlNode(rootElement); 86 } 87 88 @Override 89 public void delegateInitUiRootNode(boolean force) { 90 // The manifest UI node is always created, even if there's no corresponding XML node. 91 if (getUiRootNode() == null || force) { 92 ElementDescriptor descriptor; 93 AndroidTargetData data = getEditor().getTargetData(); 94 if (data == null) { 95 descriptor = new ColorDescriptors().getDescriptor(); 96 } else { 97 descriptor = data.getColorDescriptors().getDescriptor(); 98 } 99 setUiRootNode(descriptor.createUiNode()); 100 getUiRootNode().setEditor(getEditor()); 101 onDescriptorsChanged(); 102 } 103 } 104 105 private void onDescriptorsChanged() { 106 IStructuredModel model = getEditor().getModelForRead(); 107 if (model != null) { 108 try { 109 Node node = getEditor().getXmlDocument(model).getDocumentElement(); 110 getUiRootNode().reloadFromXmlNode(node); 111 } finally { 112 model.releaseFromRead(); 113 } 114 } 115 } 116 } 117