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.Dimension;
33 import java.awt.event.AdjustmentEvent;
34 import java.awt.event.AdjustmentListener;
35 import java.awt.event.ComponentEvent;
36 import java.awt.event.ComponentListener;
37 import java.awt.image.BufferedImage;
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.Hashtable;
41 import java.util.List;
45 import javax.swing.JPanel;
46 import javax.swing.JScrollPane;
47 import javax.swing.JTabbedPane;
48 import javax.swing.SwingConstants;
49 import javax.swing.event.ChangeEvent;
50 import javax.swing.event.ChangeListener;
52 import org.forester.archaeopteryx.phylogeny.data.RenderableDomainArchitecture;
53 import org.forester.phylogeny.Phylogeny;
54 import org.forester.util.ForesterUtil;
56 public class MainPanel extends JPanel implements ComponentListener {
58 private static final long serialVersionUID = -2682765312661416435L;
60 List<TreePanel> _treepanels;
61 ControlPanel _control_panel;
62 private List<JScrollPane> _treegraphic_scroll_panes;
63 private List<JPanel> _treegraphic_scroll_pane_panels;
64 Configuration _configuration;
65 private JTabbedPane _tabbed_pane;
66 private TreeColorSet _colorset;
67 private TreeFontSet _fontset;
68 private Phylogeny _cut_or_copied_tree;
69 private Set<Integer> _copied_and_pasted_nodes;
70 private Hashtable<String, BufferedImage> _image_map;
71 private static Map<String, String> _lineage_to_rank_map;
76 public MainPanel( final Configuration configuration, final MainFrame parent ) {
77 if ( configuration == null ) {
78 throw new IllegalArgumentException( "configuration is null" );
80 addComponentListener( this );
81 _configuration = configuration;
83 _treepanels = new ArrayList<TreePanel>();
85 _control_panel = new ControlPanel( this, configuration );
86 add( _control_panel, BorderLayout.WEST );
87 setupTreeGraphic( configuration, getControlPanel() );
88 getControlPanel().showWhole();
91 public void addPhylogenyInNewTab( final Phylogeny phy,
92 final Configuration config,
93 final String default_name,
94 final String full_path ) {
95 final TreePanel treepanel = new TreePanel( phy, config, this );
96 getControlPanel().phylogenyAdded( config );
97 treepanel.setControlPanel( getControlPanel() );
98 _treepanels.add( treepanel );
100 if ( !ForesterUtil.isEmpty( phy.getName() ) ) {
101 name = phy.getName();
103 else if ( phy.getIdentifier() != null ) {
104 name = phy.getIdentifier().toString();
106 else if ( !ForesterUtil.isEmpty( default_name ) ) {
110 name = "[" + ( getTabbedPane().getTabCount() + 1 ) + "]";
112 final JScrollPane treegraphic_scroll_pane = new JScrollPane( treepanel );
113 treegraphic_scroll_pane.getHorizontalScrollBar().addAdjustmentListener( new AdjustmentListener() {
116 public void adjustmentValueChanged( final AdjustmentEvent e ) {
117 if ( treepanel.isOvOn() || getOptions().isShowScale() ) {
122 treegraphic_scroll_pane.getVerticalScrollBar().addAdjustmentListener( new AdjustmentListener() {
125 public void adjustmentValueChanged( final AdjustmentEvent e ) {
126 if ( treepanel.isOvOn() || getOptions().isShowScale() ) {
128 //System.out.println( e.getValue() );
132 treegraphic_scroll_pane.getHorizontalScrollBar().setUnitIncrement( 10 );
133 treegraphic_scroll_pane.getHorizontalScrollBar().setBlockIncrement( 200 );
134 treegraphic_scroll_pane.getVerticalScrollBar().setUnitIncrement( 10 );
135 treegraphic_scroll_pane.getVerticalScrollBar().setBlockIncrement( 200 );
136 final JPanel treegraphic_scroll_pane_panel = new JPanel();
137 treegraphic_scroll_pane_panel.setLayout( new BorderLayout() );
138 treegraphic_scroll_pane_panel.add( treegraphic_scroll_pane, BorderLayout.CENTER );
139 _treegraphic_scroll_pane_panels.add( treegraphic_scroll_pane_panel );
140 _treegraphic_scroll_panes.add( treegraphic_scroll_pane );
141 getTabbedPane().addTab( name, null, treegraphic_scroll_pane_panel, "" );
142 getTabbedPane().setSelectedIndex( getTabbedPane().getTabCount() - 1 );
143 getControlPanel().showWhole();
146 void addPhylogenyInPanel( final Phylogeny phy, final Configuration config ) {
147 final TreePanel treepanel = new TreePanel( phy, config, this );
148 getControlPanel().phylogenyAdded( config );
149 treepanel.setControlPanel( getControlPanel() );
150 _treepanels.add( treepanel );
151 final JScrollPane treegraphic_scroll_pane = new JScrollPane( treepanel );
152 treegraphic_scroll_pane.getHorizontalScrollBar().addAdjustmentListener( new AdjustmentListener() {
155 public void adjustmentValueChanged( final AdjustmentEvent e ) {
156 if ( treepanel.isOvOn() || getOptions().isShowScale() ) {
161 treegraphic_scroll_pane.getVerticalScrollBar().addAdjustmentListener( new AdjustmentListener() {
164 public void adjustmentValueChanged( final AdjustmentEvent e ) {
165 if ( treepanel.isOvOn() || getOptions().isShowScale() ) {
167 //System.out.println( e.getValue() );
171 treegraphic_scroll_pane.getHorizontalScrollBar().setUnitIncrement( 20 );
172 treegraphic_scroll_pane.getHorizontalScrollBar().setBlockIncrement( 50 );
173 treegraphic_scroll_pane.getVerticalScrollBar().setUnitIncrement( 20 );
174 treegraphic_scroll_pane.getVerticalScrollBar().setBlockIncrement( 50 );
175 final JPanel treegraphic_scroll_pane_panel = new JPanel();
176 treegraphic_scroll_pane_panel.setLayout( new BorderLayout() );
177 treegraphic_scroll_pane_panel.add( treegraphic_scroll_pane, BorderLayout.CENTER );
178 _treegraphic_scroll_pane_panels.add( treegraphic_scroll_pane_panel );
179 _treegraphic_scroll_panes.add( treegraphic_scroll_pane );
180 add( treegraphic_scroll_pane_panel, BorderLayout.CENTER );
183 void adjustJScrollPane() {
184 if ( getTabbedPane() != null ) {
185 getCurrentScrollPanePanel().remove( getCurrentScrollPane() );
186 getCurrentScrollPanePanel().add( getCurrentScrollPane(), BorderLayout.CENTER );
188 getCurrentScrollPane().revalidate();
191 void closeCurrentPane() {
192 final int index = getCurrentTabIndex();
193 if ( ( index >= 0 ) && ( getTabbedPane().getTabCount() > 0 ) ) {
194 getTabbedPane().remove( index );
195 getTreePanels().remove( index );
196 _treegraphic_scroll_panes.remove( index );
197 _treegraphic_scroll_pane_panels.remove( index );
198 getControlPanel().phylogenyRemoved( index );
203 public void componentHidden( final ComponentEvent e ) {
208 public void componentMoved( final ComponentEvent e ) {
213 public void componentResized( final ComponentEvent e ) {
214 if ( getCurrentTreePanel() != null ) {
215 getCurrentTreePanel().updateOvSettings();
216 getCurrentTreePanel().updateOvSizes();
221 public void componentShown( final ComponentEvent e ) {
225 private Configuration getConfiguration() {
226 return _configuration;
229 public ControlPanel getControlPanel() {
230 return _control_panel;
233 public Set<Integer> getCopiedAndPastedNodes() {
234 return _copied_and_pasted_nodes;
237 Phylogeny getCurrentPhylogeny() {
238 if ( getCurrentTreePanel() == null ) {
241 return getCurrentTreePanel().getPhylogeny();
244 JScrollPane getCurrentScrollPane() {
245 if ( _treegraphic_scroll_panes.size() > 0 ) {
246 final int selected = getTabbedPane().getSelectedIndex();
247 if ( selected >= 0 ) {
248 return _treegraphic_scroll_panes.get( selected );
251 return _treegraphic_scroll_panes.get( 0 );
259 JPanel getCurrentScrollPanePanel() {
260 final int selected = getTabbedPane().getSelectedIndex();
261 if ( selected >= 0 ) {
262 return _treegraphic_scroll_pane_panels.get( selected );
265 return _treegraphic_scroll_pane_panels.get( 0 );
269 int getCurrentTabIndex() {
270 final int selected = getTabbedPane().getSelectedIndex();
271 if ( selected >= 0 ) {
279 void setTitleOfSelectedTab( final String title ) {
280 final int selected = getTabbedPane().getSelectedIndex();
281 if ( selected >= 0 ) {
282 getTabbedPane().setTitleAt( selected, title );
286 public TreePanel getCurrentTreePanel() {
287 final int selected = getTabbedPane().getSelectedIndex();
288 if ( selected >= 0 ) {
289 return _treepanels.get( selected );
292 if ( _treepanels.size() == 1 ) {
293 return _treepanels.get( 0 );
301 Phylogeny getCutOrCopiedTree() {
302 return _cut_or_copied_tree;
305 MainFrame getMainFrame() {
309 public Options getOptions() {
310 return _mainframe.getOptions();
313 Phylogeny getPhylogeny( final int index ) {
314 if ( getCurrentTreePanel() == null ) {
317 return _treepanels.get( index ).getPhylogeny();
320 Dimension getSizeOfViewport() {
321 return getCurrentScrollPane().getViewport().getExtentSize();
324 public JTabbedPane getTabbedPane() {
328 TreeColorSet getTreeColorSet() {
332 public TreeFontSet getTreeFontSet() {
336 List<TreePanel> getTreePanels() {
341 if ( !getConfiguration().isUseNativeUI() ) {
342 setBackground( getConfiguration().getGuiBackgroundColor() );
344 setTreeFontSet( new TreeFontSet( this ) );
345 getTreeFontSet().setBaseFont( getOptions().getBaseFont() );
346 setLayout( new BorderLayout() );
347 setTreeColorSet( TreeColorSet.createInstance( getConfiguration() ) );
348 _treegraphic_scroll_panes = new ArrayList<JScrollPane>();
349 _treegraphic_scroll_pane_panels = new ArrayList<JPanel>();
350 _tabbed_pane = new JTabbedPane( SwingConstants.TOP );
351 if ( !getConfiguration().isUseNativeUI() ) {
352 _tabbed_pane.setBackground( getConfiguration().getGuiBackgroundColor() );
353 _tabbed_pane.setForeground( getConfiguration().getGuiBackgroundColor() );
355 _tabbed_pane.addChangeListener( new ChangeListener() {
357 // This method is called whenever the selected tab changes
359 public void stateChanged( final ChangeEvent evt ) {
360 final JTabbedPane pane = ( JTabbedPane ) evt.getSource();
361 getControlPanel().tabChanged();
363 final int sel = pane.getSelectedIndex();
365 if ( !getConfiguration().isUseNativeUI() ) {
366 if ( _tabbed_pane.getTabCount() > 0 ) {
367 _tabbed_pane.setForegroundAt( sel, Constants.TAB_LABEL_FOREGROUND_COLOR_SELECTED );
368 for( int i = 0; i < _tabbed_pane.getTabCount(); ++i ) {
370 _tabbed_pane.setBackgroundAt( i, getConfiguration().getGuiBackgroundColor() );
371 _tabbed_pane.setForegroundAt( i, getConfiguration().getGuiCheckboxTextColor() );
379 if ( !getConfiguration().isUseNativeUI() ) {
380 _tabbed_pane.setFont( ControlPanel.jcb_font );
382 _tabbed_pane.setTabLayoutPolicy( JTabbedPane.SCROLL_TAB_LAYOUT );
383 add( _tabbed_pane, BorderLayout.CENTER );
386 public void setArrowCursor() {
387 setCursor( TreePanel.ARROW_CURSOR );
391 public void setCopiedAndPastedNodes( final Set<Integer> node_ids ) {
392 _copied_and_pasted_nodes = node_ids;
395 void setCutOrCopiedTree( final Phylogeny cut_or_copied_tree ) {
396 _cut_or_copied_tree = cut_or_copied_tree;
399 void setTreeColorSet( final TreeColorSet colorset ) {
400 _colorset = colorset;
401 for( final TreePanel p : getTreePanels() ) {
402 p.setBackground( colorset.getBackgroundColor() );
406 void setTreeFontSet( final TreeFontSet fontset ) {
410 void setupTreeGraphic( final Configuration config_settings, final ControlPanel control ) {
411 control.setSpeciesColors( config_settings.getSpeciesColors() );
412 control.setAnnotationColors( config_settings.getAnnotationColors() );
413 RenderableDomainArchitecture.setColorMap( config_settings.getDomainColors() );
416 public void setWaitCursor() {
417 setCursor( TreePanel.WAIT_CURSOR );
422 for( final TreePanel atvtreepanel : _treepanels ) {
423 atvtreepanel.removeAllEditNodeJFrames();
427 synchronized void setImageMap( final Hashtable<String, BufferedImage> image_map ) {
428 _image_map = image_map;
431 synchronized Hashtable<String, BufferedImage> getImageMap() {
435 public synchronized static Map<String, String> getLineageToRankMap() {
436 if ( _lineage_to_rank_map == null ) {
437 _lineage_to_rank_map = new HashMap<String, String>();
439 return _lineage_to_rank_map;