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 java.awt.Toolkit;
24 import java.awt.datatransfer.Clipboard;
25 import java.awt.datatransfer.StringSelection;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28 import java.awt.event.KeyEvent;
29 import java.awt.event.KeyListener;
30 import java.awt.event.MouseEvent;
31 import java.io.StringWriter;
33 import javax.swing.JMenuItem;
34 import javax.swing.JPopupMenu;
35 import javax.swing.SwingUtilities;
36 import javax.swing.event.HyperlinkEvent;
37 import javax.swing.event.HyperlinkEvent.EventType;
38 import javax.swing.event.HyperlinkListener;
40 import jalview.bin.Cache;
41 import jalview.io.JalviewFileChooser;
42 import jalview.io.JalviewFileView;
43 import jalview.jbgui.GCutAndPasteHtmlTransfer;
44 import jalview.util.MessageManager;
45 import jalview.viewmodel.AlignmentViewport;
48 * Cut'n'paste files into the desktop See JAL-1105
53 public class CutAndPasteHtmlTransfer extends GCutAndPasteHtmlTransfer
56 AlignmentViewport viewport;
58 public CutAndPasteHtmlTransfer()
61 this.setFrameIcon(null);
62 displaySource.setSelected(false);
63 textarea.addKeyListener(new KeyListener()
67 public void keyTyped(KeyEvent arg0)
69 // if (arg0.isControlDown() && arg0.getKeyCode()==KeyEvent.VK_C)
71 // copyItem_actionPerformed(null);
77 public void keyReleased(KeyEvent arg0)
79 // TODO Auto-generated method stub
84 public void keyPressed(KeyEvent arg0)
86 // TODO Auto-generated method stub
90 textarea.setEditable(false);
91 textarea.addHyperlinkListener(new HyperlinkListener()
95 public void hyperlinkUpdate(HyperlinkEvent e)
97 if (e.getEventType().equals(EventType.ACTIVATED))
99 Desktop.showUrl(e.getURL().toExternalForm());
103 SwingUtilities.invokeLater(new Runnable()
108 textarea.requestFocus();
117 public void setForInput(AlignmentViewport viewport)
119 this.viewport = viewport;
120 if (viewport != null)
122 ok.setText(MessageManager.getString("action.add"));
125 getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
131 * @return DOCUMENT ME!
133 public String getText()
135 return textarea.getText();
139 * Set contents of HTML Display pane
144 public void setText(String text)
146 textarea.setDocument(textarea.getEditorKit().createDefaultDocument());
147 textarea.setText(text);
148 textarea.setCaretPosition(0);
152 public void save_actionPerformed(ActionEvent e)
154 // TODO: JAL-3048 JalviewFileChooser - Save option
156 JalviewFileChooser chooser = new JalviewFileChooser(
157 Cache.getProperty("LAST_DIRECTORY"));
159 chooser.setAcceptAllFileFilterUsed(false);
160 chooser.setFileView(new JalviewFileView());
161 chooser.setDialogTitle(
162 MessageManager.getString("label.save_text_to_file"));
163 chooser.setToolTipText(MessageManager.getString("action.save"));
165 int value = chooser.showSaveDialog(this);
167 if (value == JalviewFileChooser.APPROVE_OPTION)
171 java.io.PrintWriter out = new java.io.PrintWriter(
172 new java.io.FileWriter(chooser.getSelectedFile()));
174 out.print(getText());
176 } catch (Exception ex)
178 ex.printStackTrace();
185 public void toggleHtml_actionPerformed(ActionEvent e)
187 String txt = textarea.getText();
188 textarea.setContentType(
189 displaySource.isSelected() ? "text/text" : "text/html");
190 textarea.setText(txt);
200 public void copyItem_actionPerformed(ActionEvent e)
202 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
203 StringWriter sw = new StringWriter();
206 textarea.getEditorKit().write(sw, textarea.getDocument(),
207 textarea.getSelectionStart(),
208 textarea.getSelectionEnd() - textarea.getSelectionStart());
209 } catch (Exception x)
213 StringSelection ssel = new StringSelection(sw.getBuffer().toString());
214 c.setContents(ssel, ssel);
224 public void cancel_actionPerformed(ActionEvent e)
228 this.setClosed(true);
229 } catch (Exception ex)
235 public void textarea_mousePressed(MouseEvent e)
237 // isPopupTrigger is on mousePressed (Mac) or mouseReleased (Windows)
238 if (e.isPopupTrigger())
240 JPopupMenu popup = new JPopupMenu(
241 MessageManager.getString("action.edit"));
242 JMenuItem item = new JMenuItem(
243 MessageManager.getString("action.copy"));
244 item.addActionListener(new ActionListener()
247 public void actionPerformed(ActionEvent e)
249 copyItem_actionPerformed(e);
253 popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);