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.ddmuilib; 18 19 import com.android.ddmuilib.ITableFocusListener.IFocusedTableActivator; 20 21 import org.eclipse.swt.dnd.Clipboard; 22 import org.eclipse.swt.dnd.TextTransfer; 23 import org.eclipse.swt.dnd.Transfer; 24 import org.eclipse.swt.events.FocusEvent; 25 import org.eclipse.swt.events.FocusListener; 26 import org.eclipse.swt.widgets.Table; 27 import org.eclipse.swt.widgets.TableItem; 28 29 import java.util.Arrays; 30 31 /** 32 * Base class for panel containing Table that need to support copy-paste-selectAll 33 */ 34 public abstract class TablePanel extends ClientDisplayPanel { 35 private ITableFocusListener mGlobalListener; 36 37 /** 38 * Sets a TableFocusListener which will be notified when one of the tables 39 * gets or loses focus. 40 * 41 * @param listener 42 */ setTableFocusListener(ITableFocusListener listener)43 public void setTableFocusListener(ITableFocusListener listener) { 44 // record the global listener, to make sure table created after 45 // this call will still be setup. 46 mGlobalListener = listener; 47 48 setTableFocusListener(); 49 } 50 51 /** 52 * Sets up the Table of object of the panel to work with the global listener.<br> 53 * Default implementation does nothing. 54 */ setTableFocusListener()55 protected void setTableFocusListener() { 56 57 } 58 59 /** 60 * Sets up a Table object to notify the global Table Focus listener when it 61 * gets or loses the focus. 62 * 63 * @param table the Table object. 64 * @param colStart 65 * @param colEnd 66 */ addTableToFocusListener(final Table table, final int colStart, final int colEnd)67 protected final void addTableToFocusListener(final Table table, 68 final int colStart, final int colEnd) { 69 // create the activator for this table 70 final IFocusedTableActivator activator = new IFocusedTableActivator() { 71 @Override 72 public void copy(Clipboard clipboard) { 73 int[] selection = table.getSelectionIndices(); 74 75 // we need to sort the items to be sure. 76 Arrays.sort(selection); 77 78 // all lines must be concatenated. 79 StringBuilder sb = new StringBuilder(); 80 81 // loop on the selection and output the file. 82 for (int i : selection) { 83 TableItem item = table.getItem(i); 84 for (int c = colStart ; c <= colEnd ; c++) { 85 sb.append(item.getText(c)); 86 sb.append('\t'); 87 } 88 sb.append('\n'); 89 } 90 91 // now add that to the clipboard if the string has content 92 String data = sb.toString(); 93 if (data != null && data.length() > 0) { 94 clipboard.setContents( 95 new Object[] { data }, 96 new Transfer[] { TextTransfer.getInstance() }); 97 } 98 } 99 100 @Override 101 public void selectAll() { 102 table.selectAll(); 103 } 104 }; 105 106 // add the focus listener on the table to notify the global listener 107 table.addFocusListener(new FocusListener() { 108 @Override 109 public void focusGained(FocusEvent e) { 110 mGlobalListener.focusGained(activator); 111 } 112 113 @Override 114 public void focusLost(FocusEvent e) { 115 mGlobalListener.focusLost(activator); 116 } 117 }); 118 } 119 120 /** 121 * Sets up a Table object to notify the global Table Focus listener when it 122 * gets or loses the focus.<br> 123 * When the copy method is invoked, all columns are put in the clipboard, separated 124 * by tabs 125 * 126 * @param table the Table object. 127 */ addTableToFocusListener(final Table table)128 protected final void addTableToFocusListener(final Table table) { 129 addTableToFocusListener(table, 0, table.getColumnCount()-1); 130 } 131 132 } 133