/*
 * Copyright 2002-2006 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.springframework.richclient.dialog.control;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JTabbedPane;

import org.springframework.richclient.factory.ControlFactory;

/**
 * Wrapper around <code>JTabbedPane</code>. When a <code>Tab</code> is made invisible, the tab is hidden from the ui, and
 * vice versa.
 * <p>
 * TODO: move this to another package?
 * 
 * @author Peter De Bruycker
 */
public class ExtTabbedPane implements ControlFactory {

	private List tabs = new ArrayList();

	private JTabbedPane tabbedPane;

	private PropertyChangeListener propertyChangeHandler = new PropertyChangeListener() {
		public void propertyChange(PropertyChangeEvent evt) {
			Tab tab = (Tab) evt.getSource();
			int index = tabs.indexOf(tab);

			if (evt.getPropertyName().equals("title")) {
				tabbedPane.setTitleAt(index, tab.getTitle());
			}
			if (evt.getPropertyName().equals("tooltip")) {
				tabbedPane.setToolTipTextAt(index, tab.getTooltip());
			}
			if (evt.getPropertyName().equals("icon")) {
				tabbedPane.setIconAt(index, tab.getIcon());
			}
			if (evt.getPropertyName().equals("component")) {
				tabbedPane.setComponentAt(index, tab.getComponent());
			}
			if (evt.getPropertyName().equals("mnemonic")) {
				tabbedPane.setMnemonicAt(index, tab.getMnemonic());
			}
			if (evt.getPropertyName().equals("visible")) {
				if (tab.isVisible()) {
					tabbedPane.insertTab(tab.getTitle(), tab.getIcon(), tab.getComponent(), tab.getTooltip(),
							determineUIIndex(tab));
				}
				else {
					tabbedPane.removeTabAt(determineUIIndex(tab));
				}
			}
		}
	};

	public ExtTabbedPane(JTabbedPane tabbedPane) {
		this.tabbedPane = tabbedPane;
	}

	public ExtTabbedPane() {
		this(new JTabbedPane());
	}

	public JComponent getControl() {
		return tabbedPane;
	}

	public void addTab(Tab tab) {
		if (tab.isVisible()) {
			tabbedPane.addTab(tab.getTitle(), tab.getIcon(), tab.getComponent(), tab.getTooltip());
		}
		tabs.add(tab);
		tab.addPropertyChangeListener(propertyChangeHandler);
	}

	public void removeTab(Tab tab) {
		removeTab(tabs.indexOf(tab));
	}

	public void addTab(int index, Tab tab) {
		if (tab.isVisible()) {
			tabbedPane.insertTab(tab.getTitle(), tab.getIcon(), tab.getComponent(), tab.getTooltip(), index);
		}
		tabs.add(index, tab);
		tab.addPropertyChangeListener(propertyChangeHandler);
	}

	public void removeTab(int index) {
		tabbedPane.removeTabAt(index);
		Tab tab = (Tab) tabs.remove(index);
		tab.removePropertyChangeListener(propertyChangeHandler);
	}

	public void selectTab(Tab tab) {
		if(tab.isVisible()) {
			int targetIndex = determineUIIndex(tab);
			if (targetIndex >= 0) {
				tabbedPane.setSelectedIndex(targetIndex);
			}
		}
	}

	private int determineUIIndex(Tab tab) {
		int result = 0;

		int index = tabs.indexOf(tab);
		for (int i = 0; i < index; i++) {
			Tab t = (Tab) tabs.get(i);
			if (t.isVisible()) {
				result++;
			}
		}

		return result;
	}

}

@@ -1508,43 +1508,96 @@
 );%0D%0A
-%09%09%09int index = tabs.i
+            if (tab.isVisible()) %7B%0D%0A                int index = determineUII
 ndex
-Of
 (tab);%0D%0A
 %0D%0A%09%09
@@ -1592,21 +1592,71 @@
 (tab);%0D%0A
-%0D%0A%09%09%09
+                if (index %3E= 0) %7B%0D%0A                    
 if (evt.
@@ -1689,28 +1689,48 @@
 title%22)) %7B%0D%0A
-%09%09%09%09
+                        
 tabbedPane.s
@@ -1760,33 +1760,67 @@
 tTitle());%0D%0A
-%09%09%09%7D%0D%0A%09%09%09
+                    %7D%0D%0A                    
 if (evt.getP
@@ -1851,28 +1851,48 @@
 oltip%22)) %7B%0D%0A
-%09%09%09%09
+                        
 tabbedPane.s
@@ -1930,33 +1930,67 @@
 ooltip());%0D%0A
-%09%09%09%7D%0D%0A%09%09%09
+                    %7D%0D%0A                    
 if (evt.getP
@@ -2018,28 +2018,48 @@
 %22icon%22)) %7B%0D%0A
-%09%09%09%09
+                        
 tabbedPane.s
@@ -2087,33 +2087,67 @@
 etIcon());%0D%0A
-%09%09%09%7D%0D%0A%09%09%09
+                    %7D%0D%0A                    
 if (evt.getP
@@ -2180,28 +2180,48 @@
 onent%22)) %7B%0D%0A
-%09%09%09%09
+                        
 tabbedPane.s
@@ -2259,33 +2259,67 @@
 ponent());%0D%0A
-%09%09%09%7D%0D%0A%09%09%09
+                    %7D%0D%0A                    
 if (evt.getP
@@ -2351,28 +2351,48 @@
 monic%22)) %7B%0D%0A
-%09%09%09%09
+                        
 tabbedPane.s
@@ -2428,27 +2428,78 @@
 emonic());%0D%0A
-%09%09%09
+                    %7D%0D%0A                %7D%0D%0A            
 %7D%0D%0A%09%09%09if (ev

change for RCP-490: only update tab properties when tab is visible