JAL-3949 Complete new abstracted logging framework in jalview.log. Updated log calls...
[jalview.git] / src / jalview / gui / CutAndPasteHtmlTransfer.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.bin.Cache;
24 import jalview.io.JalviewFileChooser;
25 import jalview.io.JalviewFileView;
26 import jalview.jbgui.GCutAndPasteHtmlTransfer;
27 import jalview.util.MessageManager;
28 import jalview.viewmodel.AlignmentViewport;
29
30 import java.awt.Toolkit;
31 import java.awt.datatransfer.Clipboard;
32 import java.awt.datatransfer.StringSelection;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.awt.event.KeyEvent;
36 import java.awt.event.KeyListener;
37 import java.awt.event.MouseEvent;
38 import java.io.StringWriter;
39
40 import javax.swing.JMenuItem;
41 import javax.swing.JPopupMenu;
42 import javax.swing.SwingUtilities;
43 import javax.swing.event.HyperlinkEvent;
44 import javax.swing.event.HyperlinkEvent.EventType;
45 import javax.swing.event.HyperlinkListener;
46
47 /**
48  * Cut'n'paste files into the desktop See JAL-1105
49  * 
50  * @author $author$
51  * @version $Revision$
52  */
53 public class CutAndPasteHtmlTransfer extends GCutAndPasteHtmlTransfer
54 {
55
56   AlignmentViewport viewport;
57
58   public CutAndPasteHtmlTransfer()
59   {
60     super();
61     displaySource.setSelected(false);
62     textarea.addKeyListener(new KeyListener()
63     {
64
65       @Override
66       public void keyTyped(KeyEvent arg0)
67       {
68         // if (arg0.isControlDown() && arg0.getKeyCode()==KeyEvent.VK_C)
69         // {
70         // copyItem_actionPerformed(null);
71         // }
72         arg0.consume();
73       }
74
75       @Override
76       public void keyReleased(KeyEvent arg0)
77       {
78         // TODO Auto-generated method stub
79
80       }
81
82       @Override
83       public void keyPressed(KeyEvent arg0)
84       {
85         // TODO Auto-generated method stub
86
87       }
88     });
89     textarea.setEditable(false);
90     textarea.addHyperlinkListener(new HyperlinkListener()
91     {
92
93       @Override
94       public void hyperlinkUpdate(HyperlinkEvent e)
95       {
96         if (e.getEventType().equals(EventType.ACTIVATED))
97         {
98           Desktop.showUrl(e.getURL().toExternalForm());
99         }
100       }
101     });
102     SwingUtilities.invokeLater(new Runnable()
103     {
104       @Override
105       public void run()
106       {
107         textarea.requestFocus();
108       }
109     });
110
111   }
112
113   /**
114    * DOCUMENT ME!
115    */
116   public void setForInput(AlignmentViewport viewport)
117   {
118     this.viewport = viewport;
119     if (viewport != null)
120     {
121       ok.setText(MessageManager.getString("action.add"));
122     }
123
124     getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
125   }
126
127   /**
128    * DOCUMENT ME!
129    * 
130    * @return DOCUMENT ME!
131    */
132   public String getText()
133   {
134     return textarea.getText();
135   }
136
137   /**
138    * Set contents of HTML Display pane
139    * 
140    * @param text
141    *          HTML text
142    */
143   public void setText(String text)
144   {
145     textarea.setDocument(textarea.getEditorKit().createDefaultDocument());
146     textarea.setText(text);
147     textarea.setCaretPosition(0);
148   }
149
150   @Override
151   public void save_actionPerformed(ActionEvent e)
152   {
153     // TODO: JAL-3048 JalviewFileChooser - Save option
154
155     JalviewFileChooser chooser = new JalviewFileChooser(
156             Cache.getProperty("LAST_DIRECTORY"));
157
158     chooser.setAcceptAllFileFilterUsed(false);
159     chooser.setFileView(new JalviewFileView());
160     chooser.setDialogTitle(
161             MessageManager.getString("label.save_text_to_file"));
162     chooser.setToolTipText(MessageManager.getString("action.save"));
163
164     int value = chooser.showSaveDialog(this);
165
166     if (value == JalviewFileChooser.APPROVE_OPTION)
167     {
168       try
169       {
170         java.io.PrintWriter out = new java.io.PrintWriter(
171                 new java.io.FileWriter(chooser.getSelectedFile()));
172
173         out.print(getText());
174         out.close();
175       } catch (Exception ex)
176       {
177         ex.printStackTrace();
178       }
179
180     }
181   }
182
183   @Override
184   public void toggleHtml_actionPerformed(ActionEvent e)
185   {
186     String txt = textarea.getText();
187     textarea.setContentType(
188             displaySource.isSelected() ? "text/text" : "text/html");
189     textarea.setText(txt);
190   }
191
192   /**
193    * DOCUMENT ME!
194    * 
195    * @param e
196    *          DOCUMENT ME!
197    */
198   @Override
199   public void copyItem_actionPerformed(ActionEvent e)
200   {
201     Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
202     StringWriter sw = new StringWriter();
203     try
204     {
205       textarea.getEditorKit().write(sw, textarea.getDocument(),
206               textarea.getSelectionStart(),
207               textarea.getSelectionEnd() - textarea.getSelectionStart());
208     } catch (Exception x)
209     {
210     }
211     ;
212     StringSelection ssel = new StringSelection(sw.getBuffer().toString());
213     c.setContents(ssel, ssel);
214   }
215
216   /**
217    * DOCUMENT ME!
218    * 
219    * @param e
220    *          DOCUMENT ME!
221    */
222   @Override
223   public void cancel_actionPerformed(ActionEvent e)
224   {
225     try
226     {
227       this.setClosed(true);
228     } catch (Exception ex)
229     {
230     }
231   }
232
233   @Override
234   public void textarea_mousePressed(MouseEvent e)
235   {
236     // isPopupTrigger is on mousePressed (Mac) or mouseReleased (Windows)
237     if (e.isPopupTrigger())
238     {
239       JPopupMenu popup = new JPopupMenu(
240               MessageManager.getString("action.edit"));
241       JMenuItem item = new JMenuItem(
242               MessageManager.getString("action.copy"));
243       item.addActionListener(new ActionListener()
244       {
245         @Override
246         public void actionPerformed(ActionEvent e)
247         {
248           copyItem_actionPerformed(e);
249         }
250       });
251       popup.add(item);
252       popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);
253
254     }
255   }
256
257 }