1 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 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 65 66 public SharedHeaderFormEditor() { 67 } 68 69 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 94 95 public IManagedForm getHeaderForm() { 96 return headerForm; 97 } 98 99 protected void createPages() { 100 super.createPages(); 101 102 if (getActivePage() == -1) { 104 super.setActivePage(0); 106 } 107 } 108 109 protected void setActivePage(int pageIndex) { 110 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 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 172 public void dispose() { 173 if (headerForm != null) { 174 headerForm.dispose(); 175 headerForm = null; 176 } 177 super.dispose(); 178 } 179 180 185 public boolean isDirty() { 186 return headerForm.isDirty() || super.isDirty(); 187 } 188 189 194 protected void commitPages(boolean onSave) { 195 if (headerForm != null && headerForm.isDirty()) 196 headerForm.commit(onSave); 197 super.commitPages(onSave); 198 } 199 200 210 protected void createHeaderContents(IManagedForm headerForm) { 211 } 212 } | Popular Tags |