DynaTabbedPane.java :  » Web-Framework » roma-webwizard » org » romaframework » aspect » view » echo2 » component » Java Open Source

Java Open Source » Web Framework » roma webwizard 
roma webwizard » org » romaframework » aspect » view » echo2 » component » DynaTabbedPane.java
/*
 * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
 * 
 * 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.romaframework.aspect.view.echo2.component;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.HashMap;
import java.util.Map;

import nextapp.echo2.app.Border;
import nextapp.echo2.app.Color;
import nextapp.echo2.app.Component;
import nextapp.echo2.app.Insets;
import nextapp.echo2.app.Label;
import nextapp.echo2.app.Style;
import nextapp.echo2.app.event.ChangeListener;

import org.romaframework.aspect.view.ViewCallback;
import org.romaframework.aspect.view.echo2.form.EntityForm;
import org.romaframework.aspect.view.echo2.form.FormUtil;

import echopointng.TabbedPane;
import echopointng.tabbedpane.DefaultTabImageRenderer;
import echopointng.tabbedpane.DefaultTabModel;

/**
 * Extension of EchoPointNG TabbedPane component. Allow to configure by XML stylesheet:
 * <ul>
 * <li><b>TabbedPane</b> component</li>
 * <li>Underlying <b>DefaultTabImageRenderer</b> component</li>
 * <li>Underlying <b>DefaultTabModel</b> component</li>
 * </ul>
 * Example: <br/><br/> <font face="courier"> &lt;style name="Object"
 * type="org.romaframework.aspect.view.echo2.component.DynaTabbedPane"><br/>&nbsp;&lt;properties><br/>&nbsp;&nbsp;&lt;property
 * name="activeTabBackgroundColor" value="#fbfbfb" /><br/>&nbsp;&nbsp;&lt;property name="activeTabForegroundColor" value="#000000" /><br/>&nbsp;&nbsp;&lt;property
 * name="inactiveTabBackgroundColor" value="#e1e1e1" /><br/>&nbsp;&nbsp;&lt;property name="inactiveTabForegroundColor"
 * value="#000000" /><br/>&nbsp;&nbsp;&lt;property name="tabPlacement" value="top" /><br/>&nbsp;&nbsp;&lt;property
 * name="insetsTab" value="8px 8px" /><br/>&nbsp;&nbsp;&lt;property name="border"><br/>&nbsp;&nbsp;&nbsp;&lt;border
 * color="#fbfbfb" size="2px" style="outset" /><br/>&nbsp;&nbsp;&lt;/property><br/>&nbsp;&lt;/properties><br/>&lt;/style> </font>
 * 
 * @author Luca Garulli (luca.garulli@assetdata.it)
 */
public class DynaTabbedPane extends TabbedPane implements PropertyChangeListener {

  public static final String         PROPERTY_ACTIVE_TAB_BACKGROUND_COLOR   = "activeTabBackgroundColor";
  public static final String         PROPERTY_ACTIVE_TAB_FOREGROUND_COLOR   = "activeTabForegroundColor";
  public static final String         PROPERTY_INACTIVE_TAB_BACKGROUND_COLOR = "inactiveTabBackgroundColor";
  public static final String         PROPERTY_INACTIVE_TAB_FOREGROUND_COLOR = "inactiveTabForegroundColor";
  public static final String         PROPERTY_BORDER_TAB_COLOR              = "borderTabColor";
  public static final String         PROPERTY_INSETS_TAB                    = "insetsTab";
  protected Color                    activeTabBackgroundColor               = Color.LIGHTGRAY;
  protected Color                    activeTabForegroundColor               = Color.BLACK;
  protected Color                    inactiveTabBackgroundColor             = Color.WHITE;
  protected Color                    inactiveTabForegroundColor             = Color.BLACK;
  protected Color                    borderTabColor                         = Color.BLACK;
  protected Insets                   insetsTab                              = new Insets(5);
  protected DefaultTabModel          tabModel;
  protected boolean                  lazyRendering;
  protected Map<EntityForm, Boolean> renderedComponents;

  public DynaTabbedPane(boolean iLazyRendering) {
    lazyRendering = iLazyRendering;

    tabModel = new DefaultTabModel();
    setModel(tabModel);

    addPropertyChangeListener(this);

    initLazyMap();
  }

  public void addTab(String iText, Component childForm) {
    Label label = new Label(iText);
    if (activeTabForegroundColor != null)
      label.setForeground(activeTabForegroundColor);
    tabModel.addTab(label, childForm);
  }

  public void addChangeListener(ChangeListener iEventListener) {
    tabModel.addChangeListener(iEventListener);
  }

  @Override
  public void setStyle(Style iStyle) {
    // LOAD ALL OWN PROPERTIES
    if (iStyle.isPropertySet(PROPERTY_ACTIVE_TAB_BACKGROUND_COLOR))
      activeTabBackgroundColor = (Color) iStyle.getProperty(PROPERTY_ACTIVE_TAB_BACKGROUND_COLOR);
    if (iStyle.isPropertySet(PROPERTY_ACTIVE_TAB_FOREGROUND_COLOR))
      activeTabForegroundColor = (Color) iStyle.getProperty(PROPERTY_ACTIVE_TAB_FOREGROUND_COLOR);
    if (iStyle.isPropertySet(PROPERTY_INACTIVE_TAB_BACKGROUND_COLOR))
      inactiveTabBackgroundColor = (Color) iStyle.getProperty(PROPERTY_INACTIVE_TAB_BACKGROUND_COLOR);
    if (iStyle.isPropertySet(PROPERTY_INACTIVE_TAB_FOREGROUND_COLOR))
      inactiveTabForegroundColor = (Color) iStyle.getProperty(PROPERTY_INACTIVE_TAB_FOREGROUND_COLOR);
    if (iStyle.isPropertySet(PROPERTY_BORDER_TAB_COLOR))
      borderTabColor = (Color) iStyle.getProperty(PROPERTY_BORDER_TAB_COLOR);
    else {
      if (iStyle.isPropertySet(PROPERTY_BORDER))
        borderTabColor = ((Border) iStyle.getProperty(PROPERTY_BORDER)).getColor();
    }
    int tabPlacement = getTabPlacement();
    if (iStyle.isPropertySet(PROPERTY_TAB_PLACEMENT))
      tabPlacement = (Integer) iStyle.getProperty(PROPERTY_TAB_PLACEMENT);
    // SET IMAGE RENDERED
    DefaultTabImageRenderer imageRender = DefaultTabImageRenderer.getInstance(activeTabBackgroundColor, inactiveTabBackgroundColor,
        borderTabColor, tabPlacement);
    if (iStyle.isPropertySet(PROPERTY_INSETS_TAB))
      insetsTab = (Insets) iStyle.getProperty(PROPERTY_INSETS_TAB);
    imageRender.setInsets(insetsTab);
    tabModel.setTabImageRenderer(imageRender);
    super.setStyle(iStyle);
  }

  public Color getActiveTabBackgroundColor() {
    return activeTabBackgroundColor;
  }

  public void setActiveTabBackgroundColor(Color activeTabColor) {
    this.activeTabBackgroundColor = activeTabColor;
  }

  public Color getBorderTabColor() {
    return borderTabColor;
  }

  public void setBorderTabColor(Color borderTabColor) {
    this.borderTabColor = borderTabColor;
  }

  public Color getInactiveTabBackgroundColor() {
    return inactiveTabBackgroundColor;
  }

  public void setInactiveTabBackgroundColor(Color inactiveTabColor) {
    this.inactiveTabBackgroundColor = inactiveTabColor;
  }

  public Insets getInsetsTab() {
    return insetsTab;
  }

  public void setInsetsTab(Insets insetsTab) {
    this.insetsTab = insetsTab;
  }

  public Color getActiveTabForegroundColor() {
    return activeTabForegroundColor;
  }

  public void setActiveTabForegroundColor(Color activeTabForegroundColor) {
    this.activeTabForegroundColor = activeTabForegroundColor;
  }

  public Color getInactiveTabForegroundColor() {
    return inactiveTabForegroundColor;
  }

  public void setInactiveTabForegroundColor(Color inactiveTabForegroundColor) {
    this.inactiveTabForegroundColor = inactiveTabForegroundColor;
  }

  public void clear() {
    renderedComponents.clear();
    while (getComponentCount() > 0)
      tabModel.removeTabAt(0);
  }

  @Override
  public void setEnabled(boolean iNewValue) {
  }

  public boolean renderLazy(EntityForm iComponent) {
    if (!lazyRendering)
      return false;

    if (renderedComponents.containsKey(iComponent))
      return false;

    renderedComponents.put(iComponent, Boolean.TRUE);
    return true;
  }

  public boolean isLazyRendering() {
    return lazyRendering;
  }

  public void setLazyRendering(boolean lazyRendering) {
    this.lazyRendering = lazyRendering;

    initLazyMap();
  }

  public void propertyChange(PropertyChangeEvent iEvent) {
    if (iEvent.getSource() instanceof DynaTabbedPane) {
      Object oldValue = iEvent.getOldValue();
      Object newValue = iEvent.getNewValue();
      if (oldValue != null && oldValue instanceof EntityForm) {
        EntityForm form = (EntityForm) oldValue;

        Object content = form.getContent();
        if (content instanceof ViewCallback)
          ((ViewCallback) content).onDispose();

      } else if (newValue != null && newValue instanceof EntityForm) {
        EntityForm form = (EntityForm) newValue;

        if (renderLazy(form)) {
          form.renderContent();
        }

        FormUtil.invokeOnShow(form.getContent());
      }
    }
  }

  private void initLazyMap() {
    if (lazyRendering)
      renderedComponents = new HashMap<EntityForm, Boolean>();
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.