Example #1
0
  @Override
  public void keyTyped(final KeyEvent e) {
    if (!hist.active()
        || control(e)
        || DELNEXT.is(e)
        || DELPREV.is(e)
        || ESCAPE.is(e)
        || CUT2.is(e)) return;

    final int caret = editor.pos();

    // remember if marked text is to be deleted
    final StringBuilder sb = new StringBuilder(1).append(e.getKeyChar());
    final boolean indent = TAB.is(e) && editor.indent(sb, e.isShiftDown());

    // delete marked text
    final boolean selected = editor.selected() && !indent;
    if (selected) editor.delete();

    final int move = ENTER.is(e) ? editor.enter(sb) : editor.add(sb, selected);

    // refresh history and adjust cursor position
    hist.store(editor.text(), caret, editor.pos());
    if (move != 0) editor.pos(Math.min(editor.size(), caret + move));

    // adjust text height
    scrollCode.invokeLater(true);
    e.consume();
  }
 void jTextFieldSendSubject_keyPressed(KeyEvent e) {
   if (e.getKeyCode() == e.VK_DOWN) {
     String s = subjectHistory.forward();
     if (s != null) {
       jTextFieldSendSubject.setText(s);
     }
   } else if (e.getKeyCode() == e.VK_UP) {
     String s = subjectHistory.back();
     if (s != null) {
       jTextFieldSendSubject.setText(s);
     }
   } else if (e.getKeyChar() == '\n') {
     jTextFieldSendMessages.requestFocus();
   }
 }
Example #3
0
  /** Sorts text. */
  public final void sort() {
    final int caret = editor.pos();
    final DialogSort ds = new DialogSort(gui);
    if (!ds.ok() || !editor.sort()) return;

    hist.store(editor.text(), caret, editor.pos());
    scrollCode.invokeLater(true);
    repaint();
  }
 void jTextFieldSendMessages_keyPressed(KeyEvent e) {
   if (e.getKeyCode() == e.VK_DOWN) {
     String s = msgHistory.forward();
     if (s != null) {
       jTextFieldSendMessages.setText(s);
     }
   } else if (e.getKeyCode() == e.VK_UP) {
     String s = msgHistory.back();
     if (s != null) {
       jTextFieldSendMessages.setText(s);
     }
   } else if (e.getKeyChar() == '\n') {
     String body = jTextFieldSendMessages.getText();
     if (body.length() > 0) {
       if (body.charAt(body.length() - 1) == '\n') body = body.substring(0, body.length() - 1);
       String subject = jTextFieldSendSubject.getText();
       if (subject.length() > 0) {
         if (subject.charAt(subject.length() - 1) == '\n')
           subject = subject.substring(0, subject.length() - 1);
       }
       if (session != null && session.isConnected()) {
         session.postMessage(jTextFieldTargetUser.getText(), subject, body);
         displaySendMessage(subject, jTextFieldTargetUser.getText(), body, outgoingMsgAttrSet);
       }
       msgHistory.add(body);
       subjectHistory.add(subject);
       subjectHistory.reset();
       msgHistory.reset();
       jTextFieldSendMessages.setText("");
       jTextFieldSendSubject.setText("");
     }
   }
 }
Example #5
0
 /**
  * Sets the output text.
  *
  * @param text output text
  * @param size text size
  */
 public final void setText(final byte[] text, final int size) {
   byte[] txt = text;
   if (Token.contains(text, '\r')) {
     // remove carriage returns
     int ns = 0;
     for (int r = 0; r < size; ++r) {
       final byte b = text[r];
       if (b != '\r') text[ns++] = b;
     }
     // new text is different...
     txt = Arrays.copyOf(text, ns);
   } else if (text.length != size) {
     txt = Arrays.copyOf(text, size);
   }
   if (editor.text(txt)) {
     if (hist != null) hist.store(txt, editor.pos(), 0);
   }
   if (isShowing()) resizeCode.invokeLater();
 }
    public void run() {

      // keep the thread alive
      while (true) {

        // wait for a signal to re-continue processing agents
        if (!running) {
          synchronized (this) {
            try {
              wait();
            } catch (InterruptedException ie) {
              System.err.println("ERROR: wait() call failed" + ie);
            }
          }
        }

        // run for the number of generations specified
        while (cur_gen < generations && running) {

          // create our list of agents
          e.purgeAgents();
          e.addAgents(children);

          // Process 100-days
          for (int i = 0; i < day_count; i++) {
            e.simulateDay();
          }

          // record this generation
          history.addGeneration(children);

          // sort the results by money, descending
          Collections.sort(children);
          Collections.reverse(children);

          // if the elite total changed, alert the main class
          int elite_most_money = children.get(0).getMoney();
          if (elite_most_money > prev_elite_total) {

            // cache the new elite total
            prev_elite_total = elite_most_money;
            current_elite = children.get(0);

            ep.setNewElite(current_elite);
            ep.repaint();

            // fire an event to the main class
            listener.actionPerformed(
                new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "elite_total"));
          }

          // now we need to select agents for crossover, mutation,
          // and elites for elites, keep the first x%
          List<Agent> elites = children.subList(0, (int) (children.size() * elite_percent));

          // for parents to keep, simply choose the top y%
          // (including the elites)
          List<Agent> parents = children.subList(0, (int) (children.size() * parent_percent));

          // create some children, and perform mutations
          children = evo.performCrossover(parents);
          children = evo.performMutation(children);

          // add the elite agents into the pool
          children.addAll(elites);

          // System.out.println(elites.get(0) + ": " + elites.get(0).getMoney());

          // reset the elites for the next generation
          for (Agent elitea : elites) elitea.reset();

          // repopulate the next generation with new Agents
          fillWithAgents();

          cur_gen++;
        }
        running = false;
      }
    }
Example #7
0
 /**
  * Finishes a command.
  *
  * @param old old cursor position; store entry to history if position != -1
  */
 private void finish(final int old) {
   if (old != -1) hist.store(editor.text(), old, editor.pos());
   scrollCode.invokeLater(true);
   release(Action.CHECK);
 }
Example #8
0
  @Override
  public void keyPressed(final KeyEvent e) {
    // ignore modifier keys
    if (specialKey(e) || modifier(e)) return;

    // re-animate cursor
    caret(true);

    // operations without cursor movement...
    final int fh = rend.fontHeight();
    if (SCROLLDOWN.is(e)) {
      scroll.pos(scroll.pos() + fh);
      return;
    }
    if (SCROLLUP.is(e)) {
      scroll.pos(scroll.pos() - fh);
      return;
    }

    // set cursor position
    final boolean selected = editor.selected();
    final int pos = editor.pos();

    final boolean shift = e.isShiftDown();
    boolean down = true, consumed = true;

    // move caret
    int lc = Integer.MIN_VALUE;
    final byte[] txt = editor.text();
    if (NEXTWORD.is(e)) {
      editor.nextWord(shift);
    } else if (PREVWORD.is(e)) {
      editor.prevWord(shift);
      down = false;
    } else if (TEXTSTART.is(e)) {
      editor.textStart(shift);
      down = false;
    } else if (TEXTEND.is(e)) {
      editor.textEnd(shift);
    } else if (LINESTART.is(e)) {
      editor.lineStart(shift);
      down = false;
    } else if (LINEEND.is(e)) {
      editor.lineEnd(shift);
    } else if (PREVPAGE_RO.is(e) && !hist.active()) {
      lc = editor.linesUp(getHeight() / fh, false, lastCol);
      down = false;
    } else if (NEXTPAGE_RO.is(e) && !hist.active()) {
      lc = editor.linesDown(getHeight() / fh, false, lastCol);
    } else if (PREVPAGE.is(e) && !sc(e)) {
      lc = editor.linesUp(getHeight() / fh, shift, lastCol);
      down = false;
    } else if (NEXTPAGE.is(e) && !sc(e)) {
      lc = editor.linesDown(getHeight() / fh, shift, lastCol);
    } else if (NEXTLINE.is(e) && !MOVEDOWN.is(e)) {
      lc = editor.linesDown(1, shift, lastCol);
    } else if (PREVLINE.is(e) && !MOVEUP.is(e)) {
      lc = editor.linesUp(1, shift, lastCol);
      down = false;
    } else if (NEXTCHAR.is(e)) {
      editor.next(shift);
    } else if (PREVCHAR.is(e)) {
      editor.previous(shift);
      down = false;
    } else {
      consumed = false;
    }
    lastCol = lc == Integer.MIN_VALUE ? -1 : lc;

    // edit text
    if (hist.active()) {
      if (COMPLETE.is(e)) {
        complete();
        return;
      }

      if (MOVEDOWN.is(e)) {
        editor.move(true);
      } else if (MOVEUP.is(e)) {
        editor.move(false);
      } else if (DELLINE.is(e)) {
        editor.deleteLine();
      } else if (DELNEXTWORD.is(e)) {
        editor.deleteNext(true);
      } else if (DELLINEEND.is(e)) {
        editor.deleteNext(false);
      } else if (DELNEXT.is(e)) {
        editor.delete();
      } else if (DELPREVWORD.is(e)) {
        editor.deletePrev(true);
        down = false;
      } else if (DELLINESTART.is(e)) {
        editor.deletePrev(false);
        down = false;
      } else if (DELPREV.is(e)) {
        editor.deletePrev();
        down = false;
      } else {
        consumed = false;
      }
    }
    if (consumed) e.consume();

    final byte[] tmp = editor.text();
    if (txt != tmp) {
      // text has changed: add old text to history
      hist.store(tmp, pos, editor.pos());
      scrollCode.invokeLater(down);
    } else if (pos != editor.pos() || selected != editor.selected()) {
      // cursor position or selection state has changed
      cursorCode.invokeLater(down ? 2 : 0);
    }
  }
Example #9
0
 /** Formats the selected text. */
 public final void format() {
   final int caret = editor.pos();
   if (editor.format(rend.getSyntax())) hist.store(editor.text(), caret, editor.pos());
   scrollCode.invokeLater(true);
 }
Example #10
0
 /**
  * Case conversion.
  *
  * @param cs case type
  */
 public final void toCase(final Case cs) {
   final int caret = editor.pos();
   if (editor.toCase(cs)) hist.store(editor.text(), caret, editor.pos());
   scrollCode.invokeLater(true);
 }
Example #11
0
 /**
  * Auto-completes a string at the specified position.
  *
  * @param string string
  * @param start start position
  */
 private void complete(final String string, final int start) {
   final int caret = editor.pos();
   editor.complete(string, start);
   hist.store(editor.text(), caret, editor.pos());
   scrollCode.invokeLater(true);
 }
Example #12
0
 public void update() {
   History history = shell.history;
   back.setEnabled(history.isBackEnabled());
   forward.setEnabled(history.isForwardEnabled());
   up.setEnabled(history.isUpEnabled());
 }