aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/XmlEditorMultiOutline.java
blob: 61db9f31338e3a950c2caa92434ff9d8107cf96c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ide.eclipse.adt.internal.editors;

import com.android.ide.eclipse.adt.AdtPlugin;

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.IPageBookViewPage;
import org.eclipse.ui.part.Page;
import org.eclipse.ui.part.PageBook;
import org.eclipse.ui.views.contentoutline.IContentOutlinePage;

import java.util.ArrayList;
import java.util.List;

/**
 * Outline used for XML editors that have multiple pages with separate outlines:
 * switches between them
 * <p>
 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=1917
 * <p>
 * Modeled after .org.eclipse.pde.internal.ui.editor.PDEMultiPageContentOutline
 */
public class XmlEditorMultiOutline extends Page implements IContentOutlinePage,
        ISelectionChangedListener {
    private boolean mDisposed;
    private PageBook mPageBook;
    private IContentOutlinePage mCurrentPage;
    private IActionBars mActionBars;
    private IContentOutlinePage mEmptyPage;
    private List<ISelectionChangedListener> mListeners;
    private ISelection mSelection;

    public XmlEditorMultiOutline() {
    }

    @Override
    public Control getControl() {
        return mPageBook;
    }

    @Override
    public void createControl(Composite parent) {
        mPageBook = new PageBook(parent, SWT.NONE);
    }

    @Override
    public void dispose() {
        mDisposed = true;
        mListeners = null;
        if (mPageBook != null && !mPageBook.isDisposed()) {
            mPageBook.dispose();
            mPageBook = null;
        }
        if (mEmptyPage != null) {
            mEmptyPage.dispose();
            mEmptyPage = null;
        }
    }

    public boolean isDisposed() {
        return mDisposed;
    }

    @Override
    public void makeContributions(IMenuManager menuManager, IToolBarManager toolBarManager,
            IStatusLineManager statusLineManager) {
    }

    @Override
    public void setActionBars(IActionBars actionBars) {
        mActionBars = actionBars;
        if (mCurrentPage != null) {
            setPageActive(mCurrentPage);
        }
    }

    @Override
    public void setFocus() {
        if (mCurrentPage != null) {
            mCurrentPage.setFocus();
        }
    }

    @Override
    public void addSelectionChangedListener(ISelectionChangedListener listener) {
        if (mListeners == null) {
            mListeners = new ArrayList<ISelectionChangedListener>(2);
        }
        mListeners.add(listener);
    }

    @Override
    public void removeSelectionChangedListener(ISelectionChangedListener listener) {
        mListeners.remove(listener);
    }

    @Override
    public ISelection getSelection() {
        return mSelection;
    }

    @Override
    public void selectionChanged(SelectionChangedEvent event) {
        setSelection(event.getSelection());
    }

    public void setPageActive(IContentOutlinePage page) {
        if (page == null) {
            if (mEmptyPage == null) {
                mEmptyPage = new EmptyPage();
            }
            page = mEmptyPage;
        }
        if (mCurrentPage != null) {
            mCurrentPage.removeSelectionChangedListener(this);
        }
        page.addSelectionChangedListener(this);
        mCurrentPage = page;
        // Still initializing?
        if (mPageBook == null) {
            return;
        }
        Control control = page.getControl();
        if (control == null || control.isDisposed()) {
            if (page instanceof IPageBookViewPage) {
                try {
                    ((IPageBookViewPage) page).init(getSite());
                } catch (PartInitException e) {
                    AdtPlugin.log(e, null);
                }
            }
            page.createControl(mPageBook);
            page.setActionBars(mActionBars);
            control = page.getControl();
        }
        mPageBook.showPage(control);
    }

    @Override
    public void setSelection(ISelection selection) {
        mSelection = selection;
        if (mListeners != null) {
            SelectionChangedEvent e = new SelectionChangedEvent(this, selection);
            for (int i = 0; i < mListeners.size(); i++) {
                mListeners.get(i).selectionChanged(e);
            }
        }
    }

    private static class EmptyPage implements IContentOutlinePage {
        private Composite mControl;

        private EmptyPage() {
        }

        @Override
        public void createControl(Composite parent) {
            mControl = new Composite(parent, SWT.NULL);
        }

        @Override
        public void dispose() {
        }

        @Override
        public Control getControl() {
            return mControl;
        }

        @Override
        public void setActionBars(IActionBars actionBars) {
        }

        @Override
        public void setFocus() {
        }

        @Override
        public void addSelectionChangedListener(ISelectionChangedListener listener) {
        }

        @Override
        public ISelection getSelection() {
            return StructuredSelection.EMPTY;
        }

        @Override
        public void removeSelectionChangedListener(ISelectionChangedListener listener) {
        }

        @Override
        public void setSelection(ISelection selection) {
        }
    }
}