update tab title
[jalview.git] / forester / java / src / org / forester / archaeopteryx / MainPanel.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.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.Hashtable;
40 import java.util.List;
41 import java.util.Set;
42
43 import javax.swing.JPanel;
44 import javax.swing.JScrollPane;
45 import javax.swing.JTabbedPane;
46 import javax.swing.SwingConstants;
47 import javax.swing.event.ChangeEvent;
48 import javax.swing.event.ChangeListener;
49
50 import org.forester.archaeopteryx.phylogeny.data.RenderableDomainArchitecture;
51 import org.forester.phylogeny.Phylogeny;
52 import org.forester.util.ForesterUtil;
53
54 public class MainPanel extends JPanel implements ComponentListener {
55
56     private static final long                serialVersionUID = -2682765312661416435L;
57     MainFrame                                _mainframe;
58     List<TreePanel>                          _treepanels;
59     ControlPanel                             _control_panel;
60     private List<JScrollPane>                _treegraphic_scroll_panes;
61     private List<JPanel>                     _treegraphic_scroll_pane_panels;
62     Configuration                            _configuration;
63     private JTabbedPane                      _tabbed_pane;
64     private TreeColorSet                     _colorset;
65     private TreeFontSet                      _fontset;
66     private Phylogeny                        _cut_or_copied_tree;
67     private Set<Integer>                     _copied_and_pasted_nodes;
68     private Hashtable<String, BufferedImage> _image_map;
69
70     MainPanel() {
71     }
72
73     MainPanel( final Configuration configuration, final MainFrame parent ) {
74         if ( configuration == null ) {
75             throw new IllegalArgumentException( "configuration is null" );
76         }
77         addComponentListener( this );
78         _configuration = configuration;
79         _mainframe = parent;
80         _treepanels = new ArrayList<TreePanel>();
81         initialize();
82         _control_panel = new ControlPanel( this, configuration );
83         add( _control_panel, BorderLayout.WEST );
84         setupTreeGraphic( configuration, getControlPanel() );
85         getControlPanel().showWhole();
86     }
87
88     void addPhylogenyInNewTab( final Phylogeny phy,
89                                final Configuration config,
90                                final String default_name,
91                                final String full_path ) {
92         final TreePanel treepanel = new TreePanel( phy, config, this );
93         getControlPanel().phylogenyAdded( config );
94         treepanel.setControlPanel( getControlPanel() );
95         _treepanels.add( treepanel );
96         String name = "";
97         //  if ( !ForesterUtil.isEmpty( phy.getName() ) ) {
98         //      name = phy.getName();
99         //  }
100         //  else if ( phy.getIdentifier() != null ) {
101         //      name = phy.getIdentifier().toString();
102         //  }
103         /* else */
104         if ( !ForesterUtil.isEmpty( default_name ) ) {
105             name = default_name;
106         }
107         else {
108             name = "[" + ( getTabbedPane().getTabCount() + 1 ) + "]";
109         }
110         final JScrollPane treegraphic_scroll_pane = new JScrollPane( treepanel );
111         treegraphic_scroll_pane.getHorizontalScrollBar().addAdjustmentListener( new AdjustmentListener() {
112
113             @Override
114             public void adjustmentValueChanged( final AdjustmentEvent e ) {
115                 if ( treepanel.isOvOn() || getOptions().isShowScale() ) {
116                     treepanel.repaint();
117                 }
118             }
119         } );
120         treegraphic_scroll_pane.getVerticalScrollBar().addAdjustmentListener( new AdjustmentListener() {
121
122             @Override
123             public void adjustmentValueChanged( final AdjustmentEvent e ) {
124                 if ( treepanel.isOvOn() || getOptions().isShowScale() ) {
125                     treepanel.repaint();
126                     //System.out.println( e.getValue() );
127                 }
128             }
129         } );
130         treegraphic_scroll_pane.getHorizontalScrollBar().setUnitIncrement( 10 );
131         treegraphic_scroll_pane.getHorizontalScrollBar().setBlockIncrement( 200 );
132         treegraphic_scroll_pane.getVerticalScrollBar().setUnitIncrement( 10 );
133         treegraphic_scroll_pane.getVerticalScrollBar().setBlockIncrement( 200 );
134         final JPanel treegraphic_scroll_pane_panel = new JPanel();
135         treegraphic_scroll_pane_panel.setLayout( new BorderLayout() );
136         treegraphic_scroll_pane_panel.add( treegraphic_scroll_pane, BorderLayout.CENTER );
137         _treegraphic_scroll_pane_panels.add( treegraphic_scroll_pane_panel );
138         _treegraphic_scroll_panes.add( treegraphic_scroll_pane );
139         getTabbedPane().addTab( name,
140                                 null,
141                                 treegraphic_scroll_pane_panel,
142                                 Util.createDescriptionForTab( phy, full_path ) );
143         getTabbedPane().setSelectedIndex( getTabbedPane().getTabCount() - 1 );
144         getControlPanel().showWhole();
145     }
146
147     void addPhylogenyInPanel( final Phylogeny phy, final Configuration config ) {
148         final TreePanel treepanel = new TreePanel( phy, config, this );
149         getControlPanel().phylogenyAdded( config );
150         treepanel.setControlPanel( getControlPanel() );
151         _treepanels.add( treepanel );
152         final JScrollPane treegraphic_scroll_pane = new JScrollPane( treepanel );
153         treegraphic_scroll_pane.getHorizontalScrollBar().setUnitIncrement( 20 );
154         treegraphic_scroll_pane.getHorizontalScrollBar().setBlockIncrement( 50 );
155         treegraphic_scroll_pane.getVerticalScrollBar().setUnitIncrement( 20 );
156         treegraphic_scroll_pane.getVerticalScrollBar().setBlockIncrement( 50 );
157         final JPanel treegraphic_scroll_pane_panel = new JPanel();
158         treegraphic_scroll_pane_panel.setLayout( new BorderLayout() );
159         treegraphic_scroll_pane_panel.add( treegraphic_scroll_pane, BorderLayout.CENTER );
160         _treegraphic_scroll_pane_panels.add( treegraphic_scroll_pane_panel );
161         _treegraphic_scroll_panes.add( treegraphic_scroll_pane );
162         add( treegraphic_scroll_pane_panel, BorderLayout.CENTER );
163     }
164
165     void adjustJScrollPane() {
166         if ( getTabbedPane() != null ) {
167             getCurrentScrollPanePanel().remove( getCurrentScrollPane() );
168             getCurrentScrollPanePanel().add( getCurrentScrollPane(), BorderLayout.CENTER );
169         }
170         getCurrentScrollPane().revalidate();
171     }
172
173     void closeCurrentPane() {
174         final int index = getCurrentTabIndex();
175         if ( ( index >= 0 ) && ( getTabbedPane().getTabCount() > 0 ) ) {
176             getTabbedPane().remove( index );
177             getTreePanels().remove( index );
178             _treegraphic_scroll_panes.remove( index );
179             _treegraphic_scroll_pane_panels.remove( index );
180             getControlPanel().phylogenyRemoved( index );
181         }
182     }
183
184     @Override
185     public void componentHidden( final ComponentEvent e ) {
186         // Do nothing.
187     }
188
189     @Override
190     public void componentMoved( final ComponentEvent e ) {
191         // Do nothing.
192     }
193
194     @Override
195     public void componentResized( final ComponentEvent e ) {
196         if ( getCurrentTreePanel() != null ) {
197             getCurrentTreePanel().updateOvSettings();
198             getCurrentTreePanel().updateOvSizes();
199         }
200     }
201
202     @Override
203     public void componentShown( final ComponentEvent e ) {
204         // Do nothing.
205     }
206
207     private Configuration getConfiguration() {
208         return _configuration;
209     }
210
211     ControlPanel getControlPanel() {
212         return _control_panel;
213     }
214
215     public Set<Integer> getCopiedAndPastedNodes() {
216         return _copied_and_pasted_nodes;
217     }
218
219     Phylogeny getCurrentPhylogeny() {
220         if ( getCurrentTreePanel() == null ) {
221             return null;
222         }
223         return getCurrentTreePanel().getPhylogeny();
224     }
225
226     JScrollPane getCurrentScrollPane() {
227         if ( _treegraphic_scroll_panes.size() > 0 ) {
228             final int selected = getTabbedPane().getSelectedIndex();
229             if ( selected >= 0 ) {
230                 return _treegraphic_scroll_panes.get( selected );
231             }
232             else {
233                 return _treegraphic_scroll_panes.get( 0 );
234             }
235         }
236         else {
237             return null;
238         }
239     }
240
241     JPanel getCurrentScrollPanePanel() {
242         final int selected = getTabbedPane().getSelectedIndex();
243         if ( selected >= 0 ) {
244             return _treegraphic_scroll_pane_panels.get( selected );
245         }
246         else {
247             return _treegraphic_scroll_pane_panels.get( 0 );
248         }
249     }
250
251     int getCurrentTabIndex() {
252         final int selected = getTabbedPane().getSelectedIndex();
253         if ( selected >= 0 ) {
254             return selected;
255         }
256         else {
257             return 0;
258         }
259     }
260
261     void setTitleOfSelectedTab( final String title ) {
262         final int selected = getTabbedPane().getSelectedIndex();
263         if ( selected >= 0 ) {
264             getTabbedPane().setTitleAt( selected, title );
265         }
266     }
267
268     TreePanel getCurrentTreePanel() {
269         final int selected = getTabbedPane().getSelectedIndex();
270         if ( selected >= 0 ) {
271             return _treepanels.get( selected );
272         }
273         else {
274             if ( _treepanels.size() == 1 ) {
275                 return _treepanels.get( 0 );
276             }
277             else {
278                 return null;
279             }
280         }
281     }
282
283     Phylogeny getCutOrCopiedTree() {
284         return _cut_or_copied_tree;
285     }
286
287     MainFrame getMainFrame() {
288         return _mainframe;
289     }
290
291     public Options getOptions() {
292         return _mainframe.getOptions();
293     }
294
295     Phylogeny getPhylogeny( final int index ) {
296         if ( getCurrentTreePanel() == null ) {
297             return null;
298         }
299         return _treepanels.get( index ).getPhylogeny();
300     }
301
302     Dimension getSizeOfViewport() {
303         return getCurrentScrollPane().getViewport().getExtentSize();
304     }
305
306     JTabbedPane getTabbedPane() {
307         return _tabbed_pane;
308     }
309
310     TreeColorSet getTreeColorSet() {
311         return _colorset;
312     }
313
314     public TreeFontSet getTreeFontSet() {
315         return _fontset;
316     }
317
318     List<TreePanel> getTreePanels() {
319         return _treepanels;
320     }
321
322     void initialize() {
323         if ( !getConfiguration().isUseNativeUI() ) {
324             setBackground( getConfiguration().getGuiBackgroundColor() );
325         }
326         setTreeFontSet( new TreeFontSet( this ) );
327         getTreeFontSet().setBaseFont( getOptions().getBaseFont() );
328         setLayout( new BorderLayout() );
329         setTreeColorSet( TreeColorSet.createInstance( getConfiguration() ) );
330         _treegraphic_scroll_panes = new ArrayList<JScrollPane>();
331         _treegraphic_scroll_pane_panels = new ArrayList<JPanel>();
332         _tabbed_pane = new JTabbedPane( SwingConstants.TOP );
333         if ( !getConfiguration().isUseNativeUI() ) {
334             _tabbed_pane.setBackground( getConfiguration().getGuiBackgroundColor() );
335             _tabbed_pane.setForeground( getConfiguration().getGuiBackgroundColor() );
336         }
337         _tabbed_pane.addChangeListener( new ChangeListener() {
338
339             // This method is called whenever the selected tab changes
340             @Override
341             public void stateChanged( final ChangeEvent evt ) {
342                 final JTabbedPane pane = ( JTabbedPane ) evt.getSource();
343                 getControlPanel().tabChanged();
344                 // Get current tab
345                 final int sel = pane.getSelectedIndex();
346                 if ( sel >= 0 ) {
347                     if ( !getConfiguration().isUseNativeUI() ) {
348                         if ( _tabbed_pane.getTabCount() > 0 ) {
349                             _tabbed_pane.setForegroundAt( sel, Constants.TAB_LABEL_FOREGROUND_COLOR_SELECTED );
350                             for( int i = 0; i < _tabbed_pane.getTabCount(); ++i ) {
351                                 if ( i != sel ) {
352                                     _tabbed_pane.setBackgroundAt( i, getConfiguration().getGuiBackgroundColor() );
353                                     _tabbed_pane.setForegroundAt( i, getConfiguration().getGuiCheckboxTextColor() );
354                                 }
355                             }
356                         }
357                     }
358                 }
359             }
360         } );
361         if ( !getConfiguration().isUseNativeUI() ) {
362             _tabbed_pane.setFont( ControlPanel.jcb_font );
363         }
364         _tabbed_pane.setTabLayoutPolicy( JTabbedPane.SCROLL_TAB_LAYOUT );
365         add( _tabbed_pane, BorderLayout.CENTER );
366     }
367
368     public void setArrowCursor() {
369         setCursor( TreePanel.ARROW_CURSOR );
370         repaint();
371     }
372
373     public void setCopiedAndPastedNodes( final Set<Integer> node_ids ) {
374         _copied_and_pasted_nodes = node_ids;
375     }
376
377     void setCutOrCopiedTree( final Phylogeny cut_or_copied_tree ) {
378         _cut_or_copied_tree = cut_or_copied_tree;
379     }
380
381     void setTreeColorSet( final TreeColorSet colorset ) {
382         _colorset = colorset;
383         for( final TreePanel p : getTreePanels() ) {
384             p.setBackground( colorset.getBackgroundColor() );
385         }
386     }
387
388     void setTreeFontSet( final TreeFontSet fontset ) {
389         _fontset = fontset;
390     }
391
392     void setupTreeGraphic( final Configuration config_settings, final ControlPanel control ) {
393         control.setSpeciesColors( config_settings.getSpeciesColors() );
394         control.setAnnotationColors( config_settings.getAnnotationColors() );
395         RenderableDomainArchitecture.setColorMap( config_settings.getDomainColors() );
396     }
397
398     public void setWaitCursor() {
399         setCursor( TreePanel.WAIT_CURSOR );
400         repaint();
401     }
402
403     void terminate() {
404         for( final TreePanel atvtreepanel : _treepanels ) {
405             atvtreepanel.removeAllEditNodeJFrames();
406         }
407     }
408
409     synchronized void setImageMap( final Hashtable<String, BufferedImage> image_map ) {
410         _image_map = image_map;
411     }
412
413     synchronized Hashtable<String, BufferedImage> getImageMap() {
414         return _image_map;
415     }
416 }