summaryrefslogtreecommitdiff
path: root/src/plugins/snippets/src/com/motorola/studio/android/codesnippets/AndroidSnippetsStartup.java
blob: e5556546a18e9874cc2f677694c10f2069abd543 (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
/*
* 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.codesnippets;

import org.eclipse.gef.ui.palette.PaletteViewer;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.swt.events.DragDetectEvent;
import org.eclipse.swt.events.DragDetectListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IPerspectiveListener;
import org.eclipse.ui.IStartup;
import org.eclipse.ui.IViewReference;
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.wst.common.snippets.core.ISnippetItem;
import org.eclipse.wst.common.snippets.core.ISnippetsEntry;
import org.eclipse.wst.common.snippets.internal.ui.SnippetsView;

import com.motorola.studio.android.common.utilities.EclipseUtils;

/**
 * This class register listeners in the Snippets View to monitor changes on its
 * selection. This way, MOTODEV Studio for Android displays the snippet preview
 * when appropriate
 * 
 */
@SuppressWarnings("restriction")
public class AndroidSnippetsStartup implements IStartup
{

    public final static String SNIPPETS_VIEW_ID =
            "org.eclipse.wst.common.snippets.internal.ui.SnippetsView";

    private static SnippetsViewContributionItem searchContributionItem;

    private static TooltipDisplayConfigContriutionItem tooltipDisplayConfigcontributionItem;

    /*
     * The tool tip being displayed
     */
    private AndroidSnippetsTooltip tooltip = null;

    private static void addSearchBar(final SnippetsView view)
    {
        if (searchContributionItem == null)
        {
            ToolBarManager tbManager =
                    (ToolBarManager) view.getViewSite().getActionBars().getToolBarManager();

            // the item which searches for words within snippets titles, descriptions and codes
            searchContributionItem = new SnippetsViewContributionItem(view);

            tbManager.add(searchContributionItem);
            tbManager.update(true);
            view.getViewSite().getActionBars().updateActionBars();
        }

        if (tooltipDisplayConfigcontributionItem == null)
        {
            ToolBarManager tbManager =
                    (ToolBarManager) view.getViewSite().getActionBars().getToolBarManager();

            // the item which configures the display of the tool tip
            tooltipDisplayConfigcontributionItem = new TooltipDisplayConfigContriutionItem();

            tbManager.add(tooltipDisplayConfigcontributionItem);
            tbManager.update(true);
            view.getViewSite().getActionBars().updateActionBars();
        }
    }

    /**
     * Add a mouse listener to Snippets View
     * Open the snippet preview tooltip when the user clicks on a
     * snippet item. 
     * Close it when he clicks again in the same item
     * 
     * @see org.eclipse.ui.IStartup#earlyStartup()
     */
    public void earlyStartup()
    {

        /*
         * This is the listener responsible for monitoring mouse clicks
         * on snippet items. When the user clicks on an item, it triggers
         * the action to display the snippet preview
         */
        final MouseListener snippetsMouseListener = new MouseListener()
        {
            /**
             * Do nothing
             * 
             * @see org.eclipse.swt.events.MouseListener#mouseUp(org.eclipse.swt.events.MouseEvent)
             */
            public void mouseUp(MouseEvent e)
            {
                // nothing
            }

            /** 
             * Open/Toogle tooltip
             * 
             * @see org.eclipse.swt.events.MouseListener#mouseDown(org.eclipse.swt.events.MouseEvent)
             */
            public void mouseDown(MouseEvent e)
            {

                // get the Snippets View
                SnippetsView snippetsView =
                        (SnippetsView) EclipseUtils.getActiveView(SNIPPETS_VIEW_ID);

                // get the selected snippet
                ISnippetsEntry snippetEntry = snippetsView.getSelectedEntry();

                // check the tooltip is already being displayed
                // open it if it's not and its config allows so, close it otherwise
                if ((snippetEntry instanceof ISnippetItem)
                        && ((tooltip == null) || (!tooltip.getItem().equals(snippetEntry)))
                        && tooltipDisplayConfigcontributionItem.isTooltipDisplayed())
                {
                    Control snippetsControl = snippetsView.getViewer().getControl();
                    tooltip =
                            new AndroidSnippetsTooltip((ISnippetItem) snippetEntry, snippetsControl);
                    tooltip.setPopupDelay(250);
                    tooltip.show(new Point(snippetsControl.getBounds().width, 0));

                }
                else
                {
                    if (tooltip != null)
                    {
                        tooltip.hide();
                    }
                    tooltip = null;
                }
            }

            /**
             * Hide tooltip
             * 
             * @see org.eclipse.swt.events.MouseListener#mouseDoubleClick(org.eclipse.swt.events.MouseEvent)
             */
            public void mouseDoubleClick(MouseEvent e)
            {
                if (tooltip != null)
                {
                    tooltip.hide();
                }
                tooltip = null;
            }
        };

        /**
         * this listener is called when the mouse is dragged. It simply
         * hides the tool tip when this action occurs.
         */
        final DragDetectListener snippetsMouseDragListener = new DragDetectListener()
        {

            /**
             * Hide the tool tip.
             */
            public void dragDetected(DragDetectEvent e)
            {
                // simply hide the tool tip
                if (tooltip != null)
                {
                    tooltip.hide();
                }
            }
        };

        /*
         * This is the listener that is attached to the workspace and monitor
         * perspectives activation, as well as changes in the views being displayed
         * (for example: view opened/closed). It intends to attach the snippetsMouseListener
         * listener declared above in the Snippets View being used by Eclipse
         */
        final IPerspectiveListener perspectiveListener = new PerspectiveAdapter()
        {
            // Set if it has already been executed
            // If so, it doesn't need to be executed again
            boolean executed = false;

            /**
             * This action is called when the user goes to another perspective (it's not called for the first perspective
             * displayed when he opens Eclipse)
             * 
             * @see org.eclipse.ui.PerspectiveAdapter#perspectiveActivated(org.eclipse.ui.IWorkbenchPage, org.eclipse.ui.IPerspectiveDescriptor)
             */
            @Override
            public void perspectiveActivated(IWorkbenchPage page, IPerspectiveDescriptor perspective)
            {
                IViewReference[] viewReferences = page.getViewReferences();
                for (IViewReference viewReference : viewReferences)
                {
                    if (SNIPPETS_VIEW_ID.equals(viewReference.getId()))
                    {
                        SnippetsView snippetsView = (SnippetsView) viewReference.getView(true);
                        if (snippetsView != null)
                        {
                            addSearchBar(snippetsView);
                            PaletteViewer palleteViewer = snippetsView.getViewer();
                            if (palleteViewer != null)
                            {
                                Control control = palleteViewer.getControl();
                                if (control != null)
                                {
                                    control.removeMouseListener(snippetsMouseListener); // remove mouse listener just to ensure we never have two listeners registered
                                    control.removeDragDetectListener(snippetsMouseDragListener); // remove mouse draggable listener just to ensure we never have two listeners registered
                                    control.addMouseListener(snippetsMouseListener);
                                    control.addDragDetectListener(snippetsMouseDragListener);
                                    executed = true;
                                }
                            }
                        }
                    }
                }
            }

            /**
             * This is called when something in the perspective have changed.
             * For example: when a view is opened or closed
             * 
             * @see org.eclipse.ui.PerspectiveAdapter#perspectiveChanged(org.eclipse.ui.IWorkbenchPage, org.eclipse.ui.IPerspectiveDescriptor, org.eclipse.ui.IWorkbenchPartReference, java.lang.String)
             */
            @Override
            public void perspectiveChanged(IWorkbenchPage page, IPerspectiveDescriptor perspective,
                    IWorkbenchPartReference partRef, String changeId)
            {
                // check if it's the Snippet View
                if (SNIPPETS_VIEW_ID.equals(partRef.getId()))
                {
                    // if it's the Snippet View and it's being OPENED
                    if (IWorkbenchPage.CHANGE_VIEW_SHOW.equals(changeId))
                    {
                        if (!executed)
                        {
                            SnippetsView snippetsView = (SnippetsView) partRef.getPart(false);
                            addSearchBar(snippetsView);
                            snippetsView.getViewer().getControl()
                                    .removeMouseListener(snippetsMouseListener); // remove mouse listener just to ensure we never have two listeners registered
                            snippetsView.getViewer().getControl()
                                    .removeDragDetectListener(snippetsMouseDragListener); // remove mouse draggable listener just to ensure we never have two liseteners registered
                            snippetsView.getViewer().getControl()
                                    .addMouseListener(snippetsMouseListener);
                            snippetsView.getViewer().getControl()
                                    .addDragDetectListener(snippetsMouseDragListener);
                            // it doesn't need to add the mouse listener to the Snippets View in further opportunities
                            executed = true;
                        }
                    }
                    // if it's the Snippet View and it's being CLOSED
                    else if (IWorkbenchPage.CHANGE_VIEW_HIDE.equals(changeId))
                    {
                        // it must add the mouse listener to the Snippets View again next time the view is opened
                        if (searchContributionItem != null)
                        {
                            searchContributionItem.clean();
                            searchContributionItem.getParent().remove(searchContributionItem);
                        }
                        
                        if (tooltipDisplayConfigcontributionItem != null) {
                            tooltipDisplayConfigcontributionItem.getParent().remove(tooltipDisplayConfigcontributionItem);
                        }
                        
                        searchContributionItem = null;
                        tooltipDisplayConfigcontributionItem = null;

                        executed = false;
                    }
                }

            }

        };

        /*
         * Attach the perspectiveListener declared above into the active window
         * Also try to add the snippetsMouseListener listener to the Snippet View, 
         * if it's already being displayed
         */
        PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable()
        {

            public void run()
            {
                /*
                 * Add mouse listener to Snippet View
                 */
                final IWorkbenchWindow activeWindow =
                        PlatformUI.getWorkbench().getActiveWorkbenchWindow();

                activeWindow.addPerspectiveListener(perspectiveListener);

                IViewReference viewReference =
                        activeWindow.getActivePage().findViewReference(SNIPPETS_VIEW_ID);

                if (viewReference != null)
                {
                    final SnippetsView snippetsView = (SnippetsView) viewReference.getView(true);
                    if (snippetsView != null)
                    {
                        addSearchBar(snippetsView);
                        snippetsView.getViewer().getControl()
                                .addMouseListener(snippetsMouseListener);
                        snippetsView.getViewer().getControl()
                                .addDragDetectListener(snippetsMouseDragListener);
                    }

                }

            }

        });

    }
}