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