in progress
[jalview.git] / forester / java / src / org / forester / archaeopteryx / TextFrame.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // Copyright (C) 2000-2001 Washington University School of Medicine
8 // and Howard Hughes Medical Institute
9 // Copyright (C) 2003-2007 Ethalinda K.S. Cannon
10 // All rights reserved
11 //
12 // This library is free software; you can redistribute it and/or
13 // modify it under the terms of the GNU Lesser General Public
14 // License as published by the Free Software Foundation; either
15 // version 2.1 of the License, or (at your option) any later version.
16 //
17 // This library is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License along with this library; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 //
26 // Contact: phylosoft @ gmail . com
27 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
28
29 package org.forester.archaeopteryx;
30
31 import java.awt.AWTPermission;
32 import java.awt.BorderLayout;
33 import java.awt.Color;
34 import java.awt.Container;
35 import java.awt.FlowLayout;
36 import java.awt.Font;
37 import java.awt.datatransfer.Clipboard;
38 import java.awt.datatransfer.ClipboardOwner;
39 import java.awt.datatransfer.StringSelection;
40 import java.awt.datatransfer.Transferable;
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import java.awt.event.WindowAdapter;
44 import java.awt.event.WindowEvent;
45 import java.security.acl.Permission;
46 import java.util.LinkedList;
47
48 import javax.swing.JButton;
49 import javax.swing.JFrame;
50 import javax.swing.JPanel;
51 import javax.swing.JScrollPane;
52 import javax.swing.JTextArea;
53
54 final class TextFrame extends JFrame implements ActionListener, ClipboardOwner {
55
56     /**
57      *
58      */
59     private static final long           serialVersionUID = -5012834229705518363L;
60     private static Color                ta_text_color    = new Color( 0, 0, 0 ),
61             ta_background_color = new Color( 240, 240, 240 ), background_color = new Color( 215, 215, 215 ),
62             button_background_color = new Color( 215, 215, 215 ), button_text_color = new Color( 0, 0, 0 );
63     private final static Font           button_font      = new Font( "Helvetica", Font.PLAIN, 10 ),
64             ta_font = new Font( "Helvetica", Font.PLAIN, 10 );
65     private boolean                     can_use_clipboard;
66     private final String                text;
67     private final JTextArea             jtextarea;
68     private final JButton               close_button;
69     private JButton                     copy_button;
70     private final JPanel                buttonjpanel;
71     private final Container             contentpane;
72     private final LinkedList<TextFrame> _tframes;
73
74     private TextFrame( final String s, final String title, final LinkedList<TextFrame> tframes ) {
75         // first things first
76         setTitle( title );
77         text = s;
78         _tframes = tframes;
79         // check to see if we have permission to use the clipboard:
80         can_use_clipboard = true;
81         final SecurityManager sm = System.getSecurityManager();
82         if ( sm != null ) {
83             try {
84                 sm.checkPermission( new AWTPermission( "accessClipboard" ) );
85             }
86             catch ( final Exception e ) {
87                 can_use_clipboard = false;
88             }
89         }
90         // set up the frame
91         setBackground( background_color );
92         buttonjpanel = new JPanel();
93         buttonjpanel.setBackground( background_color );
94         close_button = new JButton( "          Close          " );
95         close_button.setBackground( button_background_color );
96         close_button.setForeground( button_text_color );
97         close_button.setFont( button_font );
98         close_button.addActionListener( this );
99         buttonjpanel.add( close_button );
100         if ( can_use_clipboard ) {
101             copy_button = new JButton( "Copy to clipboard" );
102             copy_button.setBackground( button_background_color );
103             copy_button.setForeground( button_text_color );
104             copy_button.setFont( button_font );
105             copy_button.addActionListener( this );
106             buttonjpanel.add( copy_button );
107         }
108         contentpane = getContentPane();
109         contentpane.setLayout( new BorderLayout() );
110         jtextarea = new JTextArea( text );
111         jtextarea.setBackground( ta_background_color );
112         jtextarea.setForeground( ta_text_color );
113         jtextarea.setFont( ta_font );
114         jtextarea.setEditable( false );
115         jtextarea.setWrapStyleWord( true );
116         jtextarea.setLineWrap( true );
117         contentpane.add( new JScrollPane( jtextarea ), BorderLayout.CENTER );
118         buttonjpanel.setLayout( new FlowLayout( FlowLayout.CENTER, 20, 5 ) );
119         contentpane.add( buttonjpanel, BorderLayout.SOUTH );
120         setSize( 500, 400 );
121         addWindowListener( new WindowAdapter() {
122
123             @Override
124             public void windowClosing( final WindowEvent e ) {
125                 removeMe();
126             }
127         } );
128         setVisible( true );
129     }
130
131     @Override
132     public void actionPerformed( final ActionEvent e ) {
133         final Object o = e.getSource();
134         if ( o == close_button ) {
135             removeMe();
136         }
137         else if ( o == copy_button ) {
138             copy();
139         }
140     }
141
142     void close() {
143         setVisible( false );
144         dispose();
145     }
146
147     void removeMe() {
148         final int i = _tframes.indexOf( this );
149         if ( i >= 0 ) {
150             _tframes.remove( i );
151         }
152         close();
153     }
154
155     private void copy() {
156         if ( !can_use_clipboard ) {
157             // can't do this!
158             return;
159         }
160         final Clipboard sys_clipboard = getToolkit().getSystemClipboard();
161         final StringSelection contents = new StringSelection( jtextarea.getText() );
162         sys_clipboard.setContents( contents, this );
163     }
164
165     @Override
166     public void lostOwnership( final Clipboard clipboard, final Transferable contents ) {
167     }
168
169     static TextFrame instantiate( final String s, final String title, final LinkedList<TextFrame> tframes ) {
170         return new TextFrame( s, title, tframes );
171     }
172 }