CS 330 Lecture 19 – Abstracting a GUI
Agenda
- what ?s
- finishing GUI abstraction
- pitfalls of C
- C with Classes
TODO
- Compose a thoughtful and conceptual question that could appear on the midterm on Piazza. 1/4 sheet point, but just post.
Code
gui.xml
<?xml version="1.0" encoding="UTF-8"?>
<panel orientation="vertical">
<panel orientation="horizontal">
<label text="To:" />
<textbox name="toBox" />
</panel>
<panel orientation="horizontal">
<label text="Subject:" />
<textbox name="subjectBox" />
</panel>
<textarea name="bodyBox" />
<button text="Send" />
</panel>
gui_madness.c
#include <stdio.h>
#include <stdlib.h>
#include <expat.h>
#include <string.h>
void open_handler(void *data,
const char *tag,
const char **attributes) {
/* printf("open tag: %s\n", tag); */
int *ptag_counter = (int *) data;
if (strcmp(tag, "panel") == 0) {
int is_vertical = 1;
for (int i = 0; attributes[i] != NULL; i += 2) {
if (strcmp(attributes[i], "orientation") == 0) {
if (strcmp(attributes[i + 1], "horizontal") == 0) {
is_vertical = 0;
}
}
}
printf("{\nJPanel panel%d = new JPanel();\n", *ptag_counter);
printf("panel%d.setLayout(new BoxLayout(panel%d, BoxLayout.%s));\n", *ptag_counter, *ptag_counter, is_vertical ? "PAGE_AXIS" : "LINE_AXIS");
if (*ptag_counter == 0) {
printf("add(panel%d);\n", *ptag_counter);
} else {
printf("panel%d.add(panel%d);\n", *ptag_counter - 1, *ptag_counter);
}
++*ptag_counter;
} else if (strcmp(tag, "button") == 0) {
printf("panel%d.add(new JButton(\"%s\"));\n", *ptag_counter - 1, attributes[1]);
} else if (strcmp(tag, "label") == 0) {
printf("panel%d.add(new JLabel(\"%s\"));\n", *ptag_counter - 1, attributes[1]);
} else if (strcmp(tag, "textbox") == 0) {
printf("panel%d.add(new JTextField());\n", *ptag_counter - 1);
} else if (strcmp(tag, "textarea") == 0) {
printf("panel%d.add(new JTextArea());\n", *ptag_counter - 1);
}
}
void close_handler(void *data,
const char *tag) {
int *ptag_counter = (int *) data;
/* printf("close tag: %s\n", tag); */
if (strcmp("panel", tag) == 0) {
--*ptag_counter;
printf("}\n");
}
}
int main(int argc, char **argv) {
int tag_counter = 0;
XML_Parser parser = XML_ParserCreate(NULL);
XML_SetElementHandler(parser, open_handler, close_handler);
XML_SetUserData(parser, &tag_counter);
const int BUFFER_SIZE = 8192;
char buffer[BUFFER_SIZE];
FILE *xml_file = fopen(argv[1], "r");
int is_eof = 0;
while (!is_eof) {
int length = fread(buffer, sizeof(char), BUFFER_SIZE, xml_file);
is_eof = feof(xml_file);
XML_Parse(parser, buffer, length, is_eof);
}
fclose(xml_file);
XML_ParserFree(parser);
return 0;
}
EmailClient.java
package lecture12;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class EmailClient extends JFrame {
public EmailClient() {
super();
{
JPanel panel0 = new JPanel();
panel0.setLayout(new BoxLayout(panel0, BoxLayout.PAGE_AXIS));
add(panel0);
{
JPanel panel1 = new JPanel();
panel1.setLayout(new BoxLayout(panel1, BoxLayout.LINE_AXIS));
panel0.add(panel1);
panel1.add(new JLabel("To:"));
panel1.add(new JTextField());
}
{
JPanel panel1 = new JPanel();
panel1.setLayout(new BoxLayout(panel1, BoxLayout.LINE_AXIS));
panel0.add(panel1);
panel1.add(new JLabel("Subject:"));
panel1.add(new JTextField());
}
panel0.add(new JTextArea());
panel0.add(new JButton("Send"));
}
setSize(512, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new EmailClient();
}
}
Haiku
Just those who salvage
Those who see, accept, and fix
Only they have peace
Those who see, accept, and fix
Only they have peace