KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > dialog > model > InterfaceTableSelectionModel


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.dialog.model;
25
26 import org.objectweb.fractal.gui.model.ClientInterface;
27 import org.objectweb.fractal.gui.model.Component;
28 import org.objectweb.fractal.gui.model.Interface;
29 import org.objectweb.fractal.gui.model.ServerInterface;
30 import org.objectweb.fractal.gui.selection.model.Selection;
31
32 import javax.swing.DefaultListSelectionModel JavaDoc;
33
34 /**
35  * A {@link javax.swing.ListSelectionModel} based on a {@link Component} and
36  * a {@link Selection} models. This model makes a conversion from a {@link
37  * Selection} model to a {@link javax.swing.ListSelectionModel}. It represents
38  * the currently selected component interface, as given by the {@link Selection}
39  * model.
40  */

41
42 public class InterfaceTableSelectionModel extends DefaultListSelectionModel JavaDoc {
43
44   /**
45    * If this model represents client interfaces or server interfaces.
46    */

47
48   private boolean isClient;
49
50   /**
51    * The component model on which this model is based.
52    */

53
54   private Component model;
55
56   /**
57    * The selection model on which this model is based.
58    */

59
60   private Selection selection;
61
62   /**
63    * Constructs a new {@link InterfaceTableSelectionModel}.
64    *
65    * @param isClient if this model must represent client or server interfaces.
66    */

67
68   InterfaceTableSelectionModel (final boolean isClient) {
69     this.isClient = isClient;
70     setSelectionMode(SINGLE_SELECTION);
71   }
72
73   /**
74    * Sets the component model on which this model is based.
75    *
76    * @param model a component.
77    */

78
79   void setComponentModel (final Component model) {
80     this.model = model;
81   }
82
83   /**
84    * Sets the selection model on which this model is based.
85    *
86    * @param selection a selection model.
87    */

88
89   void setSelection (final Selection selection) {
90     this.selection = selection;
91   }
92
93   /**
94    * Notifies this listener that the selection model on which it is based has
95    * changed.
96    */

97
98   void selectionChanged () {
99     int index = -1;
100     Object JavaDoc o = selection.getSelection();
101     if (o instanceof Interface) {
102       Interface i = (Interface)o;
103       if (i.isInternal()) {
104         i = i.getComplementaryInterface();
105       }
106       if (i instanceof ClientInterface && isClient) {
107         index = i.getOwner().getClientInterfaces().indexOf(i);
108       } else if (i instanceof ServerInterface && !isClient) {
109         index = i.getOwner().getServerInterfaces().indexOf(i);
110       }
111     }
112     if (index == -1) {
113       clearSelection();
114     } else {
115       super.setSelectionInterval(index, index);
116     }
117   }
118
119   // -------------------------------------------------------------------------
120
// Overriden DefaultListSelectionListener methods
121
// -------------------------------------------------------------------------
122

123   public void setSelectionInterval (final int index0, final int index1) {
124     super.setSelectionInterval(index0, index1);
125     if (model != null && selection != null) {
126       Interface i = null;
127       try {
128         if (isClient) {
129           i = (Interface)model.getClientInterfaces().get(index0);
130         } else {
131           i = (Interface)model.getServerInterfaces().get(index0);
132         }
133       } catch (Exception JavaDoc e) {
134       }
135       if (i != null) {
136         selection.selectInterface(i);
137       }
138     }
139   }
140 }
141
Popular Tags