AbstractComponentRenderer.java :  » Content-Management-System » contelligent » de » finix » contelligent » client » gui » Java Open Source

Java Open Source » Content Management System » contelligent 
contelligent » de » finix » contelligent » client » gui » AbstractComponentRenderer.java
/*
 * Copyright 2001-2006 C:1 Financial Services GmbH
 *
 * This software is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License Version 2.1, as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
 */

package de.finix.contelligent.client.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.Action;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

import de.finix.contelligent.client.base.ComponentFactory;
import de.finix.contelligent.client.base.ComponentPath;
import de.finix.contelligent.client.base.ContelligentComponent;
import de.finix.contelligent.client.event.ComponentEventListener;
import de.finix.contelligent.client.event.ContelligentComponentEvent;
import de.finix.contelligent.client.event.ContelligentEvent;
import de.finix.contelligent.client.event.ContelligentEventDispatcher;

public abstract class AbstractComponentRenderer extends JPanel implements ComponentRenderer, ComponentEventListener {

    private static Logger logger = Logger.getLogger(AbstractComponentEditor.class.getName());

    protected final static Border UNDEFINED_BORDER = new LineBorder(Color.orange, 2);

    protected final static Border UNKNOWN_BORDER = new LineBorder(Color.red, 1);

    protected final Border DEFAULT_BORDER = getBorder();

    private ContelligentComponent component = null;

    private View view = null;

    private GUI gui;

    public AbstractComponentRenderer() {
        super(new BorderLayout());
        setOpaque(false);
        ComponentFactory.getInstance().addComponentEventListener(this, ContelligentEventDispatcher.DOES_USE_SWING);
    }

    /**
     * Counter-part to {@link AbstractComponentEditor#updateComponent}. Loads
     * values from component to be displayed in renderer. Needs to be implemented
     * by sub class.
     */
    abstract public void update();

    public void setComponent(ContelligentComponent component) {
        this.component = component;
    }

    public ContelligentComponent getComponent() {
        return component;
    }

    public void setView(View view) {
        this.view = view;
    }

    public View getView() {
        return view;
    }

    public void setGUI(GUI gui) {
        this.gui = gui;
    }

    public GUI getGUI() {
        return gui;
    }

    /**
     * Reacts on the event that the component represented by this gui has
     * changed.
     */
    abstract protected void componentChanged(ContelligentEvent event);

    /**
     * Reacts on the event that a child has been added to the component
     * represented by this gui.
     */
    abstract protected void childComponentAdded(ContelligentEvent event);

    /**
     * Reacts on the event that a child has been removed to the component
     * represented by this gui.
     */
    abstract protected void childComponentRemoved(ContelligentEvent event);

    /**
     * Reacts on the event that a child of the component represented by this gui
     * has changed.
     */
    abstract protected void childComponentChanged(ContelligentEvent event);

    /**
     * Reacts on the event that a descendent - i.e not a direct, but indirect
     * child - of the component represented by this gui has changed.
     */
    abstract protected void descendentComponentChanged(ContelligentEvent event);

    public void onComponentAdded(ContelligentComponentEvent event) {
        logger.log(Level.FINE, "event: " + event);

        // How am I related to this event?
        boolean isMyChild = isMyChild(event.getTarget());
        boolean isMyDescendent = isMyDescendent(event.getTarget());

        if (isMyChild) {
            logger.log(Level.FINE, "component added: " + event);
            childComponentAdded(event);
        } else if (isMyDescendent) {
            logger.log(Level.FINE, "descendent component changed: " + event);
            descendentComponentChanged(event);
        }

    }

    public void onComponentRemoved(ContelligentComponentEvent event) {
        logger.log(Level.FINE, "event: " + event);

        // How am I related to this event?
        boolean isMyChild = isMyChild(event.getTarget());
        boolean isMyDescendent = isMyDescendent(event.getTarget());

        if (isMyChild) {
            logger.log(Level.FINE, "child component removed: " + event);
            childComponentRemoved(event);
        } else if (isMyDescendent) {
            logger.log(Level.FINE, "descendent component changed: " + event);
            descendentComponentChanged(event);
        }
    }

    public void onComponentChanged(ContelligentComponentEvent event) {
        logger.log(Level.FINE, "event: " + event);

        // How am I related to this event?
        boolean isMe = isMe(event.getTarget());
        boolean isMyChild = isMyChild(event.getTarget());
        boolean isMyDescendent = isMyDescendent(event.getTarget());

        if (isMe) {
            logger.log(Level.FINE, "component changed: " + event);
            componentChanged(event);
        } else if (isMyChild) {
            logger.log(Level.FINE, "child component changed: " + event);
            childComponentChanged(event);
        } else if (isMyDescendent) {
            logger.log(Level.FINE, "descendent component changed: " + event);
            descendentComponentChanged(event);
        }
    }

    /** default is to have no actions */
    public Action[] getActions() {
        return new Action[] {};
    }

    /**
     * This method is used to determine whether the given server and path equals
     * the server and path of the component of this renderer.
     */
    private boolean isMe(String path) {
        if (path == null)
            return false;
        String myPath = getMyPath();
        return (myPath.equals(ComponentPath.toComponentPath(path)));
    }

    /**
     * This method is used to determine whether the given server and path
     * represents a child component of the component of this renderer.
     */
    private boolean isMyChild(String path) {
        if (path == null)
            return false;
        String myPath = getMyPath();
        return (myPath.equals(ComponentPath.getComponentDir(path)));
    }

    /**
     * This method is used to determine whether the given server and path
     * represents a descendent component of the component of this renderer.
     */
    private boolean isMyDescendent(String path) {
        if (path == null)
            return false;
        String myPath = getMyPath();

        return ((ComponentPath.toComponentPath(path).length() > myPath.length()) && ComponentPath.toComponentPath(path)
                .startsWith(myPath));
    }

    private String getMyPath() {
        String myPath = ComponentPath.toComponentPath(getComponent().getPath());
        return myPath;
    }

    public boolean isScalable() {
        return false;
    }
}
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.