package org.minimalj.frontend.swing.toolkit;

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import org.minimalj.frontend.toolkit.ClientToolkit.IContent;
import org.minimalj.frontend.toolkit.ClientToolkit.SwitchContent;

public class SwingSwitchContent extends JPanel implements SwitchContent {
	private static final long serialVersionUID = 1L;
	
	private IContent shownComponent;
	
	public SwingSwitchContent() {
		super(new BorderLayout());
		setInheritsPopupMenu(true);
	}

	@Override
	public void show(IContent c) {
		if (shownComponent != c) {
			JComponent component = (JComponent) c;
			removeAll();
			if (component != null) {
				// TODO the updateComponentTreeUI uses 4-5ms. This should only be done if necessary (if l&f has changed)
				SwingUtilities.updateComponentTreeUI(component);
				add(component, BorderLayout.CENTER);
			}
			refresh(this);
		}
		shownComponent = c;
	}

	private void refresh(Component component) {
		repaint();
		revalidate();
	}

	@Override
	public void requestFocus() {
		if (shownComponent != null) {
			Component component = (Component) shownComponent;
			component.requestFocus();
		}
	}
	
}

@@ -165,44 +165,8 @@
 el;%0D
-%0Aimport javax.swing.SwingUtilities;%0D
 %0A%0D%0Ai
@@ -729,172 +729,8 @@
  %7B%0D%0A
-%09%09%09%09// TODO the updateComponentTreeUI uses 4-5ms. This should only be done if necessary (if l&f has changed)%0D%0A%09%09%09%09SwingUtilities.updateComponentTreeUI(component);%0D%0A
 %09%09%09%09
@@ -767,16 +767,16 @@
 NTER);%0D%0A
+
 %09%09%09%7D%0D%0A%09%09
@@ -824,18 +824,19 @@
 c;%0D%0A%09%7D%0D%0A
+%09
 %0D%0A
-
 %09private
@@ -1079,14 +1079,13 @@
 %09%7D%0D%0A%09%7D%0D%0A
-%09
 %0D%0A%7D%0D%0A

update of UI not necessary any more. The content of page is created always from scratch