KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > trading > client > typemgr > TypeManager


1
2 // Copyright (C) 1998-1999
3
// Object Oriented Concepts, Inc.
4

5 // **********************************************************************
6
//
7
// Copyright (c) 1997
8
// Mark Spruiell (mark@intellisoft.com)
9
//
10
// See the COPYING file for more information
11
//
12
// **********************************************************************
13

14 package org.jacorb.trading.client.typemgr;
15
16 import java.awt.*;
17 import java.awt.event.*;
18 import java.io.*;
19 import org.omg.CORBA.ORB JavaDoc;
20 import org.omg.CORBA.TypeCode JavaDoc;
21 import org.omg.CORBA.TCKind JavaDoc;
22 import org.omg.CosTrading.*;
23 import org.omg.CosTradingRepos.*;
24 import org.omg.CosTradingRepos.ServiceTypeRepositoryPackage.*;
25 // GB: import jtport.ORBLayer;
26
import org.jacorb.trading.client.util.*;
27
28
29 public class TypeManager
30     extends Frame
31     implements ActionListener, ItemListener, Runnable JavaDoc
32 {
33     private List m_types;
34     private TextArea m_description;
35     private Button m_add;
36     private Button m_remove;
37     private Checkbox m_masked;
38     private Label m_incarnation;
39     private Label m_status;
40     private AddTypeDialog m_addDialog;
41     private ServiceTypeRepository m_repos;
42     private static ORB JavaDoc s_orb;
43
44
45     public TypeManager(ServiceTypeRepository repos)
46     {
47     super("Service Type Repository");
48
49     setFont(new Font("Helvetica", Font.PLAIN, 12));
50
51     m_repos = repos;
52
53     createContents();
54
55     refreshTypes();
56     }
57
58
59     protected void createContents()
60     {
61     Panel panel = new Panel();
62     panel.setLayout(new GridBagLayout());
63
64     Panel typesPanel = new Panel();
65     typesPanel.setLayout(new GridBagLayout());
66     Constrain.constrain(typesPanel, new Label("Service types", Label.LEFT),
67                 0, 0, 1, 1, GridBagConstraints.NONE, GridBagConstraints.NORTHWEST,
68                 0.0, 0.0, 0, 0, 0, 0);
69     m_types = new List();
70     m_types.setMultipleMode(false);
71     m_types.setSize(m_types.getMinimumSize(10));
72     m_types.addItemListener(this);
73     Constrain.constrain(typesPanel, m_types, 0, 1, 1, 1,
74                 GridBagConstraints.BOTH, GridBagConstraints.NORTHWEST, 1.0, 1.0,
75                 0, 0, 0, 0);
76
77     Constrain.constrain(panel, typesPanel, 0, 0, 1, 1, GridBagConstraints.BOTH,
78                 GridBagConstraints.NORTHWEST, 0.25, 1.0, 5, 10, 5, 5);
79
80     Panel descPanel = new Panel();
81     descPanel.setLayout(new GridBagLayout());
82     Constrain.constrain(descPanel, new Label("Description", Label.LEFT),
83                 0, 0, 2, 1, GridBagConstraints.NONE, GridBagConstraints.NORTHWEST,
84                 0.0, 0.0, 0, 0, 0, 0);
85     m_description = new TextArea(10, 40);
86     m_description.setEditable(false);
87     Constrain.constrain(descPanel, m_description, 0, 1, 2, 1,
88                 GridBagConstraints.BOTH, GridBagConstraints.NORTHWEST, 1.0, 1.0,
89                 0, 0, 0, 0);
90
91     Constrain.constrain(panel, descPanel, 1, 0, 1, 1, GridBagConstraints.BOTH,
92                 GridBagConstraints.NORTHEAST, 0.75, 1.0, 5, 5, 5, 10);
93
94     Panel buttonPanel = new Panel();
95     buttonPanel.setLayout(new GridLayout(1, 2, 10, 0));
96
97     m_add = new Button("Add...");
98     m_add.setActionCommand("add");
99     m_add.addActionListener(this);
100     buttonPanel.add(m_add);
101
102     m_remove = new Button("Remove...");
103     m_remove.setActionCommand("remove");
104     m_remove.addActionListener(this);
105     buttonPanel.add(m_remove);
106
107     Constrain.constrain(panel, buttonPanel, 0, 1, 1, 1, GridBagConstraints.NONE,
108                 GridBagConstraints.NORTHWEST, 0.0, 0.0, 5, 10, 5, 5);
109
110     Panel maskPanel = new Panel();
111     maskPanel.setLayout(new GridBagLayout());
112
113     m_masked = new Checkbox(" Masked");
114     m_masked.addItemListener(this);
115     Constrain.constrain(maskPanel, m_masked, 0, 2, 1, 1,
116                 GridBagConstraints.NONE, GridBagConstraints.NORTHWEST, 0.0, 0.0,
117                 0, 0, 0, 0);
118     m_incarnation = new Label("Incarnation:", Label.RIGHT);
119     Constrain.constrain(maskPanel, m_incarnation, 1, 2, 1, 1,
120                 GridBagConstraints.HORIZONTAL, GridBagConstraints.NORTHEAST, 1.0, 0.0,
121                 0, 0, 0, 0);
122
123     Constrain.constrain(panel, maskPanel, 1, 1, 1, 1,
124                 GridBagConstraints.HORIZONTAL, GridBagConstraints.NORTHEAST, 1.0, 0.0,
125                 5, 5, 5, 10);
126
127     m_status = new Label("", Label.LEFT);
128     Constrain.constrain(panel, m_status, 0, 2, 2, 1,
129                 GridBagConstraints.HORIZONTAL, GridBagConstraints.NORTHWEST, 1.0, 0.0,
130                 0, 10, 3, 10);
131
132     add(panel);
133     }
134
135
136     public void itemStateChanged(ItemEvent e)
137     {
138     if (e.getItemSelectable() == m_types) {
139         updateButtons();
140         clearStatus();
141         if (e.getStateChange() == ItemEvent.SELECTED)
142         describeType();
143     }
144     else if (e.getItemSelectable() == m_masked)
145         maskType(e.getStateChange() == ItemEvent.SELECTED);
146     }
147
148
149     public void actionPerformed(ActionEvent e)
150     {
151     String JavaDoc cmd = e.getActionCommand();
152
153     if (cmd.equals("add")) {
154         if (m_addDialog == null) {
155         m_addDialog = new AddTypeDialog(this, m_repos);
156         m_addDialog.setActionCommand("refresh");
157         m_addDialog.addActionListener(this);
158         }
159
160         clearStatus();
161         m_addDialog.setVisible(true);
162     }
163     else if (cmd.equals("remove")) {
164         String JavaDoc type = m_types.getSelectedItem();
165         ConfirmDialog confirm =
166         new ConfirmDialog(this, "Remove type '" + type + "'?");
167         confirm.setActionCommand("confirm");
168         confirm.addActionListener(this);
169         clearStatus();
170         confirm.setVisible(true);
171     }
172     else if (cmd.equals("refresh"))
173         refreshTypes();
174     else if (cmd.equals("confirm"))
175         removeType();
176     }
177
178
179     protected void removeType()
180     {
181     try {
182         String JavaDoc type = m_types.getSelectedItem();
183         m_repos.remove_type(type);
184         m_types.remove(type);
185         m_description.setText("");
186         clearStatus();
187         updateButtons();
188     }
189     catch (HasSubTypes e) {
190         showStatus("Service type '" + e.the_type + "' has subtypes");
191     }
192     catch (IllegalServiceType e) {
193         showStatus("Illegal service type: " + e.type);
194     }
195     catch (UnknownServiceType e) {
196         showStatus("Unknown service type: " + e.type);
197     }
198     }
199
200
201     protected void maskType(boolean mask)
202     {
203     try {
204         String JavaDoc type = m_types.getSelectedItem();
205         if (mask)
206         m_repos.mask_type(type);
207         else
208         m_repos.unmask_type(type);
209         clearStatus();
210     }
211     catch (NotMasked e) {
212         showStatus("Service type '" + e.name + "' is not masked");
213         m_masked.setState(false);
214     }
215     catch (AlreadyMasked e) {
216         showStatus("Service type '" + e.name + "' is already masked");
217         m_masked.setState(true);
218     }
219     catch (IllegalServiceType e) {
220         showStatus("Illegal service type: " + e.type);
221     }
222     catch (UnknownServiceType e) {
223         showStatus("Unknown service type: " + e.type);
224     }
225     }
226
227
228     protected void refreshTypes()
229     {
230     new Thread JavaDoc(this).start();
231     }
232
233
234     public void run()
235     {
236     showStatus("Refreshing service types...");
237     String JavaDoc[] types;
238     SpecifiedServiceTypes whichTypes = new SpecifiedServiceTypes();
239     //whichTypes._default(ListOption.all);
240
// GB: whichTypes.all_dummy((short)0);
241
whichTypes.__default();
242
243     types = m_repos.list_types(whichTypes);
244     QuickSort.sort(types);
245     m_types.removeAll();
246     for (int i = 0; i < types.length; i++)
247         m_types.add(types[i]);
248     updateButtons();
249     m_description.setText("");
250     clearStatus();
251     }
252
253
254     protected void updateButtons()
255     {
256     if (m_types.getSelectedIndex() < 0) {
257         m_remove.setEnabled(false);
258         m_masked.setEnabled(false);
259         m_incarnation.setText("Incarnation:");
260         m_incarnation.setEnabled(false);
261     }
262     else {
263         m_remove.setEnabled(true);
264         m_masked.setEnabled(true);
265         m_incarnation.setEnabled(true);
266     }
267     }
268
269
270     protected void describeType()
271     {
272     try {
273         String JavaDoc type = m_types.getSelectedItem();
274         TypeStruct ts = m_repos.describe_type(type);
275         String JavaDoc script = printType(type, ts);
276
277         m_description.setText(script);
278         m_masked.setState(ts.masked);
279         m_incarnation.setText("Incarnation: {" + ts.incarnation.high +
280                   ", " + ts.incarnation.low + "}");
281     }
282     catch (IllegalServiceType e) {
283         showStatus("Illegal service type: " + e.type);
284     }
285     catch (UnknownServiceType e) {
286         showStatus("Unknown service type: " + e.type);
287     }
288     catch (org.omg.CORBA.SystemException JavaDoc e) {
289         showStatus("System error occurred");
290         e.printStackTrace();
291     }
292     }
293
294
295     protected String JavaDoc printType(String JavaDoc name, TypeStruct ts)
296     {
297     StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
298
299     buff.append("service " + name + " ");
300     if (ts.super_types.length > 0) {
301         buff.append(": ");
302         for (int i = 0; i < ts.super_types.length; i++) {
303         buff.append(ts.super_types[i]);
304         if (i < ts.super_types.length - 1)
305             buff.append(", ");
306         else
307             buff.append(" ");
308         }
309     }
310     buff.append("{\n");
311
312     buff.append(" interface " + ts.if_name + ";\n");
313     for (int i = 0; i < ts.props.length; i++) {
314         buff.append(" ");
315         switch (ts.props[i].mode.value()) {
316         case PropertyMode._PROP_NORMAL:
317         buff.append("property ");
318         break;
319         case PropertyMode._PROP_READONLY:
320         buff.append("readonly property ");
321         break;
322         case PropertyMode._PROP_MANDATORY:
323         buff.append("mandatory property ");
324         break;
325         case PropertyMode._PROP_MANDATORY_READONLY:
326         buff.append("mandatory readonly property ");
327         break;
328         }
329
330         buff.append(convertType(ts.props[i].value_type) + " " +
331             ts.props[i].name + ";\n");
332     }
333
334     buff.append("};\n");
335
336     return buff.toString();
337     }
338
339
340     protected String JavaDoc convertType(TypeCode JavaDoc tc)
341     {
342     String JavaDoc result = null;
343
344     TCKind JavaDoc kind = tc.kind();
345     if (kind == TCKind.tk_sequence) {
346         try {
347         TypeCode JavaDoc elemTC = tc.content_type();
348         kind = elemTC.kind();
349         result = "sequence<" + convertKind(kind) + ">";
350         }
351         catch (org.omg.CORBA.TypeCodePackage.BadKind JavaDoc e) {
352         throw new RuntimeException JavaDoc();
353         }
354     }
355     else
356         result = convertKind(kind);
357
358     return result;
359     }
360
361
362     protected String JavaDoc convertKind(TCKind JavaDoc kind)
363     {
364     String JavaDoc result = "unknown";
365
366     switch (kind.value()) {
367     case TCKind._tk_null:
368         result = "other";
369         break;
370     case TCKind._tk_boolean:
371         result = "boolean";
372         break;
373     case TCKind._tk_short:
374         result = "short";
375         break;
376     case TCKind._tk_ushort:
377         result = "unsigned short";
378         break;
379     case TCKind._tk_long:
380         result = "long";
381         break;
382     case TCKind._tk_ulong:
383         result = "unsigned long";
384         break;
385     case TCKind._tk_float:
386         result = "float";
387         break;
388     case TCKind._tk_double:
389         result = "double";
390         break;
391     case TCKind._tk_char:
392         result = "char";
393         break;
394     case TCKind._tk_string:
395         result = "string";
396         break;
397     }
398
399     return result;
400     }
401
402
403     protected void showStatus(String JavaDoc message)
404     {
405     m_status.setText(message);
406     }
407
408
409     protected void clearStatus()
410     {
411     m_status.setText("");
412     }
413
414
415     protected static void usage()
416     {
417     System.out.println("Usage: TypeManager iorfile");
418     System.exit(1);
419     }
420
421
422     public static void main(String JavaDoc[] args)
423     {
424     s_orb = org.omg.CORBA.ORB.init(args,null);
425
426     ServiceTypeRepository repos = null;
427
428     try
429     {
430
431         org.omg.CORBA.Object JavaDoc obj = s_orb.resolve_initial_references("TradingService");
432         if (obj == null)
433         {
434         System.out.println("Invalid object");
435         System.exit(1);
436         }
437
438         Lookup lookup = LookupHelper.narrow(obj);
439         obj = lookup.type_repos();
440         repos = ServiceTypeRepositoryHelper.narrow(obj);
441     }
442     catch (Exception JavaDoc e)
443     {
444         e.printStackTrace();
445         System.exit(1);
446     }
447
448     TypeManager app = new TypeManager(repos);
449
450     app.addWindowListener(
451                   new WindowAdapter()
452                   {
453                   public void windowClosing(WindowEvent e)
454                       {
455                       e.getWindow().dispose();
456                       System.exit(0);
457                       }
458                   }
459                   );
460
461     app.pack();
462     app.setVisible(true);
463     }
464 }
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
Popular Tags