package infosapient.ui;
/*
* Copyright (c) 2001, Workplace Performance Tools, All Rights Reserved.
* License to use this program is provided under the COMMON PUBLIC LICENSE 0.5
* THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS COMMON PUBLIC LICENSE
* ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.FilenameFilter;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.*;
import infosapient.control.InfoSapientController;
import infosapient.system.FzySystemComponent;
import infosapient.system.FSEvent;
import infosapient.system.FzySet;
import infosapient.system.FzyKnowledgebase;
import infosapient.system.FzyAttribute;
import infosapient.system.FzyRule;
import infosapient.system.Observer;
import infosapient.system.ObservableImpl;
/**
* InfoSapientLauncher is the knowledgebase browser and GUI for creating and modifing
* the knowledgebase.
* @author Michael McConnell
* @version $Revision: 1.1.1.1 $
*
* @package infosapient.ui
*/
public class InfoSapientLauncher
extends JFrame
implements InfoSapientFrameIF, Observer, java.io.Serializable {
protected transient FzyAttribute theAttribute = null;
protected transient RepaintManager repaintManager;
protected transient JLabel attrLbl;
protected transient JLabel setLbl;
protected transient JLabel ruleLbl;
protected transient JScrollPane attribsScrollPane;
protected transient JScrollPane setsScrollPane;
protected transient JScrollPane rulesScrollPane;
protected transient final JList attributeList = new JList();
protected transient final JList setsList = new JList();
protected transient final JList rulesList = new JList();
protected transient JMenuBar mainMenuBar;
protected transient JMenu fileMenu;
protected transient JMenuItem newKb;
protected transient JMenuItem openKb;
protected transient JMenuItem parseKb;
protected transient JMenuItem saveKb;
protected transient JMenuItem saveKbAs;
protected transient JMenuItem deleteKb;
protected transient JMenuItem graphKBMItem;
protected transient JMenuItem saveAsNameKb;
protected transient JMenuItem exitAL;
protected transient JMenuItem attrMenuItem;
protected transient JMenuItem setMenuItem;
protected transient JMenuItem ruleMenuItem;
protected transient JMenuItem inferMenuItem;
protected transient JMenuItem controlMenuItem;
protected transient JMenuItem helpMenuItem;
protected transient JMenuItem aboutMenuItem;
protected transient JMenu editMenu;
protected transient JMenu editorMenu;
protected transient JMenu menu3;
protected transient JLabel kbNameLbl;
protected transient JPanel panel1 = new JPanel();
protected transient Box labelsFields = new Box(BoxLayout.Y_AXIS);
protected transient JPanel panel2 = new JPanel(new GridLayout(1, 3, 2, 2));
protected transient JPanel panel3 = new JPanel(new GridLayout(2, 1, 2, 2));
protected transient JTextField kbNameFld = new JTextField();
protected transient infosapient.control.InfoSapientController controller;
protected transient FzyKnowledgebase theKB;
protected transient boolean openKB = false;
protected transient allActionListener myActionListener =
new allActionListener();
/**
* INNER CLASSES FOR COMPONENT CONTROL
*/
protected class allWinListener
extends WindowAdapter
implements java.io.Serializable {
public void windowClosing(WindowEvent wevnt) {
getController().handleFSEvent(new FSEvent(this, "reqtoclose", null));
}
}
protected class WriteKBHandler
implements ActionListener, java.io.FilenameFilter, java.io.Serializable {
java.awt.FileDialog openKBFileDialog;
java.awt.Frame myOwner;
String m_fileName = null;
String m_dirName = null;
WriteKBHandler(java.awt.Frame mo) {
myOwner = mo;
}
public boolean accept(java.io.File f, String n) {
if (n.endsWith(".xml"))
return true;
else if (n.endsWith(".XML"))
return true;
else
return false;
}
public void actionPerformed(ActionEvent aevt) {
openKB(true);
java.awt.FileDialog openKBFileDialog = new java.awt.FileDialog(myOwner);
openKBFileDialog.setFilenameFilter(this);
openKBFileDialog.setMode(java.awt.FileDialog.SAVE);
openKBFileDialog.setTitle("InfoSapient -- Open Knowledgebase");
openKBFileDialog.setVisible(true);
m_fileName = openKBFileDialog.getFile();
m_dirName = openKBFileDialog.getDirectory();
if (openKBFileDialog.getFile() != null)
getController().writeKB(m_dirName + m_fileName);
saveKb.setEnabled(false);
}
}
public class WriteAsKBHandler
implements ActionListener, java.io.FilenameFilter, java.io.Serializable {
java.awt.FileDialog openKBFileDialog;
java.awt.Frame myOwner;
String m_fileName = null;
String m_dirName = null;
WriteAsKBHandler(java.awt.Frame mo) {
myOwner = mo;
}
public boolean accept(java.io.File f, String n) {
if (n.endsWith(".xml"))
return true;
else if (n.endsWith(".XML"))
return true;
else
return false;
}
public void actionPerformed(ActionEvent aevt) {
openKB(true);
java.awt.FileDialog openKBFileDialog = new java.awt.FileDialog(myOwner);
openKBFileDialog.setFilenameFilter(this);
openKBFileDialog.setMode(java.awt.FileDialog.SAVE);
openKBFileDialog.setTitle("InfoSapient -- Save Knowledgebase As");
openKBFileDialog.setVisible(true);
m_fileName = openKBFileDialog.getFile();
m_dirName = openKBFileDialog.getDirectory();
if (openKBFileDialog.getFile() != null)
getController().writeKB(m_dirName + m_fileName);
}
}
public class ReadKBHandler implements ActionListener {
javax.swing.JFrame parent;
public ReadKBHandler(javax.swing.JFrame p_Parent) {
parent = p_Parent;
}
public void actionPerformed(ActionEvent aevt) {
openKB(true);
java.io.File file = null;
try {
JFileChooser chooser = new JFileChooser();
ExtensionFileFilter filter = new ExtensionFileFilter();
filter.addExtension("xml");
chooser.setDialogTitle("InfoSapient -- Open Knowledgebase");
filter.setDescription("InfoSapient knowledgebases");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
if (file == null)
throw new Exception("File not chosen");
getController().readKB(file.getCanonicalPath());
updateAll();
}
} catch (Exception e) {
e.printStackTrace();
try {
JOptionPane.showConfirmDialog(
null,
"Cannot load "
+ file.getCanonicalPath()
+ "\n"
+ e.getClass().getName()
+ ":"
+ (e.getMessage() == null ? "No message" : e.getMessage()),
"InfoSapient ERROR",
JOptionPane.ERROR_MESSAGE);
} catch (Exception sub_e) {
sub_e.printStackTrace();
}
}
}
}
public class NewKBHandler implements ActionListener, java.io.Serializable {
InfoSapientLauncher myOwner;
NewKBHandler(InfoSapientLauncher mo) {
myOwner = mo;
}
public void actionPerformed(ActionEvent aevt) {
myOwner.openKB(false);
if (getController().getChgsOutstanding()) {
int returnType =
JOptionPane.showConfirmDialog(
myOwner,
"You have made changes to knowledgebase "
+ getController().getKB().getName()
+ " that have not been saved. \n"
+ "Do you want to continue?",
"InfoSapient Warning",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
if (returnType == JOptionPane.YES_OPTION)
myOwner.openNewDialog();
} else
myOwner.openNewDialog();
}
}
protected class AboutInfoSapient implements ActionListener {
public void actionPerformed(ActionEvent aE) {
JOptionPane.showMessageDialog(
null,
"InfoSapient -- A Rules Development and Execution Environment",
"InfoSapient Information",
JOptionPane.INFORMATION_MESSAGE);
}
}
protected class allCancelBtn_ClickedListener
implements ActionListener, java.io.Serializable {
public void actionPerformed(ActionEvent ae) {
cancelBtn_Clicked();
}
}
protected class allCloseBtn_ClickedListener
implements ActionListener, java.io.Serializable {
public void actionPerformed(ActionEvent ae) {
closeBtn_Clicked();
}
}
protected class allActionListener
implements ActionListener, java.io.Serializable {
public void actionPerformed(ActionEvent ae) {
getController().handleFSEvent(new FSEvent(this, ae.getActionCommand(), null));
}
}
//End Inner classes
public InfoSapientLauncher() {
this.controller = new InfoSapientController();
this.controller.addObserver(this);
this.addWindowListener(new allWinListener());
this.initialize();
}
//End Inner classes
public InfoSapientLauncher(InfoSapientController theControl) {
super();
(controller = theControl).addObserver(this);
addWindowListener(new allWinListener());
initialize();
}
/**
* CONSTRUCTORS
*/
public InfoSapientLauncher(InfoSapientController theControl, boolean flag) {
super();
(controller = theControl).addObserver(this);
addWindowListener(new allWinListener());
initialize();
}
public void applyBtn_Clicked() {
}
/**
* PROTECTED METHODS FOR COMPONENT CONTROL
*/
protected void attrItemSelected(Object iSelect) {
if (iSelect instanceof FzyAttribute) {
updateSetsList(theAttribute = (FzyAttribute) iSelect);
getController().setAttribute(theAttribute);
}
}
public void cancelBtn_Clicked() {
}
public void closeBtn_Clicked() {
}
/**
* METHODS FOR INTERFACE SATISFACTION
**/
public void deleteKnowledgebase() {
}
private void disableMenus() {
inferMenuItem.setEnabled(false);
controlMenuItem.setEnabled(false);
ruleMenuItem.setEnabled(false);
attrMenuItem.setEnabled(false);
}
private void enableMenus() {
inferMenuItem.setEnabled(true);
controlMenuItem.setEnabled(true);
ruleMenuItem.setEnabled(true);
attrMenuItem.setEnabled(true);
}
public infosapient.control.InfoSapientController getController() {
return controller;
}
public void handleFSEvent(FSEvent evt) {
if (evt.getCmd().equals("newName"))
newName((String) evt.getPackage());
}
private void initialize() {
getContentPane().setLayout(new BorderLayout());
getContentPane().setFont(new Font("Helvetica", Font.PLAIN, 12));
setSize(
getInsets().left + getInsets().right + 640,
getInsets().top + getInsets().bottom + 320);
setTitle("InfoSapient Knowledgebase Browser");
GridBagLayout launcherGBL = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);
kbNameLbl = new JLabel("Knowledgebase");
gbc.gridx = 3;
gbc.gridy = 4;
//gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 0.5;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridheight = GridBagConstraints.RELATIVE;
panel1.setLayout(launcherGBL);
kbNameFld.setText("No Knowledgebase Selected");
kbNameFld.setEnabled(false);
panel3.setBorder(
BorderFactory.createTitledBorder(
((javax.swing.border.Border) BorderFactory.createEtchedBorder()),
null));
labelsFields.add(kbNameLbl);
labelsFields.add(kbNameFld);
//panel1.add(labelsFields, gbc);
gbc.gridheight = GridBagConstraints.REMAINDER;
panel1.add(panel3, gbc);
/*gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
gbc.weighty = 0.0;
panel3.add(new JPanel(),gbc);
*/
/*
Attributes
*/
attributeList.setSelectedIndex(0);
attributeList.setVisibleRowCount(10);
attributeList.setToolTipText(
"List of all attributes defined within this knowledgebase");
attribsScrollPane = new JScrollPane();
attribsScrollPane.setBorder(
BorderFactory.createTitledBorder(
((javax.swing.border.Border) BorderFactory.createEtchedBorder()),
("Attributes")));
attribsScrollPane.getViewport().add(attributeList);
attributeList.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
attrItemSelected(attributeList.getSelectedValue());
//System.err.println("Mouse clicks from attrib list: " + e.getClickCount());
if (e.getClickCount() >= 2) {
getController().handleFSEvent(
new FSEvent(
this,
"editAttrib",
(FzyAttribute) attributeList.getSelectedValue()));
}
}
});
panel2.add(attribsScrollPane);
/* set List */
setsList.setSelectedIndex(0);
setsList.setToolTipText("List of all sets defined within a selected attribute");
setsList.setVisibleRowCount(10);
JScrollPane setsScrollPane = new JScrollPane();
setsScrollPane.setBorder(
BorderFactory.createTitledBorder(
((javax.swing.border.Border) BorderFactory.createEtchedBorder()),
("Sets")));
setsScrollPane.getViewport().add(setsList);
setsList.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.err.println("Mouse clicks from sets list: " + e.getClickCount());
if (e.getClickCount() >= 2)
setItemSelected(setsList.getSelectedValue());
}
});
panel2.add(setsScrollPane);
/* rule list */
rulesList.setSelectedIndex(0);
rulesList.setVisibleRowCount(10);
rulesList.setToolTipText("List of all rules defined within this knowledgebase");
JScrollPane ruleScrollPane = new JScrollPane();
ruleScrollPane.setBorder(
BorderFactory.createTitledBorder(
((javax.swing.border.Border) BorderFactory.createEtchedBorder()),
("Rules")));
ruleScrollPane.getViewport().add(rulesList);
rulesList.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.err.println("Mouse clicks from rules list: " + e.getClickCount());
if (e.getClickCount() >= 2)
ruleItemSelected(rulesList.getSelectedValue());
}
});
panel2.add(ruleScrollPane);
/*
* Menus Creation
*/
mainMenuBar = new JMenuBar();
fileMenu = new JMenu("File", true);
newKb = new JMenuItem("New");
newKb.addActionListener(new NewKBHandler(this));
newKb.setActionCommand("NewKB");
fileMenu.add(newKb);
//
fileMenu.addSeparator();
parseKb = new JMenuItem("Load Kb...");
parseKb.addActionListener(new ReadKBHandler(this));
fileMenu.add(parseKb);
fileMenu.addSeparator();
saveKb = new JMenuItem("Save");
saveKb.addActionListener(new WriteKBHandler(this));
saveKb.setEnabled(false);
fileMenu.add(saveKb);
saveKbAs = new JMenuItem("Save as...");
saveKbAs.addActionListener(new WriteAsKBHandler(this));
fileMenu.add(saveKbAs);
saveKbAs.setEnabled(false);
fileMenu.addSeparator();
deleteKb = new JMenuItem("Delete");
deleteKb.setActionCommand("delete-kb");
deleteKb.addActionListener(myActionListener);
fileMenu.add(deleteKb);
fileMenu.addSeparator();
exitAL = new JMenuItem("Exit");
exitAL.addActionListener(myActionListener);
exitAL.setActionCommand("REQToClose");
fileMenu.add(exitAL);
mainMenuBar.add(fileMenu);
//
editMenu = new JMenu("Edit", true);
editMenu.add("Cut");
editMenu.add("Copy");
editMenu.add("Paste");
mainMenuBar.add(editMenu);
//
editorMenu = new JMenu("Options");
attrMenuItem = new JMenuItem("Create/Edit Attribute...");
attrMenuItem.setActionCommand("newAttrEd");
attrMenuItem.addActionListener(myActionListener);
attrMenuItem.setEnabled(false);
editorMenu.add(attrMenuItem);
//
ruleMenuItem = new JMenuItem("Create/Edit Rule...");
ruleMenuItem.setActionCommand("newRuleEd");
ruleMenuItem.addActionListener(myActionListener);
ruleMenuItem.setEnabled(false);
editorMenu.add(ruleMenuItem);
//
setMenuItem = new JMenuItem("Create/Edit Set Fuzzy Set...");
setMenuItem.setActionCommand("newSetEd");
setMenuItem.addActionListener(myActionListener);
setMenuItem.setEnabled(false);
editorMenu.add(setMenuItem);
//
inferMenuItem = new JMenuItem("Execution & Inference Options");
inferMenuItem.setActionCommand("newInferEd");
inferMenuItem.addActionListener(myActionListener);
inferMenuItem.setEnabled(false);
editorMenu.add(inferMenuItem);
//
controlMenuItem = new JMenuItem("Inference Controller");
controlMenuItem.setActionCommand("newInferControl");
controlMenuItem.addActionListener(myActionListener);
controlMenuItem.setEnabled(false);
editorMenu.add(controlMenuItem);
//
mainMenuBar.add(editorMenu);
//
menu3 = new JMenu("Help");
helpMenuItem = new JMenuItem("User Guide");
helpMenuItem.setActionCommand("newHelp");
helpMenuItem.addActionListener(myActionListener);
helpMenuItem.setEnabled(false);
menu3.add(helpMenuItem);
aboutMenuItem = new JMenuItem("About");
aboutMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent aE) {
JOptionPane.showMessageDialog(
null,
"InfoSapient -- A Fuzzy Logic Based Rules Development Environment\nBrought To You By Workplace Performance Tools",
"InfoSapient Product Information",
JOptionPane.INFORMATION_MESSAGE);
}
});
menu3.add(aboutMenuItem);
mainMenuBar.add(menu3);
getContentPane().add(mainMenuBar, "North");
panel3.setLayout(new BorderLayout(5, 5));
panel3.add(panel2);
getContentPane().add(panel1, "Center");
setVisible(true);
}
/**
*
* Creation date: (05/02/01 4:22:50 PM)
* @param args java.lang.String[]
*/
public static void main(String[] args) {
InfoSapientLauncher aLauncher = new InfoSapientLauncher();
}
public void newName(String name) {
if (!openKB)
getController().newKB(name);
else {
try {
getController().readKB(name);
} catch (Exception e) {
JOptionPane.showConfirmDialog(
null,
"Cannot load " + name + "\n" + e.getMessage(),
"InfoSapient ERROR",
JOptionPane.ERROR_MESSAGE);
}
updateAll();
}
}
protected void openKB(boolean val) {
openKB = val;
}
protected void openNewDialog() {
String nameFld =
JOptionPane.showInputDialog("Please input a knowledgebase name");
if ((nameFld != null) && (!(nameFld.equals("New Name")))) {
int nameLen = nameFld.length();
for (int ix = 0; ix < nameLen; ix++) {
char c = nameFld.charAt(ix);
if (Character.isLetterOrDigit(c))
continue;
JOptionPane.showConfirmDialog(
this,
"The knowledgebase name cannot contain blanks or other special characters.",
"InfoSapient ERROR",
JOptionPane.ERROR_MESSAGE);
break;
}
this.handleFSEvent(new infosapient.system.FSEvent(this, "newName", nameFld));
openKB(false);
newName(nameFld);
} else {
JOptionPane.showConfirmDialog(
this,
"The knowledgebase name MUST be specified and cannot be blank.",
"InfoSapient ERROR",
JOptionPane.ERROR_MESSAGE);
}
}
protected void ruleItemSelected(Object iSelect) {
setMenuItem.setEnabled(false);
if (iSelect instanceof FzyRule)
getController().handleFSEvent(new FSEvent(this, "editRule", iSelect));
}
protected void setItemSelected(Object iSelect) {
if (theAttribute != null)
getController().handleFSEvent(new FSEvent(this, "editSet", (FzySet) iSelect));
}
public synchronized void setVisible(boolean f) {
setLocation(175, 50);
super.setVisible(f);
}
public void update(infosapient.system.Observable observ, Object obj) {
if (obj instanceof FSEvent) {
FSEvent anEvent = (FSEvent) obj;
String command = anEvent.getCmd();
if (command.equalsIgnoreCase("ATTR:created"))
updateAttrList();
else if (command.equalsIgnoreCase("ATTR:modified"))
updateAttrList();
else if (command.equalsIgnoreCase("Attribute:deleted"))
updateAll();
else if (command.equalsIgnoreCase("SET:SetCreated"))
updateSetsList(
theAttribute = (FzyAttribute) ((FzySet) anEvent.getPackage()).getAttribute());
else if (command.equalsIgnoreCase("SET:Set_Modified"))
updateSetsList(
theAttribute = (FzyAttribute) ((FzySet) anEvent.getPackage()).getAttribute());
else if (command.equalsIgnoreCase("SET:Set_Deleted"))
updateSetsList(
theAttribute = (FzyAttribute) ((FzySet) anEvent.getPackage()).getAttribute());
else if (command.equalsIgnoreCase("RULE:created"))
updateRulesList();
else if (command.equalsIgnoreCase("RULE:deleted"))
updateRulesList();
else if (command.startsWith("KB:", 0))
updateAll();
else if (command.equalsIgnoreCase("controller-deleted-KB"))
updateAll();
}
}
/**
* PRIVATE METHODS....
*/
private void updateAll() {
updateKBFld();
updateAttrList();
updateSetsList(null);
updateRulesList();
saveKb.setEnabled(false);
saveKbAs.setEnabled(true);
// repaintManager.repaintDirtyRegions();
}
private void updateAttrList() {
saveKb.setEnabled(true);
repaintManager = RepaintManager.currentManager(attributeList);
Vector tList = new Vector();
infosapient.system.FzyAttribute attribItem;
attributeList.removeAll();
if (getController().getKB() != null) {
java.util.Hashtable attribTable = getController().getKB().attributes();
java.util.Enumeration enum = getController().getKB().attributeKeys();
com.objectspace.jgl.OrderedSet oset =
new com.objectspace.jgl.OrderedSet(
new com.objectspace.jgl.predicates.LessString(),
true);
while (enum.hasMoreElements()) {
FzyAttribute tAttrib =
(FzyAttribute) (attribTable.get((String) enum.nextElement()));
oset.add(tAttrib);
}
java.util.Enumeration e = oset.elements();
while (e.hasMoreElements()) {
tList.addElement(e.nextElement());
}
}
attributeList.setListData(tList);
repaintManager.markCompletelyDirty(attributeList);
}
private void updateKBFld() {
RepaintManager rManager = RepaintManager.currentManager(kbNameFld);
saveKb.setEnabled(true);
if (getController().getKB() != null) {
kbNameFld.setText(getController().getKB().getName());
enableMenus();
} else {
kbNameFld.setText("No Knowledgebase Selected");
disableMenus();
}
rManager.markCompletelyDirty(kbNameFld);
}
private void updateRulesList() {
saveKb.setEnabled(true);
repaintManager = RepaintManager.currentManager(rulesList);
Vector tList = new Vector();
rulesList.removeAll();
if (getController().getKB() != null) {
infosapient.system.FzyRule ruleItem;
java.util.Hashtable rulesTable = getController().getKB().rules();
Enumeration e = getController().getKB().ruleKeys();
com.objectspace.jgl.OrderedSet oset =
new com.objectspace.jgl.OrderedSet(
new com.objectspace.jgl.predicates.LessString(),
true);
while (e.hasMoreElements()) {
oset.add(rulesTable.get((String) e.nextElement()));
}
e = oset.elements();
while (e.hasMoreElements()) {
tList.addElement(e.nextElement());
}
}
rulesList.setListData(tList);
repaintManager.markCompletelyDirty(rulesList);
}
private void updateSetsList(FzyAttribute theAttrib) {
saveKb.setEnabled(true);
repaintManager = RepaintManager.currentManager(setsList);
setsList.removeAll();
Vector tList = new Vector();
if ((getController().getKB() != null) && (theAttrib != null)) {
setMenuItem.setEnabled(true);
infosapient.system.FzySet setItem;
Enumeration e = theAttrib.sets().elements();
while (e.hasMoreElements()) {
setItem = (FzySet) e.nextElement();
tList.addElement(setItem);
}
}
setsList.setListData(tList);
repaintManager.markCompletelyDirty(setsList);
}
}