[or-cvs] r11669: added some comfort to the Groovy shell (in puppetor/trunk/src/de/uniba/wiai/lspi/puppetor: groovy impl)

kloesing at seul.org kloesing at seul.org
Thu Sep 27 16:44:02 UTC 2007


Author: kloesing
Date: 2007-09-27 12:44:02 -0400 (Thu, 27 Sep 2007)
New Revision: 11669

Modified:
   puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/groovy/RmiPuppetzShell.java
   puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/impl/NetworkImpl.java
Log:
added some comfort to the Groovy shell

Modified: puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/groovy/RmiPuppetzShell.java
===================================================================
--- puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/groovy/RmiPuppetzShell.java	2007-09-27 16:41:42 UTC (rev 11668)
+++ puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/groovy/RmiPuppetzShell.java	2007-09-27 16:44:02 UTC (rev 11669)
@@ -43,17 +43,25 @@
 package de.uniba.wiai.lspi.puppetor.groovy;
 
 import groovy.lang.GroovyShell;
+import groovy.lang.Script;
 
 import java.awt.BorderLayout;
 import java.awt.Dimension;
 import java.awt.GridBagConstraints;
 import java.awt.GridBagLayout;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.PrintStream;
 import java.net.MalformedURLException;
 import java.rmi.Naming;
 import java.rmi.NotBoundException;
 import java.rmi.RemoteException;
 import java.rmi.server.UnicastRemoteObject;
 import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
 import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.JPanel;
@@ -61,6 +69,7 @@
 import javax.swing.JSplitPane;
 import javax.swing.JTabbedPane;
 import javax.swing.JTextArea;
+
 import de.uniba.wiai.lspi.puppetor.Event;
 import de.uniba.wiai.lspi.puppetor.EventListener;
 import de.uniba.wiai.lspi.puppetor.Network;
@@ -80,6 +89,8 @@
 	 */
 	private GroovyShell shell;
 
+	private Logger logger;
+
 	/**
 	 * The ContentPane to which all other graphical components are added.
 	 */
@@ -133,6 +144,53 @@
 	private HashMap<String, EventListener> eventListeners = new HashMap<String, EventListener>();
 
 	/**
+	 * A hash map to store the entiries that have been entered into the shell.
+	 */
+	private LinkedList<String> shellEntries = null; // @jve:decl-index=0:
+
+	private int listCounter = 0;
+
+	private JButton jButtonNext = null;
+
+	private JButton jButtonPrevious = null;
+
+	/**
+	 * This method initializes jButtonNext
+	 * 
+	 * @return javax.swing.JButton
+	 */
+	private JButton getJButtonNext() {
+		if (jButtonNext == null) {
+			jButtonNext = new JButton();
+			jButtonNext.setText(">");
+			jButtonNext.addMouseListener(new java.awt.event.MouseAdapter() {
+				public void mouseReleased(java.awt.event.MouseEvent e) {
+					displayNextInput();
+				}
+			});
+		}
+		return jButtonNext;
+	}
+
+	/**
+	 * This method initializes jButtonPrevious
+	 * 
+	 * @return javax.swing.JButton
+	 */
+	private JButton getJButtonPrevious() {
+		if (jButtonPrevious == null) {
+			jButtonPrevious = new JButton();
+			jButtonPrevious.setText("<");
+			jButtonPrevious.addMouseListener(new java.awt.event.MouseAdapter() {
+				public void mouseReleased(java.awt.event.MouseEvent e) {
+					displayPreviousInput();
+				}
+			});
+		}
+		return jButtonPrevious;
+	}
+
+	/**
 	 * Main method to start the Gui.
 	 * 
 	 * @param args
@@ -146,6 +204,17 @@
 	 * Initializes the Gui and registers it as a listener to the netaNetwork
 	 */
 	public RmiPuppetzShell() {
+
+		// create logger
+		this.logger = Logger.getLogger("RmiPuppetzShell" + "."
+				+ this.getClass().getName());
+
+		this.logger.setLevel(Level.ALL);
+
+		// log entering
+		this.logger.entering(this.getClass().getName(), "RmiPuppetzShell",
+				new Object[] {});
+
 		initialize();
 
 		// binds network on the shell
@@ -154,8 +223,13 @@
 			consoleJTextArea.append("Welcome, type \"shell.[method]\" to use!"
 					+ "\n");
 		} catch (Exception e) {
+			this.logger.throwing(this.getClass().getName(), "RmiPuppetzShell",
+					e);
 			e.printStackTrace();
 		}
+
+		// log exiting
+		this.logger.exiting(this.getClass().getName(), "RmiPuppetzShell");
 	}
 
 	/**
@@ -179,6 +253,10 @@
 	public void connectNetwork(String remoteHost, String bindingName)
 			throws MalformedURLException, RemoteException, NotBoundException {
 
+		// log entering
+		this.logger.entering(this.getClass().getName(), "connectNetwork",
+				new Object[] { remoteHost, bindingName });
+
 		// network lookup
 		Network network = (Network) Naming.lookup("rmi://" + remoteHost + "/"
 				+ bindingName);
@@ -189,16 +267,58 @@
 		// binds network on the shell
 		try {
 			shell.setVariable(bindingName, network);
-			consoleJTextArea.append("Network bound to: " + bindingName + "\n");
+			getConsoleJTextArea().append(
+					"Network bound to: " + bindingName + "\n");
 		} catch (Exception e) {
+			this.logger
+					.throwing(this.getClass().getName(), "connectNetwork", e);
 			e.printStackTrace();
 		}
+
+		// log exiting
+		this.logger.exiting(this.getClass().getName(), "connectNetwork");
 	}
 
 	/**
+	 * An output stream that writes its output to a javax.swing.JTextArea
+	 * control.
+	 */
+	private class TextAreaOutputStream extends OutputStream {
+		private JTextArea textControl;
+
+		/**
+		 * Creates a new instance of TextAreaOutputStream which writes to the
+		 * specified instance of javax.swing.JTextArea control.
+		 * 
+		 * @param control
+		 *            A reference to the javax.swing.JTextArea control to which
+		 *            the output must be redirected to.
+		 */
+		public TextAreaOutputStream(JTextArea control) {
+			textControl = control;
+		}
+
+		/**
+		 * Writes the specified byte as a character to the
+		 * javax.swing.JTextArea.
+		 * 
+		 * @param b
+		 *            The byte to be written as character to the JTextArea.
+		 */
+		public void write(int b) throws IOException {
+			// append the data as characters to the JTextArea control
+			textControl.append(String.valueOf((char) b));
+		}
+	}
+
+	/**
 	 * This method initializes the window.
 	 */
 	private void initialize() {
+
+		// log entering
+		this.logger.entering(this.getClass().getName(), "initialize");
+		this.shellEntries = new LinkedList<String>();
 		this.setSize(new Dimension(531, 256));
 		this.setPreferredSize(new Dimension(100, 150));
 		this.setContentPane(getJContentPane());
@@ -206,6 +326,19 @@
 		this.setVisible(true);
 		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
 		shell = new GroovyShell();
+
+		// Create Printstream that writes to JTextArea
+		PrintStream out = new PrintStream(new TextAreaOutputStream(
+				getConsoleJTextArea()));
+
+		// redirect standard output stream to the TextAreaOutputStream
+		System.setOut(out);
+
+		// redirect error output stream to the TextAreaOutputStream
+		System.setErr(out);
+
+		// log exiting
+		this.logger.exiting(this.getClass().getName(), "initialize");
 	}
 
 	/**
@@ -214,12 +347,17 @@
 	 * @return javax.swing.JPanel
 	 */
 	private JPanel getJContentPane() {
+		// log entering
+		this.logger.entering(this.getClass().getName(), "getJContentPane");
 		if (jContentPane == null) {
 			jContentPane = new JPanel();
 			jContentPane.setLayout(new BorderLayout());
 			jContentPane.add(getJPanel(), BorderLayout.SOUTH);
 			jContentPane.add(getJSplitPane(), BorderLayout.CENTER);
 		}
+
+		// log exiting
+		this.logger.exiting(this.getClass().getName(), "getJContentPane");
 		return jContentPane;
 	}
 
@@ -229,10 +367,16 @@
 	 * @return javax.swing.JTextArea
 	 */
 	private JScrollPane getInputJScrollPane() {
+		// log entering
+		this.logger.entering(this.getClass().getName(), "getInputJScrollPane");
 		if (inputJScrollPane == null) {
 			inputJScrollPane = new JScrollPane();
 			inputJScrollPane.getViewport().add(getInputJTextArea());
 		}
+
+		// log exiting
+		this.logger.exiting(this.getClass().getName(), "getInputJScrollPane");
+
 		return inputJScrollPane;
 	}
 
@@ -242,10 +386,16 @@
 	 * @return javax.swing.JTextArea
 	 */
 	private JTextArea getInputJTextArea() {
+
+		// log entering
+		this.logger.entering(this.getClass().getName(), "getInputJTextArea");
 		if (inputJTextArea == null) {
 			inputJTextArea = new JTextArea();
 			inputJTextArea.setEditable(true);
 		}
+
+		// log exiting
+		this.logger.exiting(this.getClass().getName(), "getInputJTextArea");
 		return inputJTextArea;
 	}
 
@@ -255,10 +405,19 @@
 	 * @return javax.swing.JScrollPane
 	 */
 	private JScrollPane getConsoleJScrollPane() {
+
+		// log entering
+		this.logger.entering(this.getClass().getName(),
+				"getConsoleJScrollPane", new Object[] {});
+
 		if (ConsoleJScrollPane == null) {
 			ConsoleJScrollPane = new JScrollPane();
 			ConsoleJScrollPane.getViewport().add(getConsoleJTextArea());
 		}
+
+		// log exiting
+		this.logger.exiting(this.getClass().getName(), "getConsoleJScrollPane");
+
 		return ConsoleJScrollPane;
 	}
 
@@ -268,11 +427,19 @@
 	 * @return javax.swing.JTextArea
 	 */
 	private JTextArea getConsoleJTextArea() {
+
+		// log entering
+		this.logger.entering(this.getClass().getName(), "getConsoleJTextArea");
+
 		if (consoleJTextArea == null) {
 			consoleJTextArea = new JTextArea();
 			consoleJTextArea.setEditable(false);
 			consoleJTextArea.setName("Console");
 		}
+
+		// log exiting
+		this.logger.exiting(this.getClass().getName(), "getConsoleJTextArea");
+
 		return consoleJTextArea;
 	}
 
@@ -285,21 +452,59 @@
 	 * @return javax.swing.JButton
 	 */
 	private JButton getJButton() {
+
+		// log entering
+		this.logger.entering(this.getClass().getName(), "getJButton");
+
 		if (okJButton == null) {
 			okJButton = new JButton();
 			okJButton.setText("OK");
 			okJButton.addMouseListener(new java.awt.event.MouseAdapter() {
 				public void mouseReleased(java.awt.event.MouseEvent e) {
-					try {
-						shell.evaluate(inputJTextArea.getText());
-					} catch (Exception e1) {
-						consoleJTextArea.append("Unkorrekte eingabe" + "\n");
-						e1.printStackTrace();
-					}
 
+					Thread evaluationThread = new Thread() {
+						@Override
+						public void run() {
+							// log entering
+							logger.entering(this.getClass().getName(), "run");
+							Script script = null;
+							try {
+								String input = inputJTextArea.getText();
+								listCounter = 0;
+								shellEntries.addFirst(input);
+								script = shell.parse(input);
+							} catch (Exception e1) {
+								consoleJTextArea.append("In correct input"
+										+ "\n");
+								e1.printStackTrace();
+								logger.log(Level.WARNING, e1.getMessage());
+								return;
+							}
+							try {
+								script.run();
+							} catch (Exception e2) {
+								consoleJTextArea
+										.append("An error occured while performing script"
+												+ "\n");
+								e2.printStackTrace();
+								logger.log(Level.WARNING, e2.getMessage());
+								return;
+							}
+
+							// log exiting
+							logger.exiting(this.getClass().getName(), "run");
+						}
+					};
+					evaluationThread.setName("evaluationThread");
+					evaluationThread.start();
+
 				}
 			});
 		}
+
+		// log exiting
+		this.logger.exiting(this.getClass().getName(), "getJButton");
+
 		return okJButton;
 	}
 
@@ -309,15 +514,31 @@
 	 * @return javax.swing.JPanel
 	 */
 	private JPanel getJPanel() {
+
+		// log entering
+		this.logger.entering(this.getClass().getName(), "getJPanel");
+
 		if (buttonJPanel == null) {
+			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
+			gridBagConstraints2.gridx = 0;
+			gridBagConstraints2.gridy = 0;
+			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
+			gridBagConstraints1.gridx = 2;
+			gridBagConstraints1.gridy = 0;
 			GridBagConstraints gridBagConstraints = new GridBagConstraints();
-			gridBagConstraints.gridx = 0;
+			gridBagConstraints.gridx = 1;
 			gridBagConstraints.gridy = 0;
 			buttonJPanel = new JPanel();
 			buttonJPanel.setLayout(new GridBagLayout());
 			buttonJPanel.setPreferredSize(new Dimension(100, 30));
 			buttonJPanel.add(getJButton(), gridBagConstraints);
+			buttonJPanel.add(getJButtonNext(), gridBagConstraints1);
+			buttonJPanel.add(getJButtonPrevious(), gridBagConstraints2);
 		}
+
+		// log exiting
+		this.logger.exiting(this.getClass().getName(), "getJPanel");
+
 		return buttonJPanel;
 	}
 
@@ -327,11 +548,19 @@
 	 * @return javax.swing.JSplitPane
 	 */
 	private JSplitPane getJSplitPane() {
+
+		// log entering
+		this.logger.entering(this.getClass().getName(), "getJSplitPane");
+
 		if (jSplitPane == null) {
 			jSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
 					getInputJScrollPane(), getJTabbedPane());
 			jSplitPane.setContinuousLayout(true);
 		}
+
+		// log exiting
+		this.logger.exiting(this.getClass().getName(), "getJSplitPane");
+
 		return jSplitPane;
 	}
 
@@ -341,24 +570,36 @@
 	 * @return javax.swing.JTabbedPane
 	 */
 	private JTabbedPane getJTabbedPane() {
+
+		// log entering
+		this.logger.entering(this.getClass().getName(), "getJTabbedPane");
+
 		if (jTabbedPane == null) {
 			jTabbedPane = new JTabbedPane();
 			jTabbedPane.add("Console", getConsoleJScrollPane());
 		}
+
+		// log exiting
+		this.logger.exiting(this.getClass().getName(), "getJTabbedPane");
+
 		return this.jTabbedPane;
 	}
 
 	/**
-	 * Implements the <code>LogListener<code> interface.
+	 * Adds a tab to the network.
 	 * 
-	 *  A tab is created in the lower area of the Gui that is supposed to print 
-	 *  the output of an <code>EventManager<code> of a Network. To do this an 
-	 *  <code>EventListener<code> is created and its stub is returned.
+	 * A tab is created in the lower area of the Gui that is supposed to print
+	 * the output of an <code>EventManager<code> of a Network. To do this an 
+	 * <code>EventListener<code> is created and its stub is returned.
 	 *  
-	 *  @return the stub of the event Listener added to the output tab of a 
-	 *  registered network
+	 * @return the stub of the event Listener added to the output tab of a 
+	 * registered network
 	 */
 	public EventListener addLogTab(String tabName) {
+
+		// log entering
+		this.logger.entering(this.getClass().getName(), "addLogTab", tabName);
+
 		// Creating ScrollPane & TextArea
 		JScrollPane jScrollPane = new JScrollPane();
 		final JTextArea jTextArea = new JTextArea();
@@ -381,8 +622,43 @@
 		try {
 			elStub = (EventListener) UnicastRemoteObject.exportObject(el, 0);
 		} catch (RemoteException e) {
+			this.logger.throwing(this.getClass().getName(), "addLogTab", e);
 			e.printStackTrace();
 		}
+		// log exiting
+		this.logger.exiting(this.getClass().getName(), "addLogTab");
+
 		return elStub;
 	}
-}
+
+	/**
+	 * This method cycles throught the previous inputs and displays them to the
+	 * gui.
+	 */
+	private void displayPreviousInput() {
+
+		if (!(shellEntries.size() == 0)) {
+			listCounter++;
+			if (listCounter >= shellEntries.size()) {
+				listCounter = 0;
+			}
+			inputJTextArea.setText(shellEntries.get(listCounter));
+		}
+	}
+
+	/**
+	 * This method cycles throught the previous inputs in reverse order and
+	 * displays them to the gui.
+	 */
+	private void displayNextInput() {
+
+		if (!(shellEntries.size() == 0)) {
+			listCounter--;
+			if (listCounter < 0) {
+				listCounter = shellEntries.size() - 1;
+			}
+			inputJTextArea.setText(shellEntries.get(listCounter));
+		}
+	}
+
+} // @jve:decl-index=0:visual-constraint="28,41"

Modified: puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/impl/NetworkImpl.java
===================================================================
--- puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/impl/NetworkImpl.java	2007-09-27 16:41:42 UTC (rev 11668)
+++ puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/impl/NetworkImpl.java	2007-09-27 16:44:02 UTC (rev 11669)
@@ -262,7 +262,8 @@
 	private Map<String, ProxyNode> nodes;
 
 	/**
-	 * Determines if the given <code>name</code> is unique in this network.
+	 * Determines if the given <code>name</code> is unique in this network
+	 * (including the network name).
 	 * 
 	 * @param name
 	 *            Name to look up.
@@ -271,8 +272,9 @@
 	 */
 	private boolean isLocallyUnique(String name) {
 		return (!this.clients.containsKey(name)
-				&& !this.servers.containsKey(name) && !this.nodes
-				.containsKey(name));
+				&& !this.servers.containsKey(name)
+				&& !this.nodes.containsKey(name) && !(this.getName()
+				.equals(name)));
 	}
 
 	/**



More information about the tor-commits mailing list