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