KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > swingclient > MapPanel


1 package org.enhydra.shark.swingclient;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.util.*;
6
7 import javax.swing.*;
8
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 MapPanel 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 HashMap 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    private JPanel mainPanel;
41    private Set ntvdpanels=new HashSet();
42
43    public MapPanel(String JavaDoc varId,HashMap val,String JavaDoc nm,String JavaDoc d,String JavaDoc typeKey,boolean isReadOnly) {
44       super();
45       this.varId=varId;
46       this.val=val;
47       this.varName=nm;
48       this.desc=d;
49       this.typeKey=typeKey;
50       this.isReadOnly=isReadOnly;
51       super.init();
52    }
53
54    protected void createActions () {}
55
56    protected Component createCenterComponent() {
57       JScrollPane jsp=new JScrollPane();
58       JPanel panel = new JPanel();
59       panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
60       panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
61
62       JPanel p=new JPanel();
63       p.setLayout(new BoxLayout(p,BoxLayout.X_AXIS));
64       p.add(Box.createHorizontalStrut(5));
65       if (varName!=null && varName.trim().length()>0) {
66          name.setText(varName);
67       } else {
68          name.setText(varId);
69       }
70       name.setMinimumSize(new Dimension(labelFieldDimension));
71       name.setPreferredSize(new Dimension(labelFieldDimension));
72       name.setMaximumSize(new Dimension(labelFieldDimension));
73       p.add(name);
74       p.add(Box.createHorizontalStrut(5));
75
76       type.setText(ResourceManager.getLanguageDependentString(typeKey));
77       type.setMinimumSize(new Dimension(labelFieldDimension));
78       type.setPreferredSize(new Dimension(labelFieldDimension));
79       type.setMaximumSize(new Dimension(labelFieldDimension));
80       p.add(type);
81       p.add(Box.createHorizontalStrut(5));
82
83       description=(JButton)BarFactory.
84          createButton("VariableDescription",null,false);
85       description.setMinimumSize(new Dimension(buttonFieldDimension));
86       description.setPreferredSize(new Dimension(buttonFieldDimension));
87       description.setMaximumSize(new Dimension(buttonFieldDimension));
88       description.addActionListener(new ActionListener () {
89                public void actionPerformed (ActionEvent ae) {
90                   String JavaDoc dk=ResourceManager.getLanguageDependentString("DescriptionKey");
91                   ItemView iv=new ItemView(getWindow(),
92                                            dk+" - "+((varName==null || varName.trim().length()==0)?varId:varName),dk,desc);
93                   iv.showDialog();
94                }
95             });
96       description.setEnabled(true);
97       p.add(description);
98       p.add(Box.createHorizontalGlue());
99
100       panel.add(p);
101       if (val!=null) {
102          int i=0;
103          for (Iterator it=val.entrySet().iterator(); it.hasNext(); i++) {
104             Map.Entry me=(Map.Entry)it.next();
105             String JavaDoc varId=(String JavaDoc)me.getKey();
106             Object JavaDoc varVal=me.getValue();
107             try {
108                String JavaDoc varType;
109                if (varVal!=null) {
110                   varType=WorkflowUtilities.getTypeKeyOfAnyObject(varVal);
111                } else {
112                   varType=WorkflowUtilities.UNKNOWN_KEY;
113                }
114                ActionPanel ntvdPanel;
115                //System.out.println("Var "+varId+", val="+varVal+", varType="+varType);
116
if (!varType.equals(WorkflowUtilities.MAP_KEY)) {
117                   ntvdPanel=
118                      new NTVDPanel(varId,
119                                    varVal,
120                                    varId,
121                                    "",
122                                    varType,
123                                    isReadOnly);
124                } else {
125                   ntvdPanel=
126                      new MapPanel(varId,
127                                      (HashMap)varVal,
128                                   varId,
129                                   "",
130                                   varType,
131                                   isReadOnly);
132                }
133                panel.add(ntvdPanel);
134                panel.add(Box.createVerticalStrut(5));
135                ntvdpanels.add(ntvdPanel);
136             } catch (Exception JavaDoc ex) {ex.printStackTrace();}
137          }
138       }
139       panel.add(Box.createVerticalGlue());
140
141       jsp.setViewportView(panel);
142       return jsp;
143    }
144
145    public void requestFocus() {
146       super.requestFocus();
147       if (value!=null) {
148          value.requestFocus();
149       } else if (choices!=null) {
150          choices.requestFocus();
151       }
152    }
153
154    public boolean updateFields () {
155       Iterator nvs=ntvdpanels.iterator();
156       while (nvs.hasNext()) {
157          Object JavaDoc p=nvs.next();
158          if (p instanceof NTVDPanel) {
159             NTVDPanel ntvdp=(NTVDPanel)p;
160             if (!ntvdp.updateField() && !ntvdp.isReadOnly()) {
161                JOptionPane.showMessageDialog(this,
162                                              ResourceManager.getLanguageDependentString("ErrorUncorrectType"),
163                                              ResourceManager.getLanguageDependentString("DialogUpdateProcessVariables"),
164                                              JOptionPane.ERROR_MESSAGE);
165                ntvdp.requestFocus();
166                return false;
167             }
168          } else if (p instanceof MapPanel) {
169             MapPanel mp=(MapPanel)p;
170             if (!mp.updateFields() && !mp.isReadOnly()) {
171                return false;
172             }
173          }
174       }
175       nvs=ntvdpanels.iterator();
176       while (nvs.hasNext()) {
177          Object JavaDoc p=nvs.next();
178          if (p instanceof NTVDPanel) {
179             NTVDPanel ntvdp=(NTVDPanel)p;
180             if (!ntvdp.isReadOnly()) {
181                val.put(ntvdp.getVariableId(),ntvdp.getVariableValue());
182             }
183          } else if (p instanceof MapPanel) {
184             MapPanel mp=(MapPanel)p;
185             if (!mp.isReadOnly()) {
186                val.put(mp.getVariableId(),mp.getVariableValue());
187             }
188          }
189       }
190       return true;
191    }
192
193    public String JavaDoc getTypeKey () {
194       return typeKey;
195    }
196
197    public boolean isReadOnly () {
198       return isReadOnly;
199    }
200
201    public String JavaDoc getVariableId () {
202       return varId;
203    }
204
205    public Object JavaDoc getVariableValue () {
206       return val;
207    }
208
209 }
210
Popular Tags