Home | History | Annotate | Download | only in ddmuilib
      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      */
     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      */
     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      */
     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             public void copy(Clipboard clipboard) {
     72                 int[] selection = table.getSelectionIndices();
     73 
     74                 // we need to sort the items to be sure.
     75                 Arrays.sort(selection);
     76 
     77                 // all lines must be concatenated.
     78                 StringBuilder sb = new StringBuilder();
     79 
     80                 // loop on the selection and output the file.
     81                 for (int i : selection) {
     82                     TableItem item = table.getItem(i);
     83                     for (int c = colStart ; c <= colEnd ; c++) {
     84                         sb.append(item.getText(c));
     85                         sb.append('\t');
     86                     }
     87                     sb.append('\n');
     88                 }
     89 
     90                 // now add that to the clipboard if the string has content
     91                 String data = sb.toString();
     92                 if (data != null && data.length() > 0) {
     93                     clipboard.setContents(
     94                             new Object[] { data },
     95                             new Transfer[] { TextTransfer.getInstance() });
     96                 }
     97             }
     98 
     99             public void selectAll() {
    100                 table.selectAll();
    101             }
    102         };
    103 
    104         // add the focus listener on the table to notify the global listener
    105         table.addFocusListener(new FocusListener() {
    106             public void focusGained(FocusEvent e) {
    107                 mGlobalListener.focusGained(activator);
    108             }
    109 
    110             public void focusLost(FocusEvent e) {
    111                 mGlobalListener.focusLost(activator);
    112             }
    113         });
    114     }
    115 
    116     /**
    117      * Sets up a Table object to notify the global Table Focus listener when it
    118      * gets or loses the focus.<br>
    119      * When the copy method is invoked, all columns are put in the clipboard, separated
    120      * by tabs
    121      *
    122      * @param table the Table object.
    123      */
    124     protected final void addTableToFocusListener(final Table table) {
    125         addTableToFocusListener(table, 0, table.getColumnCount()-1);
    126     }
    127 
    128 }
    129