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