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