initial commit
[jalview.git] / forester / java / src / org / forester / archaeopteryx / ColorSchemeChooser.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.Dimension;
35 import java.awt.Font;
36 import java.awt.GridLayout;
37 import java.awt.event.ActionEvent;
38 import java.awt.event.ActionListener;
39 import java.util.Vector;
40
41 import javax.swing.JButton;
42 import javax.swing.JComboBox;
43 import javax.swing.JDialog;
44 import javax.swing.JLabel;
45 import javax.swing.JPanel;
46 import javax.swing.SwingConstants;
47 import javax.swing.event.ListDataEvent;
48 import javax.swing.event.ListDataListener;
49
50 final class ColorSchemeChooser extends JDialog implements ActionListener {
51
52     private static final long  serialVersionUID = 6150960100859081126L;
53     private final TreeColorSet _colorset;
54     private final JComboBox    _selector;
55     private final JPanel       _color_panel;
56     private final JPanel       _color_labels[];
57     private final JButton      _ok_btn;
58     private final JButton      _cancel_btn;
59     private final MainPanel    _main_panel;
60     private final int          _prev_selected_scheme;
61     private int                _selected_scheme;
62
63     ColorSchemeChooser( final MainPanel parent, final TreeColorSet colorset ) {
64         setName( "Color Scheme Chooser" );
65         setModal( true );
66         _colorset = colorset;
67         _prev_selected_scheme = _colorset.getCurrentColorScheme();
68         _main_panel = parent;
69         setSize( 400, 350 );
70         final Container contentpane = getContentPane();
71         contentpane.setLayout( new BorderLayout( 5, 15 ) );
72         // The scheme selection panel
73         final JPanel select_panel = new JPanel();
74         final JLabel l = new JLabel( "Choose a color scheme:" );
75         select_panel.add( l );
76         final Vector<String> list = new Vector<String>();
77         for( final String element : TreeColorSet.SCHEME_NAMES ) {
78             list.add( element );
79         }
80         _selector = new JComboBox( list );
81         _selector.setMaximumRowCount( list.size() );
82         _selector.getModel().addListDataListener( new ListDataListener() {
83
84             public void contentsChanged( final ListDataEvent e ) {
85                 final int selection = _selector.getSelectedIndex();
86                 changeDialogColors( selection );
87             }
88
89             public void intervalAdded( final ListDataEvent e ) {
90                 // Not needed.
91             }
92
93             public void intervalRemoved( final ListDataEvent e ) {
94                 // Not needed.
95             }
96         } );
97         select_panel.add( _selector );
98         contentpane.add( select_panel, "North" );
99         // create color panel
100         final int num_colors = TreeColorSet.COLOR_FIELDS.length;
101         _color_panel = new JPanel( new GridLayout( num_colors, 2, 8, 0 ) );
102         final JLabel headings[] = new JLabel[ num_colors ];
103         _color_labels = new JPanel[ num_colors ];
104         for( int i = 0; i < num_colors; i++ ) {
105             headings[ i ] = new JLabel( TreeColorSet.COLOR_FIELDS[ i ] );
106             headings[ i ].setFont( new Font( Configuration.getDefaultFontFamilyName(), Font.PLAIN, 9 ) );
107             headings[ i ].setHorizontalAlignment( SwingConstants.RIGHT );
108             _color_panel.add( headings[ i ] );
109             _color_labels[ i ] = new JPanel();
110             _color_labels[ i ].setPreferredSize( new Dimension( 15, 40 ) );
111             _color_panel.add( _color_labels[ i ] );
112         }
113         contentpane.add( _color_panel, "Center" );
114         setColors( _colorset.getColorSchemes()[ 0 ] );
115         // create button panel
116         final JPanel btn_panel = new JPanel();
117         _ok_btn = new JButton( "OK" );
118         _ok_btn.addActionListener( new ActionListener() {
119
120             public void actionPerformed( final ActionEvent e ) {
121                 ok();
122             }
123         } );
124         btn_panel.add( _ok_btn );
125         _cancel_btn = new JButton( "Cancel" );
126         _cancel_btn.addActionListener( new ActionListener() {
127
128             public void actionPerformed( final ActionEvent e ) {
129                 cancel();
130             }
131         } );
132         btn_panel.add( _cancel_btn );
133         btn_panel.setPreferredSize( new Dimension( 400, 30 ) );
134         getContentPane().add( btn_panel, "South" );
135         setCurrentColor( colorset.getCurrentColorScheme() );
136     }
137
138     public void actionPerformed( final ActionEvent e ) {
139         // Not needed.
140     }
141
142     private void cancel() {
143         _colorset.setColorSchema( _prev_selected_scheme );
144         for( final TreePanel tree_panel : getMainPanel().getTreePanels() ) {
145             tree_panel.setBackground( _colorset.getBackgroundColor() );
146         }
147         redrawTreePanel();
148         setVisible( false );
149         dispose();
150     }
151
152     private void changeDialogColors( final int scheme_index ) {
153         _selected_scheme = scheme_index;
154         setColors( _colorset.getColorSchemes()[ scheme_index ] );
155         _colorset.setColorSchema( getSelectedScheme() );
156         for( final TreePanel tree_panel : getMainPanel().getTreePanels() ) {
157             tree_panel.setBackground( _colorset.getBackgroundColor() );
158         }
159         redrawTreePanel();
160     }
161
162     private MainPanel getMainPanel() {
163         return _main_panel;
164     }
165
166     private int getSelectedScheme() {
167         return _selected_scheme;
168     }
169
170     private void ok() {
171         // set the new color
172         _colorset.setColorSchema( getSelectedScheme() );
173         // close the window
174         setVisible( false );
175         dispose();
176     }
177
178     private void redrawTreePanel() {
179         if ( getMainPanel().getCurrentTreePanel() != null ) {
180             getMainPanel().getCurrentTreePanel().repaint();
181         }
182     }
183
184     private void setColors( final Color colors[] ) {
185         for( int i = 0; i < colors.length; i++ ) {
186             _color_labels[ i ].setBackground( colors[ i ] );
187         }
188     }
189
190     private void setCurrentColor( final int color_index ) {
191         setColors( _colorset.getColorSchemes()[ color_index ] );
192         _selector.setSelectedIndex( color_index );
193     }
194 }