2 Java class SpdMsaFiles is GUI for create library definition elements of spd file.
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
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.
13 package org
.tianocore
.frameworkwizard
.packaging
.ui
;
15 import java
.awt
.Dimension
;
16 import java
.awt
.event
.ActionEvent
;
17 import java
.awt
.event
.ComponentEvent
;
20 import javax
.swing
.AbstractAction
;
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
.JLabel
;
27 import javax
.swing
.JScrollPane
;
28 import javax
.swing
.JButton
;
29 import javax
.swing
.JFrame
;
30 import javax
.swing
.ListSelectionModel
;
31 import javax
.swing
.event
.InternalFrameAdapter
;
32 import javax
.swing
.event
.InternalFrameEvent
;
33 import javax
.swing
.event
.ListSelectionEvent
;
34 import javax
.swing
.event
.ListSelectionListener
;
35 import javax
.swing
.event
.TableModelEvent
;
36 import javax
.swing
.event
.TableModelListener
;
37 import javax
.swing
.table
.DefaultTableModel
;
38 import javax
.swing
.table
.TableModel
;
40 import org
.tianocore
.PackageSurfaceAreaDocument
;
41 import org
.tianocore
.frameworkwizard
.common
.Tools
;
42 import org
.tianocore
.frameworkwizard
.common
.Identifications
.OpeningPackageType
;
43 import org
.tianocore
.frameworkwizard
.common
.ui
.IInternalFrame
;
44 import org
.tianocore
.frameworkwizard
.common
.ui
.StarLabel
;
47 GUI for create library definition elements of spd file.
49 @since PackageEditor 1.0
51 public class SpdMsaFiles
extends IInternalFrame
implements TableModelListener
{
55 private static final long serialVersionUID
= 1L;
59 private JScrollPane jScrollPane
= null; // @jve:decl-index=0:visual-constraint="10,95"
61 private JTable jTable
= null;
63 private DefaultTableModel model
= null;
65 private JPanel jContentPane
= null;
67 private JScrollPane jScrollPaneMsa
= null;
69 private JButton jButtonAdd
= null;
71 private JButton jButtonRemove
= null;
73 private JButton jButtonClearAll
= null;
75 private JButton jButtonCancel
= null;
77 private JButton jButtonOk
= null;
79 private JLabel jLabel
= null;
81 private JTextField jTextField
= null;
83 private JButton jButtonBrowse
= null;
85 private StarLabel jStarLabel2
= null;
87 private SpdFileContents sfc
= null;
89 private OpeningPackageType docConsole
= null;
91 private int selectedRow
= -1;
94 This method initializes this
97 private void initialize() {
99 this.setDefaultCloseOperation(javax
.swing
.WindowConstants
.DISPOSE_ON_CLOSE
);
104 This method initializes jScrollPane
106 @return javax.swing.JScrollPane
108 private JScrollPane
getJScrollPaneMsa() {
109 if (jScrollPaneMsa
== null) {
110 jScrollPaneMsa
= new JScrollPane();
111 jScrollPaneMsa
.setBounds(new java
.awt
.Rectangle(13,177,461,421));
112 jScrollPaneMsa
.setViewportView(getJTable());
114 return jScrollPaneMsa
;
118 This method initializes jTable
120 @return javax.swing.JTable
122 private JTable
getJTable() {
123 if (jTable
== null) {
124 model
= new DefaultTableModel();
125 jTable
= new JTable(model
);
126 jTable
.setRowHeight(20);
127 model
.addColumn("MsaFile");
129 jTable
.setSelectionMode(ListSelectionModel
.SINGLE_SELECTION
);
130 jTable
.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
131 public void valueChanged(ListSelectionEvent e
) {
132 if (e
.getValueIsAdjusting()){
135 ListSelectionModel lsm
= (ListSelectionModel
)e
.getSource();
136 if (lsm
.isSelectionEmpty()) {
140 selectedRow
= lsm
.getMinSelectionIndex();
145 jTable
.getModel().addTableModelListener(this);
150 This method initializes jButtonAdd
152 @return javax.swing.JButton
154 private JButton
getJButtonAdd() {
155 if (jButtonAdd
== null) {
156 jButtonAdd
= new JButton();
157 jButtonAdd
.setBounds(new java
.awt
.Rectangle(163,148,90,20));
158 jButtonAdd
.setText("Add");
159 jButtonAdd
.addActionListener(this);
165 This method initializes jButtonRemove
167 @return javax.swing.JButton
169 private JButton
getJButtonRemove() {
170 if (jButtonRemove
== null) {
171 jButtonRemove
= new JButton();
172 jButtonRemove
.setBounds(new java
.awt
.Rectangle(266,148,90,20));
173 jButtonRemove
.setText("Remove");
174 jButtonRemove
.addActionListener(this);
176 return jButtonRemove
;
180 This method initializes jButtonRemoveAll
182 @return javax.swing.JButton
184 private JButton
getJButtonClearAll() {
185 if (jButtonClearAll
== null) {
186 jButtonClearAll
= new JButton();
187 jButtonClearAll
.setBounds(new java
.awt
.Rectangle(374,148,90,20));
188 jButtonClearAll
.setText("Clear All");
189 jButtonClearAll
.addActionListener(this);
191 return jButtonClearAll
;
195 This method initializes jButtonCancel
197 @return javax.swing.JButton
199 private JButton
getJButtonCancel() {
200 if (jButtonCancel
== null) {
201 jButtonCancel
= new JButton();
202 jButtonCancel
.setPreferredSize(new java
.awt
.Dimension(90, 20));
203 jButtonCancel
.setLocation(new java
.awt
.Point(390, 305));
204 jButtonCancel
.setText("Cancel");
205 jButtonCancel
.setSize(new java
.awt
.Dimension(90, 20));
206 jButtonCancel
.setVisible(false);
207 jButtonCancel
.addActionListener(this);
209 return jButtonCancel
;
213 This method initializes jButton
215 @return javax.swing.JButton
217 private JButton
getJButtonOk() {
218 if (jButtonOk
== null) {
219 jButtonOk
= new JButton();
220 jButtonOk
.setSize(new java
.awt
.Dimension(90, 20));
221 jButtonOk
.setText("OK");
222 jButtonOk
.setLocation(new java
.awt
.Point(290, 305));
223 jButtonOk
.setVisible(false);
224 jButtonOk
.addActionListener(this);
230 This is the default constructor
232 public SpdMsaFiles() {
239 public SpdMsaFiles(PackageSurfaceAreaDocument
.PackageSurfaceArea inPsa
){
241 sfc
= new SpdFileContents(inPsa
);
245 public SpdMsaFiles(OpeningPackageType opt
){
246 this(opt
.getXmlSpd());
250 This method initializes this
254 private void init() {
255 this.setBounds(new java
.awt
.Rectangle(0, 0, 500, 370));
256 this.setPreferredSize(new Dimension(490, 400));
257 this.setContentPane(getJScrollPane());
258 this.setTitle("Msa Files");
259 this.addInternalFrameListener(new InternalFrameAdapter(){
260 public void internalFrameDeactivated(InternalFrameEvent e
){
261 if (jTable
.isEditing()) {
262 jTable
.getCellEditor().stopCellEditing();
266 this.setVisible(true);
270 private void init(SpdFileContents sfc
){
271 if (sfc
.getSpdMsaFileCount() == 0) {
275 // initialize table using SpdFileContents object
277 String
[][] saa
= new String
[sfc
.getSpdMsaFileCount()][4];
278 sfc
.getSpdMsaFiles(saa
);
280 while (i
< saa
.length
) {
281 model
.addRow(saa
[i
]);
286 private JScrollPane
getJScrollPane(){
287 if (jScrollPane
== null){
288 jScrollPane
= new JScrollPane();
289 jScrollPane
.setViewportView(getJContentPane());
294 This method initializes jContentPane
296 @return javax.swing.JPanel
298 private JPanel
getJContentPane() {
299 if (jContentPane
== null) {
300 jStarLabel2
= new StarLabel();
301 jStarLabel2
.setLocation(new java
.awt
.Point(2,24));
302 jLabel
= new JLabel();
303 jLabel
.setBounds(new java
.awt
.Rectangle(17,24,111,22));
304 jLabel
.setText("Msa File ");
306 jContentPane
= new JPanel();
307 jContentPane
.setPreferredSize(new Dimension(480, 325));
308 jContentPane
.setLayout(null);
309 jContentPane
.add(jLabel
, null);
310 jContentPane
.add(jStarLabel2
, null);
311 jContentPane
.add(getJScrollPaneMsa(), null);
312 jContentPane
.add(getJButtonAdd(), null);
313 jContentPane
.add(getJButtonRemove(), null);
314 jContentPane
.add(getJButtonClearAll(), null);
315 jContentPane
.add(getJButtonCancel(), null);
316 jContentPane
.add(getJButtonOk(), null);
318 jContentPane
.add(getJTextField(), null);
319 jContentPane
.add(getJButtonBrowse(), null);
325 fill ComboBoxes with pre-defined contents
327 private void initFrame() {
332 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
334 public void actionPerformed(ActionEvent arg0
) {
336 if (arg0
.getSource() == jButtonOk
) {
341 if (arg0
.getSource() == jButtonCancel
) {
345 if (arg0
.getSource() == jButtonAdd
) {
346 //ToDo: check before add
347 String
[] row
= {jTextField
.getText().replace('\\', '/')};
348 if (jTextField
.getText().length() == 0) {
349 JOptionPane
.showMessageDialog(this, "Msa File is NOT PathAndFilename type.");
353 String dirPrefix
= Tools
.dirForNewSpd
.substring(0, Tools
.dirForNewSpd
.lastIndexOf(File
.separator
));
354 if (!new File(dirPrefix
+ File
.separator
+ jTextField
.getText()).exists()) {
355 JOptionPane
.showMessageDialog(this, "File NOT Exists in Current Package.");
359 jTable
.changeSelection(model
.getRowCount()-1, 0, false, false);
360 sfc
.genSpdMsaFiles(row
[0], null, null, null);
361 docConsole
.setSaved(false);
364 // remove selected line
366 if (arg0
.getSource() == jButtonRemove
) {
367 if (jTable
.isEditing()){
368 jTable
.getCellEditor().stopCellEditing();
370 int rowSelected
= selectedRow
;
371 if (rowSelected
>= 0) {
372 model
.removeRow(rowSelected
);
373 sfc
.removeSpdMsaFile(rowSelected
);
374 docConsole
.setSaved(false);
378 if (arg0
.getSource() == jButtonClearAll
) {
379 if (model
.getRowCount() == 0) {
382 model
.setRowCount(0);
383 sfc
.removeSpdMsaFile();
384 docConsole
.setSaved(false);
389 public void tableChanged(TableModelEvent arg0
) {
390 // TODO Auto-generated method stub
391 int row
= arg0
.getFirstRow();
392 int column
= arg0
.getColumn();
393 TableModel m
= (TableModel
)arg0
.getSource();
394 if (arg0
.getType() == TableModelEvent
.UPDATE
){
395 String
[] sa
= new String
[1];
396 sfc
.getSpdMsaFile(sa
, row
);
397 Object cellData
= m
.getValueAt(row
, column
);
398 if (cellData
== null) {
401 if (cellData
.equals(sa
[column
])) {
404 if (cellData
.toString().length() == 0 && sa
[column
] == null) {
407 String file
= m
.getValueAt(row
, 0) + "";
408 if (file
.length() == 0) {
409 JOptionPane
.showMessageDialog(this, "Msa File is NOT PathAndFilename type.");
412 docConsole
.setSaved(false);
413 sfc
.updateSpdMsaFile(row
, file
, null, null, null);
418 Add contents in list to sfc
420 protected void save() {
425 This method initializes jTextField
427 @return javax.swing.JTextField
429 private JTextField
getJTextField() {
430 if (jTextField
== null) {
431 jTextField
= new JTextField();
432 jTextField
.setBounds(new java
.awt
.Rectangle(14,51,346,21));
433 jTextField
.setPreferredSize(new java
.awt
.Dimension(345,20));
439 This method initializes jButtonBrowse
441 @return javax.swing.JButton
443 private JButton
getJButtonBrowse() {
444 if (jButtonBrowse
== null) {
445 jButtonBrowse
= new JButton();
446 jButtonBrowse
.setBounds(new java
.awt
.Rectangle(376,50,92,21));
447 jButtonBrowse
.setText("Browse");
448 jButtonBrowse
.setPreferredSize(new java
.awt
.Dimension(90,20));
449 jButtonBrowse
.addActionListener(new AbstractAction() {
453 private static final long serialVersionUID
= 1L;
455 public void actionPerformed(ActionEvent e
) {
457 // Select files from current workspace
459 String dirPrefix
= Tools
.dirForNewSpd
.substring(0, Tools
.dirForNewSpd
.lastIndexOf(File
.separator
));
460 JFileChooser chooser
= new JFileChooser(dirPrefix
);
462 String headerDest
= null;
464 chooser
.setMultiSelectionEnabled(false);
465 chooser
.setFileSelectionMode(JFileChooser
.FILES_AND_DIRECTORIES
);
466 int retval
= chooser
.showOpenDialog(frame
);
467 if (retval
== JFileChooser
.APPROVE_OPTION
) {
469 theFile
= chooser
.getSelectedFile();
470 String file
= theFile
.getPath();
471 if (!file
.startsWith(dirPrefix
)) {
472 JOptionPane
.showMessageDialog(frame
, "You can only select files in current package!");
482 headerDest
= theFile
.getPath();
483 int fileIndex
= headerDest
.indexOf(System
.getProperty("file.separator"), dirPrefix
.length());
485 jTextField
.setText(headerDest
.substring(fileIndex
+ 1).replace('\\', '/'));
490 return jButtonBrowse
;
493 public void componentResized(ComponentEvent arg0
) {
494 int intPreferredWidth
= 500;
496 Tools
.resizeComponentWidth(this.jScrollPaneMsa
, this.getWidth(), intPreferredWidth
);
497 Tools
.resizeComponentWidth(this.jTextField
, this.getWidth(), intPreferredWidth
);
498 Tools
.relocateComponentX(this.jButtonBrowse
, this.getWidth(), this.getPreferredSize().width
, 25);
501 public static void main(String
[] args
){
502 new SpdMsaFiles().setVisible(true);