1998-09-09 04:52:38 +04:00
|
|
|
/* -*- Mode: java; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License
|
|
|
|
* Version 1.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.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS"
|
|
|
|
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
|
|
|
|
* the License for the specific language governing rights and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
* The Original Code is the Grendel mail/news client.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape Communications
|
|
|
|
* Corporation. Portions created by Netscape are Copyright (C) 1997
|
|
|
|
* Netscape Communications Corporation. All Rights Reserved.
|
1999-01-09 06:55:32 +03:00
|
|
|
*
|
1999-03-07 22:58:40 +03:00
|
|
|
* Contributors: Jeff Galyan <talisman@anamorphic.com>
|
|
|
|
* Giao Nguyen <grail@cafebabe.org>
|
1998-09-09 04:52:38 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
package grendel.ui;
|
|
|
|
|
1999-02-23 06:44:34 +03:00
|
|
|
import java.beans.SimpleBeanInfo;
|
|
|
|
import java.beans.PropertyDescriptor;
|
|
|
|
import java.beans.PropertyEditor;
|
|
|
|
|
|
|
|
import java.awt.Container;
|
|
|
|
import java.awt.BorderLayout;
|
|
|
|
import java.awt.FlowLayout;
|
|
|
|
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
|
|
|
|
import javax.swing.JButton;
|
1999-01-09 06:55:32 +03:00
|
|
|
import javax.swing.JFrame;
|
|
|
|
import javax.swing.JOptionPane;
|
1999-02-23 06:44:34 +03:00
|
|
|
import javax.swing.JDialog;
|
|
|
|
import javax.swing.JTabbedPane;
|
|
|
|
import javax.swing.JPanel;
|
1998-09-09 04:52:38 +04:00
|
|
|
|
|
|
|
import calypso.util.Preferences;
|
|
|
|
import calypso.util.PreferencesFactory;
|
|
|
|
|
|
|
|
import grendel.prefs.Prefs;
|
|
|
|
|
1999-02-25 10:44:11 +03:00
|
|
|
/**
|
|
|
|
* This class handles preference dialog box handling for Grendel. Why
|
|
|
|
* was all this stuff static?
|
|
|
|
*/
|
|
|
|
public class PrefsDialog
|
|
|
|
extends JDialog {
|
|
|
|
public static String OK = "Okay";
|
|
|
|
public static String CANCEL = "Cancel";
|
|
|
|
JTabbedPane pane;
|
|
|
|
PropertyEditor[] editors;
|
1998-09-09 04:52:38 +04:00
|
|
|
|
|
|
|
public static void CheckPrefs(JFrame aParent) {
|
|
|
|
if (!ValidPrefs()) {
|
1999-02-25 10:44:11 +03:00
|
|
|
new PrefsDialog(aParent);
|
1998-09-09 04:52:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-02-25 10:44:11 +03:00
|
|
|
public PrefsDialog(JFrame aParent) {
|
1998-09-09 04:52:38 +04:00
|
|
|
Object objs[] = {new Prefs()};
|
1999-02-25 10:44:11 +03:00
|
|
|
propertyEditorDialog(aParent, new Prefs());
|
1998-09-09 04:52:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean ValidPrefs() {
|
|
|
|
Preferences prefs = PreferencesFactory.Get();
|
|
|
|
|
|
|
|
boolean res = true;
|
1999-02-25 10:44:11 +03:00
|
|
|
|
1998-09-09 04:52:38 +04:00
|
|
|
return res;
|
|
|
|
}
|
1999-02-23 06:44:34 +03:00
|
|
|
|
1999-02-25 10:44:11 +03:00
|
|
|
private void propertyEditorDialog(JFrame parent, Object obj) {
|
1999-02-23 06:44:34 +03:00
|
|
|
Class reference = obj.getClass();
|
|
|
|
String name = reference.getName();
|
1999-02-25 10:44:11 +03:00
|
|
|
Container content = getContentPane();
|
|
|
|
String[] labels = {OK, CANCEL};
|
|
|
|
pane = new JTabbedPane();
|
1999-02-23 06:44:34 +03:00
|
|
|
|
1999-02-25 10:44:11 +03:00
|
|
|
// gui handling
|
1999-02-23 06:44:34 +03:00
|
|
|
content.setLayout(new BorderLayout());
|
|
|
|
content.add(pane, BorderLayout.CENTER);
|
|
|
|
content.add(buttonPanel(labels,
|
1999-02-25 10:44:11 +03:00
|
|
|
new PrefsDialogListener()),
|
1999-02-23 06:44:34 +03:00
|
|
|
BorderLayout.SOUTH);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// get the beaninfo
|
1999-02-25 10:44:11 +03:00
|
|
|
Object o;
|
|
|
|
SimpleBeanInfo info;
|
|
|
|
Class beanie = Class.forName(name + "BeanInfo");
|
|
|
|
PropertyDescriptor[] desc;
|
1999-02-23 06:44:34 +03:00
|
|
|
o = beanie.newInstance();
|
|
|
|
if (!(o instanceof SimpleBeanInfo)) return; // bummer
|
|
|
|
|
|
|
|
// process the descriptors
|
|
|
|
info = (SimpleBeanInfo)o;
|
|
|
|
desc = info.getPropertyDescriptors();
|
1999-02-25 10:44:11 +03:00
|
|
|
editors = new PropertyEditor[desc.length];
|
1999-02-23 06:44:34 +03:00
|
|
|
|
|
|
|
for (int i = 0; i < desc.length; i++) {
|
|
|
|
o = desc[i].getPropertyEditorClass().newInstance();
|
|
|
|
if (!(o instanceof PropertyEditor)) continue;
|
1999-02-25 10:44:11 +03:00
|
|
|
editors[i] = (PropertyEditor)o;
|
|
|
|
pane.add(desc[i].getDisplayName(), editors[i].getCustomEditor());
|
1999-02-23 06:44:34 +03:00
|
|
|
}
|
1999-02-25 10:44:11 +03:00
|
|
|
pack();
|
|
|
|
setVisible(true);
|
1999-02-23 06:44:34 +03:00
|
|
|
} catch (Exception e) {
|
|
|
|
System.out.println("welp, that sucked. beans is broke. And stuff");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-02-25 10:44:11 +03:00
|
|
|
private JPanel buttonPanel(String[] labels,
|
|
|
|
ActionListener listener) {
|
1999-02-23 06:44:34 +03:00
|
|
|
JPanel panel = new JPanel();
|
|
|
|
panel.setLayout(new FlowLayout());
|
|
|
|
|
|
|
|
if (labels != null) {
|
|
|
|
for (int i = 0; i < labels.length; i++) {
|
|
|
|
JButton button = new JButton(labels[i]);
|
1999-02-25 10:44:11 +03:00
|
|
|
button.setActionCommand(labels[i]);
|
1999-02-23 06:44:34 +03:00
|
|
|
if (listener != null) button.addActionListener(listener);
|
|
|
|
panel.add(button);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return panel;
|
|
|
|
}
|
1998-09-09 04:52:38 +04:00
|
|
|
|
1999-02-25 10:44:11 +03:00
|
|
|
class PrefsDialogListener
|
|
|
|
implements ActionListener {
|
|
|
|
JDialog dialog;
|
|
|
|
|
|
|
|
public PrefsDialogListener() {
|
|
|
|
dialog = PrefsDialog.this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void actionPerformed(ActionEvent event) {
|
|
|
|
String command = event.getActionCommand();
|
|
|
|
Prefs prefs = new Prefs();
|
|
|
|
if (command.equals(PrefsDialog.OK)) {
|
|
|
|
for (int i = 0; i < editors.length; i++) {
|
|
|
|
prefs.set(editors[i].getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dialog.setVisible(false);
|
|
|
|
}
|
1999-02-23 06:44:34 +03:00
|
|
|
}
|
|
|
|
}
|