KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > selection > model > BasicSelection


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.selection.model;
25
26 import org.objectweb.fractal.api.control.BindingController;
27
28 import org.objectweb.fractal.gui.model.Component;
29 import org.objectweb.fractal.gui.model.Interface;
30
31 import java.util.Map JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35 /**
36  * Basic implementation of the {@link Selection} interface.
37  */

38
39 public class BasicSelection implements Selection, BindingController {
40
41   /**
42    * A collection client interface bound to the {@link SelectionListener
43    * listeners} of this component.
44    */

45
46   public final static String JavaDoc SELECTION_LISTENERS_BINDING =
47     "selection-listeners";
48
49   /**
50    * The listeners client interface.
51    */

52
53   private Map JavaDoc selectionListeners;
54
55   /**
56    * The currently selected component. May be <tt>null</tt>.
57    */

58
59   private Component selectedComponent;
60
61   /**
62    * The currently selected interface. May be <tt>null</tt>.
63    */

64
65   private Interface selectedInterface;
66
67   /**
68    * Constructs a new {@link BasicSelection} component.
69    */

70
71   public BasicSelection () {
72     selectionListeners = new HashMap JavaDoc();
73   }
74
75   // -------------------------------------------------------------------------
76
// Implementation of the BindingController interface
77
// -------------------------------------------------------------------------
78

79   public String JavaDoc[] listFc () {
80     return (String JavaDoc[])selectionListeners.keySet().toArray(
81       new String JavaDoc[selectionListeners.size()]);
82   }
83
84   public Object JavaDoc lookupFc (final String JavaDoc clientItfName) {
85     if (clientItfName.startsWith(SELECTION_LISTENERS_BINDING)) {
86       return selectionListeners.get(clientItfName);
87     }
88     return null;
89   }
90
91
92   public void bindFc (
93     final String JavaDoc clientItfName,
94     final Object JavaDoc serverItf)
95   {
96     if (clientItfName.startsWith(SELECTION_LISTENERS_BINDING)) {
97       selectionListeners.put(clientItfName, serverItf);
98     }
99   }
100
101
102   public void unbindFc (final String JavaDoc clientItfName) {
103     if (clientItfName.startsWith(SELECTION_LISTENERS_BINDING)) {
104       selectionListeners.remove(clientItfName);
105     }
106   }
107
108   // -------------------------------------------------------------------------
109
// Implementation of the Selection interface
110
// -------------------------------------------------------------------------
111

112   public Object JavaDoc getSelection () {
113     if (selectedInterface == null) {
114       return selectedComponent;
115     } else {
116       return selectedInterface;
117     }
118   }
119
120   public void selectComponent (final Component component) {
121     if (selectedComponent != component) {
122       selectedComponent = component;
123       selectedInterface = null;
124       notifyListeners();
125     }
126   }
127
128   public void selectInterface (final Interface itf) {
129     if (selectedInterface != itf) {
130       selectedComponent = null;
131       selectedInterface = itf;
132       notifyListeners();
133     }
134   }
135
136   public void clearSelection () {
137     if (getSelection() != null) {
138       selectedComponent = null;
139       selectedInterface = null;
140       notifyListeners();
141     }
142   }
143
144   // -------------------------------------------------------------------------
145
// Other methods
146
// -------------------------------------------------------------------------
147

148   /**
149    * Notifies the listeners of this model that its state has changed.
150    */

151
152   private void notifyListeners () {
153     Iterator JavaDoc i = selectionListeners.values().iterator();
154     while (i.hasNext()) {
155       ((SelectionListener)i.next()).selectionChanged();
156     }
157   }
158 }
159
Popular Tags