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   * Cut to clipboard action.
33   *
34   * @author Daniel Sendula
35   */
36  public class CutAction extends TextBaseAction {
37  
38      /** Delete action to be invoked after moving lines to clipboard */
39      protected DeleteAction deleteAction;
40  
41      /**
42       * Constructor
43       */
44      public CutAction() {
45          setName("Cut");
46          setMessage("Cut");
47          setDescription("Cut");
48          putValue(Action.MNEMONIC_KEY, KeyEvent.VK_C);
49          putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
50          
51          deleteAction = new DeleteAction();
52      }
53  
54      /**
55       * Returns if selection is valid
56       * @return <code>true</code> if selection is valid<
57       */
58      public boolean isEnabled() {
59          return (column >= 0) && isSelectionValid();
60      }
61  
62      /**
63       * Sets text model
64       * @param textModel text model
65       */
66      public void setTextModel(TextModel textModel) {
67          super.setTextModel(textModel);
68          deleteAction.setTextModel(textModel);
69      }
70      
71      /**
72       * Inovkes the action
73       * @param e action event
74       */
75      public void perform(ActionEvent e) {
76  
77          TextModel model = getTextModel();
78          
79          StringBuffer text = new StringBuffer();
80          ListSelectionModel selModel = model.getSelectionModel();
81          int min = selModel.getMinSelectionIndex();
82          int max = selModel.getMaxSelectionIndex();
83          for (int i = min; i < max; i++) {
84              CompareEntry<String> entry = model.get(i);
85              text.append(entry.getData(column)).append('\n');
86          }
87  
88          final String copyText = text.toString();
89  
90          Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
91          clipboard.setContents(new Transferable() {
92  
93              public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
94                  if (!isDataFlavorSupported(flavor)) {
95                      throw new UnsupportedFlavorException(flavor);
96                  }
97                  return copyText.toString();
98              }
99  
100             public DataFlavor[] getTransferDataFlavors() {
101                 return new DataFlavor[]{DataFlavor.stringFlavor};
102             }
103 
104             public boolean isDataFlavorSupported(DataFlavor flavor) {
105                 return DataFlavor.stringFlavor.equals(flavor);
106             }
107 
108         }, null);
109         deleteAction.actionPerformed(e);
110     }
111 }