/*
* Copyright Oct 18, 2010 John T. Langton
* email: jlangton at visitrend dot com
* www.visitrend.com
*
* License: GPLv2 or (at your option) any later GPL version
*
* This file is part of NDVis.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* There should be a copy of the GNU General Public License applied to
* NDVis in the file "NDVis-license" in the folder "license". If not, see
* <http://www.gnu.org/licenses/> or write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package com.visitrend.ndvis.preferences;
import com.visitrend.ndvis.preferences.spi.NDVisPreferences;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import org.openide.util.Lookup;
import org.openide.windows.TopComponent;
/**
* @author Diane Kramer - dkramer at visitrend dot com
*/
/**
* I believe this is still to be ported the the preferences API of NB RCP.
* In the case this class becomes very small after porting, this module
* could be merged with Core.
*
* @author ao
*/
public class PreferencesDialog extends JDialog {
private static PreferencesDialog instance;
public static final int OK = 0;
public static final int CANCEL = 1;
private int exitValue;
// GUI components
private JCheckBox showDBConnect;
private PreferencesDialog(Component parent) {
super(JOptionPane.getFrameForComponent(parent), "Set Preferences", true);
NDVisPreferences prefs = Lookup.getDefault().lookup(NDVisPreferences.class);
JPanel checkBoxPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
showDBConnect = new JCheckBox("Show Connect to Database Message");
showDBConnect.setSelected(prefs.getShowDBConnectPref());
checkBoxPanel.add(showDBConnect);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
// make the actions and buttons
AbstractAction okAction = new AbstractAction("OK") {
public void actionPerformed(ActionEvent e) {
exitValue = OK;
setVisible(false);
}
};
JButton okButton = new JButton(okAction);
buttonPanel.add(okButton);
AbstractAction cancelAction = new AbstractAction("Cancel") {
public void actionPerformed(ActionEvent e) {
exitValue = CANCEL;
setVisible(false);
}
};
JButton cancelButton = new JButton(cancelAction);
buttonPanel.add(cancelButton);
setLayout(new BorderLayout());
add(checkBoxPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
pack();
}
/**
*
* @param comp the parent component to this dialog
* @return whether the user clicked cancelled or not
*/
public static PreferencesDialog showDialog(Component comp) {
if (instance == null) {
instance = new PreferencesDialog(comp);
}
instance.setVisible(true);
return instance;
}
public int getExitValue() {
return exitValue;
}
public boolean getShowDBConnect() {
return showDBConnect.isSelected();
}
public static void main(String[] args) {
PreferencesDialog dialog = PreferencesDialog.showDialog(null);
int exitValue = dialog.getExitValue();
if (exitValue == PreferencesDialog.OK) {
System.out.println("Dialog exited with OK value");
} else if (exitValue == PreferencesDialog.CANCEL) {
System.out.println("Dialog exited with CANCEL value");
} else {
throw new RuntimeException("Unknown exit value from PreferencesDialog: " + exitValue);
}
boolean showDBConnect = dialog.getShowDBConnect();
if (showDBConnect) {
System.out.println("Preference set to show DBConnect");
} else {
System.out.println("Preference set to NOT show DBConnect");
}
System.exit(0);
}
}