summaryrefslogtreecommitdiff
path: root/src/plugins/android/src/com/motorola/studio/android/perspective/MotodevStudioAndroidPerspective.java
blob: 843aec963d0e0825eb1b3fc1d2b6e3044ce3b89a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.motorola.studio.android.perspective;

import java.net.URL;

import org.eclipse.core.runtime.Platform;
import org.eclipse.jdt.internal.ui.JavaPerspectiveFactory;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IFolderLayout;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPageListener;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IPerspectiveFactory;
import org.eclipse.ui.IPerspectiveListener;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PerspectiveAdapter;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.intro.IIntroConstants;
import org.osgi.framework.BundleContext;

import com.motorola.studio.android.AndroidPlugin;
import com.motorola.studio.android.common.log.StudioLogger;
import com.motorola.studio.android.common.utilities.EclipseUtils;

@SuppressWarnings("restriction")
public class MotodevStudioAndroidPerspective extends JavaPerspectiveFactory
{
    private static String VIEW_PACKAGE_EXPLORER = "org.eclipse.jdt.ui.PackageExplorer";

    private static String VIEW_PROBLEM = "org.eclipse.ui.views.ProblemView";

    private static String VIEW_CONSOLE = "org.eclipse.ui.console.ConsoleView";

    private static String VIEW_OUTLINE = "org.eclipse.ui.views.ContentOutline";

    private static String VIEW_ANDROID_EMULATOR =
            "com.motorola.studio.android.emulator.androidView";

    private static String VIEW_SNIPPETS =
            "org.eclipse.wst.common.snippets.internal.ui.SnippetsView";

    private static String VIEW_RSS = "biz.junginger.newsfeed.eclipse.FeedView";

    // DDMS Views

    private static String DDMSVIEW_EMULATOR_CONTROL =
            "com.android.ide.eclipse.ddms.views.EmulatorControlView";

    private static String DDMSVIEW_LOGCAT = "com.android.ide.eclipse.ddms.views.LogCatView";

    private static String DDMSVIEW_FILE_EXPLORER =
            "com.android.ide.eclipse.ddms.views.FileExplorerView";

    private static String PERSPECTIVE_ANDROID = "com.motorola.studio.android.perspective";

    private static String PERSPECTIVE_OPHONE = "com.motorola.studio.android.ophone.perspective";

    private static String PERSPECTIVE_DDMS = "com.android.ide.eclipse.ddms.Perspective";

    private static String PERSPECTIVE_EMULATOR = "com.motorola.studio.android.emulator.perspective";

    private static String PERSPECTIVE_DEBUG = "org.eclipse.debug.ui.DebugPerspective";

    private static String LAUNCH_COOLBAR_SHORTCUT = "org.eclipse.debug.ui.launchActionSet";

    private static String VIEW_TML_DEV_MGT =
            "org.eclipse.sequoyah.device.framework.ui.InstanceMgtView";

    private static String WIZARD_PROJECT = "com.motorola.studio.android.wizards.newProjectWizard";

    private static String WIZARD_WIDGET_PROJECT =
            "com.motorola.studio.android.wizards.newWidgetProjectWizard";

    private static String WIZARD_ACTIVITY = "com.motorola.studio.android.wizards.newActivityWizard";

    private static String WIZARD_ACTIVITY_BASED_ON_TEMPLATE =
            "com.motorola.studio.android.wizards.newActivityBasedOnTemplateWizard";

    private static String WIZARD_RECEIVER = "com.motorola.studio.android.wizards.newReceiverWizard";

    private static String WIZARD_SERVICE = "com.motorola.studio.android.wizards.newServiceWizard";

    private static String WIZARD_PROVIDER = "com.motorola.studio.android.wizards.newProviderWizard";

    private static String WIZARD_ANDROID_XML =
            "com.android.ide.eclipse.editors.wizards.NewXmlFileWizard";

    private static String WIZARD_WIDGET_PROVIDER =
            "com.motorola.studio.android.wizard.newWidgetProviderWizard";

    private static String WIZARD_JAVA_PACKAGE =
            "org.eclipse.jdt.ui.wizards.NewPackageCreationWizard";

    private static String WIZARD_JAVA_CLASS = "org.eclipse.jdt.ui.wizards.NewClassCreationWizard";

    private static String WIZARD_JAVA_INTERFACE =
            "org.eclipse.jdt.ui.wizards.NewInterfaceCreationWizard";

    private static String WIZARD_NEW_FOLDER = "org.eclipse.ui.wizards.new.folder";

    private static String VIEW_APPLICATION_SIGNING_TOOL =
            "com.motorola.studio.android.packaging.ui.signingview";

    private static final String STUDIO_INFO_INITIAL_PAGE_PROPERTY = "studio.android.initial.page";

    private static final String STUDIO_INFO_INITIAL_PAGE_FILE = "MOTODEV/index.html";

    private static IPerspectiveListener perspectiveListener = null;

    /**
     * Creates the initial layout for a page.
     * 
     * @param layout
     *            the page layout
     * 
     * @see IPerspectiveFactory#createInitialLayout(IPageLayout)
     */
    @Override
    public void createInitialLayout(final IPageLayout layout)
    {
        String editorArea = layout.getEditorArea();
        layout.setEditorAreaVisible(true);

        IFolderLayout left = layout.createFolder("left", IPageLayout.LEFT, 0.2f, editorArea);
        left.addView(VIEW_PACKAGE_EXPLORER);
        left.addView(DDMSVIEW_FILE_EXPLORER);

        IFolderLayout leftBottom =
                layout.createFolder("leftBottom", IPageLayout.BOTTOM, 0.59f, "left");
        leftBottom.addView(VIEW_SNIPPETS);
        leftBottom.addView(VIEW_OUTLINE);

        IFolderLayout right = layout.createFolder("right", IPageLayout.RIGHT, 0.6f, editorArea);
        right.addView(VIEW_ANDROID_EMULATOR);
        right.addView(VIEW_RSS);

        IFolderLayout bottomMiddle =
                layout.createFolder("bottomMiddle", IPageLayout.BOTTOM, 0.59f, editorArea);
        bottomMiddle.addView(VIEW_TML_DEV_MGT);
        bottomMiddle.addView(DDMSVIEW_EMULATOR_CONTROL);
        bottomMiddle.addView(DDMSVIEW_LOGCAT);
        bottomMiddle.addView(VIEW_CONSOLE);
        bottomMiddle.addView(VIEW_PROBLEM);
        bottomMiddle.addView(VIEW_APPLICATION_SIGNING_TOOL);

        layout.addShowViewShortcut(VIEW_TML_DEV_MGT);
        layout.addShowViewShortcut(VIEW_ANDROID_EMULATOR);
        layout.addShowViewShortcut(VIEW_SNIPPETS);
        layout.addShowViewShortcut(VIEW_APPLICATION_SIGNING_TOOL);
        //     layout.addShowViewShortcut(VIEW_VIDEOS);

        layout.addPerspectiveShortcut(PERSPECTIVE_ANDROID);
        layout.addPerspectiveShortcut(PERSPECTIVE_OPHONE);
        layout.addPerspectiveShortcut(PERSPECTIVE_DDMS);
        layout.addPerspectiveShortcut(PERSPECTIVE_DEBUG);
        layout.addPerspectiveShortcut(PERSPECTIVE_EMULATOR);

        layout.addActionSet(LAUNCH_COOLBAR_SHORTCUT);

        layout.addNewWizardShortcut(WIZARD_PROJECT);
        layout.addNewWizardShortcut(WIZARD_WIDGET_PROJECT);
        layout.addNewWizardShortcut(WIZARD_ACTIVITY);
        layout.addNewWizardShortcut(WIZARD_ACTIVITY_BASED_ON_TEMPLATE);
        layout.addNewWizardShortcut(WIZARD_RECEIVER);
        layout.addNewWizardShortcut(WIZARD_SERVICE);
        layout.addNewWizardShortcut(WIZARD_PROVIDER);
        layout.addNewWizardShortcut(WIZARD_WIDGET_PROVIDER);
        layout.addNewWizardShortcut(WIZARD_JAVA_PACKAGE);
        layout.addNewWizardShortcut(WIZARD_JAVA_CLASS);
        layout.addNewWizardShortcut(WIZARD_JAVA_INTERFACE);
        layout.addNewWizardShortcut(WIZARD_ANDROID_XML);
        layout.addNewWizardShortcut(WIZARD_NEW_FOLDER);

        final IWorkbenchWindow activeWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        IWorkbenchPage activePage = null;
        if (activeWindow != null)
        {
            activePage = activeWindow.getActivePage();
            addPerspectiveListener(activeWindow);
            firePerspectiveInitActions(activePage, layout);
        }

        IPageListener pageListener = new IPageListener()
        {
            public void pageActivated(IWorkbenchPage page)
            {
                firePerspectiveInitActions(page, layout);
                Display disp = PlatformUI.getWorkbench().getDisplay();
                final IPageListener thisListener = this;
                disp.asyncExec(new Runnable()
                {
                    public void run()
                    {
                        if (activeWindow != null)
                        {
                            activeWindow.removePageListener(thisListener);
                        }
                    }
                });
            }

            public void pageClosed(IWorkbenchPage page)
            {
                //do nothing
            }

            public void pageOpened(IWorkbenchPage page)
            {
                //do nothing
            }

        };
        if (activeWindow != null)
        {
            activeWindow.addPageListener(pageListener);
        }

        StudioLogger.debug(MotodevStudioAndroidPerspective.class,
                "MOTODEV Studio Perspective created.");
    }

    /**
     * Creates and adds a perspective listener that will remove the
     * "maximized state" of the Welcome (intro) view before hiding it.
     * 
     * @param activeWindow
     *            Workbench Window where the listener will be added.
     */
    private synchronized static void addPerspectiveListener(IWorkbenchWindow activeWindow)
    {
        if (perspectiveListener == null)
        {
            perspectiveListener = new PerspectiveAdapter()
            {

                @Override
                public void perspectiveChanged(IWorkbenchPage page,
                        IPerspectiveDescriptor perspective, IWorkbenchPartReference partRef,
                        String changeId)
                {
                    if (PERSPECTIVE_ANDROID.equals(perspective.getId()))
                    {
                        if (IIntroConstants.INTRO_VIEW_ID.equals(partRef.getId()))
                        {
                            if (IWorkbenchPage.CHANGE_VIEW_HIDE.equals(changeId))
                            {
                                if (IWorkbenchPage.STATE_MAXIMIZED == page.getPartState(partRef))
                                {
                                    page.toggleZoom(partRef);
                                }
                            }
                        }
                    }
                }

            };
            activeWindow.addPerspectiveListener(perspectiveListener);
        }

    }

    /**
     * 
     * @param activeWindow
     * @param layout
     */
    private void firePerspectiveInitActions(final IWorkbenchPage activePage,
            final IPageLayout layout)
    {
        if (activePage != null)
        {
            // Open MOTODEV Web Resources on a Web Browser Editor
            openStudioInfoOnWebBrowserEditor(activePage);
        }

    }

    /**
     * Opens a web browser with useful information from Studio for Android
     */
    public static void openStudioInfoOnWebBrowserEditor(IWorkbenchPage page)
    {

        URL initialPageURL = getWebResourcesURL();

        if ((initialPageURL != null) && (!Platform.getOS().equals(Platform.OS_LINUX)))
        {
            EclipseUtils.openedWebEditor(page, initialPageURL);
        }
    }

    /**
     * @return
     */
    public static URL getWebResourcesURL()
    {
        URL initialPageURL = null;

        try
        {
            BundleContext context = AndroidPlugin.getDefault().getBundle().getBundleContext();
            String initialPage = context.getProperty(STUDIO_INFO_INITIAL_PAGE_PROPERTY);
            StudioLogger.debug(MotodevStudioAndroidPerspective.class, "Read initial page property:"
                    + STUDIO_INFO_INITIAL_PAGE_PROPERTY + " = " + initialPage);
            if (initialPage != null)
            {
                StudioLogger.debug(MotodevStudioAndroidPerspective.class,
                        "Using the customized URL to be opened in the Web Browser Editor:"
                                + initialPage);
                initialPageURL = new URL(initialPage);
            }
        }
        catch (Exception e)
        {
            StudioLogger.error(
                    MotodevStudioAndroidPerspective.class,
                    "Unable to read customized URL to be opened in the Web Browser Editor..."
                            + e.getMessage());
        }

        if (initialPageURL == null)
        {
            try
            {
                StudioLogger.debug(MotodevStudioAndroidPerspective.class,
                        "Use the default URL to be opened in the Web Browser Editor.");
                URL installDir = Platform.getInstallLocation().getURL();
                initialPageURL = new URL(installDir, STUDIO_INFO_INITIAL_PAGE_FILE);
            }
            catch (Exception e)
            {
                StudioLogger.error(MotodevStudioAndroidPerspective.class,
                        "Unable to show Web Browser Editor with URL: " + e.getMessage());
            }
        }
        return initialPageURL;
    }

}