/*
 * Copyright 2002-2004 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.springframework.richclient.preference;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;

import org.springframework.richclient.dialog.AbstractDialogPage;
import org.springframework.richclient.layout.GridBagLayoutBuilder;
import org.springframework.richclient.settings.Settings;
import org.springframework.util.Assert;

public abstract class PreferencePage extends AbstractDialogPage {

	private JButton applyButton;

	private boolean createApplyAndDefaultButtons = true;

	private JButton defaultsButton;

	private PreferencePage parent;

	private PreferenceDialog preferenceDialog;

	public PreferencePage(String id) {
		super(id);
	}

	private JComponent createButtons() {
		JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));

		defaultsButton = new JButton("Restore defaults");
		defaultsButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				onDefaults();
			}
		});

		applyButton = new JButton("Apply");
		applyButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				onApply();
			}
		});

		panel.add(defaultsButton);
		panel.add(applyButton);

		return panel;
	}

	protected abstract JComponent createContents();

	protected final JComponent createControl() {
		GridBagLayoutBuilder builder = new GridBagLayoutBuilder();

		// JPanel panel = new JPanel(new BorderLayout());

		JComponent contents = createContents();
		Assert.notNull(contents, "Contents cannot be null.");
		// panel.add(contents);
		builder.append(contents, 1, 1, true, true);

		if (createApplyAndDefaultButtons) {
			builder.nextLine();
			builder.append(createButtons());
			// panel.add(createButtons(), BorderLayout.SOUTH);
		}

		// return panel;
		return builder.getPanel();
	}

	public PreferencePage getParent() {
		return parent;
	}

	protected Settings getSettings() {
		return preferenceDialog.getSettings();
	}

	/**
	 * Must store the preference values in the PreferenceStore. Does not save
	 * the PreferenceStore. Subclasses should override this method.
	 */
	protected void onApply() {
		onFinish();
	}

	protected void onDefaults() {
	}

	/**
	 * Notification that the user clicked the OK button on the PreferenceDialog.
	 */
	protected boolean onFinish() {
		return true;
	}

	public void setCreateApplyAndDefaultButtons(boolean create) {
		createApplyAndDefaultButtons = create;
	}

	public void setParent(PreferencePage parent) {
		this.parent = parent;
	}

	public void setPreferenceDialog(PreferenceDialog dialog) {
		Assert.notNull(dialog);
		preferenceDialog = dialog;
	}
}
@@ -679,185 +679,149 @@
 java
-.awt.FlowLayou
+x.swing.JComponen
 t;%0A
+%0A
 import 
-java.awt.event.ActionEvent;%0Aimport java.awt.event.ActionListener;%0A%0Aimport javax.swing.JButton;%0Aimport javax.swing.JComponent;%0Aimport javax.swing.JPanel;%0A
+org.springframework.richclient.command.ActionCommand;%0Aimport org.springframework.richclient.command.CommandGroup;
 %0Aimp
@@ -1006,16 +1006,77 @@
 ttings;%0A
+import org.springframework.richclient.util.GuiStandardUtils;%0A
 import o
@@ -1179,39 +1179,8 @@
  %7B%0A%0A
-%09private JButton applyButton;%0A%0A
 %09pri
@@ -1233,43 +1233,8 @@
 ue;%0A
-%0A%09private JButton defaultsButton;%0A%0A
 %09pri
@@ -1261,17 +1261,16 @@
 parent;%0A
-%0A
 %09private
@@ -1304,16 +1304,100 @@
 eDialog;
+%0A%09private ActionCommand restoreDefaultsCommand;%0A%09private ActionCommand applyCommand;
 %0A%0A%09publi
@@ -1487,181 +1487,79 @@
 %7B%0A%09%09
-JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));%0A%0A%09%09defaultsButton = new JButton(%22R
+restoreDefaultsCommand = new ActionCommand(%22r
 estore
- d
+D
 efaults
-%22);%0A%09%09defaultsButton.addActionListener(new ActionListener(
+Command%22
 ) %7B%0A
-%0A
 %09%09%09p
@@ -1573,37 +1573,25 @@
 oid 
-actionPerformed(ActionEvent e
+doExecuteCommand(
 ) %7B%0A
@@ -1612,25 +1612,24 @@
 ();%0A%09%09%09%7D%0A%09%09%7D
-)
 ;%0A%0A%09%09applyBu
@@ -1630,95 +1630,54 @@
 pply
-Button = new JButton(%22Apply%22);%0A%09%09applyButton.addActionListener(new ActionListener(
+Command = new ActionCommand(%22applyCommand%22
 ) %7B%0A
-%0A
 %09%09%09p
@@ -1691,37 +1691,25 @@
 oid 
-actionPerformed(ActionEvent e
+doExecuteCommand(
 ) %7B%0A
@@ -1735,68 +1735,246 @@
 %0A%09%09%7D
-)
 ;%0A%0A%09%09
-panel.add(defaults
+CommandGroup commandGroup = CommandGroup.createCommandGroup(null,%0A%09%09%09%09new Object%5B%5D %7B restoreDefaultsCommand, applyCommand %7D);%0A%09%09JComponent buttonBar = commandGroup.create
 Button
+Bar(
 );%0A%09%09
-panel.add(applyB
+GuiStandardUtils.attachDialogBorder(b
 utton
+Bar
 );%0A
-%0A
 %09%09re
@@ -1978,21 +1978,25 @@
 %09return 
-panel
+buttonBar
 ;%0A%09%7D%0A%0A%09p

fix for RCP-134: missing i18n in PreferencePage