]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/packaging/ui/GenGuidDialog.java
Coding style changes
[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 jPanelEast = null;
57 private JPanel jPanelCenter = null;
58 private JTextField jTextField = null;
59 private JLabel jLabel = null;
60 private JRadioButton jRadioButton = null;
61 private JRadioButton jRadioButtonReg = null;
62 private JButton jButtonCancel = null;
63 private JButton jButtonNew = null;
64 private JButton jButtonOk = null;
65
66 // private String guid = null;
67
68 public void actionPerformed(ActionEvent arg0) {
69 // TODO Auto-generated method stub
70 if (arg0.getSource() == jButtonNew){
71 String uuid = Tools.generateUuidString();
72 if (jRadioButtonReg.isSelected()) {
73 jTextField.setText(uuid);
74 }
75 else {
76 //ToDo: transform to comma-sep guid
77 String s = GenGuidDialog.formatGuidString(uuid);
78 if (s.equals("0")) {
79 JOptionPane.showMessageDialog(frame, "Check GUID Value, it don't conform to the schema.");
80 return;
81 }
82 jTextField.setText(s);
83 }
84 }
85
86 if (arg0.getSource() == jRadioButtonReg){
87
88 //ToDo: check text field value against RegExp and transform if needed
89 if (jTextField.getText().matches(GenGuidDialog.guidRegistryPat)){
90 return;
91 }
92 if (jTextField.getText().matches(GenGuidDialog.guidArrayPat)) {
93 jTextField.setText(GenGuidDialog.formatGuidString(jTextField.getText()));
94 return;
95 }
96 if (jTextField.getText().length()>0)
97 JOptionPane.showMessageDialog(frame, "Check GUID Value, it don't conform to the schema.");
98
99 }
100
101 if (arg0.getSource() == jRadioButton){
102
103 //ToDo: check text field value against RegExp and transform if needed
104 if (jTextField.getText().matches(GenGuidDialog.guidArrayPat)){
105 return;
106 }
107 if (jTextField.getText().matches(GenGuidDialog.guidRegistryPat)) {
108 jTextField.setText(GenGuidDialog.formatGuidString(jTextField.getText()));
109 return;
110 }
111 if (jTextField.getText().length()>0)
112 JOptionPane.showMessageDialog(frame, "Check GUID Value, it don't conform to the schema.");
113
114 }
115
116 if (arg0.getSource() == jButtonOk){
117 // if (jTextField.getText().matches(Tools.guidArrayPat)
118 // || jTextField.getText().matches(Tools.guidRegistryPat)){
119 // this.setVisible(false);
120 // }
121 // else {
122 // JOptionPane.showMessageDialog(frame, "Incorrect GUID Value Format.");
123 // }
124 this.dispose();
125 }
126
127 if (arg0.getSource() == jButtonCancel){
128 this.dispose();
129 }
130 }
131
132 /**
133 * This method initializes jPanel
134 *
135 * @return javax.swing.JPanel
136 */
137 private JPanel getJPanelEast() {
138 if (jPanelEast == null) {
139 FlowLayout flowLayout = new FlowLayout();
140 flowLayout.setVgap(10);
141 jPanelEast = new JPanel();
142 jPanelEast.setLayout(flowLayout);
143 jPanelEast.setPreferredSize(new java.awt.Dimension(100,30));
144 jPanelEast.add(getJButtonNew(), null);
145 jPanelEast.add(getJButtonOk(), null);
146 jPanelEast.add(getJButtonCancel(), null);
147 }
148 return jPanelEast;
149 }
150
151 /**
152 * This method initializes jPanel4
153 *
154 * @return javax.swing.JPanel
155 */
156 private JPanel getJPanelCenter() {
157 if (jPanelCenter == null) {
158 jLabel = new JLabel();
159 jLabel.setText("GUID Value");
160 GridLayout gridLayout = new GridLayout();
161 gridLayout.setRows(4);
162 jPanelCenter = new JPanel();
163 jPanelCenter.setLayout(gridLayout);
164 jPanelCenter.add(getJRadioButtonReg(), null);
165 jPanelCenter.add(getJRadioButton(), null);
166 jPanelCenter.add(jLabel, null);
167 jPanelCenter.add(getJTextField(), null);
168 ButtonGroup bg = new ButtonGroup();
169 bg.add(jRadioButtonReg);
170 bg.add(jRadioButton);
171 }
172 return jPanelCenter;
173 }
174
175 /**
176 * This method initializes jTextField
177 *
178 * @return javax.swing.JTextField
179 */
180 private JTextField getJTextField() {
181 if (jTextField == null) {
182 jTextField = new JTextField();
183 jTextField.setHorizontalAlignment(JTextField.LEADING);
184 jTextField.setPreferredSize(new java.awt.Dimension(100,20));
185 }
186 return jTextField;
187 }
188
189 /**
190 * This method initializes jRadioButton
191 *
192 * @return javax.swing.JRadioButton
193 */
194 private JRadioButton getJRadioButton() {
195 if (jRadioButton == null) {
196 jRadioButton = new JRadioButton();
197 jRadioButton.setText("Comma-Seperated Format");
198 jRadioButton.setEnabled(false);
199 jRadioButton.addActionListener(this);
200 }
201 return jRadioButton;
202 }
203
204 /**
205 * This method initializes jRadioButton1
206 *
207 * @return javax.swing.JRadioButton
208 */
209 private JRadioButton getJRadioButtonReg() {
210 if (jRadioButtonReg == null) {
211 jRadioButtonReg = new JRadioButton();
212 jRadioButtonReg.setText("Registry Format");
213 jRadioButtonReg.setSelected(true);
214 jRadioButtonReg.addActionListener(this);
215 }
216 return jRadioButtonReg;
217 }
218
219 /**
220 * This method initializes jButton
221 *
222 * @return javax.swing.JButton
223 */
224 private JButton getJButtonCancel() {
225 if (jButtonCancel == null) {
226 jButtonCancel = new JButton();
227 jButtonCancel.setPreferredSize(new java.awt.Dimension(80,20));
228 jButtonCancel.setText("Cancel");
229 jButtonCancel.addActionListener(this);
230 }
231 return jButtonCancel;
232 }
233
234 /**
235 * This method initializes jButton1
236 *
237 * @return javax.swing.JButton
238 */
239 private JButton getJButtonNew() {
240 if (jButtonNew == null) {
241 jButtonNew = new JButton();
242 jButtonNew.setPreferredSize(new java.awt.Dimension(80,20));
243 jButtonNew.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
244 jButtonNew.setText("New");
245 jButtonNew.addActionListener(this);
246 }
247 return jButtonNew;
248 }
249
250 /**
251 * This method initializes jButton2
252 *
253 * @return javax.swing.JButton
254 */
255 private JButton getJButtonOk() {
256 if (jButtonOk == null) {
257 jButtonOk = new JButton();
258 jButtonOk.setPreferredSize(new java.awt.Dimension(80,20));
259 jButtonOk.setText("Ok");
260 jButtonOk.setActionCommand("GenGuidValue");
261 jButtonOk.addActionListener(this);
262 }
263 return jButtonOk;
264 }
265
266 /**
267
268 @param args
269 **/
270 public static void main(String[] args) {
271 // TODO Auto-generated method stub
272 new GenGuidDialog().setVisible(true);
273 }
274
275 public String getGuid(){
276 return jTextField.getText();
277 }
278
279 public void setGuid(String s){
280 jTextField.setText(s);
281 }
282 /**
283 * This is the default constructor
284 */
285 public GenGuidDialog() {
286 super();
287 initialize();
288 }
289
290 public GenGuidDialog(ActionListener i){
291 super();
292 initialize();
293 jButtonOk.addActionListener(i);
294 this.addWindowListener(new WindowAdapter(){
295
296 @Override
297 public void windowActivated(WindowEvent arg0) {
298 // TODO Auto-generated method stub
299 super.windowActivated(arg0);
300 if ((jRadioButtonReg.isSelected() && jTextField.getText().matches(GenGuidDialog.guidArrayPat))
301 || (jRadioButton.isSelected() && jTextField.getText().matches(GenGuidDialog.guidRegistryPat))) {
302 jTextField.setText(GenGuidDialog.formatGuidString(jTextField.getText()));
303 }
304
305 // if (!jTextField.getText().matches(Tools.guidArrayPat) || !jTextField.getText().matches(Tools.guidRegistryPat)) {
306 // JOptionPane.showMessageDialog(frame, "InitVal: Incorrect GUID Value Format.");
307 // return;
308 // }
309 }
310
311 });
312 }
313
314 /**
315 * This method initializes this
316 *
317 * @return void
318 */
319 private void initialize() {
320 this.setSize(466, 157);
321 this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
322 this.setModal(true);
323 this.setTitle("Editing GUID Value");
324 this.setContentPane(getJContentPane());
325 this.centerWindow();
326 }
327
328 /**
329 * This method initializes jContentPane
330 *
331 * @return javax.swing.JPanel
332 */
333 private JPanel getJContentPane() {
334 if (jContentPane == null) {
335 jContentPane = new JPanel();
336 jContentPane.setLayout(new BorderLayout());
337 jContentPane.add(getJPanelEast(), java.awt.BorderLayout.EAST);
338 jContentPane.add(getJPanelCenter(), java.awt.BorderLayout.CENTER);
339 }
340 return jContentPane;
341 }
342
343 /**
344 Start the window at the center of screen
345
346 **/
347 protected void centerWindow(int intWidth, int intHeight) {
348 Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
349 this.setLocation((d.width - intWidth) / 2, (d.height - intHeight) / 2);
350 }
351
352 /**
353 Start the window at the center of screen
354
355 **/
356 protected void centerWindow() {
357 centerWindow(this.getSize().width, this.getSize().height);
358 }
359
360 public static String formatGuidString (String guidNameConv) {
361 String[] strList;
362 String guid = "";
363 int index = 0;
364 if (guidNameConv
365 .matches(GenGuidDialog.guidRegistryPat)) {
366 strList = guidNameConv.split("-");
367 guid = "0x" + strList[0] + ", ";
368 guid = guid + "0x" + strList[1] + ", ";
369 guid = guid + "0x" + strList[2] + ", ";
370
371 guid = guid + "0x" + strList[3].substring(0, 2) + ", ";
372 guid = guid + "0x" + strList[3].substring(2, 4);
373
374 while (index < strList[4].length()) {
375 guid = guid + ", ";
376 guid = guid + "0x" + strList[4].substring(index, index + 2);
377 index = index + 2;
378 }
379
380 return guid;
381 }
382 else if (guidNameConv
383 .matches(GenGuidDialog.guidArrayPat)) {
384 strList = guidNameConv.split(",");
385
386 //
387 // chang ANSI c form to registry form
388 //
389 for (int i = 0; i < strList.length; i++){
390 strList[i] = strList[i].substring(strList[i].lastIndexOf("x") + 1);
391 }
392 if (strList[strList.length - 1].endsWith("}")) {
393 strList[strList.length -1] = strList[strList.length-1].substring(0, strList[strList.length-1].length()-1);
394 }
395 //
396 //inserting necessary leading zeros
397 //
398
399 int segLen = strList[0].length();
400 if (segLen < 8){
401 for (int i = 0; i < 8 - segLen; ++i){
402 strList[0] = "0" + strList[0];
403 }
404 }
405
406 segLen = strList[1].length();
407 if (segLen < 4){
408 for (int i = 0; i < 4 - segLen; ++i){
409 strList[1] = "0" + strList[1];
410 }
411 }
412 segLen = strList[2].length();
413 if (segLen < 4){
414 for (int i = 0; i < 4 - segLen; ++i){
415 strList[2] = "0" + strList[2];
416 }
417 }
418 for (int i = 3; i < 11; ++i) {
419 segLen = strList[i].length();
420 if (segLen < 2){
421 strList[i] = "0" + strList[i];
422 }
423 }
424
425 for (int i = 0; i < 3; i++){
426 guid += strList[i] + "-";
427 }
428
429 guid += strList[3];
430 guid += strList[4] + "-";
431
432 for (int i = 5; i < strList.length; ++i){
433 guid += strList[i];
434 }
435
436
437 return guid;
438 } else {
439
440 return "0";
441
442 }
443 }
444
445 } // @jve:decl-index=0:visual-constraint="10,10"