KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ist > coach > coachEmfClientComponents > gui > AttributesFrame


1 package ist.coach.coachEmfClientComponents.gui;
2
3 //import javax.swing.JTable;
4
import javax.swing.table.AbstractTableModel JavaDoc;
5
6 import javax.swing.DefaultCellEditor JavaDoc;
7
8 import javax.swing.JScrollPane JavaDoc;
9 import javax.swing.JFrame JavaDoc;
10 import javax.swing.JPanel JavaDoc;
11 import javax.swing.SwingUtilities JavaDoc;
12 import javax.swing.JOptionPane JavaDoc;
13 import javax.swing.JButton JavaDoc;
14 import java.awt.*;
15 import java.awt.event.*;
16 import java.util.Hashtable JavaDoc;
17 import java.util.Vector JavaDoc;
18
19 /**
20  * This is exactly like TableDemo, except that it uses a
21  * custom cell editor to validate integer input.
22  */

23 public class AttributesFrame extends JFrame JavaDoc
24 implements ActionListener{
25
26     GuiMaster master;
27     //String parent_key;
28
String JavaDoc object_type;
29     String JavaDoc visual_object_class;
30     ParamTableModel myModel;
31     String JavaDoc [] param_name = null;
32     boolean[] param_options = null;
33     MOTreeObject tree_object;
34
35     public AttributesFrame( String JavaDoc object_class,
36                             //String parent_key,
37
MOTreeObject tree_object,
38                             Object JavaDoc[] values,
39                             GuiMaster master,
40                             int x_pos, int y_pos) {
41
42         super(GuiMessages.options_properties);
43         this.master = master;
44         this.tree_object = tree_object;
45         //this.parent_key = parent_key;
46
visual_object_class = object_class;
47
48         try {
49             param_name = (String JavaDoc [])Class.forName(object_class).getDeclaredField("attributes_name").get(null);
50             object_type = (String JavaDoc)Class.forName(object_class).getDeclaredField("object_type").get(null);
51             param_options = (boolean [])Class.forName(object_class).getDeclaredField("attributes_readonly").get(null);
52
53         }
54         catch(java.lang.IllegalArgumentException JavaDoc e1) {
55             System.err.println("IllegalArgumentException");
56         }
57         catch(java.lang.IllegalAccessException JavaDoc e2) {
58             System.err.println("IllegalAccessException");
59         }
60         catch(java.lang.ClassNotFoundException JavaDoc e3) {
61             System.err.println("ClassNotFoundException" + e3.getMessage());
62         }
63         catch(java.lang.NoSuchFieldException JavaDoc e4) {
64             System.err.println("NoSuchFieldException");
65         }
66
67
68         myModel = new ParamTableModel(param_name, values, param_options);
69         JAttributesTable table = new JAttributesTable(myModel);
70         table.setPreferredScrollableViewportSize(new Dimension(300, 70));
71         table.setCellSelectionEnabled(true);
72
73         //Create the scroll pane and add the table to it.
74
JScrollPane JavaDoc scrollPane = new JScrollPane JavaDoc(table);
75
76         JButton JavaDoc update_button = new JButton JavaDoc(GuiMessages.update_str);
77         update_button.addActionListener(this);
78         update_button.setToolTipText(GuiMessages.update_tooltip_str);
79
80         JButton JavaDoc cancel_button = new JButton JavaDoc(GuiMessages.cancel_str);
81         cancel_button.addActionListener(this);
82         cancel_button.setToolTipText(GuiMessages.cancel_tooltip_str);
83
84         JPanel JavaDoc submitPanel = new JPanel JavaDoc(new FlowLayout());
85         submitPanel.add(update_button);
86         submitPanel.add(cancel_button);
87
88         //Add the scroll pane to this window.
89
getContentPane().add(scrollPane, BorderLayout.CENTER);
90         getContentPane().add(submitPanel, BorderLayout.SOUTH);
91
92         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
93         /*****
94         addWindowListener(new WindowAdapter() {
95             public void windowClosing(WindowEvent e) {
96                 System.exit(0);
97             }
98         });
99        *****/

100         setLocation(x_pos, y_pos);
101         pack();
102         setVisible(true);
103     }
104
105     public void actionPerformed(ActionEvent e) {
106
107         if (e.getActionCommand().equals(GuiMessages.update_str)) {
108
109             Hashtable JavaDoc updates = new Hashtable JavaDoc();
110             int table_size = myModel.getRowCount();
111             for(int i = 0; i < table_size ; i++) {
112
113                 if (! myModel.isCellEditable(i, 1))
114                     continue;
115
116                 updates.put(myModel.getValueAt(i, 0),
117                             myModel.getValueAt(i, 1));
118             }
119
120 /*****
121         try {
122             Class objectClass = Class.forName(visual_object_class);
123             java.lang.reflect.Method[] theMethods = objectClass.getMethods();
124             for (int i = 0; i < theMethods.length; i++) {
125                 System.err.println("Method[" + i + "] = " +
126                             theMethods[i].getName() + " returns " +
127                                 theMethods[i].getReturnType().getName() +
128                         " has parameters: ");
129
130                 Class [] parametersT = theMethods[i].getParameterTypes();
131                 for(int k=0; k < parametersT.length; k++)
132                     System.err.println("\t[" + k + "] = " + parametersT[k].getName());
133             }
134
135             Class[] partypes = new Class[1];
136             partypes[0] = (new Hashtable()).getClass();
137             java.lang.reflect.Method translate_updates = objectClass.getMethod(
138                     "translate_updates", partypes);
139
140             Object[] arguments = { updates };
141             //System.err.println("Before invoking method...");
142             Vector retobj = (Vector) translate_updates.invoke(null,
143                                                     arguments);
144         }
145         catch(java.lang.IllegalArgumentException e1) {
146             System.err.println("AttributesFrame> IllegalArgumentException");
147         }
148         catch(java.lang.IllegalAccessException e2) {
149             System.err.println("AttributesFrame> IllegalAccessException");
150         }
151         catch(java.lang.ClassNotFoundException e3) {
152             System.err.println("AttributesFrame> ClassNotFoundException" +
153                 e3.getMessage());
154         }
155         catch(java.lang.NoSuchMethodException e4) {
156             System.err.println("AttributesFrame> NoSuchMethodException" +
157                 e4.getMessage());
158             e4.printStackTrace();
159         }
160         catch(java.lang.reflect.InvocationTargetException e5) {
161             System.err.println("AttributesFrame> InvocationTargetException");
162         }
163 *****/

164             Vector JavaDoc retobj = tree_object.translate_updates(updates);
165
166             if (retobj == null || retobj.size() == 0) {
167                     JOptionPane.showMessageDialog(this,
168                         GuiMessages.update_nothing_error,
169                         GuiMessages.options_update,
170                         JOptionPane.WARNING_MESSAGE);
171
172                     this.dispose();
173                     return;
174             }
175
176             if (master.setAttributes( (String JavaDoc) myModel.getValueAt(0, 1), retobj)) {
177                     JOptionPane.showMessageDialog(this,
178                         GuiMessages.reply_ok,
179                         GuiMessages.options_update,
180                         JOptionPane.WARNING_MESSAGE);
181
182             }
183             else {
184                     JOptionPane.showMessageDialog(this,
185                         GuiMessages.reply_error,
186                         GuiMessages.options_update,
187                         JOptionPane.WARNING_MESSAGE);
188             }
189     }
190          this.dispose();
191     }
192 }
193
Popular Tags