1 /* 2 * Copyright (C) 2011 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.formatting; 17 18 import static org.eclipse.jface.text.formatter.FormattingContextProperties.CONTEXT_MEDIUM; 19 import static org.eclipse.jface.text.formatter.FormattingContextProperties.CONTEXT_PARTITION; 20 import static org.eclipse.jface.text.formatter.FormattingContextProperties.CONTEXT_REGION; 21 22 import com.android.ide.eclipse.adt.internal.preferences.AdtPrefs; 23 24 import org.eclipse.jface.text.TypedPosition; 25 import org.eclipse.jface.text.formatter.FormattingContext; 26 import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; 27 import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument; 28 import org.eclipse.wst.xml.core.internal.formatter.XMLFormatterFormatProcessor; 29 import org.eclipse.wst.xml.core.text.IXMLPartitions; 30 31 /** 32 * Customized version of the builtin XML format processor which delegates to the 33 * Android specific formatter such that applying format on IFiles work as 34 * expected 35 */ 36 @SuppressWarnings("restriction") 37 public class XmlFormatProcessor extends XMLFormatterFormatProcessor { 38 /** Constructs a new {@link XmlFormatProcessor} */ XmlFormatProcessor()39 public XmlFormatProcessor() { 40 } 41 42 @Override formatModel(IStructuredModel structuredModel, int start, int length)43 public void formatModel(IStructuredModel structuredModel, int start, int length) { 44 if (!AdtPrefs.getPrefs().getUseCustomXmlFormatter()) { 45 super.formatModel(structuredModel, start, length); 46 return; 47 } 48 49 AndroidXmlFormatter formatter = new AndroidXmlFormatter(); 50 IStructuredDocument document = structuredModel.getStructuredDocument(); 51 FormattingContext context = new FormattingContext(); 52 context.setProperty(CONTEXT_MEDIUM, document); 53 context.setProperty(CONTEXT_PARTITION, new TypedPosition(start, length, 54 IXMLPartitions.XML_DEFAULT)); 55 context.setProperty(CONTEXT_REGION, new org.eclipse.jface.text.Region(start, length)); 56 formatter.formatMaster(context, document, start, length); 57 } 58 } 59