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)

1  
sclass Chat {
2  
  JTextArea chatArea = wrappedTextArea();
3  
  new JTextField chatField;
4  
  Runnable onEnter_pre, onEnter_post;
5  
  bool showTimestamps = true;
6  
  O timeFormatter;
7  
  File logFile;
8  
  
9  
  // I/O
10  
  Android3 android;
11  
  bool monitored, hijacked;
12  
  
13  
  *() {
14  
    onEnter(chatField, runnable {
15  
      pcallFunction(onEnter_pre);
16  
      append(formatLine(chatField.getText()) + "\n");
17  
      chatField.selectAll();
18  
      pcallFunction(onEnter_post);
19  
    });
20  
  }
21  
  
22  
  void requestFocus {
23  
    chatField.requestFocus();
24  
  }
25  
  
26  
  JComponent swing() {
27  
    appendToFile(logFile, "\n");
28  
    final JComponent c = withTitle("Chat", centerAndSouth(chatArea, chatField));
29  
    ret bindToComponent(c,
30  
      
31  
      r { 
32  
        android = makeAndroid3("Swing Chat", makeResponder(new O {
33  
          S answer(S s) {
34  
            ret Chat.this.answer(s, c);
35  
          }
36  
        }));
37  
        print("Vport: " + android.vport);
38  
      }, r {
39  
        print("Disposing chat android");
40  
        android.dispose();
41  
      });
42  
  }
43  
  
44  
  // override me if you freaking like
45  
  S formatLine(S line) {
46  
   ret "Usr: " + line;
47  
  }
48  
  
49  
  void append(S s) {
50  
    if (showTimestamps)
51  
      s = formatTime() + " " + s;
52  
    chatArea.append(s);
53  
    if (logFile != null)
54  
      appendToFile(logFile, s);
55  
  }
56  
  
57  
  S formatTime() {
58  
    if (timeFormatter != null) ret (S) callFunction(timeFormatter);
59  
    ret "[" + formatInt(month(), 2) + "/" + formatInt(days(), 2) + " " + formatInt(hours(), 2) + ":" + formatInt(minutes(), 2) + ":" + formatInt(seconds(), 2) + "]";
60  
  }
61  
  
62  
  S input() {
63  
    ret chatField.getText().trim();
64  
  }
65  
  
66  
  void setInput(S s) {
67  
    chatField.setText(s);
68  
    chatField.selectAll();
69  
  }
70  
  
71  
  S answer(S s, JComponent c) {
72  
    new Matches m;
73  
    if "set input: *" {
74  
      setInput(m.unq(0));
75  
      set hijacked; ret "ok";
76  
    }
77  
    if "get input" {
78  
      set monitored; ret format("ok: *", input());
79  
    }
80  
    if "set text: *" {
81  
      chatArea.setText(m.unq(0));
82  
      set hijacked; ret "ok";
83  
    }
84  
    if "append: *" {
85  
      append(m.unq(0));
86  
      set hijacked; ret "ok";
87  
    }
88  
    if "get text" {
89  
      set monitored; ret format("ok: *", chatArea.getText());
90  
    }
91  
    if "set input cursor: * to *" {
92  
      chatField.select(m.psi(0), m.psi(1));
93  
      set hijacked; ret "ok";
94  
    }
95  
    if "window title" {
96  
      set monitored; ret getFrameTitle(c);
97  
    }
98  
    if "set window title: *" {
99  
      setFrameTitle(c, m.unq(0));
100  
      set hijacked; ret "ok";
101  
    }
102  
    if "is window focused" {
103  
      set monitored;
104  
      ret yn(getFrame(c).isFocused());
105  
    }
106  
    null;
107  
  }
108  
}

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: 739 / 1939
Referenced in: [show references]