Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2649f8074f3cee6e4bfb203725acb9e713da3d62 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/****************************************************************************
* Copyright (c) 2004 Composent, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*    Composent, Inc. - initial API and implementation
*****************************************************************************/

package org.eclipse.ecf.ui.views;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.eclipse.core.runtime.Status;
import org.eclipse.ecf.core.user.IUser;
import org.eclipse.ecf.ui.Trace;
import org.eclipse.ecf.ui.UiPlugin;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.TextViewer;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.PlatformUI;

public class TextChatComposite extends Composite {
    
    public static final Trace trace = Trace.create("textchatcomposite");
    
    protected static final int DEFAULT_INPUT_HEIGHT = 25;
    protected static final int DEFAULT_INPUT_SEPARATOR = 5;
    
    protected String TEXT_INPUT_INIT = MessageLoader
    .getString("TextChatComposite.textinputinit");

    protected Color meColor = null;
    protected Color otherColor = null;
    protected Color systemColor = null;

    protected StyledText styledText;
    protected TextViewer textoutput;
    protected Text textinput;

    protected int [] sashWeights = new int[] { 7, 2 };
    
    protected boolean isTyping;
    protected String initText;
    protected ILocalInputHandler inputHandler;
    SimpleDateFormat df = new SimpleDateFormat("hh:mm a");
    protected IUser localUser;
    protected IUser remoteUser;
    protected boolean showTimestamp = true;
    
    private Action outputClear = null;
    private Action outputCopy = null;
    private Action outputPaste = null;
    private Action outputSelectAll = null;

    public TextChatComposite(Composite parent, int style, String initText, ILocalInputHandler handler, IUser localUser, IUser remoteUser) {
        super(parent, style);

        this.initText = initText;
        this.inputHandler = handler;
        
        this.localUser = localUser;
        this.remoteUser = remoteUser;
        
        this.meColor = new Color(getShell().getDisplay(), 23, 135, 65);
        this.otherColor = new Color(getShell().getDisplay(), 65, 13, 165);
        this.systemColor = new Color(getShell().getDisplay(), 123, 135, 165);
        
        this.addDisposeListener(new DisposeListener() {

            public void widgetDisposed(DisposeEvent e) {
                meColor.dispose();
                otherColor.dispose();
                systemColor.dispose();
            }
            
        });
        
        setLayout(new GridLayout());
        SashForm sash = new SashForm(this, SWT.VERTICAL | SWT.SMOOTH);
        sash.setLayoutData(new GridData(GridData.FILL_BOTH));

        // Setup text output
        textoutput = new TextViewer(sash, SWT.BORDER | SWT.V_SCROLL | SWT.WRAP);
        styledText = textoutput.getTextWidget();
        styledText.setEditable(false);
        textoutput.setDocument(new Document(this.initText));

        textoutput.setEditable(false);
        
        // Setup text input
        textinput = new Text(sash, SWT.WRAP | SWT.BORDER | SWT.V_SCROLL);
        textinput.setText(TEXT_INPUT_INIT);
        
        textinput.selectAll();
        textinput.addKeyListener(new KeyListener() {
            public void keyPressed(KeyEvent evt) {
                handleKeyPressed(evt);
            }

            public void keyReleased(KeyEvent evt) {
            }
        });
        textinput.addFocusListener(new FocusListener() {
            public void focusGained(FocusEvent e) {
                String t = textinput.getText();
                if (t.equals(TEXT_INPUT_INIT)) {
                    textinput.selectAll();
                }
            }

            public void focusLost(FocusEvent e) {
            }
        });
        textinput.addMouseListener(new MouseListener() {
            public void mouseDoubleClick(MouseEvent e) {
            }

            public void mouseDown(MouseEvent e) {
            }

            public void mouseUp(MouseEvent e) {
                String t = textinput.getText();
                if (t.equals(TEXT_INPUT_INIT)) {
                    textinput.selectAll();
                }
            }
        });
        textinput.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {             
                if (isTyping && textinput.getText().trim().length() == 0)
                    isTyping = false;
                else if (!isTyping) {
                    isTyping = true;
                    sendStartedTyping();
                }
            }
        });

        sash.setWeights(sashWeights);

        makeActions();
        hookContextMenu();
        
        UiPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(new IPropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent event) {
                if (event.getProperty().equals(UiPlugin.PREF_DISPLAY_TIMESTAMP)) {
                    showTimestamp = ((Boolean)event.getNewValue()).booleanValue();
                }   
            }
            
        });

    }

    private void makeActions() {
        outputSelectAll = new Action() {
            public void run() {
                outputSelectAll();
            }
        };
        outputSelectAll.setText("Select All");
        outputSelectAll.setAccelerator(SWT.CTRL | 'A');
        outputCopy = new Action() {
            public void run() {
                outputCopy();
            }
        };
        outputCopy.setText("Copy");
        outputCopy.setAccelerator(SWT.CTRL | 'C');
        outputCopy.setImageDescriptor(PlatformUI.getWorkbench()
                .getSharedImages().getImageDescriptor(
                        ISharedImages.IMG_TOOL_COPY));

        outputClear = new Action() {
            public void run() {
                outputClear();
            }
        };
        outputClear.setText("Clear");

        outputPaste = new Action() {
            public void run() {
                outputPaste();
            }
        };
        outputPaste.setText("Paste");
        outputCopy.setAccelerator(SWT.CTRL | 'V');
        outputPaste.setImageDescriptor(PlatformUI.getWorkbench()
                .getSharedImages().getImageDescriptor(
                        ISharedImages.IMG_TOOL_PASTE));

    }

    protected void outputClear() {
        if (MessageDialog.openConfirm(null, "Confirm Clear Text Output",
                "Are you sure you want to clear output?"))
            textoutput.getTextWidget().setText("");
    }

    protected void outputCopy() {
        String t = textoutput.getTextWidget().getSelectionText();
        if (t == null || t.length() == 0) {
            textoutput.getTextWidget().selectAll();
        }
        textoutput.getTextWidget().copy();
        textoutput.getTextWidget().setSelection(
                textoutput.getTextWidget().getText().length());
    }

    protected void outputPaste() {
        textinput.paste();
    }

    protected void outputSelectAll() {
        textoutput.getTextWidget().selectAll();
    }

    private void hookContextMenu() {
        MenuManager menuMgr = new MenuManager("#PopupMenu");
        menuMgr.setRemoveAllWhenShown(true);
        menuMgr.addMenuListener(new IMenuListener() {
            public void menuAboutToShow(IMenuManager manager) {
                fillContextMenu(manager);
            }
        });
        Menu menu = menuMgr.createContextMenu(textoutput.getControl());
        textoutput.getControl().setMenu(menu);
        PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart().getSite().registerContextMenu(menuMgr,textoutput);
        //registerContextMenu(menuMgr, textoutput);
    }

    private void fillContextMenu(IMenuManager manager) {
        manager.add(outputCopy);
        manager.add(outputPaste);
        manager.add(outputClear);
        manager.add(new Separator());
        manager.add(outputSelectAll);
        manager.add(new Separator());
        manager.add(new Separator());
        // Other plug-ins can contribute there actions here
        manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
    }

    public void setLocalUser(IUser newUser) {
        this.localUser = newUser;
    }
    public void setRemoteUser(IUser remoteUser) {
        this.remoteUser = remoteUser;
    }
    public IUser getRemoteUser() {
        return this.remoteUser;
    }
    protected String getCurrentDateTime() {
        StringBuffer sb = new StringBuffer("(");
        sb.append(df.format(new Date())).append(") ");
        return sb.toString();
    }
    private String makeLineWithTimestamp(String line) {
        if (showTimestamp) {
            return getCurrentDateTime() + line;
        }
        return line;
    }

    public IUser getLocalUser() {
        return localUser;
    }
    public void appendText(ChatLine text) {
        StyledText st = textoutput.getTextWidget();
        
        if (text == null || textoutput == null || st == null)
            return;

        int startRange = st.getText().length();
        StringBuffer sb = new StringBuffer();
        
        if (text.getOriginator() != null) {
            sb.append(makeLineWithTimestamp(text.getOriginator().getName() + ": "));
            StyleRange sr = new StyleRange();
            sr.start = startRange;
            sr.length = sb.length();
            IUser lu = getLocalUser();
            if (lu != null && lu.getID().equals(text.getOriginator().getID())) { 
                sr.foreground = meColor;
            } else {
                sr.foreground = otherColor;
            }
            st.append(sb.toString());
            st.setStyleRange(sr);
        }
        
        int beforeMessageIndex = st.getText().length();
        
        st.append(text.getText());
        
        if (text.getOriginator() == null) {
            StyleRange sr = new StyleRange();
            sr.start = beforeMessageIndex;
            sr.length = text.getText().length();
            sr.foreground = systemColor;
            st.setStyleRange(sr);
        }
        
        if (!text.isNoCRLF()) {
            st.append("\n");
        }
        
        String t = st.getText();
        if (t == null)
            return;
        st.setSelection(t.length());
    }

    protected void handleKeyPressed(KeyEvent evt) {
        if (evt.character == SWT.CR) {
            handleEnter();
            evt.doit = false;
        } 
    }

    protected void handleEnter() {
        String text = textinput.getText().trim();
        if(text.length() > 0) sendTextLineInput(text);
        
        clearInput();
        isTyping = false;
    }

    protected void clearInput() {
        textinput.setText("");
    }

    protected void sendTextLineInput(String text) {
        if (inputHandler != null) {
            IUser lu = getLocalUser();
            IUser ru = getRemoteUser();
            if (lu != null && ru != null) {
                inputHandler.inputText(ru.getID(),text);
                appendText(new ChatLine(text,lu));
            } else {
                UiPlugin.getDefault().getLog().log(new Status(Status.ERROR,UiPlugin.PLUGIN_ID,100,"Null localUser or remoteUser for textchatcomposite",new NullPointerException()));
            }
        } else {
            UiPlugin.getDefault().getLog().log(new Status(Status.ERROR,UiPlugin.PLUGIN_ID,100,"No inputhandler available for textchatcomposite",new NullPointerException()));
        }
    }

    protected void sendStartedTyping() {
        if (inputHandler != null) {
            IUser lu = getLocalUser();
            IUser ru = getRemoteUser();
            if (lu != null && ru != null) {
                inputHandler.startTyping(ru.getID());
            } else {
                UiPlugin.getDefault().getLog().log(new Status(Status.ERROR,UiPlugin.PLUGIN_ID,100,"Null localUser or remoteUser for textchatcomposite",new NullPointerException()));
            }
        } else {
            UiPlugin.getDefault().getLog().log(new Status(Status.ERROR,UiPlugin.PLUGIN_ID,100,"No inputhandler available for textchatcomposite",new NullPointerException()));
        }
    }
    
    protected String getShellName() {
        return "org.eclipse.ecf.ui.views.TextChatComposite";
    }
    public void dispose() {
        super.dispose();
    }

    protected void setDisposed() {
        textinput.setEnabled(false);
    }
    protected void checkSubclass() {
    }
}

Back to the top