JAL-2326 updated references of JOptionPane to JvOptionPane
[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.api.FeatureSettingsModelI;
27 import jalview.api.FeaturesDisplayedI;
28 import jalview.api.FeaturesSourceI;
29 import jalview.bin.Jalview;
30 import jalview.datamodel.AlignmentI;
31 import jalview.datamodel.ColumnSelection;
32 import jalview.datamodel.SequenceI;
33 import jalview.io.AppletFormatAdapter;
34 import jalview.io.FileParse;
35 import jalview.io.FormatAdapter;
36 import jalview.io.IdentifyFile;
37 import jalview.io.JalviewFileChooser;
38 import jalview.io.JalviewFileView;
39 import jalview.jbgui.GCutAndPasteTransfer;
40 import jalview.json.binding.biojson.v1.ColourSchemeMapper;
41 import jalview.schemes.ColourSchemeI;
42 import jalview.util.MessageManager;
43
44 import java.awt.Toolkit;
45 import java.awt.datatransfer.Clipboard;
46 import java.awt.datatransfer.DataFlavor;
47 import java.awt.datatransfer.StringSelection;
48 import java.awt.datatransfer.Transferable;
49 import java.awt.event.ActionEvent;
50 import java.awt.event.ActionListener;
51 import java.awt.event.MouseEvent;
52
53 import javax.swing.JMenuItem;
54 import javax.swing.JPopupMenu;
55 import javax.swing.SwingUtilities;
56
57 /**
58  * Cut'n'paste files into the desktop See JAL-1105
59  * 
60  * @author $author$
61  * @version $Revision$
62  */
63 public class CutAndPasteTransfer extends GCutAndPasteTransfer
64 {
65
66   AlignmentViewPanel alignpanel;
67
68   AlignViewportI viewport;
69
70   FileParse source = null;
71
72   public CutAndPasteTransfer()
73   {
74     SwingUtilities.invokeLater(new Runnable()
75     {
76       @Override
77       public void run()
78       {
79         textarea.requestFocus();
80       }
81     });
82
83   }
84
85   /**
86    * DOCUMENT ME!
87    */
88   public void setForInput(AlignmentViewPanel viewpanel)
89   {
90     this.alignpanel = viewpanel;
91     if (alignpanel != null)
92     {
93       this.viewport = alignpanel.getAlignViewport();
94     }
95     if (viewport != null)
96     {
97       ok.setText(MessageManager.getString("action.add"));
98     }
99
100     getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
101   }
102
103   /**
104    * DOCUMENT ME!
105    * 
106    * @return DOCUMENT ME!
107    */
108   public String getText()
109   {
110     return textarea.getText();
111   }
112
113   /**
114    * DOCUMENT ME!
115    * 
116    * @param text
117    *          DOCUMENT ME!
118    */
119   public void setText(String text)
120   {
121     textarea.setText(text);
122   }
123
124   public void appendText(String text)
125   {
126     textarea.append(text);
127   }
128
129   @Override
130   public void save_actionPerformed(ActionEvent e)
131   {
132     JalviewFileChooser chooser = new JalviewFileChooser(
133             jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
134
135     chooser.setAcceptAllFileFilterUsed(false);
136     chooser.setFileView(new JalviewFileView());
137     chooser.setDialogTitle(MessageManager
138             .getString("label.save_text_to_file"));
139     chooser.setToolTipText(MessageManager.getString("action.save"));
140
141     int value = chooser.showSaveDialog(this);
142
143     if (value == JalviewFileChooser.APPROVE_OPTION)
144     {
145       try
146       {
147         java.io.PrintWriter out = new java.io.PrintWriter(
148                 new java.io.FileWriter(chooser.getSelectedFile()));
149
150         out.print(getText());
151         out.close();
152       } catch (Exception ex)
153       {
154         ex.printStackTrace();
155       }
156
157     }
158   }
159
160   /**
161    * DOCUMENT ME!
162    * 
163    * @param e
164    *          DOCUMENT ME!
165    */
166   @Override
167   public void copyItem_actionPerformed(ActionEvent e)
168   {
169     textarea.getSelectedText();
170     Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
171     c.setContents(new StringSelection(textarea.getSelectedText()), null);
172   }
173
174   /**
175    * DOCUMENT ME!
176    * 
177    * @param e
178    *          DOCUMENT ME!
179    */
180   @Override
181   public void pasteMenu_actionPerformed(ActionEvent e)
182   {
183     Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
184     Transferable contents = c.getContents(this);
185
186     if (contents == null)
187     {
188       return;
189     }
190
191     try
192     {
193       textarea.append((String) contents
194               .getTransferData(DataFlavor.stringFlavor));
195     } catch (Exception ex)
196     {
197     }
198   }
199
200   /**
201    * DOCUMENT ME!
202    * 
203    * @param e
204    *          DOCUMENT ME!
205    */
206   @Override
207   public void ok_actionPerformed(ActionEvent e)
208   {
209     String text = getText();
210     if (text.trim().length() < 1)
211     {
212       return;
213     }
214
215     String format = new IdentifyFile().identify(text, "Paste");
216     if (format == null || format.equalsIgnoreCase("EMPTY DATA FILE"))
217     {
218       System.err.println(MessageManager
219               .getString("label.couldnt_read_data"));
220       if (!Jalview.isHeadlessMode())
221       {
222         JvOptionPane.showInternalMessageDialog(Desktop.desktop,
223                 AppletFormatAdapter.SUPPORTED_FORMATS,
224                 MessageManager.getString("label.couldnt_read_data"),
225                 JvOptionPane.WARNING_MESSAGE);
226       }
227       return;
228     }
229
230     // TODO: identify feature, annotation or tree file and parse appropriately.
231     AlignmentI al = null;
232
233     if (FormatAdapter.isValidFormat(format))
234     {
235       try
236       {
237         FormatAdapter fa = new FormatAdapter(alignpanel);
238         al = fa.readFile(getText(), "Paste", format);
239         source = fa.getAlignFile();
240
241       } catch (java.io.IOException ex)
242       {
243         JvOptionPane.showInternalMessageDialog(Desktop.desktop,
244                 MessageManager.formatMessage(
245                         "label.couldnt_read_pasted_text",
246                         new String[] { ex.toString() }), MessageManager
247                         .getString("label.error_parsing_text"),
248                 JvOptionPane.WARNING_MESSAGE);
249       }
250     }
251
252     if (al != null && al.hasValidSequence())
253     {
254       String title = MessageManager.formatMessage(
255               "label.input_cut_paste_params", new String[] { format });
256       FeatureSettingsModelI proxyColourScheme = source
257               .getFeatureColourScheme();
258
259       /*
260        * if the view panel was closed its alignment is nulled
261        * and this is an orphaned cut and paste window
262        */
263       if (viewport != null && viewport.getAlignment() != null)
264       {
265         if (proxyColourScheme != null)
266         {
267           viewport.applyFeaturesStyle(proxyColourScheme);
268         }
269         ((AlignViewport) viewport).addAlignment(al, title);
270       }
271       else
272       {
273
274         AlignFrame af;
275         if (source instanceof ComplexAlignFile)
276         {
277           ColumnSelection colSel = ((ComplexAlignFile) source)
278                   .getColumnSelection();
279           SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
280                   .getHiddenSequences();
281           boolean showSeqFeatures = ((ComplexAlignFile) source)
282                   .isShowSeqFeatures();
283           String colourSchemeName = ((ComplexAlignFile) source)
284                   .getGlobalColourScheme();
285           FeaturesDisplayedI fd = ((ComplexAlignFile) source)
286                   .getDisplayedFeatures();
287           af = new AlignFrame(al, hiddenSeqs, colSel,
288                   AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
289           af.getViewport().setShowSequenceFeatures(showSeqFeatures);
290           af.getViewport().setFeaturesDisplayed(fd);
291           ColourSchemeI cs = ColourSchemeMapper.getJalviewColourScheme(
292                   colourSchemeName, al);
293           if (cs != null)
294           {
295             af.changeColour(cs);
296           }
297         }
298         else
299         {
300           af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
301                   AlignFrame.DEFAULT_HEIGHT);
302           if (source instanceof FeaturesSourceI)
303           {
304             af.getViewport().setShowSequenceFeatures(true);
305           }
306         }
307         if (proxyColourScheme != null)
308         {
309           af.getViewport().applyFeaturesStyle(proxyColourScheme);
310         }
311         af.currentFileFormat = format;
312         Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
313                 AlignFrame.DEFAULT_HEIGHT);
314         af.statusBar.setText(MessageManager
315                 .getString("label.successfully_pasted_alignment_file"));
316
317         try
318         {
319           af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
320                   false));
321         } catch (Exception ex)
322         {
323         }
324       }
325     }
326     else
327     {
328       System.err.println(MessageManager
329               .getString("label.couldnt_read_data"));
330       if (!Jalview.isHeadlessMode())
331       {
332         JvOptionPane.showInternalMessageDialog(Desktop.desktop,
333                 AppletFormatAdapter.SUPPORTED_FORMATS,
334                 MessageManager.getString("label.couldnt_read_data"),
335                 JvOptionPane.WARNING_MESSAGE);
336       }
337     }
338   }
339
340   /**
341    * DOCUMENT ME!
342    * 
343    * @param e
344    *          DOCUMENT ME!
345    */
346   @Override
347   public void cancel_actionPerformed(ActionEvent e)
348   {
349     try
350     {
351       this.setClosed(true);
352     } catch (Exception ex)
353     {
354     }
355   }
356
357   @Override
358   public void textarea_mousePressed(MouseEvent e)
359   {
360     /*
361      * isPopupTrigger is checked in mousePressed on Mac,
362      * in mouseReleased on Windows
363      */
364     if (e.isPopupTrigger())
365     {
366       JPopupMenu popup = new JPopupMenu(
367               MessageManager.getString("action.edit"));
368       JMenuItem item = new JMenuItem(
369               MessageManager.getString("action.copy"));
370       item.addActionListener(new ActionListener()
371       {
372         @Override
373         public void actionPerformed(ActionEvent e)
374         {
375           copyItem_actionPerformed(e);
376         }
377       });
378       popup.add(item);
379       item = new JMenuItem(MessageManager.getString("action.paste"));
380       item.addActionListener(new ActionListener()
381       {
382         @Override
383         public void actionPerformed(ActionEvent e)
384         {
385           pasteMenu_actionPerformed(e);
386         }
387       });
388       popup.add(item);
389       popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);
390
391     }
392   }
393
394 }