KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > forms > editor > SharedHeaderFormEditor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.forms.editor;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.layout.FillLayout;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Event;
17 import org.eclipse.swt.widgets.Listener;
18 import org.eclipse.ui.IEditorPart;
19 import org.eclipse.ui.IKeyBindingService;
20 import org.eclipse.ui.INestableKeyBindingService;
21 import org.eclipse.ui.forms.IFormPart;
22 import org.eclipse.ui.forms.IManagedForm;
23 import org.eclipse.ui.forms.ManagedForm;
24 import org.eclipse.ui.forms.widgets.ScrolledForm;
25 import org.eclipse.ui.internal.forms.widgets.FormUtil;
26 import org.eclipse.ui.internal.services.INestable;
27
28 /**
29  * A variation of {@link FormEditor}, this editor has a stable header that does
30  * not change when pages are switched. Pages that are added to this editor
31  * should not have the title or image set.
32  *
33  * @since 3.3
34  */

35 public abstract class SharedHeaderFormEditor extends FormEditor {
36     private HeaderForm headerForm;
37     
38     private boolean wasHeaderActive= true;
39     private Listener activationListener= null;
40
41     private static class HeaderForm extends ManagedForm {
42         public HeaderForm(FormEditor editor, ScrolledForm form) {
43             super(editor.getToolkit(), form);
44             setContainer(editor);
45             if (editor.getEditorInput() != null)
46                 setInput(editor.getEditorInput());
47         }
48
49         private FormEditor getEditor() {
50             return (FormEditor) getContainer();
51         }
52
53         public void dirtyStateChanged() {
54             getEditor().editorDirtyStateChanged();
55         }
56
57         public void staleStateChanged() {
58             refresh();
59         }
60     }
61
62     /**
63      * The default constructor.
64      */

65
66     public SharedHeaderFormEditor() {
67     }
68
69     /**
70      * Overrides <code>super</code> to create a form in which to host the tab
71      * folder. This form will be responsible for managing
72      *
73      * @param parent
74      * the page container parent
75      *
76      * @see org.eclipse.ui.part.MultiPageEditorPart#createPageContainer(org.eclipse.swt.widgets.Composite)
77      */

78
79     protected Composite createPageContainer(Composite parent) {
80         parent = super.createPageContainer(parent);
81         parent.setLayout(new FillLayout());
82         ScrolledForm scform = getToolkit().createScrolledForm(parent);
83         scform.getForm().setData(FormUtil.IGNORE_BODY, Boolean.TRUE);
84         headerForm = new HeaderForm(this, scform);
85         createHeaderContents(headerForm);
86         return headerForm.getForm().getBody();
87     }
88
89     /**
90      * Returns the form that owns the shared header.
91      *
92      * @return the shared header
93      */

94
95     public IManagedForm getHeaderForm() {
96         return headerForm;
97     }
98     
99     protected void createPages() {
100         super.createPages();
101         
102         // preempt MultiPageEditorPart#createPartControl(Composite)
103
if (getActivePage() == -1) {
104             // create page control and initialize page, keep focus on header by calling super implementation
105
super.setActivePage(0);
106         }
107     }
108     
109     protected void setActivePage(int pageIndex) {
110         // programmatic focus change
111
wasHeaderActive= false;
112         super.setActivePage(pageIndex);
113     }
114     
115     public void setFocus() {
116         installActivationListener();
117         if (wasHeaderActive)
118             ((ManagedForm) getHeaderForm()).setFocus();
119         else {
120             int index= getActivePage();
121             if (index == -1)
122                 ((ManagedForm) getHeaderForm()).setFocus();
123             else
124                 super.setFocus();
125         }
126     }
127     
128     private void installActivationListener() {
129         if (activationListener == null) {
130             activationListener = new Listener() {
131                 public void handleEvent(Event event) {
132                     boolean wasHeaderActive = event.widget != getContainer();
133                     
134                     // fix for https://bugs.eclipse.org/bugs/show_bug.cgi?id=177331
135
int activePage = getActivePage();
136                     if (SharedHeaderFormEditor.this.wasHeaderActive != wasHeaderActive && activePage != -1 && pages.get(activePage) instanceof IEditorPart) {
137                         IEditorPart activePart = (IEditorPart) pages.get(activePage);
138                         IKeyBindingService keyBindingService = getSite().getKeyBindingService();
139                         
140                         if (wasHeaderActive) {
141                             
142                             if (activePart.getSite() instanceof INestable)
143                                 ((INestable) activePart.getSite()).deactivate();
144                             
145                             if (keyBindingService instanceof INestableKeyBindingService)
146                                 ((INestableKeyBindingService) keyBindingService).activateKeyBindingService(null);
147                             
148                         } else {
149                             
150                             if (keyBindingService instanceof INestableKeyBindingService)
151                                 ((INestableKeyBindingService) keyBindingService).activateKeyBindingService(activePart.getSite());
152                             
153                             if (activePart.getSite() instanceof INestable)
154                                 ((INestable) activePart.getSite()).activate();
155                             
156                         }
157                     }
158                     
159                     SharedHeaderFormEditor.this.wasHeaderActive = wasHeaderActive;
160                 }
161             };
162             getContainer().addListener(SWT.Activate, activationListener);
163             getHeaderForm().getForm().getForm().getHead().addListener(SWT.Activate, activationListener);
164         }
165     }
166     
167     /*
168      * (non-Javadoc)
169      *
170      * @see org.eclipse.ui.forms.editor.FormEditor#dispose()
171      */

172     public void dispose() {
173         if (headerForm != null) {
174             headerForm.dispose();
175             headerForm = null;
176         }
177         super.dispose();
178     }
179
180     /*
181      * (non-Javadoc)
182      *
183      * @see org.eclipse.ui.forms.editor.FormEditor#isDirty()
184      */

185     public boolean isDirty() {
186         return headerForm.isDirty() || super.isDirty();
187     }
188
189     /*
190      * (non-Javadoc)
191      *
192      * @see org.eclipse.ui.forms.editor.FormEditor#commitPages(boolean)
193      */

194     protected void commitPages(boolean onSave) {
195         if (headerForm != null && headerForm.isDirty())
196             headerForm.commit(onSave);
197         super.commitPages(onSave);
198     }
199
200     /**
201      * Subclasses should extend this method to configure the form that owns the
202      * shared header. If the header form will contain controls that can change
203      * the state of the editor, they should be wrapped in an IFormPart so that
204      * they can participate in the life cycle event management.
205      *
206      * @param headerForm
207      * the form that owns the shared header
208      * @see IFormPart
209      */

210     protected void createHeaderContents(IManagedForm headerForm) {
211     }
212 }
Popular Tags