JAL-1157 new window for displaying HTML formatted text with menus for saving to a...
authorjprocter <jprocter@compbio.dundee.ac.uk>
Fri, 31 Aug 2012 10:30:13 +0000 (11:30 +0100)
committerjprocter <jprocter@compbio.dundee.ac.uk>
Fri, 31 Aug 2012 10:30:13 +0000 (11:30 +0100)
src/jalview/gui/CutAndPasteHtmlTransfer.java [new file with mode: 0644]
src/jalview/jbgui/GCutAndPasteHtmlTransfer.java [new file with mode: 0644]

diff --git a/src/jalview/gui/CutAndPasteHtmlTransfer.java b/src/jalview/gui/CutAndPasteHtmlTransfer.java
new file mode 100644 (file)
index 0000000..83e8399
--- /dev/null
@@ -0,0 +1,221 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
+ * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+ * 
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package jalview.gui;
+
+import java.awt.*;
+import java.awt.datatransfer.*;
+import java.awt.event.*;
+import java.io.StringWriter;
+
+import javax.swing.*;
+import javax.swing.event.HyperlinkEvent;
+import javax.swing.event.HyperlinkListener;
+import javax.swing.event.HyperlinkEvent.EventType;
+
+import jalview.datamodel.*;
+import jalview.io.*;
+import jalview.jbgui.*;
+
+/**
+ * Cut'n'paste files into the desktop
+ * See JAL-1105
+ * 
+ * @author $author$
+ * @version $Revision$
+ */
+public class CutAndPasteHtmlTransfer extends GCutAndPasteHtmlTransfer
+{
+
+  AlignViewport viewport;
+
+  public CutAndPasteHtmlTransfer()
+  {
+    super();
+    displaySource.setSelected(false);
+    textarea.addKeyListener(new KeyListener()
+    {
+      
+      @Override
+      public void keyTyped(KeyEvent arg0)
+      {
+        //if (arg0.isControlDown() && arg0.getKeyCode()==KeyEvent.VK_C)
+        //{
+        //  copyItem_actionPerformed(null);
+        //} 
+        arg0.consume();
+      }
+      
+      @Override
+      public void keyReleased(KeyEvent arg0)
+      {
+        // TODO Auto-generated method stub
+        
+      }
+      
+      @Override
+      public void keyPressed(KeyEvent arg0)
+      {
+        // TODO Auto-generated method stub
+        
+      }
+    });
+    textarea.setEditable(false);
+    textarea.addHyperlinkListener(new HyperlinkListener()
+    {
+      
+      @Override
+      public void hyperlinkUpdate(HyperlinkEvent e)
+      {
+        if (e.getEventType().equals(EventType.ACTIVATED))
+        {
+          Desktop.showUrl(e.getURL().toExternalForm());
+        }
+      }
+    });
+    SwingUtilities.invokeLater(new Runnable()
+    {
+      public void run()
+      {
+//        textarea.requestFocus();
+      }
+    });
+
+  }
+
+  /**
+   * DOCUMENT ME!
+   */
+  public void setForInput(AlignViewport viewport)
+  {
+    this.viewport = viewport;
+    if (viewport != null)
+    {
+      ok.setText("Add");
+    }
+
+    getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
+  }
+
+  /**
+   * DOCUMENT ME!
+   * 
+   * @return DOCUMENT ME!
+   */
+  public String getText()
+  {
+    return textarea.getText();
+  }
+  /**
+   * Set contents of HTML Display pane
+   * 
+   * @param text HTML text
+   */
+  public void setText(String text)
+  {
+    textarea.setText(text);
+  }
+
+  public void save_actionPerformed(ActionEvent e)
+  {
+    JalviewFileChooser chooser = new JalviewFileChooser(
+            jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
+
+    chooser.setAcceptAllFileFilterUsed(false);
+    chooser.setFileView(new JalviewFileView());
+    chooser.setDialogTitle("Save Text to File");
+    chooser.setToolTipText("Save");
+
+    int value = chooser.showSaveDialog(this);
+
+    if (value == JalviewFileChooser.APPROVE_OPTION)
+    {
+      try
+      {
+        java.io.PrintWriter out = new java.io.PrintWriter(
+                new java.io.FileWriter(chooser.getSelectedFile()));
+
+        out.print(getText());
+        out.close();
+      } catch (Exception ex)
+      {
+        ex.printStackTrace();
+      }
+
+    }
+  }
+
+  public void toggleHtml_actionPerformed(ActionEvent e)
+  {
+    String txt=textarea.getText();
+    textarea.setContentType(displaySource.isSelected() ? "text/text" : "text/html");
+    textarea.setText(txt);
+  }
+  /**
+   * DOCUMENT ME!
+   * 
+   * @param e
+   *          DOCUMENT ME!
+   */
+  public void copyItem_actionPerformed(ActionEvent e)
+  {
+    Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+    StringWriter sw = new StringWriter();
+    try {
+      textarea.getEditorKit().write(sw, textarea.getDocument(), textarea.getSelectionStart(), textarea.getSelectionEnd()-textarea.getSelectionStart());
+    } catch (Exception x){};
+    StringSelection ssel=new StringSelection(sw.getBuffer().toString());
+    c.setContents(ssel,ssel);
+  }
+
+
+  /**
+   * DOCUMENT ME!
+   * 
+   * @param e
+   *          DOCUMENT ME!
+   */
+  public void cancel_actionPerformed(ActionEvent e)
+  {
+    try
+    {
+      this.setClosed(true);
+    } catch (Exception ex)
+    {
+    }
+  }
+
+  public void textarea_mousePressed(MouseEvent e)
+  {
+    if (SwingUtilities.isRightMouseButton(e))
+    {
+      JPopupMenu popup = new JPopupMenu("Edit");
+      JMenuItem item = new JMenuItem("Copy");
+      item.addActionListener(new ActionListener()
+      {
+        public void actionPerformed(ActionEvent e)
+        {
+          copyItem_actionPerformed(e);
+        }
+      });
+      popup.add(item);
+      popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);
+
+    }
+  }
+
+}
diff --git a/src/jalview/jbgui/GCutAndPasteHtmlTransfer.java b/src/jalview/jbgui/GCutAndPasteHtmlTransfer.java
new file mode 100644 (file)
index 0000000..6627029
--- /dev/null
@@ -0,0 +1,243 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
+ * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+ * 
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package jalview.jbgui;
+
+import jalview.gui.JvSwingUtils;
+
+import java.awt.*;
+import java.awt.event.*;
+
+import javax.swing.*;
+
+/**
+ * DOCUMENT ME!
+ * 
+ * @author $author$
+ * @version $Revision$
+ */
+public class GCutAndPasteHtmlTransfer extends JInternalFrame
+{
+  protected JEditorPane textarea = new JEditorPane("text/html","");
+
+  protected JScrollPane scrollPane = new JScrollPane();
+
+  BorderLayout borderLayout1 = new BorderLayout();
+
+  JMenuBar editMenubar = new JMenuBar();
+
+  JMenu editMenu = new JMenu();
+
+  JMenuItem copyItem = new JMenuItem();
+  protected JCheckBoxMenuItem displaySource=new JCheckBoxMenuItem();
+
+  BorderLayout borderLayout2 = new BorderLayout();
+
+  protected JPanel inputButtonPanel = new JPanel();
+
+  protected JButton ok = new JButton();
+
+  JButton cancel = new JButton();
+
+  JMenuItem close = new JMenuItem();
+
+  JMenuItem selectAll = new JMenuItem();
+
+  JMenu jMenu1 = new JMenu();
+
+  JMenuItem save = new JMenuItem();
+
+  /**
+   * Creates a new GCutAndPasteTransfer object.
+   */
+  public GCutAndPasteHtmlTransfer()
+  {
+    try
+    {
+      setJMenuBar(editMenubar);
+      jbInit();
+    } catch (Exception e)
+    {
+      e.printStackTrace();
+    }
+  }
+
+  /**
+   * DOCUMENT ME!
+   * 
+   * @throws Exception
+   *           DOCUMENT ME!
+   */
+  private void jbInit() throws Exception
+  {
+    scrollPane.setBorder(null);
+    ok.setFont(JvSwingUtils.getLabelFont());
+    ok.setText("New Window");
+    ok.addActionListener(new ActionListener()
+    {
+      public void actionPerformed(ActionEvent e)
+      {
+        ok_actionPerformed(e);
+      }
+    });
+    cancel.setText("Close");
+    cancel.addActionListener(new ActionListener()
+    {
+      public void actionPerformed(ActionEvent e)
+      {
+        cancel_actionPerformed(e);
+      }
+    });
+    textarea.setBorder(null);
+    close.setText("Close");
+    close.addActionListener(new ActionListener() {
+      public void actionPerformed(ActionEvent e)
+      {
+        cancel_actionPerformed(e);
+      }
+    });
+    close.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
+            java.awt.event.KeyEvent.VK_W, Toolkit.getDefaultToolkit()
+            .getMenuShortcutKeyMask(), false));
+    selectAll.setText("Select All");
+    selectAll.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
+            java.awt.event.KeyEvent.VK_A, Toolkit.getDefaultToolkit()
+                    .getMenuShortcutKeyMask(), false));
+    selectAll.addActionListener(new ActionListener()
+    {
+      public void actionPerformed(ActionEvent e)
+      {
+        selectAll_actionPerformed(e);
+      }
+    });
+    jMenu1.setText("File");
+    save.setText("Save");
+    save.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
+            java.awt.event.KeyEvent.VK_S, Toolkit.getDefaultToolkit()
+                    .getMenuShortcutKeyMask(), false));
+    save.addActionListener(new ActionListener()
+    {
+      public void actionPerformed(ActionEvent e)
+      {
+        save_actionPerformed(e);
+      }
+    });
+    copyItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
+            java.awt.event.KeyEvent.VK_C, Toolkit.getDefaultToolkit()
+                    .getMenuShortcutKeyMask(), false));
+    
+    editMenubar.add(jMenu1);
+    editMenubar.add(editMenu);
+    textarea.setFont(new java.awt.Font("Monospaced", Font.PLAIN, 12));
+    textarea.addMouseListener(new java.awt.event.MouseAdapter()
+    {
+      public void mousePressed(MouseEvent e)
+      {
+        textarea_mousePressed(e);
+      }
+    });
+    editMenu.setText("Edit");
+    copyItem.setText("Copy");
+    copyItem.addActionListener(new ActionListener()
+    {
+      public void actionPerformed(ActionEvent e)
+      {
+        copyItem_actionPerformed(e);
+      }
+    });
+    displaySource.setText("Show HTML Source");
+    displaySource.setToolTipText("Select this if you want to copy raw html");
+    displaySource.addActionListener(new ActionListener()
+    {
+      
+      @Override
+      public void actionPerformed(ActionEvent arg0)
+      {
+        toggleHtml_actionPerformed(arg0);
+      }
+    });
+    editMenu.add(displaySource);
+    this.getContentPane().setLayout(borderLayout2);
+    scrollPane.setBorder(null);
+    scrollPane.getViewport().add(textarea, null);
+    editMenu.add(selectAll);
+    editMenu.add(copyItem);
+    this.getContentPane().add(scrollPane, java.awt.BorderLayout.CENTER);
+    inputButtonPanel.add(ok);
+    inputButtonPanel.add(cancel);
+    jMenu1.add(save);
+    jMenu1.add(close);
+  }
+
+  protected void toggleHtml_actionPerformed(ActionEvent arg0)
+  {
+    // TODO Auto-generated method stub
+    
+  }
+
+  /**
+   * DOCUMENT ME!
+   * 
+   * @param e
+   *          DOCUMENT ME!
+   */
+  public void textarea_mousePressed(MouseEvent e)
+  {
+
+  }
+
+  /**
+   * DOCUMENT ME!
+   * 
+   * @param e
+   *          DOCUMENT ME!
+   */
+  public void copyItem_actionPerformed(ActionEvent e)
+  {
+  }
+
+
+  /**
+   * DOCUMENT ME!
+   * 
+   * @param e
+   *          DOCUMENT ME!
+   */
+  public void ok_actionPerformed(ActionEvent e)
+  {
+  }
+
+  /**
+   * DOCUMENT ME!
+   * 
+   * @param e
+   *          DOCUMENT ME!
+   */
+  public void cancel_actionPerformed(ActionEvent e)
+  {
+  }
+
+  public void selectAll_actionPerformed(ActionEvent e)
+  {
+    textarea.selectAll();
+  }
+
+  public void save_actionPerformed(ActionEvent e)
+  {
+
+  }
+}