2866b14d305cc0524b0762ab4e5ee821e58b74a8
[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             @Override
85             public void contentsChanged( final ListDataEvent e ) {
86                 final int selection = _selector.getSelectedIndex();
87                 changeDialogColors( selection );
88             }
89
90             @Override
91             public void intervalAdded( final ListDataEvent e ) {
92                 // Not needed.
93             }
94
95             @Override
96             public void intervalRemoved( final ListDataEvent e ) {
97                 // Not needed.
98             }
99         } );
100         select_panel.add( _selector );
101         contentpane.add( select_panel, "North" );
102         // create color panel
103         final int num_colors = TreeColorSet.COLOR_FIELDS.length;
104         _color_panel = new JPanel( new GridLayout( num_colors, 2, 8, 0 ) );
105         final JLabel headings[] = new JLabel[ num_colors ];
106         _color_labels = new JPanel[ num_colors ];
107         for( int i = 0; i < num_colors; i++ ) {
108             headings[ i ] = new JLabel( TreeColorSet.COLOR_FIELDS[ i ] );
109             headings[ i ].setFont( new Font( Configuration.getDefaultFontFamilyName(), Font.PLAIN, 9 ) );
110             headings[ i ].setHorizontalAlignment( SwingConstants.RIGHT );
111             _color_panel.add( headings[ i ] );
112             _color_labels[ i ] = new JPanel();
113             _color_labels[ i ].setPreferredSize( new Dimension( 15, 40 ) );
114             _color_panel.add( _color_labels[ i ] );
115         }
116         contentpane.add( _color_panel, "Center" );
117         setColors( _colorset.getColorSchemes()[ 0 ] );
118         // create button panel
119         final JPanel btn_panel = new JPanel();
120         _ok_btn = new JButton( "OK" );
121         _ok_btn.addActionListener( new ActionListener() {
122
123             @Override
124             public void actionPerformed( final ActionEvent e ) {
125                 ok();
126             }
127         } );
128         btn_panel.add( _ok_btn );
129         _cancel_btn = new JButton( "Cancel" );
130         _cancel_btn.addActionListener( new ActionListener() {
131
132             @Override
133             public void actionPerformed( final ActionEvent e ) {
134                 cancel();
135             }
136         } );
137         btn_panel.add( _cancel_btn );
138         btn_panel.setPreferredSize( new Dimension( 400, 30 ) );
139         getContentPane().add( btn_panel, "South" );
140         setCurrentColor( colorset.getCurrentColorScheme() );
141     }
142
143     @Override
144     public void actionPerformed( final ActionEvent e ) {
145         // Not needed.
146     }
147
148     private void cancel() {
149         _colorset.setColorSchema( _prev_selected_scheme );
150         for( final TreePanel tree_panel : getMainPanel().getTreePanels() ) {
151             tree_panel.setBackground( _colorset.getBackgroundColor() );
152         }
153         redrawTreePanel();
154         setVisible( false );
155         dispose();
156     }
157
158     private void changeDialogColors( final int scheme_index ) {
159         _selected_scheme = scheme_index;
160         setColors( _colorset.getColorSchemes()[ scheme_index ] );
161         _colorset.setColorSchema( getSelectedScheme() );
162         for( final TreePanel tree_panel : getMainPanel().getTreePanels() ) {
163             tree_panel.setBackground( _colorset.getBackgroundColor() );
164         }
165         redrawTreePanel();
166     }
167
168     private MainPanel getMainPanel() {
169         return _main_panel;
170     }
171
172     private int getSelectedScheme() {
173         return _selected_scheme;
174     }
175
176     private void ok() {
177         // set the new color
178         _colorset.setColorSchema( getSelectedScheme() );
179         // close the window
180         setVisible( false );
181         dispose();
182     }
183
184     private void redrawTreePanel() {
185         if ( getMainPanel().getCurrentTreePanel() != null ) {
186             getMainPanel().getCurrentTreePanel().repaint();
187         }
188     }
189
190     private void setColors( final Color colors[] ) {
191         for( int i = 0; i < colors.length; i++ ) {
192             _color_labels[ i ].setBackground( colors[ i ] );
193         }
194     }
195
196     private void setCurrentColor( final int color_index ) {
197         setColors( _colorset.getColorSchemes()[ color_index ] );
198         _selector.setSelectedIndex( color_index );
199     }
200 }