summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/platform/ui/FpdFvOptions.java
blob: b91f0c8f039cfb136ebbe6ac75674eb3cc469f97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/** @file
  Java class FpdFvOptions is GUI for FV options in FPD file.
 
Copyright (c) 2006, Intel Corporation
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution.  The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
package org.tianocore.frameworkwizard.platform.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.JButton;

import org.tianocore.frameworkwizard.FrameworkWizardUI;
import org.tianocore.frameworkwizard.common.Identifications.OpeningPlatformType;

/**
 * 
 *
 */
public class FpdFvOptions extends JDialog {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private static JFrame frame;
    private JPanel jContentPane = null;
    private JPanel jPanelN = null;
    private JPanel jPanelS = null;
    private JPanel jPanelC = null;
    private JScrollPane jScrollPaneFvOptions = null;
    private JTable jTableFvOptions = null;
    private DefaultTableModel tableModel = null;
    private String fvName = null;
    private FpdFileContents ffc = null;
    private OpeningPlatformType docConsole = null;
    private JButton jButtonNew = null;
    private JButton jButtonDelete = null;
    private String oldOptionName = "";
    private int selectedRow = -1;
    private TableModelListener tableModelListener = null;

    /**
     * This is the default constructor
     */
    public FpdFvOptions(String name, DefaultTableModel tm, FpdFileContents ffc, OpeningPlatformType dc) {
        super(FrameworkWizardUI.getInstance());
        fvName = name;
        this.ffc = ffc;
        this.docConsole = dc;
        setTableModel(tm);
        initOptions();
        initialize();
        
    }
    
    protected void processWindowEvent (WindowEvent e) {
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
            if (jTableFvOptions.isEditing()) {
                jTableFvOptions.getCellEditor().stopCellEditing();
            }
            tableModel.removeTableModelListener(tableModelListener);
            this.dispose();
        }
    }

    private void initOptions() {
        tableModel.setRowCount(0);
        LinkedHashMap<String, String> mOpts = new LinkedHashMap<String, String>();
        ffc.getFvImagesFvImageOptions(fvName, mOpts);
        Set<String> sKey = mOpts.keySet();
        Iterator<String> iter = sKey.iterator();
        while (iter.hasNext()) {
            String name = iter.next();
            String value = mOpts.get(name);
            tableModel.addRow(new String[]{name, value});
        }
    }
    
    private boolean fvOptionNameExists (String name) {
        int count = 0;
        for (int i = 0; i < jTableFvOptions.getRowCount(); ++i) {
            if (getTableModel().getValueAt(i, 0).equals(name)) {
                ++count;
            }
        }
        if (count > 1) {
            return true;
        }
        return false;
    }
    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize() {
        this.setSize(650, 400);
        this.setModal(true);
        this.setTitle("FV Options");
        this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
        this.setContentPane(getJContentPane());
        this.centerWindow();
        this.setVisible(true);
        
    }

    /**
     * This method initializes jContentPane
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane() {
        if (jContentPane == null) {
            jContentPane = new JPanel();
            jContentPane.setLayout(new BorderLayout());
            jContentPane.add(getJPanelN(), java.awt.BorderLayout.NORTH);
            jContentPane.add(getJPanelS(), java.awt.BorderLayout.SOUTH);
            jContentPane.add(getJPanelC(), java.awt.BorderLayout.CENTER);
        }
        return jContentPane;
    }

    /**
     * This method initializes jPanelN	
     * 	
     * @return javax.swing.JPanel	
     */
    private JPanel getJPanelN() {
        if (jPanelN == null) {
            jPanelN = new JPanel();
        }
        return jPanelN;
    }

    /**
     * This method initializes jPanelS	
     * 	
     * @return javax.swing.JPanel	
     */
    private JPanel getJPanelS() {
        if (jPanelS == null) {
            jPanelS = new JPanel();
            jPanelS.add(getJButtonNew(), null);
            jPanelS.add(getJButtonDelete(), null);
        }
        return jPanelS;
    }

    /**
     * This method initializes jPanelC	
     * 	
     * @return javax.swing.JPanel	
     */
    private JPanel getJPanelC() {
        if (jPanelC == null) {
            jPanelC = new JPanel();
            jPanelC.add(getJScrollPaneFvOptions(), null);
        }
        return jPanelC;
    }

    /**
     * This method initializes jScrollPaneFvOptions	
     * 	
     * @return javax.swing.JScrollPane	
     */
    private JScrollPane getJScrollPaneFvOptions() {
        if (jScrollPaneFvOptions == null) {
            jScrollPaneFvOptions = new JScrollPane();
            jScrollPaneFvOptions.setPreferredSize(new java.awt.Dimension(600,320));
            jScrollPaneFvOptions.setViewportView(getJTableFvOptions());
        }
        return jScrollPaneFvOptions;
    }

    /**
     * This method initializes jTableFvOptions	
     * 	
     * @return javax.swing.JTable	
     */
    private JTable getJTableFvOptions() {
        if (jTableFvOptions == null) {
            jTableFvOptions = new JTable(getTableModel()) {
                /**
                 * 
                 */
                private static final long serialVersionUID = -1941328952828651192L;

                public String getToolTipText(MouseEvent e) {
                    String tip = null;
                    java.awt.Point p = e.getPoint();
                    int rowIndex = rowAtPoint(p);
//                    int colIndex = columnAtPoint(p);
//                    int realColumnIndex = convertColumnIndexToModel(colIndex);

                    TableModel model = getModel();
                    String optName = (String) model.getValueAt(rowIndex, 0);
                    if (((FvOptsTableModel)model).getVKeyWords().contains(optName)){
                        tip = optName + " is from Flash Definition File and it is NOT editable.";
                    }
                         
                    return tip;
                }

            };
 
            jTableFvOptions.setRowHeight(20);
            
            jTableFvOptions.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
                public void valueChanged(ListSelectionEvent e) {
                    if (e.getValueIsAdjusting()) {
                        return;
                    }
                    ListSelectionModel lsm = (ListSelectionModel) e.getSource();
                    if (lsm.isSelectionEmpty()) {
                        return;
                    } else {
                        selectedRow = lsm.getMinSelectionIndex();
                        oldOptionName = getTableModel().getValueAt(selectedRow, 0)+"";
                    }
                }
            });
            
            tableModelListener = new TableModelListener() {
                public void tableChanged(TableModelEvent arg0) {
                    // TODO Auto-generated method stub
                    int row = arg0.getFirstRow();
                    int col = arg0.getColumn();
                    TableModel m = (TableModel) arg0.getSource();
                    
                    if (arg0.getType() == TableModelEvent.UPDATE) {
                        String newOptionName = m.getValueAt(row, 0) + "";
                        if (col == 0) {
                            if (newOptionName.equals(oldOptionName)) {
                                return;
                            }
                            if (fvOptionNameExists(newOptionName)) {
                                JOptionPane.showMessageDialog(frame, "This Option already exists. Please choose another Option name.");
                                m.setValueAt(oldOptionName, row, 0);
                                return;
                            }
                            
                            ffc.setTypedNamedFvImageNameValue(fvName, "Options", oldOptionName, m.getValueAt(row, 1)+"", newOptionName);
                            docConsole.setSaved(false);
                            oldOptionName = newOptionName;
                        }
                        
                        if (col == 1) {
                            ffc.setTypedNamedFvImageNameValue(fvName, "Options", oldOptionName, m.getValueAt(row, 1)+"", newOptionName);
                            docConsole.setSaved(false);    
                        }
                                            
                    }
                }
            };
            
            jTableFvOptions.getModel().addTableModelListener(tableModelListener);
        }
        return jTableFvOptions;
    }

    protected DefaultTableModel getTableModel() {
        return tableModel;
    }

    protected void setTableModel(DefaultTableModel tableModel) {
        
        this.tableModel = tableModel;
        
    }

    /**
    Start the window at the center of screen
    
    **/
   protected void centerWindow(int intWidth, int intHeight) {
       Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
       this.setLocation((d.width - intWidth) / 2, (d.height - intHeight) / 2);
   }

   /**
    Start the window at the center of screen
    
    **/
   protected void centerWindow() {
       centerWindow(this.getSize().width, this.getSize().height);
   }

/**
 * This method initializes jButtonNew	
 * 	
 * @return javax.swing.JButton	
 */
private JButton getJButtonNew() {
    if (jButtonNew == null) {
        jButtonNew = new JButton();
        jButtonNew.setPreferredSize(new java.awt.Dimension(80,20));
        jButtonNew.setText("New");
        jButtonNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                tableModel.addRow(new String[]{"", ""});
                oldOptionName = "";
            }
        });
    }
    return jButtonNew;
}

/**
 * This method initializes jButtonDelete	
 * 	
 * @return javax.swing.JButton	
 */
private JButton getJButtonDelete() {
    if (jButtonDelete == null) {
        jButtonDelete = new JButton();
        jButtonDelete.setPreferredSize(new java.awt.Dimension(80,20));
        jButtonDelete.setText("Delete");
        jButtonDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                int selectedRow = jTableFvOptions.getSelectedRow();
                if (selectedRow < 0) {
                    return;
                }
                String optName = tableModel.getValueAt(selectedRow, 0)+"";
                if (((FvOptsTableModel)tableModel).getVKeyWords().contains(optName)){
                    return;
                }
                if (((FvOptsTableModel)tableModel).getVNonEditableName().contains(optName)){
                    return;
                }
                
                ffc.removeTypedNamedFvImageNameValue(fvName, "Options", optName);
                tableModel.removeRow(selectedRow);
                docConsole.setSaved(false);
            }
        });
    }
    return jButtonDelete;
}
}