/** * Copyright 1999-2009 The Pegadi Team * * 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. */ /** * Dialog for preferences. * * @author HÃ¥vard Wigtil <havardw at pvv.org> */ package org.pegadi.swing; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Properties; import java.util.ResourceBundle; public abstract class AbstractPreferencesDialog extends JDialog { protected Logger log = LoggerFactory.getLogger(getClass()); /** The preferences, as given to this dialog. */ protected Properties prefs; /** The preferences that were changed. This is only set after the dialog is closed. */ protected Properties changedPrefs; /** <code>true</code> if OK was pressed to close the dialog. */ protected boolean okResult; /** UI strings. */ protected ResourceBundle apdStrings; /** * Create a new dialog without any preferences set. */ public AbstractPreferencesDialog() { this(null, new Properties()); } /** * Creates a new dialog, and calls <code>setPreferences</code> * to populate the dialog with existing / default preferences. * The dialog will be modal by default. */ public AbstractPreferencesDialog(Frame parent, Properties preferences) { super(parent, true); changedPrefs = null; okResult = false; apdStrings = ResourceBundle.getBundle("org.pegadi.swing.AbstractPreferencesDialog"); createUI(); setPreferences(preferences); } /** * Returns the changed preferences. Will return <code>null</code> if called * before the dialog is closed, or if called when the dialog was cancelled. * When "OK" was pressed, this will always return a valid * <code>Properties</code> object, even when no preferences were changed. * * @return changed preferences, or <code>null</code>. */ public Properties getChangedPreferences() { return changedPrefs; } /** * Returns <code>true</code> if OK was pressed to close the dialog. */ public boolean okResult() { return okResult; } /** * Create the user interface. This will create the buttons at the bottom * of the dialog and insert the component from * <code>createPreferencesPane</code>. * * @see #createPreferencesPane */ protected void createUI() { setTitle(apdStrings.getString("dialog_title")); JButton okButton = new JButton(apdStrings.getString("button_OK")); okButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { okButton_actionPerformed(e); } }); JButton cancelButton = new JButton(apdStrings.getString("button_cancel")); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { cancelButton_actionPerformed(e); } }); JPanel buttonPanel = new JPanel(new FlowLayout()); buttonPanel.add(cancelButton); buttonPanel.add(okButton); getContentPane().add(buttonPanel, BorderLayout.SOUTH); getContentPane().add(createPreferencesPane(), BorderLayout.CENTER); pack(); } /** * Called when the "OK" button is pressed. This method will call * <code>findChangedPreferences</code> gather all changed * preferences. * * @see #findChangedPreferences * @param e The event from the press. */ protected void okButton_actionPerformed(ActionEvent e) { findChangedPreferences(); okResult = true; dispose(); } /** * Called when the "Cancel" button is pressed. * * @param e The event from the press. */ protected void cancelButton_actionPerformed(ActionEvent e) { dispose(); } /** * This method will be called to create the body of the dialog, * the part added above the buttons. * * @return The component that will be added. */ protected abstract Component createPreferencesPane(); /** * Set preferences controls according to this <code>Properties</code>. * The <code>Properties</code> object will also be set as the new * <code>prefs</code> variable. */ public abstract void setPreferences(Properties preferences); /** * Called when "OK" is pressed to determine which preferences are changed. * All changed preferences will be placed in <code>changedPrefs</code>, which * prior to this call will be <code>null</code>. */ protected abstract void findChangedPreferences(); }