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