/**********************************************************************
*
* Prolog+CG : Prolog with conceptual graphs
*
* Copyright (C) 2004-2005 Ulrik Petersen
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
*
* Website
* =======
*
* Prolog+CG has a website here:
*
* http://prologpluscg.sourceforge.net/
*
*
* Contact
* =======
*
* Please DO NOT send email to Prof. Kabbaj. Instead, all
* correspondence regarding Prolog+CG should be sent to the current
* maintainter:
*
* Ulrik Petersen <ulrikp{t-a}users{dot}sourceforge{|dot|}net
*
* (Email obfuscated to foil spammers).
*
*
* NO SUPPORT
* ==========
*
* Note that NEITHER Prof. KABBAJ NOR ULRIK PETERSEN WILL GIVE
* SUPPORT! No support is available.
*
* If you need help in using Prolog+CG, please check out the resources
* available at:
*
* http://prologpluscg.sourceforge.net/docs.html
*
* That page points to ample resources for learning Prolog+CG.
*
*
* Bugreports
* ==========
*
* Notwithstanding the lack of support, the maintainer will gladly
* receive bugreports and bugfixes. Please feel free to send such
* bulletins to the address given above.
*
*
*/
package PrologPlusCG;
import java.net.*;
import java.util.HashMap;
import java.awt.GridBagConstraints;
import java.io.*;
import javax.swing.ScrollPaneConstants;
import PrologPlusCG.gui.PrologPlusCGFrame;
import PrologPlusCG.gui.MyTextArea;
import PrologPlusCG.prolog.PPCGEnv;
/**
*
* @author Ulrik Petersen
*/
public class PrologPlusCGApplet extends javax.swing.JApplet {
private static final long serialVersionUID = 3762813775056877107L;
// Name of Applet jar
public static final String strAppletJarFilename = "PPCGApplet.jar";
private PPCGEnv env = new PPCGEnv();
PrologPlusCGFrame prologFrame = null;
// Boxes
int noofboxes = 1;
HashMap <Integer, String> boxNr2LabelMap;
// Buttons
int noofbuttons = 1;
HashMap <Integer, String> buttonNr2LabelMap;
HashMap <Integer, String> buttonNr2GoalMap;
HashMap <Integer, String> buttonNr2BreakAfterMap;
HashMap <Integer, javax.swing.JButton> buttonNr2ButtonMap;
HashMap <Integer, javax.swing.JTextField> boxNr2TextFieldMap;
// Prolog
String prologfile = "";
String prologdir = ".";
boolean bPrologProgramLoaded = false;
// Describe parameters
public String[][] getParameterInfo() {
String[][] info = {
// Boxes
{"noofboxes", "int", "the number of input boxes (values: 0-5)"},
{"box1label", "string", "The label of box 1 (default: empty = no label)"},
{"box2label", "string", "The label of box 2 (default: empty = no label)"},
{"box3label", "string", "The label of box 3 (default: empty = no label)"},
{"box4label", "string", "The label of box 4 (default: empty = no label)"},
{"box5label", "string", "The label of box 5 (default: empty = no label)"},
// Buttons
{"noofbuttons", "int", "The number of action buttons (values: 1-20)"},
{"button1label", "string", "The label of button 1 (default 'Clear')"},
{"button2label", "string", "The label of button 2 (default 'Clear')"},
{"button3label", "string", "The label of button 3 (default 'Clear')"},
{"button4label", "string", "The label of button 4 (default 'Clear')"},
{"button5label", "string", "The label of button 5 (default 'Clear')"},
{"button6label", "string", "The label of button 6 (default 'Clear')"},
{"button7label", "string", "The label of button 7 (default 'Clear')"},
{"button8label", "string", "The label of button 8 (default 'Clear')"},
{"button9label", "string", "The label of button 9 (default 'Clear')"},
{"button10label", "string", "The label of button 10 (default 'Clear')"},
{"button11label", "string", "The label of button 11 (default 'Clear')"},
{"button12label", "string", "The label of button 12 (default 'Clear')"},
{"button13label", "string", "The label of button 13 (default 'Clear')"},
{"button14label", "string", "The label of button 14 (default 'Clear')"},
{"button15label", "string", "The label of button 15 (default 'Clear')"},
{"button16label", "string", "The label of button 16 (default 'Clear')"},
{"button17label", "string", "The label of button 17 (default 'Clear')"},
{"button18label", "string", "The label of button 18 (default 'Clear')"},
{"button19label", "string", "The label of button 19 (default 'Clear')"},
{"button20label", "string", "The label of button 20 (default 'Clear')"},
{"button1goal", "string", "The goal of button 1 (default 'false')"},
{"button2goal", "string", "The goal of button 2 (default 'false')"},
{"button3goal", "string", "The goal of button 3 (default 'false')"},
{"button4goal", "string", "The goal of button 4 (default 'false')"},
{"button5goal", "string", "The goal of button 5 (default 'false')"},
{"button6goal", "string", "The goal of button 6 (default 'false')"},
{"button7goal", "string", "The goal of button 7 (default 'false')"},
{"button8goal", "string", "The goal of button 8 (default 'false')"},
{"button9goal", "string", "The goal of button 9 (default 'false')"},
{"button10goal", "string", "The goal of button 10 (default 'false')"},
{"button11goal", "string", "The goal of button 11 (default 'false')"},
{"button12goal", "string", "The goal of button 12 (default 'false')"},
{"button13goal", "string", "The goal of button 13 (default 'false')"},
{"button14goal", "string", "The goal of button 14 (default 'false')"},
{"button15goal", "string", "The goal of button 15 (default 'false')"},
{"button16goal", "string", "The goal of button 16 (default 'false')"},
{"button17goal", "string", "The goal of button 17 (default 'false')"},
{"button18goal", "string", "The goal of button 18 (default 'false')"},
{"button19goal", "string", "The goal of button 19 (default 'false')"},
{"button20goal", "string", "The goal of button 20 (default 'false')"},
{"button1breakafter", "string", "A boolean saying whether to break after button 1 (default 'false')"},
{"button2breakafter", "string", "A boolean saying whether to break after button 2 (default 'false')"},
{"button3breakafter", "string", "A boolean saying whether to break after button 3 (default 'false')"},
{"button4breakafter", "string", "A boolean saying whether to break after button 4 (default 'false')"},
{"button5breakafter", "string", "A boolean saying whether to break after button 5 (default 'false')"},
{"button6breakafter", "string", "A boolean saying whether to break after button 6 (default 'false')"},
{"button7breakafter", "string", "A boolean saying whether to break after button 7 (default 'false')"},
{"button8breakafter", "string", "A boolean saying whether to break after button 8 (default 'false')"},
{"button9breakafter", "string", "A boolean saying whether to break after button 9 (default 'false')"},
{"button10breakafter", "string", "A boolean saying whether to break after button 10 (default 'false')"},
{"button11breakafter", "string", "A boolean saying whether to break after button 11 (default 'false')"},
{"button12breakafter", "string", "A boolean saying whether to break after button 12 (default 'false')"},
{"button13breakafter", "string", "A boolean saying whether to break after button 13 (default 'false')"},
{"button14breakafter", "string", "A boolean saying whether to break after button 14 (default 'false')"},
{"button15breakafter", "string", "A boolean saying whether to break after button 15 (default 'false')"},
{"button16breakafter", "string", "A boolean saying whether to break after button 16 (default 'false')"},
{"button17breakafter", "string", "A boolean saying whether to break after button 17 (default 'false')"},
{"button18breakafter", "string", "A boolean saying whether to break after button 18 (default 'false')"},
{"button19breakafter", "string", "A boolean saying whether to break after button 19 (default 'false')"},
{"button20breakafter", "string", "A boolean saying whether to break after button 20 (default 'false')"},
// PROLOG
{"prologfile", "string", "The name of the Prolog+CG file"},
{"prologdir", "string", "The directory in which to find the Prolog+CG file, relative to DOCUMENT BASE (default '.')"}
};
return info;
}
// Load parameters
protected void loadAppletParameters() {
String param;
///////////////////////////////////
//
// Get parameter 'noofboxes'
//
///////////////////////////////////
param = getParameter("noofboxes");
if (param != null) {
// Parse integer
try {
noofboxes = Integer.parseInt(param);
} catch (NumberFormatException e) {
// Use default
}
}
// Make sure it is within range
if (noofboxes < -1) {
noofboxes = 0;
} else if (noofboxes > 5) {
noofboxes = 5;
}
///////////////////////////////////
//
// Get 'box?label' parameters
//
///////////////////////////////////
for (int i = 1;
i <= noofboxes;
++i) {
String boxLabelParam = "box" + i + "label";
String localParam = getParameter(boxLabelParam);
if (localParam != null) {
boxNr2LabelMap.put(new Integer(i), localParam);
} else {
boxNr2LabelMap.put(new Integer(i), "");
}
}
///////////////////////////////////
//
// Get parameter 'noofbuttons'
//
///////////////////////////////////
param = getParameter("noofbuttons");
if (param != null) {
// Parse integer
try {
noofbuttons = Integer.parseInt(param);
} catch (NumberFormatException e) {
// Use default
}
}
// Make sure it is within range
if (noofbuttons < 1) {
noofbuttons = 1;
} else if (noofbuttons > 20) {
noofbuttons = 20;
}
///////////////////////////////////
//
// Get 'button?label' parameters
//
///////////////////////////////////
for (int i = 1;
i <= noofbuttons;
++i) {
String buttonLabelParam = "button" + i + "label";
String buttonGoalParam = "button" + i + "goal";
String buttonBreakAfterParam = "button" + i + "breakafter";
String localLabelParam = getParameter(buttonLabelParam);
if (localLabelParam != null) {
buttonNr2LabelMap.put(new Integer(i), localLabelParam);
} else {
buttonNr2LabelMap.put(new Integer(i), "Clear");
}
String localGoalParam = getParameter(buttonGoalParam);
if (localGoalParam != null) {
buttonNr2GoalMap.put(new Integer(i), localGoalParam);
} else {
buttonNr2GoalMap.put(new Integer(i), "clearConsole.");
}
String localBreakAfterParam = getParameter(buttonBreakAfterParam);
if (localBreakAfterParam != null) {
buttonNr2BreakAfterMap.put(new Integer(i), localBreakAfterParam);
} else {
buttonNr2BreakAfterMap.put(new Integer(i), "false");
}
}
param = getParameter("prologfile");
if (param != null) {
prologfile = param;
}
param = getParameter("prologdir");
if (param != null) {
prologdir = param;
}
// If prologdir is empty, set it to to ".".
// This is so that we can always add it, plus "/" to get the
// URL
if (prologdir.equals("")) {
prologdir = ".";
}
// If prologdir mistakenly ends with '/' or '\',
// trim it off.
if (prologdir.endsWith("/") || prologdir.endsWith("\\")) {
prologdir = prologdir.substring(0, prologdir.length());
}
}
/** Initializes the applet PrologPlusCGApplet */
public void init() {
// Create prolog frame, telling it that we are an applet,
// so that it doesn't create a GUI.
prologFrame = new PrologPlusCGFrame(env, true);
PPCGIO_Applet io = new PPCGIO_Applet(env, prologFrame);
env.io = io;
// Initialze hash maps
boxNr2LabelMap = new HashMap<Integer, String>();
boxNr2TextFieldMap = new HashMap<Integer, javax.swing.JTextField>();
buttonNr2LabelMap = new HashMap <Integer, String>();
buttonNr2GoalMap = new HashMap <Integer, String>();
buttonNr2BreakAfterMap = new HashMap <Integer, String>();
buttonNr2ButtonMap = new HashMap <Integer, javax.swing.JButton>();
loadAppletParameters();
// Create the GUI on the event-dispatching thread
try {
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
public synchronized void run() {
initComponents();
}
});
} catch (Exception e) {
System.err.println("GUI was not created successfully!");
}
// Load Prolog program into memory
loadPrologProgram();
}
private void loadPrologProgram() {
// Get applet's code base. This is the directory in which
// the applet's jar file is found, and it is a URL.
String strCodeBase = getCodeBase().toString();
// Decide which separator to use
String strSeparator;
if (strCodeBase.startsWith("file:")) {
// For file:, use the system's separator
// ('\' on Windows, '/' on Unix, etc.)
strSeparator = System.getProperty("file.separator");
} else {
// For http:, ftp:, etc, use the URL separator ('/').
strSeparator = "/";
}
// Make URL string from which to get the Prolog program
String strURLtoGet =
strCodeBase + prologdir + strSeparator + prologfile;
// Assume it didn't work
bPrologProgramLoaded = false;
// Create the URL
URL myURL;
try {
myURL = new URL(strURLtoGet);
} catch (MalformedURLException e) {
// Set error message
webConsoleArea.setText("ERROR1: Could not load Prolog program from URL '" + strURLtoGet + "'. This applet is not going to respond.");
// Return now. don't go any further.
return;
}
// Get the InputStream
InputStream istream;
try {
istream = myURL.openStream();
} catch (IOException e) {
// Set error message
webConsoleArea.setText("ERROR2: Could not load Prolog program from URL '" + strURLtoGet + "'. This applet is not going to respond.");
// Return now. don't go any further.
return;
} catch (java.security.AccessControlException e) {
// Set error message
webConsoleArea.setText("ERROR2.1: Did not have permissions to access Prolog program from URL '" + strURLtoGet + "'. This applet is not going to respond.");
// Return now. don't go any further.
return;
}
// Assume by default that it is UTF-8
// NOTE that the loop below assumes that it is true by default!
boolean bIsUTF8 = true;
try {
if (istream.markSupported()) {
istream.mark(5);
byte firstThreeBytes[] = new byte[3];
int noOfBytesRead = istream.read(firstThreeBytes);
if (noOfBytesRead != 3) {
bIsUTF8 = false;
} else {
byte BOM[] = { (byte) 239, (byte) 187, (byte) 191 };
for (int i = 0; bIsUTF8 && (i < 3); ++i) {
if (firstThreeBytes[i] != BOM[i]) {
bIsUTF8 = false;
}
}
}
istream.reset();
} else {
// Assume it is true, even though
// we cannot verify it.
bIsUTF8 = true;
}
} catch (IOException e) {
// Set error message
webConsoleArea.setText("ERROR4: Could not find encoding for Prolog program from URL '" + strURLtoGet + "'. This applet is not going to respond.");
// Return now. don't go any further.
return;
}
try {
if (bIsUTF8) {
// Read BOM.
byte firstThreeBytes[] = new byte[3];
istream.read(firstThreeBytes);
InputStreamReader inReader = new InputStreamReader(istream, "UTF-8");
prologFrame.ProgramTxtArea.read(inReader, null);
} else {
// Not UTF-8; use the default encoding of the system.
prologFrame.ProgramTxtArea.read(istream, null);
}
} catch (IOException e){
// Set error message
webConsoleArea.setText("ERROR3: Could not load Prolog program from URL '" + strURLtoGet + "'. This applet is not going to respond.");
// Return now. don't go any further.
return;
}
// Remember that we have loaded the thing.
bPrologProgramLoaded = true;
}
/** This method is called from within the init() method to
* initialize the form.
*/
private void initComponents() {//GEN-BEGIN:initComponents
java.awt.GridBagConstraints gridBagConstraints;
jBoxesButtonsPanel = new javax.swing.JPanel();
jButtonsPanel = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
webConsoleArea = new MyTextArea(env, prologFrame, true, 14);
getContentPane().setLayout(new java.awt.GridBagLayout());
jBoxesButtonsPanel.setLayout(new java.awt.GridBagLayout());
jBoxesButtonsPanel.setAlignmentY(0.0F);
for (int boxNr = 1;
boxNr <= noofboxes;
++boxNr) {
String boxLabel = boxNr2LabelMap.get(new Integer(boxNr));
javax.swing.JLabel jBoxLabel = new javax.swing.JLabel();
javax.swing.JTextField jBoxTextField = new javax.swing.JTextField();
jBoxLabel.setText(boxLabel);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = boxNr - 1;
jBoxesButtonsPanel.add(jBoxLabel, gridBagConstraints);
jBoxTextField.setText("");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = boxNr - 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0;
jBoxesButtonsPanel.add(jBoxTextField, gridBagConstraints);
boxNr2TextFieldMap.put(new Integer(boxNr), jBoxTextField);
}
//jButtonsPanel.setLayout(new javax.swing.BoxLayout(jButtonsPanel, javax.swing.BoxLayout.X_AXIS));
jButtonsPanel.setLayout(new javax.swing.BoxLayout(jButtonsPanel, javax.swing.BoxLayout.Y_AXIS));
javax.swing.JPanel jCurButtonRowPanel = new javax.swing.JPanel();
jCurButtonRowPanel.setLayout(new javax.swing.BoxLayout(jCurButtonRowPanel, javax.swing.BoxLayout.X_AXIS));
jButtonsPanel.add(jCurButtonRowPanel);
gridBagConstraints = new java.awt.GridBagConstraints();
boolean bJustAddedNewRow = true;
int rowCount = -1;
for (int buttonNr = 1;
buttonNr <= noofbuttons;
++buttonNr) {
String buttonLabel = buttonNr2LabelMap.get(new Integer(buttonNr));
String buttonBreakAfter = buttonNr2BreakAfterMap.get(new Integer(buttonNr));
boolean bBreakAfter = buttonBreakAfter.equals("true");
javax.swing.JButton jButton = new javax.swing.JButton();
buttonNr2ButtonMap.put(new Integer(buttonNr), jButton);
jButton.setText(buttonLabel);
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonActionPerformed(evt);
}
});
jCurButtonRowPanel.add(jButton);
if (bBreakAfter) {
jCurButtonRowPanel.add(javax.swing.Box.createHorizontalGlue()); // Make buttons not stretch
jCurButtonRowPanel = new javax.swing.JPanel();
jCurButtonRowPanel.setLayout(new javax.swing.BoxLayout(jCurButtonRowPanel, javax.swing.BoxLayout.X_AXIS));
jButtonsPanel.add(jCurButtonRowPanel);
bJustAddedNewRow = true;
bBreakAfter = false;
} else {
if (bJustAddedNewRow) {
bJustAddedNewRow = false;
} else {
}
}
}
jCurButtonRowPanel.add(javax.swing.Box.createHorizontalGlue()); // Make buttons on last row not stretch
jBoxesButtonsPanel.add(jButtonsPanel, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = noofboxes;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 2.0;
jBoxesButtonsPanel.add(jButtonsPanel, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
gridBagConstraints.weightx = 1.0;
getContentPane().add(jBoxesButtonsPanel, gridBagConstraints);
jScrollPane1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane1.setMinimumSize(new java.awt.Dimension(560, 250));
jScrollPane1.setPreferredSize(new java.awt.Dimension(20, 20));
webConsoleArea.setColumns(80);
webConsoleArea.setEditable(false);
webConsoleArea.setRows(30);
webConsoleArea.setMinimumSize(new java.awt.Dimension(20, 20));
jScrollPane1.setViewportView(webConsoleArea);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
getContentPane().add(jScrollPane1, gridBagConstraints);
}//GEN-END:initComponents
private void jButtonActionPerformed(java.awt.event.ActionEvent zevt) {
try {
javax.swing.JButton source = (javax.swing.JButton) zevt.getSource();
for (int i = 1;
i <= noofbuttons;
++i) {
Integer myInteger = new Integer(i);
javax.swing.JButton buttonNrI = buttonNr2ButtonMap.get(myInteger);
if (buttonNrI != null
&& source != null
&& buttonNrI == source) {
// We found it!
String goal = buttonNr2GoalMap.get(myInteger);
resolve(goal);
break;
}
}
} catch (java.lang.Throwable e) {
}
}
private void resolve(String goal) {
// Substitute stuff
String strGoalToResolve = substitute(goal);
// See if we need to add "." at the end.
if (!strGoalToResolve.endsWith(".")) {
// Yes, it did not end with '.', so we add it.
strGoalToResolve = strGoalToResolve + ".";
}
// Compile program
prologFrame.compileApplet();
// Ask question
prologFrame.ResolveApplet(strGoalToResolve);
}
private String substitute(String inGoal) {
String result = inGoal;
for (int boxNr = 1;
boxNr <= noofboxes;
++boxNr) {
javax.swing.JTextField jBoxTextField = boxNr2TextFieldMap.get(new Integer(boxNr));
String fieldContents = jBoxTextField.getText();
result = result.replaceAll("#" + boxNr, fieldContents);
}
// "
result = result.replaceAll(""", "\"");
// <
result = result.replaceAll("<", "<");
// >
result = result.replaceAll(">", ">");
// &
result = result.replaceAll("&", "&");
return result;
}
public String getAppletInfo() {
return "Title: Prolog+CG Applet\n"
+ "Author: Ulrik Sandborg-Petersen\n"
+ "An applet to run Prolog+CG in a browser.";
}
// Variables declaration - do not modify//GEN-BEGIN:variables
public static MyTextArea webConsoleArea;
private javax.swing.JPanel jBoxesButtonsPanel;
private javax.swing.JPanel jButtonsPanel;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration//GEN-END:variables
}