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.feature;
011:
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.jface.action.IMenuManager;
014: import org.eclipse.jface.resource.JFaceResources;
015: import org.eclipse.jface.text.Document;
016: import org.eclipse.jface.text.DocumentEvent;
017: import org.eclipse.jface.text.IDocument;
018: import org.eclipse.jface.text.IDocumentListener;
019: import org.eclipse.jface.text.ITextOperationTarget;
020: import org.eclipse.jface.text.source.SourceViewer;
021: import org.eclipse.jface.viewers.ISelection;
022: import org.eclipse.jface.viewers.ISelectionChangedListener;
023: import org.eclipse.jface.viewers.SelectionChangedEvent;
024: import org.eclipse.pde.core.IEditable;
025: import org.eclipse.pde.core.IModelChangedEvent;
026: import org.eclipse.pde.internal.core.ifeature.IFeature;
027: import org.eclipse.pde.internal.core.ifeature.IFeatureInfo;
028: import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
029: import org.eclipse.pde.internal.core.ifeature.IFeatureURLElement;
030: import org.eclipse.pde.internal.ui.PDEPlugin;
031: import org.eclipse.pde.internal.ui.PDEPluginImages;
032: import org.eclipse.pde.internal.ui.PDEUIMessages;
033: import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
034: import org.eclipse.pde.internal.ui.editor.PDEFormPage;
035: import org.eclipse.pde.internal.ui.editor.PDESection;
036: import org.eclipse.pde.internal.ui.editor.context.XMLDocumentSetupParticpant;
037: import org.eclipse.pde.internal.ui.editor.text.IColorManager;
038: import org.eclipse.pde.internal.ui.editor.text.XMLConfiguration;
039: import org.eclipse.pde.internal.ui.elements.NamedElement;
040: import org.eclipse.pde.internal.ui.util.TextUtil;
041: import org.eclipse.swt.SWT;
042: import org.eclipse.swt.custom.CTabFolder;
043: import org.eclipse.swt.custom.CTabItem;
044: import org.eclipse.swt.custom.StackLayout;
045: import org.eclipse.swt.custom.StyledText;
046: import org.eclipse.swt.dnd.Clipboard;
047: import org.eclipse.swt.events.ModifyEvent;
048: import org.eclipse.swt.events.ModifyListener;
049: import org.eclipse.swt.events.SelectionAdapter;
050: import org.eclipse.swt.events.SelectionEvent;
051: import org.eclipse.swt.graphics.Color;
052: import org.eclipse.swt.layout.GridData;
053: import org.eclipse.swt.layout.GridLayout;
054: import org.eclipse.swt.widgets.Composite;
055: import org.eclipse.swt.widgets.Control;
056: import org.eclipse.swt.widgets.Label;
057: import org.eclipse.swt.widgets.Text;
058: import org.eclipse.ui.actions.ActionFactory;
059: import org.eclipse.ui.forms.IFormColors;
060: import org.eclipse.ui.forms.widgets.ExpandableComposite;
061: import org.eclipse.ui.forms.widgets.FormToolkit;
062: import org.eclipse.ui.forms.widgets.Section;
063:
064: public class InfoSection extends PDESection {
065: private IDocument fDocument;
066:
067: private XMLConfiguration fSourceConfiguration;
068:
069: private SourceViewer fSourceViewer;
070:
071: private CTabFolder fTabFolder;
072:
073: private Text fUrlText;
074:
075: private Object fElement;
076:
077: private int fElementIndex;
078:
079: private boolean fIgnoreChange;
080:
081: private Composite fNotebook;
082:
083: private StackLayout fNotebookLayout;
084:
085: private Control fInfoPage;
086:
087: private Control fUrlsPage;
088:
089: public InfoSection(PDEFormPage page, Composite parent,
090: IColorManager colorManager) {
091: super (page, parent, Section.DESCRIPTION
092: | ExpandableComposite.NO_TITLE, false);
093: String description = PDEUIMessages.FeatureEditor_InfoSection_desc;
094: getSection().setDescription(description);
095: fSourceConfiguration = new XMLConfiguration(colorManager);
096: fDocument = new Document();
097: new XMLDocumentSetupParticpant().setup(fDocument);
098: createClient(getSection(), page.getManagedForm().getToolkit());
099: }
100:
101: public void commit(boolean onSave) {
102: handleApply();
103: super .commit(onSave);
104: }
105:
106: public void createClient(Section section, FormToolkit toolkit) {
107:
108: section.setLayout(FormLayoutFactory.createClearGridLayout(
109: false, 1));
110: GridData data = new GridData(GridData.FILL_BOTH);
111: section.setLayoutData(data);
112:
113: Composite container = toolkit.createComposite(section);
114: container.setLayout(FormLayoutFactory
115: .createSectionClientGridLayout(false, 2));
116: container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
117:
118: GridData gd;
119:
120: toolkit.createLabel(container, null);
121: fTabFolder = new CTabFolder(container, SWT.FLAT | SWT.TOP);
122: toolkit.adapt(fTabFolder, true, true);
123: gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
124: fTabFolder.setLayoutData(gd);
125: gd.heightHint = 2;
126: toolkit.getColors().initializeSectionToolBarColors();
127: Color selectedColor = toolkit.getColors().getColor(
128: IFormColors.TB_BG);
129: fTabFolder.setSelectionBackground(new Color[] { selectedColor,
130: toolkit.getColors().getBackground() },
131: new int[] { 100 }, true);
132:
133: fTabFolder.addSelectionListener(new SelectionAdapter() {
134: public void widgetSelected(SelectionEvent e) {
135: updateTabSelection();
136: }
137: });
138:
139: fNotebook = toolkit.createComposite(container);
140: gd = new GridData(GridData.FILL_BOTH);
141: gd.horizontalSpan = 2;
142: fNotebook.setLayoutData(gd);
143: fNotebookLayout = new StackLayout();
144: fNotebook.setLayout(fNotebookLayout);
145:
146: fInfoPage = createInfoPage(toolkit, fNotebook);
147: fUrlsPage = createUrlsPage(toolkit, fNotebook);
148: fNotebookLayout.topControl = fInfoPage;
149:
150: createTabs();
151: section.setClient(container);
152: initialize();
153: if (fTabFolder.getItemCount() > 0) {
154: fTabFolder.setSelection(0);
155: updateTabSelection();
156: }
157: }
158:
159: /**
160: * @param toolkit
161: * @param parent
162: */
163: private Control createInfoPage(FormToolkit toolkit, Composite parent) {
164: Composite page = toolkit.createComposite(parent);
165: GridLayout layout = new GridLayout();
166: layout.numColumns = 2;
167: layout.marginWidth = 2;
168: layout.marginHeight = 5;
169: layout.verticalSpacing = 8;
170: page.setLayout(layout);
171: GridData gd;
172: Label label = toolkit.createLabel(page,
173: PDEUIMessages.FeatureEditor_InfoSection_url);
174: label.setForeground(toolkit.getColors().getColor(
175: IFormColors.TITLE));
176: fUrlText = toolkit.createText(page, null, SWT.SINGLE);
177: fUrlText.addModifyListener(new ModifyListener() {
178: public void modifyText(ModifyEvent e) {
179: infoModified();
180: }
181: });
182: gd = new GridData(GridData.FILL_HORIZONTAL);
183: fUrlText.setLayoutData(gd);
184: label = toolkit.createLabel(page,
185: PDEUIMessages.FeatureEditor_InfoSection_text);
186: label.setForeground(toolkit.getColors().getColor(
187: IFormColors.TITLE));
188: gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
189: label.setLayoutData(gd);
190: int styles = SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.H_SCROLL;
191: fSourceViewer = new SourceViewer(page, null, styles);
192: fSourceViewer.configure(fSourceConfiguration);
193: fSourceViewer.setDocument(fDocument);
194: fSourceViewer
195: .addSelectionChangedListener(new ISelectionChangedListener() {
196: public void selectionChanged(
197: SelectionChangedEvent event) {
198: updateSelection(event.getSelection());
199: }
200: });
201: StyledText styledText = fSourceViewer.getTextWidget();
202: styledText.setFont(JFaceResources.getTextFont());
203: styledText.setMenu(getPage().getPDEEditor().getContextMenu());
204: styledText.setData(FormToolkit.KEY_DRAW_BORDER,
205: FormToolkit.TEXT_BORDER);
206: //
207: if (SWT.getPlatform().equals("motif") == false) //$NON-NLS-1$
208: toolkit.paintBordersFor(page);
209: Control[] children = page.getChildren();
210: Control control = children[children.length - 1];
211: gd = new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL
212: | GridData.GRAB_VERTICAL);
213: gd.widthHint = 50;
214: gd.heightHint = 50;
215: control.setLayoutData(gd);
216:
217: return page;
218: }
219:
220: /**
221: * @param toolkit
222: * @param parent
223: */
224: private Control createUrlsPage(FormToolkit toolkit, Composite parent) {
225: Composite page = toolkit.createComposite(parent);
226: GridLayout layout = new GridLayout();
227: layout.numColumns = 2;
228: layout.makeColumnsEqualWidth = true;
229: layout.marginWidth = 2;
230: layout.marginHeight = 5;
231: layout.verticalSpacing = 8;
232: page.setLayout(layout);
233:
234: URLSection urlSection = new URLSection(getPage(), page);
235: urlSection.getSection().setLayoutData(
236: new GridData(GridData.FILL_BOTH
237: | GridData.VERTICAL_ALIGN_BEGINNING));
238:
239: URLDetailsSection urlDetailsSection = new URLDetailsSection(
240: getPage(), page);
241: urlDetailsSection.getSection().setLayoutData(
242: new GridData(GridData.FILL_HORIZONTAL
243: | GridData.VERTICAL_ALIGN_BEGINNING));
244:
245: getManagedForm().addPart(urlSection);
246: getManagedForm().addPart(urlDetailsSection);
247: return page;
248: }
249:
250: private void updateSelection(ISelection selection) {
251: getPage().getPDEEditor().setSelection(selection);
252: }
253:
254: public boolean doGlobalAction(String actionId) {
255: if (actionId.equals(ActionFactory.CUT.getId())) {
256: fSourceViewer.doOperation(ITextOperationTarget.CUT);
257: return true;
258: } else if (actionId.equals(ActionFactory.COPY.getId())) {
259: fSourceViewer.doOperation(ITextOperationTarget.COPY);
260: return true;
261: } else if (actionId.equals(ActionFactory.PASTE.getId())) {
262: fSourceViewer.doOperation(ITextOperationTarget.PASTE);
263: return true;
264: } else if (actionId.equals(ActionFactory.DELETE.getId())) {
265: fSourceViewer.doOperation(ITextOperationTarget.DELETE);
266: return true;
267: } else if (actionId.equals(ActionFactory.UNDO.getId())) {
268: fSourceViewer.doOperation(ITextOperationTarget.UNDO);
269: return true;
270: } else if (actionId.equals(ActionFactory.REDO.getId())) {
271: fSourceViewer.doOperation(ITextOperationTarget.REDO);
272: return true;
273: }
274: return false;
275: }
276:
277: public boolean setFormInput(Object input) {
278: if (input instanceof IFeatureInfo) {
279: IFeatureInfo info = (IFeatureInfo) input;
280: int index = info.getIndex();
281: if (index != -1)
282: fTabFolder.setSelection(index);
283: updateEditorInput(input, false);
284: return true;
285: }
286: if (input instanceof IFeatureURLElement
287: || input instanceof NamedElement) {
288: fTabFolder.setSelection(3);
289: updateEditorInput(input, false);
290: return true;
291: }
292: return false;
293: }
294:
295: private void handleApply() {
296: if (0 <= fElementIndex && fElementIndex < 3 && fElement != null) {
297: handleApply((IFeatureInfo) fElement, fTabFolder
298: .getSelectionIndex());
299: } else {
300: handleApply(null, fTabFolder.getSelectionIndex());
301: }
302: }
303:
304: private void handleApply(IFeatureInfo info, int index) {
305: if (index >= 3)
306: return;
307: String urlName = fUrlText.getText();
308: String text = fDocument.get();
309: applyInfoText(info, urlName, text, index);
310: updateTabImage(fTabFolder.getSelection());
311: }
312:
313: private void applyInfoText(IFeatureInfo targetInfo, String urlText,
314: String text, int index) {
315: String url = null;
316:
317: if (urlText.length() > 0) {
318: url = urlText;
319: }
320: try {
321: IFeatureModel model = (IFeatureModel) getPage().getModel();
322: IFeature feature = model.getFeature();
323:
324: if (targetInfo == null)
325: targetInfo = feature.getFeatureInfo(index);
326:
327: if (targetInfo == null) {
328: targetInfo = model.getFactory().createInfo(index);
329: feature.setFeatureInfo(targetInfo, index);
330: }
331: targetInfo.setURL(url);
332: targetInfo.setDescription(text);
333: } catch (CoreException e) {
334: }
335: }
336:
337: protected void fillContextMenu(IMenuManager manager) {
338: getPage().getPDEEditor().getContributor()
339: .contextMenuAboutToShow(manager);
340: }
341:
342: public void initialize() {
343: IFeatureModel featureModel = (IFeatureModel) getPage()
344: .getModel();
345: fDocument.addDocumentListener(new IDocumentListener() {
346: public void documentChanged(DocumentEvent e) {
347: infoModified();
348: }
349:
350: public void documentAboutToBeChanged(DocumentEvent e) {
351: }
352: });
353: fUrlText.setEditable(featureModel.isEditable());
354: fSourceViewer.getTextWidget().setEditable(
355: featureModel.isEditable());
356: featureModel.addModelChangedListener(this );
357: updateEditorInput(featureModel.getFeature().getFeatureInfo(0),
358: false);
359: }
360:
361: /* (non-Javadoc)
362: * @see org.eclipse.ui.forms.AbstractFormPart#dispose()
363: */
364: public void dispose() {
365: // Dispose of the source configuration
366: if (fSourceConfiguration != null) {
367: fSourceConfiguration.dispose();
368: }
369: IFeatureModel featureModel = (IFeatureModel) getPage()
370: .getModel();
371: if (featureModel != null)
372: featureModel.removeModelChangedListener(this );
373: super .dispose();
374: }
375:
376: private void infoModified() {
377: IFeatureModel featureModel = (IFeatureModel) getPage()
378: .getModel();
379: if (!fIgnoreChange && featureModel instanceof IEditable) {
380: markDirty();
381: }
382: }
383:
384: public void modelChanged(IModelChangedEvent e) {
385: if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
386: markStale();
387: }
388: }
389:
390: public void refresh() {
391: IFeatureModel model = (IFeatureModel) getPage().getModel();
392: int index = fTabFolder.getSelectionIndex();
393: if (index < 3) {
394: IFeatureInfo info = model.getFeature()
395: .getFeatureInfo(index);
396: fElement = null;
397: fElementIndex = -1;
398: updateEditorInput(info, false);
399: }
400: super .refresh();
401: }
402:
403: private void createTabs() {
404: IFeatureModel model = (IFeatureModel) getPage().getModel();
405: IFeature feature = model.getFeature();
406: addTab(PDEUIMessages.FeatureEditor_info_description, feature
407: .getFeatureInfo(0));
408: addTab(PDEUIMessages.FeatureEditor_info_copyright, feature
409: .getFeatureInfo(1));
410: addTab(PDEUIMessages.FeatureEditor_info_license, feature
411: .getFeatureInfo(2));
412: addTab(PDEUIMessages.FeatureEditor_info_discoveryUrls, null);
413: }
414:
415: private void addTab(String label, IFeatureInfo info) {
416: CTabItem item = new CTabItem(fTabFolder, SWT.NULL);
417: item.setText(label);
418: item.setData(info);
419: updateTabImage(item);
420: }
421:
422: private void updateTabImage(CTabItem item) {
423: if (item == null)
424: return;
425: Object info = item.getData();
426: if (info != null) {
427: item.setImage(PDEPlugin.getDefault().getLabelProvider()
428: .getImage(info));
429: } else {
430: item.setImage(PDEPlugin.getDefault().getLabelProvider()
431: .get(PDEPluginImages.DESC_DOC_SECTION_OBJ));
432: }
433: }
434:
435: private void updateTabSelection() {
436: IFeatureModel model = (IFeatureModel) getPage().getModel();
437: IFeature feature = model.getFeature();
438: int index = fTabFolder.getSelectionIndex();
439: if (index < 3) {
440: IFeatureInfo info = feature.getFeatureInfo(index);
441: updateEditorInput(info, true);
442: }
443: Control oldPage = fNotebookLayout.topControl;
444: if (index < 3)
445: fNotebookLayout.topControl = fInfoPage;
446: else
447: fNotebookLayout.topControl = fUrlsPage;
448: if (oldPage != fNotebookLayout.topControl)
449: fNotebook.layout();
450: }
451:
452: public void setFocus() {
453: fSourceViewer.getTextWidget().setFocus();
454: updateSelection(fSourceViewer.getSelection());
455: }
456:
457: private void commitPrevious() {
458: IFeatureInfo previous = (IFeatureInfo) fElement;
459: handleApply(previous, fElementIndex);
460: }
461:
462: public void updateEditorInput(Object input, boolean commitPrevious) {
463: if (isDirty() && commitPrevious /*
464: * && element != null && element !=
465: * input
466: */) {
467: commitPrevious();
468: }
469: fIgnoreChange = true;
470: String text = ""; //$NON-NLS-1$
471: String url = null;
472: if (input instanceof IFeatureInfo) {
473: IFeatureInfo info = (IFeatureInfo) input;
474: text = info.getDescription();
475: url = info.getURL();
476: }
477: if (text == null)
478: text = ""; //$NON-NLS-1$
479: else
480: text = TextUtil.createMultiLine(text, 60, false);
481: fDocument.set(text);
482: if (url == null)
483: fUrlText.setText(""); //$NON-NLS-1$
484: else
485: fUrlText.setText(url.toString());
486: fElement = input;
487: fElementIndex = fTabFolder.getSelectionIndex();
488:
489: Control oldPage = fNotebookLayout.topControl;
490: if (input instanceof IFeatureURLElement
491: || input instanceof NamedElement) {
492: fNotebookLayout.topControl = fUrlsPage;
493: } else {
494: fNotebookLayout.topControl = fInfoPage;
495: }
496: if (oldPage != fNotebookLayout.topControl)
497: fNotebook.layout();
498:
499: fIgnoreChange = false;
500: }
501:
502: public boolean canPaste(Clipboard clipboard) {
503: return fSourceViewer.canDoOperation(ITextOperationTarget.PASTE);
504: }
505: }
|