]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/platform/ui/FpdFvOptions.java
Add Fv option value edit support;
[mirror_edk2.git] / Tools / Java / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / platform / ui / FpdFvOptions.java
1 /** @file
2 Java class FpdFvOptions is GUI for FV options in FPD 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.platform.ui;
14
15 import java.awt.BorderLayout;
16 import java.awt.Dimension;
17 import java.awt.Toolkit;
18 import java.awt.event.MouseEvent;
19 import java.awt.event.WindowEvent;
20 import java.util.Iterator;
21 import java.util.LinkedHashMap;
22 import java.util.Set;
23
24 import javax.swing.JFrame;
25 import javax.swing.JOptionPane;
26 import javax.swing.JPanel;
27 import javax.swing.JDialog;
28 import javax.swing.JScrollPane;
29 import javax.swing.JTable;
30 import javax.swing.ListSelectionModel;
31 import javax.swing.event.ListSelectionEvent;
32 import javax.swing.event.ListSelectionListener;
33 import javax.swing.event.TableModelEvent;
34 import javax.swing.event.TableModelListener;
35 import javax.swing.table.DefaultTableModel;
36 import javax.swing.table.TableModel;
37 import javax.swing.JButton;
38
39 import org.tianocore.frameworkwizard.common.Identifications.OpeningPlatformType;
40
41 /**
42 *
43 *
44 */
45 public class FpdFvOptions extends JDialog {
46
47 /**
48 *
49 */
50 private static final long serialVersionUID = 1L;
51 private static JFrame frame;
52 private JPanel jContentPane = null;
53 private JPanel jPanelN = null;
54 private JPanel jPanelS = null;
55 private JPanel jPanelC = null;
56 private JScrollPane jScrollPaneFvOptions = null;
57 private JTable jTableFvOptions = null;
58 private DefaultTableModel tableModel = null;
59 private String fvName = null;
60 private FpdFileContents ffc = null;
61 private OpeningPlatformType docConsole = null;
62 private JButton jButtonNew = null;
63 private JButton jButtonDelete = null;
64 private String oldOptionName = "";
65 private int selectedRow = -1;
66 private TableModelListener tableModelListener = null;
67
68 /**
69 * This is the default constructor
70 */
71 public FpdFvOptions(String name, DefaultTableModel tm, FpdFileContents ffc, OpeningPlatformType dc) {
72 super();
73 fvName = name;
74 this.ffc = ffc;
75 this.docConsole = dc;
76 setTableModel(tm);
77 initOptions();
78 initialize();
79
80 }
81
82 protected void processWindowEvent (WindowEvent e) {
83 if (e.getID() == WindowEvent.WINDOW_CLOSING) {
84 if (jTableFvOptions.isEditing()) {
85 jTableFvOptions.getCellEditor().stopCellEditing();
86 }
87 tableModel.removeTableModelListener(tableModelListener);
88 this.dispose();
89 }
90 }
91
92 private void initOptions() {
93 tableModel.setRowCount(0);
94 LinkedHashMap<String, String> mOpts = new LinkedHashMap<String, String>();
95 ffc.getFvImagesFvImageOptions(fvName, mOpts);
96 Set<String> sKey = mOpts.keySet();
97 Iterator<String> iter = sKey.iterator();
98 while (iter.hasNext()) {
99 String name = iter.next();
100 String value = mOpts.get(name);
101 tableModel.addRow(new String[]{name, value});
102 }
103 }
104
105 private boolean fvOptionNameExists (String name) {
106 int count = 0;
107 for (int i = 0; i < jTableFvOptions.getRowCount(); ++i) {
108 if (getTableModel().getValueAt(i, 0).equals(name)) {
109 ++count;
110 }
111 }
112 if (count > 1) {
113 return true;
114 }
115 return false;
116 }
117 /**
118 * This method initializes this
119 *
120 * @return void
121 */
122 private void initialize() {
123 this.setSize(650, 400);
124 // this.setModal(true);
125 this.setTitle("FV Options");
126 this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
127 this.setContentPane(getJContentPane());
128 this.centerWindow();
129 this.setVisible(true);
130
131 }
132
133 /**
134 * This method initializes jContentPane
135 *
136 * @return javax.swing.JPanel
137 */
138 private JPanel getJContentPane() {
139 if (jContentPane == null) {
140 jContentPane = new JPanel();
141 jContentPane.setLayout(new BorderLayout());
142 jContentPane.add(getJPanelN(), java.awt.BorderLayout.NORTH);
143 jContentPane.add(getJPanelS(), java.awt.BorderLayout.SOUTH);
144 jContentPane.add(getJPanelC(), java.awt.BorderLayout.CENTER);
145 }
146 return jContentPane;
147 }
148
149 /**
150 * This method initializes jPanelN
151 *
152 * @return javax.swing.JPanel
153 */
154 private JPanel getJPanelN() {
155 if (jPanelN == null) {
156 jPanelN = new JPanel();
157 }
158 return jPanelN;
159 }
160
161 /**
162 * This method initializes jPanelS
163 *
164 * @return javax.swing.JPanel
165 */
166 private JPanel getJPanelS() {
167 if (jPanelS == null) {
168 jPanelS = new JPanel();
169 jPanelS.add(getJButtonNew(), null);
170 jPanelS.add(getJButtonDelete(), null);
171 }
172 return jPanelS;
173 }
174
175 /**
176 * This method initializes jPanelC
177 *
178 * @return javax.swing.JPanel
179 */
180 private JPanel getJPanelC() {
181 if (jPanelC == null) {
182 jPanelC = new JPanel();
183 jPanelC.add(getJScrollPaneFvOptions(), null);
184 }
185 return jPanelC;
186 }
187
188 /**
189 * This method initializes jScrollPaneFvOptions
190 *
191 * @return javax.swing.JScrollPane
192 */
193 private JScrollPane getJScrollPaneFvOptions() {
194 if (jScrollPaneFvOptions == null) {
195 jScrollPaneFvOptions = new JScrollPane();
196 jScrollPaneFvOptions.setPreferredSize(new java.awt.Dimension(600,320));
197 jScrollPaneFvOptions.setViewportView(getJTableFvOptions());
198 }
199 return jScrollPaneFvOptions;
200 }
201
202 /**
203 * This method initializes jTableFvOptions
204 *
205 * @return javax.swing.JTable
206 */
207 private JTable getJTableFvOptions() {
208 if (jTableFvOptions == null) {
209 jTableFvOptions = new JTable(getTableModel()) {
210 /**
211 *
212 */
213 private static final long serialVersionUID = -1941328952828651192L;
214
215 public String getToolTipText(MouseEvent e) {
216 String tip = null;
217 java.awt.Point p = e.getPoint();
218 int rowIndex = rowAtPoint(p);
219 // int colIndex = columnAtPoint(p);
220 // int realColumnIndex = convertColumnIndexToModel(colIndex);
221
222 TableModel model = getModel();
223 String optName = (String) model.getValueAt(rowIndex, 0);
224 if (((FvOptsTableModel)model).getVKeyWords().contains(optName)){
225 tip = optName + " is from Flash Definition File and it is NOT editable.";
226 }
227
228 return tip;
229 }
230
231 };
232
233 jTableFvOptions.setRowHeight(20);
234
235 jTableFvOptions.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
236 public void valueChanged(ListSelectionEvent e) {
237 if (e.getValueIsAdjusting()) {
238 return;
239 }
240 ListSelectionModel lsm = (ListSelectionModel) e.getSource();
241 if (lsm.isSelectionEmpty()) {
242 return;
243 } else {
244 selectedRow = lsm.getMinSelectionIndex();
245 oldOptionName = getTableModel().getValueAt(selectedRow, 0)+"";
246 }
247 }
248 });
249
250 tableModelListener = new TableModelListener() {
251 public void tableChanged(TableModelEvent arg0) {
252 // TODO Auto-generated method stub
253 int row = arg0.getFirstRow();
254 int col = arg0.getColumn();
255 TableModel m = (TableModel) arg0.getSource();
256
257 if (arg0.getType() == TableModelEvent.UPDATE) {
258 String newOptionName = m.getValueAt(row, 0) + "";
259 if (col == 0) {
260 if (newOptionName.equals(oldOptionName)) {
261 return;
262 }
263 if (fvOptionNameExists(newOptionName)) {
264 JOptionPane.showMessageDialog(frame, "This Option already exists. Please choose another Option name.");
265 m.setValueAt(oldOptionName, row, 0);
266 return;
267 }
268
269 ffc.setTypedNamedFvImageNameValue(fvName, "Options", oldOptionName, m.getValueAt(row, 1)+"", newOptionName);
270 docConsole.setSaved(false);
271 oldOptionName = newOptionName;
272 }
273
274 if (col == 1) {
275 ffc.setTypedNamedFvImageNameValue(fvName, "Options", oldOptionName, m.getValueAt(row, 1)+"", newOptionName);
276 docConsole.setSaved(false);
277 }
278
279 }
280 }
281 };
282
283 jTableFvOptions.getModel().addTableModelListener(tableModelListener);
284 }
285 return jTableFvOptions;
286 }
287
288 protected DefaultTableModel getTableModel() {
289 return tableModel;
290 }
291
292 protected void setTableModel(DefaultTableModel tableModel) {
293
294 this.tableModel = tableModel;
295
296 }
297
298 /**
299 Start the window at the center of screen
300
301 **/
302 protected void centerWindow(int intWidth, int intHeight) {
303 Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
304 this.setLocation((d.width - intWidth) / 2, (d.height - intHeight) / 2);
305 }
306
307 /**
308 Start the window at the center of screen
309
310 **/
311 protected void centerWindow() {
312 centerWindow(this.getSize().width, this.getSize().height);
313 }
314
315 /**
316 * This method initializes jButtonNew
317 *
318 * @return javax.swing.JButton
319 */
320 private JButton getJButtonNew() {
321 if (jButtonNew == null) {
322 jButtonNew = new JButton();
323 jButtonNew.setPreferredSize(new java.awt.Dimension(80,20));
324 jButtonNew.setText("New");
325 jButtonNew.addActionListener(new java.awt.event.ActionListener() {
326 public void actionPerformed(java.awt.event.ActionEvent e) {
327 tableModel.addRow(new String[]{"", ""});
328 oldOptionName = "";
329 }
330 });
331 }
332 return jButtonNew;
333 }
334
335 /**
336 * This method initializes jButtonDelete
337 *
338 * @return javax.swing.JButton
339 */
340 private JButton getJButtonDelete() {
341 if (jButtonDelete == null) {
342 jButtonDelete = new JButton();
343 jButtonDelete.setPreferredSize(new java.awt.Dimension(80,20));
344 jButtonDelete.setText("Delete");
345 jButtonDelete.addActionListener(new java.awt.event.ActionListener() {
346 public void actionPerformed(java.awt.event.ActionEvent e) {
347 int selectedRow = jTableFvOptions.getSelectedRow();
348 if (selectedRow < 0) {
349 return;
350 }
351 String optName = tableModel.getValueAt(selectedRow, 0)+"";
352 if (((FvOptsTableModel)tableModel).getVKeyWords().contains(optName)){
353 return;
354 }
355 if (((FvOptsTableModel)tableModel).getVNonEditableName().contains(optName)){
356 return;
357 }
358
359 ffc.removeTypedNamedFvImageNameValue(fvName, "Options", optName);
360 tableModel.removeRow(selectedRow);
361 docConsole.setSaved(false);
362 }
363 });
364 }
365 return jButtonDelete;
366 }
367 }