Fixed alignment for labels and fields
[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("Remove");
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 initFrame();
252 }
253
254 private void init(SpdFileContents sfc){
255 if (sfc.getSpdPackageHeaderCount() == 0) {
256 return ;
257 }
258 String[][] saa = new String[sfc.getSpdPackageHeaderCount()][2];
259 sfc.getSpdPackageHeaders(saa);
260 int i = 0;
261 while (i < saa.length) {
262 model.addRow(saa[i]);
263 i++;
264 }
265 }
266
267 private JScrollPane getJScrollPane(){
268 if (jScrollPane == null){
269 jScrollPane = new JScrollPane();
270 jScrollPane.setViewportView(getJContentPane());
271 }
272 return jScrollPane;
273 }
274 /**
275 This method initializes jContentPane
276
277 @return javax.swing.JPanel
278 **/
279 private JPanel getJContentPane() {
280 if (jContentPane == null) {
281 jStarLabel1 = new StarLabel();
282 jStarLabel1.setLocation(new java.awt.Point(0, rowOne));
283 jStarLabel2 = new StarLabel();
284 jStarLabel2.setLocation(new java.awt.Point(0,rowTwo));
285 jLabel = new JLabel();
286 jLabel.setBounds(new java.awt.Rectangle(labelCol,rowTwo,320,22));
287 jLabel.setText("Include Header for Module Type");
288
289 jContentPane = new JPanel();
290 jContentPane.setPreferredSize(new Dimension(480, 325));
291 jContentPane.setLayout(null);
292 jContentPane.add(jLabel, null);
293 jContentPane.add(jStarLabel1, null);
294 jContentPane.add(jStarLabel2, null);
295 jContentPane.add(getJLabelSelect(), null);
296 jContentPane.add(getJComboBoxSelect(), null);
297 jContentPane.add(getJButtonAdd(), null);
298 jContentPane.add(getJButtonRemove(), null);
299 jContentPane.add(getJButtonClearAll(), null);
300
301 jContentPane.add(getJTextField(), null);
302 jContentPane.add(getJButtonBrowse(), null);
303 jContentPane.add(getJScrollPanePkgHdr(), null);
304 }
305 return jContentPane;
306 }
307
308 /**
309 fill ComboBoxes with pre-defined contents
310 **/
311 private void initFrame() {
312 jComboBoxSelect.addItem("BASE");
313 jComboBoxSelect.addItem("SEC");
314 jComboBoxSelect.addItem("PEI_CORE");
315 jComboBoxSelect.addItem("PEIM");
316 jComboBoxSelect.addItem("DXE_CORE");
317 jComboBoxSelect.addItem("DXE_DRIVER");
318 jComboBoxSelect.addItem("DXE_RUNTIME_DRIVER");
319 jComboBoxSelect.addItem("DXE_SAL_DRIVER");
320 jComboBoxSelect.addItem("DXE_SMM_DRIVER");
321 jComboBoxSelect.addItem("TOOL");
322 jComboBoxSelect.addItem("UEFI_DRIVER");
323 jComboBoxSelect.addItem("UEFI_APPLICATION");
324 jComboBoxSelect.addItem("USER_DEFINED");
325 jComboBoxSelect.setSelectedIndex(0);
326 }
327
328 /* (non-Javadoc)
329 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
330 */
331 public void actionPerformed(ActionEvent arg0) {
332
333 docConsole.setSaved(false);
334
335 if (arg0.getSource() == jButtonAdd) {
336 String strLibClass = "";
337
338 strLibClass = jComboBoxSelect.getSelectedItem().toString();
339 //ToDo: check before add
340 String[] row = {"", ""};
341 row[0] = strLibClass;
342 row[1] = jTextField.getText().replace('\\', '/');
343 if (!dataValidation(row)) {
344 return;
345 }
346 model.addRow(row);
347 jTable.changeSelection(model.getRowCount()-1, 0, false, false);
348 sfc.genSpdModuleHeaders(row[0], row[1], null, null, null, null, null, null);
349 }
350 //
351 // remove selected line
352 //
353 if (arg0.getSource() == jButtonRemove) {
354 if (jTable.isEditing()){
355 jTable.getCellEditor().stopCellEditing();
356 }
357 int rowSelected = selectedRow;
358 if (rowSelected >= 0) {
359 model.removeRow(rowSelected);
360 sfc.removeSpdPkgHeader(rowSelected);
361 }
362 }
363
364 if (arg0.getSource() == jButtonClearAll) {
365 if (model.getRowCount() == 0) {
366 return;
367 }
368 model.setRowCount(0);
369 sfc.removeSpdPkgHeader();
370 }
371
372 }
373
374 private boolean dataValidation(String[] row) {
375 if (!DataValidation.isPathAndFilename(row[1])) {
376 JOptionPane.showMessageDialog(this, "Include header path is NOT PathAndFilename type.");
377 return false;
378 }
379
380 return true;
381 }
382
383 /**
384 Add contents in list to sfc
385 **/
386 protected void save() {
387
388 }
389
390 /**
391 This method initializes jTextField
392
393 @return javax.swing.JTextField
394 **/
395 private JTextField getJTextField() {
396 if (jTextField == null) {
397 jTextField = new JTextField();
398 jTextField.setBounds(new java.awt.Rectangle(labelCol,rowThree,346,20));
399 jTextField.setPreferredSize(new java.awt.Dimension(345,20));
400 }
401 return jTextField;
402 }
403
404 /**
405 This method initializes jButtonBrowse
406
407 @return javax.swing.JButton
408 **/
409 private JButton getJButtonBrowse() {
410 if (jButtonBrowse == null) {
411 jButtonBrowse = new JButton();
412 jButtonBrowse.setBounds(new java.awt.Rectangle(374,rowThree,buttonWidth,20));
413 jButtonBrowse.setText("Browse");
414 jButtonBrowse.setPreferredSize(new java.awt.Dimension(buttonWidth,20));
415 jButtonBrowse.addActionListener(new javax.swing.AbstractAction() {
416 /**
417 *
418 */
419 private static final long serialVersionUID = 1L;
420
421 public void actionPerformed(ActionEvent e) {
422 //
423 // Select files from current workspace
424 //
425 String dirPrefix = Tools.dirForNewSpd.substring(0, Tools.dirForNewSpd.lastIndexOf(File.separator));
426 JFileChooser chooser = new JFileChooser(dirPrefix);
427 File theFile = null;
428 String headerDest = null;
429
430 chooser.setMultiSelectionEnabled(false);
431 chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
432 int retval = chooser.showOpenDialog(frame);
433 if (retval == JFileChooser.APPROVE_OPTION) {
434
435 theFile = chooser.getSelectedFile();
436 String file = theFile.getPath();
437 if (!file.startsWith(dirPrefix)) {
438 JOptionPane.showMessageDialog(frame, "You can only select files in current package!");
439 return;
440 }
441
442
443 }
444 else {
445 return;
446 }
447
448 headerDest = theFile.getPath();
449 int fileIndex = headerDest.indexOf(System.getProperty("file.separator"), dirPrefix.length());
450
451 jTextField.setText(headerDest.substring(fileIndex + 1).replace('\\', '/'));
452
453 }
454 });
455 }
456 return jButtonBrowse;
457 }
458
459 /**
460 * This method initializes jScrollPane
461 *
462 * @return javax.swing.JScrollPane
463 */
464 private JScrollPane getJScrollPanePkgHdr() {
465 if (jScrollPanePkgHdr == null) {
466 jScrollPanePkgHdr = new JScrollPane();
467 jScrollPanePkgHdr.setBounds(new java.awt.Rectangle(labelCol,rowFive,453,258));
468 jScrollPanePkgHdr.setViewportView(getJTable());
469 }
470 return jScrollPanePkgHdr;
471 }
472
473 /**
474 * This method initializes jTable
475 *
476 * @return javax.swing.JTable
477 */
478 private JTable getJTable() {
479 if (jTable == null) {
480 model = new DefaultTableModel();
481 jTable = new JTable(model);
482 jTable.setRowHeight(20);
483 model.addColumn("ModuleType");
484 model.addColumn("IncludeHeader");
485 TableColumn typeColumn = jTable.getColumnModel().getColumn(0);
486 JComboBox jComboBoxSelect = new JComboBox();
487 jComboBoxSelect.addItem("BASE");
488 jComboBoxSelect.addItem("SEC");
489 jComboBoxSelect.addItem("PEI_CORE");
490 jComboBoxSelect.addItem("PEIM");
491 jComboBoxSelect.addItem("DXE_CORE");
492 jComboBoxSelect.addItem("DXE_DRIVER");
493 jComboBoxSelect.addItem("DXE_RUNTIME_DRIVER");
494 jComboBoxSelect.addItem("DXE_SAL_DRIVER");
495 jComboBoxSelect.addItem("DXE_SMM_DRIVER");
496 jComboBoxSelect.addItem("TOOL");
497 jComboBoxSelect.addItem("UEFI_DRIVER");
498 jComboBoxSelect.addItem("UEFI_APPLICATION");
499 jComboBoxSelect.addItem("USER_DEFINED");
500 typeColumn.setCellEditor(new DefaultCellEditor(jComboBoxSelect));
501
502 jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
503 jTable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
504 public void valueChanged(ListSelectionEvent e) {
505 if (e.getValueIsAdjusting()){
506 return;
507 }
508 ListSelectionModel lsm = (ListSelectionModel)e.getSource();
509 if (lsm.isSelectionEmpty()) {
510 return;
511 }
512 else{
513 selectedRow = lsm.getMinSelectionIndex();
514 }
515 }
516 });
517
518 jTable.getModel().addTableModelListener(this);
519 }
520 return jTable;
521 }
522
523 public void tableChanged(TableModelEvent arg0) {
524 // TODO Auto-generated method stub
525 int row = arg0.getFirstRow();
526 int column = arg0.getColumn();
527 TableModel m = (TableModel)arg0.getSource();
528 if (arg0.getType() == TableModelEvent.UPDATE){
529 String[] sa = new String[2];
530 sfc.getSpdModuleHeader(sa, row);
531 Object cellData = m.getValueAt(row, column);
532 if (cellData == null) {
533 cellData = "";
534 }
535 if (cellData.equals(sa[column])) {
536 return;
537 }
538 if (cellData.toString().length() == 0 && sa[column] == null) {
539 return;
540 }
541 String pkg = m.getValueAt(row, 0) + "";
542 String hdr = m.getValueAt(row, 1) + "";
543 String[] rowData = {pkg, hdr};
544 if (!dataValidation(rowData)) {
545 return;
546 }
547 docConsole.setSaved(false);
548 sfc.updateSpdPkgHdr(row, pkg, hdr);
549 }
550 }
551
552 public void componentResized(ComponentEvent arg0) {
553 int intPreferredWidth = 500;
554
555 // Tools.resizeComponentWidth(this.jComboBoxSelect, this.getWidth(), intPreferredWidth);
556 Tools.resizeComponentWidth(this.jTextField, this.getWidth(), intPreferredWidth);
557 Tools.resizeComponentWidth(this.jScrollPanePkgHdr, this.getWidth(), intPreferredWidth);
558 Tools.relocateComponentX(this.jButtonBrowse, this.getWidth(), this.getPreferredSize().width, 30);
559 }
560
561 public static void main(String[] args){
562 new SpdPackageHeaders().setVisible(true);
563 }
564 }
565
566