Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

108
LINES

< > BotCompany Repo | #1003380 // class Chat - Swing-based chat

JavaX fragment (include)

sclass Chat {
  JTextArea chatArea = wrappedTextArea();
  new JTextField chatField;
  Runnable onEnter_pre, onEnter_post;
  bool showTimestamps = true;
  O timeFormatter;
  File logFile;
  
  // I/O
  Android3 android;
  bool monitored, hijacked;
  
  *() {
    onEnter(chatField, runnable {
      pcallFunction(onEnter_pre);
      append(formatLine(chatField.getText()) + "\n");
      chatField.selectAll();
      pcallFunction(onEnter_post);
    });
  }
  
  void requestFocus {
    chatField.requestFocus();
  }
  
  JComponent swing() {
    appendToFile(logFile, "\n");
    final JComponent c = withTitle("Chat", centerAndSouth(chatArea, chatField));
    ret bindToComponent(c,
      
      r { 
        android = makeAndroid3("Swing Chat", makeResponder(new O {
          S answer(S s) {
            ret Chat.this.answer(s, c);
          }
        }));
        print("Vport: " + android.vport);
      }, r {
        print("Disposing chat android");
        android.dispose();
      });
  }
  
  // override me if you freaking like
  S formatLine(S line) {
   ret "Usr: " + line;
  }
  
  void append(S s) {
    if (showTimestamps)
      s = formatTime() + " " + s;
    chatArea.append(s);
    if (logFile != null)
      appendToFile(logFile, s);
  }
  
  S formatTime() {
    if (timeFormatter != null) ret (S) callFunction(timeFormatter);
    ret "[" + formatInt(month(), 2) + "/" + formatInt(days(), 2) + " " + formatInt(hours(), 2) + ":" + formatInt(minutes(), 2) + ":" + formatInt(seconds(), 2) + "]";
  }
  
  S input() {
    ret chatField.getText().trim();
  }
  
  void setInput(S s) {
    chatField.setText(s);
    chatField.selectAll();
  }
  
  S answer(S s, JComponent c) {
    new Matches m;
    if "set input: *" {
      setInput(m.unq(0));
      set hijacked; ret "ok";
    }
    if "get input" {
      set monitored; ret format("ok: *", input());
    }
    if "set text: *" {
      chatArea.setText(m.unq(0));
      set hijacked; ret "ok";
    }
    if "append: *" {
      append(m.unq(0));
      set hijacked; ret "ok";
    }
    if "get text" {
      set monitored; ret format("ok: *", chatArea.getText());
    }
    if "set input cursor: * to *" {
      chatField.select(m.psi(0), m.psi(1));
      set hijacked; ret "ok";
    }
    if "window title" {
      set monitored; ret getFrameTitle(c);
    }
    if "set window title: *" {
      setFrameTitle(c, m.unq(0));
      set hijacked; ret "ok";
    }
    if "is window focused" {
      set monitored;
      ret yn(getFrame(c).isFocused());
    }
    null;
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1003380
Snippet name: class Chat - Swing-based chat
Eternal ID of this version: #1003380/1
Text MD5: 1cb5438f9c5c01c0c0e2f3c1730a45f3
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-07-19 02:10:23
Source code size: 2642 bytes / 108 lines
Pitched / IR pitched: No / No
Views / Downloads: 677 / 1871
Referenced in: #1003314 - GMail Bot with stop/move buttons (dev.)
#1003381 - Test Swing Chat
#1003383 - Test Swing Chat with Bot
#1003389 - Color Showing Bot (works, just not for repetitions)
#1003406 - Color Showing Bot v2
#1003416 - class BotChat (old)
#1003499 - class BotChat - Swing-based bot chat with dialog bot + backend (current)