package skrueger.swing; import java.awt.Component; import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.Icon; import javax.swing.JTabbedPane; import net.miginfocom.swing.MigLayout; import schmitzm.swing.JPanel; public abstract class CancellableTabbedDialogAdapter extends CancellableDialogAdapter { private final JTabbedPane tabbedPane; public CancellableTabbedDialogAdapter(Component owner) { super(owner); /** * Prepare buttons */ final JPanel buttons = createButtons(); /*** Build GUI ***/ tabbedPane = new JTabbedPane(){ @Override public void insertTab(String title, Icon icon, Component component, String tip, int index) { super.insertTab(title, icon, component, tip, index); CancellableTabbedDialogAdapter.this.pack(); } }; /** * Building the content pane */ final JPanel contentPane = new JPanel(new MigLayout("wrap 1")); contentPane.add(getTabbedPane()); contentPane.add(buttons); setContentPane(contentPane); } /** * Is only called once! Doesn't use lazy initialization. Use * super.createButtons.add( newButton ) to add buttons. */ protected JPanel createButtons() { final JPanel buttonsJPanel = new JPanel(new MigLayout()); final OkButton okButton = new OkButton(new AbstractAction() { { // Set a mnemonic character. In most look and feels, this // causes the // specified character to be underlined This indicates that // if the component // using this action has the focus and In some look and // feels, this causes // the specified character in the label to be underlined and putValue(Action.MNEMONIC_KEY, new Integer( java.awt.event.KeyEvent.VK_E)); // Set tool tip text putValue(Action.SHORT_DESCRIPTION, "Accept the changes made in this dialog."); // i8n } // public void actionPerformed(final ActionEvent evt) { okClose(); } }); buttonsJPanel.add(okButton, "tag ok"); final CancelButton cancelButton = new CancelButton(new AbstractAction( "") { public void actionPerformed(final ActionEvent evt) { cancelClose(); } }); buttonsJPanel.add(cancelButton, "tag cancel"); return buttonsJPanel; } public JTabbedPane getTabbedPane() { return tabbedPane; } /** * Calling cancel() will call cancel to all {@link Cancellable} children in the tabbedPane. */ @Override public void cancel() { cancelled = true; for (int tIdx = 0; tIdx < tabbedPane.getTabCount(); tIdx++) { final Component tab = tabbedPane.getComponentAt(tIdx); if (tab instanceof Cancellable) { ((Cancellable) tab).cancel(); } } } @Override public boolean okClose() { // TranslationAskJDialog.this.firePropertyChange( // PROPERTY_APPLY_AND_CLOSE, null, null); // for (int tIdx = 0; tIdx < tabbedPane.getTabCount(); tIdx++) { final Component tab = tabbedPane.getComponentAt(tIdx); if (tab instanceof Checkable) { if (!((Checkable) tab).checkValidInputs()) return false; } } dispose(); return true; } }