Fixed a spelling error and changed the Package Headers Tab title to be Package Header...
[mirror_edk2.git] / Tools / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / packaging / ui / SpdPackageHeaders.java
1 /** @file
2 Java class SpdPackageHeaders is GUI for create library definition elements of spd file.
3
4 Copyright (c) 2006, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 **/
13 package org.tianocore.frameworkwizard.packaging.ui;
14
15 import java.awt.Dimension;
16 import java.awt.event.ActionEvent;
17 import java.awt.event.ComponentEvent;
18 import java.io.File;
19
20 import javax.swing.DefaultCellEditor;
21 import javax.swing.JFileChooser;
22 import javax.swing.JOptionPane;
23 import javax.swing.JPanel;
24 import javax.swing.JRadioButton;
25 import javax.swing.JTable;
26 import javax.swing.JTextField;
27 import javax.swing.JComboBox;
28 import javax.swing.JLabel;
29 import javax.swing.JScrollPane;
30 import javax.swing.JButton;
31 import javax.swing.JFrame;
32 import javax.swing.ListSelectionModel;
33 import javax.swing.event.InternalFrameAdapter;
34 import javax.swing.event.InternalFrameEvent;
35 import javax.swing.event.ListSelectionEvent;
36 import javax.swing.event.ListSelectionListener;
37 import javax.swing.event.TableModelEvent;
38 import javax.swing.event.TableModelListener;
39 import javax.swing.table.DefaultTableModel;
40 import javax.swing.table.TableColumn;
41 import javax.swing.table.TableModel;
42
43 import org.tianocore.PackageSurfaceAreaDocument;
44 import org.tianocore.frameworkwizard.common.DataValidation;
45 import org.tianocore.frameworkwizard.common.Tools;
46 import org.tianocore.frameworkwizard.common.Identifications.OpeningPackageType;
47 import org.tianocore.frameworkwizard.common.ui.IInternalFrame;
48 import org.tianocore.frameworkwizard.common.ui.StarLabel;
49
50 /**
51 GUI for create library definition elements of spd file.
52
53 @since PackageEditor 1.0
54 **/
55 public class SpdPackageHeaders extends IInternalFrame implements TableModelListener{
56 /**
57 *
58 */
59 private static final long serialVersionUID = 1L;
60
61 static JFrame frame;
62
63 private SpdFileContents sfc = null;
64
65 private OpeningPackageType docConsole = null;
66
67 private DefaultTableModel model = null;
68
69 private JPanel jContentPane = null;
70
71 private JRadioButton jRadioButtonSelect = null;
72
73 private JComboBox jComboBoxSelect = null;
74
75 private JButton jButtonAdd = null;
76
77 private JButton jButtonRemove = null;
78
79 private JButton jButtonClearAll = null;
80
81 private JButton jButtonCancel = null;
82
83 private JButton jButtonOk = null;
84
85 private JLabel jLabel = null;
86
87 private JTextField jTextField = null;
88
89 private JButton jButtonBrowse = null;
90
91 private StarLabel jStarLabel1 = null;
92
93 private StarLabel jStarLabel2 = null;
94
95 private JScrollPane jScrollPane = null;
96
97 private JScrollPane jScrollPane1 = null;
98
99 private int selectedRow = -1;
100
101 private JTable jTable = null;
102
103
104
105 /**
106 This method initializes this
107
108 **/
109 private void initialize() {
110
111 this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
112
113 }
114
115 /**
116 This method initializes jRadioButtonSelect
117
118 @return javax.swing.JRadioButton
119 **/
120 private JRadioButton getJRadioButtonSelect() {
121 if (jRadioButtonSelect == null) {
122 jRadioButtonSelect = new JRadioButton();
123 jRadioButtonSelect.setBounds(new java.awt.Rectangle(9,10,198,20));
124 jRadioButtonSelect.setText("Select Existing ModuleType");
125 jRadioButtonSelect.addActionListener(this);
126 jRadioButtonSelect.setSelected(true);
127 }
128 return jRadioButtonSelect;
129 }
130
131 /**
132 This method initializes jComboBoxSelect
133
134 @return javax.swing.JComboBox
135 **/
136 private JComboBox getJComboBoxSelect() {
137 if (jComboBoxSelect == null) {
138 jComboBoxSelect = new JComboBox();
139 jComboBoxSelect.setBounds(new java.awt.Rectangle(220, 10, 260, 20));
140 jComboBoxSelect.setPreferredSize(new java.awt.Dimension(260,20));
141
142 jComboBoxSelect.setEnabled(true);
143 }
144 return jComboBoxSelect;
145 }
146
147 /**
148 This method initializes jTable
149
150 @return javax.swing.JTable
151 *
152 private JTable getJTable() {
153 if (jTable == null) {
154 model = new PackageHeaderTableModel();
155 model.addColumn("ModuleType");
156 model.addColumn("IncludeHeader");
157
158
159 }
160 return jTable;
161 }*/
162 /**
163 This method initializes jButtonAdd
164
165 @return javax.swing.JButton
166 **/
167 private JButton getJButtonAdd() {
168 if (jButtonAdd == null) {
169 jButtonAdd = new JButton();
170 jButtonAdd.setBounds(new java.awt.Rectangle(189,119,73,20));
171 jButtonAdd.setText("Add");
172 jButtonAdd.addActionListener(this);
173 }
174 return jButtonAdd;
175 }
176
177 /**
178 This method initializes jButtonRemove
179
180 @return javax.swing.JButton
181 **/
182 private JButton getJButtonRemove() {
183 if (jButtonRemove == null) {
184 jButtonRemove = new JButton();
185 jButtonRemove.setBounds(new java.awt.Rectangle(271,119,87,20));
186 jButtonRemove.setText("Remove");
187 jButtonRemove.addActionListener(this);
188 }
189 return jButtonRemove;
190 }
191
192 /**
193 This method initializes jButtonRemoveAll
194
195 @return javax.swing.JButton
196 **/
197 private JButton getJButtonClearAll() {
198 if (jButtonClearAll == null) {
199 jButtonClearAll = new JButton();
200 jButtonClearAll.setBounds(new java.awt.Rectangle(374,119,90,20));
201 jButtonClearAll.setText("Clear All");
202 jButtonClearAll.addActionListener(this);
203 }
204 return jButtonClearAll;
205 }
206
207 /**
208 This method initializes jButtonCancel
209
210 @return javax.swing.JButton
211 **/
212 private JButton getJButtonCancel() {
213 if (jButtonCancel == null) {
214 jButtonCancel = new JButton();
215 jButtonCancel.setPreferredSize(new java.awt.Dimension(90, 20));
216 jButtonCancel.setLocation(new java.awt.Point(390, 305));
217 jButtonCancel.setText("Cancel");
218 jButtonCancel.setSize(new java.awt.Dimension(90, 20));
219 jButtonCancel.setVisible(false);
220 jButtonCancel.addActionListener(this);
221 }
222 return jButtonCancel;
223 }
224
225 /**
226 This method initializes jButton
227
228 @return javax.swing.JButton
229 **/
230 private JButton getJButtonOk() {
231 if (jButtonOk == null) {
232 jButtonOk = new JButton();
233 jButtonOk.setSize(new java.awt.Dimension(90, 20));
234 jButtonOk.setText("OK");
235 jButtonOk.setLocation(new java.awt.Point(290, 305));
236 jButtonOk.setVisible(false);
237 jButtonOk.addActionListener(this);
238 }
239 return jButtonOk;
240 }
241
242 /**
243 This is the default constructor
244 **/
245 public SpdPackageHeaders() {
246 super();
247 initialize();
248 init();
249
250 }
251
252 public SpdPackageHeaders(PackageSurfaceAreaDocument.PackageSurfaceArea inPsa){
253 this();
254 sfc = new SpdFileContents(inPsa);
255 init(sfc);
256 }
257
258 public SpdPackageHeaders(OpeningPackageType opt) {
259 this(opt.getXmlSpd());
260 docConsole = opt;
261 }
262 /**
263 This method initializes this
264
265 @return void
266 **/
267 private void init() {
268
269 this.setContentPane(getJScrollPane());
270 this.setTitle("Package Headers");
271 this.setBounds(new java.awt.Rectangle(0, 0, 500, 370));
272 this.setVisible(true);
273 this.addInternalFrameListener(new InternalFrameAdapter(){
274 public void internalFrameDeactivated(InternalFrameEvent e){
275 if (jTable.isEditing()) {
276 jTable.getCellEditor().stopCellEditing();
277 }
278 }
279 });
280 initFrame();
281 }
282
283 private void init(SpdFileContents sfc){
284 if (sfc.getSpdPackageHeaderCount() == 0) {
285 return ;
286 }
287 String[][] saa = new String[sfc.getSpdPackageHeaderCount()][2];
288 sfc.getSpdPackageHeaders(saa);
289 int i = 0;
290 while (i < saa.length) {
291 model.addRow(saa[i]);
292 i++;
293 }
294 }
295
296 private JScrollPane getJScrollPane(){
297 if (jScrollPane == null){
298 jScrollPane = new JScrollPane();
299 jScrollPane.setViewportView(getJContentPane());
300 }
301 return jScrollPane;
302 }
303 /**
304 This method initializes jContentPane
305
306 @return javax.swing.JPanel
307 **/
308 private JPanel getJContentPane() {
309 if (jContentPane == null) {
310 jStarLabel1 = new StarLabel();
311 jStarLabel1.setLocation(new java.awt.Point(0, 10));
312 jStarLabel2 = new StarLabel();
313 jStarLabel2.setLocation(new java.awt.Point(0,62));
314 jLabel = new JLabel();
315 jLabel.setBounds(new java.awt.Rectangle(14,62,199,22));
316 jLabel.setText("Include Header for Module Type");
317
318 jContentPane = new JPanel();
319 jContentPane.setPreferredSize(new Dimension(480, 325));
320 jContentPane.setLayout(null);
321 jContentPane.add(jLabel, null);
322 jContentPane.add(jStarLabel1, null);
323 jContentPane.add(jStarLabel2, null);
324 jContentPane.add(getJRadioButtonSelect(), null);
325 jContentPane.add(getJComboBoxSelect(), null);
326 jContentPane.add(getJButtonAdd(), null);
327 jContentPane.add(getJButtonRemove(), null);
328 jContentPane.add(getJButtonClearAll(), null);
329 jContentPane.add(getJButtonCancel(), null);
330 jContentPane.add(getJButtonOk(), null);
331
332 jContentPane.add(getJTextField(), null);
333 jContentPane.add(getJButtonBrowse(), null);
334 jContentPane.add(getJScrollPane1(), null);
335 }
336 return jContentPane;
337 }
338
339 /**
340 fill ComboBoxes with pre-defined contents
341 **/
342 private void initFrame() {
343 jComboBoxSelect.addItem("BASE");
344 jComboBoxSelect.addItem("SEC");
345 jComboBoxSelect.addItem("PEI_CORE");
346 jComboBoxSelect.addItem("PEIM");
347 jComboBoxSelect.addItem("DXE_CORE");
348 jComboBoxSelect.addItem("DXE_DRIVER");
349 jComboBoxSelect.addItem("DXE_RUNTIME_DRIVER");
350 jComboBoxSelect.addItem("DXE_SAL_DRIVER");
351 jComboBoxSelect.addItem("DXE_SMM_DRIVER");
352 jComboBoxSelect.addItem("TOOL");
353 jComboBoxSelect.addItem("UEFI_DRIVER");
354 jComboBoxSelect.addItem("UEFI_APPLICATION");
355 jComboBoxSelect.addItem("USER_DEFINED");
356 jComboBoxSelect.setSelectedIndex(0);
357 }
358
359 /* (non-Javadoc)
360 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
361 */
362 public void actionPerformed(ActionEvent arg0) {
363
364 docConsole.setSaved(false);
365 if (arg0.getSource() == jButtonOk) {
366 this.save();
367 this.dispose();
368
369 }
370 if (arg0.getSource() == jButtonCancel) {
371 this.dispose();
372 }
373
374 if (arg0.getSource() == jButtonAdd) {
375 String strLibClass = "";
376
377 if (jRadioButtonSelect.isSelected()) {
378 strLibClass = jComboBoxSelect.getSelectedItem().toString();
379 }
380 //ToDo: check before add
381 String[] row = {"", ""};
382 row[0] = strLibClass;
383 row[1] = jTextField.getText().replace('\\', '/');
384 if (!dataValidation(row)) {
385 return;
386 }
387 model.addRow(row);
388 jTable.changeSelection(model.getRowCount()-1, 0, false, false);
389 sfc.genSpdModuleHeaders(row[0], row[1], null, null, null, null, null, null);
390 }
391 //
392 // remove selected line
393 //
394 if (arg0.getSource() == jButtonRemove) {
395 if (jTable.isEditing()){
396 jTable.getCellEditor().stopCellEditing();
397 }
398 int rowSelected = selectedRow;
399 if (rowSelected >= 0) {
400 model.removeRow(rowSelected);
401 sfc.removeSpdPkgHeader(rowSelected);
402 }
403 }
404
405 if (arg0.getSource() == jButtonClearAll) {
406 if (model.getRowCount() == 0) {
407 return;
408 }
409 model.setRowCount(0);
410 sfc.removeSpdPkgHeader();
411 }
412
413 }
414
415 private boolean dataValidation(String[] row) {
416 if (!DataValidation.isPathAndFilename(row[1])) {
417 JOptionPane.showMessageDialog(this, "Include header path is NOT PathAndFilename type.");
418 return false;
419 }
420
421 return true;
422 }
423
424 /**
425 Add contents in list to sfc
426 **/
427 protected void save() {
428
429 }
430
431 /**
432 This method initializes jTextField
433
434 @return javax.swing.JTextField
435 **/
436 private JTextField getJTextField() {
437 if (jTextField == null) {
438 jTextField = new JTextField();
439 jTextField.setBounds(new java.awt.Rectangle(13,90,346,21));
440 jTextField.setPreferredSize(new java.awt.Dimension(345,20));
441 }
442 return jTextField;
443 }
444
445 /**
446 This method initializes jButtonBrowse
447
448 @return javax.swing.JButton
449 **/
450 private JButton getJButtonBrowse() {
451 if (jButtonBrowse == null) {
452 jButtonBrowse = new JButton();
453 jButtonBrowse.setBounds(new java.awt.Rectangle(374,89,92,21));
454 jButtonBrowse.setText("Browse");
455 jButtonBrowse.setPreferredSize(new java.awt.Dimension(92,20));
456 jButtonBrowse.addActionListener(new javax.swing.AbstractAction() {
457 /**
458 *
459 */
460 private static final long serialVersionUID = 1L;
461
462 public void actionPerformed(ActionEvent e) {
463 //
464 // Select files from current workspace
465 //
466 String dirPrefix = Tools.dirForNewSpd.substring(0, Tools.dirForNewSpd.lastIndexOf(File.separator));
467 JFileChooser chooser = new JFileChooser(dirPrefix);
468 File theFile = null;
469 String headerDest = null;
470
471 chooser.setMultiSelectionEnabled(false);
472 chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
473 int retval = chooser.showOpenDialog(frame);
474 if (retval == JFileChooser.APPROVE_OPTION) {
475
476 theFile = chooser.getSelectedFile();
477 String file = theFile.getPath();
478 if (!file.startsWith(dirPrefix)) {
479 JOptionPane.showMessageDialog(frame, "You can only select files in current package!");
480 return;
481 }
482
483
484 }
485 else {
486 return;
487 }
488
489 headerDest = theFile.getPath();
490 int fileIndex = headerDest.indexOf(System.getProperty("file.separator"), dirPrefix.length());
491
492 jTextField.setText(headerDest.substring(fileIndex + 1).replace('\\', '/'));
493
494 }
495 });
496 }
497 return jButtonBrowse;
498 }
499
500 /**
501 * This method initializes jScrollPane
502 *
503 * @return javax.swing.JScrollPane
504 */
505 private JScrollPane getJScrollPane1() {
506 if (jScrollPane1 == null) {
507 jScrollPane1 = new JScrollPane();
508 jScrollPane1.setBounds(new java.awt.Rectangle(13,149,453,258));
509 jScrollPane1.setViewportView(getJTable());
510 }
511 return jScrollPane1;
512 }
513
514 /**
515 * This method initializes jTable
516 *
517 * @return javax.swing.JTable
518 */
519 private JTable getJTable() {
520 if (jTable == null) {
521 model = new DefaultTableModel();
522 jTable = new JTable(model);
523 jTable.setRowHeight(20);
524 model.addColumn("ModuleType");
525 model.addColumn("IncludeHeader");
526 TableColumn typeColumn = jTable.getColumnModel().getColumn(0);
527 JComboBox jComboBoxSelect = new JComboBox();
528 jComboBoxSelect.addItem("BASE");
529 jComboBoxSelect.addItem("SEC");
530 jComboBoxSelect.addItem("PEI_CORE");
531 jComboBoxSelect.addItem("PEIM");
532 jComboBoxSelect.addItem("DXE_CORE");
533 jComboBoxSelect.addItem("DXE_DRIVER");
534 jComboBoxSelect.addItem("DXE_RUNTIME_DRIVER");
535 jComboBoxSelect.addItem("DXE_SAL_DRIVER");
536 jComboBoxSelect.addItem("DXE_SMM_DRIVER");
537 jComboBoxSelect.addItem("TOOL");
538 jComboBoxSelect.addItem("UEFI_DRIVER");
539 jComboBoxSelect.addItem("UEFI_APPLICATION");
540 jComboBoxSelect.addItem("USER_DEFINED");
541 typeColumn.setCellEditor(new DefaultCellEditor(jComboBoxSelect));
542
543 jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
544 jTable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
545 public void valueChanged(ListSelectionEvent e) {
546 if (e.getValueIsAdjusting()){
547 return;
548 }
549 ListSelectionModel lsm = (ListSelectionModel)e.getSource();
550 if (lsm.isSelectionEmpty()) {
551 return;
552 }
553 else{
554 selectedRow = lsm.getMinSelectionIndex();
555 }
556 }
557 });
558
559 jTable.getModel().addTableModelListener(this);
560 }
561 return jTable;
562 }
563
564 public void tableChanged(TableModelEvent arg0) {
565 // TODO Auto-generated method stub
566 int row = arg0.getFirstRow();
567 TableModel m = (TableModel)arg0.getSource();
568 if (arg0.getType() == TableModelEvent.UPDATE){
569 String pkg = m.getValueAt(row, 0) + "";
570 String hdr = m.getValueAt(row, 1) + "";
571 String[] rowData = {pkg, hdr};
572 if (!dataValidation(rowData)) {
573 return;
574 }
575 docConsole.setSaved(false);
576 sfc.updateSpdPkgHdr(row, pkg, hdr);
577 }
578 }
579
580 public void componentResized(ComponentEvent arg0) {
581 int intPreferredWidth = 500;
582
583 resizeComponentWidth(this.jComboBoxSelect, this.getWidth(), intPreferredWidth);
584 resizeComponentWidth(this.jTextField, this.getWidth(), intPreferredWidth);
585 resizeComponentWidth(this.jScrollPane, this.getWidth(), intPreferredWidth);
586 relocateComponentX(this.jButtonBrowse, this.getWidth(), this.getPreferredSize().width, 30);
587 }
588
589 public static void main(String[] args){
590 new SpdPackageHeaders().setVisible(true);
591 }
592 }
593
594