]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/MigrationTools/org/tianocore/migration/FirstPanel.java
Coding Style
[mirror_edk2.git] / Tools / Java / Source / MigrationTools / org / tianocore / migration / FirstPanel.java
1 /** @file
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 **/
13 package org.tianocore.migration;
14
15 import java.awt.GridBagConstraints;
16 import java.awt.GridBagLayout;
17 import java.awt.Insets;
18 import java.awt.event.ActionEvent;
19 import java.awt.event.ActionListener;
20 import java.awt.event.ItemEvent;
21 import java.awt.event.ItemListener;
22 import java.io.BufferedWriter;
23 import java.io.File;
24 import java.io.FileWriter;
25 import java.io.PrintWriter;
26 import java.util.Set;
27
28 import javax.swing.BoxLayout;
29 import javax.swing.JButton;
30 import javax.swing.JCheckBox;
31 import javax.swing.JFileChooser;
32 import javax.swing.JFrame;
33 import javax.swing.JOptionPane;
34 import javax.swing.JPanel;
35 import javax.swing.JScrollPane;
36 import javax.swing.JTextArea;
37 import javax.swing.JTextField;
38 import javax.swing.UIManager;
39
40 public final class FirstPanel extends JPanel implements ActionListener,
41 ItemListener, UI {
42 /**
43 * Define class Serial Version UID
44 */
45 private static final long serialVersionUID = 207759413522910399L;
46
47 private static final FirstPanel INSTANCE = FirstPanel.init();
48
49 private String startpath = null;
50
51 private JButton moduleButton, goButton, msaEditorButton, criticButton,
52 specifyCommentButton;
53
54 private JTextField moduletext;
55
56 private JTextArea log;
57
58 private JFileChooser fc = new JFileChooser();
59
60 private JCheckBox filebox, screenbox, mibox, criticbox, defaultpathbox;
61
62 private boolean tofile = true, toscreen = true;
63
64 private PrintWriter logfile;
65
66 FirstPanel() {
67 GridBagLayout gridbag = new GridBagLayout();
68 setLayout(gridbag);
69
70 GridBagConstraints cst = new GridBagConstraints();
71
72 goButton = new JButton("Go");
73 goButton.addActionListener(this);
74 goButton.setActionCommand("go");
75
76 moduleButton = new JButton("Choose ModulePath");
77 moduleButton.addActionListener(this);
78
79 msaEditorButton = new JButton("MsaEditor");
80 msaEditorButton.addActionListener(this);
81
82 criticButton = new JButton("Critic");
83 criticButton.addActionListener(this);
84
85 specifyCommentButton = new JButton("Comment Style");
86 specifyCommentButton.addActionListener(this);
87
88 moduletext = new JTextField(30);
89
90 filebox = new JCheckBox("Output to logfile", true);
91 filebox.addItemListener(this);
92
93 screenbox = new JCheckBox("Specify logfile", false);
94 screenbox.addItemListener(this);
95
96 mibox = new JCheckBox("Print ModuleInfo", false);
97 mibox.addItemListener(this);
98 MigrationTool.printModuleInfo = false;
99
100 criticbox = new JCheckBox("Run Critic", true);
101 criticbox.addItemListener(this);
102 MigrationTool.doCritic = true;
103
104 defaultpathbox = new JCheckBox("Use Default Output Path", true);
105 defaultpathbox.addItemListener(this);
106 MigrationTool.defaultoutput = true;
107
108 JPanel modulePanel = new JPanel();
109 modulePanel.add(moduleButton);
110 modulePanel.add(moduletext);
111 modulePanel.add(goButton);
112 // modulePanel.add(msaEditorButton);
113 cst.gridx = 0;
114 cst.gridy = 0;
115 // cst.gridwidth = GridBagConstraints.REMAINDER;
116 gridbag.setConstraints(modulePanel, cst);
117 add(modulePanel);
118
119 cst.gridx = 1;
120 cst.gridy = 0;
121 gridbag.setConstraints(specifyCommentButton, cst);
122 add(specifyCommentButton);
123 // gridbag.setConstraints(criticButton, cst);
124 // add(criticButton);
125
126 JPanel checkboxPanel = new JPanel();
127 checkboxPanel.setLayout(new BoxLayout(checkboxPanel, BoxLayout.Y_AXIS));
128 checkboxPanel.add(filebox);
129 checkboxPanel.add(screenbox);
130 checkboxPanel.add(mibox);
131 checkboxPanel.add(criticbox);
132 checkboxPanel.add(defaultpathbox);
133 cst.gridx = 1;
134 cst.gridy = 1;
135 // cst.gridheight = 2;
136 gridbag.setConstraints(checkboxPanel, cst);
137 add(checkboxPanel);
138
139 log = new JTextArea(10, 20);
140 log.setMargin(new Insets(5, 5, 5, 5));
141 log.setEditable(false);
142 JScrollPane logScrollPane = new JScrollPane(log);
143 cst.gridx = 0;
144 cst.gridy = 1;
145 cst.fill = GridBagConstraints.BOTH;
146 gridbag.setConstraints(logScrollPane, cst);
147 add(logScrollPane);
148 }
149
150 // ---------------------------------------------------------------------------------------//
151
152 public boolean yesOrNo(String question) {
153 return JOptionPane.showConfirmDialog(this, question, "Yes or No",
154 JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION;
155 }
156
157 public void print(String message) {
158 if (toscreen == true) {
159 log.append(message);
160 System.out.print(message);
161 }
162 if (tofile == true) {
163 logfile.append(message);
164 }
165 }
166
167 public void println(String message) {
168 print(message + "\n");
169 }
170
171 public void println(Set<String> hash) {
172 if (toscreen == true) {
173 log.append(hash + "\n");
174 System.out.println(hash);
175 }
176 if (tofile == true) {
177 logfile.append(hash + "\n");
178 }
179 }
180
181 public String choose(String message, Object[] choicelist) {
182 return (String) JOptionPane.showInputDialog(this, message, "Choose",
183 JOptionPane.PLAIN_MESSAGE, null, choicelist, choicelist[0]);
184 }
185
186 public String getInput(String message) {
187 return (String) JOptionPane.showInputDialog(message);
188 }
189
190 // ---------------------------------------------------------------------------------------//
191
192 public String getFilepath(String title, int mode) {
193 fc.setDialogTitle(title);
194 fc.setFileSelectionMode(mode);
195 if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
196 log.append(fc.getSelectedFile().getAbsolutePath() + "\n");
197 return fc.getSelectedFile().getAbsolutePath();
198 }
199 return null;
200 }
201
202 // ---------------------------------------------------------------------------------------//
203
204 public void actionPerformed(ActionEvent e) {
205 if (e.getSource() == moduleButton) {
206 startpath = getFilepath("Please choose a starting path",
207 JFileChooser.DIRECTORIES_ONLY);
208 moduletext.setText(startpath);
209 }
210 if (e.getSource() == goButton) {
211 try {
212 logfile = new PrintWriter(new BufferedWriter(new FileWriter(
213 startpath.replaceAll(Common.STRSEPARATER, "$1")
214 + File.separator + "migration.log")));
215 MigrationTool.startMigrateAll(startpath);
216 logfile.flush();
217 logfile.close();
218 } catch (Exception en) {
219 println(en.getMessage());
220 }
221 }
222 if (e.getSource() == msaEditorButton) {
223 try {
224 MsaTreeEditor.init();
225 } catch (Exception en) {
226 println(en.getMessage());
227 }
228 }
229 if (e.getSource() == criticButton) {
230 try {
231 Critic.fireAt(startpath);
232 } catch (Exception en) {
233 println(en.getMessage());
234 }
235 }
236 if (e.getSource() == specifyCommentButton) {
237 try { // input examine is not imposed but should be added
238 MigrationTool.MIGRATIONCOMMENT = getInput("Please type in wanted comment style used by the tool\nbe sure to start with '//', or you won't enjoy the result");
239 // MsaWriter.parse("C:\\tianocore\\edk2\\MdePkg\\Library\\BaseLib\\BaseLib.msa");
240 } catch (Exception en) {
241 println(en.getMessage());
242 }
243 }
244 }
245
246 public void itemStateChanged(ItemEvent e) {
247 if (e.getSource() == filebox) {
248 if (e.getStateChange() == ItemEvent.DESELECTED) {
249 System.out.println("filebox DESELECTED");
250 } else if (e.getStateChange() == ItemEvent.SELECTED) {
251 System.out.println("filebox SELECTED");
252 }
253 } else if (e.getSource() == screenbox) {
254 if (e.getStateChange() == ItemEvent.DESELECTED) {
255 System.out.println("screenbox DESELECTED");
256 } else if (e.getStateChange() == ItemEvent.SELECTED) {
257 System.out.println("screenbox SELECTED");
258 }
259 } else if (e.getSource() == mibox) {
260 if (e.getStateChange() == ItemEvent.DESELECTED) {
261 MigrationTool.printModuleInfo = false;
262 } else if (e.getStateChange() == ItemEvent.SELECTED) {
263 MigrationTool.printModuleInfo = true;
264 }
265 } else if (e.getSource() == criticbox) {
266 if (e.getStateChange() == ItemEvent.DESELECTED) {
267 MigrationTool.doCritic = false;
268 } else if (e.getStateChange() == ItemEvent.SELECTED) {
269 MigrationTool.doCritic = true;
270 }
271 } else if (e.getSource() == defaultpathbox) {
272 if (e.getStateChange() == ItemEvent.DESELECTED) {
273 MigrationTool.defaultoutput = false;
274 } else if (e.getStateChange() == ItemEvent.SELECTED) {
275 MigrationTool.defaultoutput = true;
276 }
277 }
278 }
279
280 // ---------------------------------------------------------------------------------------//
281
282 private static final FirstPanel init() {
283 try {
284 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
285 } catch (Exception e) {
286 System.out.println(e.getMessage());
287 }
288
289 JFrame frame = new JFrame("MigrationTools");
290 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
291
292 FirstPanel fp = new FirstPanel();
293 fp.setOpaque(true);
294 frame.setContentPane(fp);
295
296 frame.pack();
297 frame.setVisible(true);
298
299 return fp;
300 }
301
302 public static final FirstPanel getInstance() {
303 return INSTANCE;
304 }
305 }