001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.jface.action.Action;
015: import org.eclipse.jface.action.IMenuManager;
016: import org.eclipse.jface.action.IStatusLineManager;
017: import org.eclipse.jface.action.IToolBarManager;
018: import org.eclipse.jface.viewers.ISelection;
019: import org.eclipse.jface.viewers.ISelectionChangedListener;
020: import org.eclipse.jface.viewers.ISelectionProvider;
021: import org.eclipse.jface.viewers.SelectionChangedEvent;
022: import org.eclipse.pde.internal.ui.IHelpContextIds;
023: import org.eclipse.pde.internal.ui.IPreferenceConstants;
024: import org.eclipse.pde.internal.ui.PDEPlugin;
025: import org.eclipse.pde.internal.ui.PDEPluginImages;
026: import org.eclipse.pde.internal.ui.PDEUIMessages;
027: import org.eclipse.swt.SWT;
028: import org.eclipse.swt.events.FocusListener;
029: import org.eclipse.swt.widgets.Composite;
030: import org.eclipse.swt.widgets.Control;
031: import org.eclipse.ui.IActionBars;
032: import org.eclipse.ui.PlatformUI;
033: import org.eclipse.ui.part.Page;
034: import org.eclipse.ui.part.PageBook;
035: import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
036:
037: public class PDEMultiPageContentOutline extends Page implements
038: IContentOutlinePage, ISelectionProvider,
039: ISelectionChangedListener, IPreferenceConstants {
040: private PageBook pagebook;
041: private ISelection selection;
042: private ArrayList listeners;
043: private ISortableContentOutlinePage currentPage;
044: private ISortableContentOutlinePage emptyPage;
045: private IActionBars actionBars;
046: private boolean sortingOn;
047: private PDEFormEditor editor;
048:
049: public PDEMultiPageContentOutline(PDEFormEditor editor) {
050: this .editor = editor;
051: listeners = new ArrayList();
052: sortingOn = PDEPlugin
053: .getDefault()
054: .getPreferenceStore()
055: .getBoolean(
056: "PDEMultiPageContentOutline.SortingAction.isChecked"); //$NON-NLS-1$
057:
058: }
059:
060: public void addFocusListener(FocusListener listener) {
061: }
062:
063: public void addSelectionChangedListener(
064: ISelectionChangedListener listener) {
065: listeners.add(listener);
066: }
067:
068: public void createControl(Composite parent) {
069: pagebook = new PageBook(parent, SWT.NONE);
070: }
071:
072: public void dispose() {
073: if (pagebook != null && !pagebook.isDisposed())
074: pagebook.dispose();
075: if (emptyPage != null) {
076: emptyPage.dispose();
077: emptyPage = null;
078: }
079: pagebook = null;
080: listeners = null;
081: }
082:
083: public boolean isDisposed() {
084: return listeners == null;
085: }
086:
087: public Control getControl() {
088: return pagebook;
089: }
090:
091: public PageBook getPagebook() {
092: return pagebook;
093: }
094:
095: public ISelection getSelection() {
096: return selection;
097: }
098:
099: public void makeContributions(IMenuManager menuManager,
100: IToolBarManager toolBarManager,
101: IStatusLineManager statusLineManager) {
102: }
103:
104: public void removeFocusListener(FocusListener listener) {
105: }
106:
107: public void removeSelectionChangedListener(
108: ISelectionChangedListener listener) {
109: listeners.remove(listener);
110: }
111:
112: public void selectionChanged(SelectionChangedEvent event) {
113: setSelection(event.getSelection());
114: }
115:
116: public void setActionBars(IActionBars actionBars) {
117: this .actionBars = actionBars;
118: registerToolbarActions(actionBars);
119: if (currentPage != null)
120: setPageActive(currentPage);
121:
122: }
123:
124: public IActionBars getActionBars() {
125: return actionBars;
126: }
127:
128: public void setFocus() {
129: if (currentPage != null)
130: currentPage.setFocus();
131: }
132:
133: private ISortableContentOutlinePage getEmptyPage() {
134: if (emptyPage == null)
135: emptyPage = new EmptyOutlinePage();
136: return emptyPage;
137: }
138:
139: public void setPageActive(ISortableContentOutlinePage page) {
140: if (page == null) {
141: page = getEmptyPage();
142: }
143: if (currentPage != null) {
144: currentPage.removeSelectionChangedListener(this );
145: }
146: //page.init(getSite());
147: page.sort(sortingOn);
148: page.addSelectionChangedListener(this );
149: this .currentPage = page;
150: if (pagebook == null) {
151: // still not being made
152: return;
153: }
154: Control control = page.getControl();
155: if (control == null || control.isDisposed()) {
156: // first time
157: page.createControl(pagebook);
158: page.setActionBars(getActionBars());
159: control = page.getControl();
160: }
161: pagebook.showPage(control);
162: this .currentPage = page;
163: }
164:
165: /**
166: * Set the selection.
167: */
168: public void setSelection(ISelection selection) {
169: this .selection = selection;
170: if (listeners == null)
171: return;
172: SelectionChangedEvent e = new SelectionChangedEvent(this ,
173: selection);
174: for (int i = 0; i < listeners.size(); i++) {
175: ((ISelectionChangedListener) listeners.get(i))
176: .selectionChanged(e);
177: }
178: }
179:
180: private void registerToolbarActions(IActionBars actionBars) {
181:
182: IToolBarManager toolBarManager = actionBars.getToolBarManager();
183: if (toolBarManager != null) {
184: toolBarManager.add(new ToggleLinkWithEditorAction(editor));
185: toolBarManager.add(new SortingAction());
186: }
187: }
188:
189: class SortingAction extends Action {
190:
191: public SortingAction() {
192: super ();
193: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
194: IHelpContextIds.OUTLINE_SORT_ACTION);
195: setText(PDEUIMessages.PDEMultiPageContentOutline_SortingAction_label);
196: setImageDescriptor(PDEPluginImages.DESC_ALPHAB_SORT_CO);
197: setDisabledImageDescriptor(PDEPluginImages.DESC_ALPHAB_SORT_CO_DISABLED);
198: setToolTipText(PDEUIMessages.PDEMultiPageContentOutline_SortingAction_tooltip);
199: setDescription(PDEUIMessages.PDEMultiPageContentOutline_SortingAction_description);
200: setChecked(sortingOn);
201: }
202:
203: public void run() {
204: setChecked(isChecked());
205: valueChanged(isChecked());
206: }
207:
208: private void valueChanged(final boolean on) {
209: sortingOn = on;
210: if (currentPage != null)
211: currentPage.sort(on);
212: PDEPlugin
213: .getDefault()
214: .getPreferenceStore()
215: .setValue(
216: "PDEMultiPageContentOutline.SortingAction.isChecked", on); //$NON-NLS-1$
217: }
218:
219: }
220: }
|