1. Fix EDKT451 It should append ".txt" to filename
[mirror_edk2.git] / Tools / Java / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / ToolChainConfig.java
CommitLineData
79cb6fdb 1/** @file\r
2 \r
3 The file is used to setup tool chain configuration\r
4 \r
5 Copyright (c) 2006, Intel Corporation\r
6 All rights reserved. This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10 \r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13 \r
14 **/\r
15package org.tianocore.frameworkwizard;\r
16\r
17import java.awt.event.ActionEvent;\r
8f4f211c 18import java.awt.event.WindowEvent;\r
79cb6fdb 19import java.io.File;\r
20import java.io.IOException;\r
21\r
22import javax.swing.JButton;\r
23import javax.swing.JFileChooser;\r
8f9acbd7 24import javax.swing.JLabel;\r
79cb6fdb 25import javax.swing.JPanel;\r
26import javax.swing.JScrollPane;\r
27import javax.swing.JTable;\r
8f9acbd7 28import javax.swing.JTextField;\r
29import javax.swing.ListSelectionModel;\r
30import javax.swing.event.ListSelectionEvent;\r
31import javax.swing.event.ListSelectionListener;\r
32import javax.swing.event.TableModelEvent;\r
33import javax.swing.event.TableModelListener;\r
79cb6fdb 34import javax.swing.table.DefaultTableModel;\r
8f9acbd7 35import javax.swing.table.TableModel;\r
79cb6fdb 36\r
37import org.tianocore.frameworkwizard.common.DataType;\r
55a2762d 38import org.tianocore.frameworkwizard.common.IFileFilter;\r
79cb6fdb 39import org.tianocore.frameworkwizard.common.Log;\r
40import org.tianocore.frameworkwizard.common.Tools;\r
8f9acbd7 41import org.tianocore.frameworkwizard.common.Identifications.ToolChainConfigId;\r
79cb6fdb 42import org.tianocore.frameworkwizard.common.Identifications.ToolChainConfigVector;\r
79cb6fdb 43import org.tianocore.frameworkwizard.common.ui.IFrame;\r
44import org.tianocore.frameworkwizard.workspace.Workspace;\r
45\r
8f4f211c 46public class ToolChainConfig extends IFrame implements ListSelectionListener, TableModelListener{\r
79cb6fdb 47\r
48 ///\r
49 /// Define Class Members\r
50 ///\r
51 private static final long serialVersionUID = 1486930966278269085L;\r
52\r
53 private JPanel jContentPane = null;\r
54\r
55 private JScrollPane jScrollPane = null;\r
56\r
57 private DefaultTableModel model = null;\r
58\r
59 private JTable jTable = null;\r
60\r
61 private JButton jButtonOpen = null;\r
62\r
63 private JButton jButtonSave = null;\r
64\r
65 private JButton jButtonClose = null;\r
66\r
67 private String toolsDir = Tools.addFileSeparator(Workspace.getCurrentWorkspace()) + "Tools"\r
68 + DataType.FILE_SEPARATOR + "Conf";\r
69\r
70 private String currentFile = Tools.addFileSeparator(toolsDir) + "tools_def.template";\r
8f9acbd7 71 \r
79cb6fdb 72 private ToolChainConfigVector vtcc = new ToolChainConfigVector();\r
73\r
8f9acbd7 74 private JLabel jLabelName = null;\r
75\r
76 private JTextField jTextFieldName = null;\r
77\r
78 private JLabel jLabelValue = null;\r
79\r
80 private JTextField jTextFieldValue = null;\r
81\r
82 private JButton jButtonAdd = null;\r
83\r
84 private JButton jButtonRemove = null;\r
85\r
86 private int selectedRow = -1;\r
87\r
88 private JButton jButtonHelp = null;\r
89 \r
8f4f211c 90 private static ToolChainConfig tcc = null;\r
91 \r
92 private ToolChainConfigHelp tcch = null;\r
93 \r
79cb6fdb 94 /**\r
8f9acbd7 95 This method initializes jScrollPane \r
96 \r
97 @return javax.swing.JScrollPane \r
98 \r
99 **/\r
79cb6fdb 100 private JScrollPane getJScrollPane() {\r
101 if (jScrollPane == null) {\r
102 jScrollPane = new JScrollPane();\r
103 jScrollPane.setBounds(new java.awt.Rectangle(15, 15, 555, 345));\r
104 jScrollPane.setViewportView(getJTable());\r
105 }\r
106 return jScrollPane;\r
107 }\r
108\r
109 /**\r
8f9acbd7 110 This method initializes jTable \r
111 \r
112 @return javax.swing.JTable \r
113 \r
114 **/\r
79cb6fdb 115 private JTable getJTable() {\r
116 if (jTable == null) {\r
8f9acbd7 117 model = new DefaultTableModel();\r
8f9acbd7 118 jTable = new JTable(model);\r
119 jTable.setRowHeight(20);\r
120\r
121 model.addColumn("Property");\r
122 model.addColumn("Value");\r
123\r
124 jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);\r
125 jTable.getSelectionModel().addListSelectionListener(this);\r
126 jTable.getModel().addTableModelListener(this);\r
79cb6fdb 127 }\r
128 return jTable;\r
129 }\r
130\r
131 /**\r
8f9acbd7 132 This method initializes jButtonOpen \r
133 \r
134 @return javax.swing.JButton \r
135 \r
136 **/\r
79cb6fdb 137 private JButton getJButtonOpen() {\r
138 if (jButtonOpen == null) {\r
139 jButtonOpen = new JButton();\r
8f9acbd7 140 jButtonOpen.setBounds(new java.awt.Rectangle(40, 405, 120, 20));\r
ea7f8a93 141 jButtonOpen.setText("Open File");\r
79cb6fdb 142 jButtonOpen.addActionListener(this);\r
143 }\r
144 return jButtonOpen;\r
145 }\r
146\r
147 /**\r
8f9acbd7 148 This method initializes jButtonSave \r
149 \r
150 @return javax.swing.JButton \r
151 \r
152 **/\r
79cb6fdb 153 private JButton getJButtonSave() {\r
154 if (jButtonSave == null) {\r
155 jButtonSave = new JButton();\r
8f9acbd7 156 jButtonSave.setBounds(new java.awt.Rectangle(170, 405, 120, 20));\r
ea7f8a93 157 jButtonSave.setText("Save File");\r
79cb6fdb 158 jButtonSave.addActionListener(this);\r
159 }\r
160 return jButtonSave;\r
161 }\r
162\r
163 /**\r
8f9acbd7 164 This method initializes jButtonClose \r
165 \r
166 @return javax.swing.JButton \r
167 \r
168 **/\r
79cb6fdb 169 private JButton getJButtonClose() {\r
170 if (jButtonClose == null) {\r
171 jButtonClose = new JButton();\r
8f9acbd7 172 jButtonClose.setBounds(new java.awt.Rectangle(490, 405, 80, 20));\r
79cb6fdb 173 jButtonClose.setText("Close");\r
174 jButtonClose.addActionListener(this);\r
175 }\r
176 return jButtonClose;\r
177 }\r
178\r
8f9acbd7 179 /**\r
180 This method initializes jTextFieldName \r
181 \r
182 @return javax.swing.JTextField \r
183 \r
184 **/\r
185 private JTextField getJTextFieldName() {\r
186 if (jTextFieldName == null) {\r
187 jTextFieldName = new JTextField();\r
188 jTextFieldName.setBounds(new java.awt.Rectangle(60, 365, 140, 20));\r
189 }\r
190 return jTextFieldName;\r
191 }\r
192\r
193 /**\r
194 This method initializes jTextFieldValue \r
195 \r
196 @return javax.swing.JTextField \r
197 \r
198 **/\r
199 private JTextField getJTextFieldValue() {\r
200 if (jTextFieldValue == null) {\r
201 jTextFieldValue = new JTextField();\r
202 jTextFieldValue.setBounds(new java.awt.Rectangle(250, 365, 140, 20));\r
203 }\r
204 return jTextFieldValue;\r
205 }\r
206\r
207 /**\r
208 This method initializes jButtonAdd \r
209 \r
210 @return javax.swing.JButton \r
211 \r
212 **/\r
213 private JButton getJButtonAdd() {\r
214 if (jButtonAdd == null) {\r
215 jButtonAdd = new JButton();\r
216 jButtonAdd.setBounds(new java.awt.Rectangle(400, 365, 80, 20));\r
217 jButtonAdd.setText("Add");\r
218 jButtonAdd.addActionListener(this);\r
219 }\r
220 return jButtonAdd;\r
221 }\r
222\r
223 /**\r
224 This method initializes jButtonRemove \r
225 \r
226 @return javax.swing.JButton \r
227 \r
228 **/\r
229 private JButton getJButtonRemove() {\r
230 if (jButtonRemove == null) {\r
231 jButtonRemove = new JButton();\r
232 jButtonRemove.setBounds(new java.awt.Rectangle(490, 365, 80, 20));\r
233 jButtonRemove.setText("Remove");\r
234 jButtonRemove.addActionListener(this);\r
235 }\r
236 return jButtonRemove;\r
237 }\r
238\r
239 /**\r
240 * This method initializes jButtonHelp \r
241 * \r
242 * @return javax.swing.JButton \r
243 */\r
244 private JButton getJButtonHelp() {\r
245 if (jButtonHelp == null) {\r
246 jButtonHelp = new JButton();\r
247 jButtonHelp.setBounds(new java.awt.Rectangle(300,405,120,20));\r
248 jButtonHelp.setText("Help");\r
249 jButtonHelp.addActionListener(this);\r
250 }\r
251 return jButtonHelp;\r
252 }\r
253\r
79cb6fdb 254 /**\r
255 \r
256 @param args\r
257 \r
258 **/\r
259 public static void main(String[] args) {\r
260 // TODO Auto-generated method stub\r
261\r
262 }\r
8f4f211c 263 \r
264 public static ToolChainConfig getInstance() {\r
265 if (tcc == null) {\r
266 tcc = new ToolChainConfig();\r
267 }\r
268 return tcc;\r
269 }\r
79cb6fdb 270\r
271 /**\r
272 * This is the default constructor\r
273 */\r
8f4f211c 274 public ToolChainConfig() {\r
275 super();\r
79cb6fdb 276 init();\r
277 }\r
278\r
279 /**\r
8f9acbd7 280 This method initializes this\r
281 \r
282 **/\r
79cb6fdb 283 private void init() {\r
284 this.setSize(600, 480);\r
285 this.setContentPane(getJContentPane());\r
286 this.setTitle("Tool Chain Configuration");\r
287 this.centerWindow();\r
288\r
289 //\r
290 // Read default file\r
291 //\r
292 File f = new File(currentFile);\r
293 if (f.exists()) {\r
294 try {\r
295 vtcc.removeAll();\r
296 vtcc.parseFile(this.currentFile);\r
297 this.setTitle("Tool Chain Configuration" + " [" + currentFile + "]");\r
298 } catch (IOException e) {\r
299 Log.log(this.currentFile + "Read Error", e.getMessage());\r
300 e.printStackTrace();\r
301 }\r
302 } else {\r
ea7f8a93 303 Log.log("File Open Error: ", this.currentFile + " File Not Found");\r
79cb6fdb 304 }\r
305\r
306 showTable();\r
307 }\r
308\r
309 /**\r
8f9acbd7 310 This method initializes jContentPane\r
311 \r
312 @return javax.swing.JPanel\r
313 \r
314 **/\r
79cb6fdb 315 private JPanel getJContentPane() {\r
316 if (jContentPane == null) {\r
8f9acbd7 317 jLabelValue = new JLabel();\r
318 jLabelValue.setBounds(new java.awt.Rectangle(205, 365, 40, 20));\r
319 jLabelValue.setText("Value");\r
320 jLabelName = new JLabel();\r
321 jLabelName.setBounds(new java.awt.Rectangle(15, 365, 40, 20));\r
322 jLabelName.setText("Name");\r
79cb6fdb 323 jContentPane = new JPanel();\r
324 jContentPane.setLayout(null);\r
325 jContentPane.add(getJScrollPane(), null);\r
326 jContentPane.add(getJButtonOpen(), null);\r
327 jContentPane.add(getJButtonSave(), null);\r
328 jContentPane.add(getJButtonClose(), null);\r
8f9acbd7 329 jContentPane.add(jLabelName, null);\r
330 jContentPane.add(getJTextFieldName(), null);\r
331 jContentPane.add(jLabelValue, null);\r
332 jContentPane.add(getJTextFieldValue(), null);\r
333 jContentPane.add(getJButtonAdd(), null);\r
334 jContentPane.add(getJButtonRemove(), null);\r
335 jContentPane.add(getJButtonHelp(), null);\r
79cb6fdb 336 }\r
337 return jContentPane;\r
338 }\r
339\r
340 /* (non-Javadoc)\r
341 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)\r
342 * \r
343 * Override actionPerformed to listen all actions\r
344 */\r
345 public void actionPerformed(ActionEvent arg0) {\r
346 if (arg0.getSource() == jButtonClose) {\r
8f4f211c 347 this.exit();\r
79cb6fdb 348 }\r
349\r
350 if (arg0.getSource() == jButtonOpen) {\r
351 JFileChooser fc = new JFileChooser();\r
55a2762d 352 fc.setAcceptAllFileFilterUsed(false);\r
353 IFileFilter iff = new IFileFilter(DataType.TEXT_FILE_EXT);\r
354 fc.addChoosableFileFilter(iff);\r
79cb6fdb 355 fc.setCurrentDirectory(new File(toolsDir));\r
356\r
8f9acbd7 357 int result = fc.showOpenDialog(new JPanel());\r
79cb6fdb 358 if (result == JFileChooser.APPROVE_OPTION) {\r
359 try {\r
360 vtcc.removeAll();\r
361 vtcc.parseFile(fc.getSelectedFile().getPath());\r
8f9acbd7 362 currentFile = fc.getSelectedFile().getPath();\r
79cb6fdb 363 this.setTitle("Tool Chain Configuration" + " [" + currentFile + "]");\r
364 } catch (IOException e) {\r
ed1665f2 365 Log.wrn(this.currentFile + "Read Error", e.getMessage());\r
79cb6fdb 366 Log.err(this.currentFile + "Read Error", e.getMessage());\r
79cb6fdb 367 return;\r
368 }\r
369 this.showTable();\r
370 }\r
371 }\r
372\r
373 if (arg0.getSource() == jButtonSave) {\r
374 JFileChooser fc = new JFileChooser();\r
55a2762d 375 fc.setAcceptAllFileFilterUsed(false);\r
376 IFileFilter iff = new IFileFilter(DataType.TEXT_FILE_EXT);\r
377 fc.addChoosableFileFilter(iff);\r
378 fc.setCurrentDirectory(new File(toolsDir));\r
79cb6fdb 379\r
8f9acbd7 380 int result = fc.showSaveDialog(new JPanel());\r
79cb6fdb 381 if (result == JFileChooser.APPROVE_OPTION) {\r
8f9acbd7 382 currentFile = fc.getSelectedFile().getPath();\r
ba73bc0b 383 currentFile = Tools.addPathExt(currentFile, DataType.RETURN_TYPE_TEXT);\r
8f9acbd7 384 try {\r
385 vtcc.saveFile(currentFile);\r
386 } catch (IOException e) {\r
ed1665f2 387 Log.wrn(this.currentFile + "Write Error", e.getMessage());\r
8f9acbd7 388 Log.err(this.currentFile + "Write Error", e.getMessage());\r
8f9acbd7 389 return;\r
390 }\r
391 }\r
392 }\r
393\r
394 if (arg0.getSource() == jButtonAdd) {\r
395 if (check()) {\r
396 String[] row = { jTextFieldName.getText(), jTextFieldValue.getText() };\r
397 this.vtcc.addToolChainConfigs(new ToolChainConfigId(row[0], row[1]));\r
398 this.model.addRow(row);\r
79cb6fdb 399 }\r
400 }\r
8f9acbd7 401\r
402 if (arg0.getSource() == jButtonRemove) {\r
403 if (jTable.isEditing()) {\r
404 jTable.getCellEditor().stopCellEditing();\r
405 }\r
406 if (selectedRow > -1) {\r
407 this.model.removeRow(selectedRow);\r
408 this.vtcc.removeToolChainConfigs(selectedRow);\r
409 selectedRow = -1;\r
410 }\r
411 }\r
412 \r
413 if (arg0.getSource() == jButtonHelp) {\r
8f4f211c 414 tcch = ToolChainConfigHelp.getInstance();\r
8f9acbd7 415 tcch.setVisible(true);\r
416 }\r
79cb6fdb 417 }\r
418\r
419 /**\r
420 Read content of vector and put then into table\r
421 \r
422 **/\r
423 private void showTable() {\r
8f9acbd7 424 clearAll();\r
425 \r
79cb6fdb 426 if (vtcc.size() > 0) {\r
427 for (int index = 0; index < vtcc.size(); index++) {\r
428 model.addRow(vtcc.toStringVector(index));\r
429 }\r
430 }\r
8f9acbd7 431 this.jTable.repaint();\r
432 this.jTable.updateUI();\r
433 //this.jScrollPane.setViewportView(this.jTable);\r
434 }\r
435\r
436 /**\r
437 Clear all table rows\r
438 \r
439 **/\r
440 private void clearAll() {\r
441 if (model != null) {\r
442 for (int index = model.getRowCount() - 1; index >= 0; index--) {\r
443 model.removeRow(index);\r
444 }\r
445 }\r
446 }\r
447\r
448 /**\r
449 Check if name or value is empty\r
450 \r
451 @return\r
452 \r
453 **/\r
454 private boolean check() {\r
455 if (isEmpty(this.jTextFieldName.getText())) {\r
ea7f8a93 456 Log.wrn("Add Tool Chain", "The Property Name must be entered!");\r
8f9acbd7 457 return false;\r
458 }\r
459\r
460 if (isEmpty(this.jTextFieldValue.getText())) {\r
ea7f8a93 461 Log.wrn("Add Tool Chain", "The Property Value must be entered!");\r
8f9acbd7 462 return false;\r
463 }\r
464 return true;\r
465 }\r
466\r
467 /* (non-Javadoc)\r
468 * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)\r
469 *\r
470 */\r
471 public void valueChanged(ListSelectionEvent arg0) {\r
472 if (arg0.getValueIsAdjusting()) {\r
473 return;\r
474 }\r
475 ListSelectionModel lsm = (ListSelectionModel) arg0.getSource();\r
476 if (lsm.isSelectionEmpty()) {\r
477 return;\r
478 } else {\r
479 selectedRow = lsm.getMinSelectionIndex();\r
480 }\r
481 }\r
482\r
483 public void tableChanged(TableModelEvent arg0) {\r
484 int row = arg0.getFirstRow();\r
485 TableModel m = (TableModel)arg0.getSource();\r
486 if (arg0.getType() == TableModelEvent.UPDATE){\r
487 this.vtcc.getToolChainConfigs(row).setName(m.getValueAt(row, 0).toString());\r
488 this.vtcc.getToolChainConfigs(row).setValue(m.getValueAt(row, 1).toString());\r
489 }\r
79cb6fdb 490 }\r
8f4f211c 491 \r
492 /* (non-Javadoc)\r
493 * @see java.awt.event.WindowListener#windowClosing(java.awt.event.WindowEvent)\r
494 *\r
495 * Override windowClosing to popup warning message to confirm quit\r
496 * \r
497 */\r
498 public void windowClosing(WindowEvent arg0) {\r
499 this.exit();\r
500 }\r
501 \r
502 private void exit() {\r
503 this.setVisible(false);\r
504 if (tcch != null) {\r
505 tcch.dispose();\r
506 }\r
507 }\r
79cb6fdb 508}\r