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