001: /*******************************************************************************
002: * Copyright (c) 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.toc;
011:
012: import org.eclipse.jface.text.source.ISourceViewer;
013: import org.eclipse.jface.viewers.ISelection;
014: import org.eclipse.jface.viewers.StructuredSelection;
015: import org.eclipse.pde.core.IModelChangedEvent;
016: import org.eclipse.pde.core.IModelChangedListener;
017: import org.eclipse.pde.internal.core.itoc.ITocConstants;
018: import org.eclipse.pde.internal.core.text.IDocumentAttributeNode;
019: import org.eclipse.pde.internal.core.text.IDocumentRange;
020: import org.eclipse.pde.internal.core.text.IDocumentTextNode;
021: import org.eclipse.pde.internal.core.text.toc.Toc;
022: import org.eclipse.pde.internal.core.text.toc.TocModel;
023: import org.eclipse.pde.internal.core.text.toc.TocObject;
024: import org.eclipse.pde.internal.core.util.PDETextHelper;
025: import org.eclipse.pde.internal.ui.IHelpContextIds;
026: import org.eclipse.pde.internal.ui.IPDEUIConstants;
027: import org.eclipse.pde.internal.ui.PDEPlugin;
028: import org.eclipse.pde.internal.ui.PDEPluginImages;
029: import org.eclipse.pde.internal.ui.PDEUIMessages;
030: import org.eclipse.pde.internal.ui.editor.PDEFormPage;
031: import org.eclipse.pde.internal.ui.editor.PDEMasterDetailsBlock;
032: import org.eclipse.swt.custom.StyledText;
033: import org.eclipse.ui.PlatformUI;
034: import org.eclipse.ui.forms.IManagedForm;
035: import org.eclipse.ui.forms.editor.FormEditor;
036: import org.eclipse.ui.forms.editor.IFormPage;
037: import org.eclipse.ui.forms.widgets.ScrolledForm;
038:
039: /**
040: * TocPage
041: *
042: */
043: public class TocPage extends PDEFormPage implements
044: IModelChangedListener {
045: public static final String PAGE_ID = "tocPage"; //$NON-NLS-1$
046:
047: private TocBlock fBlock;
048:
049: /**
050: * @param editor
051: */
052: public TocPage(FormEditor editor) {
053: super (editor, PAGE_ID, PDEUIMessages.TocPage_title);
054:
055: fBlock = new TocBlock(this );
056: }
057:
058: /**
059: * @return
060: */
061: public PDEMasterDetailsBlock getBlock() {
062: return fBlock;
063: }
064:
065: /* (non-Javadoc)
066: * @see org.eclipse.pde.internal.ui.editor.PDEFormPage#createFormContent(org.eclipse.ui.forms.IManagedForm)
067: */
068: protected void createFormContent(IManagedForm managedForm) {
069: ScrolledForm form = managedForm.getForm();
070: TocModel model = (TocModel) getModel();
071:
072: // Ensure the model was loaded properly
073: if ((model == null) || (model.isLoaded() == false)) {
074: createErrorContent(managedForm, model);
075: return;
076: }
077:
078: PlatformUI.getWorkbench().getHelpSystem().setHelp(
079: form.getBody(), IHelpContextIds.TOC_EDITOR);
080: // Create the rest of the actions in the form title area
081: super .createFormContent(managedForm);
082: // Form image
083: form.setImage(PDEPlugin.getDefault().getLabelProvider().get(
084: PDEPluginImages.DESC_TOC_OBJ));
085: setFormTitle(form, model);
086: // Create the master details block
087: fBlock.createContent(managedForm);
088: // Force the selection in the masters tree section to load the
089: // proper details section
090: fBlock.getMasterSection().fireSelection();
091: // Register this page to be informed of model change events
092: model.addModelChangedListener(this );
093: }
094:
095: private void createErrorContent(IManagedForm managedForm,
096: TocModel model) {
097: Exception e = null;
098: //e = ((AbstractModel)model).getException();
099:
100: // Create a formatted error page
101: createFormErrorContent(managedForm,
102: PDEUIMessages.TocPage_msgTOCLoadFailure,
103: PDEUIMessages.TocPage_msgTOCParsingFailure, e);
104: }
105:
106: private void setFormTitle(ScrolledForm form, TocModel model) {
107: // Form title
108: String title = PDETextHelper.translateReadText(model.getToc()
109: .getFieldLabel());
110: if (title.length() > 0) {
111: form.setText(title);
112: } else {
113: form.setText(PDEUIMessages.TocPage_title);
114: }
115: }
116:
117: /* (non-Javadoc)
118: * @see org.eclipse.pde.internal.ui.editor.PDEFormPage#dispose()
119: */
120: public void dispose() {
121:
122: TocModel tocModel = (TocModel) getModel();
123: if (tocModel != null) {
124: tocModel.removeModelChangedListener(this );
125: }
126: super .dispose();
127: }
128:
129: /* (non-Javadoc)
130: * @see org.eclipse.pde.core.IModelChangedListener#modelChanged(org.eclipse.pde.core.IModelChangedEvent)
131: */
132: public void modelChanged(IModelChangedEvent event) {
133:
134: if (event.getChangeType() == IModelChangedEvent.CHANGE) {
135: Object[] objects = event.getChangedObjects();
136: TocObject object = (TocObject) objects[0];
137: if (object == null) {
138: // Ignore
139: } else if (object.getType() == ITocConstants.TYPE_TOC) {
140: String changeProperty = event.getChangedProperty();
141: if ((changeProperty != null)
142: && changeProperty
143: .equals(ITocConstants.ATTRIBUTE_LABEL)) {
144: // Has to be a String if the property is a title
145: // Update the form page title
146: getManagedForm().getForm().setText(
147: PDETextHelper
148: .translateReadText((String) event
149: .getNewValue()));
150: }
151: }
152: } else if (event.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
153: handleModelEventWorldChanged(event);
154: }
155: // Inform the block
156: fBlock.modelChanged(event);
157: }
158:
159: /**
160: * @return
161: */
162: public ISelection getSelection() {
163: return fBlock.getSelection();
164: }
165:
166: /**
167: * @param event
168: */
169: private void handleModelEventWorldChanged(IModelChangedEvent event) {
170:
171: Object[] objects = event.getChangedObjects();
172:
173: if (objects[0] != null && objects[0] instanceof TocObject) {
174: TocObject object = (TocObject) objects[0];
175: if (object.getType() == ITocConstants.TYPE_TOC) {
176: String newValue = ((Toc) object).getFieldLabel();
177:
178: // Update page title
179: getManagedForm().getForm().setText(
180: PDETextHelper.translateReadText(newValue));
181: }
182: }
183: }
184:
185: public void setActive(boolean active) {
186: super .setActive(active);
187: if (active) {
188: IFormPage page = getPDEEditor().findPage(
189: TocInputContext.CONTEXT_ID);
190: if (page instanceof TocSourcePage
191: && ((TocSourcePage) page).getInputContext()
192: .isInSourceMode()) {
193: ISourceViewer viewer = ((TocSourcePage) page)
194: .getViewer();
195: if (viewer == null) {
196: return;
197: }
198:
199: StyledText text = viewer.getTextWidget();
200: if (text == null) {
201: return;
202: }
203:
204: int offset = text.getCaretOffset();
205: if (offset < 0) {
206: return;
207: }
208:
209: IDocumentRange range = ((TocSourcePage) page)
210: .getRangeElement(offset, true);
211: if (range instanceof IDocumentAttributeNode) {
212: range = ((IDocumentAttributeNode) range)
213: .getEnclosingElement();
214: } else if (range instanceof IDocumentTextNode) {
215: range = ((IDocumentTextNode) range)
216: .getEnclosingElement();
217: }
218:
219: if (range instanceof TocObject) {
220: fBlock.getMasterSection().setSelection(
221: new StructuredSelection(range));
222: }
223: }
224: }
225: }
226:
227: /* (non-Javadoc)
228: * @see org.eclipse.pde.internal.ui.editor.PDEFormPage#getHelpResource()
229: */
230: protected String getHelpResource() {
231: return IPDEUIConstants.PLUGIN_DOC_ROOT
232: + "guide/tools/editors/toc_editor/page_toc.htm"; //$NON-NLS-1$
233: }
234: }
|