001 /*
002 * Copyright 2002-2004 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016 package org.springframework.richclient.factory;
017
018 import javax.swing.JComponent;
019
020 import org.springframework.richclient.application.support.ApplicationServicesAccessor;
021
022 /**
023 * A skeleton implementation of the {@link ControlFactory} interface that only
024 * creates it's control when requested.
025 *
026 * <p>
027 * The factory may operate in singleton mode, which is the default. In this
028 * case, the control will be created the first time it is requested and the same
029 * instance will be returned for each subsequent request. When operating in
030 * non-singleton mode, a new control instance is created each time it is
031 * requested.
032 * </p>
033 *
034 * @author Keith Donald
035 */
036 public abstract class AbstractControlFactory extends ApplicationServicesAccessor implements ControlFactory {
037
038 private boolean singleton = true;
039
040 private JComponent control;
041
042 /**
043 * Creates a new uninitialized {@code AbstractControlFactory}.
044 */
045 protected AbstractControlFactory() {
046 // do nothing
047 }
048
049 /**
050 * Returns true (the default) if this factory is to create a single instance
051 * of its control.
052 *
053 * @return <code>true</code> if this factory returns a singleton instance
054 * of its control.
055 */
056 protected final boolean isSingleton() {
057 return singleton;
058 }
059
060 /**
061 * Sets the flag that determines if this factory is to create a single
062 * instance of its control. By default, this flag is true.
063 *
064 * @param singleton The singleton flag.
065 */
066 protected final void setSingleton(boolean singleton) {
067 this.singleton = singleton;
068 }
069
070 /**
071 * Returns an instance of the control that this factory produces.
072 *
073 * <p>
074 * This implementation is a template method, calling the abstract
075 * {@link #createControl()} method if operating in non-singleton mode or if
076 * the control has not yet been created when operating in singleton mode.
077 * </p>
078 *
079 */
080 public final JComponent getControl() {
081 if (isSingleton()) {
082 if (this.control == null) {
083 this.control = createControl();
084 }
085 return this.control;
086 }
087
088 return createControl();
089 }
090
091 /**
092 * Returns true if the control for this factory has been created. If this
093 * factory is set to non-singleton mode, this method will always return
094 * false even if an instance of the control has previously been created.
095 *
096 * @return <code>true</code> if operating in singleton mode and an instance of the
097 * control has already been created, false otherwise.
098 */
099 public final boolean isControlCreated() {
100 if (isSingleton()) {
101 return this.control != null;
102 }
103
104 return false;
105 }
106
107 /**
108 * Creates an instance of the control produced by this factory if operating
109 * in singleton mode and the control instance has not already been created.
110 */
111 protected void createControlIfNecessary() {
112 if (isSingleton() && this.control == null) {
113 getControl();
114 }
115 }
116
117 /**
118 * Subclasses must override this method to create a new instance of the
119 * control that this factory produces.
120 *
121 * @return The newly created control, never null.
122 */
123 protected abstract JComponent createControl();
124
125 }