545460df0d8d8de1478e0be9929029971a2c3abf
[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       public void run()
105       {
106         textarea.requestFocus();
107       }
108     });
109
110   }
111
112   /**
113    * DOCUMENT ME!
114    */
115   public void setForInput(AlignmentViewport viewport)
116   {
117     this.viewport = viewport;
118     if (viewport != null)
119     {
120       ok.setText(MessageManager.getString("action.add"));
121     }
122
123     getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
124   }
125
126   /**
127    * DOCUMENT ME!
128    * 
129    * @return DOCUMENT ME!
130    */
131   public String getText()
132   {
133     return textarea.getText();
134   }
135
136   /**
137    * Set contents of HTML Display pane
138    * 
139    * @param text
140    *          HTML text
141    */
142   public void setText(String text)
143   {
144     textarea.setText(text);
145   }
146
147   public void save_actionPerformed(ActionEvent e)
148   {
149     JalviewFileChooser chooser = new JalviewFileChooser(
150             Cache.getProperty("LAST_DIRECTORY"));
151
152     chooser.setAcceptAllFileFilterUsed(false);
153     chooser.setFileView(new JalviewFileView());
154     chooser.setDialogTitle(MessageManager.getString("label.save_text_to_file"));
155     chooser.setToolTipText(MessageManager.getString("action.save"));
156
157     int value = chooser.showSaveDialog(this);
158
159     if (value == JalviewFileChooser.APPROVE_OPTION)
160     {
161       try
162       {
163         java.io.PrintWriter out = new java.io.PrintWriter(
164                 new java.io.FileWriter(chooser.getSelectedFile()));
165
166         out.print(getText());
167         out.close();
168       } catch (Exception ex)
169       {
170         ex.printStackTrace();
171       }
172
173     }
174   }
175
176   public void toggleHtml_actionPerformed(ActionEvent e)
177   {
178     String txt = textarea.getText();
179     textarea.setContentType(displaySource.isSelected() ? "text/text"
180             : "text/html");
181     textarea.setText(txt);
182   }
183
184   /**
185    * DOCUMENT ME!
186    * 
187    * @param e
188    *          DOCUMENT ME!
189    */
190   public void copyItem_actionPerformed(ActionEvent e)
191   {
192     Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
193     StringWriter sw = new StringWriter();
194     try
195     {
196       textarea.getEditorKit().write(sw, textarea.getDocument(),
197               textarea.getSelectionStart(),
198               textarea.getSelectionEnd() - textarea.getSelectionStart());
199     } catch (Exception x)
200     {
201     }
202     ;
203     StringSelection ssel = new StringSelection(sw.getBuffer().toString());
204     c.setContents(ssel, ssel);
205   }
206
207   /**
208    * DOCUMENT ME!
209    * 
210    * @param e
211    *          DOCUMENT ME!
212    */
213   public void cancel_actionPerformed(ActionEvent e)
214   {
215     try
216     {
217       this.setClosed(true);
218     } catch (Exception ex)
219     {
220     }
221   }
222
223   public void textarea_mousePressed(MouseEvent e)
224   {
225     if (SwingUtilities.isRightMouseButton(e))
226     {
227       JPopupMenu popup = new JPopupMenu(
228               MessageManager.getString("action.edit"));
229       JMenuItem item = new JMenuItem(
230               MessageManager.getString("action.copy"));
231       item.addActionListener(new ActionListener()
232       {
233         public void actionPerformed(ActionEvent e)
234         {
235           copyItem_actionPerformed(e);
236         }
237       });
238       popup.add(item);
239       popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);
240
241     }
242   }
243
244 }