KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > core > ModelSupport


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.jpivot.core;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collection JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Locale JavaDoc;
20 import java.util.Map JavaDoc;
21
22 /**
23  * Default implementation of a model
24  * @author av
25  */

26
27 public class ModelSupport implements Model {
28
29   private Collection JavaDoc listeners = new ArrayList JavaDoc();
30   private Map JavaDoc extensionMap = new HashMap JavaDoc();
31   private Locale JavaDoc locale;
32   private Model decoratedModel;
33
34   public ModelSupport() {
35     decoratedModel = this;
36   }
37   
38   /**
39    * clears all references to extensioins (to avoid memory leaks)
40    */

41   public void destroy() {
42     listeners.clear();
43     extensionMap.clear();
44     locale = null;
45     decoratedModel = null;
46   }
47
48   /**
49    * @see com.tonbeller.jpivot.model.Model#getFeature(String)
50    */

51   public Extension getExtension(String JavaDoc id) {
52     return (Extension) extensionMap.get(id);
53   }
54
55   /**
56    * returns the extensions
57    * @see Extension
58    */

59   public Map JavaDoc getExtensions() {
60     return extensionMap;
61   }
62
63   /**
64    * adds a Feature to this model. Used by ModelFactory.
65    */

66   public void addExtension(Extension extension) {
67     extensionMap.put(extension.getId(), extension);
68     extension.setModel(this);
69     decoratedModel = extension.decorate(decoratedModel);
70   }
71
72   /**
73    * returns null
74    */

75   public Object JavaDoc getBookmarkState(int levelOfDetail) {
76     return null;
77   }
78
79   /**
80    * does nothing
81    */

82   public void setBookmarkState(Object JavaDoc state) {
83   }
84
85   /**
86    * @see com.tonbeller.jpivot.core.Model#addModelChangeListener(ModelChangeListener)
87    */

88   public void addModelChangeListener(ModelChangeListener l) {
89     listeners.add(l);
90   }
91
92   /**
93    * @see com.tonbeller.jpivot.core.Model#removeModelChangeListener(ModelChangeListener)
94    */

95   public void removeModelChangeListener(ModelChangeListener l) {
96     listeners.remove(l);
97   }
98
99   public void fireModelChanged() {
100     fireModelChanged(new ModelChangeEvent(this));
101   }
102
103   public void fireModelChanged(ModelChangeEvent e) {
104     Iterator JavaDoc it = listeners.iterator();
105     while (it.hasNext())
106        ((ModelChangeListener) it.next()).modelChanged(e);
107   }
108
109   public void fireStructureChanged() {
110     fireStructureChanged(new ModelChangeEvent(this));
111   }
112
113   public void fireStructureChanged(ModelChangeEvent e) {
114     Iterator JavaDoc it = listeners.iterator();
115     while (it.hasNext())
116        ((ModelChangeListener) it.next()).structureChanged(e);
117   }
118
119   /**
120    * Returns the current locale.
121    * @return Locale
122    */

123   public Locale JavaDoc getLocale() {
124     if (locale == null)
125       return Locale.getDefault();
126     return locale;
127   }
128
129   /**
130    * Sets the current locale.
131    * @param locale The locale to set
132    */

133   public void setLocale(Locale JavaDoc locale) {
134     this.locale = locale;
135   }
136
137   /**
138    * Returns a decorated model if any of the extensions decorate this. Returns this otherwise.
139    */

140   public Model getTopDecorator() {
141     return decoratedModel;
142   }
143
144   public Model getRootModel() {
145     return this;
146   }
147 }
148
Popular Tags