/*
 * Copyright 2002-2004 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.factory;

import javax.swing.JComponent;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.richclient.application.support.ApplicationServicesAccessor;

/**
 * A skeleton implementation of the {@link ControlFactory} interface that only creates it's control 
 * when requested.
 * 
 * <p>
 * The factory may operate in singleton mode, which is the default. In this case, the control
 * will be created the first time it is requested and the same instance will be returned for each
 * subsequent request. When operating in non-singleton mode, a new control instance is created each 
 * time it is requested.
 * </p>
 * 
 * @author Keith Donald
 */
public abstract class AbstractControlFactory extends ApplicationServicesAccessor implements ControlFactory {

    /** Class logger, available to subclasses. */
    protected Log logger = LogFactory.getLog(getClass());

    private boolean singleton = true;

    private JComponent control;
    
    /**
     * Creates a new uninitialized {@code AbstractControlFactory}.
     */
    protected AbstractControlFactory() {
        //do nothing
    }

    /**
     * Returns true (the default) if this factory is to create a single instance of its control. 
     *
     * @return true if this factory returns a singleton instance of its control.
     */
    protected final boolean isSingleton() {
        return singleton;
    }

    /**
     * Sets the flag that determines if this factory is to create a single instance of its control.
     * By default, this flag is true.
     *
     * @param singleton The singleton flag.
     */
    protected final void setSingleton(boolean singleton) {
        this.singleton = singleton;
    }

    /**
     * Returns an instance of the control that this factory produces. 
     * 
     * <p>
     * This implementation is a template method, calling the abstract {@link #createControl()} 
     * method if operating in non-singleton mode or if the control has not yet been created when 
     * operating in singleton mode.
     * </p>
     * 
     */
    public final JComponent getControl() {
        if (isSingleton()) {
            if (control == null) {
                this.control = createControl();
            }
            return control;
        }

        return createControl();
    }

    /**
     * Returns true if the control for this factory has been created. If this factory is set 
     * to non-singleton mode, this method will always return false even if an instance of the 
     * control has previously been created.
     *
     * @return true if operating in singleton mode and an instance of the control has already 
     * been created, false otherwise.
     */
    public final boolean isControlCreated() {
        if (isSingleton()) {
            return control != null;
        }

        return false;
    }

    /**
     * Creates an instance of the control produced by this factory if operating in singleton mode
     * and the control instance has not already been created.
     */
    protected void createControlIfNecessary() {
        if (isSingleton() && control == null) {
            getControl();
        }
    }

    /**
     * Subclasses must override this method to create a new instance of the control that this 
     * factory produces.
     *
     * @return The newly created control, never null.
     */
    protected abstract JComponent createControl();
    
}

@@ -697,93 +697,8 @@
 t;%0A%0A
-import org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0A
 impo
@@ -780,16 +780,16 @@
 cessor;%0A
+
 %0A/**%0A * 
@@ -1388,117 +1388,8 @@
  %7B%0A%0A
-    /** Class logger, available to subclasses. */%0A    protected Log logger = LogFactory.getLog(getClass());%0A%0A
     
@@ -2642,16 +2642,21 @@
     if (
+this.
 control 
@@ -2743,24 +2743,29 @@
      return 
+this.
 control;%0A   
@@ -3294,16 +3294,21 @@
  return 
+this.
 control 
@@ -3576,24 +3576,24 @@
 cessary() %7B%0A
-
         if (
@@ -3609,16 +3609,21 @@
 on() && 
+this.
 control 

Fix for RCP-211, logger shadows superclass logger.