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