KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > history > model > BasicHistoryManager


1 /***
2  * FractalGUI: a graphical tool to edit Fractal component configurations.
3  * Copyright (C) 2003 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: fractal@objectweb.org
20  *
21  * Authors: Eric Bruneton, Patrice Fauvel
22  */

23
24 package org.objectweb.fractal.gui.history.model;
25
26 import org.objectweb.fractal.api.control.BindingController;
27
28 import org.objectweb.fractal.gui.model.ClientInterface;
29 import org.objectweb.fractal.gui.model.Component;
30 import org.objectweb.fractal.gui.model.Configuration;
31 import org.objectweb.fractal.gui.model.ConfigurationListener;
32 import org.objectweb.fractal.gui.model.Interface;
33 import org.objectweb.fractal.gui.model.ServerInterface;
34
35 import java.util.ArrayList JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Iterator JavaDoc;
40
41 /**
42  * Basic implementation of the {@link HistoryManager} interface.
43  */

44
45 public class BasicHistoryManager
46   implements HistoryManager, ConfigurationListener, BindingController
47 {
48
49   /**
50    * A mandatory client interface bound to a {@link Configuration configuration}
51    * model. This is the configuration whose root component is changed when going
52    * to the next or previous 'view'.
53    */

54
55   public final static String JavaDoc CONFIGURATION_BINDING = "configuration";
56
57   /**
58    * A collection client interface bound to the {@link HistoryListener
59    * listeners} of this component.
60    */

61
62   public final static String JavaDoc HISTORY_LISTENERS_BINDING = "history-listeners";
63
64   /**
65    * Maximum number of 'views' stored by this component.
66    */

67
68   private final static int MAX_HISTORY = 50;
69
70   /**
71    * The configuration client interface.
72    */

73
74   private Configuration configuration;
75
76   /**
77    * The listeners client interface.
78    */

79
80   private Map JavaDoc historyListeners;
81
82   /**
83    * The previous root components of {@link #configuration}. This list is used
84    * to go to previous 'views' in the navigation history.
85    */

86
87   private List JavaDoc previousRoots;
88
89   /**
90    * The next root components of {@link #configuration}. This list is used to
91    * undo the 'go previous' actions, i.e., to go to next 'views' in the
92    * navigation history.
93    */

94
95   private List JavaDoc nextRoots;
96
97   /**
98    * If the {@link #goPrevious goPrevious} method is currently executing.
99    */

100
101   private boolean isGoingPrevious;
102
103   /**
104    * If the {@link #goNext goNext} method is currently executing.
105    */

106
107   private boolean isGoingNext;
108
109   /**
110    * Constructs a new {@link BasicHistoryManager} component.
111    */

112
113   public BasicHistoryManager () {
114     previousRoots = new ArrayList JavaDoc();
115     nextRoots = new ArrayList JavaDoc();
116     historyListeners = new HashMap JavaDoc();
117   }
118
119   // -------------------------------------------------------------------------
120
// Implementation of the BindingController interface
121
// -------------------------------------------------------------------------
122

123   public String JavaDoc[] listFc () {
124     int size = historyListeners.size();
125     String JavaDoc[] names = new String JavaDoc[size + 1];
126     historyListeners.keySet().toArray(names);
127     names[size] = CONFIGURATION_BINDING;
128     return names;
129   }
130
131   public Object JavaDoc lookupFc (final String JavaDoc clientItfName) {
132     if (CONFIGURATION_BINDING.equals(clientItfName)) {
133       return configuration;
134     } else if (clientItfName.startsWith(HISTORY_LISTENERS_BINDING)) {
135       return historyListeners.get(clientItfName);
136     }
137     return null;
138   }
139
140   public void bindFc (
141     final String JavaDoc clientItfName,
142     final Object JavaDoc serverItf)
143   {
144     if (CONFIGURATION_BINDING.equals(clientItfName)) {
145       configuration = (Configuration)serverItf;
146     } else if (clientItfName.startsWith(HISTORY_LISTENERS_BINDING)) {
147       historyListeners.put(clientItfName, serverItf);
148     }
149   }
150
151   public void unbindFc (final String JavaDoc clientItfName) {
152     if (CONFIGURATION_BINDING.equals(clientItfName)) {
153       configuration = null;
154     } else if (clientItfName.startsWith(HISTORY_LISTENERS_BINDING)) {
155       historyListeners.remove(clientItfName);
156     }
157   }
158
159   // -------------------------------------------------------------------------
160
// Implementation of the ConfigurationListener interface
161
// -------------------------------------------------------------------------
162

163   public void changeCountChanged (Component component, long changeCount) {
164     // does nothing
165
}
166
167   public void rootComponentChanged (final Component oldModel) {
168     if (oldModel == null) {
169       return;
170     }
171     if (oldModel.getRootComponent() !=
172         configuration.getRootComponent().getRootComponent())
173     {
174       clear();
175       return;
176     }
177     if (isGoingPrevious) {
178       nextRoots.add(oldModel);
179       if (nextRoots.size() == 1) {
180         notifyListeners();
181       }
182     } else {
183       int size = previousRoots.size();
184       if (size >= MAX_HISTORY) {
185         previousRoots.remove(0);
186       }
187       previousRoots.add(oldModel);
188       if (!isGoingNext) {
189         nextRoots.clear();
190       }
191       if (previousRoots.size() == 1 || !isGoingNext) {
192         notifyListeners();
193       }
194     }
195   }
196
197   public void nameChanged (final Component component, final String JavaDoc oldValue) {
198     // does nothing
199
}
200
201   public void typeChanged (final Component component, final String JavaDoc oldValue) {
202     // does nothing
203
}
204
205   public void implementationChanged (
206     final Component component,
207     final String JavaDoc oldValue) {
208     // does nothing
209
}
210
211   public void interfaceNameChanged (final Interface i, final String JavaDoc oldValue) {
212     // does nothing
213
}
214
215   public void interfaceSignatureChanged (
216     final Interface i,
217     final String JavaDoc oldValue)
218   {
219     // does nothing
220
}
221
222   public void interfaceContingencyChanged (
223     final Interface i,
224     final boolean oldValue)
225   {
226     // does nothing
227
}
228
229   public void interfaceCardinalityChanged (
230     final Interface i,
231     final boolean oldValue)
232   {
233     // does nothing
234
}
235
236   public void clientInterfaceAdded (
237     final Component component,
238     final ClientInterface i,
239     final int index)
240   {
241     // does nothing
242
}
243
244   public void clientInterfaceRemoved (
245     final Component component,
246     final ClientInterface i,
247     final int index)
248   {
249     // does nothing
250
}
251
252   public void serverInterfaceAdded (
253     final Component component,
254     final ServerInterface i,
255     final int index)
256   {
257     // does nothing
258
}
259
260   public void serverInterfaceRemoved (
261     final Component component,
262     final ServerInterface i,
263     final int index)
264   {
265     // does nothing
266
}
267
268   public void interfaceBound (
269     final ClientInterface citf,
270     final ServerInterface sitf)
271   {
272     // does nothing
273
}
274
275   public void interfaceRebound (
276     final ClientInterface citf,
277     final ServerInterface oldSitf)
278   {
279     // does nothing
280
}
281
282   public void interfaceUnbound (
283     final ClientInterface citf,
284     final ServerInterface sitf)
285   {
286     // does nothing
287
}
288
289   public void attributeControllerChanged (
290     final Component component,
291     final String JavaDoc oldValue)
292   {
293     // does nothing
294
}
295
296   public void attributeChanged (
297     final Component component,
298     final String JavaDoc attributeName,
299     final String JavaDoc oldValue)
300   {
301     // does nothing
302
}
303
304   public void templateControllerDescriptorChanged (
305     final Component component,
306     final String JavaDoc oldValue)
307   {
308     // does nothing
309
}
310
311   public void componentControllerDescriptorChanged (
312     final Component component,
313     final String JavaDoc oldValue)
314   {
315     // does nothing
316
}
317
318   public void subComponentAdded (
319     final Component parent,
320     final Component child,
321     final int index)
322   {
323     // does nothing
324
}
325
326   public void subComponentRemoved (
327     final Component parent,
328     final Component child,
329     final int index)
330   {
331     // does nothing
332
}
333
334   // -------------------------------------------------------------------------
335
// Implementation of the HistoryManager interface
336
// -------------------------------------------------------------------------
337

338   public boolean canGoPrevious () {
339     return previousRoots.size() > 0;
340   }
341
342   public void goPrevious () {
343     isGoingPrevious = true;
344     try {
345       Component root = (Component)previousRoots.remove(previousRoots.size()-1);
346       configuration.setRootComponent(root);
347     } finally {
348       isGoingPrevious = false;
349     }
350     if (previousRoots.size() == 0) {
351       notifyListeners();
352     }
353   }
354
355   public boolean canGoNext () {
356     return nextRoots.size() > 0;
357   }
358
359   public void goNext () {
360     isGoingNext = true;
361     try {
362       Component root = (Component)nextRoots.remove(nextRoots.size() - 1);
363       configuration.setRootComponent(root);
364     } finally {
365       isGoingNext = false;
366     }
367     if (nextRoots.size() == 0) {
368       notifyListeners();
369     }
370   }
371
372   public void clear () {
373     nextRoots.clear();
374     previousRoots.clear();
375     notifyListeners();
376   }
377
378   // -------------------------------------------------------------------------
379
// Other methods
380
// -------------------------------------------------------------------------
381

382   /**
383    * Notifies the listeners of this model that its state has changed.
384    */

385
386   private void notifyListeners () {
387     Iterator JavaDoc i = historyListeners.values().iterator();
388     while (i.hasNext()) {
389       ((HistoryListener)i.next()).historyStateChanged();
390     }
391   }
392 }
393
Popular Tags