]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/ToolChainConfig.java
8d0055a4033f5c3f2c8358dfad9473d9be42cfb6
[mirror_edk2.git] / Tools / 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.io.File;
19 import java.io.IOException;
20
21 import javax.swing.JButton;
22 import javax.swing.JFileChooser;
23 import javax.swing.JLabel;
24 import javax.swing.JPanel;
25 import javax.swing.JScrollPane;
26 import javax.swing.JTable;
27 import javax.swing.JTextField;
28 import javax.swing.ListSelectionModel;
29 import javax.swing.event.ListSelectionEvent;
30 import javax.swing.event.ListSelectionListener;
31 import javax.swing.event.TableModelEvent;
32 import javax.swing.event.TableModelListener;
33 import javax.swing.table.DefaultTableModel;
34 import javax.swing.table.TableModel;
35
36 import org.tianocore.frameworkwizard.common.DataType;
37 import org.tianocore.frameworkwizard.common.Log;
38 import org.tianocore.frameworkwizard.common.Tools;
39 import org.tianocore.frameworkwizard.common.Identifications.ToolChainConfigId;
40 import org.tianocore.frameworkwizard.common.Identifications.ToolChainConfigVector;
41 import org.tianocore.frameworkwizard.common.ui.IDialog;
42 import org.tianocore.frameworkwizard.common.ui.IFrame;
43 import org.tianocore.frameworkwizard.workspace.Workspace;
44
45 public class ToolChainConfig extends IDialog implements ListSelectionListener, TableModelListener{
46
47 ///
48 /// Define Class Members
49 ///
50 private static final long serialVersionUID = 1486930966278269085L;
51
52 private JPanel jContentPane = null;
53
54 private JScrollPane jScrollPane = null;
55
56 private DefaultTableModel model = null;
57
58 private JTable jTable = null;
59
60 private JButton jButtonOpen = null;
61
62 private JButton jButtonSave = null;
63
64 private JButton jButtonClose = null;
65
66 private String toolsDir = Tools.addFileSeparator(Workspace.getCurrentWorkspace()) + "Tools"
67 + DataType.FILE_SEPARATOR + "Conf";
68
69 private String currentFile = Tools.addFileSeparator(toolsDir) + "tools_def.template";
70
71 private ToolChainConfigVector vtcc = new ToolChainConfigVector();
72
73 private JLabel jLabelName = null;
74
75 private JTextField jTextFieldName = null;
76
77 private JLabel jLabelValue = null;
78
79 private JTextField jTextFieldValue = null;
80
81 private JButton jButtonAdd = null;
82
83 private JButton jButtonRemove = null;
84
85 private int selectedRow = -1;
86
87 private JButton jButtonHelp = null;
88
89 /**
90 This method initializes jScrollPane
91
92 @return javax.swing.JScrollPane
93
94 **/
95 private JScrollPane getJScrollPane() {
96 if (jScrollPane == null) {
97 jScrollPane = new JScrollPane();
98 jScrollPane.setBounds(new java.awt.Rectangle(15, 15, 555, 345));
99 jScrollPane.setViewportView(getJTable());
100 }
101 return jScrollPane;
102 }
103
104 /**
105 This method initializes jTable
106
107 @return javax.swing.JTable
108
109 **/
110 private JTable getJTable() {
111 if (jTable == null) {
112 // Vector<String> vTableHeader = new Vector<String>();
113 // vTableHeader.addElement("Name");
114 // vTableHeader.addElement("Value");
115 //
116 model = new DefaultTableModel();
117 //model = new DefaultTableModel(vTableHeader);
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 a 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 to a 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 /**
265 * This is the default constructor
266 */
267 public ToolChainConfig(IFrame parentFrame, boolean modal) {
268 super(parentFrame, modal);
269 init();
270 }
271
272 /**
273 This method initializes this
274
275 **/
276 private void init() {
277 this.setSize(600, 480);
278 this.setContentPane(getJContentPane());
279 this.setTitle("Tool Chain Configuration");
280 this.centerWindow();
281
282 //
283 // Read default file
284 //
285 File f = new File(currentFile);
286 if (f.exists()) {
287 try {
288 vtcc.removeAll();
289 vtcc.parseFile(this.currentFile);
290 this.setTitle("Tool Chain Configuration" + " [" + currentFile + "]");
291 } catch (IOException e) {
292 Log.log(this.currentFile + "Read Error", e.getMessage());
293 e.printStackTrace();
294 }
295 } else {
296 Log.log("Open file", this.currentFile + " Not Found");
297 }
298
299 showTable();
300 }
301
302 /**
303 This method initializes jContentPane
304
305 @return javax.swing.JPanel
306
307 **/
308 private JPanel getJContentPane() {
309 if (jContentPane == null) {
310 jLabelValue = new JLabel();
311 jLabelValue.setBounds(new java.awt.Rectangle(205, 365, 40, 20));
312 jLabelValue.setText("Value");
313 jLabelName = new JLabel();
314 jLabelName.setBounds(new java.awt.Rectangle(15, 365, 40, 20));
315 jLabelName.setText("Name");
316 jContentPane = new JPanel();
317 jContentPane.setLayout(null);
318 jContentPane.add(getJScrollPane(), null);
319 jContentPane.add(getJButtonOpen(), null);
320 jContentPane.add(getJButtonSave(), null);
321 jContentPane.add(getJButtonClose(), null);
322 jContentPane.add(jLabelName, null);
323 jContentPane.add(getJTextFieldName(), null);
324 jContentPane.add(jLabelValue, null);
325 jContentPane.add(getJTextFieldValue(), null);
326 jContentPane.add(getJButtonAdd(), null);
327 jContentPane.add(getJButtonRemove(), null);
328 jContentPane.add(getJButtonHelp(), null);
329 }
330 return jContentPane;
331 }
332
333 /* (non-Javadoc)
334 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
335 *
336 * Override actionPerformed to listen all actions
337 */
338 public void actionPerformed(ActionEvent arg0) {
339 if (arg0.getSource() == jButtonClose) {
340 this.setVisible(false);
341 this.returnType = DataType.RETURN_TYPE_CANCEL;
342 }
343
344 if (arg0.getSource() == jButtonOpen) {
345 JFileChooser fc = new JFileChooser();
346 fc.setAcceptAllFileFilterUsed(true);
347 fc.setCurrentDirectory(new File(toolsDir));
348
349 int result = fc.showOpenDialog(new JPanel());
350 if (result == JFileChooser.APPROVE_OPTION) {
351 try {
352 vtcc.removeAll();
353 vtcc.parseFile(fc.getSelectedFile().getPath());
354 currentFile = fc.getSelectedFile().getPath();
355 this.setTitle("Tool Chain Configuration" + " [" + currentFile + "]");
356 } catch (IOException e) {
357 Log.err(this.currentFile + "Read Error", e.getMessage());
358 e.printStackTrace();
359 return;
360 }
361 this.showTable();
362 }
363 }
364
365 if (arg0.getSource() == jButtonSave) {
366 JFileChooser fc = new JFileChooser();
367 fc.setAcceptAllFileFilterUsed(true);
368 fc.setSelectedFile(new File(currentFile));
369
370 int result = fc.showSaveDialog(new JPanel());
371 if (result == JFileChooser.APPROVE_OPTION) {
372 currentFile = fc.getSelectedFile().getPath();
373 try {
374 vtcc.saveFile(currentFile);
375 } catch (IOException e) {
376 Log.err(this.currentFile + "Write Error", e.getMessage());
377 e.printStackTrace();
378 return;
379 }
380 }
381 }
382
383 if (arg0.getSource() == jButtonAdd) {
384 if (check()) {
385 String[] row = { jTextFieldName.getText(), jTextFieldValue.getText() };
386 this.vtcc.addToolChainConfigs(new ToolChainConfigId(row[0], row[1]));
387 this.model.addRow(row);
388 }
389 }
390
391 if (arg0.getSource() == jButtonRemove) {
392 if (jTable.isEditing()) {
393 jTable.getCellEditor().stopCellEditing();
394 }
395 if (selectedRow > -1) {
396 this.model.removeRow(selectedRow);
397 this.vtcc.removeToolChainConfigs(selectedRow);
398 selectedRow = -1;
399 }
400 }
401
402 if (arg0.getSource() == jButtonHelp) {
403 ToolChainConfigHelp tcch = new ToolChainConfigHelp();
404 tcch.setVisible(true);
405 }
406 }
407
408 /**
409 Read content of vector and put then into table
410
411 **/
412 private void showTable() {
413 clearAll();
414
415 if (vtcc.size() > 0) {
416 for (int index = 0; index < vtcc.size(); index++) {
417 model.addRow(vtcc.toStringVector(index));
418 }
419 }
420 this.jTable.repaint();
421 this.jTable.updateUI();
422 //this.jScrollPane.setViewportView(this.jTable);
423 }
424
425 /**
426 Clear all table rows
427
428 **/
429 private void clearAll() {
430 if (model != null) {
431 for (int index = model.getRowCount() - 1; index >= 0; index--) {
432 model.removeRow(index);
433 }
434 }
435 }
436
437 /**
438 Check if name or value is empty
439
440 @return
441
442 **/
443 private boolean check() {
444 if (isEmpty(this.jTextFieldName.getText())) {
445 Log.err("Add Tool Chain", "Name couldn't be empty!");
446 return false;
447 }
448
449 if (isEmpty(this.jTextFieldValue.getText())) {
450 Log.err("Add Tool Chain", "Value couldn't be empty");
451 return false;
452 }
453 return true;
454 }
455
456 /* (non-Javadoc)
457 * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
458 *
459 */
460 public void valueChanged(ListSelectionEvent arg0) {
461 if (arg0.getValueIsAdjusting()) {
462 return;
463 }
464 ListSelectionModel lsm = (ListSelectionModel) arg0.getSource();
465 if (lsm.isSelectionEmpty()) {
466 return;
467 } else {
468 selectedRow = lsm.getMinSelectionIndex();
469 }
470 }
471
472 public void tableChanged(TableModelEvent arg0) {
473 int row = arg0.getFirstRow();
474 TableModel m = (TableModel)arg0.getSource();
475 if (arg0.getType() == TableModelEvent.UPDATE){
476 this.vtcc.getToolChainConfigs(row).setName(m.getValueAt(row, 0).toString());
477 this.vtcc.getToolChainConfigs(row).setValue(m.getValueAt(row, 1).toString());
478 }
479 }
480 }