KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > InOut > Impl > DistributeDialog


1 /**
2  * DistributeDialog.java is a part of the SOFA project.
3  * This file was created by pepan on 18.4.2003.
4  */

5 package SOFA.SOFAnode.InOut.Impl;
6
7 import java.awt.Container JavaDoc;
8 import java.awt.GridBagConstraints JavaDoc;
9 import java.awt.GridBagLayout JavaDoc;
10 import java.awt.Insets JavaDoc;
11 import java.awt.event.ActionEvent JavaDoc;
12 import java.awt.event.ActionListener JavaDoc;
13 import java.util.HashSet JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Set JavaDoc;
17 import java.util.Vector JavaDoc;
18
19 import javax.swing.JButton JavaDoc;
20 import javax.swing.JComponent JavaDoc;
21 import javax.swing.JDialog JavaDoc;
22 import javax.swing.JLabel JavaDoc;
23 import javax.swing.JList JavaDoc;
24 import javax.swing.JScrollPane JavaDoc;
25
26 import SOFA.SOFAnode.InOut.InOut2Client;
27 import SOFA.SOFAnode.TR.ComponentInfo;
28
29 /**
30  * A dialog showing details of the chosen action (push/pull components) which should be
31  * performed on selected SOFA nodes. It also contains a button to confirmate the chosen action.
32  * <p>Note for developers: Just before the distribution of components between two SOFA nodes,
33  * a connection between their InOut parts is made. Since the client knows connector names to
34  * their InOut2Client interfaces only, the first type of connectors must be <i>derived</i> from
35  * the second one. To achieve this, just a simple implementation is provided - both names differ in
36  * a piece of text only. Instead of the string 'InOut2ClientRMISkel', the connector name contains
37  * 'InOut2InOutRMISkel'.
38  * @author Petr Panuska
39  */

40 class DistributeDialog extends JDialog JavaDoc implements ActionListener JavaDoc {
41
42   /**
43    * A list of components to be distributed.
44    */

45   private JList JavaDoc components;
46
47   /**
48    * A list of target SOFA nodes.
49    */

50   private JList JavaDoc nodes;
51
52   /**
53    * A button to confirm the selected action.
54    */

55   private JButton JavaDoc yesButton;
56
57   /**
58    * A button to refuse the selected action.
59    */

60   private JButton JavaDoc noButton;
61
62   /**
63    * A set of components which are to be distributed. Each key is a {@link SOFANode}
64    * where the component can be obtained from, each value is a HashSet of {@link ComponentInfo components}.
65    */

66   private Map JavaDoc from;
67
68   /**
69    * A set of SOFA nodes where the components are to be distributed.
70    */

71   private Set JavaDoc to;
72
73   /**
74    * If <code>true</code>, the push method is used; pull method otherwise.
75    */

76   private boolean pushMethod;
77
78   /**
79    * If <code>true</code>, only the description of chosen components is distributed.
80    */

81   private boolean descriptionOnly;
82
83   /**
84    *
85    * @param from every key is a {@link SOFANode}, every value is a {@link HashSet} with
86    * {@link ComponentInfo components} to be distributed.
87    * @param to set of {@link SOFANode SOFAnodes} where the components are to be distributed to.
88    * @param pushMethod <code>true</code> when the push method is to be employed, <code>false</code> otherwise.
89    * @param descriptionOnly if <code>true</code>, the description of chosen components is distributed only.
90    */

91   DistributeDialog (Map JavaDoc from, Set JavaDoc to, boolean pushMethod, boolean descriptionOnly) {
92     this.from = from;
93     this.to = to;
94     this.pushMethod = pushMethod;
95     this.descriptionOnly = descriptionOnly;
96
97     Vector JavaDoc v = new Vector JavaDoc();
98     for (Iterator JavaDoc iterator = from.keySet().iterator(); iterator.hasNext();) {
99       SOFANode node = (SOFANode) iterator.next();
100       Set JavaDoc set = (Set JavaDoc) from.get(node); // set of components to be distributed from the node
101
// set = getTransitiveClosure(set);
102
for (Iterator JavaDoc iter2 = set.iterator(); iter2.hasNext();) {
103         ComponentInfo info = (ComponentInfo) iter2.next();
104         v.add(node.getName() + " - " + info.getName() + "[" + info.getImplementationVersion() + "]");
105       }
106     }
107     components = new JList JavaDoc(v);
108
109     nodes = new JList JavaDoc(to.toArray());
110
111     yesButton = new JButton JavaDoc("Yes");
112     yesButton.addActionListener(this);
113
114     noButton = new JButton JavaDoc("No");
115     noButton.addActionListener(this);
116
117     GridBagLayout JavaDoc gb = new GridBagLayout JavaDoc();
118     GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
119     Container JavaDoc panel = getContentPane();
120     panel.setLayout(gb);
121
122     c.fill = GridBagConstraints.BOTH;
123     c.weightx = 1.0;
124     c.weighty = 1.0;
125     c.gridwidth = GridBagConstraints.REMAINDER;
126     c.insets = new Insets JavaDoc(1, 1, 1, 1);
127
128     JLabel JavaDoc question;
129     question = new JLabel JavaDoc("Really distribute " +
130       ((descriptionOnly) ? "the description of " : "") +
131       "listed components to nodes below? " +
132       ((pushMethod) ? "(Push method)" : ("Pull method"))
133     );
134     gb.setConstraints(question, c);
135     panel.add(question);
136
137     JComponent JavaDoc comp = new JScrollPane JavaDoc(components);
138     gb.setConstraints(comp, c);
139     panel.add(comp);
140
141     comp = new JScrollPane JavaDoc(nodes);
142     gb.setConstraints(comp, c);
143     panel.add(comp);
144
145     c.fill = GridBagConstraints.NONE;
146     c.gridwidth = GridBagConstraints.RELATIVE;
147     c.weighty = 0.0;
148     c.anchor = GridBagConstraints.EAST;
149     gb.setConstraints(yesButton, c);
150     panel.add(yesButton);
151
152     c.gridwidth = GridBagConstraints.REMAINDER;
153     c.anchor = GridBagConstraints.WEST;
154     gb.setConstraints(noButton, c);
155     panel.add(noButton);
156
157     setDefaultCloseOperation(DISPOSE_ON_CLOSE);
158     setModal(true);
159     pack();
160     setVisible(true);
161   }
162
163   /**
164    * Called when a user presses a button. If the user refuses to perform the action,
165    * it just disposes the window. If the user confirms the action, the action is
166    * performed immediately.
167    * @param e the emitted event.
168    */

169   public void actionPerformed (ActionEvent JavaDoc e) {
170     if (e.getSource() == noButton) {
171       dispose();
172     }
173     if (e.getSource() == yesButton) {
174       if (pushMethod) {
175         String JavaDoc[] nodes = new String JavaDoc[to.size()]; // where the components distribute to
176
int i = 0;
177         for (Iterator JavaDoc iterator = to.iterator(); iterator.hasNext();) {
178           SOFANode node = (SOFANode) iterator.next();
179           // todo current implementation counts with name relation in the following way:
180
// the name of the InOut2InOut connector reference of a SOFA node differs from
181
// the name of the InOut2Client connection reference of the same SOFA node
182
// just in the following string:
183
nodes[i++] = node.getReference().toString().replaceAll("InOut2ClientRMISkel", "InOut2InOutRMISkel");
184         }
185         Set JavaDoc fromSet = from.keySet();
186         for (Iterator JavaDoc iterator = fromSet.iterator(); iterator.hasNext();) { // for all source nodes
187
SOFANode node = (SOFANode) iterator.next();
188           InOut2Client destInOut = node.getInOut();
189           if (destInOut != null) {
190             HashSet JavaDoc comps = (HashSet JavaDoc) from.get(node);
191             String JavaDoc[] compsFullNames = new String JavaDoc[comps.size()];
192             int i2 = 0;
193             for (Iterator JavaDoc iter2 = comps.iterator(); iter2.hasNext();) { // for all components at a source node
194
ComponentInfo info = (ComponentInfo) iter2.next();
195               compsFullNames[i2++] = info.getName() + "[" + info.getImplementationVersion() + "]";
196             }
197             if (descriptionOnly) {
198               destInOut.distributeOffers(compsFullNames, nodes);
199             } else {
200               destInOut.distributeComponents(compsFullNames, nodes);
201             }
202           }
203         }
204       } else { // pull method
205
for (Iterator JavaDoc iter1 = to.iterator(); iter1.hasNext();) { // for all dest nodes
206
SOFANode destNode = (SOFANode) iter1.next();
207           InOut2Client destInOut = destNode.getInOut();
208           if (destInOut != null) {
209             Set JavaDoc fromSet = from.keySet();
210             for (Iterator JavaDoc iter2 = fromSet.iterator(); iter2.hasNext();) { // for all source nodes
211
SOFANode sourceNode = (SOFANode) iter2.next();
212               HashSet JavaDoc comps = (HashSet JavaDoc) from.get(sourceNode);
213
214               String JavaDoc[] compsFullNames = new String JavaDoc[comps.size()];
215               int i2 = 0;
216               for (Iterator JavaDoc iter3 = comps.iterator(); iter3.hasNext();) { // for all components at the source node
217
ComponentInfo info = (ComponentInfo) iter3.next();
218                 compsFullNames[i2++] = info.getName() + "[" + info.getImplementationVersion() + "]";
219               }
220               // todo current implementation counts with name relation in the following way:
221
// the name of the InOut2InOut connector reference of a SOFA node differs from
222
// the name of the InOut2Client connection reference of the same SOFA node
223
// just in the following string:
224
String JavaDoc node = sourceNode.getReference().toString().replaceAll("InOut2ClientRMISkel", "InOut2InOutRMISkel");
225               if (descriptionOnly) {
226                 destInOut.obtainOffers(compsFullNames, node);
227               } else {
228                 destInOut.obtainComponents(compsFullNames, node);
229               }
230             }
231           }
232         } // dest nodes
233
} // pull method
234
} // yes button
235
dispose();
236   }
237 }
238
Popular Tags