7d5d8d7213ba6df7311eaa2c7f1a499510c9a43d
[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.io.JalviewFileChooser;
24 import jalview.io.JalviewFileView;
25 import jalview.jbgui.GCutAndPasteHtmlTransfer;
26 import jalview.util.MessageManager;
27 import jalview.viewmodel.AlignmentViewport;
28
29 import java.awt.Toolkit;
30 import java.awt.datatransfer.Clipboard;
31 import java.awt.datatransfer.StringSelection;
32 import java.awt.event.ActionEvent;
33 import java.awt.event.ActionListener;
34 import java.awt.event.KeyEvent;
35 import java.awt.event.KeyListener;
36 import java.awt.event.MouseEvent;
37 import java.io.StringWriter;
38
39 import javax.swing.JMenuItem;
40 import javax.swing.JPopupMenu;
41 import javax.swing.SwingUtilities;
42 import javax.swing.event.HyperlinkEvent;
43 import javax.swing.event.HyperlinkEvent.EventType;
44 import javax.swing.event.HyperlinkListener;
45
46 /**
47  * Cut'n'paste files into the desktop See JAL-1105
48  * 
49  * @author $author$
50  * @version $Revision$
51  */
52 public class CutAndPasteHtmlTransfer extends GCutAndPasteHtmlTransfer
53 {
54
55   AlignmentViewport viewport;
56
57   public CutAndPasteHtmlTransfer()
58   {
59     super();
60     displaySource.setSelected(false);
61     textarea.addKeyListener(new KeyListener()
62     {
63
64       @Override
65       public void keyTyped(KeyEvent arg0)
66       {
67         // if (arg0.isControlDown() && arg0.getKeyCode()==KeyEvent.VK_C)
68         // {
69         // copyItem_actionPerformed(null);
70         // }
71         arg0.consume();
72       }
73
74       @Override
75       public void keyReleased(KeyEvent arg0)
76       {
77         // TODO Auto-generated method stub
78
79       }
80
81       @Override
82       public void keyPressed(KeyEvent arg0)
83       {
84         // TODO Auto-generated method stub
85
86       }
87     });
88     textarea.setEditable(false);
89     textarea.addHyperlinkListener(new HyperlinkListener()
90     {
91
92       @Override
93       public void hyperlinkUpdate(HyperlinkEvent e)
94       {
95         if (e.getEventType().equals(EventType.ACTIVATED))
96         {
97           Desktop.showUrl(e.getURL().toExternalForm());
98         }
99       }
100     });
101     SwingUtilities.invokeLater(new Runnable()
102     {
103       public void run()
104       {
105         textarea.requestFocus();
106       }
107     });
108
109   }
110
111   /**
112    * DOCUMENT ME!
113    */
114   public void setForInput(AlignmentViewport viewport)
115   {
116     this.viewport = viewport;
117     if (viewport != null)
118     {
119       ok.setText(MessageManager.getString("action.add"));
120     }
121
122     getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
123   }
124
125   /**
126    * DOCUMENT ME!
127    * 
128    * @return DOCUMENT ME!
129    */
130   public String getText()
131   {
132     return textarea.getText();
133   }
134
135   /**
136    * Set contents of HTML Display pane
137    * 
138    * @param text
139    *          HTML text
140    */
141   public void setText(String text)
142   {
143     textarea.setText(text);
144   }
145
146   public void save_actionPerformed(ActionEvent e)
147   {
148     JalviewFileChooser chooser = new JalviewFileChooser(
149             jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
150
151     chooser.setAcceptAllFileFilterUsed(false);
152     chooser.setFileView(new JalviewFileView());
153     chooser.setDialogTitle(MessageManager
154             .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 }