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