KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > tree > model > BasicTreeSelectionModel


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.tree.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.Configuration;
30 import org.objectweb.fractal.gui.model.Interface;
31 import org.objectweb.fractal.gui.selection.model.Selection;
32 import org.objectweb.fractal.gui.selection.model.SelectionListener;
33
34 import javax.swing.tree.DefaultTreeSelectionModel JavaDoc;
35 import javax.swing.tree.TreePath JavaDoc;
36
37 /**
38  * A {@link javax.swing.tree.TreeSelectionModel} based on a {@link Selection}.
39  * This model makes a conversion from a {@link Selection} model to a {@link
40  * javax.swing.tree.TreeSelectionModel}. It listens to the
41  * selection in order to update its state when the selection changes.
42  */

43
44 public class BasicTreeSelectionModel extends DefaultTreeSelectionModel JavaDoc
45   implements SelectionListener, BindingController
46 {
47
48   /**
49    * An optional client interface bound to a {@link Configuration configuration}
50    * model. The root component of this configuration is changed each time the
51    * selection is changed through <i>this</i> model (but not if the selection
52    * model on which this model is based changes).
53    */

54
55   public final static String JavaDoc CONFIGURATION_BINDING = "configuration";
56
57   /**
58    * A mandatory client interface bound to a {@link Selection selection} model.
59    * This is the model on which this model is based and synchronized.
60    */

61
62   public final static String JavaDoc SELECTION_BINDING = "selection";
63
64   /**
65    * The configuration client interface.
66    */

67
68   private Configuration configuration;
69
70   /**
71    * The selection client interface.
72    */

73
74   private Selection selection;
75
76   /**
77    * Constructs a new {@link BasicTreeSelectionModel} component.
78    */

79
80   public BasicTreeSelectionModel () {
81     setSelectionMode(SINGLE_TREE_SELECTION);
82   }
83
84   // -------------------------------------------------------------------------
85
// Implementation of the BindingController interface
86
// -------------------------------------------------------------------------
87

88   public String JavaDoc[] listFc () {
89     return new String JavaDoc[] { CONFIGURATION_BINDING, SELECTION_BINDING };
90   }
91
92   public Object JavaDoc lookupFc (final String JavaDoc clientItfName) {
93     if (CONFIGURATION_BINDING.equals(clientItfName)) {
94       return configuration;
95     } else if (SELECTION_BINDING.equals(clientItfName)) {
96       return selection;
97     }
98     return null;
99   }
100
101   public void bindFc (
102     final String JavaDoc clientItfName,
103     final Object JavaDoc serverItf)
104   {
105     if (CONFIGURATION_BINDING.equals(clientItfName)) {
106       configuration = (Configuration)serverItf;
107     } else if (SELECTION_BINDING.equals(clientItfName)) {
108       selection = (Selection)serverItf;
109     }
110   }
111
112   public void unbindFc (final String JavaDoc clientItfName) {
113     if (CONFIGURATION_BINDING.equals(clientItfName)) {
114       configuration = null;
115     } else if (SELECTION_BINDING.equals(clientItfName)) {
116       selection = null;
117     }
118   }
119
120   // -------------------------------------------------------------------------
121
// Implementation of the SelectionListener interface
122
// -------------------------------------------------------------------------
123

124   public void selectionChanged () {
125     if (selection == null) {
126       clearSelection();
127     } else {
128       Object JavaDoc o = selection.getSelection();
129       if (o instanceof Interface) {
130         o = ((Interface)o).getOwner();
131       }
132       if (o == null) {
133         clearSelection();
134       } else {
135         super.setSelectionPath(new TreePath JavaDoc(((Component)o).getPath()));
136       }
137     }
138   }
139
140   // -------------------------------------------------------------------------
141
// Overriden DefaultTreeSelectionModel methods
142
// -------------------------------------------------------------------------
143

144   public void setSelectionPath (final TreePath JavaDoc path) {
145     super.setSelectionPath(path);
146     Component component = (Component)path.getLastPathComponent();
147     if (configuration != null) {
148       configuration.setRootComponent(component);
149     }
150     selection.selectComponent(component);
151   }
152 }
153
Popular Tags