6ccc5ff6a0d04e588162418e0b42ca6afc5122ce
[jalview.git] / src / jalview / gui / CutAndPasteTransfer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.gui;
20
21 import java.awt.*;
22 import java.awt.datatransfer.*;
23 import java.awt.event.*;
24 import javax.swing.*;
25
26 import jalview.datamodel.*;
27 import jalview.io.*;
28 import jalview.jbgui.*;
29 import jalview.util.MessageManager;
30
31 /**
32  * Cut'n'paste files into the desktop See JAL-1105
33  * 
34  * @author $author$
35  * @version $Revision$
36  */
37 public class CutAndPasteTransfer extends GCutAndPasteTransfer
38 {
39
40   AlignViewport viewport;
41
42   public CutAndPasteTransfer()
43   {
44     SwingUtilities.invokeLater(new Runnable()
45     {
46       public void run()
47       {
48         textarea.requestFocus();
49       }
50     });
51
52   }
53
54   /**
55    * DOCUMENT ME!
56    */
57   public void setForInput(AlignViewport viewport)
58   {
59     this.viewport = viewport;
60     if (viewport != null)
61     {
62       ok.setText(MessageManager.getString("action.add"));
63     }
64
65     getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
66   }
67
68   /**
69    * DOCUMENT ME!
70    * 
71    * @return DOCUMENT ME!
72    */
73   public String getText()
74   {
75     return textarea.getText();
76   }
77
78   /**
79    * DOCUMENT ME!
80    * 
81    * @param text
82    *          DOCUMENT ME!
83    */
84   public void setText(String text)
85   {
86     textarea.setText(text);
87   }
88
89   public void appendText(String text)
90   {
91     textarea.append(text);
92   }
93
94   public void save_actionPerformed(ActionEvent e)
95   {
96     JalviewFileChooser chooser = new JalviewFileChooser(
97             jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
98
99     chooser.setAcceptAllFileFilterUsed(false);
100     chooser.setFileView(new JalviewFileView());
101     chooser.setDialogTitle("Save Text to File");
102     chooser.setToolTipText(MessageManager.getString("action.save"));
103
104     int value = chooser.showSaveDialog(this);
105
106     if (value == JalviewFileChooser.APPROVE_OPTION)
107     {
108       try
109       {
110         java.io.PrintWriter out = new java.io.PrintWriter(
111                 new java.io.FileWriter(chooser.getSelectedFile()));
112
113         out.print(getText());
114         out.close();
115       } catch (Exception ex)
116       {
117         ex.printStackTrace();
118       }
119
120     }
121   }
122
123   /**
124    * DOCUMENT ME!
125    * 
126    * @param e
127    *          DOCUMENT ME!
128    */
129   public void copyItem_actionPerformed(ActionEvent e)
130   {
131     textarea.getSelectedText();
132     Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
133     c.setContents(new StringSelection(textarea.getSelectedText()), null);
134   }
135
136   /**
137    * DOCUMENT ME!
138    * 
139    * @param e
140    *          DOCUMENT ME!
141    */
142   public void pasteMenu_actionPerformed(ActionEvent e)
143   {
144     Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
145     Transferable contents = c.getContents(this);
146
147     if (contents == null)
148     {
149       return;
150     }
151
152     try
153     {
154       textarea.append((String) contents
155               .getTransferData(DataFlavor.stringFlavor));
156     } catch (Exception ex)
157     {
158     }
159   }
160
161   /**
162    * DOCUMENT ME!
163    * 
164    * @param e
165    *          DOCUMENT ME!
166    */
167   public void ok_actionPerformed(ActionEvent e)
168   {
169     String format = new IdentifyFile().Identify(getText(), "Paste");
170     // TODO: identify feature, annotation or tree file and parse appropriately.
171     Alignment al = null;
172
173     if (FormatAdapter.isValidFormat(format))
174     {
175       try
176       {
177         al = new FormatAdapter().readFile(getText(), "Paste", format);
178       } catch (java.io.IOException ex)
179       {
180         JOptionPane.showInternalMessageDialog(Desktop.desktop,
181                         MessageManager.formatMessage("label.couldnt_read_pasted_text", new String[]{ex.toString()}),
182                 MessageManager.getString("label.error_parsing_text"), JOptionPane.WARNING_MESSAGE);
183       }
184     }
185
186     if (al != null)
187     {
188       if (viewport != null)
189       {
190         for (int i = 0; i < al.getHeight(); i++)
191         {
192           viewport.getAlignment().addSequence(al.getSequenceAt(i));
193         }
194
195         viewport.firePropertyChange("alignment", null, viewport
196                 .getAlignment().getSequences());
197       }
198       else
199       {
200         AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
201                 AlignFrame.DEFAULT_HEIGHT);
202         af.currentFileFormat = format;
203         Desktop.addInternalFrame(af, MessageManager.formatMessage("label.input_cut_paste_params", new String[]{format}),
204                 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
205         af.statusBar.setText(MessageManager.getString("label.successfully_pasted_alignment_file"));
206
207         try
208         {
209           af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
210                   false));
211         } catch (Exception ex)
212         {
213         }
214       }
215     }
216   }
217
218   /**
219    * DOCUMENT ME!
220    * 
221    * @param e
222    *          DOCUMENT ME!
223    */
224   public void cancel_actionPerformed(ActionEvent e)
225   {
226     try
227     {
228       this.setClosed(true);
229     } catch (Exception ex)
230     {
231     }
232   }
233
234   public void textarea_mousePressed(MouseEvent e)
235   {
236     if (SwingUtilities.isRightMouseButton(e))
237     {
238       JPopupMenu popup = new JPopupMenu(MessageManager.getString("action.edit"));
239       JMenuItem item = new JMenuItem(MessageManager.getString("action.copy"));
240       item.addActionListener(new ActionListener()
241       {
242         public void actionPerformed(ActionEvent e)
243         {
244           copyItem_actionPerformed(e);
245         }
246       });
247       popup.add(item);
248       item = new JMenuItem(MessageManager.getString("action.paste"));
249       item.addActionListener(new ActionListener()
250       {
251         public void actionPerformed(ActionEvent e)
252         {
253           pasteMenu_actionPerformed(e);
254         }
255       });
256       popup.add(item);
257       popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);
258
259     }
260   }
261
262 }