FileDocCategorySizeDatePackage
LayoutEditor.javaAPI DocAndroid 1.5 API14072Wed May 06 22:41:10 BST 2009com.android.ide.eclipse.editors.layout

LayoutEditor

public class LayoutEditor extends com.android.ide.eclipse.editors.AndroidEditor implements org.eclipse.ui.IPartListener, org.eclipse.ui.IShowEditorInput
Multi-page form editor for /res/layout XML files.

Fields Summary
public static final String
ID
private com.android.ide.eclipse.editors.uimodel.UiDocumentNode
mUiRootNode
Root node of the UI element hierarchy
private AbstractGraphicalLayoutEditor
mGraphicalEditor
private int
mGraphicalEditorIndex
private UiContentOutlinePage
mOutline
Implementation of the {@link IContentOutlinePage} for this editor
private UiPropertySheetPage
mPropertyPage
Custom implementation of {@link IPropertySheetPage} for this editor
private UiEditorActions
mUiEditorActions
Constructors Summary
public LayoutEditor()
Creates the form editor for resources XML files.

   
                 
      
        super();
    
Methods Summary
protected voidcreateFormPages()
Create the various form pages.

        try {
            // The graphical layout editor is now enabled by default.
            // In case there's an issue we provide a way to disable it using an
            // env variable.
            if (System.getenv("ANDROID_DISABLE_LAYOUT") == null) {
                if (mGraphicalEditor == null) {
                    mGraphicalEditor = new GraphicalLayoutEditor(this);
                    mGraphicalEditorIndex = addPage(mGraphicalEditor, getEditorInput());
                    setPageText(mGraphicalEditorIndex, mGraphicalEditor.getTitle());
                } else {
                    mGraphicalEditor.reloadEditor();
                }

                // update the config based on the opened file.
                IEditorInput input = getEditorInput();
                if (input instanceof FileEditorInput) {
                    FileEditorInput fileInput = (FileEditorInput)input;
                    ResourceFolder resFolder = ResourceManager.getInstance().getResourceFolder(
                            fileInput.getFile());
                    if (resFolder != null) {
                        mGraphicalEditor.editNewFile(resFolder.getConfiguration());
                    }
                }

                // put in place the listener to handle layout recompute only when needed.
                getSite().getPage().addPartListener(this);
            }
        } catch (PartInitException e) {
            AdtPlugin.log(e, "Error creating nested page"); //$NON-NLS-1$
        }
     
public voiddispose()

        getSite().getPage().removePartListener(this);

        super.dispose();
    
public voiddoSave(org.eclipse.core.runtime.IProgressMonitor monitor)
Save the XML.

The actual save operation is done in the super class by committing all data to the XML model and then having the Structured XML Editor save the XML.

Here we just need to tell the graphical editor that the model has been saved.

        super.doSave(monitor);
        if (mGraphicalEditor != null) {
            mGraphicalEditor.doSave(monitor);
        }
    
public java.lang.ObjectgetAdapter(java.lang.Class adapter)

        // for the outline, force it to come from the Graphical Editor.
        // This fixes the case where a layout file is opened in XML view first and the outline
        // gets stuck in the XML outline.
        if (IContentOutlinePage.class == adapter && mGraphicalEditor != null) {
            if (mOutline == null) {
                mOutline = new UiContentOutlinePage(mGraphicalEditor, new TreeViewer());
            }
            
            return mOutline;
        }
        
        if (IPropertySheetPage.class == adapter && mGraphicalEditor != null) {
            if (mPropertyPage == null) {
                mPropertyPage = new UiPropertySheetPage();
            }
            
            return mPropertyPage;
        }

        // return default
        return super.getAdapter(adapter);
    
public com.android.ide.eclipse.editors.layout.LayoutEditor$UiEditorActionsgetUiEditorActions()

        if (mUiEditorActions == null) {
            mUiEditorActions = new UiEditorActions();
        }
        return mUiEditorActions;
    
public com.android.ide.eclipse.editors.uimodel.UiDocumentNodegetUiRootNode()

return
The root node of the UI element hierarchy

        return mUiRootNode;
    
private voidhandleNewInput(org.eclipse.ui.IEditorInput input)
Handles a new input, and update the part name.

param
input the new input.

        if (input instanceof FileEditorInput) {
            FileEditorInput fileInput = (FileEditorInput) input;
            IFile file = fileInput.getFile();
            setPartName(String.format("%1$s",
                    file.getName()));
        }
    
protected voidinitUiRootNode(boolean force)

        // The root UI node is always created, even if there's no corresponding XML node.
        if (mUiRootNode == null || force) {
            // get the target data from the opened file (and its project)
            AndroidTargetData data = getTargetData();
            
            Document doc = null;
            if (mUiRootNode != null) {
                doc = mUiRootNode.getXmlDocument();
            }
            
            DocumentDescriptor desc;
            if (data == null) {
                desc = new DocumentDescriptor("temp", null /*children*/);
            } else {
                desc = data.getLayoutDescriptors().getDescriptor();
            }

            // get the descriptors from the data.
            mUiRootNode = (UiDocumentNode) desc.createUiNode();
            mUiRootNode.setEditor(this);

            onDescriptorsChanged(doc);
        }
    
booleanisGraphicalEditorActive()
Returns true if the Graphics editor page is visible. This must be called from the UI thread.

        IWorkbenchPartSite workbenchSite = getSite();
        IWorkbenchPage workbenchPage = workbenchSite.getPage();

        // check if the editor is visible in the workbench page
        if (workbenchPage.isPartVisible(this) && workbenchPage.getActiveEditor() == this) {
            // and then if the page of the editor is visible (not to be confused with
            // the workbench page)
            return mGraphicalEditorIndex == getActivePage();
        }

        return false;
    
public booleanisSaveAsAllowed()
Returns whether the "save as" operation is supported by this editor.

Save-As is a valid operation for the ManifestEditor since it acts on a single source file.

see
IEditorPart

        return true;
    
private voidonDescriptorsChanged(org.w3c.dom.Document document)

        if (document != null) {
            mUiRootNode.loadFromXmlNode(document);
        } else {
            mUiRootNode.reloadFromXmlNode(mUiRootNode.getXmlDocument());
        }
        
        if (mOutline != null) {
            mOutline.reloadModel();
        }
        
        if (mGraphicalEditor != null) {
            mGraphicalEditor.reloadEditor();
            mGraphicalEditor.reloadPalette();
            mGraphicalEditor.recomputeLayout();
        }
    
protected voidpageChange(int newPageIndex)

        super.pageChange(newPageIndex);
        
        if (mGraphicalEditor != null) {
            if (newPageIndex == mGraphicalEditorIndex) {
                mGraphicalEditor.activated();
            } else {
                mGraphicalEditor.deactivated();
            }
        }
    
public voidpartActivated(org.eclipse.ui.IWorkbenchPart part)

        if (part == this) {
            if (mGraphicalEditor != null) {
                if (getActivePage() == mGraphicalEditorIndex) {
                    mGraphicalEditor.activated();
                } else {
                    mGraphicalEditor.deactivated();
                }
            }
        }
    
public voidpartBroughtToTop(org.eclipse.ui.IWorkbenchPart part)

        partActivated(part);
    
public voidpartClosed(org.eclipse.ui.IWorkbenchPart part)

        // pass
    
public voidpartDeactivated(org.eclipse.ui.IWorkbenchPart part)

        if (part == this) {
            if (mGraphicalEditor != null && getActivePage() == mGraphicalEditorIndex) {
                mGraphicalEditor.deactivated();
            }
        }
    
public voidpartOpened(org.eclipse.ui.IWorkbenchPart part)

        EclipseUiHelper.showView(EclipseUiHelper.CONTENT_OUTLINE_VIEW_ID, false /* activate */);
        EclipseUiHelper.showView(EclipseUiHelper.PROPERTY_SHEET_VIEW_ID, false /* activate */);
    
protected voidsetInput(org.eclipse.ui.IEditorInput input)

        super.setInput(input);
        handleNewInput(input);
    
protected voidsetInputWithNotify(org.eclipse.ui.IEditorInput input)

        super.setInputWithNotify(input);
        handleNewInput(input);
    
public voidshowEditorInput(org.eclipse.ui.IEditorInput editorInput)
Called to replace the current {@link IEditorInput} with another one.

This is used when {@link MatchingStrategy} returned true which means we're opening a different configuration of the same layout.

        // save the current editor input.
        doSave(new NullProgressMonitor());
        
        // get the current page
        int currentPage = getActivePage();
        
        // remove the pages, except for the graphical editor, which will be dynamically adapted
        // to the new model.
        // page after the graphical editor:
        int count = getPageCount();
        for (int i = count - 1 ; i > mGraphicalEditorIndex ; i--) {
            removePage(i);
        }
        // pages before the graphical editor
        for (int i = mGraphicalEditorIndex - 1 ; i >= 0 ; i--) {
            removePage(i);
        }
        
        // set the current input.
        setInputWithNotify(editorInput);
        
        // re-create or reload the pages with the default page shown as the previous active page.
        createAndroidPages();
        selectDefaultPage(Integer.toString(currentPage));

        // update the outline
        if (mOutline != null && mGraphicalEditor != null) {
            mOutline.reloadModel();
        }
    
protected voidxmlModelChanged(org.w3c.dom.Document xml_doc)
Processes the new XML Model, which XML root node is given.

param
xml_doc The XML document, if available, or null if none exists.

        // init the ui root on demand
        initUiRootNode(false /*force*/);

        mUiRootNode.loadFromXmlNode(xml_doc);

        // update the model first, since it is used by the viewers.
        super.xmlModelChanged(xml_doc);
        
        if (mGraphicalEditor != null) {
            mGraphicalEditor.onXmlModelChanged();
        }
        
        if (mOutline != null) {
            mOutline.reloadModel();
        }