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