JAL-3295 pre-merged code with JAL-3295 branch which will go onto develop
[jalview.git] / src / jalview / jbgui / GCutAndPasteHtmlTransfer.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.jbgui;
22
23 import jalview.gui.JvSwingUtils;
24 import jalview.util.MessageManager;
25
26 import java.awt.BorderLayout;
27 import java.awt.Font;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.awt.event.MouseEvent;
31
32 import javax.swing.JButton;
33 import javax.swing.JCheckBoxMenuItem;
34 import javax.swing.JEditorPane;
35 import javax.swing.JInternalFrame;
36 import javax.swing.JMenu;
37 import javax.swing.JMenuBar;
38 import javax.swing.JMenuItem;
39 import javax.swing.JPanel;
40 import javax.swing.JScrollPane;
41 import javax.swing.text.EditorKit;
42 import javax.swing.text.html.HTMLEditorKit;
43
44 /**
45  * DOCUMENT ME!
46  * 
47  * @author $author$
48  * @version $Revision$
49  */
50 public class GCutAndPasteHtmlTransfer extends JInternalFrame
51 {
52   protected JEditorPane textarea = new JEditorPane("text/html", "");
53
54   protected JScrollPane scrollPane = new JScrollPane();
55
56   BorderLayout borderLayout1 = new BorderLayout();
57
58   JMenuBar editMenubar = new JMenuBar();
59
60   JMenu editMenu = new JMenu();
61
62   JMenuItem copyItem = new JMenuItem();
63
64   protected JCheckBoxMenuItem displaySource = new JCheckBoxMenuItem();
65
66   BorderLayout borderLayout2 = new BorderLayout();
67
68   protected JPanel inputButtonPanel = new JPanel();
69
70   protected JButton ok = new JButton();
71
72   JButton cancel = new JButton();
73
74   JMenuItem close = new JMenuItem();
75
76   JMenuItem selectAll = new JMenuItem();
77
78   JMenu jMenu1 = new JMenu();
79
80   JMenuItem save = new JMenuItem();
81
82   /**
83    * Creates a new GCutAndPasteTransfer object.
84    */
85   public GCutAndPasteHtmlTransfer()
86   {
87     try
88     {
89       textarea.setEditorKit(new HTMLEditorKit());
90       setJMenuBar(editMenubar);
91       jbInit();
92     } catch (Exception e)
93     {
94       e.printStackTrace();
95     }
96   }
97
98   /**
99    * DOCUMENT ME!
100    * 
101    * @throws Exception
102    *           DOCUMENT ME!
103    */
104   private void jbInit() throws Exception
105   {
106     scrollPane.setBorder(null);
107     ok.setFont(JvSwingUtils.getLabelFont());
108     ok.setText(MessageManager.getString("label.new_window"));
109     ok.addActionListener(new ActionListener()
110     {
111       @Override
112       public void actionPerformed(ActionEvent e)
113       {
114         ok_actionPerformed(e);
115       }
116     });
117     cancel.setText(MessageManager.getString("action.close"));
118     cancel.addActionListener(new ActionListener()
119     {
120       @Override
121       public void actionPerformed(ActionEvent e)
122       {
123         cancel_actionPerformed(e);
124       }
125     });
126     textarea.setBorder(null);
127     close.setText(MessageManager.getString("action.close"));
128     close.addActionListener(new ActionListener()
129     {
130       @Override
131       public void actionPerformed(ActionEvent e)
132       {
133         cancel_actionPerformed(e);
134       }
135     });
136     close.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
137             java.awt.event.KeyEvent.VK_W,
138             jalview.util.ShortcutKeyMaskExWrapper
139                     .getMenuShortcutKeyMaskEx(),
140             false));
141     selectAll.setText(MessageManager.getString("action.select_all"));
142     selectAll.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
143             java.awt.event.KeyEvent.VK_A,
144             jalview.util.ShortcutKeyMaskExWrapper
145                     .getMenuShortcutKeyMaskEx(),
146             false));
147     selectAll.addActionListener(new ActionListener()
148     {
149       @Override
150       public void actionPerformed(ActionEvent e)
151       {
152         selectAll_actionPerformed(e);
153       }
154     });
155     jMenu1.setText(MessageManager.getString("action.file"));
156     save.setText(MessageManager.getString("action.save"));
157     save.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
158             java.awt.event.KeyEvent.VK_S,
159             jalview.util.ShortcutKeyMaskExWrapper
160                     .getMenuShortcutKeyMaskEx(),
161             false));
162     save.addActionListener(new ActionListener()
163     {
164       @Override
165       public void actionPerformed(ActionEvent e)
166       {
167         save_actionPerformed(e);
168       }
169     });
170     copyItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
171             java.awt.event.KeyEvent.VK_C,
172             jalview.util.ShortcutKeyMaskExWrapper
173                     .getMenuShortcutKeyMaskEx(),
174             false));
175
176     editMenubar.add(jMenu1);
177     editMenubar.add(editMenu);
178     textarea.setFont(new java.awt.Font("Monospaced", Font.PLAIN, 12));
179     textarea.addMouseListener(new java.awt.event.MouseAdapter()
180     {
181       @Override
182       public void mousePressed(MouseEvent e)
183       {
184         textarea_mousePressed(e);
185       }
186
187       @Override
188       public void mouseReleased(MouseEvent e)
189       {
190         textarea_mousePressed(e);
191       }
192     });
193     editMenu.setText(MessageManager.getString("action.edit"));
194     copyItem.setText(MessageManager.getString("action.copy"));
195     copyItem.addActionListener(new ActionListener()
196     {
197       @Override
198       public void actionPerformed(ActionEvent e)
199       {
200         copyItem_actionPerformed(e);
201       }
202     });
203     displaySource
204             .setText(MessageManager.getString("action.show_html_source"));
205     displaySource.setToolTipText(
206             MessageManager.getString("label.select_copy_raw_html"));
207     displaySource.addActionListener(new ActionListener()
208     {
209
210       @Override
211       public void actionPerformed(ActionEvent arg0)
212       {
213         toggleHtml_actionPerformed(arg0);
214       }
215     });
216     editMenu.add(displaySource);
217     this.getContentPane().setLayout(borderLayout2);
218     scrollPane.setBorder(null);
219     scrollPane.getViewport().add(textarea, null);
220     editMenu.add(selectAll);
221     editMenu.add(copyItem);
222     this.getContentPane().add(scrollPane, java.awt.BorderLayout.CENTER);
223     inputButtonPanel.add(ok);
224     inputButtonPanel.add(cancel);
225     jMenu1.add(save);
226     jMenu1.add(close);
227   }
228
229   protected void toggleHtml_actionPerformed(ActionEvent arg0)
230   {
231     // TODO Auto-generated method stub
232
233   }
234
235   /**
236    * DOCUMENT ME!
237    * 
238    * @param e
239    *          DOCUMENT ME!
240    */
241   public void textarea_mousePressed(MouseEvent e)
242   {
243
244   }
245
246   /**
247    * DOCUMENT ME!
248    * 
249    * @param e
250    *          DOCUMENT ME!
251    */
252   public void copyItem_actionPerformed(ActionEvent e)
253   {
254   }
255
256   /**
257    * DOCUMENT ME!
258    * 
259    * @param e
260    *          DOCUMENT ME!
261    */
262   public void ok_actionPerformed(ActionEvent e)
263   {
264   }
265
266   /**
267    * DOCUMENT ME!
268    * 
269    * @param e
270    *          DOCUMENT ME!
271    */
272   public void cancel_actionPerformed(ActionEvent e)
273   {
274   }
275
276   public void selectAll_actionPerformed(ActionEvent e)
277   {
278     textarea.selectAll();
279   }
280
281   public void save_actionPerformed(ActionEvent e)
282   {
283
284   }
285
286   /**
287    * Adds the given stylesheet rule to the Html editor. However note that CSS
288    * support is limited.
289    * 
290    * @param rule
291    * @see javax.swing.text.html.CSS
292    */
293   public void addStylesheetRule(String rule)
294   {
295     EditorKit editorKit = textarea.getEditorKit();
296     if (editorKit != null)
297     {
298       ((HTMLEditorKit) editorKit).getStyleSheet().addRule(rule);
299     }
300   }
301 }