Add check before adding msa file to spd file.
[mirror_edk2.git] / Tools / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / packaging / ui / SpdMsaFiles.java
1 /** @file
2 Java class SpdMsaFiles 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.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;
39
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;
45
46 /**
47 GUI for create library definition elements of spd file.
48
49 @since PackageEditor 1.0
50 **/
51 public class SpdMsaFiles extends IInternalFrame implements TableModelListener{
52 /**
53 *
54 */
55 private static final long serialVersionUID = 1L;
56
57 static JFrame frame;
58
59 private JScrollPane jScrollPane = null; // @jve:decl-index=0:visual-constraint="10,95"
60
61 private JTable jTable = null;
62
63 private DefaultTableModel model = null;
64
65 private JPanel jContentPane = null;
66
67 private JScrollPane jScrollPaneMsa = null;
68
69 private JButton jButtonAdd = null;
70
71 private JButton jButtonRemove = null;
72
73 private JButton jButtonClearAll = null;
74
75 private JButton jButtonCancel = null;
76
77 private JButton jButtonOk = null;
78
79 private JLabel jLabel = null;
80
81 private JTextField jTextField = null;
82
83 private JButton jButtonBrowse = null;
84
85 private StarLabel jStarLabel2 = null;
86
87 private SpdFileContents sfc = null;
88
89 private OpeningPackageType docConsole = null;
90
91 private int selectedRow = -1;
92
93 /**
94 This method initializes this
95
96 **/
97 private void initialize() {
98
99 this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
100
101 }
102
103 /**
104 This method initializes jScrollPane
105
106 @return javax.swing.JScrollPane
107 **/
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());
113 }
114 return jScrollPaneMsa;
115 }
116
117 /**
118 This method initializes jTable
119
120 @return javax.swing.JTable
121 **/
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");
128
129 jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
130 jTable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
131 public void valueChanged(ListSelectionEvent e) {
132 if (e.getValueIsAdjusting()){
133 return;
134 }
135 ListSelectionModel lsm = (ListSelectionModel)e.getSource();
136 if (lsm.isSelectionEmpty()) {
137 return;
138 }
139 else{
140 selectedRow = lsm.getMinSelectionIndex();
141 }
142 }
143 });
144
145 jTable.getModel().addTableModelListener(this);
146 }
147 return jTable;
148 }
149 /**
150 This method initializes jButtonAdd
151
152 @return javax.swing.JButton
153 **/
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);
160 }
161 return jButtonAdd;
162 }
163
164 /**
165 This method initializes jButtonRemove
166
167 @return javax.swing.JButton
168 **/
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);
175 }
176 return jButtonRemove;
177 }
178
179 /**
180 This method initializes jButtonRemoveAll
181
182 @return javax.swing.JButton
183 **/
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);
190 }
191 return jButtonClearAll;
192 }
193
194 /**
195 This method initializes jButtonCancel
196
197 @return javax.swing.JButton
198 **/
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);
208 }
209 return jButtonCancel;
210 }
211
212 /**
213 This method initializes jButton
214
215 @return javax.swing.JButton
216 **/
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);
225 }
226 return jButtonOk;
227 }
228
229 /**
230 This is the default constructor
231 **/
232 public SpdMsaFiles() {
233 super();
234 initialize();
235 init();
236
237 }
238
239 public SpdMsaFiles(PackageSurfaceAreaDocument.PackageSurfaceArea inPsa){
240 this();
241 sfc = new SpdFileContents(inPsa);
242 init(sfc);
243 }
244
245 public SpdMsaFiles(OpeningPackageType opt){
246 this(opt.getXmlSpd());
247 docConsole = opt;
248 }
249 /**
250 This method initializes this
251
252 @return void
253 **/
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();
263 }
264 }
265 });
266 this.setVisible(true);
267 initFrame();
268 }
269
270 private void init(SpdFileContents sfc){
271 if (sfc.getSpdMsaFileCount() == 0) {
272 return ;
273 }
274 //
275 // initialize table using SpdFileContents object
276 //
277 String[][] saa = new String[sfc.getSpdMsaFileCount()][4];
278 sfc.getSpdMsaFiles(saa);
279 int i = 0;
280 while (i < saa.length) {
281 model.addRow(saa[i]);
282 i++;
283 }
284 }
285
286 private JScrollPane getJScrollPane(){
287 if (jScrollPane == null){
288 jScrollPane = new JScrollPane();
289 jScrollPane.setViewportView(getJContentPane());
290 }
291 return jScrollPane;
292 }
293 /**
294 This method initializes jContentPane
295
296 @return javax.swing.JPanel
297 **/
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 ");
305
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);
317
318 jContentPane.add(getJTextField(), null);
319 jContentPane.add(getJButtonBrowse(), null);
320 }
321 return jContentPane;
322 }
323
324 /**
325 fill ComboBoxes with pre-defined contents
326 **/
327 private void initFrame() {
328
329 }
330
331 /* (non-Javadoc)
332 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
333 */
334 public void actionPerformed(ActionEvent arg0) {
335
336 if (arg0.getSource() == jButtonOk) {
337 this.save();
338 this.dispose();
339
340 }
341 if (arg0.getSource() == jButtonCancel) {
342 this.dispose();
343 }
344
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.");
350 return;
351 }
352
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.");
356 return;
357 }
358 model.addRow(row);
359 jTable.changeSelection(model.getRowCount()-1, 0, false, false);
360 sfc.genSpdMsaFiles(row[0], null, null, null);
361 docConsole.setSaved(false);
362 }
363 //
364 // remove selected line
365 //
366 if (arg0.getSource() == jButtonRemove) {
367 if (jTable.isEditing()){
368 jTable.getCellEditor().stopCellEditing();
369 }
370 int rowSelected = selectedRow;
371 if (rowSelected >= 0) {
372 model.removeRow(rowSelected);
373 sfc.removeSpdMsaFile(rowSelected);
374 docConsole.setSaved(false);
375 }
376 }
377
378 if (arg0.getSource() == jButtonClearAll) {
379 if (model.getRowCount() == 0) {
380 return;
381 }
382 model.setRowCount(0);
383 sfc.removeSpdMsaFile();
384 docConsole.setSaved(false);
385 }
386
387 }
388
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) {
399 cellData = "";
400 }
401 if (cellData.equals(sa[column])) {
402 return;
403 }
404 if (cellData.toString().length() == 0 && sa[column] == null) {
405 return;
406 }
407 String file = m.getValueAt(row, 0) + "";
408 if (file.length() == 0) {
409 JOptionPane.showMessageDialog(this, "Msa File is NOT PathAndFilename type.");
410 return;
411 }
412 docConsole.setSaved(false);
413 sfc.updateSpdMsaFile(row, file, null, null, null);
414 }
415 }
416
417 /**
418 Add contents in list to sfc
419 **/
420 protected void save() {
421
422 }
423
424 /**
425 This method initializes jTextField
426
427 @return javax.swing.JTextField
428 **/
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));
434 }
435 return jTextField;
436 }
437
438 /**
439 This method initializes jButtonBrowse
440
441 @return javax.swing.JButton
442 **/
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() {
450 /**
451 *
452 */
453 private static final long serialVersionUID = 1L;
454
455 public void actionPerformed(ActionEvent e) {
456 //
457 // Select files from current workspace
458 //
459 String dirPrefix = Tools.dirForNewSpd.substring(0, Tools.dirForNewSpd.lastIndexOf(File.separator));
460 JFileChooser chooser = new JFileChooser(dirPrefix);
461 File theFile = null;
462 String headerDest = null;
463
464 chooser.setMultiSelectionEnabled(false);
465 chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
466 int retval = chooser.showOpenDialog(frame);
467 if (retval == JFileChooser.APPROVE_OPTION) {
468
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!");
473 return;
474 }
475
476
477 }
478 else {
479 return;
480 }
481
482 headerDest = theFile.getPath();
483 int fileIndex = headerDest.indexOf(System.getProperty("file.separator"), dirPrefix.length());
484
485 jTextField.setText(headerDest.substring(fileIndex + 1).replace('\\', '/'));
486
487 }
488 });
489 }
490 return jButtonBrowse;
491 }
492
493 public void componentResized(ComponentEvent arg0) {
494 int intPreferredWidth = 500;
495
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);
499 }
500
501 public static void main(String[] args){
502 new SpdMsaFiles().setVisible(true);
503 }
504 }
505
506