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