@Override
    public void run() {
      try {
        PySelection selection = new PySelection(edit);

        ScriptConsole console = ScriptConsole.getActiveScriptConsole();

        if (console == null) {
          // if no console is available, create it (if possible).
          PydevConsoleFactory factory = new PydevConsoleFactory();
          String cmd = null;

          // Check if the current selection should be sent to the editor.
          if (InteractiveConsolePrefs.getSendCommandOnCreationFromEditor()) {
            cmd = getCommandToSend(edit, selection);
            if (cmd != null) {
              cmd = "\n" + cmd;
            }
          }
          factory.createConsole(cmd);

        } else {
          if (console instanceof ScriptConsole) {
            // ok, console available
            sendCommandToConsole(selection, console, this.edit);
          }
        }
      } catch (Exception e) {
        Log.log(e);
      }
    }
  /**
   * Wait for an established connection.
   *
   * @param monitor
   * @throws Exception if no suitable response is received before the timeout
   * @throws UserCanceledException if user cancelled with monitor
   */
  public void hello(IProgressMonitor monitor) throws Exception, UserCanceledException {
    int maximumAttempts = InteractiveConsolePrefs.getMaximumAttempts();
    monitor.beginTask("Establishing Connection To Console Process", maximumAttempts);
    try {
      if (firstCommWorked) {
        return;
      }

      // We'll do a connection attempt, we can try to
      // connect n times (until the 1st time the connection
      // is accepted) -- that's mostly because the server may take
      // a while to get started.

      String result = null;
      for (int commAttempts = 0; commAttempts < maximumAttempts; commAttempts++) {
        if (monitor.isCanceled())
          throw new UserCanceledException("Canceled before hello was successful");
        try {
          Object[] resulta;
          resulta = (Object[]) client.execute("hello", new Object[] {"Hello pydevconsole"});
          result = resulta[0].toString();
        } catch (XmlRpcException e) {
          // We'll retry in a moment
        }

        if ("Hello eclipse".equals(result)) {
          firstCommWorked = true;
          break;
        }

        try {
          Thread.sleep(250);
        } catch (InterruptedException e) {
          // Retry now
        }
        monitor.worked(1);
      }

      if (!firstCommWorked) {
        throw new Exception(
            "Failed to recive suitable Hello response from pydevconsole. Last msg received: "
                + result);
      }
    } finally {
      monitor.done();
    }
  }
  /** Sends the current selected text/editor to the console. */
  private static void sendCommandToConsole(
      PySelection selection, ScriptConsole console, PyEdit edit) throws BadLocationException {
    ScriptConsole pydevConsole = console;
    IDocument document = pydevConsole.getDocument();

    String cmd = getCommandToSend(edit, selection);
    if (cmd != null) {
      document.replace(document.getLength(), 0, cmd);
    }

    if (InteractiveConsolePrefs.getFocusConsoleOnSendCommand()) {
      ScriptConsoleViewer viewer = pydevConsole.getViewer();
      if (viewer == null) {
        return;
      }
      StyledText textWidget = viewer.getTextWidget();
      if (textWidget == null) {
        return;
      }
      textWidget.setFocus();
    }
  }
Exemple #4
0
 @Override
 public boolean getFocusOnStart() {
   return InteractiveConsolePrefs.getFocusConsoleOnStartup();
 }