1 11 package org.eclipse.pde.internal.ui.editor.schema; 12 13 import org.eclipse.jface.action.IMenuManager; 14 import org.eclipse.jface.resource.JFaceResources; 15 import org.eclipse.jface.text.Document; 16 import org.eclipse.jface.text.DocumentEvent; 17 import org.eclipse.jface.text.IDocument; 18 import org.eclipse.jface.text.IDocumentListener; 19 import org.eclipse.jface.text.ITextOperationTarget; 20 import org.eclipse.jface.text.source.SourceViewer; 21 import org.eclipse.jface.viewers.ISelection; 22 import org.eclipse.jface.viewers.ISelectionChangedListener; 23 import org.eclipse.jface.viewers.SelectionChangedEvent; 24 import org.eclipse.pde.core.IModelChangedEvent; 25 import org.eclipse.pde.internal.core.ischema.IDocumentSection; 26 import org.eclipse.pde.internal.core.ischema.ISchema; 27 import org.eclipse.pde.internal.core.ischema.ISchemaObject; 28 import org.eclipse.pde.internal.core.schema.Schema; 29 import org.eclipse.pde.internal.core.schema.SchemaObject; 30 import org.eclipse.pde.internal.ui.PDEPlugin; 31 import org.eclipse.pde.internal.ui.PDEUIMessages; 32 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory; 33 import org.eclipse.pde.internal.ui.editor.PDEFormPage; 34 import org.eclipse.pde.internal.ui.editor.PDESection; 35 import org.eclipse.pde.internal.ui.editor.context.XMLDocumentSetupParticpant; 36 import org.eclipse.pde.internal.ui.editor.text.IColorManager; 37 import org.eclipse.pde.internal.ui.editor.text.XMLConfiguration; 38 import org.eclipse.swt.SWT; 39 import org.eclipse.swt.custom.CTabFolder; 40 import org.eclipse.swt.custom.CTabItem; 41 import org.eclipse.swt.custom.StyledText; 42 import org.eclipse.swt.dnd.Clipboard; 43 import org.eclipse.swt.events.FocusAdapter; 44 import org.eclipse.swt.events.FocusEvent; 45 import org.eclipse.swt.events.SelectionAdapter; 46 import org.eclipse.swt.events.SelectionEvent; 47 import org.eclipse.swt.graphics.Color; 48 import org.eclipse.swt.layout.GridData; 49 import org.eclipse.swt.widgets.Composite; 50 import org.eclipse.swt.widgets.Control; 51 import org.eclipse.ui.actions.ActionFactory; 52 import org.eclipse.ui.forms.IFormColors; 53 import org.eclipse.ui.forms.widgets.FormToolkit; 54 import org.eclipse.ui.forms.widgets.Section; 55 56 public class DocSection extends PDESection { 57 private IDocument fDocument; 58 private XMLConfiguration fSourceConfiguration; 59 private SourceViewer fSourceViewer; 60 private CTabFolder fTabFolder; 61 private ISchema fSchema; 62 private Object fElement; 63 private boolean fIgnoreChange; 64 65 public DocSection(PDEFormPage page, Composite parent, IColorManager colorManager) { 66 super(page, parent, Section.DESCRIPTION, true); 67 getSection().setText(PDEUIMessages.DocSection_text); 68 getSection().setDescription(PDEUIMessages.SchemaEditor_DocSection_desc); 69 fSourceConfiguration = new XMLConfiguration(colorManager); 70 fDocument = new Document(); 71 new XMLDocumentSetupParticpant().setup(fDocument); 72 fSchema = (ISchema) getPage().getModel(); 73 createClient(getSection(), page.getManagedForm().getToolkit()); 74 } 75 76 public void commit(boolean onSave) { 77 handleApply(); 78 super.commit(onSave); 79 } 80 81 public void createClient(Section section, FormToolkit toolkit) { 82 Composite container = toolkit.createComposite(section); 83 container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 1)); 84 GridData data = new GridData(GridData.FILL_BOTH); 85 data.horizontalSpan = 2; 86 section.setLayoutData(data); 87 88 fTabFolder = new CTabFolder(container, SWT.FLAT|SWT.TOP); 89 toolkit.adapt(fTabFolder, true, true); 90 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL); 91 gd.heightHint = 2; 92 fTabFolder.setLayoutData(gd); 93 94 toolkit.getColors().initializeSectionToolBarColors(); 95 Color selectedColor = toolkit.getColors().getColor(IFormColors.TB_BG); 96 fTabFolder.setSelectionBackground(new Color[] {selectedColor, 97 toolkit.getColors().getBackground()}, new int[] {100}, true); 98 99 fTabFolder.addSelectionListener(new SelectionAdapter() { 100 public void widgetSelected(SelectionEvent e) { 101 updateTabSelection(); 102 } 103 }); 104 105 int styles = SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.H_SCROLL; 106 fSourceViewer = new SourceViewer(container, null, styles); 107 fSourceViewer.configure(fSourceConfiguration); 108 fSourceViewer.setDocument(fDocument); 109 fSourceViewer.addSelectionChangedListener(new ISelectionChangedListener() { 110 public void selectionChanged(SelectionChangedEvent event) { 111 updateSelection(event.getSelection()); 112 } 113 }); 114 StyledText styledText = fSourceViewer.getTextWidget(); 115 styledText.setFont(JFaceResources.getTextFont()); 116 styledText.setMenu(getPage().getPDEEditor().getContextMenu()); 117 styledText.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TEXT_BORDER); 118 styledText.addFocusListener(new FocusAdapter() { 119 public void focusGained(FocusEvent e) { 120 getPage().getPDEEditor().getContributor().updateSelectableActions(null); 121 } 122 }); 123 124 if (SWT.getPlatform().equals("motif") == false) toolkit.paintBordersFor(container); 126 Control[] children = container.getChildren(); 127 Control control = children[children.length - 1]; 128 gd = new GridData(GridData.FILL_BOTH); 129 gd.widthHint = 50; 130 gd.heightHint = 50; 131 control.setLayoutData(gd); 132 133 createTabs(); 134 section.setClient(container); 135 initialize(); 136 if (fTabFolder.getItemCount() > 0) { 137 fTabFolder.setSelection(0); 138 updateTabSelection(); 139 } 140 } 141 142 public boolean doGlobalAction(String actionId) { 143 if (actionId.equals(ActionFactory.CUT.getId())) { 144 fSourceViewer.doOperation(ITextOperationTarget.CUT); 145 return true; 146 } else if ( 147 actionId.equals(ActionFactory.COPY.getId())) { 148 fSourceViewer.doOperation(ITextOperationTarget.COPY); 149 return true; 150 } else if ( 151 actionId.equals(ActionFactory.PASTE.getId())) { 152 fSourceViewer.doOperation(ITextOperationTarget.PASTE); 153 return true; 154 } else if ( 155 actionId.equals(ActionFactory.SELECT_ALL.getId())) { 156 fSourceViewer.doOperation(ITextOperationTarget.SELECT_ALL); 157 return true; 158 } else if ( 159 actionId.equals(ActionFactory.DELETE.getId())) { 160 fSourceViewer.doOperation(ITextOperationTarget.DELETE); 161 return true; 162 } else if ( 163 actionId.equals(ActionFactory.UNDO.getId())) { 164 fSourceViewer.doOperation(ITextOperationTarget.UNDO); 165 return true; 166 } else if ( 167 actionId.equals(ActionFactory.REDO.getId())) { 168 fSourceViewer.doOperation(ITextOperationTarget.REDO); 169 return true; 170 } 171 return false; 172 } 173 174 protected void fillContextMenu(IMenuManager manager) { 175 getPage().getPDEEditor().getContributor().contextMenuAboutToShow( 176 manager); 177 } 178 public boolean setFormInput(Object input) { 179 int index = -1; 180 if (input instanceof ISchema) { 181 index = 0; 182 } else if (input instanceof IDocumentSection) { 183 IDocumentSection[] sections = fSchema.getDocumentSections(); 184 for (int i = 0; i < sections.length; i++) { 185 IDocumentSection section = sections[i]; 186 if (section.equals(input)) { 187 index = i + 1; 188 break; 189 } 190 } 191 } 192 if (index != -1) 193 fTabFolder.setSelection(index); 194 updateEditorInput(input); 195 return true; 196 } 197 198 private String getTopicName(Object object) { 199 if (object instanceof ISchema) { 200 return PDEUIMessages.SchemaEditor_topic_overview; 201 } else if (object instanceof IDocumentSection) { 202 IDocumentSection section = (IDocumentSection) object; 203 String sectionId = section.getSectionId(); 204 if (sectionId.equals(IDocumentSection.EXAMPLES)) 205 return PDEUIMessages.SchemaEditor_topic_examples; 206 if (sectionId.equals(IDocumentSection.SINCE)) 207 return PDEUIMessages.SchemaEditor_topic_since; 208 if (sectionId.equals(IDocumentSection.IMPLEMENTATION)) 209 return PDEUIMessages.SchemaEditor_topic_implementation; 210 if (sectionId.equals(IDocumentSection.API_INFO)) 211 return PDEUIMessages.SchemaEditor_topic_api; 212 if (sectionId.equals(IDocumentSection.COPYRIGHT)) 213 return PDEUIMessages.SchemaEditor_topic_copyright; 214 } 215 return "?"; } 217 218 private void handleApply() { 219 if (fElement != null) { 220 if (fElement instanceof ISchema) 221 ((Schema) fElement).setDescription(fDocument.get()); 222 else 223 ((SchemaObject) fElement).setDescription(fDocument.get()); 224 updateTabImage(fTabFolder.getSelection()); 225 } 226 } 227 228 public void initialize() { 229 fSourceViewer.setEditable(fSchema.isEditable()); 230 fDocument.addDocumentListener(new IDocumentListener() { 231 public void documentChanged(DocumentEvent e) { 232 if (!fIgnoreChange && fSchema.isEditable()) { 233 markDirty(); 234 } 235 } 236 public void documentAboutToBeChanged(DocumentEvent e) { 237 } 238 }); 239 updateEditorInput(fSchema); 240 fSchema.addModelChangedListener(this); 241 } 242 243 246 public void dispose() { 247 if (fSourceConfiguration != null) { 249 fSourceConfiguration.dispose(); 250 } 251 fSchema.removeModelChangedListener(this); 252 super.dispose(); 253 } 254 255 private void createTabs() { 256 IDocumentSection[] sections = fSchema.getDocumentSections(); 257 addTab(fSchema); 258 for (int i = 0; i < sections.length; i++) { 259 IDocumentSection section = sections[i]; 260 addTab(section); 261 } 262 } 263 264 private void addTab(ISchemaObject section) { 265 String label = getTopicName(section); 266 CTabItem item = new CTabItem(fTabFolder, SWT.NULL); 267 item.setText(label); 268 item.setData(section); 269 updateTabImage(item); 270 } 271 272 private void updateTabImage(CTabItem item) { 273 if (item != null) { 274 ISchemaObject section = (ISchemaObject)item.getData(); 275 if (section != null) 276 item.setImage(PDEPlugin.getDefault().getLabelProvider().getImage(section)); 277 } 278 } 279 280 private void updateTabSelection() { 281 int index = fTabFolder.getSelectionIndex(); 282 if (fSchema.isEditable() && isDirty()) 283 handleApply(); 284 285 if (index == 0) 286 updateEditorInput(fSchema); 287 else { 288 IDocumentSection[] sections = fSchema.getDocumentSections(); 289 updateEditorInput(sections[index - 1]); 290 } 291 } 292 293 public void setFocus() { 294 fSourceViewer.getTextWidget().setFocus(); 295 updateSelection(fSourceViewer.getSelection()); 296 } 297 298 private void updateSelection(ISelection selection) { 299 getPage().getPDEEditor().setSelection(selection); 300 } 301 302 public void updateEditorInput(Object input) { 303 fIgnoreChange = true; 304 String text = ""; if (input instanceof ISchemaObject) 306 text = ((ISchemaObject) input).getDescription(); 307 fDocument.set(text == null ? "" : text); fElement = input; 309 fIgnoreChange = false; 310 } 311 312 public void modelChanged(IModelChangedEvent e) { 313 if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { 314 markStale(); 315 } 316 } 317 318 public void refresh() { 319 IDocumentSection[] sections = fSchema.getDocumentSections(); 320 int index = fTabFolder.getSelectionIndex(); 321 if (index == 0) 322 updateEditorInput(fSchema); 323 else { 324 updateEditorInput(sections[index - 1]); 325 } 326 super.refresh(); 327 } 328 329 public boolean canPaste(Clipboard clipboard) { 330 return fSourceViewer.canDoOperation(ITextOperationTarget.PASTE); 331 } 332 } 333 | Popular Tags |