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