 |
Home | Changes | Index | Search | Go
The "layout shootout" had me curious about how well my autobuild/autolayout code could perform the same task. I'm pleased with the results, and it's pointed out some interesting directions for the code.
It's not quite ready for public consumption yet, but there's no magic going on here so I'm sure others can easily recreate it. What you see below is the entire code; the auto code is inferring a number of visual, structural, and event aspects automatically. See if you can figure out how!
Several styles of layout and structuring are supported. They can be freely intermixed, and you can always revert to the "old school" when necessary.
- Components are created automatically based on their type. "JTextField name = new JTextField?()" isn't necessary.
- The "Labels" annotation tells autobuild to make labels for the components. Field names are used to generate these, and that works well in most cases. It also encourages good field naming.
- Layouts are inferred based on the type of constraint. If you specify that a child component has a Border(CENTER) layout, the parent container is set to a BorderLayout?.
- Method signatures are used for event handling. If a component has a method with a signature that matches an event handler it exhibits, that method is wired in automatically.
- Inner classes generate the containment structure used; child components are inner objects.
package test.com.soletta.auto;
import static com.soletta.auto.Auto.onListSelectCopy;
import static com.soletta.auto.ScrollPolicy.VAlways;
import static java.awt.BorderLayout.*;
import static javax.swing.JOptionPane.showMessageDialog;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import javax.swing.*;
import com.soletta.auto.*;
public class Shootout extends JFrame {
@Empty(10) JPanel
content = new JPanel() {
@Border(WEST) @VScroll(VAlways) JScrollPane
listScroll = new JScrollPane() {
JList cl = characterList = new JList(names);
};
@Border(CENTER) JPanel
mainPanel = new JPanel() {
@Border(CENTER) @Labels JPanel
formPanel = new JPanel() {
@Row JTextField lastName; JTextField firstName;
@Row JTextField phone; JTextField email;
@Row @Rest JTextField address1,
address2;
@Row JTextField city;
@Row JTextField state; JTextField postalCode;
@Row JTextField country;
};
@Border(SOUTH) JPanel
buttonPanel = new JPanel() {
JButton newButton, deleteButton, editAddress, save;
JButton cancel = new JButton("Cancel") {
public void actionPerformed(ActionEvent e) {
showMessageDialog(this, "Cancel");
}
};
};
};
};
@Skip JList characterList;
public Shootout() {
super("Shootout");
}
public void windowOpened(WindowEvent e) {
onListSelectCopy(characterList, "source.selectedValue", this, "title");
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public void editAddress() {
}
public void newButton() {
showMessageDialog(this, "New!");
}
@SuppressWarnings("serial")
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
Shootout shootFrame = new Shootout();
Auto.auto(shootFrame, shootFrame.getClass().getPackage());
shootFrame.setSize(800, 600);
shootFrame.setVisible(true);
}
static String[] names={
"Bunny, Bugs",
"Cat, Sylvester ",
"Coyote, Willie E. ",
"Devil, Tasmanian",
"Duck, Daffy",
"Fudd, Elmer",
"LePew, Pepe",
"Martian, Marvin"
};
}
|