JAL-1641 fixed broken functionalities after refactor
[jalview.git] / src / jalview / gui / CutAndPasteTransfer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.gui;
22
23 import jalview.api.AlignViewportI;
24 import jalview.api.AlignmentViewPanel;
25 import jalview.api.ComplexAlignFile;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.ColumnSelection;
28 import jalview.datamodel.SequenceI;
29 import jalview.io.FileParse;
30 import jalview.io.FormatAdapter;
31 import jalview.io.IdentifyFile;
32 import jalview.io.JalviewFileChooser;
33 import jalview.io.JalviewFileView;
34 import jalview.jbgui.GCutAndPasteTransfer;
35 import jalview.schemes.ColourSchemeI;
36 import jalview.util.MessageManager;
37
38 import java.awt.Toolkit;
39 import java.awt.datatransfer.Clipboard;
40 import java.awt.datatransfer.DataFlavor;
41 import java.awt.datatransfer.StringSelection;
42 import java.awt.datatransfer.Transferable;
43 import java.awt.event.ActionEvent;
44 import java.awt.event.ActionListener;
45 import java.awt.event.MouseEvent;
46
47 import javax.swing.JMenuItem;
48 import javax.swing.JOptionPane;
49 import javax.swing.JPopupMenu;
50 import javax.swing.SwingUtilities;
51
52 /**
53  * Cut'n'paste files into the desktop See JAL-1105
54  * 
55  * @author $author$
56  * @version $Revision$
57  */
58 public class CutAndPasteTransfer extends GCutAndPasteTransfer
59 {
60
61   AlignmentViewPanel alignpanel;
62
63   AlignViewportI viewport;
64
65   FileParse source = null;
66   public CutAndPasteTransfer()
67   {
68     SwingUtilities.invokeLater(new Runnable()
69     {
70       public void run()
71       {
72         textarea.requestFocus();
73       }
74     });
75
76   }
77
78   /**
79    * DOCUMENT ME!
80    */
81   public void setForInput(AlignmentViewPanel viewpanel)
82   {
83     this.alignpanel = viewpanel;
84     if (alignpanel != null)
85     {
86       this.viewport = alignpanel.getAlignViewport();
87     }
88     if (viewport != null)
89     {
90       ok.setText(MessageManager.getString("action.add"));
91     }
92
93     getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
94   }
95
96   /**
97    * DOCUMENT ME!
98    * 
99    * @return DOCUMENT ME!
100    */
101   public String getText()
102   {
103     return textarea.getText();
104   }
105
106   /**
107    * DOCUMENT ME!
108    * 
109    * @param text
110    *          DOCUMENT ME!
111    */
112   public void setText(String text)
113   {
114     textarea.setText(text);
115   }
116
117   public void appendText(String text)
118   {
119     textarea.append(text);
120   }
121
122   public void save_actionPerformed(ActionEvent e)
123   {
124     JalviewFileChooser chooser = new JalviewFileChooser(
125             jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
126
127     chooser.setAcceptAllFileFilterUsed(false);
128     chooser.setFileView(new JalviewFileView());
129     chooser.setDialogTitle(MessageManager.getString("label.save_text_to_file"));
130     chooser.setToolTipText(MessageManager.getString("action.save"));
131
132     int value = chooser.showSaveDialog(this);
133
134     if (value == JalviewFileChooser.APPROVE_OPTION)
135     {
136       try
137       {
138         java.io.PrintWriter out = new java.io.PrintWriter(
139                 new java.io.FileWriter(chooser.getSelectedFile()));
140
141         out.print(getText());
142         out.close();
143       } catch (Exception ex)
144       {
145         ex.printStackTrace();
146       }
147
148     }
149   }
150
151   /**
152    * DOCUMENT ME!
153    * 
154    * @param e
155    *          DOCUMENT ME!
156    */
157   public void copyItem_actionPerformed(ActionEvent e)
158   {
159     textarea.getSelectedText();
160     Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
161     c.setContents(new StringSelection(textarea.getSelectedText()), null);
162   }
163
164   /**
165    * DOCUMENT ME!
166    * 
167    * @param e
168    *          DOCUMENT ME!
169    */
170   public void pasteMenu_actionPerformed(ActionEvent e)
171   {
172     Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
173     Transferable contents = c.getContents(this);
174
175     if (contents == null)
176     {
177       return;
178     }
179
180     try
181     {
182       textarea.append((String) contents
183               .getTransferData(DataFlavor.stringFlavor));
184     } catch (Exception ex)
185     {
186     }
187   }
188
189   /**
190    * DOCUMENT ME!
191    * 
192    * @param e
193    *          DOCUMENT ME!
194    */
195   public void ok_actionPerformed(ActionEvent e)
196   {
197     String format = new IdentifyFile().Identify(getText(), "Paste");
198     // TODO: identify feature, annotation or tree file and parse appropriately.
199     AlignmentI al = null;
200
201     if (FormatAdapter.isValidFormat(format))
202     {
203       try
204       {
205         FormatAdapter fa = new FormatAdapter(alignpanel);
206         al = fa.readFile(getText(), "Paste", format);
207         source = fa.getAlignFile();
208
209       } catch (java.io.IOException ex)
210       {
211         JOptionPane.showInternalMessageDialog(Desktop.desktop,
212                 MessageManager.formatMessage(
213                         "label.couldnt_read_pasted_text", new String[]
214                         { ex.toString() }), MessageManager
215                         .getString("label.error_parsing_text"),
216                 JOptionPane.WARNING_MESSAGE);
217       }
218     }
219
220     if (al != null)
221     {
222       String title = MessageManager.formatMessage(
223               "label.input_cut_paste_params", new String[]
224               { format });
225       if (viewport != null)
226       {
227         ((AlignViewport) viewport).addAlignment(al, title);
228       }
229       else
230       {
231
232         AlignFrame af;
233         if (source instanceof ComplexAlignFile)
234         {
235           ColumnSelection colSel = ((ComplexAlignFile) source)
236                   .getColumnSelection();
237           SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
238                   .getHiddenSequences();
239           boolean showSeqFeatures = ((ComplexAlignFile) source)
240                   .isShowSeqFeatures();
241           ColourSchemeI cs = ((ComplexAlignFile) source).getColourScheme();
242           af = new AlignFrame(al, hiddenSeqs, colSel,
243                   AlignFrame.DEFAULT_WIDTH,
244                   AlignFrame.DEFAULT_HEIGHT);
245
246           af.getViewport().setShowSequenceFeatures(showSeqFeatures);
247           af.changeColour(cs);
248         }
249         else
250         {
251           af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
252                   AlignFrame.DEFAULT_HEIGHT);
253         }
254
255         af.currentFileFormat = format;
256         Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
257                 AlignFrame.DEFAULT_HEIGHT);
258         af.statusBar.setText(MessageManager
259                 .getString("label.successfully_pasted_alignment_file"));
260
261         try
262         {
263           af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
264                   false));
265         } catch (Exception ex)
266         {
267         }
268       }
269     }
270   }
271
272
273   /**
274    * DOCUMENT ME!
275    * 
276    * @param e
277    *          DOCUMENT ME!
278    */
279   public void cancel_actionPerformed(ActionEvent e)
280   {
281     try
282     {
283       this.setClosed(true);
284     } catch (Exception ex)
285     {
286     }
287   }
288
289   public void textarea_mousePressed(MouseEvent e)
290   {
291     if (SwingUtilities.isRightMouseButton(e))
292     {
293       JPopupMenu popup = new JPopupMenu(
294               MessageManager.getString("action.edit"));
295       JMenuItem item = new JMenuItem(
296               MessageManager.getString("action.copy"));
297       item.addActionListener(new ActionListener()
298       {
299         public void actionPerformed(ActionEvent e)
300         {
301           copyItem_actionPerformed(e);
302         }
303       });
304       popup.add(item);
305       item = new JMenuItem(MessageManager.getString("action.paste"));
306       item.addActionListener(new ActionListener()
307       {
308         public void actionPerformed(ActionEvent e)
309         {
310           pasteMenu_actionPerformed(e);
311         }
312       });
313       popup.add(item);
314       popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);
315
316     }
317   }
318
319 }