/Users/lyon/j4p/src/gui/run/RunButton.java
|
1 package gui.run;
2
3 import futils.FileList;
4 import gui.ClosableJFrame;
5 import gui.layouts.Alignable;
6
7 import javax.swing.Box;
8 import javax.swing.BoxLayout;
9 import javax.swing.Icon;
10 import javax.swing.JButton;
11 import java.awt.Container;
12 import java.awt.Dimension;
13 import java.awt.FlowLayout;
14 import java.awt.GridLayout;
15 import java.awt.event.ActionEvent;
16 import java.awt.event.ActionListener;
17
18 public abstract class RunButton extends JButton
19 implements ActionListener, Alignable,
20 Runnable {
21 public RunButton(String label) {
22 this(label, null);
23 }
24
25 private int alignment = Alignable.CENTER;
26
27 public void setAlignment(int alignment) {
28 this.alignment = alignment;
29 }
30
31 public int getAlignment() {
32 return alignment;
33 }
34
35 public RunButton(String l, Icon i) {
36 super(l, i);
37 addActionListener(this);
38 ShortcutUtils.addShortcut(this);
39 setToolTipText(getText());
40 }
41
42 public RunButton(Icon i) {
43 this(null, i);
44 }
45
46 public RunButton() {
47 this(null, null);
48 }
49
50 public void actionPerformed(ActionEvent e) {
51 run();
52 }
53
54 public static void main(String args[]) {
55 // anonymous inner class
56 // That uses the command pattern
57 // also uses adapter pattern
58 // since the normal
59 // requires an
60 // actionListener-
61 // actionPerformed(ActionEvent e)
62 // now we just need a run method.
63 // Semantics for the runButton now include
64 // an implicit metaChar='['
65
66
67 final ClosableJFrame cf =
68 new ClosableJFrame("OK-CANCEL Frame");
69 final Container c = cf.getContentPane();
70 c.setLayout(new BoxLayout(c, BoxLayout.PAGE_AXIS));
71 c.add(new RunButton("[boxLayout-pageAxis") {
72 public void run() {
73 c.setLayout(new BoxLayout(c, BoxLayout.PAGE_AXIS));
74 cf.pack();
75 }
76 });
77 c.add(new RunButton("[boxLayout-LINE_AXIS") {
78 public void run() {
79 c.setLayout(new BoxLayout(c, BoxLayout.LINE_AXIS));
80 cf.pack();
81 }
82 });
83 c.add(new RunButton("[boxLayout-X_AXIS") {
84 public void run() {
85 c.setLayout(new BoxLayout(c, BoxLayout.X_AXIS));
86 cf.pack();
87 }
88 });
89 c.add(new RunButton("[boxLayout-Y_AXIS") {
90 public void run() {
91 BoxLayout boxLayout = new BoxLayout(c, BoxLayout.Y_AXIS);
92 c.setLayout(boxLayout);
93 cf.pack();
94 }
95 });
96 c.add(new RunButton("[FlowLayout") {
97 public void run() {
98 c.setLayout(new FlowLayout());
99 cf.pack();
100 }
101 });
102 c.add(new RunButton("[GridLayout(2,0)") {
103 public void run() {
104 c.setLayout(new GridLayout(2, 0));
105 cf.pack();
106 }
107 });
108 c.add(new RunButton("[GridLayout(1,0)") {
109 public void run() {
110 c.setLayout(new GridLayout(1, 0));
111 cf.pack();
112 }
113 });
114 c.add(new RunButton("[GridLayout(3,0)") {
115 public void run() {
116 c.setLayout(new GridLayout(3, 0));
117 cf.pack();
118 }
119 });
120 c.add(Box.createRigidArea(new Dimension(50, 50)));
121 c.add(new RunButton("[exit") {
122 public void run() {
123 cf.hide();
124 System.exit(0);
125 }
126 });
127 c.add(new RunButton("[delete all class files!") {
128 public void run() {
129 FileList fl = new FileList();
130 fl.deleteAllClassFiles();
131 }
132 });
133 cf.setSize(200, 200);
134 cf.show();
135 }
136 }