~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to twisted/words/im/jychat.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-17 14:52:35 UTC
  • mfrom: (1.1.5 upstream) (2.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20070117145235-btmig6qfmqfen0om
Tags: 2.5.0-0ubuntu1
New upstream version, compatible with python2.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
 
2
# See LICENSE for details.
 
3
 
 
4
#
 
5
from twisted.words.im.basechat import ContactsList, Conversation, GroupConversation,\
 
6
     ChatUI
 
7
from twisted.words.im.locals import OFFLINE, ONLINE, AWAY
 
8
 
 
9
from java.awt import GridLayout, FlowLayout, BorderLayout, Container
 
10
import sys
 
11
from java.awt.event import ActionListener
 
12
from javax.swing import JTextField, JPasswordField, JComboBox, JPanel, JLabel,\
 
13
     JTextArea, JFrame, JButton, BoxLayout, JTable, JScrollPane, \
 
14
     ListSelectionModel
 
15
from javax.swing.table import DefaultTableModel
 
16
 
 
17
doublebuffered = 0
 
18
 
 
19
 
 
20
class UneditableTableModel(DefaultTableModel):
 
21
    def isCellEditable(self, x, y):
 
22
        return 0
 
23
 
 
24
class _AccountAdder:
 
25
    def __init__(self, contactslist):
 
26
        self.contactslist = contactslist
 
27
        self.mainframe = JFrame("Add New Contact")
 
28
        self.account = JComboBox(self.contactslist.clientsByName.keys())
 
29
        self.contactname = JTextField()
 
30
        self.buildpane()
 
31
 
 
32
    def buildpane(self):
 
33
        buttons = JPanel()
 
34
        buttons.add(JButton("OK", actionPerformed=self.add))
 
35
        buttons.add(JButton("Cancel", actionPerformed=self.cancel))
 
36
 
 
37
        acct = JPanel(GridLayout(1, 2), doublebuffered)
 
38
        acct.add(JLabel("Account"))
 
39
        acct.add(self.account)
 
40
 
 
41
        mainpane = self.mainframe.getContentPane()
 
42
        mainpane.setLayout(BoxLayout(mainpane, BoxLayout.Y_AXIS))
 
43
        mainpane.add(self.contactname)
 
44
        mainpane.add(acct)
 
45
        mainpane.add(buttons)
 
46
        self.mainframe.pack()
 
47
        self.mainframe.show()
 
48
 
 
49
    #action listeners
 
50
    def add(self, ae):
 
51
        acct = self.contactslist.clientsByName[self.account.getSelectedItem()]
 
52
        acct.addContact(self.contactname.getText())
 
53
        self.mainframe.dispose()
 
54
 
 
55
    def cancel(self, ae):
 
56
        self.mainframe.dispose()
 
57
 
 
58
class ContactsListGUI(ContactsList):
 
59
    """A GUI object that displays a contacts list"""
 
60
    def __init__(self, chatui):
 
61
        ContactsList.__init__(self, chatui)
 
62
        self.clientsByName = {}
 
63
        self.mainframe = JFrame("Contacts List")
 
64
        self.headers = ["Contact", "Status", "Idle", "Account"]
 
65
        self.data = UneditableTableModel([], self.headers)
 
66
        self.table = JTable(self.data,
 
67
                            columnSelectionAllowed = 0, #cannot select columns
 
68
                            selectionMode = ListSelectionModel.SINGLE_SELECTION)
 
69
 
 
70
        self.buildpane()
 
71
        self.mainframe.pack()
 
72
        self.mainframe.show()
 
73
 
 
74
    def setContactStatus(self, person):
 
75
        ContactsList.setContactStatus(self, person)
 
76
        self.update()
 
77
 
 
78
    def registerAccountClient(self, client):
 
79
        ContactsList.registerAccountClient(self, client)
 
80
        if not client.accountName in self.clientsByName.keys():
 
81
            self.clientsByName[client.accountName] = client
 
82
 
 
83
    def unregisterAccount(self, client):
 
84
        ContactsList.unregisterAccountClient(self, client)
 
85
        if client.accountName in self.clientsByName.keys():
 
86
            del self.clientsByName[client.accountName]
 
87
 
 
88
    def contactChangedNick(self, person, newnick):
 
89
        ContactsList.contactChangedNick(self, person, newnick)
 
90
        self.update()
 
91
 
 
92
    #GUI code
 
93
    def buildpane(self):
 
94
        buttons = JPanel(FlowLayout(), doublebuffered)
 
95
        buttons.add(JButton("Send Message", actionPerformed=self.message))
 
96
        buttons.add(JButton("Add Contact", actionPerformed=self.addContact))
 
97
        #buttons.add(JButton("Quit", actionPerformed=self.quit))
 
98
 
 
99
        mainpane = self.mainframe.getContentPane()
 
100
        mainpane.setLayout(BoxLayout(mainpane, BoxLayout.Y_AXIS))
 
101
        mainpane.add(JScrollPane(self.table))
 
102
        mainpane.add(buttons)
 
103
        self.update()
 
104
 
 
105
    def update(self):
 
106
        contactdata = []
 
107
        for contact in self.onlineContacts.values():
 
108
            if contact.status == AWAY:
 
109
                stat = "(away)"
 
110
            else:
 
111
                stat = "(active)"
 
112
            contactdata.append([contact.name, stat, contact.getIdleTime(),
 
113
                                contact.client.accountName])
 
114
        self.data.setDataVector(contactdata, self.headers)
 
115
 
 
116
    #callable actionlisteners
 
117
    def message(self, ae):
 
118
        row = self.table.getSelectedRow()
 
119
        if row < 0:
 
120
            print "Trying to send IM to person, but no person selected"
 
121
        else:
 
122
            person = self.onlineContacts[self.data.getValueAt(row, 0)]
 
123
            self.chat.getConversation(person)
 
124
 
 
125
    def addContact(self, ae):
 
126
        _AccountAdder(self)
 
127
 
 
128
    def quit(self, ae):
 
129
        sys.exit()
 
130
 
 
131
 
 
132
class ConversationWindow(Conversation):
 
133
    """A GUI window of a conversation with a specific person"""
 
134
    def __init__(self, person, chatui):
 
135
        """ConversationWindow(basesupport.AbstractPerson:person)"""
 
136
        Conversation.__init__(self, person, chatui)
 
137
        self.mainframe = JFrame("Conversation with "+person.name)
 
138
        self.display = JTextArea(columns=100,
 
139
                                 rows=15,
 
140
                                 editable=0,
 
141
                                 lineWrap=1)
 
142
        self.typepad = JTextField()
 
143
        self.buildpane()
 
144
        self.lentext = 0
 
145
 
 
146
    def buildpane(self):
 
147
        buttons = JPanel(doublebuffered)
 
148
        buttons.add(JButton("Send", actionPerformed=self.send))
 
149
        buttons.add(JButton("Hide", actionPerformed=self.hidewindow))
 
150
 
 
151
        mainpane = self.mainframe.getContentPane()
 
152
        mainpane.setLayout(BoxLayout(mainpane, BoxLayout.Y_AXIS))
 
153
        mainpane.add(JScrollPane(self.display))
 
154
        self.typepad.actionPerformed = self.send
 
155
        mainpane.add(self.typepad)
 
156
        mainpane.add(buttons)
 
157
 
 
158
    def show(self):
 
159
        self.mainframe.pack()
 
160
        self.mainframe.show()
 
161
 
 
162
    def hide(self):
 
163
        self.mainframe.hide()
 
164
 
 
165
    def sendText(self, text):
 
166
        self.displayText("\n"+self.person.client.name+": "+text)
 
167
        Conversation.sendText(self, text)
 
168
 
 
169
    def showMessage(self, text, metadata=None):
 
170
        self.displayText("\n"+self.person.name+": "+text)
 
171
 
 
172
    def contactChangedNick(self, person, newnick):
 
173
        Conversation.contactChangedNick(self, person, newnick)
 
174
        self.mainframe.setTitle("Conversation with "+newnick)
 
175
 
 
176
    #GUI code
 
177
    def displayText(self, text):
 
178
        self.lentext = self.lentext + len(text)
 
179
        self.display.append(text)
 
180
        self.display.setCaretPosition(self.lentext)
 
181
 
 
182
    #actionlisteners
 
183
    def hidewindow(self, ae):
 
184
        self.hide()
 
185
 
 
186
    def send(self, ae):
 
187
        text = self.typepad.getText()
 
188
        self.typepad.setText("")
 
189
        if text != "" and text != None:
 
190
            self.sendText(text)
 
191
 
 
192
 
 
193
class GroupConversationWindow(GroupConversation):
 
194
    """A GUI window of a conversation witha  group of people"""
 
195
    def __init__(self, group, chatui):
 
196
        GroupConversation.__init__(self, group, chatui)
 
197
        self.mainframe = JFrame(self.group.name)
 
198
        self.headers = ["Member"]
 
199
        self.memberdata = UneditableTableModel([], self.headers)
 
200
        self.display = JTextArea(columns=100, rows=15, editable=0, lineWrap=1)
 
201
        self.typepad = JTextField()
 
202
        self.buildpane()
 
203
        self.lentext = 0
 
204
 
 
205
    def show(self):
 
206
        self.mainframe.pack()
 
207
        self.mainframe.show()
 
208
 
 
209
    def hide(self):
 
210
        self.mainframe.hide()
 
211
 
 
212
    def showGroupMessage(self, sender, text, metadata=None):
 
213
        self.displayText(sender + ": " + text)
 
214
 
 
215
    def setGroupMembers(self, members):
 
216
        GroupConversation.setGroupMembers(self, members)
 
217
        self.updatelist()
 
218
 
 
219
    def setTopic(self, topic, author):
 
220
        topictext = "Topic: " + topic + ", set by " + author
 
221
        self.mainframe.setTitle(self.group.name + ": " + topictext)
 
222
        self.displayText(topictext)
 
223
 
 
224
    def memberJoined(self, member):
 
225
        GroupConversation.memberJoined(self, member)
 
226
        self.updatelist()
 
227
 
 
228
    def memberChangedNick(self, oldnick, newnick):
 
229
        GroupConversation.memberChangedNick(self, oldnick, newnick)
 
230
        self.updatelist()
 
231
 
 
232
    def memberLeft(self, member):
 
233
        GroupConversation.memberLeft(self, member)
 
234
        self.updatelist()
 
235
 
 
236
    #GUI code
 
237
    def buildpane(self):
 
238
        buttons = JPanel(doublebuffered)
 
239
        buttons.add(JButton("Hide", actionPerformed=self.hidewindow))
 
240
 
 
241
        memberpane = JTable(self.memberdata)
 
242
        memberframe = JScrollPane(memberpane)
 
243
 
 
244
        chat = JPanel(doublebuffered)
 
245
        chat.setLayout(BoxLayout(chat, BoxLayout.Y_AXIS))
 
246
        chat.add(JScrollPane(self.display))
 
247
        self.typepad.actionPerformed = self.send
 
248
        chat.add(self.typepad)
 
249
        chat.add(buttons)
 
250
 
 
251
        mainpane = self.mainframe.getContentPane()
 
252
        mainpane.setLayout(BoxLayout(mainpane, BoxLayout.X_AXIS))
 
253
        mainpane.add(chat)
 
254
        mainpane.add(memberframe)
 
255
 
 
256
    def displayText(self, text):
 
257
        self.lentext = self.lentext + len(text)
 
258
        self.display.append(text)
 
259
        self.display.setCaretPosition(self.lentext)
 
260
 
 
261
    def updatelist(self):
 
262
        self.memberdata.setDataVector([self.members], self.headers)
 
263
 
 
264
    #actionListener
 
265
    def send(self, ae):
 
266
        text = self.typepad.getText()
 
267
        self.typepad.setText("")
 
268
        if text != "" and text != None:
 
269
            GroupConversation.sendText(self, text)
 
270
 
 
271
    def hidewindow(self, ae):
 
272
        self.hide()
 
273
 
 
274
class JyChatUI(ChatUI):
 
275
    def __init__(self):
 
276
        ChatUI.__init__(self)
 
277
        self.contactsList = ContactsListGUI(self)
 
278
 
 
279
    def getConversation(self, person, stayHidden=0):
 
280
        return ChatUI.getGroupConversation(self, person, ConversationWindow,
 
281
                                            stayHidden)
 
282
 
 
283
    def getGroupConversation(self, group, stayHidden=0):
 
284
        return ChatUI.getGroupConversation(self, group,
 
285
                                            GroupConversationWindow,
 
286
                                            stayHidden)