01: /*
02: * $Id: AbstractObjectPanelModel.java 6115 2006-01-30 04:50:47Z dfs $
03: *
04: * Copyright 2006 Daniel F. Savarese
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.savarese.org/software/ApacheLicense-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: package org.savarese.unicorn.ui;
20:
21: import java.util.*;
22:
23: /**
24: * An abstract class implementing most of the methods declared by the
25: * {@link ObjectPanelModel} interface. It simplifies the implementation of
26: * the {@link ObjectPanelModel} interface.
27: */
28: public abstract class AbstractObjectPanelModel<O extends Object>
29: implements ObjectPanelModel<O> {
30: /**
31: * The object wrapped by the model. It can be set and retrieved
32: * with {@link #setObject} and {@link #getObject}.
33: */
34: protected O _object_;
35:
36: /**
37: * A list of {@link ObjectPanelModelListener} instances that are notified
38: * each time {@link #setObject} is called with a new object.
39: */
40: protected LinkedList<ObjectPanelModelListener<O>> _listeners_;
41:
42: /**
43: * Invokes {@link ObjectPanelModelListener#modelChanged} for each
44: * {@link ObjectPanelModelListener} in {@link #_listeners_}.
45: */
46: protected void _notifyListeners_() {
47: for (ObjectPanelModelListener<O> listener : _listeners_)
48: listener.modelChanged(this );
49: }
50:
51: /**
52: * Initializes {@link #_listeners_}.
53: */
54: protected AbstractObjectPanelModel() {
55: _listeners_ = new LinkedList<ObjectPanelModelListener<O>>();
56: }
57:
58: public void setObject(O obj) {
59: if (obj != _object_) {
60: _object_ = obj;
61: _notifyListeners_();
62: }
63: }
64:
65: public O getObject() {
66: return _object_;
67: }
68:
69: public String getObjectName() {
70: return _object_.toString();
71: }
72:
73: public boolean isValid() {
74: return (getObject() != null);
75: }
76:
77: public void addObjectPanelModelListener(
78: ObjectPanelModelListener<O> listener) {
79: _listeners_.add(listener);
80: }
81:
82: public void removeObjectPanelModelListener(
83: ObjectPanelModelListener<O> listener) {
84: _listeners_.remove(listener);
85: }
86: }
|