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