|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2008 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.pde.internal.ui.wizards.ctxhelp; |
| 12 |
|
| 13 |
import java.lang.reflect.InvocationTargetException; |
| 14 |
import org.eclipse.core.resources.IFile; |
| 15 |
import org.eclipse.core.runtime.CoreException; |
| 16 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 17 |
import org.eclipse.jface.viewers.ISelection; |
| 18 |
import org.eclipse.jface.viewers.StructuredSelection; |
| 19 |
import org.eclipse.pde.internal.core.text.ctxhelp.*; |
| 20 |
import org.eclipse.pde.internal.core.util.CoreUtility; |
| 21 |
import org.eclipse.pde.internal.ui.*; |
| 22 |
import org.eclipse.swt.widgets.Display; |
| 23 |
import org.eclipse.ui.*; |
| 24 |
import org.eclipse.ui.actions.WorkspaceModifyOperation; |
| 25 |
import org.eclipse.ui.ide.IDE; |
| 26 |
import org.eclipse.ui.part.ISetSelectionTarget; |
| 27 |
|
| 28 |
/** |
| 29 |
* Operation to create a new context help xml file, add example entries and open it |
| 30 |
* in the context help editor. |
| 31 |
* @since 3.4 |
| 32 |
*/ |
| 33 |
public class NewCtxHelpOperation extends WorkspaceModifyOperation { |
| 34 |
|
| 35 |
private IFile fFile; |
| 36 |
|
| 37 |
public NewCtxHelpOperation(IFile file) { |
| 38 |
fFile = file; |
| 39 |
} |
| 40 |
|
| 41 |
/* (non-Javadoc) |
| 42 |
* @see org.eclipse.ui.actions.WorkspaceModifyOperation#execute(org.eclipse.core.runtime.IProgressMonitor) |
| 43 |
*/ |
| 44 |
protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException { |
| 45 |
CtxHelpModel model = new CtxHelpModel(CoreUtility.getTextDocument(fFile.getContents()), false); |
| 46 |
model.setUnderlyingResource(fFile); |
| 47 |
initializeModel(model); |
| 48 |
model.save(); |
| 49 |
model.dispose(); |
| 50 |
openFile(); |
| 51 |
monitor.done(); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Initialize the xml with example entries |
| 56 |
* @param model model for the file |
| 57 |
*/ |
| 58 |
private void initializeModel(CtxHelpModel model) { |
| 59 |
CtxHelpContext context = model.getFactory().createContext(); |
| 60 |
context.setID(PDEUIMessages.NewCtxHelpOperation_ExampleContextId); |
| 61 |
model.getCtxHelpRoot().addChild(context); |
| 62 |
CtxHelpTopic topic = model.getFactory().createTopic(); |
| 63 |
topic.setLabel(PDEUIMessages.NewCtxHelpOperation_ExampleTopicLabel); |
| 64 |
context.addChild(topic); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Asynchronously opens the created file in the context help editor. |
| 69 |
*/ |
| 70 |
protected void openFile() { |
| 71 |
Display.getCurrent().asyncExec(new Runnable() { |
| 72 |
public void run() { |
| 73 |
IWorkbenchWindow ww = PDEPlugin.getActiveWorkbenchWindow(); |
| 74 |
if (ww == null) { |
| 75 |
return; |
| 76 |
} |
| 77 |
IWorkbenchPage page = ww.getActivePage(); |
| 78 |
if (page == null || !fFile.exists()) |
| 79 |
return; |
| 80 |
IWorkbenchPart focusPart = page.getActivePart(); |
| 81 |
if (focusPart instanceof ISetSelectionTarget) { |
| 82 |
ISelection selection = new StructuredSelection(fFile); |
| 83 |
((ISetSelectionTarget) focusPart).selectReveal(selection); |
| 84 |
} |
| 85 |
try { |
| 86 |
IDE.openEditor(page, fFile, IPDEUIConstants.CONTEXT_HELP_EDITOR_ID); |
| 87 |
} catch (PartInitException e) { |
| 88 |
} |
| 89 |
} |
| 90 |
}); |
| 91 |
} |
| 92 |
|
| 93 |
} |