View Javadoc

1   /*
2    * Copyright (c) 2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.aequo.text.action;
14  
15  import java.awt.Toolkit;
16  import java.awt.datatransfer.Clipboard;
17  import java.awt.datatransfer.DataFlavor;
18  import java.awt.datatransfer.Transferable;
19  import java.awt.datatransfer.UnsupportedFlavorException;
20  import java.awt.event.ActionEvent;
21  import java.awt.event.KeyEvent;
22  import java.io.IOException;
23  
24  import javax.swing.Action;
25  import javax.swing.KeyStroke;
26  import javax.swing.ListSelectionModel;
27  
28  import org.abstracthorizon.aequo.CompareEntry;
29  import org.abstracthorizon.aequo.text.TextModel;
30  
31  /**
32   * Copy to clipboard action.
33   *
34   * @author Daniel Sendula
35   */
36  public class CopyAction extends TextBaseAction {
37  
38      /**
39       * Constructor
40       */
41      public CopyAction() {
42          setName("Copy");
43          setMessage("Copy");
44          setDescription("Copy");
45          putValue(Action.MNEMONIC_KEY, KeyEvent.VK_C);
46          putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
47      }
48  
49      /**
50       * Returns if selection is valid
51       * @return <code>true</code> if selection is valid<
52       */
53      public boolean isEnabled() {
54          return (column >= 0) && isSelectionValid();
55      }
56  
57      /**
58       * Invokes action
59       * @param e action event
60       */
61      public void perform(ActionEvent e) {
62  
63          TextModel model = getTextModel();
64          
65          StringBuffer text = new StringBuffer();
66          ListSelectionModel selModel = model.getSelectionModel();
67          int min = selModel.getMinSelectionIndex();
68          int max = selModel.getMaxSelectionIndex();
69          for (int i = min; i < max; i++) {
70              CompareEntry<String> entry = model.get(i);
71              text.append(entry.getData(column)).append('\n');
72          }
73  
74          final String copyText = text.toString();
75          Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
76          clipboard.setContents(new Transferable() {
77  
78              public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
79                  if (!isDataFlavorSupported(flavor)) {
80                      throw new UnsupportedFlavorException(flavor);
81                  }
82                  return copyText.toString();
83              }
84  
85              public DataFlavor[] getTransferDataFlavors() {
86                  return new DataFlavor[]{DataFlavor.stringFlavor};
87              }
88  
89              public boolean isDataFlavorSupported(DataFlavor flavor) {
90                  return DataFlavor.stringFlavor.equals(flavor);
91              }
92  
93          }, null);
94      }
95  }