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 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 */ 51 void setupTableFocusListener(TablePanel panel, Composite parent) { 52 panel.setTableFocusListener(new ITableFocusListener() { 53 public void focusGained(IFocusedTableActivator activator) { 54 mActivator = activator; 55 mCopyAction.setEnabled(true); 56 mSelectAllAction.setEnabled(true); 57 } 58 59 public void focusLost(IFocusedTableActivator activator) { 60 if (activator == mActivator) { 61 mActivator = null; 62 mCopyAction.setEnabled(false); 63 mSelectAllAction.setEnabled(false); 64 } 65 } 66 }); 67 68 // setup the copy action 69 mClipboard = new Clipboard(parent.getDisplay()); 70 IActionBars actionBars = getViewSite().getActionBars(); 71 actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), 72 mCopyAction = new Action(Messages.TableView_Copy) { 73 @Override 74 public void run() { 75 if (mActivator != null) { 76 mActivator.copy(mClipboard); 77 } 78 } 79 }); 80 81 // setup the select all action 82 actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), 83 mSelectAllAction = new Action(Messages.TableView_Select_All) { 84 @Override 85 public void run() { 86 if (mActivator != null) { 87 mActivator.selectAll(); 88 } 89 } 90 }); 91 92 } 93 94 @Override 95 public void dispose() { 96 super.dispose(); 97 mClipboard.dispose(); 98 } 99 } 100