]> 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@671 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 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}( )*(\\})?";
46
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}";
48
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;
63
64 // private String guid = null;
65
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);
72 }
73 else {
74 //ToDo: transform to comma-sep guid
75 String s = GenGuidDialog.formatGuidString(uuid);
76 if (s.equals("0")) {
77 JOptionPane.showMessageDialog(frame, "Check GUID Value, it don't conform to the schema.");
78 return;
79 }
80 jTextField.setText(s);
81 }
82 }
83
84 if (arg0.getSource() == jRadioButton1){
85
86 //ToDo: check text field value against RegExp and transform if needed
87 if (jTextField.getText().matches(GenGuidDialog.guidRegistryPat)){
88 return;
89 }
90 if (jTextField.getText().matches(GenGuidDialog.guidArrayPat)) {
91 jTextField.setText(GenGuidDialog.formatGuidString(jTextField.getText()));
92 return;
93 }
94 if (jTextField.getText().length()>0)
95 JOptionPane.showMessageDialog(frame, "Check GUID Value, it don't conform to the schema.");
96
97 }
98
99 if (arg0.getSource() == jRadioButton){
100
101 //ToDo: check text field value against RegExp and transform if needed
102 if (jTextField.getText().matches(GenGuidDialog.guidArrayPat)){
103 return;
104 }
105 if (jTextField.getText().matches(GenGuidDialog.guidRegistryPat)) {
106 jTextField.setText(GenGuidDialog.formatGuidString(jTextField.getText()));
107 return;
108 }
109 if (jTextField.getText().length()>0)
110 JOptionPane.showMessageDialog(frame, "Check GUID Value, it don't conform to the schema.");
111
112 }
113
114 if (arg0.getSource() == jButton2){
115 // if (jTextField.getText().matches(Tools.guidArrayPat)
116 // || jTextField.getText().matches(Tools.guidRegistryPat)){
117 // this.setVisible(false);
118 // }
119 // else {
120 // JOptionPane.showMessageDialog(frame, "Incorrect GUID Value Format.");
121 // }
122 this.dispose();
123 }
124
125 if (arg0.getSource() == jButton){
126 this.dispose();
127 }
128 }
129
130 /**
131 * This method initializes jPanel
132 *
133 * @return javax.swing.JPanel
134 */
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);
145 }
146 return jPanel;
147 }
148
149 /**
150 * This method initializes jPanel1
151 *
152 * @return javax.swing.JPanel
153 */
154 private JPanel getJPanel1() {
155 if (jPanel1 == null) {
156 jPanel1 = new JPanel();
157 }
158 return jPanel1;
159 }
160
161 /**
162 * This method initializes jPanel2
163 *
164 * @return javax.swing.JPanel
165 */
166 private JPanel getJPanel2() {
167 if (jPanel2 == null) {
168 jPanel2 = new JPanel();
169 }
170 return jPanel2;
171 }
172
173 /**
174 * This method initializes jPanel3
175 *
176 * @return javax.swing.JPanel
177 */
178 private JPanel getJPanel3() {
179 if (jPanel3 == null) {
180 jPanel3 = new JPanel();
181 }
182 return jPanel3;
183 }
184
185 /**
186 * This method initializes jPanel4
187 *
188 * @return javax.swing.JPanel
189 */
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);
205 }
206 return jPanel4;
207 }
208
209 /**
210 * This method initializes jTextField
211 *
212 * @return javax.swing.JTextField
213 */
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));
219 }
220 return jTextField;
221 }
222
223 /**
224 * This method initializes jRadioButton
225 *
226 * @return javax.swing.JRadioButton
227 */
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);
234 }
235 return jRadioButton;
236 }
237
238 /**
239 * This method initializes jRadioButton1
240 *
241 * @return javax.swing.JRadioButton
242 */
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);
249 }
250 return jRadioButton1;
251 }
252
253 /**
254 * This method initializes jButton
255 *
256 * @return javax.swing.JButton
257 */
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);
264 }
265 return jButton;
266 }
267
268 /**
269 * This method initializes jButton1
270 *
271 * @return javax.swing.JButton
272 */
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);
280 }
281 return jButton1;
282 }
283
284 /**
285 * This method initializes jButton2
286 *
287 * @return javax.swing.JButton
288 */
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);
296 }
297 return jButton2;
298 }
299
300 /**
301
302 @param args
303 **/
304 public static void main(String[] args) {
305 // TODO Auto-generated method stub
306 new GenGuidDialog().setVisible(true);
307 }
308
309 public String getGuid(){
310 return jTextField.getText();
311 }
312
313 public void setGuid(String s){
314 jTextField.setText(s);
315 }
316 /**
317 * This is the default constructor
318 */
319 public GenGuidDialog() {
320 super();
321 initialize();
322 }
323
324 public GenGuidDialog(ActionListener i){
325 super();
326 initialize();
327 jButton2.addActionListener(i);
328 this.addWindowListener(new WindowAdapter(){
329
330 @Override
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()));
337 }
338
339 // if (!jTextField.getText().matches(Tools.guidArrayPat) || !jTextField.getText().matches(Tools.guidRegistryPat)) {
340 // JOptionPane.showMessageDialog(frame, "InitVal: Incorrect GUID Value Format.");
341 // return;
342 // }
343 }
344
345 });
346 }
347
348 /**
349 * This method initializes this
350 *
351 * @return void
352 */
353 private void initialize() {
354 this.setSize(466, 157);
355 this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
356 this.setModal(true);
357 this.setTitle("Editing GUID Value");
358 this.setContentPane(getJContentPane());
359 this.centerWindow();
360 }
361
362 /**
363 * This method initializes jContentPane
364 *
365 * @return javax.swing.JPanel
366 */
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);
376 }
377 return jContentPane;
378 }
379
380 /**
381 Start the window at the center of screen
382
383 **/
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);
387 }
388
389 /**
390 Start the window at the center of screen
391
392 **/
393 protected void centerWindow() {
394 centerWindow(this.getSize().width, this.getSize().height);
395 }
396
397 public static String formatGuidString (String guidNameConv) {
398 String[] strList;
399 String guid = "";
400 int index = 0;
401 if (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] + ", ";
407
408 guid = guid + "0x" + strList[3].substring(0, 2) + ", ";
409 guid = guid + "0x" + strList[3].substring(2, 4);
410
411 while (index < strList[4].length()) {
412 guid = guid + ", ";
413 guid = guid + "0x" + strList[4].substring(index, index + 2);
414 index = index + 2;
415 }
416
417 return guid;
418 }
419 else if (guidNameConv
420 .matches(GenGuidDialog.guidArrayPat)) {
421 strList = guidNameConv.split(",");
422
423 //
424 // chang ANSI c form to registry form
425 //
426 for (int i = 0; i < strList.length; i++){
427 strList[i] = strList[i].substring(strList[i].lastIndexOf("x") + 1);
428 }
429 if (strList[strList.length - 1].endsWith("}")) {
430 strList[strList.length -1] = strList[strList.length-1].substring(0, strList[strList.length-1].length()-1);
431 }
432 //
433 //inserting necessary leading zeros
434 //
435
436 int segLen = strList[0].length();
437 if (segLen < 8){
438 for (int i = 0; i < 8 - segLen; ++i){
439 strList[0] = "0" + strList[0];
440 }
441 }
442
443 segLen = strList[1].length();
444 if (segLen < 4){
445 for (int i = 0; i < 4 - segLen; ++i){
446 strList[1] = "0" + strList[1];
447 }
448 }
449 segLen = strList[2].length();
450 if (segLen < 4){
451 for (int i = 0; i < 4 - segLen; ++i){
452 strList[2] = "0" + strList[2];
453 }
454 }
455 for (int i = 3; i < 11; ++i) {
456 segLen = strList[i].length();
457 if (segLen < 2){
458 strList[i] = "0" + strList[i];
459 }
460 }
461
462 for (int i = 0; i < 3; i++){
463 guid += strList[i] + "-";
464 }
465
466 guid += strList[3];
467 guid += strList[4] + "-";
468
469 for (int i = 5; i < strList.length; ++i){
470 guid += strList[i];
471 }
472
473
474 return guid;
475 } else {
476
477 return "0";
478
479 }
480 }
481
482 } // @jve:decl-index=0:visual-constraint="10,10"