2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
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;
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;
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;
47 * Cut'n'paste files into the desktop See JAL-1105
52 public class CutAndPasteHtmlTransfer extends GCutAndPasteHtmlTransfer
55 AlignmentViewport viewport;
57 public CutAndPasteHtmlTransfer()
60 displaySource.setSelected(false);
61 textarea.addKeyListener(new KeyListener()
65 public void keyTyped(KeyEvent arg0)
67 // if (arg0.isControlDown() && arg0.getKeyCode()==KeyEvent.VK_C)
69 // copyItem_actionPerformed(null);
75 public void keyReleased(KeyEvent arg0)
77 // TODO Auto-generated method stub
82 public void keyPressed(KeyEvent arg0)
84 // TODO Auto-generated method stub
88 textarea.setEditable(false);
89 textarea.addHyperlinkListener(new HyperlinkListener()
93 public void hyperlinkUpdate(HyperlinkEvent e)
95 if (e.getEventType().equals(EventType.ACTIVATED))
97 Desktop.showUrl(e.getURL().toExternalForm());
101 SwingUtilities.invokeLater(new Runnable()
106 textarea.requestFocus();
115 public void setForInput(AlignmentViewport viewport)
117 this.viewport = viewport;
118 if (viewport != null)
120 ok.setText(MessageManager.getString("action.add"));
123 getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
129 * @return DOCUMENT ME!
131 public String getText()
133 return textarea.getText();
137 * Set contents of HTML Display pane
142 public void setText(String text)
144 textarea.setDocument(textarea.getEditorKit().createDefaultDocument());
145 textarea.setText(text);
146 textarea.setCaretPosition(0);
150 public void save_actionPerformed(ActionEvent e)
152 // TODO: JAL-3048 JalviewFileChooser - Save option
154 JalviewFileChooser chooser = new JalviewFileChooser(
155 jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
157 chooser.setAcceptAllFileFilterUsed(false);
158 chooser.setFileView(new JalviewFileView());
159 chooser.setDialogTitle(
160 MessageManager.getString("label.save_text_to_file"));
161 chooser.setToolTipText(MessageManager.getString("action.save"));
163 int value = chooser.showSaveDialog(this);
165 if (value == JalviewFileChooser.APPROVE_OPTION)
169 java.io.PrintWriter out = new java.io.PrintWriter(
170 new java.io.FileWriter(chooser.getSelectedFile()));
172 out.print(getText());
174 } catch (Exception ex)
176 ex.printStackTrace();
183 public void toggleHtml_actionPerformed(ActionEvent e)
185 String txt = textarea.getText();
186 textarea.setContentType(
187 displaySource.isSelected() ? "text/text" : "text/html");
188 textarea.setText(txt);
198 public void copyItem_actionPerformed(ActionEvent e)
200 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
201 StringWriter sw = new StringWriter();
204 textarea.getEditorKit().write(sw, textarea.getDocument(),
205 textarea.getSelectionStart(),
206 textarea.getSelectionEnd() - textarea.getSelectionStart());
207 } catch (Exception x)
211 StringSelection ssel = new StringSelection(sw.getBuffer().toString());
212 c.setContents(ssel, ssel);
222 public void cancel_actionPerformed(ActionEvent e)
226 this.setClosed(true);
227 } catch (Exception ex)
233 public void textarea_mousePressed(MouseEvent e)
235 // isPopupTrigger is on mousePressed (Mac) or mouseReleased (Windows)
236 if (e.isPopupTrigger())
238 JPopupMenu popup = new JPopupMenu(
239 MessageManager.getString("action.edit"));
240 JMenuItem item = new JMenuItem(
241 MessageManager.getString("action.copy"));
242 item.addActionListener(new ActionListener()
245 public void actionPerformed(ActionEvent e)
247 copyItem_actionPerformed(e);
251 popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);