JAL-653 set showSequenceFeatures=true on loading a FeaturesSourceI
[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.FeaturesDisplayedI;
27 import jalview.api.FeaturesSourceI;
28 import jalview.bin.Jalview;
29 import jalview.datamodel.AlignmentI;
30 import jalview.datamodel.ColumnSelection;
31 import jalview.datamodel.SequenceI;
32 import jalview.io.AppletFormatAdapter;
33 import jalview.io.FileParse;
34 import jalview.io.FormatAdapter;
35 import jalview.io.IdentifyFile;
36 import jalview.io.JalviewFileChooser;
37 import jalview.io.JalviewFileView;
38 import jalview.jbgui.GCutAndPasteTransfer;
39 import jalview.json.binding.biojson.v1.ColourSchemeMapper;
40 import jalview.schemes.ColourSchemeI;
41 import jalview.util.MessageManager;
42
43 import java.awt.Toolkit;
44 import java.awt.datatransfer.Clipboard;
45 import java.awt.datatransfer.DataFlavor;
46 import java.awt.datatransfer.StringSelection;
47 import java.awt.datatransfer.Transferable;
48 import java.awt.event.ActionEvent;
49 import java.awt.event.ActionListener;
50 import java.awt.event.MouseEvent;
51
52 import javax.swing.JMenuItem;
53 import javax.swing.JOptionPane;
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         javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,
223                 AppletFormatAdapter.SUPPORTED_FORMATS,
224                 MessageManager.getString("label.couldnt_read_data"),
225                 JOptionPane.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         JOptionPane.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                 JOptionPane.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       /*
257        * if the view panel was closed its alignment is nulled
258        * and this is an orphaned cut and paste window
259        */
260       if (viewport != null && viewport.getAlignment() != null)
261       {
262         ((AlignViewport) viewport).addAlignment(al, title);
263       }
264       else
265       {
266
267         AlignFrame af;
268         if (source instanceof ComplexAlignFile)
269         {
270           ColumnSelection colSel = ((ComplexAlignFile) source)
271                   .getColumnSelection();
272           SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
273                   .getHiddenSequences();
274           boolean showSeqFeatures = ((ComplexAlignFile) source)
275                   .isShowSeqFeatures();
276           String colourSchemeName = ((ComplexAlignFile) source)
277                   .getGlobalColourScheme();
278           FeaturesDisplayedI fd = ((ComplexAlignFile) source)
279                   .getDisplayedFeatures();
280           af = new AlignFrame(al, hiddenSeqs, colSel,
281                   AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
282           af.getViewport().setShowSequenceFeatures(showSeqFeatures);
283           af.getViewport().setFeaturesDisplayed(fd);
284           ColourSchemeI cs = ColourSchemeMapper.getJalviewColourScheme(
285                   colourSchemeName, al);
286           if (cs != null)
287           {
288             af.changeColour(cs);
289           }
290         }
291         else
292         {
293           af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
294                   AlignFrame.DEFAULT_HEIGHT);
295           if (source instanceof FeaturesSourceI)
296           {
297             af.getViewport().setShowSequenceFeatures(true);
298           }
299         }
300
301         af.currentFileFormat = format;
302         Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
303                 AlignFrame.DEFAULT_HEIGHT);
304         af.statusBar.setText(MessageManager
305                 .getString("label.successfully_pasted_alignment_file"));
306
307         try
308         {
309           af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
310                   false));
311         } catch (Exception ex)
312         {
313         }
314       }
315     }
316     else
317     {
318       System.err.println(MessageManager
319               .getString("label.couldnt_read_data"));
320       if (!Jalview.isHeadlessMode())
321       {
322         javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,
323                 AppletFormatAdapter.SUPPORTED_FORMATS,
324                 MessageManager.getString("label.couldnt_read_data"),
325                 JOptionPane.WARNING_MESSAGE);
326       }
327     }
328   }
329
330   /**
331    * DOCUMENT ME!
332    * 
333    * @param e
334    *          DOCUMENT ME!
335    */
336   @Override
337   public void cancel_actionPerformed(ActionEvent e)
338   {
339     try
340     {
341       this.setClosed(true);
342     } catch (Exception ex)
343     {
344     }
345   }
346
347   @Override
348   public void textarea_mousePressed(MouseEvent e)
349   {
350     if (SwingUtilities.isRightMouseButton(e))
351     {
352       JPopupMenu popup = new JPopupMenu(
353               MessageManager.getString("action.edit"));
354       JMenuItem item = new JMenuItem(
355               MessageManager.getString("action.copy"));
356       item.addActionListener(new ActionListener()
357       {
358         @Override
359         public void actionPerformed(ActionEvent e)
360         {
361           copyItem_actionPerformed(e);
362         }
363       });
364       popup.add(item);
365       item = new JMenuItem(MessageManager.getString("action.paste"));
366       item.addActionListener(new ActionListener()
367       {
368         @Override
369         public void actionPerformed(ActionEvent e)
370         {
371           pasteMenu_actionPerformed(e);
372         }
373       });
374       popup.add(item);
375       popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);
376
377     }
378   }
379
380 }