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