summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/packaging/ui/GenGuidDialog.java
blob: b219c9ed70d97fd6516dafcc2872479dd320f011 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/** @file
  Java class GenGuidDialog.
 
Copyright (c) 2006, Intel Corporation
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution.  The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
package org.tianocore.frameworkwizard.packaging.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JPanel;
import javax.swing.JDialog;
import java.awt.GridLayout;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.KeyStroke;

import org.tianocore.frameworkwizard.common.Tools;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 Dialog for GUID generation. 
 @since PackageEditor 1.0
**/
public class GenGuidDialog extends JDialog implements ActionListener{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    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}( )*(\\})?";
    
    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}";
    
    static private JFrame frame;
    private JPanel jContentPane = null;
    private JPanel jPanelEast = null;
    private JPanel jPanelCenter = null;
    private JTextField jTextField = null;
    private JLabel jLabel = null;
    private JRadioButton jRadioButton = null;
    private JRadioButton jRadioButtonReg = null;
    private JButton jButtonCancel = null;
    private JButton jButtonNew = null;
    private JButton jButtonOk = null;
    private ActionListener outerListener = null;
    
//    private String guid = null;

    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        if (arg0.getSource() == jButtonNew){
            String uuid = Tools.generateUuidString();
            if (jRadioButtonReg.isSelected()) {
                jTextField.setText(uuid);
            }
            else {
                //ToDo: transform to comma-sep guid
                String s = GenGuidDialog.formatGuidString(uuid);
                if (s.equals("0")) {
                    JOptionPane.showMessageDialog(frame, "Check GUID Value, it don't conform to the schema.");
                    return;
                }
                jTextField.setText(s);
            }
        }
        
        if (arg0.getSource() == jRadioButtonReg){
            
            //ToDo: check text field value against RegExp and transform if needed
            if (jTextField.getText().matches(GenGuidDialog.guidRegistryPat)){
                return;
            }
            if (jTextField.getText().matches(GenGuidDialog.guidArrayPat)) {
                jTextField.setText(GenGuidDialog.formatGuidString(jTextField.getText()));
                return;
            }
            if (jTextField.getText().length()>0)
            JOptionPane.showMessageDialog(frame, "Check GUID Value, it don't conform to the schema.");
                    
        }
        
        if (arg0.getSource() == jRadioButton){
            
            //ToDo: check text field value against RegExp and transform if needed
            if (jTextField.getText().matches(GenGuidDialog.guidArrayPat)){
                return;
            }
            if (jTextField.getText().matches(GenGuidDialog.guidRegistryPat)) {
                jTextField.setText(GenGuidDialog.formatGuidString(jTextField.getText()));
                return;
            }
            if (jTextField.getText().length()>0)
            JOptionPane.showMessageDialog(frame, "Check GUID Value, it don't conform to the schema.");
            
        }
        
        if (arg0.getSource() == jButtonOk){
//            if (jTextField.getText().matches(Tools.guidArrayPat) 
//                            || jTextField.getText().matches(Tools.guidRegistryPat)){
//                this.setVisible(false);
//            }
//            else {
//                JOptionPane.showMessageDialog(frame, "Incorrect GUID Value Format.");
//            }
//            this.dispose();
        }
        
        if (arg0.getSource() == jButtonCancel){
            this.dispose();
        }
    }

    /**
     * This method initializes jPanel	
     * 	
     * @return javax.swing.JPanel	
     */
    private JPanel getJPanelEast() {
        if (jPanelEast == null) {
            FlowLayout flowLayout = new FlowLayout();
            flowLayout.setVgap(10);
            jPanelEast = new JPanel();
            jPanelEast.setLayout(flowLayout);
            jPanelEast.setPreferredSize(new java.awt.Dimension(100,30));
            jPanelEast.add(getJButtonNew(), null);
            jPanelEast.add(getJButtonOk(), null);
            jPanelEast.add(getJButtonCancel(), null);
        }
        return jPanelEast;
    }

    /**
     * This method initializes jPanel4	
     * 	
     * @return javax.swing.JPanel	
     */
    private JPanel getJPanelCenter() {
        if (jPanelCenter == null) {
            jLabel = new JLabel();
            jLabel.setText("GUID Value");
            GridLayout gridLayout = new GridLayout();
            gridLayout.setRows(4);
            jPanelCenter = new JPanel();
            jPanelCenter.setLayout(gridLayout);
            jPanelCenter.add(getJRadioButtonReg(), null);
            jPanelCenter.add(getJRadioButton(), null);
            jPanelCenter.add(jLabel, null);
            jPanelCenter.add(getJTextField(), null);
            ButtonGroup bg = new ButtonGroup();
            bg.add(jRadioButtonReg);
            bg.add(jRadioButton);
        }
        return jPanelCenter;
    }

    /**
     * This method initializes jTextField	
     * 	
     * @return javax.swing.JTextField	
     */
    private JTextField getJTextField() {
        if (jTextField == null) {
            jTextField = new JTextField();
            jTextField.setHorizontalAlignment(JTextField.LEADING);
            jTextField.setPreferredSize(new java.awt.Dimension(100,20));
        }
        return jTextField;
    }

    /**
     * This method initializes jRadioButton	
     * 	
     * @return javax.swing.JRadioButton	
     */
    private JRadioButton getJRadioButton() {
        if (jRadioButton == null) {
            jRadioButton = new JRadioButton();
            jRadioButton.setText("Comma-Seperated Format");
            jRadioButton.setEnabled(false);
            jRadioButton.addActionListener(this);
        }
        return jRadioButton;
    }

    /**
     * This method initializes jRadioButton1	
     * 	
     * @return javax.swing.JRadioButton	
     */
    private JRadioButton getJRadioButtonReg() {
        if (jRadioButtonReg == null) {
            jRadioButtonReg = new JRadioButton();
            jRadioButtonReg.setText("Registry Format");
            jRadioButtonReg.setSelected(true);
            jRadioButtonReg.addActionListener(this);
        }
        return jRadioButtonReg;
    }

    /**
     * This method initializes jButton	
     * 	
     * @return javax.swing.JButton	
     */
    private JButton getJButtonCancel() {
        if (jButtonCancel == null) {
            jButtonCancel = new JButton();
            jButtonCancel.setPreferredSize(new java.awt.Dimension(80,20));
            jButtonCancel.setText("Cancel");
            jButtonCancel.addActionListener(this);
            jButtonCancel.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), JComponent.WHEN_FOCUSED);
        }
        return jButtonCancel;
    }

    /**
     * This method initializes jButton1	
     * 	
     * @return javax.swing.JButton	
     */
    private JButton getJButtonNew() {
        if (jButtonNew == null) {
            jButtonNew = new JButton();
            jButtonNew.setPreferredSize(new java.awt.Dimension(80,20));
            jButtonNew.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
            jButtonNew.setText("New");
            jButtonNew.addActionListener(this);
            jButtonNew.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), JComponent.WHEN_FOCUSED);
        }
        return jButtonNew;
    }

    /**
     * This method initializes jButton2	
     * 	
     * @return javax.swing.JButton	
     */
    private JButton getJButtonOk() {
        if (jButtonOk == null) {
            jButtonOk = new JButton();
            jButtonOk.setPreferredSize(new java.awt.Dimension(80,20));
            jButtonOk.setText("Ok");
            jButtonOk.setActionCommand("GenGuidValue");
//            jButtonOk.addActionListener(this);
            jButtonOk.registerKeyboardAction(outerListener, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), JComponent.WHEN_FOCUSED);
            
        }
        return jButtonOk;
    }

    public String getGuid(){
        return jTextField.getText();
    }
    
    public void setGuid(String s){
        jTextField.setText(s);
    }
    /**
     * This is the default constructor
     */
    public GenGuidDialog() {
        super();
        initialize();
    }
    
    public GenGuidDialog(ActionListener i, JFrame frame){
        super(frame);
        outerListener = i;
        initialize();
        jButtonOk.addActionListener(i);
        this.addWindowListener(new WindowAdapter(){

            @Override
            public void windowActivated(WindowEvent arg0) {
                // TODO Auto-generated method stub
                super.windowActivated(arg0);
                if ((jRadioButtonReg.isSelected() && jTextField.getText().matches(GenGuidDialog.guidArrayPat))
                                || (jRadioButton.isSelected() && jTextField.getText().matches(GenGuidDialog.guidRegistryPat))) {
                    jTextField.setText(GenGuidDialog.formatGuidString(jTextField.getText()));
                }
                
//                if (!jTextField.getText().matches(Tools.guidArrayPat) || !jTextField.getText().matches(Tools.guidRegistryPat)) {
//                  JOptionPane.showMessageDialog(frame, "InitVal: Incorrect GUID Value Format.");
//                  return;
//                }
            }
            
        });
    }

    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize() {
        this.setSize(466, 157);
        this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        this.setModal(true);
        this.setTitle("Editing GUID Value");
        this.setContentPane(getJContentPane());
        this.centerWindow();
    }

    /**
     * This method initializes jContentPane
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane() {
        if (jContentPane == null) {
            jContentPane = new JPanel();
            jContentPane.setLayout(new BorderLayout());
            jContentPane.add(getJPanelEast(), java.awt.BorderLayout.EAST);
            jContentPane.add(getJPanelCenter(), java.awt.BorderLayout.CENTER);
        }
        return jContentPane;
    }

    /**
     Start the window at the center of screen
     
     **/
    protected void centerWindow(int intWidth, int intHeight) {
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation((d.width - intWidth) / 2, (d.height - intHeight) / 2);
    }

    /**
     Start the window at the center of screen
     
     **/
    protected void centerWindow() {
        centerWindow(this.getSize().width, this.getSize().height);
    }
    
    public static String formatGuidString (String guidNameConv) {
        String[] strList;
        String guid = "";
        int index = 0;
        if (guidNameConv
                        .matches(GenGuidDialog.guidRegistryPat)) {
            strList = guidNameConv.split("-");
            guid = "0x" + strList[0] + ", ";
            guid = guid + "0x" + strList[1] + ", ";
            guid = guid + "0x" + strList[2] + ", ";

            guid = guid + "0x" + strList[3].substring(0, 2) + ", ";
            guid = guid + "0x" + strList[3].substring(2, 4);

            while (index < strList[4].length()) {
                guid = guid + ", ";
                guid = guid + "0x" + strList[4].substring(index, index + 2);
                index = index + 2;
            }

            return guid;
        }
        else if (guidNameConv
                        .matches(GenGuidDialog.guidArrayPat)) {
            strList = guidNameConv.split(",");
            
            //
            // chang ANSI c form to registry form
            //
            for (int i = 0; i < strList.length; i++){
                strList[i] = strList[i].substring(strList[i].lastIndexOf("x") + 1);
            }
            if (strList[strList.length - 1].endsWith("}")) {
                strList[strList.length -1] = strList[strList.length-1].substring(0, strList[strList.length-1].length()-1); 
            }
            //
            //inserting necessary leading zeros
            //
            
            int segLen = strList[0].length();
            if (segLen < 8){
                for (int i = 0; i < 8 - segLen; ++i){
                    strList[0] = "0" + strList[0];
                }
            }
            
            segLen = strList[1].length();
            if (segLen < 4){
                for (int i = 0; i < 4 - segLen; ++i){
                    strList[1] = "0" + strList[1];
                }
            }
            segLen = strList[2].length();
            if (segLen < 4){
                for (int i = 0; i < 4 - segLen; ++i){
                    strList[2] = "0" + strList[2];
                }
            }
            for (int i = 3; i < 11; ++i) {
                segLen = strList[i].length();
                if (segLen < 2){
                    strList[i] = "0" + strList[i];
                }
            }
            
            for (int i = 0; i < 3; i++){
                guid += strList[i] + "-";
            }
            
            guid += strList[3];
            guid += strList[4] + "-";
            
            for (int i = 5; i < strList.length; ++i){
                guid += strList[i];
            }
            
            
            return guid;
        } else {
            
            return "0";

        }
    }
    
}  //  @jve:decl-index=0:visual-constraint="10,10"