dfa1922ea141a9488c246d9d8e488d7007fa6f48
[people/mcb30/edk2.git] / edk2 / Tools / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / packaging / ui / GenGuidDialog.java
1 /** @file\r
2   Java class GenGuidDialog.\r
3  \r
4 Copyright (c) 2006, Intel Corporation\r
5 All rights reserved. This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution.  The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9 \r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12 **/\r
13 package org.tianocore.frameworkwizard.packaging.ui;\r
14 \r
15 import java.awt.BorderLayout;\r
16 import java.awt.Dimension;\r
17 import java.awt.Toolkit;\r
18 \r
19 import javax.swing.JPanel;\r
20 import javax.swing.JDialog;\r
21 import java.awt.GridLayout;\r
22 \r
23 import javax.swing.JFrame;\r
24 import javax.swing.JOptionPane;\r
25 import javax.swing.JTextField;\r
26 import javax.swing.JLabel;\r
27 import javax.swing.JRadioButton;\r
28 import javax.swing.ButtonGroup;\r
29 import javax.swing.JButton;\r
30 \r
31 import org.tianocore.frameworkwizard.common.Tools;\r
32 \r
33 import java.awt.FlowLayout;\r
34 import java.awt.event.ActionEvent;\r
35 import java.awt.event.ActionListener;\r
36 import java.awt.event.WindowAdapter;\r
37 import java.awt.event.WindowEvent;\r
38 \r
39 /**\r
40  Dialog for GUID generation. \r
41  @since PackageEditor 1.0\r
42 **/\r
43 public class GenGuidDialog extends JDialog implements ActionListener{\r
44 \r
45     /**\r
46      * \r
47      */\r
48     private static final long serialVersionUID = 1L;\r
49 \r
50     public static final String guidArrayPat = "0x[a-fA-F0-9]{1,8},( )*0x[a-fA-F0-9]{1,4},( )*0x[a-fA-F0-9]{1,4}(,( )*\\{)?(,?( )*0x[a-fA-F0-9]{1,2}){8}( )*(\\})?";\r
51     \r
52     public static final String guidRegistryPat = "[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}";\r
53     \r
54     static private JFrame frame;\r
55     private JPanel jContentPane = null;\r
56     private JPanel jPanel = null;\r
57     private JPanel jPanel1 = null;\r
58     private JPanel jPanel2 = null;\r
59     private JPanel jPanel3 = null;\r
60     private JPanel jPanel4 = null;\r
61     private JTextField jTextField = null;\r
62     private JLabel jLabel = null;\r
63     private JRadioButton jRadioButton = null;\r
64     private JRadioButton jRadioButton1 = null;\r
65     private JButton jButton = null;\r
66     private JButton jButton1 = null;\r
67     private JButton jButton2 = null;\r
68     \r
69 //    private String guid = null;\r
70 \r
71     public void actionPerformed(ActionEvent arg0) {\r
72         // TODO Auto-generated method stub\r
73         if (arg0.getSource() == jButton1){\r
74             String uuid = Tools.generateUuidString();\r
75             if (jRadioButton1.isSelected()) {\r
76                 jTextField.setText(uuid);\r
77             }\r
78             else {\r
79                 //ToDo: transform to comma-sep guid\r
80                 String s = GenGuidDialog.formatGuidString(uuid);\r
81                 if (s.equals("0")) {\r
82                     JOptionPane.showMessageDialog(frame, "Check GUID Value, it don't conform to the schema.");\r
83                     return;\r
84                 }\r
85                 jTextField.setText(s);\r
86             }\r
87         }\r
88         \r
89         if (arg0.getSource() == jRadioButton1){\r
90             \r
91             //ToDo: check text field value against RegExp and transform if needed\r
92             if (jTextField.getText().matches(GenGuidDialog.guidRegistryPat)){\r
93                 return;\r
94             }\r
95             if (jTextField.getText().matches(GenGuidDialog.guidArrayPat)) {\r
96                 jTextField.setText(GenGuidDialog.formatGuidString(jTextField.getText()));\r
97                 return;\r
98             }\r
99             if (jTextField.getText().length()>0)\r
100             JOptionPane.showMessageDialog(frame, "Check GUID Value, it don't conform to the schema.");\r
101                     \r
102         }\r
103         \r
104         if (arg0.getSource() == jRadioButton){\r
105             \r
106             //ToDo: check text field value against RegExp and transform if needed\r
107             if (jTextField.getText().matches(GenGuidDialog.guidArrayPat)){\r
108                 return;\r
109             }\r
110             if (jTextField.getText().matches(GenGuidDialog.guidRegistryPat)) {\r
111                 jTextField.setText(GenGuidDialog.formatGuidString(jTextField.getText()));\r
112                 return;\r
113             }\r
114             if (jTextField.getText().length()>0)\r
115             JOptionPane.showMessageDialog(frame, "Check GUID Value, it don't conform to the schema.");\r
116             \r
117         }\r
118         \r
119         if (arg0.getSource() == jButton2){\r
120 //            if (jTextField.getText().matches(Tools.guidArrayPat) \r
121 //                            || jTextField.getText().matches(Tools.guidRegistryPat)){\r
122 //                this.setVisible(false);\r
123 //            }\r
124 //            else {\r
125 //                JOptionPane.showMessageDialog(frame, "Incorrect GUID Value Format.");\r
126 //            }\r
127             this.dispose();\r
128         }\r
129         \r
130         if (arg0.getSource() == jButton){\r
131             this.dispose();\r
132         }\r
133     }\r
134 \r
135     /**\r
136      * This method initializes jPanel   \r
137      *  \r
138      * @return javax.swing.JPanel       \r
139      */\r
140     private JPanel getJPanel() {\r
141         if (jPanel == null) {\r
142             FlowLayout flowLayout = new FlowLayout();\r
143             flowLayout.setVgap(10);\r
144             jPanel = new JPanel();\r
145             jPanel.setLayout(flowLayout);\r
146             jPanel.setPreferredSize(new java.awt.Dimension(100,30));\r
147             jPanel.add(getJButton1(), null);\r
148             jPanel.add(getJButton2(), null);\r
149             jPanel.add(getJButton(), null);\r
150         }\r
151         return jPanel;\r
152     }\r
153 \r
154     /**\r
155      * This method initializes jPanel1  \r
156      *  \r
157      * @return javax.swing.JPanel       \r
158      */\r
159     private JPanel getJPanel1() {\r
160         if (jPanel1 == null) {\r
161             jPanel1 = new JPanel();\r
162         }\r
163         return jPanel1;\r
164     }\r
165 \r
166     /**\r
167      * This method initializes jPanel2  \r
168      *  \r
169      * @return javax.swing.JPanel       \r
170      */\r
171     private JPanel getJPanel2() {\r
172         if (jPanel2 == null) {\r
173             jPanel2 = new JPanel();\r
174         }\r
175         return jPanel2;\r
176     }\r
177 \r
178     /**\r
179      * This method initializes jPanel3  \r
180      *  \r
181      * @return javax.swing.JPanel       \r
182      */\r
183     private JPanel getJPanel3() {\r
184         if (jPanel3 == null) {\r
185             jPanel3 = new JPanel();\r
186         }\r
187         return jPanel3;\r
188     }\r
189 \r
190     /**\r
191      * This method initializes jPanel4  \r
192      *  \r
193      * @return javax.swing.JPanel       \r
194      */\r
195     private JPanel getJPanel4() {\r
196         if (jPanel4 == null) {\r
197             jLabel = new JLabel();\r
198             jLabel.setText("GUID Value");\r
199             GridLayout gridLayout = new GridLayout();\r
200             gridLayout.setRows(4);\r
201             jPanel4 = new JPanel();\r
202             jPanel4.setLayout(gridLayout);\r
203             jPanel4.add(getJRadioButton1(), null);\r
204             jPanel4.add(getJRadioButton(), null);\r
205             jPanel4.add(jLabel, null);\r
206             jPanel4.add(getJTextField(), null);\r
207             ButtonGroup bg = new ButtonGroup();\r
208             bg.add(jRadioButton1);\r
209             bg.add(jRadioButton);\r
210         }\r
211         return jPanel4;\r
212     }\r
213 \r
214     /**\r
215      * This method initializes jTextField       \r
216      *  \r
217      * @return javax.swing.JTextField   \r
218      */\r
219     private JTextField getJTextField() {\r
220         if (jTextField == null) {\r
221             jTextField = new JTextField();\r
222             jTextField.setHorizontalAlignment(JTextField.LEADING);\r
223             jTextField.setPreferredSize(new java.awt.Dimension(100,20));\r
224         }\r
225         return jTextField;\r
226     }\r
227 \r
228     /**\r
229      * This method initializes jRadioButton     \r
230      *  \r
231      * @return javax.swing.JRadioButton \r
232      */\r
233     private JRadioButton getJRadioButton() {\r
234         if (jRadioButton == null) {\r
235             jRadioButton = new JRadioButton();\r
236             jRadioButton.setText("Comma-Seperated Format");\r
237             jRadioButton.setEnabled(false);\r
238             jRadioButton.addActionListener(this);\r
239         }\r
240         return jRadioButton;\r
241     }\r
242 \r
243     /**\r
244      * This method initializes jRadioButton1    \r
245      *  \r
246      * @return javax.swing.JRadioButton \r
247      */\r
248     private JRadioButton getJRadioButton1() {\r
249         if (jRadioButton1 == null) {\r
250             jRadioButton1 = new JRadioButton();\r
251             jRadioButton1.setText("Registry Format");\r
252             jRadioButton1.setSelected(true);\r
253             jRadioButton1.addActionListener(this);\r
254         }\r
255         return jRadioButton1;\r
256     }\r
257 \r
258     /**\r
259      * This method initializes jButton  \r
260      *  \r
261      * @return javax.swing.JButton      \r
262      */\r
263     private JButton getJButton() {\r
264         if (jButton == null) {\r
265             jButton = new JButton();\r
266             jButton.setPreferredSize(new java.awt.Dimension(80,20));\r
267             jButton.setText("Cancel");\r
268             jButton.addActionListener(this);\r
269         }\r
270         return jButton;\r
271     }\r
272 \r
273     /**\r
274      * This method initializes jButton1 \r
275      *  \r
276      * @return javax.swing.JButton      \r
277      */\r
278     private JButton getJButton1() {\r
279         if (jButton1 == null) {\r
280             jButton1 = new JButton();\r
281             jButton1.setPreferredSize(new java.awt.Dimension(80,20));\r
282             jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);\r
283             jButton1.setText("New");\r
284             jButton1.addActionListener(this);\r
285         }\r
286         return jButton1;\r
287     }\r
288 \r
289     /**\r
290      * This method initializes jButton2 \r
291      *  \r
292      * @return javax.swing.JButton      \r
293      */\r
294     private JButton getJButton2() {\r
295         if (jButton2 == null) {\r
296             jButton2 = new JButton();\r
297             jButton2.setPreferredSize(new java.awt.Dimension(80,20));\r
298             jButton2.setText("Ok");\r
299             jButton2.setActionCommand("GenGuidValue");\r
300             jButton2.addActionListener(this);\r
301         }\r
302         return jButton2;\r
303     }\r
304 \r
305     /**\r
306      \r
307      @param args\r
308      **/\r
309     public static void main(String[] args) {\r
310         // TODO Auto-generated method stub\r
311         new GenGuidDialog().setVisible(true);\r
312     }\r
313 \r
314     public String getGuid(){\r
315         return jTextField.getText();\r
316     }\r
317     \r
318     public void setGuid(String s){\r
319         jTextField.setText(s);\r
320     }\r
321     /**\r
322      * This is the default constructor\r
323      */\r
324     public GenGuidDialog() {\r
325         super();\r
326         initialize();\r
327     }\r
328     \r
329     public GenGuidDialog(ActionListener i){\r
330         super();\r
331         initialize();\r
332         jButton2.addActionListener(i);\r
333         this.addWindowListener(new WindowAdapter(){\r
334 \r
335             @Override\r
336             public void windowActivated(WindowEvent arg0) {\r
337                 // TODO Auto-generated method stub\r
338                 super.windowActivated(arg0);\r
339                 if ((jRadioButton1.isSelected() && jTextField.getText().matches(GenGuidDialog.guidArrayPat))\r
340                                 || (jRadioButton.isSelected() && jTextField.getText().matches(GenGuidDialog.guidRegistryPat))) {\r
341                     jTextField.setText(GenGuidDialog.formatGuidString(jTextField.getText()));\r
342                 }\r
343                 \r
344 //                if (!jTextField.getText().matches(Tools.guidArrayPat) || !jTextField.getText().matches(Tools.guidRegistryPat)) {\r
345 //                  JOptionPane.showMessageDialog(frame, "InitVal: Incorrect GUID Value Format.");\r
346 //                  return;\r
347 //                }\r
348             }\r
349             \r
350         });\r
351     }\r
352 \r
353     /**\r
354      * This method initializes this\r
355      * \r
356      * @return void\r
357      */\r
358     private void initialize() {\r
359         this.setSize(466, 157);\r
360         this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);\r
361         this.setModal(true);\r
362         this.setTitle("Editing GUID Value");\r
363         this.setContentPane(getJContentPane());\r
364         this.centerWindow();\r
365     }\r
366 \r
367     /**\r
368      * This method initializes jContentPane\r
369      * \r
370      * @return javax.swing.JPanel\r
371      */\r
372     private JPanel getJContentPane() {\r
373         if (jContentPane == null) {\r
374             jContentPane = new JPanel();\r
375             jContentPane.setLayout(new BorderLayout());\r
376             jContentPane.add(getJPanel(), java.awt.BorderLayout.EAST);\r
377             jContentPane.add(getJPanel1(), java.awt.BorderLayout.WEST);\r
378             jContentPane.add(getJPanel2(), java.awt.BorderLayout.NORTH);\r
379             jContentPane.add(getJPanel3(), java.awt.BorderLayout.SOUTH);\r
380             jContentPane.add(getJPanel4(), java.awt.BorderLayout.CENTER);\r
381         }\r
382         return jContentPane;\r
383     }\r
384 \r
385     /**\r
386      Start the window at the center of screen\r
387      \r
388      **/\r
389     protected void centerWindow(int intWidth, int intHeight) {\r
390         Dimension d = Toolkit.getDefaultToolkit().getScreenSize();\r
391         this.setLocation((d.width - intWidth) / 2, (d.height - intHeight) / 2);\r
392     }\r
393 \r
394     /**\r
395      Start the window at the center of screen\r
396      \r
397      **/\r
398     protected void centerWindow() {\r
399         centerWindow(this.getSize().width, this.getSize().height);\r
400     }\r
401     \r
402     public static String formatGuidString (String guidNameConv) {\r
403         String[] strList;\r
404         String guid = "";\r
405         int index = 0;\r
406         if (guidNameConv\r
407                         .matches(GenGuidDialog.guidRegistryPat)) {\r
408             strList = guidNameConv.split("-");\r
409             guid = "0x" + strList[0] + ", ";\r
410             guid = guid + "0x" + strList[1] + ", ";\r
411             guid = guid + "0x" + strList[2] + ", ";\r
412 \r
413             guid = guid + "0x" + strList[3].substring(0, 2) + ", ";\r
414             guid = guid + "0x" + strList[3].substring(2, 4);\r
415 \r
416             while (index < strList[4].length()) {\r
417                 guid = guid + ", ";\r
418                 guid = guid + "0x" + strList[4].substring(index, index + 2);\r
419                 index = index + 2;\r
420             }\r
421 \r
422             return guid;\r
423         }\r
424         else if (guidNameConv\r
425                         .matches(GenGuidDialog.guidArrayPat)) {\r
426             strList = guidNameConv.split(",");\r
427             \r
428             //\r
429             // chang ANSI c form to registry form\r
430             //\r
431             for (int i = 0; i < strList.length; i++){\r
432                 strList[i] = strList[i].substring(strList[i].lastIndexOf("x") + 1);\r
433             }\r
434             if (strList[strList.length - 1].endsWith("}")) {\r
435                 strList[strList.length -1] = strList[strList.length-1].substring(0, strList[strList.length-1].length()-1); \r
436             }\r
437             //\r
438             //inserting necessary leading zeros\r
439             //\r
440             \r
441             int segLen = strList[0].length();\r
442             if (segLen < 8){\r
443                 for (int i = 0; i < 8 - segLen; ++i){\r
444                     strList[0] = "0" + strList[0];\r
445                 }\r
446             }\r
447             \r
448             segLen = strList[1].length();\r
449             if (segLen < 4){\r
450                 for (int i = 0; i < 4 - segLen; ++i){\r
451                     strList[1] = "0" + strList[1];\r
452                 }\r
453             }\r
454             segLen = strList[2].length();\r
455             if (segLen < 4){\r
456                 for (int i = 0; i < 4 - segLen; ++i){\r
457                     strList[2] = "0" + strList[2];\r
458                 }\r
459             }\r
460             for (int i = 3; i < 11; ++i) {\r
461                 segLen = strList[i].length();\r
462                 if (segLen < 2){\r
463                     strList[i] = "0" + strList[i];\r
464                 }\r
465             }\r
466             \r
467             for (int i = 0; i < 3; i++){\r
468                 guid += strList[i] + "-";\r
469             }\r
470             \r
471             guid += strList[3];\r
472             guid += strList[4] + "-";\r
473             \r
474             for (int i = 5; i < strList.length; ++i){\r
475                 guid += strList[i];\r
476             }\r
477             \r
478             \r
479             return guid;\r
480         } else {\r
481             \r
482             return "0";\r
483 \r
484         }\r
485     }\r
486     \r
487 }  //  @jve:decl-index=0:visual-constraint="10,10"\r