private ScriptManager() {
    dialog = new JDialog();
    panel = new ScriptEditorPanel(dialog);
    dialog.setTitle("Script Editor - Wonderland Client");
    // 2. Optional: What happens when the frame closes?
    dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);

    // 3. Create component and put them in the frame.
    dialog.setContentPane(panel);

    // 4. Size the frame.
    dialog.pack();

    // Next, acquire the scripting magicry
    engineManager = new ScriptEngineManager(LoginManager.getPrimary().getClassloader());
    scriptEngine = engineManager.getEngineByName("JavaScript");
    scriptBindings = scriptEngine.createBindings();

    // Add the necessary script bindings
    scriptBindings.put("Client", ClientContextJME.getClientMain());

    stringToCellID = new HashMap<String, CellID>();

    // Load the methods into the library
    ScannedClassLoader loader = LoginManager.getPrimary().getClassloader();
    Iterator<ScriptMethodSPI> iter = loader.getInstances(ScriptMethod.class, ScriptMethodSPI.class);

    // grab all global void methods
    while (iter.hasNext()) {
      final ScriptMethodSPI method = iter.next();
      addFunctionBinding(method);
      SwingUtilities.invokeLater(
          new Runnable() {
            public void run() {
              panel.addLibraryEntry(method);
            }
          });
    }

    // grab all returnablesa
    Iterator<ReturnableScriptMethodSPI> returnables =
        loader.getInstances(ReturnableScriptMethod.class, ReturnableScriptMethodSPI.class);
    while (returnables.hasNext()) {
      ReturnableScriptMethodSPI method = returnables.next();
      addFunctionBinding(method);
      panel.addLibraryEntry(method);
    }
  }
    @Override
    public void commitEvent(Event event) {
      if (event instanceof AvatarNameEvent) {
        AvatarNameEvent e = (AvatarNameEvent) event;

        // Fetch the name tag node, there should only be one of
        // these in the system and set the name.
        NameTagNode nameTagNode = getNameTagNode();
        if (nameTagNode != null && e.getUsername().equals(username) == true) {
          nameTagNode.setNameTag(e.getEventType(), username, e.getUsernameAlias());
        }

        // Issue#172 & 286 : Avatar name out of sync after Mute.
        // update server state for mute & unmute events
        // send message to server that mute/unmute event is fired
        String currUsername = LoginManager.getPrimary().getUsername();
        if (e.getUsername().equals(currUsername)) {
          if (e.getUsername().equals(username)) {
            if (e.getEventType().equals(AvatarNameEvent.EventType.MUTE)
                || e.getEventType().equals(AvatarNameEvent.EventType.UNMUTE)) {
              NameTagMessage msg = new NameTagMessage();
              msg.setUsername(currUsername);
              msg.setIsMute(e.getEventType().equals(AvatarNameEvent.EventType.MUTE) ? true : false);
              cell.sendCellMessage(msg);
            }
          }
        }

      } else if (event instanceof AvatarRendererChangeRequestEvent) {
        handleAvatarRendererChangeRequest((AvatarRendererChangeRequestEvent) event);
      }
    }
  public File uploadFileAudioSource(String audioSource) throws AudioCacheHandlerException {
    // make sure specified file exists, create an
    // entry in the content repository and upload the file.
    String pattern = "file://";

    String s = audioSource;

    int ix = audioSource.indexOf(pattern);

    if (ix >= 0) {
      s = s.substring(ix + pattern.length());
    }

    File file = new File(s);

    if (file.exists() == false) {
      throw new AudioCacheHandlerException("Nonexistent file to upload " + s);
    }

    logger.warning("Upload File: " + s);

    ContentRepositoryRegistry registry = ContentRepositoryRegistry.getInstance();

    ContentRepository repo = registry.getRepository(LoginManager.getPrimary());

    ContentCollection audioCollection = null;

    try {
      ContentCollection c = repo.getUserRoot();

      audioCollection = (ContentCollection) c.getChild("audio");

      if (audioCollection == null) {
        audioCollection = (ContentCollection) c.createChild("audio", Type.COLLECTION);
      }
    } catch (ContentRepositoryException e) {
      throw new AudioCacheHandlerException("Content repository exception: " + e.getMessage());
    }

    try {
      /*
       * Remove file if it exists.
       */
      ContentResource r = (ContentResource) audioCollection.removeChild(file.getName());
    } catch (Exception e) {
    }

    try {
      ContentResource r =
          (ContentResource) audioCollection.createChild(file.getName(), ContentNode.Type.RESOURCE);

      r.put(file);
    } catch (Exception e) {
      throw new AudioCacheHandlerException("Failed to upload file:  " + e.getMessage());
    }

    return file;
  }
    private Image scaleImage(URL imageURL, int width, int height) {
      try {
        Image scaledBimg = null;
        ContentRepositoryRegistry registry = ContentRepositoryRegistry.getInstance();
        ContentRepository cr = registry.getRepository(LoginManager.getPrimary());
        String[] urls = imageURL.toString().split("/");
        String fname = urls[urls.length - 1];
        String userName = urls[3];
        ContentCollection user = cr.getUserRoot(userName);

        ContentResource res = (ContentResource) user.getChild(fname);

        BufferedImage bimg = ImageIO.read(res.getInputStream());
        if (bimg.getWidth() > width || bimg.getHeight() > height) {
          if (bimg.getWidth() > width && bimg.getHeight() > height) {
            if (bimg.getWidth() - width > bimg.getHeight() - height) {
              float nw = width;
              float nh = (width * bimg.getHeight()) / bimg.getWidth();
              scaledBimg = bimg.getScaledInstance((int) nw, (int) nh, BufferedImage.SCALE_SMOOTH);
              // scaledBimg.getGraphics().drawImage(bimg, 0, 0, null);
            } else {
              float nh = height;
              float nw = (height * bimg.getWidth()) / bimg.getHeight();
              scaledBimg = bimg.getScaledInstance((int) nw, (int) nh, BufferedImage.SCALE_SMOOTH);
              // scaledBimg.getGraphics().drawImage(bimg, 0, 0, null);
            }
          } else if (bimg.getWidth() > width) {
            float nw = width;
            float nh = (width * bimg.getHeight()) / bimg.getWidth();
            scaledBimg = bimg.getScaledInstance((int) nw, (int) nh, BufferedImage.SCALE_SMOOTH);
          } else if (bimg.getHeight() > height) {
            float nh = height;
            float nw = (height * bimg.getWidth()) / bimg.getHeight();
            scaledBimg = bimg.getScaledInstance((int) nw, (int) nh, BufferedImage.SCALE_SMOOTH);
          }
        } else {
          scaledBimg = bimg;
        }
        return scaledBimg;
      } catch (Exception ex) {
        ex.printStackTrace();
        Logger.getLogger(CoverScreen.class.getName()).log(Level.SEVERE, null, ex);
      }
      return null;
    }
  public String cacheContent(String serverURLText, String audioSource)
      throws AudioCacheHandlerException {

    if (audioSource.startsWith("wlcontent://")) {
      URL url;

      String serverURL = serverURLText;

      try {
        url = new URL(new URL(serverURL), "webdav/content" + audioSource);
      } catch (MalformedURLException e) {
        throw new AudioCacheHandlerException("Bad URL: " + e.getMessage());
      }

      audioSource = url.toString();
    }

    int ix = audioSource.lastIndexOf(File.separator);

    if (ix >= 0) {
      audioSource = audioSource.substring(ix + 1);
    }

    audioSource = audioSource.replaceAll(" ", "%20");

    URL url;

    try {
      url =
          new URL(
              new URL(serverURLText),
              "webdav/content/users/"
                  + LoginManager.getPrimary().getUsername()
                  + "/audio/"
                  + audioSource);
    } catch (MalformedURLException e) {
      throw new AudioCacheHandlerException("Bad URL: " + e.getMessage());
    }

    logger.warning("Cache content: " + url);
    return cacheURL(url);
  }