1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.ide.eclipse.adt.internal.wizards.actions;
18 
19 import com.android.ide.eclipse.adt.internal.ui.IUpdateWizardDialog;
20 import com.android.ide.eclipse.adt.internal.ui.WizardDialogEx;
21 
22 import org.eclipse.jface.action.IAction;
23 import org.eclipse.jface.dialogs.Dialog;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.jface.viewers.StructuredSelection;
27 import org.eclipse.swt.graphics.Point;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.ui.IEditorInput;
30 import org.eclipse.ui.IEditorPart;
31 import org.eclipse.ui.IObjectActionDelegate;
32 import org.eclipse.ui.IWorkbench;
33 import org.eclipse.ui.IWorkbenchPart;
34 import org.eclipse.ui.IWorkbenchWindow;
35 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
36 import org.eclipse.ui.IWorkbenchWizard;
37 import org.eclipse.ui.PlatformUI;
38 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
39 import org.eclipse.ui.internal.LegacyResourceSupport;
40 import org.eclipse.ui.internal.actions.NewWizardShortcutAction;
41 import org.eclipse.ui.internal.util.Util;
42 
43 /**
44  * An abstract action that displays one of our wizards.
45  * Derived classes must provide the actual wizard to display.
46  */
47 /*package*/ abstract class OpenWizardAction
48     implements IWorkbenchWindowActionDelegate, IObjectActionDelegate {
49 
50     /**
51      * The wizard dialog width, extracted from {@link NewWizardShortcutAction}
52      */
53     private static final int SIZING_WIZARD_WIDTH = 500;
54 
55     /**
56      * The wizard dialog height, extracted from {@link NewWizardShortcutAction}
57      */
58     private static final int SIZING_WIZARD_HEIGHT = 500;
59 
60     /** The wizard that was created by {@link #run(IAction)}. */
61     private IWorkbenchWizard mWizard;
62     /** The result from the dialog */
63     private int mDialogResult;
64 
65     private ISelection mSelection;
66     private IWorkbench mWorkbench;
67 
68     /** Returns the wizard that was created by {@link #run(IAction)}. */
getWizard()69     public IWorkbenchWizard getWizard() {
70         return mWizard;
71     }
72 
73     /** Returns the result from {@link Dialog#open()}, available after
74      * the completion of {@link #run(IAction)}. */
getDialogResult()75     public int getDialogResult() {
76         return mDialogResult;
77     }
78 
79     /* (non-Javadoc)
80      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
81      */
82     @Override
dispose()83     public void dispose() {
84         // pass
85     }
86 
87     /* (non-Javadoc)
88      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
89      */
90     @Override
init(IWorkbenchWindow window)91     public void init(IWorkbenchWindow window) {
92         // pass
93     }
94 
95     /**
96      * Opens and display the Android New Project Wizard.
97      * <p/>
98      * Most of this implementation is extracted from {@link NewWizardShortcutAction#run()}.
99      *
100      * @param action The action that got us here. Can be null when used internally.
101      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
102      */
103     @Override
run(IAction action)104     public void run(IAction action) {
105 
106         // get the workbench and the current window
107         IWorkbench workbench = mWorkbench != null ? mWorkbench : PlatformUI.getWorkbench();
108         IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
109 
110         // This code from NewWizardShortcutAction#run() gets the current window selection
111         // and converts it to a workbench structured selection for the wizard, if possible.
112         ISelection selection = mSelection;
113         if (selection == null) {
114             selection = window.getSelectionService().getSelection();
115         }
116 
117         IStructuredSelection selectionToPass = StructuredSelection.EMPTY;
118         if (selection instanceof IStructuredSelection) {
119             selectionToPass = (IStructuredSelection) selection;
120         } else {
121             // Build the selection from the IFile of the editor
122             IWorkbenchPart part = window.getPartService().getActivePart();
123             if (part instanceof IEditorPart) {
124                 IEditorInput input = ((IEditorPart) part).getEditorInput();
125                 Class<?> fileClass = LegacyResourceSupport.getFileClass();
126                 if (input != null && fileClass != null) {
127                     Object file = Util.getAdapter(input, fileClass);
128                     if (file != null) {
129                         selectionToPass = new StructuredSelection(file);
130                     }
131                 }
132             }
133         }
134 
135         // Create the wizard and initialize it with the selection
136         mWizard = instanciateWizard(action);
137         mWizard.init(workbench, selectionToPass);
138 
139         // It's not visible yet until a dialog is created and opened
140         Shell parent = window.getShell();
141         WizardDialogEx dialog = new WizardDialogEx(parent, mWizard);
142         dialog.create();
143 
144         if (mWizard instanceof IUpdateWizardDialog) {
145             ((IUpdateWizardDialog) mWizard).updateWizardDialog(dialog);
146         }
147 
148         // This code comes straight from NewWizardShortcutAction#run()
149         Point defaultSize = dialog.getShell().getSize();
150         dialog.getShell().setSize(
151                 Math.max(SIZING_WIZARD_WIDTH, defaultSize.x),
152                 Math.max(SIZING_WIZARD_HEIGHT, defaultSize.y));
153         window.getWorkbench().getHelpSystem().setHelp(dialog.getShell(),
154                 IWorkbenchHelpContextIds.NEW_WIZARD_SHORTCUT);
155 
156         mDialogResult = dialog.open();
157     }
158 
159     /**
160      * Called by {@link #run(IAction)} to instantiate the actual wizard.
161      *
162      * @param action The action parameter from {@link #run(IAction)}.
163      *               This can be null.
164      * @return A new wizard instance. Must not be null.
165      */
instanciateWizard(IAction action)166     protected abstract IWorkbenchWizard instanciateWizard(IAction action);
167 
168     /* (non-Javadoc)
169      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
170      */
171     @Override
selectionChanged(IAction action, ISelection selection)172     public void selectionChanged(IAction action, ISelection selection) {
173         mSelection = selection;
174     }
175 
176     /* (non-Javadoc)
177      * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
178      */
179     @Override
setActivePart(IAction action, IWorkbenchPart targetPart)180     public void setActivePart(IAction action, IWorkbenchPart targetPart) {
181         mWorkbench = targetPart.getSite().getWorkbenchWindow().getWorkbench();
182     }
183 }
184