6a794f41fed787b68129a1977cb7c3ea301f53c4
[mirror_edk2.git] / Tools / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / module / ui / dialog / ProtocolsDlg.java
1 /** @file
2
3 The file is used to create, update Protocol of MSA/MBD file
4
5 Copyright (c) 2006, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 package org.tianocore.frameworkwizard.module.ui.dialog;
17
18 import java.awt.event.ActionEvent;
19 import java.awt.event.ItemEvent;
20 import java.awt.event.ItemListener;
21 import java.util.Vector;
22
23 import javax.swing.JButton;
24 import javax.swing.JComboBox;
25 import javax.swing.JLabel;
26 import javax.swing.JPanel;
27 import javax.swing.JScrollPane;
28 import javax.swing.JTextField;
29
30 import org.tianocore.frameworkwizard.common.DataType;
31 import org.tianocore.frameworkwizard.common.DataValidation;
32 import org.tianocore.frameworkwizard.common.EnumerationData;
33 import org.tianocore.frameworkwizard.common.Log;
34 import org.tianocore.frameworkwizard.common.Tools;
35 import org.tianocore.frameworkwizard.common.ui.ArchCheckBox;
36 import org.tianocore.frameworkwizard.common.ui.IDialog;
37 import org.tianocore.frameworkwizard.common.ui.IFrame;
38 import org.tianocore.frameworkwizard.common.ui.StarLabel;
39 import org.tianocore.frameworkwizard.module.Identifications.Protocols.ProtocolsIdentification;
40 import org.tianocore.frameworkwizard.workspace.WorkspaceTools;
41
42 /**
43 The class is used to create, update Protocol of MSA/MBD file
44 It extends IInternalFrame
45
46
47
48 **/
49 public class ProtocolsDlg extends IDialog implements ItemListener {
50
51 ///
52 /// Define class Serial Version UID
53 ///
54 private static final long serialVersionUID = -9084913640747858848L;
55
56 //
57 //Define class members
58 //
59 private JPanel jContentPane = null;
60
61 private JLabel jLabelC_Name = null;
62
63 private JLabel jLabelFeatureFlag = null;
64
65 private JTextField jTextFieldFeatureFlag = null;
66
67 private JLabel jLabelUsage = null;
68
69 private JComboBox jComboBoxUsage = null;
70
71 private StarLabel jStarLabel1 = null;
72
73 private StarLabel jStarLabel2 = null;
74
75 private JLabel jLabelProtocolType = null;
76
77 private JLabel jLabelArch = null;
78
79 private JScrollPane jScrollPane = null;
80
81 private JComboBox jComboBoxProtocolType = null;
82
83 private JComboBox jComboBoxCName = null;
84
85 private JLabel jLabelHelpText = null;
86
87 private JTextField jTextFieldHelpText = null;
88
89 private ArchCheckBox jArchCheckBox = null;
90
91 private JButton jButtonOk = null;
92
93 private JButton jButtonCancel = null;
94
95 //
96 // Not used by UI
97 //
98 private ProtocolsIdentification id = null;
99
100 private WorkspaceTools wt = new WorkspaceTools();
101
102 private EnumerationData ed = new EnumerationData();
103
104 /**
105 This method initializes jTextFieldFeatureFlag
106
107 @return javax.swing.JTextField jTextFieldFeatureFlag
108
109 **/
110 private JTextField getJTextFieldFeatureFlag() {
111 if (jTextFieldFeatureFlag == null) {
112 jTextFieldFeatureFlag = new JTextField();
113 jTextFieldFeatureFlag.setBounds(new java.awt.Rectangle(160, 110, 320, 20));
114 jTextFieldFeatureFlag.setPreferredSize(new java.awt.Dimension(320, 20));
115 }
116 return jTextFieldFeatureFlag;
117 }
118
119 /**
120 This method initializes jComboBoxUsage
121
122 @return javax.swing.JComboBox jComboBoxUsage
123
124 **/
125 private JComboBox getJComboBoxProtocolUsage() {
126 if (jComboBoxUsage == null) {
127 jComboBoxUsage = new JComboBox();
128 jComboBoxUsage.setBounds(new java.awt.Rectangle(160, 60, 320, 20));
129 jComboBoxUsage.setPreferredSize(new java.awt.Dimension(320, 20));
130 jComboBoxUsage.setToolTipText("ALWAYS_CONSUMED is the only valid usage for type ProtocolNotify.");
131 }
132 return jComboBoxUsage;
133 }
134
135 /**
136 This method initializes jScrollPane
137
138 @return javax.swing.JScrollPane
139 */
140 private JScrollPane getJScrollPane() {
141 if (jScrollPane == null) {
142 jScrollPane = new JScrollPane();
143 jScrollPane.setViewportView(getJContentPane());
144 }
145 return jScrollPane;
146 }
147
148 /**
149 * This method initializes jComboBoxProtocolType
150 *
151 * @return javax.swing.JComboBox
152 */
153 private JComboBox getJComboBoxProtocolType() {
154 if (jComboBoxProtocolType == null) {
155 jComboBoxProtocolType = new JComboBox();
156 jComboBoxProtocolType.setBounds(new java.awt.Rectangle(160, 10, 320, 20));
157 jComboBoxProtocolType.setPreferredSize(new java.awt.Dimension(320, 20));
158 jComboBoxProtocolType.addItemListener(this);
159 jComboBoxProtocolType.setToolTipText("<html>Select Protocol Type<br>Protocol Notify is a register protocol notify mechanism.");
160 }
161 return jComboBoxProtocolType;
162 }
163
164 /**
165 * This method initializes jComboBoxCName
166 *
167 * @return javax.swing.JComboBox
168 */
169 private JComboBox getJComboBoxCName() {
170 if (jComboBoxCName == null) {
171 jComboBoxCName = new JComboBox();
172 jComboBoxCName.setBounds(new java.awt.Rectangle(160, 35, 320, 20));
173 jComboBoxCName.setPreferredSize(new java.awt.Dimension(320, 20));
174 jComboBoxCName.setToolTipText("Select Guid C Name of the Protocol");
175
176 }
177 return jComboBoxCName;
178 }
179
180 /**
181 This method initializes jTextFieldHelpText
182
183 @return javax.swing.JTextField
184
185 **/
186 private JTextField getJTextFieldHelpText() {
187 if (jTextFieldHelpText == null) {
188 jTextFieldHelpText = new JTextField();
189 jTextFieldHelpText.setBounds(new java.awt.Rectangle(160, 85, 320, 20));
190 jTextFieldHelpText.setPreferredSize(new java.awt.Dimension(320, 20));
191 }
192 return jTextFieldHelpText;
193 }
194
195 /**
196 This method initializes jButtonOk
197
198 @return javax.swing.JButton
199
200 **/
201 private JButton getJButtonOk() {
202 if (jButtonOk == null) {
203 jButtonOk = new JButton();
204 jButtonOk.setBounds(new java.awt.Rectangle(290, 165, 90, 20));
205 jButtonOk.setText("Ok");
206 jButtonOk.addActionListener(this);
207 }
208 return jButtonOk;
209 }
210
211 /**
212 This method initializes jButtonCancel
213
214 @return javax.swing.JButton
215
216 **/
217 private JButton getJButtonCancel() {
218 if (jButtonCancel == null) {
219 jButtonCancel = new JButton();
220 jButtonCancel.setBounds(new java.awt.Rectangle(390, 165, 90, 20));
221 jButtonCancel.setText("Cancel");
222 jButtonCancel.addActionListener(this);
223 }
224 return jButtonCancel;
225 }
226
227 public static void main(String[] args) {
228
229 }
230
231 /**
232 This method initializes this
233
234 **/
235 private void init() {
236 this.setSize(510, 240);
237 this.setContentPane(getJScrollPane());
238 this.setTitle("Protocols");
239 initFrame();
240 this.setViewMode(false);
241 this.centerWindow();
242 }
243
244 /**
245 This method initializes this
246 Fill values to all fields if these values are not empty
247
248 @param inProtocolsId
249
250 **/
251 private void init(ProtocolsIdentification inProtocolsId) {
252 init();
253 this.id = inProtocolsId;
254
255 if (this.id != null) {
256 this.jComboBoxCName.setSelectedItem(id.getName());
257 this.jComboBoxProtocolType.setSelectedItem(id.getType());
258 this.jComboBoxUsage.setSelectedItem(id.getUsage());
259 this.jTextFieldHelpText.setText(id.getHelp());
260
261 jTextFieldFeatureFlag.setText(id.getFeatureFlag());
262 this.jArchCheckBox.setSelectedItems(id.getSupArchList());
263 }
264 }
265
266 /**
267 This is the override edit constructor
268
269 @param inProtocolsIdentification
270 @param iFrame
271
272 **/
273 public ProtocolsDlg(ProtocolsIdentification inProtocolsIdentification, IFrame iFrame) {
274 super(iFrame, true);
275 init(inProtocolsIdentification);
276 }
277
278 /**
279 Disable all components when the mode is view
280
281 @param isView true - The view mode; false - The non-view mode
282
283 **/
284 public void setViewMode(boolean isView) {
285 if (isView) {
286 this.jComboBoxUsage.setEnabled(!isView);
287 this.jTextFieldFeatureFlag.setEnabled(!isView);
288 }
289 }
290
291 /**
292 This method initializes jContentPane
293
294 @return javax.swing.JPanel jContentPane
295
296 **/
297 private JPanel getJContentPane() {
298 if (jContentPane == null) {
299 jArchCheckBox = new ArchCheckBox();
300 jArchCheckBox.setBounds(new java.awt.Rectangle(160, 135, 320, 20));
301 jArchCheckBox.setPreferredSize(new java.awt.Dimension(320, 20));
302 jLabelHelpText = new JLabel();
303 jLabelHelpText.setBounds(new java.awt.Rectangle(14, 85, 140, 20));
304 jLabelHelpText.setText("Help Text");
305 jLabelArch = new JLabel();
306 jLabelArch.setBounds(new java.awt.Rectangle(15, 135, 140, 20));
307 jLabelArch.setText("Arch Type");
308 jLabelProtocolType = new JLabel();
309 jLabelProtocolType.setBounds(new java.awt.Rectangle(15, 10, 140, 20));
310 jLabelProtocolType.setText("Protocol Type");
311 jLabelUsage = new JLabel();
312 jLabelUsage.setText("Usage");
313 jLabelUsage.setBounds(new java.awt.Rectangle(15, 60, 140, 20));
314 jLabelFeatureFlag = new JLabel();
315 jLabelFeatureFlag.setText("Feature Flag");
316 jLabelFeatureFlag.setBounds(new java.awt.Rectangle(15, 110, 140, 20));
317 jLabelC_Name = new JLabel();
318 jLabelC_Name.setText("C Name Type");
319 jLabelC_Name.setBounds(new java.awt.Rectangle(15, 35, 140, 20));
320
321 jContentPane = new JPanel();
322 jContentPane.setLayout(null);
323 jContentPane.setPreferredSize(new java.awt.Dimension(490, 190));
324
325 jContentPane.add(jLabelC_Name, null);
326 jContentPane.add(jLabelFeatureFlag, null);
327 jContentPane.add(getJTextFieldFeatureFlag(), null);
328 jContentPane.add(jLabelUsage, null);
329 jContentPane.add(getJComboBoxProtocolUsage(), null);
330 jContentPane.add(jLabelProtocolType, null);
331
332 jStarLabel1 = new StarLabel();
333 jStarLabel1.setBounds(new java.awt.Rectangle(0, 10, 10, 20));
334 jStarLabel2 = new StarLabel();
335 jStarLabel2.setBounds(new java.awt.Rectangle(0, 35, 10, 20));
336
337 jContentPane.add(jStarLabel1, null);
338 jContentPane.add(jStarLabel2, null);
339 jContentPane.add(jLabelArch, null);
340 jContentPane.add(getJComboBoxProtocolType(), null);
341 jContentPane.add(getJComboBoxCName(), null);
342 jContentPane.add(jLabelHelpText, null);
343 jContentPane.add(getJTextFieldHelpText(), null);
344 jContentPane.add(jArchCheckBox, null);
345 jContentPane.add(getJButtonOk(), null);
346 jContentPane.add(getJButtonCancel(), null);
347 }
348 return jContentPane;
349 }
350
351 /**
352 This method initializes Usage type
353
354 **/
355 private void initFrame() {
356 Tools.generateComboBoxByVector(jComboBoxProtocolType, ed.getVProtocolType());
357 Tools.generateComboBoxByVector(jComboBoxCName, wt.getAllProtocolDeclarationsFromWorkspace());
358 Tools.generateComboBoxByVector(jComboBoxUsage, ed.getVProtocolUsage());
359 }
360
361 /* (non-Javadoc)
362 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
363 *
364 * Override actionPerformed to listen all actions
365 *
366 */
367 public void actionPerformed(ActionEvent arg0) {
368 if (arg0.getSource() == jButtonOk) {
369 if (checkAdd()) {
370 getCurrentProtocols();
371 this.returnType = DataType.RETURN_TYPE_OK;
372 this.setVisible(false);
373 }
374 }
375
376 if (arg0.getSource() == jButtonCancel) {
377 this.returnType = DataType.RETURN_TYPE_CANCEL;
378 this.setVisible(false);
379 }
380 }
381
382 /**
383 Data validation for all fields
384
385 @retval true - All datas are valid
386 @retval false - At least one data is invalid
387
388 **/
389 public boolean checkAdd() {
390 //
391 // Check if all fields have correct data types
392 //
393
394 //
395 // Check Name
396 //
397 if (!isEmpty(this.jComboBoxCName.getSelectedItem().toString())) {
398 if (!DataValidation.isC_NameType(this.jComboBoxCName.getSelectedItem().toString())) {
399 Log.wrn("Update Protocols", "Incorrect data type for Protocol/ProtocolNotify Name");
400 return false;
401 }
402 }
403
404 //
405 // Check FeatureFlag
406 //
407 if (!isEmpty(this.jTextFieldFeatureFlag.getText())) {
408 if (!DataValidation.isFeatureFlag(this.jTextFieldFeatureFlag.getText())) {
409 Log.wrn("Update Protocols", "Incorrect data type for Feature Flag");
410 return false;
411 }
412 }
413
414 return true;
415 }
416
417 private ProtocolsIdentification getCurrentProtocols() {
418 String arg0 = this.jComboBoxCName.getSelectedItem().toString();
419 String arg1 = this.jComboBoxProtocolType.getSelectedItem().toString();
420 String arg2 = this.jComboBoxUsage.getSelectedItem().toString();
421
422 String arg3 = this.jTextFieldFeatureFlag.getText();
423 Vector<String> arg4 = this.jArchCheckBox.getSelectedItemsVector();
424 String arg5 = this.jTextFieldHelpText.getText();
425 id = new ProtocolsIdentification(arg0, arg1, arg2, arg3, arg4, arg5);
426 return id;
427 }
428
429 /* (non-Javadoc)
430 * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
431 *
432 * Reflesh the frame when selected item changed
433 *
434 */
435 public void itemStateChanged(ItemEvent arg0) {
436 if (arg0.getSource() == this.jComboBoxProtocolType && arg0.getStateChange() == ItemEvent.SELECTED) {
437 if (this.jComboBoxProtocolType.getSelectedItem().toString().equals(ed.getVProtocolType().get(0))) {
438 Tools.generateComboBoxByVector(this.jComboBoxUsage, ed.getVProtocolUsage());
439 } else {
440 Tools.generateComboBoxByVector(this.jComboBoxUsage, ed.getVProtocolNotifyUsage());
441 }
442 }
443 }
444
445 public ProtocolsIdentification getId() {
446 return id;
447 }
448
449 public void setId(ProtocolsIdentification id) {
450 this.id = id;
451 }
452 }