1 |
package skrueger.swing; |
2 |
|
3 |
import java.awt.Component; |
4 |
import java.awt.event.ActionEvent; |
5 |
|
6 |
import javax.swing.AbstractAction; |
7 |
import javax.swing.Action; |
8 |
import javax.swing.Icon; |
9 |
import javax.swing.JTabbedPane; |
10 |
|
11 |
import net.miginfocom.swing.MigLayout; |
12 |
import schmitzm.swing.JPanel; |
13 |
|
14 |
public abstract class CancellableTabbedDialogAdapter extends |
15 |
CancellableDialogAdapter { |
16 |
|
17 |
private final JTabbedPane tabbedPane; |
18 |
|
19 |
public CancellableTabbedDialogAdapter(Component owner, String title) { |
20 |
super(owner, title); |
21 |
|
22 |
/** |
23 |
* Prepare buttons |
24 |
*/ |
25 |
final JPanel buttons = createButtons(); |
26 |
|
27 |
/*** Build GUI ***/ |
28 |
tabbedPane = new JTabbedPane() { |
29 |
@Override |
30 |
public void insertTab(String title, Icon icon, Component component, |
31 |
String tip, int index) { |
32 |
super.insertTab(title, icon, component, tip, index); |
33 |
CancellableTabbedDialogAdapter.this.pack(); |
34 |
} |
35 |
}; |
36 |
|
37 |
/** |
38 |
* Building the content pane |
39 |
*/ |
40 |
final JPanel contentPane = new JPanel(new MigLayout("wrap 1")); |
41 |
contentPane.add(getTabbedPane()); |
42 |
contentPane.add(buttons); |
43 |
|
44 |
setContentPane(contentPane); |
45 |
|
46 |
} |
47 |
|
48 |
public CancellableTabbedDialogAdapter(Component parentWindow) { |
49 |
this(parentWindow, null); |
50 |
} |
51 |
|
52 |
/** |
53 |
* Is only called once! Doesn't use lazy initialization. Use |
54 |
* <code>super.createButtons.add( newButton )</code> to add buttons. |
55 |
*/ |
56 |
protected JPanel createButtons() { |
57 |
final JPanel buttonsJPanel = new JPanel(new MigLayout()); |
58 |
|
59 |
final OkButton okButton = new OkButton(new AbstractAction() { |
60 |
{ |
61 |
// Set a mnemonic character. In most look and feels, this |
62 |
// causes the |
63 |
// specified character to be underlined This indicates that |
64 |
// if the component |
65 |
// using this action has the focus and In some look and |
66 |
// feels, this causes |
67 |
// the specified character in the label to be underlined and |
68 |
putValue(Action.MNEMONIC_KEY, new Integer( |
69 |
java.awt.event.KeyEvent.VK_E)); |
70 |
|
71 |
// Set tool tip text |
72 |
putValue(Action.SHORT_DESCRIPTION, |
73 |
"Accept the changes made in this dialog."); // i8n i8n |
74 |
// i8n |
75 |
} |
76 |
|
77 |
// |
78 |
public void actionPerformed(final ActionEvent evt) { |
79 |
okClose(); |
80 |
} |
81 |
|
82 |
}); |
83 |
|
84 |
buttonsJPanel.add(okButton, "tag ok"); |
85 |
|
86 |
final CancelButton cancelButton = new CancelButton(new AbstractAction( |
87 |
"") { |
88 |
public void actionPerformed(final ActionEvent evt) { |
89 |
cancelClose(); |
90 |
} |
91 |
}); |
92 |
buttonsJPanel.add(cancelButton, "tag cancel"); |
93 |
|
94 |
return buttonsJPanel; |
95 |
} |
96 |
|
97 |
public JTabbedPane getTabbedPane() { |
98 |
return tabbedPane; |
99 |
} |
100 |
|
101 |
/** |
102 |
* Calling cancel() will call cancel to all {@link Cancellable} children in |
103 |
* the tabbedPane. |
104 |
*/ |
105 |
@Override |
106 |
public void cancel() { |
107 |
cancelled = true; |
108 |
|
109 |
for (int tIdx = 0; tIdx < tabbedPane.getTabCount(); tIdx++) { |
110 |
final Component tab = tabbedPane.getComponentAt(tIdx); |
111 |
if (tab instanceof Cancellable) { |
112 |
((Cancellable) tab).cancel(); |
113 |
} |
114 |
} |
115 |
} |
116 |
|
117 |
@Override |
118 |
public boolean okClose() { |
119 |
// TranslationAskJDialog.this.firePropertyChange( |
120 |
// PROPERTY_APPLY_AND_CLOSE, null, null); |
121 |
// |
122 |
for (int tIdx = 0; tIdx < tabbedPane.getTabCount(); tIdx++) { |
123 |
final Component tab = tabbedPane.getComponentAt(tIdx); |
124 |
if (tab instanceof Checkable) { |
125 |
if (!((Checkable) tab).checkValidInputs()) |
126 |
return false; |
127 |
} |
128 |
} |
129 |
|
130 |
dispose(); |
131 |
|
132 |
return true; |
133 |
} |
134 |
} |