2 Java class GenGuidDialog.
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
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.
13 package org
.tianocore
.frameworkwizard
.packaging
.ui
;
15 import java
.awt
.BorderLayout
;
16 import java
.awt
.Dimension
;
17 import java
.awt
.Toolkit
;
19 import javax
.swing
.JPanel
;
20 import javax
.swing
.JDialog
;
21 import java
.awt
.GridLayout
;
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
;
31 import org
.tianocore
.frameworkwizard
.common
.Tools
;
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
;
40 Dialog for GUID generation.
41 @since PackageEditor 1.0
43 public class GenGuidDialog
extends JDialog
implements ActionListener
{
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}( )*(\\})?";
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}";
49 static private JFrame frame
;
50 private JPanel jContentPane
= null;
51 private JPanel jPanel
= null;
52 private JPanel jPanel1
= null;
53 private JPanel jPanel2
= null;
54 private JPanel jPanel3
= null;
55 private JPanel jPanel4
= null;
56 private JTextField jTextField
= null;
57 private JLabel jLabel
= null;
58 private JRadioButton jRadioButton
= null;
59 private JRadioButton jRadioButton1
= null;
60 private JButton jButton
= null;
61 private JButton jButton1
= null;
62 private JButton jButton2
= null;
64 // private String guid = null;
66 public void actionPerformed(ActionEvent arg0
) {
67 // TODO Auto-generated method stub
68 if (arg0
.getSource() == jButton1
){
69 String uuid
= Tools
.generateUuidString();
70 if (jRadioButton1
.isSelected()) {
71 jTextField
.setText(uuid
);
74 //ToDo: transform to comma-sep guid
75 String s
= GenGuidDialog
.formatGuidString(uuid
);
77 JOptionPane
.showMessageDialog(frame
, "Check GUID Value, it don't conform to the schema.");
80 jTextField
.setText(s
);
84 if (arg0
.getSource() == jRadioButton1
){
86 //ToDo: check text field value against RegExp and transform if needed
87 if (jTextField
.getText().matches(GenGuidDialog
.guidRegistryPat
)){
90 if (jTextField
.getText().matches(GenGuidDialog
.guidArrayPat
)) {
91 jTextField
.setText(GenGuidDialog
.formatGuidString(jTextField
.getText()));
94 if (jTextField
.getText().length()>0)
95 JOptionPane
.showMessageDialog(frame
, "Check GUID Value, it don't conform to the schema.");
99 if (arg0
.getSource() == jRadioButton
){
101 //ToDo: check text field value against RegExp and transform if needed
102 if (jTextField
.getText().matches(GenGuidDialog
.guidArrayPat
)){
105 if (jTextField
.getText().matches(GenGuidDialog
.guidRegistryPat
)) {
106 jTextField
.setText(GenGuidDialog
.formatGuidString(jTextField
.getText()));
109 if (jTextField
.getText().length()>0)
110 JOptionPane
.showMessageDialog(frame
, "Check GUID Value, it don't conform to the schema.");
114 if (arg0
.getSource() == jButton2
){
115 // if (jTextField.getText().matches(Tools.guidArrayPat)
116 // || jTextField.getText().matches(Tools.guidRegistryPat)){
117 // this.setVisible(false);
120 // JOptionPane.showMessageDialog(frame, "Incorrect GUID Value Format.");
125 if (arg0
.getSource() == jButton
){
131 * This method initializes jPanel
133 * @return javax.swing.JPanel
135 private JPanel
getJPanel() {
136 if (jPanel
== null) {
137 FlowLayout flowLayout
= new FlowLayout();
138 flowLayout
.setVgap(10);
139 jPanel
= new JPanel();
140 jPanel
.setLayout(flowLayout
);
141 jPanel
.setPreferredSize(new java
.awt
.Dimension(100,30));
142 jPanel
.add(getJButton1(), null);
143 jPanel
.add(getJButton2(), null);
144 jPanel
.add(getJButton(), null);
150 * This method initializes jPanel1
152 * @return javax.swing.JPanel
154 private JPanel
getJPanel1() {
155 if (jPanel1
== null) {
156 jPanel1
= new JPanel();
162 * This method initializes jPanel2
164 * @return javax.swing.JPanel
166 private JPanel
getJPanel2() {
167 if (jPanel2
== null) {
168 jPanel2
= new JPanel();
174 * This method initializes jPanel3
176 * @return javax.swing.JPanel
178 private JPanel
getJPanel3() {
179 if (jPanel3
== null) {
180 jPanel3
= new JPanel();
186 * This method initializes jPanel4
188 * @return javax.swing.JPanel
190 private JPanel
getJPanel4() {
191 if (jPanel4
== null) {
192 jLabel
= new JLabel();
193 jLabel
.setText("GUID Value");
194 GridLayout gridLayout
= new GridLayout();
195 gridLayout
.setRows(4);
196 jPanel4
= new JPanel();
197 jPanel4
.setLayout(gridLayout
);
198 jPanel4
.add(getJRadioButton1(), null);
199 jPanel4
.add(getJRadioButton(), null);
200 jPanel4
.add(jLabel
, null);
201 jPanel4
.add(getJTextField(), null);
202 ButtonGroup bg
= new ButtonGroup();
203 bg
.add(jRadioButton1
);
204 bg
.add(jRadioButton
);
210 * This method initializes jTextField
212 * @return javax.swing.JTextField
214 private JTextField
getJTextField() {
215 if (jTextField
== null) {
216 jTextField
= new JTextField();
217 jTextField
.setHorizontalAlignment(JTextField
.LEADING
);
218 jTextField
.setPreferredSize(new java
.awt
.Dimension(100,20));
224 * This method initializes jRadioButton
226 * @return javax.swing.JRadioButton
228 private JRadioButton
getJRadioButton() {
229 if (jRadioButton
== null) {
230 jRadioButton
= new JRadioButton();
231 jRadioButton
.setText("Comma-Seperated Format");
232 jRadioButton
.setEnabled(false);
233 jRadioButton
.addActionListener(this);
239 * This method initializes jRadioButton1
241 * @return javax.swing.JRadioButton
243 private JRadioButton
getJRadioButton1() {
244 if (jRadioButton1
== null) {
245 jRadioButton1
= new JRadioButton();
246 jRadioButton1
.setText("Registry Format");
247 jRadioButton1
.setSelected(true);
248 jRadioButton1
.addActionListener(this);
250 return jRadioButton1
;
254 * This method initializes jButton
256 * @return javax.swing.JButton
258 private JButton
getJButton() {
259 if (jButton
== null) {
260 jButton
= new JButton();
261 jButton
.setPreferredSize(new java
.awt
.Dimension(80,20));
262 jButton
.setText("Cancel");
263 jButton
.addActionListener(this);
269 * This method initializes jButton1
271 * @return javax.swing.JButton
273 private JButton
getJButton1() {
274 if (jButton1
== null) {
275 jButton1
= new JButton();
276 jButton1
.setPreferredSize(new java
.awt
.Dimension(80,20));
277 jButton1
.setHorizontalTextPosition(javax
.swing
.SwingConstants
.LEADING
);
278 jButton1
.setText("New");
279 jButton1
.addActionListener(this);
285 * This method initializes jButton2
287 * @return javax.swing.JButton
289 private JButton
getJButton2() {
290 if (jButton2
== null) {
291 jButton2
= new JButton();
292 jButton2
.setPreferredSize(new java
.awt
.Dimension(80,20));
293 jButton2
.setText("Ok");
294 jButton2
.setActionCommand("GenGuidValue");
295 jButton2
.addActionListener(this);
304 public static void main(String
[] args
) {
305 // TODO Auto-generated method stub
306 new GenGuidDialog().setVisible(true);
309 public String
getGuid(){
310 return jTextField
.getText();
313 public void setGuid(String s
){
314 jTextField
.setText(s
);
317 * This is the default constructor
319 public GenGuidDialog() {
324 public GenGuidDialog(ActionListener i
){
327 jButton2
.addActionListener(i
);
328 this.addWindowListener(new WindowAdapter(){
331 public void windowActivated(WindowEvent arg0
) {
332 // TODO Auto-generated method stub
333 super.windowActivated(arg0
);
334 if ((jRadioButton1
.isSelected() && jTextField
.getText().matches(GenGuidDialog
.guidArrayPat
))
335 || (jRadioButton
.isSelected() && jTextField
.getText().matches(GenGuidDialog
.guidRegistryPat
))) {
336 jTextField
.setText(GenGuidDialog
.formatGuidString(jTextField
.getText()));
339 // if (!jTextField.getText().matches(Tools.guidArrayPat) || !jTextField.getText().matches(Tools.guidRegistryPat)) {
340 // JOptionPane.showMessageDialog(frame, "InitVal: Incorrect GUID Value Format.");
349 * This method initializes this
353 private void initialize() {
354 this.setSize(466, 157);
355 this.setDefaultCloseOperation(javax
.swing
.WindowConstants
.DISPOSE_ON_CLOSE
);
357 this.setTitle("Editing GUID Value");
358 this.setContentPane(getJContentPane());
363 * This method initializes jContentPane
365 * @return javax.swing.JPanel
367 private JPanel
getJContentPane() {
368 if (jContentPane
== null) {
369 jContentPane
= new JPanel();
370 jContentPane
.setLayout(new BorderLayout());
371 jContentPane
.add(getJPanel(), java
.awt
.BorderLayout
.EAST
);
372 jContentPane
.add(getJPanel1(), java
.awt
.BorderLayout
.WEST
);
373 jContentPane
.add(getJPanel2(), java
.awt
.BorderLayout
.NORTH
);
374 jContentPane
.add(getJPanel3(), java
.awt
.BorderLayout
.SOUTH
);
375 jContentPane
.add(getJPanel4(), java
.awt
.BorderLayout
.CENTER
);
381 Start the window at the center of screen
384 protected void centerWindow(int intWidth
, int intHeight
) {
385 Dimension d
= Toolkit
.getDefaultToolkit().getScreenSize();
386 this.setLocation((d
.width
- intWidth
) / 2, (d
.height
- intHeight
) / 2);
390 Start the window at the center of screen
393 protected void centerWindow() {
394 centerWindow(this.getSize().width
, this.getSize().height
);
397 public static String
formatGuidString (String guidNameConv
) {
402 .matches(GenGuidDialog
.guidRegistryPat
)) {
403 strList
= guidNameConv
.split("-");
404 guid
= "0x" + strList
[0] + ", ";
405 guid
= guid
+ "0x" + strList
[1] + ", ";
406 guid
= guid
+ "0x" + strList
[2] + ", ";
408 guid
= guid
+ "0x" + strList
[3].substring(0, 2) + ", ";
409 guid
= guid
+ "0x" + strList
[3].substring(2, 4);
411 while (index
< strList
[4].length()) {
413 guid
= guid
+ "0x" + strList
[4].substring(index
, index
+ 2);
419 else if (guidNameConv
420 .matches(GenGuidDialog
.guidArrayPat
)) {
421 strList
= guidNameConv
.split(",");
424 // chang ANSI c form to registry form
426 for (int i
= 0; i
< strList
.length
; i
++){
427 strList
[i
] = strList
[i
].substring(strList
[i
].lastIndexOf("x") + 1);
429 if (strList
[strList
.length
- 1].endsWith("}")) {
430 strList
[strList
.length
-1] = strList
[strList
.length
-1].substring(0, strList
[strList
.length
-1].length()-1);
433 //inserting necessary leading zeros
436 int segLen
= strList
[0].length();
438 for (int i
= 0; i
< 8 - segLen
; ++i
){
439 strList
[0] = "0" + strList
[0];
443 segLen
= strList
[1].length();
445 for (int i
= 0; i
< 4 - segLen
; ++i
){
446 strList
[1] = "0" + strList
[1];
449 segLen
= strList
[2].length();
451 for (int i
= 0; i
< 4 - segLen
; ++i
){
452 strList
[2] = "0" + strList
[2];
455 for (int i
= 3; i
< 11; ++i
) {
456 segLen
= strList
[i
].length();
458 strList
[i
] = "0" + strList
[i
];
462 for (int i
= 0; i
< 3; i
++){
463 guid
+= strList
[i
] + "-";
467 guid
+= strList
[4] + "-";
469 for (int i
= 5; i
< strList
.length
; ++i
){
482 } // @jve:decl-index=0:visual-constraint="10,10"