KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > corbaclient > MapsNTVDPanel


1 package org.enhydra.shark.corbaclient;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.util.*;
6
7 import javax.swing.*;
8 import java.text.SimpleDateFormat JavaDoc;
9
10
11 /**
12  * Used to show one workflow variable and edit it.
13  *
14  * @author Sasa Bojanic
15  * @version 1.0
16  */

17 public class MapsNTVDPanel extends ActionPanel {
18
19    private static Dimension labelFieldDimension=new Dimension(250,20);
20    private static Dimension textFieldDimension=new Dimension(250,20);
21    private static Dimension buttonFieldDimension=new Dimension(50,25);
22
23    private String JavaDoc varId;
24    private String JavaDoc varName;
25    private Object JavaDoc val;
26
27    private JLabel name=new JLabel();
28    private JLabel type=new JLabel();
29    private JTextField value;
30    private JComboBox choices;
31    private JTextField vDay;
32    private JTextField vMonth;
33    private JTextField vYear;
34    private JButton description;
35
36    private String JavaDoc typeKey;
37    private String JavaDoc desc;
38    private boolean isReadOnly;
39
40    public MapsNTVDPanel(String JavaDoc varId,Object JavaDoc val,String JavaDoc nm,String JavaDoc d,String JavaDoc typeKey,boolean isReadOnly) {
41       super();
42       this.varId=varId;
43       this.val=val;
44       this.varName=nm;
45       this.desc=d;
46       this.typeKey=typeKey;
47       this.isReadOnly=isReadOnly;
48       super.init();
49    }
50
51    protected void createActions () {}
52
53    protected Component createCenterComponent () {
54       JPanel p=new JPanel();
55       p.setLayout(new BoxLayout(p,BoxLayout.X_AXIS));
56       p.add(Box.createHorizontalStrut(5));
57       p.add(Box.createHorizontalGlue());
58       if (varName!=null && varName.trim().length()>0) {
59          name.setText(varName);
60       } else {
61          name.setText(varId);
62       }
63       name.setMinimumSize(new Dimension(labelFieldDimension));
64       name.setPreferredSize(new Dimension(labelFieldDimension));
65       name.setMaximumSize(new Dimension(labelFieldDimension));
66       p.add(name);
67       p.add(Box.createHorizontalStrut(5));
68
69       type.setText(ResourceManager.getLanguageDependentString(typeKey));
70       type.setMinimumSize(new Dimension(labelFieldDimension));
71       type.setPreferredSize(new Dimension(labelFieldDimension));
72       type.setMaximumSize(new Dimension(labelFieldDimension));
73       p.add(type);
74       p.add(Box.createHorizontalStrut(5));
75
76       //System.out.println("Type="+typeKey);
77
if (typeKey.equals(WorkflowUtilities.UNKNOWN_KEY)) {
78          value=new JTextField();
79          value.setText(ResourceManager.getLanguageDependentString(
80                           WorkflowUtilities.UNKNOWN_KEY));
81          value.setEnabled(false);
82          value.setMinimumSize(new Dimension(textFieldDimension));
83          value.setPreferredSize(new Dimension(textFieldDimension));
84          value.setMaximumSize(new Dimension(textFieldDimension));
85          //value.setEnabled(!isReadOnly);
86
p.add(value);
87       } else if (typeKey.equals(WorkflowUtilities.BOOLEAN_KEY)) {
88          Vector vChc=new Vector();
89          vChc.add("true");
90          vChc.add("false");
91          choices=new JComboBox(vChc);
92          choices.setSelectedItem(val.toString());
93          choices.setMinimumSize(new Dimension(textFieldDimension));
94          choices.setPreferredSize(new Dimension(textFieldDimension));
95          choices.setMaximumSize(new Dimension(textFieldDimension));
96          choices.setEnabled(!isReadOnly);
97          p.add(choices);
98       } else {
99          if (val!=null && val instanceof Date) {
100             vDay=new JTextField();
101             vMonth=new JTextField();
102             vYear=new JTextField();
103             Date dte=(Date)val;
104             Calendar c=Calendar.getInstance();
105             c.setTime(dte);
106
107             vYear.setText(String.valueOf(c.get(Calendar.YEAR)));
108             vMonth.setText(String.valueOf(c.get(Calendar.MONTH)+1));
109             vDay.setText(String.valueOf(c.get(Calendar.DAY_OF_MONTH)));
110             vYear.setMinimumSize(new Dimension((int)(4*(textFieldDimension.width-10)/8),textFieldDimension.height));
111             vYear.setPreferredSize(new Dimension((int)(4*(textFieldDimension.width-10)/8),textFieldDimension.height));
112             vYear.setMaximumSize(new Dimension((int)(4*(textFieldDimension.width-10)/8),textFieldDimension.height));
113             vYear.setEnabled(!isReadOnly);
114             vMonth.setMinimumSize(new Dimension((int)(2*(textFieldDimension.width-10)/8),textFieldDimension.height));
115             vMonth.setPreferredSize(new Dimension((int)(2*(textFieldDimension.width-10)/8),textFieldDimension.height));
116             vMonth.setMaximumSize(new Dimension((int)(2*(textFieldDimension.width-10)/8),textFieldDimension.height));
117             vMonth.setEnabled(!isReadOnly);
118             vDay.setMinimumSize(new Dimension((int)(2*(textFieldDimension.width-10)/8),textFieldDimension.height));
119             vDay.setPreferredSize(new Dimension((int)(2*(textFieldDimension.width-10)/8),textFieldDimension.height));
120             vDay.setMaximumSize(new Dimension((int)(2*(textFieldDimension.width-10)/8),textFieldDimension.height));
121             vDay.setEnabled(!isReadOnly);
122
123             p.add(vYear);
124             p.add(Box.createHorizontalStrut(5));
125             p.add(vMonth);
126             p.add(Box.createHorizontalStrut(5));
127             p.add(vDay);
128          } else {
129             value=new JTextField();
130             if (val==null) {
131                value.setText("");
132             } else {
133                value.setText(val.toString());
134             }
135             value.setMinimumSize(new Dimension(textFieldDimension));
136             value.setPreferredSize(new Dimension(textFieldDimension));
137             value.setMaximumSize(new Dimension(textFieldDimension));
138             value.setEnabled(!isReadOnly);
139             p.add(value);
140          }
141       }
142       p.add(Box.createHorizontalStrut(5));
143
144       description=(JButton)BarFactory.
145          createButton("VariableDescription",null,false);
146       description.setMinimumSize(new Dimension(buttonFieldDimension));
147       description.setPreferredSize(new Dimension(buttonFieldDimension));
148       description.setMaximumSize(new Dimension(buttonFieldDimension));
149       description.addActionListener(new ActionListener () {
150                public void actionPerformed (ActionEvent ae) {
151                   String JavaDoc dk=ResourceManager.getLanguageDependentString("DescriptionKey");
152                   ItemView iv=new ItemView(getWindow(),
153                                            dk+" - "+((varName==null || varName.trim().length()==0)?varId:varName),dk,desc);
154                   iv.showDialog();
155                }
156             });
157       description.setEnabled(true);
158       p.add(description);
159       p.add(Box.createHorizontalStrut(5));
160       return p;
161    }
162
163    public void requestFocus() {
164       super.requestFocus();
165       if (value!=null) {
166          value.requestFocus();
167       } else if (choices!=null) {
168          choices.requestFocus();
169       }
170    }
171
172    public boolean updateField () {
173       boolean updated=false;
174       if (isReadOnly) return false;
175       if (value!=null) {
176          Object JavaDoc a=MapsNTVDPanel.insertValueIntoAnyObject(
177             val,value.getText(),typeKey);
178          if (a!=null) {
179             updated=true;
180             val=a;
181          }
182       } else if (choices!=null) {
183          Object JavaDoc a=MapsNTVDPanel.insertValueIntoAnyObject(
184             val,choices.getSelectedItem().toString(),typeKey);
185          if (a!=null) {
186             updated=true;
187             val=a;
188          }
189       } else if (vYear!=null) {
190          String JavaDoc dte=vYear.getText()+"-"+vMonth.getText()+"-"+vDay.getText();
191          Object JavaDoc a=MapsNTVDPanel.insertValueIntoAnyObject(
192             val,dte,typeKey);
193          if (a!=null) {
194             updated=true;
195             val=a;
196          }
197       }
198       return (updated || typeKey.equals(WorkflowUtilities.UNKNOWN_KEY));
199    }
200
201    public String JavaDoc getTypeKey () {
202       return typeKey;
203    }
204
205    public boolean isReadOnly () {
206       return isReadOnly;
207    }
208
209    public String JavaDoc getVariableId () {
210       return varId;
211    }
212
213    public Object JavaDoc getVariableValue () {
214       return val;
215    }
216
217    public static Object JavaDoc insertValueIntoAnyObject (Object JavaDoc theValueHolder,String JavaDoc txtVal,String JavaDoc typeKey) {
218       try {
219          if (typeKey.equals(WorkflowUtilities.BOOLEAN_KEY)) {
220             if (txtVal.equalsIgnoreCase("true") || txtVal.equalsIgnoreCase("false")) {
221                Boolean JavaDoc val=new Boolean JavaDoc(txtVal);
222                theValueHolder=val;
223             } else {
224                return null;
225             }
226          } else if (typeKey.equals(WorkflowUtilities.STRING_KEY)) {
227             theValueHolder=txtVal;
228          } else if (typeKey.equals(WorkflowUtilities.DATE_KEY)) {
229             System.out.println("dte="+txtVal);
230             SimpleDateFormat JavaDoc sdf=new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss");
231             theValueHolder=sdf.parse(txtVal);
232             System.out.println("dtec="+theValueHolder);
233          } else if (typeKey.equals(WorkflowUtilities.INTEGER_KEY)) {
234             theValueHolder=new Long JavaDoc(txtVal);
235          } else if (typeKey.equals(WorkflowUtilities.DOUBLE_KEY)) {
236             theValueHolder=new Double JavaDoc(txtVal);
237          }/* else if (typeKey.equals(ENUMERATION_KEY)) {
238              ORB orb=SharkClient.getORB();
239              org.omg.CORBA.Object obj = null;
240              obj=orb.resolve_initial_references ("DynAnyFactory");
241              // narrow the reference to the correct type
242              DynAnyFactory daf = DynAnyFactoryHelper.narrow (obj);
243              org.omg.DynamicAny.DynEnum de=(org.omg.DynamicAny.DynEnum)daf.create_dyn_any(theValueHolder);
244              de.set_as_string(txtVal);
245              theValueHolder=de.to_any();
246           }*/
else {
247             System.out.println("Unknown type");
248             return null;
249          }
250       } catch (Exception JavaDoc ex) {
251          System.out.println("Wrong value");
252          ex.printStackTrace();
253          return null;
254       }
255       return theValueHolder;
256    }
257
258
259 }
260
Popular Tags