(no commit message)
[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.BorderLayout;
32 import java.awt.Color;
33 import java.awt.Container;
34 import java.awt.FlowLayout;
35 import java.awt.Font;
36 import java.awt.datatransfer.Clipboard;
37 import java.awt.datatransfer.ClipboardOwner;
38 import java.awt.datatransfer.StringSelection;
39 import java.awt.datatransfer.Transferable;
40 import java.awt.event.ActionEvent;
41 import java.awt.event.ActionListener;
42 import java.awt.event.WindowAdapter;
43 import java.awt.event.WindowEvent;
44 import java.util.LinkedList;
45
46 import javax.swing.JButton;
47 import javax.swing.JFrame;
48 import javax.swing.JPanel;
49 import javax.swing.JScrollPane;
50 import javax.swing.JTextArea;
51
52 final class TextFrame extends JFrame implements ActionListener, ClipboardOwner {
53
54     /**
55      *
56      */
57     private static final long           serialVersionUID = -5012834229705518363L;
58     private static Color                ta_text_color    = new Color( 0, 0, 0 ),
59             ta_background_color = new Color( 240, 240, 240 ), background_color = new Color( 215, 215, 215 ),
60             button_background_color = new Color( 215, 215, 215 ), button_text_color = new Color( 0, 0, 0 );
61     private final static Font           button_font      = new Font( "Helvetica", Font.PLAIN, 10 ),
62             ta_font = new Font( "Helvetica", Font.PLAIN, 10 );
63     private boolean                     can_use_clipboard;
64     private final String                text;
65     private final JTextArea             jtextarea;
66     private final JButton               close_button;
67     private JButton                     copy_button;
68     private final JPanel                buttonjpanel;
69     private final Container             contentpane;
70     private final LinkedList<TextFrame> _tframes;
71
72     private TextFrame( final String s, final String title, final LinkedList<TextFrame> tframes ) {
73         // first things first
74         setTitle( title );
75         text = s;
76         _tframes = tframes;
77         // check to see if we have permission to use the clipboard:
78         can_use_clipboard = true;
79         final SecurityManager sm = System.getSecurityManager();
80         if ( sm != null ) {
81             try {
82                 sm.checkSystemClipboardAccess();
83             }
84             catch ( final Exception e ) {
85                 //nope!
86                 can_use_clipboard = false;
87             }
88         }
89         // set up the frame
90         setBackground( background_color );
91         buttonjpanel = new JPanel();
92         buttonjpanel.setBackground( background_color );
93         close_button = new JButton( "          Close          " );
94         close_button.setBackground( button_background_color );
95         close_button.setForeground( button_text_color );
96         close_button.setFont( button_font );
97         close_button.addActionListener( this );
98         buttonjpanel.add( close_button );
99         if ( can_use_clipboard ) {
100             copy_button = new JButton( "Copy to clipboard" );
101             copy_button.setBackground( button_background_color );
102             copy_button.setForeground( button_text_color );
103             copy_button.setFont( button_font );
104             copy_button.addActionListener( this );
105             buttonjpanel.add( copy_button );
106         }
107         contentpane = getContentPane();
108         contentpane.setLayout( new BorderLayout() );
109         jtextarea = new JTextArea( text );
110         jtextarea.setBackground( ta_background_color );
111         jtextarea.setForeground( ta_text_color );
112         jtextarea.setFont( ta_font );
113         jtextarea.setEditable( false );
114         jtextarea.setWrapStyleWord( true );
115         jtextarea.setLineWrap( true );
116         contentpane.add( new JScrollPane( jtextarea ), BorderLayout.CENTER );
117         buttonjpanel.setLayout( new FlowLayout( FlowLayout.CENTER, 20, 5 ) );
118         contentpane.add( buttonjpanel, BorderLayout.SOUTH );
119         setSize( 500, 400 );
120         addWindowListener( new WindowAdapter() {
121
122             @Override
123             public void windowClosing( final WindowEvent e ) {
124                 removeMe();
125             }
126         } );
127         setVisible( true );
128     }
129
130     @Override
131     public void actionPerformed( final ActionEvent e ) {
132         final Object o = e.getSource();
133         if ( o == close_button ) {
134             removeMe();
135         }
136         else if ( o == copy_button ) {
137             copy();
138         }
139     }
140
141     void close() {
142         setVisible( false );
143         dispose();
144     }
145
146     void removeMe() {
147         final int i = _tframes.indexOf( this );
148         if ( i >= 0 ) {
149             _tframes.remove( i );
150         }
151         close();
152     }
153
154     private void copy() {
155         if ( !can_use_clipboard ) {
156             // can't do this!
157             return;
158         }
159         final Clipboard sys_clipboard = getToolkit().getSystemClipboard();
160         final StringSelection contents = new StringSelection( jtextarea.getText() );
161         sys_clipboard.setContents( contents, this );
162     }
163
164     @Override
165     public void lostOwnership( final Clipboard clipboard, final Transferable contents ) {
166     }
167
168     static TextFrame instantiate( final String s, final String title, final LinkedList<TextFrame> tframes ) {
169         return new TextFrame( s, title, tframes );
170     }
171 }