1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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.ddms.views;
18 
19 import com.android.ddmuilib.ITableFocusListener;
20 import com.android.ddmuilib.ITableFocusListener.IFocusedTableActivator;
21 import com.android.ddmuilib.TablePanel;
22 import com.android.ide.eclipse.ddms.i18n.Messages;
23 
24 import org.eclipse.jface.action.Action;
25 import org.eclipse.swt.dnd.Clipboard;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.ui.IActionBars;
28 import org.eclipse.ui.actions.ActionFactory;
29 
30 /**
31  * Base class for view containing Table that needs to support copy, and select
32  * all.
33  */
34 public abstract class TableView extends SelectionDependentViewPart {
35 
36     /** Activator for the current Table that has the focus */
37     IFocusedTableActivator mActivator = null;
38 
39     private Clipboard mClipboard;
40 
41     private Action mCopyAction;
42     private Action mSelectAllAction;
43 
44     /**
45      * Setup the listener for the Table objects of <code>Panel</code>, and setup
46      * the copy and select all actions.
47      *
48      * @param panel The panel to setup
49      * @param parent The parent composite of the Panel's content.
50      */
setupTableFocusListener(TablePanel panel, Composite parent)51     void setupTableFocusListener(TablePanel panel, Composite parent) {
52         panel.setTableFocusListener(new ITableFocusListener() {
53             @Override
54             public void focusGained(IFocusedTableActivator activator) {
55                 mActivator = activator;
56                 mCopyAction.setEnabled(true);
57                 mSelectAllAction.setEnabled(true);
58             }
59 
60             @Override
61             public void focusLost(IFocusedTableActivator activator) {
62                 if (activator == mActivator) {
63                     mActivator = null;
64                     mCopyAction.setEnabled(false);
65                     mSelectAllAction.setEnabled(false);
66                 }
67             }
68         });
69 
70         // setup the copy action
71         mClipboard = new Clipboard(parent.getDisplay());
72         IActionBars actionBars = getViewSite().getActionBars();
73         actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(),
74                 mCopyAction = new Action(Messages.TableView_Copy) {
75                     @Override
76                     public void run() {
77                         if (mActivator != null) {
78                             mActivator.copy(mClipboard);
79                         }
80                     }
81                 });
82 
83         // setup the select all action
84         actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(),
85                 mSelectAllAction = new Action(Messages.TableView_Select_All) {
86                     @Override
87                     public void run() {
88                         if (mActivator != null) {
89                             mActivator.selectAll();
90                         }
91                     }
92                 });
93 
94     }
95 
96     @Override
dispose()97     public void dispose() {
98         super.dispose();
99         mClipboard.dispose();
100     }
101 }
102