OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / sdk / eclipse / plugins / com.android.ide.eclipse.ddms / src / com / android / ide / eclipse / ddms / views / TableView.java
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.TablePanel;
21 import com.android.ddmuilib.ITableFocusListener.IFocusedTableActivator;
22
23 import org.eclipse.jface.action.Action;
24 import org.eclipse.swt.dnd.Clipboard;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.ui.IActionBars;
27 import org.eclipse.ui.actions.ActionFactory;
28
29 /**
30  * Base class for view containing Table that needs to support copy, and select all.
31  */
32 public abstract class TableView extends SelectionDependentViewPart {
33
34     /** Activator for the current Table that has the focus */
35     IFocusedTableActivator mActivator = null;
36
37     private Clipboard mClipboard;
38
39     private Action mCopyAction;
40     private Action mSelectAllAction;
41
42     /**
43      * Setup the listener for the Table objects of <code>Panel</code>, and setup
44      * the copy and select all actions.
45      * @param panel The panel to setup
46      * @param parent The parent composite of the Panel's content.
47      */
48     void setupTableFocusListener(TablePanel panel, Composite parent) {
49         panel.setTableFocusListener(new ITableFocusListener() {
50             public void focusGained(IFocusedTableActivator activator) {
51                 mActivator = activator;
52                 mCopyAction.setEnabled(true);
53                 mSelectAllAction.setEnabled(true);
54             }
55
56             public void focusLost(IFocusedTableActivator activator) {
57                 if (activator == mActivator) {
58                     mActivator = null;
59                     mCopyAction.setEnabled(false);
60                     mSelectAllAction.setEnabled(false);
61                 }
62             }
63         });
64
65         // setup the copy action
66         mClipboard = new Clipboard(parent.getDisplay());
67         IActionBars actionBars = getViewSite().getActionBars();
68         actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(),
69                 mCopyAction = new Action("Copy") {
70             @Override
71             public void run() {
72                 if (mActivator != null) {
73                     mActivator.copy(mClipboard);
74                 }
75             }
76         });
77
78         // setup the select all action
79         actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(),
80                 mSelectAllAction = new Action("Select All") {
81             @Override
82             public void run() {
83                 if (mActivator != null) {
84                     mActivator.selectAll();
85                 }
86             }
87         });
88
89     }
90
91     @Override
92     public void dispose() {
93         super.dispose();
94         mClipboard.dispose();
95     }
96 }