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