2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
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
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.
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.
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
26 // Contact: phylosoft @ gmail . com
27 // WWW: www.phylosoft.org/forester
29 package org.forester.archaeopteryx;
31 import java.awt.BorderLayout;
32 import java.awt.Color;
33 import java.awt.Container;
34 import java.awt.Dimension;
36 import java.awt.GridLayout;
37 import java.awt.event.ActionEvent;
38 import java.awt.event.ActionListener;
39 import java.util.Vector;
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;
50 final class ColorSchemeChooser extends JDialog implements ActionListener {
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;
63 ColorSchemeChooser( final MainPanel parent, final TreeColorSet colorset ) {
64 setName( "Color Scheme Chooser" );
67 _prev_selected_scheme = _colorset.getCurrentColorScheme();
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 ) {
80 _selector = new JComboBox( list );
81 _selector.setMaximumRowCount( list.size() );
82 _selector.getModel().addListDataListener( new ListDataListener() {
85 public void contentsChanged( final ListDataEvent e ) {
86 final int selection = _selector.getSelectedIndex();
87 changeDialogColors( selection );
91 public void intervalAdded( final ListDataEvent e ) {
96 public void intervalRemoved( final ListDataEvent e ) {
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 ] );
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() {
124 public void actionPerformed( final ActionEvent e ) {
128 btn_panel.add( _ok_btn );
129 _cancel_btn = new JButton( "Cancel" );
130 _cancel_btn.addActionListener( new ActionListener() {
133 public void actionPerformed( final ActionEvent e ) {
137 btn_panel.add( _cancel_btn );
138 btn_panel.setPreferredSize( new Dimension( 400, 30 ) );
139 getContentPane().add( btn_panel, "South" );
140 setCurrentColor( colorset.getCurrentColorScheme() );
144 public void actionPerformed( final ActionEvent e ) {
148 private void cancel() {
149 _colorset.setColorSchema( _prev_selected_scheme );
150 for( final TreePanel tree_panel : getMainPanel().getTreePanels() ) {
151 tree_panel.setBackground( _colorset.getBackgroundColor() );
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() );
168 private MainPanel getMainPanel() {
172 private int getSelectedScheme() {
173 return _selected_scheme;
178 _colorset.setColorSchema( getSelectedScheme() );
184 private void redrawTreePanel() {
185 if ( getMainPanel().getCurrentTreePanel() != null ) {
186 getMainPanel().getCurrentTreePanel().repaint();
190 private void setColors( final Color colors[] ) {
191 for( int i = 0; i < colors.length; i++ ) {
192 _color_labels[ i ].setBackground( colors[ i ] );
196 private void setCurrentColor( final int color_index ) {
197 setColors( _colorset.getColorSchemes()[ color_index ] );
198 _selector.setSelectedIndex( color_index );