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