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.wizards.toc;
011:
012: import java.lang.reflect.InvocationTargetException;
013:
014: import org.eclipse.core.resources.IFile;
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.IProgressMonitor;
017: import org.eclipse.jface.viewers.ISelection;
018: import org.eclipse.jface.viewers.StructuredSelection;
019: import org.eclipse.pde.internal.core.text.toc.TocModel;
020: import org.eclipse.pde.internal.core.text.toc.TocDocumentFactory;
021: import org.eclipse.pde.internal.core.text.toc.TocTopic;
022: import org.eclipse.pde.internal.core.util.CoreUtility;
023: import org.eclipse.pde.internal.ui.IPDEUIConstants;
024: import org.eclipse.pde.internal.ui.PDEPlugin;
025: import org.eclipse.pde.internal.ui.PDEUIMessages;
026: import org.eclipse.swt.widgets.Display;
027: import org.eclipse.ui.IWorkbenchPage;
028: import org.eclipse.ui.IWorkbenchPart;
029: import org.eclipse.ui.IWorkbenchWindow;
030: import org.eclipse.ui.PartInitException;
031: import org.eclipse.ui.actions.WorkspaceModifyOperation;
032: import org.eclipse.ui.ide.IDE;
033: import org.eclipse.ui.part.ISetSelectionTarget;
034:
035: public class TocOperation extends WorkspaceModifyOperation {
036:
037: private IFile fFile;
038: private String fTocName;
039:
040: public TocOperation(IFile file, String tocName) {
041: fFile = file;
042: fTocName = tocName;
043: }
044:
045: protected void execute(IProgressMonitor monitor)
046: throws CoreException, InvocationTargetException,
047: InterruptedException {
048: TocModel model = new TocModel(CoreUtility.getTextDocument(fFile
049: .getContents()), false);
050: model.setUnderlyingResource(fFile);
051: initializeToc(model);
052: model.save();
053: model.dispose();
054: openFile();
055: monitor.done();
056: }
057:
058: private void initializeToc(TocModel model) {
059: // Create Topic
060: TocTopic topic = createTopic(model);
061:
062: // Bind the created topic to this TOC
063: model.getToc().addChild(topic);
064:
065: // Set the initial TOC name
066: model.getToc().setFieldLabel(fTocName);
067: }
068:
069: private TocTopic createTopic(TocModel model) {
070: TocDocumentFactory factory = model.getFactory();
071: TocTopic topic = factory.createTocTopic();
072:
073: topic.setFieldLabel(PDEUIMessages.TocPage_TocTopic);
074:
075: return topic;
076: }
077:
078: protected void openFile() {
079: Display.getCurrent().asyncExec(new Runnable() {
080: public void run() {
081: IWorkbenchWindow ww = PDEPlugin
082: .getActiveWorkbenchWindow();
083: if (ww == null) {
084: return;
085: }
086: IWorkbenchPage page = ww.getActivePage();
087: if (page == null || !fFile.exists())
088: return;
089: IWorkbenchPart focusPart = page.getActivePart();
090: if (focusPart instanceof ISetSelectionTarget) {
091: ISelection selection = new StructuredSelection(
092: fFile);
093: ((ISetSelectionTarget) focusPart)
094: .selectReveal(selection);
095: }
096: try {
097: IDE
098: .openEditor(
099: page,
100: fFile,
101: IPDEUIConstants.TABLE_OF_CONTENTS_EDITOR_ID);
102: } catch (PartInitException e) {
103: }
104: }
105: });
106: }
107:
108: }
|