KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > target > BaseTargetDefinitionOperation


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.target;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.StructuredSelection;
20 import org.eclipse.pde.internal.core.ICoreConstants;
21 import org.eclipse.pde.internal.core.PDECore;
22 import org.eclipse.pde.internal.core.itarget.ILocationInfo;
23 import org.eclipse.pde.internal.core.itarget.ITarget;
24 import org.eclipse.pde.internal.core.itarget.ITargetModel;
25 import org.eclipse.pde.internal.core.target.WorkspaceTargetModel;
26 import org.eclipse.pde.internal.ui.IPDEUIConstants;
27 import org.eclipse.pde.internal.ui.PDEPlugin;
28 import org.eclipse.swt.widgets.Display;
29 import org.eclipse.ui.IWorkbenchPage;
30 import org.eclipse.ui.IWorkbenchPart;
31 import org.eclipse.ui.IWorkbenchWindow;
32 import org.eclipse.ui.PartInitException;
33 import org.eclipse.ui.actions.WorkspaceModifyOperation;
34 import org.eclipse.ui.ide.IDE;
35 import org.eclipse.ui.part.ISetSelectionTarget;
36
37 public class BaseTargetDefinitionOperation extends WorkspaceModifyOperation {
38     
39     private IFile fFile;
40
41     public BaseTargetDefinitionOperation(IFile file){
42         fFile = file;
43     }
44
45     protected void execute(IProgressMonitor monitor) throws CoreException,
46             InvocationTargetException JavaDoc, InterruptedException JavaDoc {
47         WorkspaceTargetModel model = new WorkspaceTargetModel(fFile, false);
48         initializeTarget(model);
49         model.save();
50         model.dispose();
51         openFile();
52         monitor.done();
53     }
54     
55     protected void openFile() {
56         Display.getCurrent().asyncExec(new Runnable JavaDoc() {
57             public void run() {
58                 IWorkbenchWindow ww = PDEPlugin.getActiveWorkbenchWindow();
59                 if (ww == null) {
60                     return;
61                 }
62                 IWorkbenchPage page = ww.getActivePage();
63                 if (page == null || !fFile.exists())
64                     return;
65                 IWorkbenchPart focusPart = page.getActivePart();
66                 if (focusPart instanceof ISetSelectionTarget) {
67                     ISelection selection = new StructuredSelection(fFile);
68                     ((ISetSelectionTarget) focusPart).selectReveal(selection);
69                 }
70                 try {
71                     IDE.openEditor(page, fFile, IPDEUIConstants.TARGET_EDITOR_ID);
72                 } catch (PartInitException e) {
73                 }
74             }
75         });
76     }
77     
78     protected void initializeTarget(ITargetModel model) {
79         ITarget target = model.getTarget();
80         ILocationInfo info = model.getFactory().createLocation();
81         info.setPath(PDECore.getDefault().getPluginPreferences().getString(ICoreConstants.PLATFORM_PATH));
82         info.setDefault(true);
83         target.setLocationInfo(info);
84     }
85
86 }
87
Popular Tags