Ollie following are two classes that I have created inorder to meet my requirements. Note I don't need messageArea pane for Panel.
1. ApplicationPanel Class
Code:
public abstract class ApplicationPanel extends AbstractControlFactory implements Guarded {
protected final Log logger = LogFactory.getLog(getClass());
protected static final String DEFAULT_FINISH_KEY = "applyCommand";
protected static final String DEFAULT_CANCEL_KEY = "revertCommand";
protected static final String DEFAULT_HELP_KEY = "helpCommand";
private boolean defaultEnabled = false;
private ActionCommand finishCommand;
private ActionCommand cancelCommand;
private ActionCommand helpCommand;
private CommandGroup panelCommandGroup;
/**
* Should the finish button be enabled by default?
*
* @param enabled
* true or false
*/
public void setDefaultEnabled(boolean enabled) {
this.defaultEnabled = enabled;
}
public boolean isEnabled() {
if (isControlCreated()) {
return finishCommand.isEnabled();
}
else {
return false;
}
}
public void setEnabled(boolean enabled) {
setFinishEnabled(enabled);
}
protected void setFinishEnabled(boolean enabled) {
if (isControlCreated()) {
finishCommand.setEnabled(enabled);
}
}
protected JComponent createControl() {
initStandardCommands();
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JComponent contentPane = createContentPane();
GuiStandardUtils.attachBorder(contentPane, Borders.EMPTY_BORDER);
panel.add(contentPane);
panel.add(createButtonBar(), BorderLayout.SOUTH);
return panel;
}
private void initStandardCommands() {
finishCommand = new ActionCommand(getFinishFaceConfigurationKey()) {
public void doExecuteCommand() {
try {
boolean result = onFinish();
if (result) {
/** @todo May be some notification */
}
}
catch (Exception e) {
logger.warn("Exception occured executing panel finish command.", e);
onFinishException(e);
}
}
};
finishCommand.setEnabled(defaultEnabled);
cancelCommand = new ActionCommand(getCancelFaceConfigurationKey()) {
public void doExecuteCommand() {
onCancel();
}
};
helpCommand = new ActionCommand(getHelpFaceConfigurationKey()) {
public void doExecuteCommand() {
onHelp();
}
};
}
protected void onFinishException(Exception e) {
String exceptionMessage;
if (e instanceof MessageSourceResolvable) {
exceptionMessage = getMessageSourceAccessor().getMessage((MessageSourceResolvable)e);
}
else {
exceptionMessage = e.getLocalizedMessage();
}
if (!StringUtils.hasText(exceptionMessage)) {
String defaultMessage =
"Unable to finish; an application exception occured.\nPlease contact your administrator.";
exceptionMessage = getMessageSourceAccessor().getMessage(
"applicationPanel.defaultFinishException", defaultMessage);
}
JOptionPane.showMessageDialog(getControl(), exceptionMessage, getApplicationName(),
JOptionPane.ERROR_MESSAGE);
}
protected String getFinishFaceConfigurationKey() {
return DEFAULT_FINISH_KEY;
}
protected String getCancelFaceConfigurationKey() {
return DEFAULT_CANCEL_KEY;
}
protected String getHelpFaceConfigurationKey() {
return DEFAULT_HELP_KEY;
}
/**
* Return a standardized row of command buttons, right-justified and all of
* the same size, with Apply as the default button, and no mnemonics used, as
* per the Java Look and Feel guidelines.
*/
protected JComponent createButtonBar() {
this.panelCommandGroup = CommandGroup.createCommandGroup(null, getCommandGroupMembers());
JComponent buttonBar = this.panelCommandGroup.createButtonBar();
GuiStandardUtils.attachBorder(buttonBar, Borders.DIALOG_BORDER);
return buttonBar;
}
protected ActionCommand getFinishCommand() {
return finishCommand;
}
protected ActionCommand getCancelCommand() {
return cancelCommand;
}
protected ActionCommand getHelpCommand() {
return helpCommand;
}
/**
* Template getter method to return the commands to populate the panel
* button bar.
*
* @return The array of commands (may also be a separator or glue identifier)
*/
protected Object[] getCommandGroupMembers() {
return new AbstractCommand[] {
getFinishCommand(), getCancelCommand(), getHelpCommand()};
}
protected void onHelp() {
}
/**
* Template lifecycle method invoked after the dialog control is
* initialized.
*/
protected void onInitialized() {
}
/**
* Return the GUI which allows the user to manipulate the business objects
* related to this panel; this GUI will be placed above the <code>OK</code>
* and <code>Cancel</code> buttons, in a standard manner.
*/
protected abstract JComponent createContentPane();
/**
* Request invocation of the action taken when the user hits the
* <code>Apply</code> (finish) button.
*
* @return true if action completed succesfully; false otherwise.
*/
protected abstract boolean onFinish();
/**
* Request invocation of the action taken when the user hits the
* <code>Cancel</code> (cancel) button.
*
*/
protected abstract void onCancel();
}
2. PageApplicationPanel Class
Code:
public abstract class PageApplictionPanel extends ApplicationPanel implements
PropertyChangeListener {
private DialogPage dialogPage;
public PageApplictionPanel(DialogPage dialogPage) {
super();
setDialogPage(dialogPage);
}
protected DialogPage getDialogPage() {
return dialogPage;
}
private void setDialogPage(DialogPage dialogPage) {
this.dialogPage = dialogPage;
}
protected JComponent createContentPane() {
dialogPage.addPropertyChangeListener(this);
return dialogPage.getControl();
}
public void propertyChange(PropertyChangeEvent e) {
if (DialogPage.PAGE_COMPLETE_PROPERTY.equals(e.getPropertyName())) {
setEnabled(dialogPage.isPageComplete());
}
}
Usage :
Code:
PageApplictionPanel pageApplictionPanel = new PageApplictionPanel(tabbedPage) {
protected boolean onFinish() {
globalUserFormModel.commit();
return true;
}
protected void onCancel() {
globalUserFormModel.revert(); // Note : this doesn't currently work If I have two list boxes and I am copying from one (avilable) to (selected) other..
}
};
Only problem I have now is from Keith's class (above pasted) there is no way of reverting since we are adding value to list directly on Add action. There is some BufferedList stuff, I wonder if that can help me here.? Anyone?
Amad