001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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.schema;
011:
012: import org.eclipse.jface.action.IMenuManager;
013: import org.eclipse.jface.resource.JFaceResources;
014: import org.eclipse.jface.text.Document;
015: import org.eclipse.jface.text.DocumentEvent;
016: import org.eclipse.jface.text.IDocument;
017: import org.eclipse.jface.text.IDocumentListener;
018: import org.eclipse.jface.text.ITextOperationTarget;
019: import org.eclipse.jface.text.source.SourceViewer;
020: import org.eclipse.jface.viewers.ISelection;
021: import org.eclipse.jface.viewers.ISelectionChangedListener;
022: import org.eclipse.jface.viewers.SelectionChangedEvent;
023: import org.eclipse.pde.core.IModelChangedEvent;
024: import org.eclipse.pde.internal.core.ischema.IDocumentSection;
025: import org.eclipse.pde.internal.core.ischema.ISchema;
026: import org.eclipse.pde.internal.core.ischema.ISchemaObject;
027: import org.eclipse.pde.internal.core.schema.Schema;
028: import org.eclipse.pde.internal.core.schema.SchemaObject;
029: import org.eclipse.pde.internal.ui.PDEPlugin;
030: import org.eclipse.pde.internal.ui.PDEUIMessages;
031: import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
032: import org.eclipse.pde.internal.ui.editor.PDEFormPage;
033: import org.eclipse.pde.internal.ui.editor.PDESection;
034: import org.eclipse.pde.internal.ui.editor.context.XMLDocumentSetupParticpant;
035: import org.eclipse.pde.internal.ui.editor.text.IColorManager;
036: import org.eclipse.pde.internal.ui.editor.text.XMLConfiguration;
037: import org.eclipse.swt.SWT;
038: import org.eclipse.swt.custom.CTabFolder;
039: import org.eclipse.swt.custom.CTabItem;
040: import org.eclipse.swt.custom.StyledText;
041: import org.eclipse.swt.dnd.Clipboard;
042: import org.eclipse.swt.events.FocusAdapter;
043: import org.eclipse.swt.events.FocusEvent;
044: import org.eclipse.swt.events.SelectionAdapter;
045: import org.eclipse.swt.events.SelectionEvent;
046: import org.eclipse.swt.graphics.Color;
047: import org.eclipse.swt.layout.GridData;
048: import org.eclipse.swt.widgets.Composite;
049: import org.eclipse.swt.widgets.Control;
050: import org.eclipse.ui.actions.ActionFactory;
051: import org.eclipse.ui.forms.IFormColors;
052: import org.eclipse.ui.forms.widgets.FormToolkit;
053: import org.eclipse.ui.forms.widgets.Section;
054:
055: public class DocSection extends PDESection {
056: private IDocument fDocument;
057: private XMLConfiguration fSourceConfiguration;
058: private SourceViewer fSourceViewer;
059: private CTabFolder fTabFolder;
060: private ISchema fSchema;
061: private Object fElement;
062: private boolean fIgnoreChange;
063:
064: public DocSection(PDEFormPage page, Composite parent,
065: IColorManager colorManager) {
066: super (page, parent, Section.DESCRIPTION, true);
067: getSection().setText(PDEUIMessages.DocSection_text);
068: getSection().setDescription(
069: PDEUIMessages.SchemaEditor_DocSection_desc);
070: fSourceConfiguration = new XMLConfiguration(colorManager);
071: fDocument = new Document();
072: new XMLDocumentSetupParticpant().setup(fDocument);
073: fSchema = (ISchema) getPage().getModel();
074: createClient(getSection(), page.getManagedForm().getToolkit());
075: }
076:
077: public void commit(boolean onSave) {
078: handleApply();
079: super .commit(onSave);
080: }
081:
082: public void createClient(Section section, FormToolkit toolkit) {
083: Composite container = toolkit.createComposite(section);
084: container.setLayout(FormLayoutFactory
085: .createSectionClientGridLayout(false, 1));
086: GridData data = new GridData(GridData.FILL_BOTH);
087: data.horizontalSpan = 2;
088: section.setLayoutData(data);
089:
090: fTabFolder = new CTabFolder(container, SWT.FLAT | SWT.TOP);
091: toolkit.adapt(fTabFolder, true, true);
092: GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
093: gd.heightHint = 2;
094: fTabFolder.setLayoutData(gd);
095:
096: toolkit.getColors().initializeSectionToolBarColors();
097: Color selectedColor = toolkit.getColors().getColor(
098: IFormColors.TB_BG);
099: fTabFolder.setSelectionBackground(new Color[] { selectedColor,
100: toolkit.getColors().getBackground() },
101: new int[] { 100 }, true);
102:
103: fTabFolder.addSelectionListener(new SelectionAdapter() {
104: public void widgetSelected(SelectionEvent e) {
105: updateTabSelection();
106: }
107: });
108:
109: int styles = SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.H_SCROLL;
110: fSourceViewer = new SourceViewer(container, null, styles);
111: fSourceViewer.configure(fSourceConfiguration);
112: fSourceViewer.setDocument(fDocument);
113: fSourceViewer
114: .addSelectionChangedListener(new ISelectionChangedListener() {
115: public void selectionChanged(
116: SelectionChangedEvent event) {
117: updateSelection(event.getSelection());
118: }
119: });
120: StyledText styledText = fSourceViewer.getTextWidget();
121: styledText.setFont(JFaceResources.getTextFont());
122: styledText.setMenu(getPage().getPDEEditor().getContextMenu());
123: styledText.setData(FormToolkit.KEY_DRAW_BORDER,
124: FormToolkit.TEXT_BORDER);
125: styledText.addFocusListener(new FocusAdapter() {
126: public void focusGained(FocusEvent e) {
127: getPage().getPDEEditor().getContributor()
128: .updateSelectableActions(null);
129: }
130: });
131:
132: if (SWT.getPlatform().equals("motif") == false) //$NON-NLS-1$
133: toolkit.paintBordersFor(container);
134: Control[] children = container.getChildren();
135: Control control = children[children.length - 1];
136: gd = new GridData(GridData.FILL_BOTH);
137: gd.widthHint = 50;
138: gd.heightHint = 50;
139: control.setLayoutData(gd);
140:
141: createTabs();
142: section.setClient(container);
143: initialize();
144: if (fTabFolder.getItemCount() > 0) {
145: fTabFolder.setSelection(0);
146: updateTabSelection();
147: }
148: }
149:
150: public boolean doGlobalAction(String actionId) {
151: if (actionId.equals(ActionFactory.CUT.getId())) {
152: fSourceViewer.doOperation(ITextOperationTarget.CUT);
153: return true;
154: } else if (actionId.equals(ActionFactory.COPY.getId())) {
155: fSourceViewer.doOperation(ITextOperationTarget.COPY);
156: return true;
157: } else if (actionId.equals(ActionFactory.PASTE.getId())) {
158: fSourceViewer.doOperation(ITextOperationTarget.PASTE);
159: return true;
160: } else if (actionId.equals(ActionFactory.SELECT_ALL.getId())) {
161: fSourceViewer.doOperation(ITextOperationTarget.SELECT_ALL);
162: return true;
163: } else if (actionId.equals(ActionFactory.DELETE.getId())) {
164: fSourceViewer.doOperation(ITextOperationTarget.DELETE);
165: return true;
166: } else if (actionId.equals(ActionFactory.UNDO.getId())) {
167: fSourceViewer.doOperation(ITextOperationTarget.UNDO);
168: return true;
169: } else if (actionId.equals(ActionFactory.REDO.getId())) {
170: fSourceViewer.doOperation(ITextOperationTarget.REDO);
171: return true;
172: }
173: return false;
174: }
175:
176: protected void fillContextMenu(IMenuManager manager) {
177: getPage().getPDEEditor().getContributor()
178: .contextMenuAboutToShow(manager);
179: }
180:
181: public boolean setFormInput(Object input) {
182: int index = -1;
183: if (input instanceof ISchema) {
184: index = 0;
185: } else if (input instanceof IDocumentSection) {
186: IDocumentSection[] sections = fSchema.getDocumentSections();
187: for (int i = 0; i < sections.length; i++) {
188: IDocumentSection section = sections[i];
189: if (section.equals(input)) {
190: index = i + 1;
191: break;
192: }
193: }
194: }
195: if (index != -1)
196: fTabFolder.setSelection(index);
197: updateEditorInput(input);
198: return true;
199: }
200:
201: private String getTopicName(Object object) {
202: if (object instanceof ISchema) {
203: return PDEUIMessages.SchemaEditor_topic_overview;
204: } else if (object instanceof IDocumentSection) {
205: IDocumentSection section = (IDocumentSection) object;
206: String sectionId = section.getSectionId();
207: if (sectionId.equals(IDocumentSection.EXAMPLES))
208: return PDEUIMessages.SchemaEditor_topic_examples;
209: if (sectionId.equals(IDocumentSection.SINCE))
210: return PDEUIMessages.SchemaEditor_topic_since;
211: if (sectionId.equals(IDocumentSection.IMPLEMENTATION))
212: return PDEUIMessages.SchemaEditor_topic_implementation;
213: if (sectionId.equals(IDocumentSection.API_INFO))
214: return PDEUIMessages.SchemaEditor_topic_api;
215: if (sectionId.equals(IDocumentSection.COPYRIGHT))
216: return PDEUIMessages.SchemaEditor_topic_copyright;
217: }
218: return "?"; //$NON-NLS-1$
219: }
220:
221: private void handleApply() {
222: if (fElement != null) {
223: if (fElement instanceof ISchema)
224: ((Schema) fElement).setDescription(fDocument.get());
225: else
226: ((SchemaObject) fElement).setDescription(fDocument
227: .get());
228: updateTabImage(fTabFolder.getSelection());
229: }
230: }
231:
232: public void initialize() {
233: fSourceViewer.setEditable(fSchema.isEditable());
234: fDocument.addDocumentListener(new IDocumentListener() {
235: public void documentChanged(DocumentEvent e) {
236: if (!fIgnoreChange && fSchema.isEditable()) {
237: markDirty();
238: }
239: }
240:
241: public void documentAboutToBeChanged(DocumentEvent e) {
242: }
243: });
244: updateEditorInput(fSchema);
245: fSchema.addModelChangedListener(this );
246: }
247:
248: /* (non-Javadoc)
249: * @see org.eclipse.ui.forms.AbstractFormPart#dispose()
250: */
251: public void dispose() {
252: // Dispose of the source configuration
253: if (fSourceConfiguration != null) {
254: fSourceConfiguration.dispose();
255: }
256: fSchema.removeModelChangedListener(this );
257: super .dispose();
258: }
259:
260: private void createTabs() {
261: IDocumentSection[] sections = fSchema.getDocumentSections();
262: addTab(fSchema);
263: for (int i = 0; i < sections.length; i++) {
264: IDocumentSection section = sections[i];
265: addTab(section);
266: }
267: }
268:
269: private void addTab(ISchemaObject section) {
270: String label = getTopicName(section);
271: CTabItem item = new CTabItem(fTabFolder, SWT.NULL);
272: item.setText(label);
273: item.setData(section);
274: updateTabImage(item);
275: }
276:
277: private void updateTabImage(CTabItem item) {
278: if (item != null) {
279: ISchemaObject section = (ISchemaObject) item.getData();
280: if (section != null)
281: item.setImage(PDEPlugin.getDefault().getLabelProvider()
282: .getImage(section));
283: }
284: }
285:
286: private void updateTabSelection() {
287: int index = fTabFolder.getSelectionIndex();
288: if (fSchema.isEditable() && isDirty())
289: handleApply();
290:
291: if (index == 0)
292: updateEditorInput(fSchema);
293: else {
294: IDocumentSection[] sections = fSchema.getDocumentSections();
295: updateEditorInput(sections[index - 1]);
296: }
297: }
298:
299: public void setFocus() {
300: fSourceViewer.getTextWidget().setFocus();
301: updateSelection(fSourceViewer.getSelection());
302: }
303:
304: private void updateSelection(ISelection selection) {
305: getPage().getPDEEditor().setSelection(selection);
306: }
307:
308: public void updateEditorInput(Object input) {
309: fIgnoreChange = true;
310: String text = ""; //$NON-NLS-1$
311: if (input instanceof ISchemaObject)
312: text = ((ISchemaObject) input).getDescription();
313: fDocument.set(text == null ? "" : text); //$NON-NLS-1$
314: fElement = input;
315: fIgnoreChange = false;
316: }
317:
318: public void modelChanged(IModelChangedEvent e) {
319: if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
320: markStale();
321: }
322: }
323:
324: public void refresh() {
325: IDocumentSection[] sections = fSchema.getDocumentSections();
326: int index = fTabFolder.getSelectionIndex();
327: if (index == 0)
328: updateEditorInput(fSchema);
329: else {
330: updateEditorInput(sections[index - 1]);
331: }
332: super .refresh();
333: }
334:
335: public boolean canPaste(Clipboard clipboard) {
336: return fSourceViewer.canDoOperation(ITextOperationTarget.PASTE);
337: }
338: }
|