1. Project Clover database Fri Dec 6 2024 13:47:14 GMT
  2. Package jalview.gui

File CutAndPasteTransfer.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
60% of files have more coverage

Code metrics

30
98
16
1
431
305
40
0.41
6.12
16
2.5

Classes

Class
Line #
Actions
CutAndPasteTransfer 72 98 40
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

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