ded198d16694029dd076e58d40e0518648643f5f
[people/mcb30/edk2.git] / edk2 / Tools / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / toolchain / Preferences.java
1 /** @file\r
2  <<The file is used to update the Build Preferences file, target.txt>>\r
3  \r
4  <<The program will use target.txt, the tools config file specified in that file,\r
5  or it will use the default tools_def.txt file, and it will also scan the \r
6  FrameworkDatabase.db file for certain parameters. >>\r
7  \r
8  Copyright (c) 2006, Intel Corporation\r
9  All rights reserved. This program and the accompanying materials\r
10  are licensed and made available under the terms and conditions of the BSD License\r
11  which accompanies this distribution.  The full text of the license may be found at\r
12  http://opensource.org/licenses/bsd-license.php\r
13  \r
14  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
15  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
16  \r
17  Package Name: Tools\r
18  Module Name:  FrameworkWizard\r
19  \r
20  **/\r
21 \r
22 package org.tianocore.frameworkwizard.toolchain;\r
23 \r
24 import java.awt.event.ActionEvent;\r
25 import java.io.*;\r
26 import java.util.Vector;\r
27 import java.util.Iterator;\r
28 \r
29 import javax.swing.*;\r
30 // import javax.swing.JScrollPane;\r
31 import javax.swing.JTextField;\r
32 import javax.swing.JFrame;\r
33 \r
34 import org.tianocore.frameworkwizard.common.GlobalData;\r
35 import org.tianocore.frameworkwizard.common.Tools;\r
36 import org.tianocore.frameworkwizard.common.Log;\r
37 import org.tianocore.frameworkwizard.common.ui.ArchCheckBox;\r
38 import org.tianocore.frameworkwizard.common.ui.iCheckBoxList.*;\r
39 import org.tianocore.frameworkwizard.common.ui.IFrame;\r
40 import org.tianocore.frameworkwizard.workspace.Workspace;\r
41 import org.tianocore.frameworkwizard.workspace.WorkspaceTools;\r
42 import org.tianocore.frameworkwizard.platform.PlatformIdentification;\r
43 import org.tianocore.PlatformSurfaceAreaDocument;\r
44 \r
45 /**\r
46  * The class is used to update the target.txt file.\r
47  * \r
48  * It extends IDialog\r
49  * \r
50  */\r
51 public class Preferences extends IFrame {\r
52 \r
53     // /\r
54     // / Define class Serial Version UID\r
55     // /\r
56     private static final long serialVersionUID = -4777906991966638888L;\r
57 \r
58     private final boolean Debug = false;\r
59 \r
60     //\r
61     // Define class members\r
62     //\r
63     private final int oneRowHeight = 20;\r
64 \r
65     private final int twoRowHeight = 40;\r
66 \r
67     private final int threeRowHeight = 60;\r
68 \r
69     private final int sepHeight = 6;\r
70 \r
71     private final int rowOne = 12;\r
72 \r
73     private final int rowTwo = rowOne + oneRowHeight + sepHeight;\r
74 \r
75     private final int rowThree = rowTwo + oneRowHeight + sepHeight;\r
76 \r
77     private final int rowFour = rowThree + threeRowHeight + sepHeight;\r
78 \r
79     private final int rowFive = rowFour + threeRowHeight + sepHeight;\r
80 \r
81     private final int buttonRow = rowFive + oneRowHeight + sepHeight + sepHeight;\r
82 \r
83     private final int dialogHeight = buttonRow + twoRowHeight + twoRowHeight;\r
84 \r
85     private final int dialogWidth = 540;\r
86 \r
87     private final int lastButtonXLoc = 430;\r
88 \r
89     private final int next2LastButtonLoc = 329;\r
90 \r
91     /*\r
92      * Define the contents for this dialog box\r
93      */\r
94     private static Preferences bTarget = null;\r
95 \r
96     private WorkspaceTools wt = new WorkspaceTools();\r
97 \r
98     private JFrame frame;\r
99 \r
100     private JPanel jContentPane = null;\r
101 \r
102     private JLabel jLabelToolsConfigFile = null;\r
103 \r
104     private JTextField jTextFieldToolsConfigFile = null;\r
105 \r
106     private final int toolConfigFileRow = rowOne;\r
107 \r
108     private final int toolDefFileId = 3;\r
109 \r
110     private JLabel jLabelActivePlatform = null;\r
111 \r
112     private JComboBox jComboBoxActivePlatform = null;\r
113 \r
114     private final int activePlatformId = 0;\r
115 \r
116     private final int activePlatformRow = rowTwo;\r
117 \r
118     private JLabel jLabelToolChainTagName = null;\r
119 \r
120     private JScrollPane jScrollPaneTagName = null;\r
121 \r
122     private ICheckBoxList iCheckBoxListTagName = null;\r
123 \r
124     private final int toolChainTagNameRow = rowThree;\r
125 \r
126     private final int tagNameId = 4;\r
127 \r
128     private JLabel jLabelBuildTarget = null;\r
129 \r
130     private JScrollPane jScrollPaneBuildTarget = null;\r
131 \r
132     private ICheckBoxList iCheckBoxListBuildTarget = null;\r
133 \r
134     private final int buildTargetRow = rowFour;\r
135 \r
136     private final int buildTargetId = 1;\r
137 \r
138     private JLabel jLabelTargetArch = null;\r
139 \r
140     private ArchCheckBox jArchCheckBox = null;\r
141 \r
142     private final int targetArchRow = rowFive;\r
143 \r
144     private final int targetArchId = 2;\r
145 \r
146     private JButton jButtonBrowse = null;\r
147 \r
148     private JButton jButtonSave = null;\r
149 \r
150     private JButton jButtonCancel = null;\r
151 \r
152     private final int labelColumn = 12;\r
153 \r
154     private final int labelWidth = 155;\r
155 \r
156     private final int valueColumn = 168;\r
157 \r
158     private final int valueWidth = 352;\r
159 \r
160     private final int valueWidthShort = 260;\r
161 \r
162     private final int buttonWidth = 90;\r
163 \r
164     private String workspaceDir = Workspace.getCurrentWorkspace() + System.getProperty("file.separator");\r
165 \r
166     private String toolsDir = Workspace.getCurrentWorkspace() + System.getProperty("file.separator") + "Tools"\r
167                               + System.getProperty("file.separator") + "Conf";\r
168 \r
169     private String defaultToolsConf = toolsDir + System.getProperty("file.separator") + "tools_def.txt";\r
170 \r
171     private String targetFile = toolsDir + System.getProperty("file.separator") + "target.txt";\r
172 \r
173     private String[] targetFileContents = new String[500];\r
174 \r
175     // private String[] toolsConfContents;\r
176 \r
177     private String[] targetLines = new String[5];\r
178 \r
179     private int targetLineNumber[] = new int[5];\r
180 \r
181     private String toolsConfFile;\r
182 \r
183     private String toolsDefTargetNames = null;\r
184 \r
185     private final int toolsDefTargetNameField = 0;\r
186 \r
187     private String toolsDefTagNames = null;\r
188 \r
189     private final int toolsDefTagNameField = 1;\r
190 \r
191     private String toolsDefArchNames = null;\r
192 \r
193     private final int toolsDefArchNameField = 2;\r
194 \r
195     private String toolsDefIdentifier = null;\r
196 \r
197     private int targetLineNumberMax;\r
198 \r
199     private Vector<String> vArchList = null;\r
200 \r
201     //\r
202     // Not used by UI\r
203     //\r
204     //    private Preferences id = null;\r
205 \r
206     //    private EnumerationData ed = new EnumerationData();\r
207 \r
208     /**\r
209      This method initializes jTextFieldToolsConfigFile  \r
210      \r
211      @return javax.swing.JTextField  jTextFieldToolsConfigFile\r
212      **/\r
213     private JTextField getJTextFieldToolsConfigFile() {\r
214         if (jTextFieldToolsConfigFile == null) {\r
215             if (targetLines[toolDefFileId] != null) {\r
216                 String sLine[] = targetLines[toolDefFileId].trim().split("=");\r
217                 jTextFieldToolsConfigFile = new JTextField(sLine[1].trim());\r
218             } else\r
219                 jTextFieldToolsConfigFile = new JTextField();\r
220 \r
221             jTextFieldToolsConfigFile.setBounds(new java.awt.Rectangle(valueColumn, toolConfigFileRow, valueWidthShort,\r
222                                                                        oneRowHeight));\r
223             jTextFieldToolsConfigFile.setPreferredSize(new java.awt.Dimension(valueWidthShort, oneRowHeight));\r
224             jTextFieldToolsConfigFile\r
225                                      .setToolTipText("<html>"\r
226                                                      + "Specify the name of the filename to use for specifying"\r
227                                                      + "<br>the tools to use for the build.  If not specified,"\r
228                                                      + "<br>tools_def.txt will be used for the build.  This file"\r
229                                                      + "<br>MUST be located in the WORKSPACE/Tools/Conf directory.</html>");\r
230 \r
231         }\r
232         return jTextFieldToolsConfigFile;\r
233     }\r
234 \r
235     /**\r
236      * This method initializes jComboBoxActivePlatform\r
237      * \r
238      * @return javax.swing.JComboBox jComboBoxActivePlatform\r
239      * \r
240      */\r
241     private JComboBox getActivePlatform() {\r
242         Vector<PlatformIdentification> vPlatformId = wt.getAllPlatforms();\r
243 \r
244         if (jComboBoxActivePlatform == null) {\r
245             jComboBoxActivePlatform = new JComboBox();\r
246             jComboBoxActivePlatform.setBounds(new java.awt.Rectangle(valueColumn, activePlatformRow, valueWidth,\r
247                                                                      oneRowHeight));\r
248             jComboBoxActivePlatform.setPreferredSize(new java.awt.Dimension(valueWidth, oneRowHeight));\r
249             jComboBoxActivePlatform.setToolTipText("<html>Select &quot;Do Not Set&quot; if you want to build a platform"\r
250                                                    + "<br>from the directory where the FPD file exists,"\r
251                                                    + "<br>otherwise scroll down to select the platform.</html>");\r
252 \r
253             /*\r
254              * Generate the data, selecting what is in target.txt\r
255              */\r
256             jComboBoxActivePlatform.addItem("Do Not Set");\r
257             Iterator<PlatformIdentification> iter = vPlatformId.iterator();\r
258             while (iter.hasNext()) {\r
259                 PlatformIdentification item = iter.next();\r
260                 String path = item.getPath().trim();\r
261                 String str = path.substring(workspaceDir.length(), path.length());\r
262                 str.replace(System.getProperty("file.separator"), "/");\r
263                 jComboBoxActivePlatform.addItem(str.trim());\r
264             }\r
265             if (targetLines[activePlatformId] == null)\r
266                 jComboBoxActivePlatform.setSelectedItem("Do Not Set");\r
267             else\r
268                 jComboBoxActivePlatform.setSelectedItem(targetLines[activePlatformId]);\r
269         }\r
270         return jComboBoxActivePlatform;\r
271     }\r
272 \r
273     /**\r
274      * This method initializes jScrollPaneTagName\r
275      * \r
276      * @return javax.swing.JScrollPane jScrollPaneTagName\r
277      * \r
278      */\r
279     private JScrollPane getJScrollPaneTagName() {\r
280 \r
281         if (jScrollPaneTagName == null) {\r
282             jScrollPaneTagName = new JScrollPane();\r
283             jScrollPaneTagName.setBounds(new java.awt.Rectangle(valueColumn, toolChainTagNameRow, valueWidth,\r
284                                                                 threeRowHeight));\r
285             jScrollPaneTagName.setPreferredSize(new java.awt.Dimension(valueWidth, threeRowHeight));\r
286             jScrollPaneTagName.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);\r
287             jScrollPaneTagName.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);\r
288             jScrollPaneTagName.setViewportView(getICheckBoxListTagName());\r
289             jScrollPaneTagName.setToolTipText("<html>"\r
290                                               + "Specify the TagName(s) from the tool configuration file to use"\r
291                                               + "<br>for your builds.  If not specified, all applicable TagName"\r
292                                               + " <br>tools will be used for the build.</html>");\r
293             jScrollPaneTagName.setVisible(true);\r
294 \r
295         }\r
296         return jScrollPaneTagName;\r
297     }\r
298 \r
299     private ICheckBoxList getICheckBoxListTagName() {\r
300         if (iCheckBoxListTagName == null) {\r
301             iCheckBoxListTagName = new ICheckBoxList();\r
302             \r
303             if (toolsDefTagNames != null) {\r
304                 toolsDefTagNames.trim();\r
305                 String aTagNames[] = toolsDefTagNames.trim().split(" ");\r
306                 Vector<String> vTags = new Vector<String>();\r
307                 for (int i = 0; i < aTagNames.length; i++) {\r
308                     vTags.add(aTagNames[i]);\r
309                 }\r
310                 iCheckBoxListTagName.setAllItems(vTags);\r
311             } else {\r
312                 Vector<String> defaultTags = stringToVector("MYTOOLS");\r
313                 iCheckBoxListTagName.setAllItems(defaultTags);\r
314             }\r
315             \r
316             iCheckBoxListTagName.setAllItemsUnchecked();\r
317             iCheckBoxListTagName.setToolTipText("<html>"\r
318                                                 + "Specify the TagName(s) from the tool configuration file to use"\r
319                                                 + "<br>for your builds.  If not specified, all applicable TagName"\r
320                                                 + " <br>tools will be used for the build.</html>");\r
321             Vector<String> vSelectedTags = new Vector<String>();\r
322             if (targetLines[tagNameId] != null) {\r
323                 targetLines[tagNameId].trim();\r
324                 String targetTags[] = targetLines[tagNameId].trim().split(" ");\r
325                 for (int j = 0; j < targetTags.length; j++)\r
326                     vSelectedTags.add(targetTags[j]);\r
327                 iCheckBoxListTagName.initCheckedItem(true, vSelectedTags);\r
328             }\r
329         }\r
330         return iCheckBoxListTagName;\r
331     }\r
332 \r
333     /**\r
334      * This method initializes jScrollPaneBuildTarget\r
335      * \r
336      * @return javax.swing.JComboBox jScrollPaneBuildTarget\r
337      * \r
338      */\r
339     private JScrollPane getJScrollPaneBuildTarget() {\r
340         if (jScrollPaneBuildTarget == null) {\r
341             jScrollPaneBuildTarget = new JScrollPane();\r
342             jScrollPaneBuildTarget.setBounds(new java.awt.Rectangle(valueColumn, buildTargetRow, valueWidth,\r
343                                                                     threeRowHeight));\r
344             jScrollPaneBuildTarget.setPreferredSize(new java.awt.Dimension(valueWidth, threeRowHeight));\r
345             jScrollPaneBuildTarget.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);\r
346             jScrollPaneBuildTarget.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);\r
347             jScrollPaneBuildTarget.setViewportView(getICheckBoxListBuildTarget());\r
348             jScrollPaneBuildTarget.setVisible(true);\r
349             jScrollPaneBuildTarget.setToolTipText("<html>"\r
350                                                   + "Select the TARGET Names that you want to build, such as<BR>"\r
351                                                   + "BUILD or BUILD and RELEASE"\r
352                                                   + "<br>If you do not set any of these, all available targets"\r
353                                                   + "<br>will be built.</html>");\r
354 \r
355         }\r
356         return jScrollPaneBuildTarget;\r
357     }\r
358 \r
359     private ICheckBoxList getICheckBoxListBuildTarget() {\r
360         if (iCheckBoxListBuildTarget == null) {\r
361             \r
362             String aBuildTargets[] = toolsDefTargetNames.trim().split(" ");\r
363             Vector<String> vBuildTargets = new Vector<String>();\r
364             for (int i = 0; i < aBuildTargets.length; i++) {\r
365                 vBuildTargets.add(aBuildTargets[i]);\r
366             }\r
367             iCheckBoxListBuildTarget = new ICheckBoxList();\r
368             iCheckBoxListBuildTarget.setAllItems(vBuildTargets);\r
369             iCheckBoxListBuildTarget.setAllItemsUnchecked();\r
370             iCheckBoxListBuildTarget.setToolTipText("<html>"\r
371                                                     + "Select the TARGET Names that you want to build, such as<BR>"\r
372                                                     + "BUILD or BUILD and RELEASE"\r
373                                                     + "<br>If you do not set any of these, all available targets"\r
374                                                     + "<br>will be built.</html>");\r
375 \r
376             Vector<String> vSelectedTags = new Vector<String>();\r
377             if (targetLines[buildTargetId] != null) {\r
378                 targetLines[buildTargetId].trim();\r
379                 String targetTags[] = targetLines[buildTargetId].trim().split(" ");\r
380                 for (int j = 0; j < targetTags.length; j++)\r
381                     vSelectedTags.add(targetTags[j]);\r
382                 iCheckBoxListBuildTarget.initCheckedItem(true, vSelectedTags);\r
383             }\r
384         }\r
385         return iCheckBoxListBuildTarget;\r
386     }\r
387 \r
388     /**\r
389      This method initializes jButtonBrowse   \r
390      \r
391      @return javax.swing.JButton \r
392      **/\r
393     private JButton getJButtonBrowse() {\r
394         if (jButtonBrowse == null) {\r
395             jButtonBrowse = new JButton();\r
396             jButtonBrowse\r
397                          .setBounds(new java.awt.Rectangle(lastButtonXLoc, toolConfigFileRow, buttonWidth, oneRowHeight));\r
398             jButtonBrowse.setText("Browse");\r
399             jButtonBrowse.setPreferredSize(new java.awt.Dimension(buttonWidth, oneRowHeight));\r
400             jButtonBrowse.addActionListener(new AbstractAction() {\r
401                 /**\r
402                  * \r
403                  */\r
404                 private static final long serialVersionUID = 1L;\r
405 \r
406                 public void actionPerformed(ActionEvent e) {\r
407                     //\r
408                     // Select files from current workspace\r
409                     //\r
410                     String dirPrefix = toolsDir + System.getProperty("file.separator");\r
411                     JFileChooser chooser = new JFileChooser(dirPrefix);\r
412                     File theFile = null;\r
413                     //                    String headerDest = null;\r
414 \r
415                     chooser.setMultiSelectionEnabled(false);\r
416                     chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);\r
417                     int retval = chooser.showOpenDialog(frame);\r
418                     if (retval == JFileChooser.APPROVE_OPTION) {\r
419 \r
420                         theFile = chooser.getSelectedFile();\r
421                         String file = theFile.getPath();\r
422                         if (!file.startsWith(dirPrefix)) {\r
423                             JOptionPane.showMessageDialog(frame, "You can only select files in the Tools"\r
424                                                                  + System.getProperty("file.separator")\r
425                                                                  + "Conf directory!");\r
426 \r
427                             return;\r
428                         }\r
429 \r
430                         jTextFieldToolsConfigFile.setText("Tools/Conf/" + theFile.getName());\r
431                     } else {\r
432                         return;\r
433                     }\r
434                 }\r
435             });\r
436         }\r
437         return jButtonBrowse;\r
438     }\r
439 \r
440     /**\r
441      * This method initializes jButtonOk\r
442      * \r
443      * @return javax.swing.JButton\r
444      * \r
445      */\r
446     private JButton getJButtonSave() {\r
447         if (jButtonSave == null) {\r
448             jButtonSave = new JButton();\r
449             jButtonSave.setBounds(new java.awt.Rectangle(next2LastButtonLoc, buttonRow, buttonWidth, oneRowHeight));\r
450             jButtonSave.setText("Save");\r
451             jButtonSave.addActionListener(this);\r
452         }\r
453         return jButtonSave;\r
454     }\r
455 \r
456     /**\r
457      * This method initializes jButtonCancel\r
458      * \r
459      * @return javax.swing.JButton\r
460      * \r
461      */\r
462     private JButton getJButtonCancel() {\r
463         if (jButtonCancel == null) {\r
464             jButtonCancel = new JButton();\r
465             jButtonCancel.setBounds(new java.awt.Rectangle(lastButtonXLoc, buttonRow, buttonWidth, oneRowHeight));\r
466             jButtonCancel.setText("Cancel");\r
467             jButtonCancel.addActionListener(this);\r
468         }\r
469         return jButtonCancel;\r
470     }\r
471 \r
472     public static void main(String[] args) {\r
473         // TODO Auto-generated method stub\r
474 \r
475     }\r
476 \r
477     public static Preferences getInstance() {\r
478         if (bTarget == null) {\r
479             bTarget = new Preferences();\r
480         }\r
481         return bTarget;\r
482     }\r
483 \r
484     /**\r
485      * This is the default constructor\r
486      */\r
487     public Preferences() {\r
488         super();\r
489         init();\r
490     }\r
491 \r
492     /**\r
493      * This method initializes this\r
494      * \r
495      */\r
496     private void init() {\r
497 \r
498         for (int i = 0; i < 5; i++) {\r
499             targetLines[i] = null;\r
500             targetLineNumber[i] = -1;\r
501         }\r
502         initReadFiles();\r
503         this.setSize(dialogWidth, dialogHeight);\r
504         this.setContentPane(getJContentPane());\r
505         this.setTitle("Build Preferences [" + toolsDefIdentifier + "]");\r
506         this.setDefaultCloseOperation(IFrame.EXIT_ON_CLOSE);\r
507         this.centerWindow();\r
508         this.setVisible(true);\r
509     }\r
510 \r
511     /**\r
512      * This method initializes this Fill values to all fields if these values are\r
513      * not empty\r
514      * \r
515      * @param initReadFiles\r
516      * \r
517      */\r
518     private void initReadFiles() {\r
519         /*\r
520          * TODO\r
521          * Read Current target.txt file first\r
522          * Read TOOL_CHAIN_CONF file if specified, otherwise use tools_def.txt\r
523          */\r
524 \r
525         readTargetTxtFile();\r
526         boolean haveBuildTargets = readToolDefinitionFile();\r
527         if (!haveBuildTargets) {\r
528             // Lookup Build Targets from the platforms\r
529             readPlatformFileBuildTargets();\r
530         }\r
531     }\r
532 \r
533     private void readPlatformFileBuildTargets() {\r
534         Vector<PlatformIdentification> vPlatformId = wt.getAllPlatforms();\r
535         String sBuildTargets = "";\r
536 \r
537         // foreach platform, build a list of BuildTargets\r
538         Iterator<PlatformIdentification> iter = vPlatformId.iterator();\r
539         while (iter.hasNext()) {\r
540             PlatformIdentification item = iter.next();\r
541             PlatformSurfaceAreaDocument.PlatformSurfaceArea fpd = GlobalData.openingPlatformList\r
542                                                                                                 .getOpeningPlatformById(\r
543                                                                                                                         item)\r
544                                                                                                 .getXmlFpd();\r
545             sBuildTargets += fpd.getPlatformDefinitions().getBuildTargets().toString() + " ";\r
546         }\r
547         String allTargets[] = sBuildTargets.trim().split(" ");\r
548         for (int i = 0; i < allTargets.length; i++) {\r
549             if (!toolsDefTargetNames.contains(allTargets[i])) {\r
550                 toolsDefTargetNames += allTargets[i] + " ";\r
551             }\r
552         }\r
553     }\r
554 \r
555     private boolean readToolDefinitionFile() {\r
556 \r
557         // Parse the tool definition file looking for targets and architectures\r
558         toolsConfFile = null;\r
559         boolean buildTargetsExist = true;\r
560 \r
561         if (targetLines[toolDefFileId] != null) {\r
562             String[] result = new String[2];\r
563             targetLines[toolDefFileId].trim();\r
564             result = (targetLines[toolDefFileId]).split("=");\r
565             String resString = (Tools.convertPathToCurrentOsType(result[1])).trim();\r
566             toolsConfFile = workspaceDir.trim() + resString.trim();\r
567             File toolsDefFile = new File(toolsConfFile);\r
568             if (!toolsDefFile.exists()) {\r
569                 JOptionPane.showMessageDialog(this, "<html>" + "Tool Definition file, " + toolsConfFile\r
570                                                     + "<br>specified in the target.txt file does not exist!"\r
571                                                     + "<br>Using the default Tool Definition File:<br>"\r
572                                                     + defaultToolsConf);\r
573                 toolsConfFile = defaultToolsConf;\r
574             }\r
575         } else {\r
576             toolsConfFile = defaultToolsConf;\r
577         }\r
578         String[] toolsDefFields = new String[5];\r
579         for (int i = 0; i < 5; i++)\r
580             toolsDefFields[i] = null;\r
581         File toolDefFile = new File(toolsConfFile);\r
582         if (toolDefFile.exists()) {\r
583             try {\r
584                 FileReader fileReader = new FileReader(toolDefFile);\r
585                 BufferedReader reader = new BufferedReader(fileReader);\r
586                 String rLine = null;\r
587                 String result[];\r
588                 int lineCounter = 0;\r
589                 while ((rLine = reader.readLine()) != null) {\r
590 \r
591                     if (rLine.startsWith("IDENTIFIER")) {\r
592                         result = rLine.split("=");\r
593                         toolsDefIdentifier = (result[1]).trim();\r
594                     } else if ((!rLine.startsWith("#")) && (rLine.contains("="))) {\r
595                         result = rLine.split("=");\r
596                         toolsDefFields = ((result[0]).trim()).split("_");\r
597                         if (toolsDefTargetNames == null) {\r
598                             toolsDefTargetNames = (toolsDefFields[toolsDefTargetNameField]).trim() + " ";\r
599                         } else if (!toolsDefTargetNames.contains((toolsDefFields[toolsDefTargetNameField]).trim())) {\r
600                             toolsDefTargetNames += (toolsDefFields[toolsDefTargetNameField]).trim() + " ";\r
601                         }\r
602                         if (toolsDefTagNames == null) {\r
603                             toolsDefTagNames = (toolsDefFields[toolsDefTagNameField]).trim() + " ";\r
604                         } else if (!toolsDefTagNames.contains((toolsDefFields[toolsDefTagNameField]).trim())) {\r
605                             toolsDefTagNames += (toolsDefFields[toolsDefTagNameField]).trim() + " ";\r
606                         }\r
607                         if (toolsDefArchNames == null) {\r
608                             toolsDefArchNames = (toolsDefFields[toolsDefArchNameField]).trim() + " ";\r
609                         } else if (!toolsDefArchNames.contains((toolsDefFields[toolsDefArchNameField]).trim())) {\r
610                             toolsDefArchNames += (toolsDefFields[toolsDefArchNameField]).trim() + " ";\r
611                         }\r
612                     }\r
613                     lineCounter++;\r
614                 }\r
615                 reader.close();\r
616                 if (!toolsDefTargetNames.matches("[A-Z]+")) {\r
617                     toolsDefTargetNames = toolsDefTargetNames.replace("* ", "").trim();\r
618                     if (Debug)\r
619                         System.out.println("tools_def file does not define build targets: '" + toolsDefTargetNames\r
620                                            + "'");\r
621                     buildTargetsExist = false;\r
622                 }\r
623             } catch (IOException e) {\r
624                 Log.log(toolsConfFile + " Read Error ", e.getMessage());\r
625                 e.printStackTrace();\r
626             }\r
627         }\r
628         return buildTargetsExist;\r
629     }\r
630 \r
631     private void readTargetTxtFile() {\r
632         File tFile = new File(targetFile);\r
633 \r
634         if (tFile.exists()) {\r
635             try {\r
636                 FileReader fileReader = new FileReader(targetFile);\r
637                 BufferedReader reader = new BufferedReader(fileReader);\r
638                 targetLineNumberMax = 0;\r
639                 String rLine = null;\r
640                 while ((rLine = reader.readLine()) != null) {\r
641                     targetFileContents[targetLineNumberMax] = rLine;\r
642                     if (rLine.startsWith("ACTIVE_PLATFORM")) {\r
643                         // Only one active platform is permitted!\r
644                         targetLines[activePlatformId] = rLine;\r
645                         targetLineNumber[activePlatformId] = targetLineNumberMax;\r
646                     }\r
647                     if ((rLine.startsWith("TARGET" + " ")) || (rLine.startsWith("TARGET" + "\t"))\r
648                         || (rLine.startsWith("TARGET="))) {\r
649                         // Handle multiple Target Names\r
650                         if (rLine.contains(","))\r
651                             targetLines[buildTargetId] = rLine.trim().replaceAll(",", " ");\r
652                         else\r
653                             targetLines[buildTargetId] = rLine.trim();\r
654                         targetLineNumber[buildTargetId] = targetLineNumberMax;\r
655                     }\r
656                     if (rLine.startsWith("TARGET_ARCH")) {\r
657                         // Handle multiple Target Architectures\r
658                         if (rLine.contains(","))\r
659                             targetLines[targetArchId] = rLine.trim().replaceAll(",", " ");\r
660                         else\r
661                             targetLines[targetArchId] = rLine.trim();\r
662                         targetLineNumber[targetArchId] = targetLineNumberMax;\r
663                     }\r
664                     if (rLine.startsWith("TOOL_CHAIN_CONF")) {\r
665                         // Only one file is permitted\r
666                         targetLines[toolDefFileId] = rLine.trim();\r
667                         targetLineNumber[toolDefFileId] = targetLineNumberMax;\r
668                     }\r
669 \r
670                     if (rLine.startsWith("TOOL_CHAIN_TAG")) {\r
671                         // Handle multiple Tool TagNames\r
672                         if (rLine.contains(","))\r
673                             targetLines[tagNameId] = rLine.trim().replaceAll(",", " ");\r
674                         else\r
675                             targetLines[tagNameId] = rLine.trim();\r
676                         targetLineNumber[tagNameId] = targetLineNumberMax;\r
677                     }\r
678                     targetLineNumberMax++;\r
679                 }\r
680                 reader.close();\r
681                 String archLine[] = new String[2];\r
682                 if (targetLines[targetArchId] != null) {\r
683                     if (targetLines[targetArchId].contains("=")) {\r
684                         if (targetLines[targetArchId].contains(","))\r
685                             targetLines[targetArchId] = targetLines[targetArchId].trim().replaceAll(",", " ");\r
686                         if (targetLines[targetArchId].length() > 0)\r
687                             archLine = targetLines[targetArchId].trim().split("=");\r
688                         vArchList = stringToVector(archLine[1]);\r
689                     }\r
690                 }\r
691 \r
692                 if (Debug == true)\r
693                     for (int i = 0; i <= tagNameId; i++)\r
694                         System.out.println("targetLines[" + i + "] contains: " + targetLines[i] + " index is: "\r
695                                            + targetLineNumber[i]);\r
696 \r
697             } catch (IOException e) {\r
698                 Log.log(this.targetFile + " Read Error ", e.getMessage());\r
699                 e.printStackTrace();\r
700             }\r
701         }\r
702 \r
703     }\r
704 \r
705     /**\r
706      * This method initializes jContentPane\r
707      * \r
708      * @return javax.swing.JPanel jContentPane\r
709      * \r
710      */\r
711     private JPanel getJContentPane() {\r
712         if (jContentPane == null) {\r
713             jLabelToolsConfigFile = new JLabel();\r
714             jLabelToolsConfigFile.setBounds(new java.awt.Rectangle(labelColumn, toolConfigFileRow, labelWidth,\r
715                                                                    oneRowHeight));\r
716             jLabelToolsConfigFile.setText("Tool Chain Definition File");\r
717             jLabelActivePlatform = new JLabel();\r
718             jLabelActivePlatform.setText("Select Active Platform");\r
719             jLabelActivePlatform.setBounds(new java.awt.Rectangle(labelColumn, activePlatformRow, labelWidth,\r
720                                                                   oneRowHeight));\r
721             jLabelToolChainTagName = new JLabel();\r
722             jLabelToolChainTagName.setBounds(new java.awt.Rectangle(labelColumn, toolChainTagNameRow, labelWidth,\r
723                                                                     oneRowHeight));\r
724             jLabelToolChainTagName.setText("Select Tool Tag Name");\r
725             jLabelBuildTarget = new JLabel();\r
726             jLabelBuildTarget.setBounds(new java.awt.Rectangle(labelColumn, buildTargetRow, labelWidth, oneRowHeight));\r
727             jLabelBuildTarget.setText("Select Build Target");\r
728             jLabelTargetArch = new JLabel();\r
729             jLabelTargetArch.setBounds(new java.awt.Rectangle(labelColumn, targetArchRow, labelWidth, oneRowHeight));\r
730             jLabelTargetArch.setText("Build Architectures");\r
731             jArchCheckBox = new ArchCheckBox();\r
732             jArchCheckBox.setBounds(new java.awt.Rectangle(valueColumn, targetArchRow, valueWidth, oneRowHeight));\r
733             jArchCheckBox.setPreferredSize(new java.awt.Dimension(valueWidth, oneRowHeight));\r
734 \r
735             jContentPane = new JPanel();\r
736             jContentPane.setLayout(null);\r
737             jContentPane.setPreferredSize(new java.awt.Dimension(dialogWidth - 10, dialogHeight - 10));\r
738 \r
739             jContentPane.add(jLabelToolsConfigFile, null);\r
740             jContentPane.add(getJTextFieldToolsConfigFile(), null);\r
741             jContentPane.add(getJButtonBrowse(), null);\r
742 \r
743             jContentPane.add(jLabelActivePlatform, null);\r
744             jContentPane.add(getActivePlatform(), null);\r
745 \r
746             jContentPane.add(jLabelToolChainTagName, null);\r
747             jContentPane.add(getJScrollPaneTagName(), null);\r
748 \r
749             jContentPane.add(jLabelBuildTarget, null);\r
750             jContentPane.add(getJScrollPaneBuildTarget(), null);\r
751 \r
752             jContentPane.add(jLabelTargetArch, null);\r
753 \r
754             jArchCheckBox.setSelectedItems(vArchList);\r
755             jContentPane.add(jArchCheckBox, null);\r
756 \r
757             jContentPane.add(getJButtonSave(), null);\r
758             jContentPane.add(getJButtonCancel(), null);\r
759         }\r
760         return jContentPane;\r
761     }\r
762 \r
763     /*\r
764      * (non-Javadoc)\r
765      * \r
766      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)\r
767      * \r
768      * Override actionPerformed to listen all actions\r
769      * \r
770      */\r
771     public void actionPerformed(ActionEvent arg0) {\r
772 \r
773         if (arg0.getSource() == jButtonBrowse) {\r
774             // TODO: Call file browser, starting in $WORKSPACE/Tools/Conf directory\r
775 \r
776         }\r
777 \r
778         if (arg0.getSource() == jButtonSave) {\r
779             saveTargetFile();\r
780             JOptionPane.showMessageDialog(this, "<html>The target.txt file has been saved!"\r
781                                                 + "<br>A copy of the original file, target.txt.bak has"\r
782                                                 + "<br>also been created.</html>");\r
783             this.exit();\r
784         }\r
785 \r
786         if (arg0.getSource() == jButtonCancel) {\r
787             this.exit();\r
788         }\r
789     }\r
790 \r
791     private void updateActivePlatform() {\r
792         int lineAP;\r
793         if (targetLines[activePlatformId] != null) {\r
794             lineAP = targetLineNumber[activePlatformId];\r
795         } else {\r
796             lineAP = targetLineNumberMax;\r
797             targetLineNumber[activePlatformId] = lineAP;\r
798             targetLineNumberMax++;\r
799         }\r
800         if (jComboBoxActivePlatform.getSelectedItem() == "Do Not Set") {\r
801             targetFileContents[lineAP] = "";\r
802             targetLines[activePlatformId] = "";\r
803         } else {\r
804             targetFileContents[lineAP] = "ACTIVE_PLATFORM = " + jComboBoxActivePlatform.getSelectedItem() + "\r\n";\r
805             targetLines[activePlatformId] = targetFileContents[lineAP];\r
806         }\r
807         if (Debug)\r
808             System.out.println("Active Platform: " + targetFileContents[lineAP]);\r
809     }\r
810 \r
811     private void updateToolDefFile() {\r
812         int lineTDF;\r
813         if (targetLines[toolDefFileId] != null) {\r
814             lineTDF = targetLineNumber[toolDefFileId];\r
815         } else {\r
816             lineTDF = targetLineNumberMax;\r
817             targetLineNumber[toolDefFileId] = lineTDF;\r
818             targetLineNumberMax++;\r
819         }\r
820         if (Debug)\r
821             System.out.println("Tool Config File: " + jTextFieldToolsConfigFile.getText());\r
822         if (jTextFieldToolsConfigFile.getText() == null) {\r
823             targetFileContents[lineTDF] = "";\r
824             targetLines[toolDefFileId] = "";\r
825         } else {\r
826             targetFileContents[lineTDF] = "TOOL_CHAIN_CONF = " + jTextFieldToolsConfigFile.getText() + "\r\n";\r
827             targetLines[toolDefFileId] = targetFileContents[lineTDF];\r
828         }\r
829     }\r
830 \r
831     private void updateToolTagNames() {\r
832         String sTagNames = vectorToString(iCheckBoxListTagName.getAllCheckedItemsString());\r
833         int lineTTN;\r
834 \r
835         if (targetLines[tagNameId] != null) {\r
836             lineTTN = targetLineNumber[tagNameId];\r
837         } else {\r
838             lineTTN = targetLineNumberMax;\r
839             targetLineNumber[tagNameId] = lineTTN;\r
840             targetLineNumberMax++;\r
841         }\r
842 \r
843         if (Debug)\r
844             System.out.println("Tag Name(s): " + sTagNames);\r
845 \r
846         if (sTagNames.length() > 0) {\r
847             targetFileContents[lineTTN] = "TOOL_CHAIN_TAG = " + sTagNames + "\r\n";\r
848             targetLines[tagNameId] = targetFileContents[lineTTN];\r
849         } else {\r
850             targetFileContents[lineTTN] = "";\r
851             targetLines[tagNameId] = "";\r
852         }\r
853     }\r
854 \r
855     private void updateBuildTargets() {\r
856         String sBuildTargets = vectorToString(iCheckBoxListBuildTarget.getAllCheckedItemsString());\r
857         int lineBT;\r
858 \r
859         if (targetLines[buildTargetId] != null) {\r
860             lineBT = targetLineNumber[buildTargetId];\r
861         } else {\r
862             lineBT = targetLineNumberMax;\r
863             targetLineNumber[buildTargetId] = lineBT;\r
864             targetLineNumberMax++;\r
865         }\r
866         if (Debug)\r
867             System.out.println("Build Target(s): " + sBuildTargets);\r
868         if (sBuildTargets.length() > 0) {\r
869             targetFileContents[lineBT] = "TARGET = " + sBuildTargets + "\r\n";\r
870             targetLines[buildTargetId] = targetFileContents[lineBT];\r
871         } else {\r
872             targetFileContents[lineBT] = "";\r
873             targetLines[buildTargetId] = "";\r
874         }\r
875 \r
876     }\r
877 \r
878     private void updateArchitectures() {\r
879         String sArchList = jArchCheckBox.getSelectedItemsString().trim();\r
880 \r
881         if (Debug)\r
882             System.out.println("Architectures: " + sArchList);\r
883 \r
884         int lineSA;\r
885         if (targetLines[targetArchId] != null) {\r
886             lineSA = targetLineNumber[targetArchId];\r
887         } else {\r
888             lineSA = targetLineNumberMax;\r
889             targetLineNumber[targetArchId] = lineSA;\r
890             targetLineNumberMax++;\r
891         }\r
892         if (sArchList == "") {\r
893             targetFileContents[lineSA] = "";\r
894             targetLines[targetArchId] = "";\r
895         } else {\r
896             targetFileContents[lineSA] = "TARGET_ARCH = " + sArchList + "\r\n";\r
897             targetLines[targetArchId] = targetFileContents[lineSA];\r
898         }\r
899 \r
900     }\r
901 \r
902     private String vectorToString(Vector<String> v) {\r
903         String s = " ";\r
904         for (int i = 0; i < v.size(); ++i) {\r
905             s += v.get(i);\r
906             s += " ";\r
907         }\r
908         return s.trim();\r
909     }\r
910 \r
911     protected Vector<String> stringToVector(String s) {\r
912         if (s == null) {\r
913             return null;\r
914         }\r
915         String[] sArray = s.split(" ");\r
916         Vector<String> v = new Vector<String>();\r
917         for (int i = 0; i < sArray.length; ++i) {\r
918             v.add(sArray[i]);\r
919         }\r
920         return v;\r
921     }\r
922 \r
923     private void saveTargetFile() {\r
924         updateActivePlatform();\r
925         updateToolDefFile();\r
926         updateToolTagNames();\r
927         updateBuildTargets();\r
928         updateArchitectures();\r
929 \r
930         try {\r
931             copy(targetFile, targetFile + ".bak");\r
932             FileWriter fileWriter = new FileWriter(targetFile);\r
933             BufferedWriter writer = new BufferedWriter(fileWriter);\r
934             for (int i = 0; i < targetLineNumberMax; i++) {\r
935                 writer.write(targetFileContents[i] + "\r\n");\r
936             }\r
937             writer.close();\r
938         } catch (IOException e) {\r
939             Log.err(toolsConfFile + " Write Error ", e.getMessage());\r
940             e.printStackTrace();\r
941         }\r
942     }\r
943 \r
944     private void copy(String txtFile, String bakFile) throws IOException {\r
945         File fromFile = new File(txtFile);\r
946         File toFile = new File(bakFile);\r
947         FileInputStream fromTxt = null;\r
948         FileOutputStream toBak = null;\r
949         try {\r
950             fromTxt = new FileInputStream(fromFile);\r
951             toBak = new FileOutputStream(toFile);\r
952             byte[] buffer = new byte[4096];\r
953             int bytes_read;\r
954             while ((bytes_read = fromTxt.read(buffer)) != -1) {\r
955                 toBak.write(buffer, 0, bytes_read);\r
956             }\r
957         } finally {\r
958             if (fromTxt != null)\r
959                 try {\r
960                     fromTxt.close();\r
961                 } catch (IOException e) {\r
962                     Log.err(toolsConfFile + " Read Error ", e.getMessage());\r
963 \r
964                 }\r
965             if (toBak != null)\r
966                 try {\r
967                     toBak.close();\r
968                 } catch (IOException e) {\r
969                     Log.err(toolsConfFile + ".bak Write Error ", e.getMessage());\r
970                 }\r
971         }\r
972     }\r
973 \r
974     private void exit() {\r
975         this.setVisible(false);\r
976         if (bTarget != null) {\r
977             bTarget.dispose();\r
978         }\r
979     }\r
980 }\r