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
{
48 private static final long serialVersionUID
= 1L;
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}( )*(\\})?";
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}";
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;
69 // private String guid = null;
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
);
79 //ToDo: transform to comma-sep guid
80 String s
= GenGuidDialog
.formatGuidString(uuid
);
82 JOptionPane
.showMessageDialog(frame
, "Check GUID Value, it don't conform to the schema.");
85 jTextField
.setText(s
);
89 if (arg0
.getSource() == jRadioButton1
){
91 //ToDo: check text field value against RegExp and transform if needed
92 if (jTextField
.getText().matches(GenGuidDialog
.guidRegistryPat
)){
95 if (jTextField
.getText().matches(GenGuidDialog
.guidArrayPat
)) {
96 jTextField
.setText(GenGuidDialog
.formatGuidString(jTextField
.getText()));
99 if (jTextField
.getText().length()>0)
100 JOptionPane
.showMessageDialog(frame
, "Check GUID Value, it don't conform to the schema.");
104 if (arg0
.getSource() == jRadioButton
){
106 //ToDo: check text field value against RegExp and transform if needed
107 if (jTextField
.getText().matches(GenGuidDialog
.guidArrayPat
)){
110 if (jTextField
.getText().matches(GenGuidDialog
.guidRegistryPat
)) {
111 jTextField
.setText(GenGuidDialog
.formatGuidString(jTextField
.getText()));
114 if (jTextField
.getText().length()>0)
115 JOptionPane
.showMessageDialog(frame
, "Check GUID Value, it don't conform to the schema.");
119 if (arg0
.getSource() == jButton2
){
120 // if (jTextField.getText().matches(Tools.guidArrayPat)
121 // || jTextField.getText().matches(Tools.guidRegistryPat)){
122 // this.setVisible(false);
125 // JOptionPane.showMessageDialog(frame, "Incorrect GUID Value Format.");
130 if (arg0
.getSource() == jButton
){
136 * This method initializes jPanel
138 * @return javax.swing.JPanel
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);
155 * This method initializes jPanel1
157 * @return javax.swing.JPanel
159 private JPanel
getJPanel1() {
160 if (jPanel1
== null) {
161 jPanel1
= new JPanel();
167 * This method initializes jPanel2
169 * @return javax.swing.JPanel
171 private JPanel
getJPanel2() {
172 if (jPanel2
== null) {
173 jPanel2
= new JPanel();
179 * This method initializes jPanel3
181 * @return javax.swing.JPanel
183 private JPanel
getJPanel3() {
184 if (jPanel3
== null) {
185 jPanel3
= new JPanel();
191 * This method initializes jPanel4
193 * @return javax.swing.JPanel
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
);
215 * This method initializes jTextField
217 * @return javax.swing.JTextField
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));
229 * This method initializes jRadioButton
231 * @return javax.swing.JRadioButton
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);
244 * This method initializes jRadioButton1
246 * @return javax.swing.JRadioButton
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);
255 return jRadioButton1
;
259 * This method initializes jButton
261 * @return javax.swing.JButton
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);
274 * This method initializes jButton1
276 * @return javax.swing.JButton
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);
290 * This method initializes jButton2
292 * @return javax.swing.JButton
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);
309 public static void main(String
[] args
) {
310 // TODO Auto-generated method stub
311 new GenGuidDialog().setVisible(true);
314 public String
getGuid(){
315 return jTextField
.getText();
318 public void setGuid(String s
){
319 jTextField
.setText(s
);
322 * This is the default constructor
324 public GenGuidDialog() {
329 public GenGuidDialog(ActionListener i
){
332 jButton2
.addActionListener(i
);
333 this.addWindowListener(new WindowAdapter(){
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()));
344 // if (!jTextField.getText().matches(Tools.guidArrayPat) || !jTextField.getText().matches(Tools.guidRegistryPat)) {
345 // JOptionPane.showMessageDialog(frame, "InitVal: Incorrect GUID Value Format.");
354 * This method initializes this
358 private void initialize() {
359 this.setSize(466, 157);
360 this.setDefaultCloseOperation(javax
.swing
.WindowConstants
.DISPOSE_ON_CLOSE
);
362 this.setTitle("Editing GUID Value");
363 this.setContentPane(getJContentPane());
368 * This method initializes jContentPane
370 * @return javax.swing.JPanel
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
);
386 Start the window at the center of screen
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);
395 Start the window at the center of screen
398 protected void centerWindow() {
399 centerWindow(this.getSize().width
, this.getSize().height
);
402 public static String
formatGuidString (String 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] + ", ";
413 guid
= guid
+ "0x" + strList
[3].substring(0, 2) + ", ";
414 guid
= guid
+ "0x" + strList
[3].substring(2, 4);
416 while (index
< strList
[4].length()) {
418 guid
= guid
+ "0x" + strList
[4].substring(index
, index
+ 2);
424 else if (guidNameConv
425 .matches(GenGuidDialog
.guidArrayPat
)) {
426 strList
= guidNameConv
.split(",");
429 // chang ANSI c form to registry form
431 for (int i
= 0; i
< strList
.length
; i
++){
432 strList
[i
] = strList
[i
].substring(strList
[i
].lastIndexOf("x") + 1);
434 if (strList
[strList
.length
- 1].endsWith("}")) {
435 strList
[strList
.length
-1] = strList
[strList
.length
-1].substring(0, strList
[strList
.length
-1].length()-1);
438 //inserting necessary leading zeros
441 int segLen
= strList
[0].length();
443 for (int i
= 0; i
< 8 - segLen
; ++i
){
444 strList
[0] = "0" + strList
[0];
448 segLen
= strList
[1].length();
450 for (int i
= 0; i
< 4 - segLen
; ++i
){
451 strList
[1] = "0" + strList
[1];
454 segLen
= strList
[2].length();
456 for (int i
= 0; i
< 4 - segLen
; ++i
){
457 strList
[2] = "0" + strList
[2];
460 for (int i
= 3; i
< 11; ++i
) {
461 segLen
= strList
[i
].length();
463 strList
[i
] = "0" + strList
[i
];
467 for (int i
= 0; i
< 3; i
++){
468 guid
+= strList
[i
] + "-";
472 guid
+= strList
[4] + "-";
474 for (int i
= 5; i
< strList
.length
; ++i
){
487 } // @jve:decl-index=0:visual-constraint="10,10"