forked from MrDHat/Syn-C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainUI.java
268 lines (227 loc) · 6.84 KB
/
MainUI.java
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
import javax.swing.* ;
import java.awt.* ;
import java.awt.event.*;
import java.io.* ;
public class MainUI extends JFrame implements ActionListener {
String dir = null, file ;
private JScrollPane scrollPane1;
private JTextArea codeArea;
private JButton browseBtn;
private JButton convertBtn;
private JButton compileBtn;
private JTabbedPane TP, LP;
private JPanel consolePanel, logPanel;
private JTextArea consoleArea, logArea;
private JScrollPane scrollPane2, scrollPane3, scrollPane4;
MainUI() {
scrollPane1 = new JScrollPane();
codeArea = new JTextArea();
browseBtn = new JButton();
convertBtn = new JButton();
compileBtn = new JButton();
TP = new JTabbedPane();
LP = new JTabbedPane();
consolePanel = new JPanel();
scrollPane3 = new JScrollPane();
consoleArea = new JTextArea();
logPanel = new JPanel();
scrollPane4 = new JScrollPane();
logArea = new JTextArea();
//======== this ========
setResizable(false);
setTitle("Syn-C");
Container contentPane = getContentPane();
contentPane.setLayout(null);
//======== scrollPane1 ========
{
scrollPane1.setViewportView(codeArea);
}
contentPane.add(scrollPane1);
scrollPane1.setBounds(5, 85, 875, 375);
//---- browseBtn ----
browseBtn.setText("Browse");
contentPane.add(browseBtn);
browseBtn.setBounds(190, 20, 100, 45);
browseBtn.addActionListener(this) ;
//---- convertBtn ----
convertBtn.setText("Convert");
contentPane.add(convertBtn);
convertBtn.setBounds(385, 20, 110, 45);
convertBtn.addActionListener(this) ;
//---- compileBtn ----
compileBtn.setText("Compile");
contentPane.add(compileBtn);
compileBtn.setBounds(580, 20, 165, 45);
compileBtn.addActionListener(this) ;
//======== TP ========
{
//======== consolePanel ========
{
consolePanel.setMinimumSize(new Dimension(900, 85));
consolePanel.setPreferredSize(new Dimension(900, 85));
consolePanel.setLayout(null);
//======== scrollPane3 ========
{
scrollPane3.setViewportView(consoleArea);
}
consolePanel.add(scrollPane3);
scrollPane3.setBounds(0, 0, 850, 80);
{ // compute preferred size
Dimension preferredSize = new Dimension();
for(int i = 0; i < consolePanel.getComponentCount(); i++) {
Rectangle bounds = consolePanel.getComponent(i).getBounds();
preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
}
Insets insets = consolePanel.getInsets();
preferredSize.width += insets.right;
preferredSize.height += insets.bottom;
consolePanel.setMinimumSize(preferredSize);
consolePanel.setPreferredSize(preferredSize);
}
}
TP.addTab("Console", consolePanel);
}
// contentPane.add(TP);
// TP.setBounds(15, 475, 795, 130);
//======== LP ========
{
//======== logPanel ========
{
logPanel.setMinimumSize(new Dimension(900, 85));
logPanel.setPreferredSize(new Dimension(900, 85));
logPanel.setLayout(null);
//======== scrollPane4 ========
{
scrollPane4.setViewportView(logArea);
}
logPanel.add(scrollPane4);
scrollPane4.setBounds(0, 0, 850, 80);
{ // compute preferred size
Dimension preferredSize = new Dimension();
for(int i = 0; i < logPanel.getComponentCount(); i++) {
Rectangle bounds = logPanel.getComponent(i).getBounds();
preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
}
Insets insets = logPanel.getInsets();
preferredSize.width += insets.right;
preferredSize.height += insets.bottom;
logPanel.setMinimumSize(preferredSize);
logPanel.setPreferredSize(preferredSize);
}
}
TP.addTab("Log", logPanel);
}
contentPane.add(TP);
TP.setBounds(15, 475, 795, 130);
contentPane.setPreferredSize(new Dimension(905, 640));
pack();
setLocationRelativeTo(getOwner());
setVisible(true) ;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
File delFile = new File("temp.c") ;
delFile.delete() ;
delFile = new File("temp") ;
delFile.delete() ;
}
public void actionPerformed(ActionEvent ae) {
if(ae.getSource() == browseBtn) {
FileDialog inp = new FileDialog(this, "Input File...") ;
inp.setVisible(true) ;
file = inp.getFile();
dir = inp.getDirectory();
try {
FileReader reader = new FileReader(dir+file) ;
BufferedReader br = new BufferedReader(reader) ;
codeArea.read(br, null) ;
br.close() ;
}
catch(Exception e) {
System.out.println(e) ;
}
}
else if(ae.getSource() == convertBtn) {
if(dir != null) {
String param ;
param = "./flex_output " + dir + " " + file ;
try{
Process p = Runtime.getRuntime().exec(param) ;
int res = 1 ;
System.out.println(dir) ;
while(res != 0) {
try{
res = p.waitFor() ;
if(res == -1)
return ;
}
catch(Exception e){System.out.println("Error!!") ;}
}
}
catch (IOException e) {System.out.println(" procccess not read"+e);}
try {
FileReader reader = new FileReader(dir+"sync_"+file) ;
BufferedReader br = new BufferedReader(reader) ;
codeArea.read(br, null) ;
br.close() ;
}
catch(Exception e) {
System.out.println(e) ;
}
try {
FileReader reader = new FileReader("log.txt") ;
BufferedReader br = new BufferedReader(reader) ;
logArea.read(br, null) ;
br.close() ;
}
catch(Exception e) {
System.out.println(e) ;
}
}
}
else {
consoleArea.setText("") ;
// COMPILE
try {
BufferedWriter fileOut = new BufferedWriter(new FileWriter("temp.c"));
String myString1 =codeArea.getText();
String myString2 = myString1.replace("\r", "\n");
fileOut.write(myString2);
fileOut.close();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
try {
String line;
Process p = Runtime.getRuntime().exec("gcc -o temp temp.c");
BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader bre = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((line = bri.readLine()) != null) {
consoleArea.append(line) ;
}
bri.close();
while ((line = bre.readLine()) != null) {
consoleArea.append(line) ;
consoleArea.append("\n") ;
}
bre.close();
p.waitFor();
}
catch (Exception err) {
consoleArea.append(err.toString()) ;
}
// RUN
if(consoleArea.getText().trim().length() == 0) {
consoleArea.setText("Compilation Successfull!") ;
}
File delFile = new File("temp.c") ;
delFile.delete() ;
delFile = new File("temp") ;
delFile.delete() ;
}
}
public static void main(String[] args) {
new MainUI() ;
}
}