2 Java class GenGuidDialog.
\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
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
13 package org.tianocore.frameworkwizard.packaging.ui;
\r
15 import java.awt.BorderLayout;
\r
16 import java.awt.Dimension;
\r
17 import java.awt.Toolkit;
\r
19 import javax.swing.JPanel;
\r
20 import javax.swing.JDialog;
\r
21 import java.awt.GridLayout;
\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
31 import org.tianocore.frameworkwizard.common.Tools;
\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
40 Dialog for GUID generation.
\r
41 @since PackageEditor 1.0
\r
43 public class GenGuidDialog extends JDialog implements ActionListener{
\r
48 private static final long serialVersionUID = 1L;
\r
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
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
54 static private JFrame frame;
\r
55 private JPanel jContentPane = null;
\r
56 private JPanel jPanel = null;
\r
57 private JPanel jPanel1 = null;
\r
58 private JPanel jPanel2 = null;
\r
59 private JPanel jPanel3 = null;
\r
60 private JPanel jPanel4 = null;
\r
61 private JTextField jTextField = null;
\r
62 private JLabel jLabel = null;
\r
63 private JRadioButton jRadioButton = null;
\r
64 private JRadioButton jRadioButton1 = null;
\r
65 private JButton jButton = null;
\r
66 private JButton jButton1 = null;
\r
67 private JButton jButton2 = null;
\r
69 // private String guid = null;
\r
71 public void actionPerformed(ActionEvent arg0) {
\r
72 // TODO Auto-generated method stub
\r
73 if (arg0.getSource() == jButton1){
\r
74 String uuid = Tools.generateUuidString();
\r
75 if (jRadioButton1.isSelected()) {
\r
76 jTextField.setText(uuid);
\r
79 //ToDo: transform to comma-sep guid
\r
80 String s = GenGuidDialog.formatGuidString(uuid);
\r
81 if (s.equals("0")) {
\r
82 JOptionPane.showMessageDialog(frame, "Check GUID Value, it don't conform to the schema.");
\r
85 jTextField.setText(s);
\r
89 if (arg0.getSource() == jRadioButton1){
\r
91 //ToDo: check text field value against RegExp and transform if needed
\r
92 if (jTextField.getText().matches(GenGuidDialog.guidRegistryPat)){
\r
95 if (jTextField.getText().matches(GenGuidDialog.guidArrayPat)) {
\r
96 jTextField.setText(GenGuidDialog.formatGuidString(jTextField.getText()));
\r
99 if (jTextField.getText().length()>0)
\r
100 JOptionPane.showMessageDialog(frame, "Check GUID Value, it don't conform to the schema.");
\r
104 if (arg0.getSource() == jRadioButton){
\r
106 //ToDo: check text field value against RegExp and transform if needed
\r
107 if (jTextField.getText().matches(GenGuidDialog.guidArrayPat)){
\r
110 if (jTextField.getText().matches(GenGuidDialog.guidRegistryPat)) {
\r
111 jTextField.setText(GenGuidDialog.formatGuidString(jTextField.getText()));
\r
114 if (jTextField.getText().length()>0)
\r
115 JOptionPane.showMessageDialog(frame, "Check GUID Value, it don't conform to the schema.");
\r
119 if (arg0.getSource() == jButton2){
\r
120 // if (jTextField.getText().matches(Tools.guidArrayPat)
\r
121 // || jTextField.getText().matches(Tools.guidRegistryPat)){
\r
122 // this.setVisible(false);
\r
125 // JOptionPane.showMessageDialog(frame, "Incorrect GUID Value Format.");
\r
130 if (arg0.getSource() == jButton){
\r
136 * This method initializes jPanel
\r
138 * @return javax.swing.JPanel
\r
140 private JPanel getJPanel() {
\r
141 if (jPanel == null) {
\r
142 FlowLayout flowLayout = new FlowLayout();
\r
143 flowLayout.setVgap(10);
\r
144 jPanel = new JPanel();
\r
145 jPanel.setLayout(flowLayout);
\r
146 jPanel.setPreferredSize(new java.awt.Dimension(100,30));
\r
147 jPanel.add(getJButton1(), null);
\r
148 jPanel.add(getJButton2(), null);
\r
149 jPanel.add(getJButton(), null);
\r
155 * This method initializes jPanel1
\r
157 * @return javax.swing.JPanel
\r
159 private JPanel getJPanel1() {
\r
160 if (jPanel1 == null) {
\r
161 jPanel1 = new JPanel();
\r
167 * This method initializes jPanel2
\r
169 * @return javax.swing.JPanel
\r
171 private JPanel getJPanel2() {
\r
172 if (jPanel2 == null) {
\r
173 jPanel2 = new JPanel();
\r
179 * This method initializes jPanel3
\r
181 * @return javax.swing.JPanel
\r
183 private JPanel getJPanel3() {
\r
184 if (jPanel3 == null) {
\r
185 jPanel3 = new JPanel();
\r
191 * This method initializes jPanel4
\r
193 * @return javax.swing.JPanel
\r
195 private JPanel getJPanel4() {
\r
196 if (jPanel4 == null) {
\r
197 jLabel = new JLabel();
\r
198 jLabel.setText("GUID Value");
\r
199 GridLayout gridLayout = new GridLayout();
\r
200 gridLayout.setRows(4);
\r
201 jPanel4 = new JPanel();
\r
202 jPanel4.setLayout(gridLayout);
\r
203 jPanel4.add(getJRadioButton1(), null);
\r
204 jPanel4.add(getJRadioButton(), null);
\r
205 jPanel4.add(jLabel, null);
\r
206 jPanel4.add(getJTextField(), null);
\r
207 ButtonGroup bg = new ButtonGroup();
\r
208 bg.add(jRadioButton1);
\r
209 bg.add(jRadioButton);
\r
215 * This method initializes jTextField
\r
217 * @return javax.swing.JTextField
\r
219 private JTextField getJTextField() {
\r
220 if (jTextField == null) {
\r
221 jTextField = new JTextField();
\r
222 jTextField.setHorizontalAlignment(JTextField.LEADING);
\r
223 jTextField.setPreferredSize(new java.awt.Dimension(100,20));
\r
229 * This method initializes jRadioButton
\r
231 * @return javax.swing.JRadioButton
\r
233 private JRadioButton getJRadioButton() {
\r
234 if (jRadioButton == null) {
\r
235 jRadioButton = new JRadioButton();
\r
236 jRadioButton.setText("Comma-Seperated Format");
\r
237 jRadioButton.setEnabled(false);
\r
238 jRadioButton.addActionListener(this);
\r
240 return jRadioButton;
\r
244 * This method initializes jRadioButton1
\r
246 * @return javax.swing.JRadioButton
\r
248 private JRadioButton getJRadioButton1() {
\r
249 if (jRadioButton1 == null) {
\r
250 jRadioButton1 = new JRadioButton();
\r
251 jRadioButton1.setText("Registry Format");
\r
252 jRadioButton1.setSelected(true);
\r
253 jRadioButton1.addActionListener(this);
\r
255 return jRadioButton1;
\r
259 * This method initializes jButton
\r
261 * @return javax.swing.JButton
\r
263 private JButton getJButton() {
\r
264 if (jButton == null) {
\r
265 jButton = new JButton();
\r
266 jButton.setPreferredSize(new java.awt.Dimension(80,20));
\r
267 jButton.setText("Cancel");
\r
268 jButton.addActionListener(this);
\r
274 * This method initializes jButton1
\r
276 * @return javax.swing.JButton
\r
278 private JButton getJButton1() {
\r
279 if (jButton1 == null) {
\r
280 jButton1 = new JButton();
\r
281 jButton1.setPreferredSize(new java.awt.Dimension(80,20));
\r
282 jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
\r
283 jButton1.setText("New");
\r
284 jButton1.addActionListener(this);
\r
290 * This method initializes jButton2
\r
292 * @return javax.swing.JButton
\r
294 private JButton getJButton2() {
\r
295 if (jButton2 == null) {
\r
296 jButton2 = new JButton();
\r
297 jButton2.setPreferredSize(new java.awt.Dimension(80,20));
\r
298 jButton2.setText("Ok");
\r
299 jButton2.setActionCommand("GenGuidValue");
\r
300 jButton2.addActionListener(this);
\r
309 public static void main(String[] args) {
\r
310 // TODO Auto-generated method stub
\r
311 new GenGuidDialog().setVisible(true);
\r
314 public String getGuid(){
\r
315 return jTextField.getText();
\r
318 public void setGuid(String s){
\r
319 jTextField.setText(s);
\r
322 * This is the default constructor
\r
324 public GenGuidDialog() {
\r
329 public GenGuidDialog(ActionListener i){
\r
332 jButton2.addActionListener(i);
\r
333 this.addWindowListener(new WindowAdapter(){
\r
336 public void windowActivated(WindowEvent arg0) {
\r
337 // TODO Auto-generated method stub
\r
338 super.windowActivated(arg0);
\r
339 if ((jRadioButton1.isSelected() && jTextField.getText().matches(GenGuidDialog.guidArrayPat))
\r
340 || (jRadioButton.isSelected() && jTextField.getText().matches(GenGuidDialog.guidRegistryPat))) {
\r
341 jTextField.setText(GenGuidDialog.formatGuidString(jTextField.getText()));
\r
344 // if (!jTextField.getText().matches(Tools.guidArrayPat) || !jTextField.getText().matches(Tools.guidRegistryPat)) {
\r
345 // JOptionPane.showMessageDialog(frame, "InitVal: Incorrect GUID Value Format.");
\r
354 * This method initializes this
\r
358 private void initialize() {
\r
359 this.setSize(466, 157);
\r
360 this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
\r
361 this.setModal(true);
\r
362 this.setTitle("Editing GUID Value");
\r
363 this.setContentPane(getJContentPane());
\r
364 this.centerWindow();
\r
368 * This method initializes jContentPane
\r
370 * @return javax.swing.JPanel
\r
372 private JPanel getJContentPane() {
\r
373 if (jContentPane == null) {
\r
374 jContentPane = new JPanel();
\r
375 jContentPane.setLayout(new BorderLayout());
\r
376 jContentPane.add(getJPanel(), java.awt.BorderLayout.EAST);
\r
377 jContentPane.add(getJPanel1(), java.awt.BorderLayout.WEST);
\r
378 jContentPane.add(getJPanel2(), java.awt.BorderLayout.NORTH);
\r
379 jContentPane.add(getJPanel3(), java.awt.BorderLayout.SOUTH);
\r
380 jContentPane.add(getJPanel4(), java.awt.BorderLayout.CENTER);
\r
382 return jContentPane;
\r
386 Start the window at the center of screen
\r
389 protected void centerWindow(int intWidth, int intHeight) {
\r
390 Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
\r
391 this.setLocation((d.width - intWidth) / 2, (d.height - intHeight) / 2);
\r
395 Start the window at the center of screen
\r
398 protected void centerWindow() {
\r
399 centerWindow(this.getSize().width, this.getSize().height);
\r
402 public static String formatGuidString (String guidNameConv) {
\r
407 .matches(GenGuidDialog.guidRegistryPat)) {
\r
408 strList = guidNameConv.split("-");
\r
409 guid = "0x" + strList[0] + ", ";
\r
410 guid = guid + "0x" + strList[1] + ", ";
\r
411 guid = guid + "0x" + strList[2] + ", ";
\r
413 guid = guid + "0x" + strList[3].substring(0, 2) + ", ";
\r
414 guid = guid + "0x" + strList[3].substring(2, 4);
\r
416 while (index < strList[4].length()) {
\r
417 guid = guid + ", ";
\r
418 guid = guid + "0x" + strList[4].substring(index, index + 2);
\r
424 else if (guidNameConv
\r
425 .matches(GenGuidDialog.guidArrayPat)) {
\r
426 strList = guidNameConv.split(",");
\r
429 // chang ANSI c form to registry form
\r
431 for (int i = 0; i < strList.length; i++){
\r
432 strList[i] = strList[i].substring(strList[i].lastIndexOf("x") + 1);
\r
434 if (strList[strList.length - 1].endsWith("}")) {
\r
435 strList[strList.length -1] = strList[strList.length-1].substring(0, strList[strList.length-1].length()-1);
\r
438 //inserting necessary leading zeros
\r
441 int segLen = strList[0].length();
\r
443 for (int i = 0; i < 8 - segLen; ++i){
\r
444 strList[0] = "0" + strList[0];
\r
448 segLen = strList[1].length();
\r
450 for (int i = 0; i < 4 - segLen; ++i){
\r
451 strList[1] = "0" + strList[1];
\r
454 segLen = strList[2].length();
\r
456 for (int i = 0; i < 4 - segLen; ++i){
\r
457 strList[2] = "0" + strList[2];
\r
460 for (int i = 3; i < 11; ++i) {
\r
461 segLen = strList[i].length();
\r
463 strList[i] = "0" + strList[i];
\r
467 for (int i = 0; i < 3; i++){
\r
468 guid += strList[i] + "-";
\r
471 guid += strList[3];
\r
472 guid += strList[4] + "-";
\r
474 for (int i = 5; i < strList.length; ++i){
\r
475 guid += strList[i];
\r
487 } // @jve:decl-index=0:visual-constraint="10,10"
\r