summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/toolchain/Preferences.java
blob: 0c23b6a854bfd2a80ef331376614886141eb3e79 (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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
/** @file
 <<The file is used to update the Build Preferences file, target.txt>>
 
 <<The program will use target.txt, the tools config file specified in that file,
 or it will use the default tools_def.txt file, and it will also scan the 
 FrameworkDatabase.db file for certain parameters. >>
 
 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 Name: Tools
 Module Name:  FrameworkWizard
 
 **/

package org.tianocore.frameworkwizard.toolchain;

import java.awt.event.ActionEvent;
import java.io.*;
import java.util.Vector;
import java.util.Iterator;
import java.util.Scanner;

import javax.swing.*;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import org.tianocore.frameworkwizard.common.GlobalData;
import org.tianocore.frameworkwizard.common.Tools;
import org.tianocore.frameworkwizard.common.Log;
import org.tianocore.frameworkwizard.common.ui.ArchCheckBox;
import org.tianocore.frameworkwizard.common.ui.iCheckBoxList.*;
import org.tianocore.frameworkwizard.common.ui.IFrame;
import org.tianocore.frameworkwizard.workspace.Workspace;
import org.tianocore.frameworkwizard.workspace.WorkspaceTools;
import org.tianocore.frameworkwizard.platform.PlatformIdentification;
import org.tianocore.PlatformSurfaceAreaDocument;

/**
 * The class is used to update the target.txt file.
 * 
 * It extends IDialog
 * 
 */
public class Preferences extends IFrame {

    // /
    // / Define class Serial Version UID
    // /
    private static final long serialVersionUID = -4777906991966638888L;

    private final boolean Debug = false;

    //
    // Define class members
    //
    private final int oneRowHeight = 20;

    private final int twoRowHeight = 40;

    private final int threeRowHeight = 60;

    private final int sepHeight = 6;

    private final int rowOne = 12;

    private final int rowTwo = rowOne + oneRowHeight + sepHeight;

    private final int rowThree = rowTwo + oneRowHeight + sepHeight;

    private final int rowFour = rowThree + threeRowHeight + sepHeight;

    private final int rowFive = rowFour + threeRowHeight + sepHeight;

    private final int rowSix = rowFive + oneRowHeight + sepHeight;

    private final int buttonRow = rowSix + oneRowHeight + sepHeight + sepHeight;

    private final int dialogHeight = buttonRow + twoRowHeight + twoRowHeight;

    private final int dialogWidth = 540;

    private final int lastButtonXLoc = 430;

    private final int next2LastButtonLoc = 329;

    /*
     * Define the contents for this dialog box
     */
    private static Preferences bTarget = null;

    private WorkspaceTools wt = new WorkspaceTools();

    private final int activePlatformId = 0;

    private final int buildTargetId = 1;

    private final int targetArchId = 2;

    private final int toolDefFileId = 3;

    private final int tagNameId = 4;

    private final int threadEnableId = 5;

    private final int threadCountId = 6;

    private final int maxTargetLines = threadCountId + 1;

    private JPanel jContentPane = null;

    private JLabel jLabelToolsConfigFile = null;

    private JTextField jTextFieldToolsConfigFile = null;

    private final int toolConfigFileRow = rowOne;

    private JLabel jLabelActivePlatform = null;

    private JComboBox jComboBoxActivePlatform = null;

    private final int activePlatformRow = rowTwo;

    private JLabel jLabelToolChainTagName = null;

    private JScrollPane jScrollPaneTagName = null;

    private ICheckBoxList iCheckBoxListTagName = null;

    private final int toolChainTagNameRow = rowThree;

    private JLabel jLabelBuildTarget = null;

    private JScrollPane jScrollPaneBuildTarget = null;

    private ICheckBoxList iCheckBoxListBuildTarget = null;

    private final int buildTargetRow = rowFour;

    private JLabel jLabelTargetArch = null;

    private ArchCheckBox jArchCheckBox = null;

    private final int targetArchRow = rowFive;

    private JLabel jLabelEnableThreads = null;

    private JLabel jLabelThreadCount = null;

    private final int threadRow = rowSix;

    private JCheckBox jCheckBoxEnableThreads = null;

    private JTextField jTextFieldThreadCount = null;
    
    private String threadCount = "";
    
    private boolean threadEnabled = false;

    private JButton jButtonBrowse = null;

    private JButton jButtonSave = null;

    private JButton jButtonCancel = null;

    private final int labelColumn = 12;

    private final int labelWidth = 155;

    private final int valueColumn = 168;

    private final int valueWidth = 352;

    private final int valueWidthShort = 260;

    private final int buttonWidth = 90;

    private String workspaceDir = Workspace.getCurrentWorkspace() + System.getProperty("file.separator");

    private String toolsDir = Workspace.getCurrentWorkspace() + System.getProperty("file.separator") + "Tools"
                              + System.getProperty("file.separator") + "Conf";

    private String defaultToolsConf = toolsDir + System.getProperty("file.separator") + "tools_def.txt";

    private String targetFile = toolsDir + System.getProperty("file.separator") + "target.txt";

    private String[] targetFileContents = new String[500];

    // private String[] toolsConfContents;

    private String[] targetLines = new String[maxTargetLines];

    private int targetLineNumber[] = new int[maxTargetLines];

    private String toolsConfFile;

    private String toolsDefTargetNames = null;

    private final int toolsDefTargetNameField = 0;

    private String toolsDefTagNames = null;

    private final int toolsDefTagNameField = 1;

    private String toolsDefArchNames = null;

    private final int toolsDefArchNameField = 2;

    private String toolsDefIdentifier = null;

    private int targetLineNumberMax;
    
    private final int toolDefFieldCount = 5;

    private Vector<String> vArchList = null;

    private Vector<String> vDisableArchList = null;
    
    //
    // Not used by UI
    //
    //    private Preferences id = null;

    //    private EnumerationData ed = new EnumerationData();

    /**
     This method initializes jTextFieldToolsConfigFile  
     
     @return javax.swing.JTextField  jTextFieldToolsConfigFile
     **/
    private JTextField getJTextFieldToolsConfigFile() {
        if (jTextFieldToolsConfigFile == null) {
            if (targetLines[toolDefFileId] != null) {
                String sLine[] = targetLines[toolDefFileId].trim().split("=");
                jTextFieldToolsConfigFile = new JTextField(sLine[1].trim());
            } else
                jTextFieldToolsConfigFile = new JTextField();

            jTextFieldToolsConfigFile.setBounds(new java.awt.Rectangle(valueColumn, toolConfigFileRow, valueWidthShort,
                                                                       oneRowHeight));
            jTextFieldToolsConfigFile.setPreferredSize(new java.awt.Dimension(valueWidthShort, oneRowHeight));
            jTextFieldToolsConfigFile
                                     .setToolTipText("<html>"
                                                     + "Specify the name of the filename to use for specifying"
                                                     + "<br>the tools to use for the build.  If not specified,"
                                                     + "<br>tools_def.txt will be used for the build.  This file"
                                                     + "<br>MUST be located in the WORKSPACE/Tools/Conf directory.</html>");

        }
        return jTextFieldToolsConfigFile;
    }

    /**
     * This method initializes jComboBoxActivePlatform
     * 
     * @return javax.swing.JComboBox jComboBoxActivePlatform
     * 
     */
    private JComboBox getActivePlatform() {
        Vector<PlatformIdentification> vPlatformId = wt.getAllPlatforms();

        if (jComboBoxActivePlatform == null) {
            jComboBoxActivePlatform = new JComboBox();
            jComboBoxActivePlatform.setBounds(new java.awt.Rectangle(valueColumn, activePlatformRow, valueWidth,
                                                                     oneRowHeight));
            jComboBoxActivePlatform.setPreferredSize(new java.awt.Dimension(valueWidth, oneRowHeight));
            jComboBoxActivePlatform
                                   .setToolTipText("<html>Select &quot;Do Not Set&quot; if you want to build a platform"
                                                   + "<br>from the directory where the FPD file exists,"
                                                   + "<br>otherwise scroll down to select the platform.</html>");

            /*
             * Generate the data, selecting what is in target.txt
             */
            jComboBoxActivePlatform.addItem("Do Not Set");
            Iterator<PlatformIdentification> iter = vPlatformId.iterator();
            while (iter.hasNext()) {
                PlatformIdentification item = iter.next();
                String path = item.getPath().trim();
                String str = path.substring(workspaceDir.length(), path.length());
                str.replace(System.getProperty("file.separator"), "/");
                jComboBoxActivePlatform.addItem(str.trim());
            }
            if (targetLines[activePlatformId] == null)
                jComboBoxActivePlatform.setSelectedItem("Do Not Set");
            else
                jComboBoxActivePlatform.setSelectedItem(targetLines[activePlatformId]);
        }
        return jComboBoxActivePlatform;
    }

    /**
     * This method initializes jScrollPaneTagName
     * 
     * @return javax.swing.JScrollPane jScrollPaneTagName
     * 
     */
    private JScrollPane getJScrollPaneTagName() {

        if (jScrollPaneTagName == null) {
            jScrollPaneTagName = new JScrollPane();
            jScrollPaneTagName.setBounds(new java.awt.Rectangle(valueColumn, toolChainTagNameRow, valueWidth,
                                                                threeRowHeight));
            jScrollPaneTagName.setPreferredSize(new java.awt.Dimension(valueWidth, threeRowHeight));
            jScrollPaneTagName.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            jScrollPaneTagName.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
            jScrollPaneTagName.setViewportView(getICheckBoxListTagName());
            jScrollPaneTagName.setToolTipText("<html>"
                                              + "Specify the TagName(s) from the tool configuration file to use"
                                              + "<br>for your builds.  If not specified, all applicable TagName"
                                              + " <br>tools will be used for the build.</html>");
            jScrollPaneTagName.setVisible(true);

        }
        return jScrollPaneTagName;
    }

    private ICheckBoxList getICheckBoxListTagName() {
        if (iCheckBoxListTagName == null) {
            iCheckBoxListTagName = new ICheckBoxList();

            if (toolsDefTagNames != null) {
                toolsDefTagNames.trim();
                String aTagNames[] = toolsDefTagNames.trim().split(" ");
                Vector<String> vTags = new Vector<String>();
                for (int i = 0; i < aTagNames.length; i++) {
                    vTags.add(aTagNames[i]);
                }
                iCheckBoxListTagName.setAllItems(vTags);
            } else {
                Vector<String> defaultTags = stringToVector("MYTOOLS");
                iCheckBoxListTagName.setAllItems(defaultTags);
            }

            iCheckBoxListTagName.setAllItemsUnchecked();
            iCheckBoxListTagName.setToolTipText("<html>"
                                                + "Specify the TagName(s) from the tool configuration file to use"
                                                + "<br>for your builds.  If not specified, all applicable TagName"
                                                + " <br>tools will be used for the build.</html>");
            Vector<String> vSelectedTags = new Vector<String>();
            if (targetLines[tagNameId] != null) {
                targetLines[tagNameId].trim();
                String targetTags[] = targetLines[tagNameId].trim().split(" ");
                for (int j = 0; j < targetTags.length; j++)
                    vSelectedTags.add(targetTags[j]);
                iCheckBoxListTagName.initCheckedItem(true, vSelectedTags);
            }
        }
        return iCheckBoxListTagName;
    }

    /**
     * This method initializes jScrollPaneBuildTarget
     * 
     * @return javax.swing.JComboBox jScrollPaneBuildTarget
     * 
     */
    private JScrollPane getJScrollPaneBuildTarget() {
        if (jScrollPaneBuildTarget == null) {
            jScrollPaneBuildTarget = new JScrollPane();
            jScrollPaneBuildTarget.setBounds(new java.awt.Rectangle(valueColumn, buildTargetRow, valueWidth,
                                                                    threeRowHeight));
            jScrollPaneBuildTarget.setPreferredSize(new java.awt.Dimension(valueWidth, threeRowHeight));
            jScrollPaneBuildTarget.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            jScrollPaneBuildTarget.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
            jScrollPaneBuildTarget.setViewportView(getICheckBoxListBuildTarget());
            jScrollPaneBuildTarget.setVisible(true);
            jScrollPaneBuildTarget.setToolTipText("<html>"
                                                  + "Select the TARGET Names that you want to build, such as<BR>"
                                                  + "BUILD or BUILD and RELEASE"
                                                  + "<br>If you do not set any of these, all available targets"
                                                  + "<br>will be built.</html>");

        }
        return jScrollPaneBuildTarget;
    }

    private JCheckBox getCheckBoxEnableThreads() {
        if (jCheckBoxEnableThreads == null) {
            jCheckBoxEnableThreads = new JCheckBox();
            jCheckBoxEnableThreads.setBounds(valueColumn, threadRow, 20, oneRowHeight);
            jCheckBoxEnableThreads.setToolTipText("Select this if you want to enable parallel compilation.");
            jCheckBoxEnableThreads.setSelected(threadEnabled);
            jCheckBoxEnableThreads.addActionListener(this);
            
        }
        return jCheckBoxEnableThreads;
    }

    private JTextField getTextFieldThreadCount() {
        if (jTextFieldThreadCount == null) {
            jTextFieldThreadCount = new JTextField();
            jTextFieldThreadCount.setBounds(valueColumn + 215, threadRow, 30, oneRowHeight);
            if (threadCount.length() > 0)
                jTextFieldThreadCount.setText(threadCount);
            jTextFieldThreadCount.setToolTipText("<html>Recommended setting is N+1,<br> where N is the number of physical processors or cores in the system</html>");
            // If CheckBoxEnableThreads is selected, then enable editing

        }
        return jTextFieldThreadCount;
    }

    private ICheckBoxList getICheckBoxListBuildTarget() {
        if (iCheckBoxListBuildTarget == null) {

            String aBuildTargets[] = toolsDefTargetNames.trim().split(" ");
            Vector<String> vBuildTargets = new Vector<String>();
            for (int i = 0; i < aBuildTargets.length; i++) {
                vBuildTargets.add(aBuildTargets[i]);
            }
            iCheckBoxListBuildTarget = new ICheckBoxList();
            iCheckBoxListBuildTarget.setAllItems(vBuildTargets);
            iCheckBoxListBuildTarget.setAllItemsUnchecked();
            iCheckBoxListBuildTarget.setToolTipText("<html>"
                                                    + "Select the TARGET Names that you want to build, such as<BR>"
                                                    + "BUILD or BUILD and RELEASE"
                                                    + "<br>If you do not set any of these, all available targets"
                                                    + "<br>will be built.</html>");

            Vector<String> vSelectedTags = new Vector<String>();
            if (targetLines[buildTargetId] != null) {
                targetLines[buildTargetId].trim();
                String targetTags[] = targetLines[buildTargetId].trim().split(" ");
                for (int j = 0; j < targetTags.length; j++)
                    vSelectedTags.add(targetTags[j]);
                iCheckBoxListBuildTarget.initCheckedItem(true, vSelectedTags);
            }
        }
        return iCheckBoxListBuildTarget;
    }

    /**
     This method initializes jButtonBrowse   
     
     @return javax.swing.JButton 
     **/
    private JButton getJButtonBrowse() {
        if (jButtonBrowse == null) {
            jButtonBrowse = new JButton();
            jButtonBrowse
                         .setBounds(new java.awt.Rectangle(lastButtonXLoc, toolConfigFileRow, buttonWidth, oneRowHeight));
            jButtonBrowse.setText("Browse");
            jButtonBrowse.setPreferredSize(new java.awt.Dimension(buttonWidth, oneRowHeight));
            jButtonBrowse.addActionListener(new AbstractAction() {
                /**
                 * 
                 */
                private static final long serialVersionUID = 1L;

                public void actionPerformed(ActionEvent e) {
                    //
                    // Select files from current workspace
                    //
                    String dirPrefix = toolsDir + System.getProperty("file.separator");
                    JFileChooser chooser = new JFileChooser(dirPrefix);
                    File theFile = null;
                    //                    String headerDest = null;

                    chooser.setMultiSelectionEnabled(false);
                    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                    int retval = chooser.showOpenDialog(Preferences.this);
                    if (retval == JFileChooser.APPROVE_OPTION) {

                        theFile = chooser.getSelectedFile();
                        String file = theFile.getPath();
                        if (!file.startsWith(dirPrefix)) {
                            JOptionPane.showMessageDialog(Preferences.this, "You can only select files in the Tools"
                                                                 + System.getProperty("file.separator")
                                                                 + "Conf directory!");

                            return;
                        }

                        jTextFieldToolsConfigFile.setText("Tools/Conf/" + theFile.getName());
                    } else {
                        return;
                    }
                }
            });
        }
        return jButtonBrowse;
    }

    /**
     * This method initializes jButtonOk
     * 
     * @return javax.swing.JButton
     * 
     */
    private JButton getJButtonSave() {
        if (jButtonSave == null) {
            jButtonSave = new JButton();
            jButtonSave.setBounds(new java.awt.Rectangle(next2LastButtonLoc, buttonRow, buttonWidth, oneRowHeight));
            jButtonSave.setText("Save");
            jButtonSave.addActionListener(this);
        }
        return jButtonSave;
    }

    /**
     * This method initializes jButtonCancel
     * 
     * @return javax.swing.JButton
     * 
     */
    private JButton getJButtonCancel() {
        if (jButtonCancel == null) {
            jButtonCancel = new JButton();
            jButtonCancel.setBounds(new java.awt.Rectangle(lastButtonXLoc, buttonRow, buttonWidth, oneRowHeight));
            jButtonCancel.setText("Cancel");
            jButtonCancel.addActionListener(this);
        }
        return jButtonCancel;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

    public static Preferences getInstance() {
        if (bTarget == null) {
            bTarget = new Preferences();
        }
        return bTarget;
    }

    /**
     * This is the default constructor
     */
    public Preferences() {
        super();
        init();
    }

    /**
     * This method initializes this
     * 
     */
    private void init() {
        for (int i = 0; i < maxTargetLines; i++) {
            targetLines[i] = null;
            targetLineNumber[i] = -1;
        }
        initReadFiles();
        this.setSize(dialogWidth, dialogHeight);
        this.setContentPane(getJContentPane());
        this.setTitle("Build Preferences [" + toolsDefIdentifier + "]");
        this.setDefaultCloseOperation(IFrame.DISPOSE_ON_CLOSE);
        this.centerWindow();
        this.setVisible(true);
    }

    /**
     * This method initializes this Fill values to all fields if these values are
     * not empty
     * 
     * @param initReadFiles
     * 
     */
    private void initReadFiles() {
        /*
         * TODO
         * Read Current target.txt file first
         * Read TOOL_CHAIN_CONF file if specified, otherwise use tools_def.txt
         */
        readTargetTxtFile();
        boolean haveBuildTargets = readToolDefinitionFile();
        if (!haveBuildTargets) {
            // Lookup Build Targets from the platforms
            readPlatformFileBuildTargets();
        }
    }

    private void readPlatformFileBuildTargets() {
        Vector<PlatformIdentification> vPlatformId = wt.getAllPlatforms();
        String sBuildTargets = "";

        // foreach platform, build a list of BuildTargets
        Iterator<PlatformIdentification> iter = vPlatformId.iterator();
        while (iter.hasNext()) {
            PlatformIdentification item = iter.next();
            PlatformSurfaceAreaDocument.PlatformSurfaceArea fpd = GlobalData.openingPlatformList
                                                                                                .getOpeningPlatformById(
                                                                                                                        item)
                                                                                                .getXmlFpd();
            sBuildTargets += fpd.getPlatformDefinitions().getBuildTargets().toString() + " ";
        }
        String allTargets[] = sBuildTargets.trim().split(" ");
        for (int i = 0; i < allTargets.length; i++) {
            if (!toolsDefTargetNames.contains(allTargets[i])) {
                toolsDefTargetNames += allTargets[i] + " ";
            }
        }
    }

    private boolean readToolDefinitionFile() {

        // Parse the tool definition file looking for targets and architectures
        toolsConfFile = null;
        boolean buildTargetsExist = true;

        if (targetLines[toolDefFileId] != null) {
            String[] result = new String[2];
            targetLines[toolDefFileId].trim();
            result = (targetLines[toolDefFileId]).split("=");
            String resString = (Tools.convertPathToCurrentOsType(result[1])).trim();
            toolsConfFile = workspaceDir.trim() + resString.trim();
            File toolsDefFile = new File(toolsConfFile);
            if (!toolsDefFile.exists()) {
                JOptionPane.showMessageDialog(this, "<html>" + "Tool Definition file, " + toolsConfFile
                                                    + "<br>specified in the target.txt file does not exist!"
                                                    + "<br>Using the default Tool Definition File:<br>"
                                                    + defaultToolsConf);
                toolsConfFile = defaultToolsConf;
            }
        } else {
            toolsConfFile = defaultToolsConf;
        }
        String[] toolsDefFields = new String[toolDefFieldCount];
        for (int i = 0; i < toolDefFieldCount; i++)
            toolsDefFields[i] = null;
        File toolDefFile = new File(toolsConfFile);
        if (toolDefFile.exists()) {
            try {
                FileReader fileReader = new FileReader(toolDefFile);
                BufferedReader reader = new BufferedReader(fileReader);
                String rLine = null;
                String result[];
                int lineCounter = 0;
                while ((rLine = reader.readLine()) != null) {

                    if (rLine.startsWith("IDENTIFIER")) {
                        result = rLine.split("=");
                        toolsDefIdentifier = (result[1]).trim();
                    } else if ((!rLine.startsWith("#")) && (rLine.contains("="))) {
                        result = rLine.split("=");
                        toolsDefFields = ((result[0]).trim()).split("_");
                        if (toolsDefTargetNames == null) {
                            toolsDefTargetNames = (toolsDefFields[toolsDefTargetNameField]).trim() + " ";
                        } else if (!toolsDefTargetNames.contains((toolsDefFields[toolsDefTargetNameField]).trim())) {
                            toolsDefTargetNames += (toolsDefFields[toolsDefTargetNameField]).trim() + " ";
                        }
                        if (toolsDefTagNames == null) {
                            toolsDefTagNames = (toolsDefFields[toolsDefTagNameField]).trim() + " ";
                        } else if (!toolsDefTagNames.contains((toolsDefFields[toolsDefTagNameField]).trim())) {
                            toolsDefTagNames += (toolsDefFields[toolsDefTagNameField]).trim() + " ";
                        }
                        if (toolsDefArchNames == null) {
                            toolsDefArchNames = (toolsDefFields[toolsDefArchNameField]).trim() + " ";
                        } else if (!toolsDefArchNames.contains((toolsDefFields[toolsDefArchNameField]).trim())) {
                            toolsDefArchNames += (toolsDefFields[toolsDefArchNameField]).trim() + " ";
                        }
                    }
                    lineCounter++;
                }
                reader.close();
                // Only enable Architecture selection based on tool chain installations
                String turnOff = "";
                if (!toolsDefArchNames.contains("EBC"))
                    turnOff = "EBC ";
                if (!toolsDefArchNames.contains("PPC"))
                    turnOff += "PPC ";
                if (!toolsDefArchNames.contains("IPF"))
                    turnOff += "IPF ";
                if (!toolsDefArchNames.contains("X64"))
                    turnOff += "X64 ";
                if (!toolsDefArchNames.contains("IA32"))
                    turnOff += "X64 ";
                if (!toolsDefArchNames.contains("ARM"))
                    turnOff += "ARM ";
                turnOff = turnOff.trim();
                vDisableArchList = stringToVector(turnOff);

                if (!toolsDefTargetNames.matches("[A-Z]+")) {
                    toolsDefTargetNames = toolsDefTargetNames.replace("* ", "").trim();
                    if (Debug)
                        System.out.println("tools_def file does not define build targets: '" + toolsDefTargetNames
                                           + "'");
                    buildTargetsExist = false;
                }
            } catch (IOException e) {
                Log.log(toolsConfFile + " Read Error ", e.getMessage());
                e.printStackTrace();
            }
        }
        return buildTargetsExist;
    }

    private void readTargetTxtFile() {
        File tFile = new File(targetFile);

        if (tFile.exists()) {
            try {
                FileReader fileReader = new FileReader(targetFile);
                BufferedReader reader = new BufferedReader(fileReader);
                targetLineNumberMax = 0;
                String rLine = null;
                while ((rLine = reader.readLine()) != null) {
                    targetFileContents[targetLineNumberMax] = rLine;
                    if (rLine.startsWith("ACTIVE_PLATFORM")) {
                        // Only one active platform is permitted!
                        targetLines[activePlatformId] = rLine;
                        targetLineNumber[activePlatformId] = targetLineNumberMax;
                    }
                    if ((rLine.startsWith("TARGET" + " ")) || (rLine.startsWith("TARGET" + "\t"))
                        || (rLine.startsWith("TARGET="))) {
                        // Handle multiple Target Names
                        if (rLine.contains(","))
                            targetLines[buildTargetId] = rLine.trim().replaceAll(",", " ");
                        else
                            targetLines[buildTargetId] = rLine.trim();
                        targetLineNumber[buildTargetId] = targetLineNumberMax;
                    }
                    if (rLine.startsWith("TARGET_ARCH")) {
                        // Handle multiple Target Architectures
                        if (rLine.contains(","))
                            targetLines[targetArchId] = rLine.trim().replaceAll(",", " ");
                        else
                            targetLines[targetArchId] = rLine.trim();
                        targetLineNumber[targetArchId] = targetLineNumberMax;
                    }
                    if (rLine.startsWith("TOOL_CHAIN_CONF")) {
                        // Only one file is permitted
                        targetLines[toolDefFileId] = rLine.trim();
                        targetLineNumber[toolDefFileId] = targetLineNumberMax;
                    }

                    if (rLine.startsWith("TOOL_CHAIN_TAG")) {
                        // Handle multiple Tool TagNames
                        if (rLine.contains(","))
                            targetLines[tagNameId] = rLine.trim().replaceAll(",", " ");
                        else
                            targetLines[tagNameId] = rLine.trim();
                        targetLineNumber[tagNameId] = targetLineNumberMax;
                    }
                    
                    if (rLine.startsWith("MULTIPLE_THREAD")) {
                        // Handle Thread Enable flag
                        targetLines[threadEnableId] = rLine.trim();
                        targetLineNumber[threadEnableId] = targetLineNumberMax;
                        if ((rLine.trim().toLowerCase().contains("enabled")) || (rLine.trim().toLowerCase().contains("true"))) { 
                            threadEnabled = true;
                        } else {
                            threadEnabled = false;
                        }
                    }
                    
                    if (rLine.startsWith("MAX_CONCURRENT_THREAD_NUMBER")) {
                        // Handle Thread Enable flag
                        targetLines[threadCountId] = rLine.trim();
                        targetLineNumber[threadCountId] = targetLineNumberMax;
                    }
                    targetLineNumberMax++;
                }
                reader.close();
                String archLine[] = new String[2];
                if (targetLines[targetArchId] != null) {
                    if (targetLines[targetArchId].contains("=")) {
                        if (targetLines[targetArchId].contains(","))
                            targetLines[targetArchId] = targetLines[targetArchId].trim().replaceAll(",", " ");
                        if (targetLines[targetArchId].length() > 0)
                            archLine = targetLines[targetArchId].trim().split("=");
                        vArchList = stringToVector(archLine[1]);
                    }
                }

                if (targetLines[threadCountId] != null) {
                    String tcLine[] = new String[2];
                    tcLine = targetLines[threadCountId].trim().split("=");
                    threadCount = tcLine[1];
                } else
                    threadCount = "";
                
                if (Debug == true)
                    for (int i = 0; i <= maxTargetLines; i++)
                        System.out.println("targetLines[" + i + "] contains: " + targetLines[i] + " index is: "
                                           + targetLineNumber[i]);
            } catch (IOException e) {
                Log.log(this.targetFile + " Read Error ", e.getMessage());
                e.printStackTrace();
            }
        }

    }

    /**
     * This method initializes jContentPane
     * 
     * @return javax.swing.JPanel jContentPane
     * 
     */
    private JPanel getJContentPane() {
        if (jContentPane == null) {
            jLabelToolsConfigFile = new JLabel();
            jLabelToolsConfigFile.setBounds(new java.awt.Rectangle(labelColumn, toolConfigFileRow, labelWidth,
                                                                   oneRowHeight));
            jLabelToolsConfigFile.setText("Tool Chain Definition File");
            jLabelActivePlatform = new JLabel();
            jLabelActivePlatform.setText("Select Active Platform");
            jLabelActivePlatform.setBounds(new java.awt.Rectangle(labelColumn, activePlatformRow, labelWidth,
                                                                  oneRowHeight));
            jLabelToolChainTagName = new JLabel();
            jLabelToolChainTagName.setBounds(new java.awt.Rectangle(labelColumn, toolChainTagNameRow, labelWidth,
                                                                    oneRowHeight));
            jLabelToolChainTagName.setText("Select Tool Tag Name");
            jLabelBuildTarget = new JLabel();
            jLabelBuildTarget.setBounds(new java.awt.Rectangle(labelColumn, buildTargetRow, labelWidth, oneRowHeight));
            jLabelBuildTarget.setText("Select Build Target");
            jLabelTargetArch = new JLabel();
            jLabelTargetArch.setBounds(new java.awt.Rectangle(labelColumn, targetArchRow, labelWidth, oneRowHeight));
            jLabelTargetArch.setText("Build Architectures");

            jArchCheckBox = new ArchCheckBox();
            jArchCheckBox.setBounds(new java.awt.Rectangle(valueColumn, targetArchRow, valueWidth, oneRowHeight));
            jArchCheckBox.setPreferredSize(new java.awt.Dimension(valueWidth, oneRowHeight));

            jLabelEnableThreads = new JLabel();
            jLabelEnableThreads.setBounds(new java.awt.Rectangle(labelColumn, threadRow, labelWidth, oneRowHeight));
            jLabelEnableThreads.setText("Enable Compiler Threading");

            jLabelThreadCount = new JLabel();
            jLabelThreadCount.setBounds(new java.awt.Rectangle(valueColumn + 60, threadRow, labelWidth, oneRowHeight));
            jLabelThreadCount.setText("Number of threads to start");

            jContentPane = new JPanel();
            jContentPane.setLayout(null);
            jContentPane.setPreferredSize(new java.awt.Dimension(dialogWidth - 10, dialogHeight - 10));

            jContentPane.add(jLabelToolsConfigFile, null);
            jContentPane.add(getJTextFieldToolsConfigFile(), null);
            jContentPane.add(getJButtonBrowse(), null);

            jContentPane.add(jLabelActivePlatform, null);
            jContentPane.add(getActivePlatform(), null);

            jContentPane.add(jLabelToolChainTagName, null);
            jContentPane.add(getJScrollPaneTagName(), null);

            jContentPane.add(jLabelBuildTarget, null);
            jContentPane.add(getJScrollPaneBuildTarget(), null);

            jContentPane.add(jLabelTargetArch, null);

            jArchCheckBox.setDisabledItems(vDisableArchList);
            jArchCheckBox.setSelectedItems(vArchList);
            jContentPane.add(jArchCheckBox, null);

            jContentPane.add(jLabelEnableThreads, null);
            jContentPane.add(getCheckBoxEnableThreads(), null);

            jContentPane.add(jLabelThreadCount, null);
            jContentPane.add(getTextFieldThreadCount(), null);

            jContentPane.add(getJButtonSave(), null);
            jContentPane.add(getJButtonCancel(), null);
        }
        return jContentPane;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     * 
     * Override actionPerformed to listen all actions
     * 
     */
    public void actionPerformed(ActionEvent arg0) {

        if (arg0.getSource() == jButtonBrowse) {
            // TODO: Call file browser, starting in $WORKSPACE/Tools/Conf directory

        }

        if (arg0.getSource() == jButtonSave) {
            saveTargetFile();
            JOptionPane.showMessageDialog(this, "<html>The target.txt file has been saved!"
                                                + "<br>A copy of the original file, target.txt.bak has"
                                                + "<br>also been created.</html>");
            this.exit();
        }

        if (arg0.getSource() == jButtonCancel) {
            this.exit();
        }
        
        if (arg0.getSource() == jCheckBoxEnableThreads) {
            if (jCheckBoxEnableThreads.isSelected() == false) {
                threadCount = "";
                jTextFieldThreadCount.setText(threadCount);
            }
        }
    }
    

    private void updateActivePlatform() {
        int lineAP;
        if (targetLines[activePlatformId] != null) {
            lineAP = targetLineNumber[activePlatformId];
        } else {
            lineAP = targetLineNumberMax;
            targetLineNumber[activePlatformId] = lineAP;
            targetLineNumberMax++;
        }
        if (jComboBoxActivePlatform.getSelectedItem() == "Do Not Set") {
            targetFileContents[lineAP] = "";
            targetLines[activePlatformId] = "";
        } else {
            targetFileContents[lineAP] = "ACTIVE_PLATFORM = " + jComboBoxActivePlatform.getSelectedItem() + "\r\n";
            targetLines[activePlatformId] = targetFileContents[lineAP];
        }
        if (Debug)
            System.out.println("Active Platform: " + targetFileContents[lineAP]);
    }

    private void updateToolDefFile() {
        int lineTDF;
        if (targetLines[toolDefFileId] != null) {
            lineTDF = targetLineNumber[toolDefFileId];
        } else {
            lineTDF = targetLineNumberMax;
            targetLineNumber[toolDefFileId] = lineTDF;
            targetLineNumberMax++;
        }
        if (Debug)
            System.out.println("Tool Config File: " + jTextFieldToolsConfigFile.getText());
        if (jTextFieldToolsConfigFile.getText() == null) {
            targetFileContents[lineTDF] = "#MT#";
            targetLines[toolDefFileId] = "";
        } else {
            targetFileContents[lineTDF] = "TOOL_CHAIN_CONF = " + jTextFieldToolsConfigFile.getText();
            targetLines[toolDefFileId] = targetFileContents[lineTDF];
        }
    }

    private void updateToolTagNames() {
        String sTagNames = vectorToString(iCheckBoxListTagName.getAllCheckedItemsString());
        int lineTTN;

        if (targetLines[tagNameId] != null) {
            lineTTN = targetLineNumber[tagNameId];
        } else {
            lineTTN = targetLineNumberMax;
            targetLineNumber[tagNameId] = lineTTN;
            targetLineNumberMax++;
        }

        if (Debug)
            System.out.println("Tag Name(s): " + sTagNames);

        if (sTagNames.length() > 0) {
            targetFileContents[lineTTN] = "TOOL_CHAIN_TAG = " + sTagNames;
            targetLines[tagNameId] = targetFileContents[lineTTN];
        } else {
            targetFileContents[lineTTN] = "#MT#";
            targetLines[tagNameId] = "";
        }
    }

    private void updateBuildTargets() {
        String sBuildTargets = vectorToString(iCheckBoxListBuildTarget.getAllCheckedItemsString());
        int lineBT;

        if (targetLines[buildTargetId] != null) {
            lineBT = targetLineNumber[buildTargetId];
        } else {
            lineBT = targetLineNumberMax;
            targetLineNumber[buildTargetId] = lineBT;
            targetLineNumberMax++;
        }
        if (Debug)
            System.out.println("Build Target(s): " + sBuildTargets);
        if (sBuildTargets.length() > 0) {
            targetFileContents[lineBT] = "TARGET = " + sBuildTargets;
            targetLines[buildTargetId] = targetFileContents[lineBT];
        } else {
            targetFileContents[lineBT] = "#MT#";
            targetLines[buildTargetId] = "";
        }

    }

    private void updateArchitectures() {
        String sArchList = jArchCheckBox.getSelectedItemsString().trim();

        if (Debug)
            System.out.println("Architectures: " + sArchList);

        int lineSA;
        if (targetLines[targetArchId] != null) {
            lineSA = targetLineNumber[targetArchId];
        } else {
            lineSA = targetLineNumberMax;
            targetLineNumber[targetArchId] = lineSA;
            targetLineNumberMax++;
        }
        if (sArchList == "") {
            targetFileContents[lineSA] = "#MT#";
            targetLines[targetArchId] = "";
        } else {
            targetFileContents[lineSA] = "TARGET_ARCH = " + sArchList;
            targetLines[targetArchId] = targetFileContents[lineSA];
        }

    }

    private void updateEnableThreads() {
        int lineET;
        if (targetLines[threadEnableId] != null) {
            lineET = targetLineNumber[threadEnableId];
        } else {
            lineET = targetLineNumberMax;
            targetLineNumber[threadEnableId] = lineET;
            targetLineNumberMax++;
        }
        if (jCheckBoxEnableThreads.isSelected() == true) {
            targetFileContents[lineET] = "MULTIPLE_THREAD = enabled";
            targetLines[threadEnableId] = targetFileContents[lineET];
        } else {
            targetFileContents[lineET] = "#MT#";
            targetLines[threadEnableId] = "";
        }
    }
    
    private void updateThreadCount() {
        int lineTC;

        if (targetLines[threadCountId] != null) {
            lineTC = targetLineNumber[threadCountId];
        } else {
            lineTC = targetLineNumberMax;
            targetLineNumber[threadCountId] = lineTC;
            targetLineNumberMax++;
        }
        if (jCheckBoxEnableThreads.isSelected() == true) {
            // Threading must be enabled
            if (jTextFieldThreadCount.getText().length() > 0) {
                // Thread Count must be greater than 0
                Scanner scan = new Scanner(jTextFieldThreadCount.getText().trim()); 
                if (scan.nextInt() > 0) {      
                    targetFileContents[lineTC] = "MAX_CONCURRENT_THREAD_NUMBER = " + jTextFieldThreadCount.getText().trim();
                    targetLines[threadCountId] = targetFileContents[lineTC];
                } else {
                    Log.wrn("Build Preferences", "Threading Enabled, but thread count is not set, setting to default of 1.");
                    targetFileContents[lineTC] = "MAX_CONCURRENT_THREAD_NUMBER = 1";
                    targetLines[threadCountId] = "MAX_CONCURRENT_THREAD_NUMBER = 1";
                }
            } else {
                Log.wrn("Build Preferences", "Threading Enabled, but thread count is not set, setting to default of 1.");
                targetFileContents[lineTC] = "MAX_CONCURRENT_THREAD_NUMBER = 1";
                targetLines[threadCountId] = "MAX_CONCURRENT_THREAD_NUMBER = 1";
            }
        } else {
            // Don't track threads if threading is not enabled
            targetFileContents[lineTC] = "#MT#";
            targetLines[threadCountId] = "";
            threadCount = "";
        }
        
    }
    private String vectorToString(Vector<String> v) {
        String s = " ";
        for (int i = 0; i < v.size(); ++i) {
            s += v.get(i);
            s += " ";
        }
        return s.trim();
    }

    protected Vector<String> stringToVector(String s) {
        if (s == null) {
            return null;
        }
        String[] sArray = s.split(" ");
        Vector<String> v = new Vector<String>();
        for (int i = 0; i < sArray.length; ++i) {
            v.add(sArray[i]);
        }
        return v;
    }

    private void saveTargetFile() {
        updateActivePlatform();
        updateToolDefFile();
        updateToolTagNames();
        updateBuildTargets();
        updateArchitectures();
        updateEnableThreads();
        updateThreadCount();

        try {
            copy(targetFile, targetFile + ".bak");
            FileWriter fileWriter = new FileWriter(targetFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);
            for (int i = 0; i < targetLineNumberMax; i++) {
                if (! targetFileContents[i].contains("#MT#"))
                    writer.write(targetFileContents[i] + "\n");
            }
            writer.close();
        } catch (IOException e) {
            Log.err(toolsConfFile + " Write Error ", e.getMessage());
            e.printStackTrace();
        }
    }

    private void copy(String txtFile, String bakFile) throws IOException {
        File fromFile = new File(txtFile);
        File toFile = new File(bakFile);
        FileInputStream fromTxt = null;
        FileOutputStream toBak = null;
        if (!fromFile.exists()) {
            fromFile.createNewFile();
        }
        try {
            fromTxt = new FileInputStream(fromFile);
            toBak = new FileOutputStream(toFile);
            byte[] buffer = new byte[4096];
            int bytes_read;
            while ((bytes_read = fromTxt.read(buffer)) != -1) {
                toBak.write(buffer, 0, bytes_read);
            }
        } finally {
            if (fromTxt != null)
                try {
                    fromTxt.close();
                } catch (IOException e) {
                    Log.err(toolsConfFile + " Read Error ", e.getMessage());

                }
            if (toBak != null)
                try {
                    toBak.close();
                } catch (IOException e) {
                    Log.err(toolsConfFile + ".bak Write Error ", e.getMessage());
                }
        }
    }

    private void exit() {
        this.setVisible(false);
        if (bTarget != null) {
            bTarget.dispose();
        }
    }
}