~smaioli/azureus/ubuntu-experimental

« back to all changes in this revision

Viewing changes to com/aelitis/azureus/ui/swt/views/skin/MediaList.java

MergedĀ VuzeĀ 4.2.0.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * Copyright (C) 2006 Aelitis, All Rights Reserved.
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or
5
 
 * modify it under the terms of the GNU General Public License
6
 
 * as published by the Free Software Foundation; either version 2
7
 
 * of the License, or (at your option) any later version.
8
 
 * This program is distributed in the hope that it will be useful,
9
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
 
 * GNU General Public License for more details.
12
 
 * You should have received a copy of the GNU General Public License
13
 
 * along with this program; if not, write to the Free Software
14
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15
 
 *
16
 
 * AELITIS, SAS au capital de 63.529,40 euros
17
 
 * 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
18
 
 *
19
 
 */
20
 
 
21
 
package com.aelitis.azureus.ui.swt.views.skin;
22
 
 
23
 
import java.io.ByteArrayInputStream;
24
 
import java.util.regex.Pattern;
25
 
 
26
 
import org.eclipse.swt.SWT;
27
 
import org.eclipse.swt.events.*;
28
 
import org.eclipse.swt.graphics.Color;
29
 
import org.eclipse.swt.graphics.Font;
30
 
import org.eclipse.swt.graphics.Image;
31
 
import org.eclipse.swt.layout.FormData;
32
 
import org.eclipse.swt.widgets.*;
33
 
 
34
 
import org.gudy.azureus2.core3.download.DownloadManager;
35
 
import org.gudy.azureus2.core3.global.GlobalManager;
36
 
import org.gudy.azureus2.core3.internat.MessageText;
37
 
import org.gudy.azureus2.core3.torrent.TOTorrent;
38
 
import org.gudy.azureus2.core3.util.*;
39
 
import org.gudy.azureus2.ui.swt.ImageRepository;
40
 
import org.gudy.azureus2.ui.swt.Messages;
41
 
import org.gudy.azureus2.ui.swt.Utils;
42
 
import org.gudy.azureus2.ui.swt.mainwindow.Colors;
43
 
import org.gudy.azureus2.ui.swt.views.table.TableRowSWT;
44
 
 
45
 
import com.aelitis.azureus.core.AzureusCore;
46
 
import com.aelitis.azureus.core.AzureusCoreFactory;
47
 
import com.aelitis.azureus.core.torrent.MetaDataUpdateListener;
48
 
import com.aelitis.azureus.core.torrent.PlatformTorrentUtils;
49
 
import com.aelitis.azureus.ui.common.table.TableCountChangeListener;
50
 
import com.aelitis.azureus.ui.common.table.TableRowCore;
51
 
import com.aelitis.azureus.ui.common.table.TableSelectionAdapter;
52
 
import com.aelitis.azureus.ui.selectedcontent.SelectedContentManager;
53
 
import com.aelitis.azureus.ui.swt.skin.*;
54
 
import com.aelitis.azureus.ui.swt.views.TorrentListView;
55
 
import com.aelitis.azureus.ui.swt.views.TorrentListViewListener;
56
 
import com.aelitis.azureus.ui.swt.views.list.ListRow;
57
 
import com.aelitis.azureus.ui.swt.views.list.ListView;
58
 
import com.aelitis.azureus.util.PublishUtils;
59
 
 
60
 
/**
61
 
 * @author TuxPaper
62
 
 * @created Oct 12, 2006
63
 
 *
64
 
 */
65
 
public class MediaList
66
 
        extends SkinView
67
 
{
68
 
        private static final int ASYOUTYPE_UPDATEDELAY = 150;
69
 
 
70
 
        private SWTSkinObjectText lblCountAreaNotOurs;
71
 
 
72
 
        private SWTSkinObjectText lblCountAreaOurs;
73
 
 
74
 
        private TorrentListView view;
75
 
 
76
 
        private String PREFIX = "my-media-";
77
 
 
78
 
        private AzureusCore core;
79
 
 
80
 
        private SWTSkinObjectImage skinImgThumb;
81
 
 
82
 
        private SWTSkinObjectText skinDetailInfo;
83
 
 
84
 
        private MetaDataUpdateListener listener;
85
 
 
86
 
        protected String sLastSearch = "";
87
 
 
88
 
        private Text txtFilter;
89
 
 
90
 
        private boolean bRegexSearch;
91
 
 
92
 
        private Label lblX;
93
 
 
94
 
        private TimerEvent searchUpdateEvent;
95
 
 
96
 
        // @see com.aelitis.azureus.ui.swt.views.skin.SkinView#showSupport(com.aelitis.azureus.ui.swt.skin.SWTSkinObject, java.lang.Object)
97
 
        public Object skinObjectInitialShow(SWTSkinObject skinObject, Object params) {
98
 
                SelectedContentManager.changeCurrentlySelectedContent(PREFIX, null, view);
99
 
 
100
 
                core = AzureusCoreFactory.getSingleton();
101
 
                view = new TorrentListView(this, PREFIX, TorrentListView.VIEW_MY_MEDIA,
102
 
                                false, true) {
103
 
                        public boolean isOurDownload(DownloadManager dm) {
104
 
                                if (PlatformTorrentUtils.getAdId(dm.getTorrent()) != null) {
105
 
                                        return false;
106
 
                                }
107
 
                                if (sLastSearch.length() == 0) {
108
 
                                        return true;
109
 
                                }
110
 
 
111
 
                                boolean bOurs = true;
112
 
                                try {
113
 
                                        String[][] names = {
114
 
                                                {
115
 
                                                        "",
116
 
                                                        dm.getDisplayName()
117
 
                                                },
118
 
                                                {
119
 
                                                        "t:",
120
 
                                                        dm.getTorrent().getAnnounceURL().getHost()
121
 
                                                },
122
 
                                                {
123
 
                                                        "st:",
124
 
                                                        "" + dm.getState()
125
 
                                                }
126
 
                                        };
127
 
 
128
 
                                        String name = names[0][1];
129
 
                                        String tmpSearch = sLastSearch;
130
 
 
131
 
                                        for (int i = 0; i < names.length; i++) {
132
 
                                                if (tmpSearch.startsWith(names[i][0])) {
133
 
                                                        tmpSearch = tmpSearch.substring(names[i][0].length());
134
 
                                                        name = names[i][1];
135
 
                                                }
136
 
                                        }
137
 
 
138
 
                                        String s = bRegexSearch ? tmpSearch : "\\Q"
139
 
                                                        + tmpSearch.replaceAll("[|;]", "\\\\E|\\\\Q") + "\\E";
140
 
                                        Pattern pattern = Pattern.compile(s, Pattern.CASE_INSENSITIVE);
141
 
 
142
 
                                        if (!pattern.matcher(name).find()) {
143
 
                                                bOurs = false;
144
 
                                        }
145
 
                                } catch (Exception e) {
146
 
                                        // Future: report PatternSyntaxException message to user.
147
 
                                }
148
 
                                return bOurs;
149
 
                        }
150
 
                };
151
 
                
152
 
                if (view instanceof ListView) {
153
 
                        ((ListView) view).addCountChangeListener(new TableCountChangeListener() {
154
 
 
155
 
                                public void rowRemoved(TableRowCore row) {
156
 
                                }
157
 
 
158
 
                                public void rowAdded(TableRowCore row) {
159
 
                                        Object dataSource = row.getDataSource(true);
160
 
                                        if (dataSource instanceof DownloadManager) {
161
 
                                                updateRowFGColor((DownloadManager) dataSource);
162
 
                                        }
163
 
                                }
164
 
                        });
165
 
                }
166
 
 
167
 
                view.addListener(new TorrentListViewListener() {
168
 
                        boolean countChanging = false;
169
 
 
170
 
                        // @see com.aelitis.azureus.ui.swt.views.TorrentListViewListener#stateChanged(org.gudy.azureus2.core3.download.DownloadManager)
171
 
 
172
 
                        public void stateChanged(final DownloadManager manager) {
173
 
                                updateRowFGColor(manager);
174
 
                        }
175
 
 
176
 
                        // @see com.aelitis.azureus.ui.swt.views.TorrentListViewListener#countChanged()
177
 
                        public void countChanged() {
178
 
                                if (countChanging) {
179
 
                                        return;
180
 
                                }
181
 
 
182
 
                                countChanging = true;
183
 
                                Utils.execSWTThreadLater(0, new AERunnable() {
184
 
                                        public void runSupport() {
185
 
                                                countChanging = false;
186
 
 
187
 
                                                long totalOurs = 0;
188
 
                                                long totalNotOurs = 0;
189
 
 
190
 
                                                GlobalManager globalManager = core.getGlobalManager();
191
 
                                                Object[] dms = globalManager.getDownloadManagers().toArray();
192
 
 
193
 
                                                for (int i = 0; i < dms.length; i++) {
194
 
                                                        DownloadManager dm = (DownloadManager) dms[i];
195
 
                                                        if (dm.isDownloadComplete(false)) {
196
 
                                                                if (PublishUtils.isPublished(dm)) {
197
 
                                                                        totalOurs++;
198
 
                                                                } else {
199
 
                                                                        totalNotOurs++;
200
 
                                                                }
201
 
                                                        }
202
 
                                                }
203
 
 
204
 
                                                if (lblCountAreaOurs != null) {
205
 
                                                        lblCountAreaOurs.setText(MessageText.getString("v3.MainWindow."
206
 
                                                                        + PREFIX + "ours.count", new String[] {
207
 
                                                                "" + totalOurs
208
 
                                                        }));
209
 
                                                }
210
 
                                                if (lblCountAreaNotOurs != null) {
211
 
                                                        lblCountAreaNotOurs.setText(MessageText.getString(
212
 
                                                                        "v3.MainWindow." + PREFIX + "notours.count", new String[] {
213
 
                                                                                "" + totalNotOurs
214
 
                                                                        }));
215
 
                                                        lblCountAreaNotOurs.getControl().getParent().layout(true, true);
216
 
                                                }
217
 
 
218
 
                                                int count = view.getSelectedRowsSize();
219
 
                                                if (count == 0 || count > 1) {
220
 
                                                        updateDetailsInfo();
221
 
                                                }
222
 
                                        }
223
 
                                });
224
 
                        }
225
 
                });
226
 
 
227
 
                skinObject = getSkinObject(PREFIX + "bigthumb");
228
 
                if (skinObject instanceof SWTSkinObjectImage) {
229
 
                        listener = new MetaDataUpdateListener() {
230
 
                                public void metaDataUpdated(TOTorrent torrent) {
231
 
                                        ListRow rowFocused = view.getRowFocused();
232
 
                                        if (rowFocused != null) {
233
 
                                                DownloadManager dm = (DownloadManager) rowFocused.getDataSource(true);
234
 
                                                if (dm.getTorrent().equals(torrent)) {
235
 
                                                        update();
236
 
                                                }
237
 
                                        }
238
 
                                }
239
 
                        };
240
 
                        PlatformTorrentUtils.addListener(listener);
241
 
 
242
 
                        skinImgThumb = (SWTSkinObjectImage) skinObject;
243
 
                        view.addSelectionListener(new TableSelectionAdapter() {
244
 
                                public void deselected(TableRowCore[] rows) {
245
 
                                        update();
246
 
                                }
247
 
 
248
 
                                public void selected(TableRowCore[] rows) {
249
 
                                        update();
250
 
                                }
251
 
 
252
 
                                public void focusChanged(TableRowCore focusedRow) {
253
 
                                        update();
254
 
                                }
255
 
 
256
 
                        }, false);
257
 
                }
258
 
 
259
 
                skinObject = getSkinObject(PREFIX + "detail-info");
260
 
                if (skinObject instanceof SWTSkinObjectText) {
261
 
                        skinDetailInfo = (SWTSkinObjectText) skinObject;
262
 
                        view.addSelectionListener(new TableSelectionAdapter() {
263
 
                                public void deselected(TableRowCore[] rows) {
264
 
                                        updateDetailsInfo();
265
 
                                }
266
 
 
267
 
                                public void selected(TableRowCore[] rows) {
268
 
                                        updateDetailsInfo();
269
 
                                }
270
 
 
271
 
                                public void focusChanged(TableRowCore focusedRow) {
272
 
                                        updateDetailsInfo();
273
 
                                }
274
 
                        }, true);
275
 
                }
276
 
 
277
 
                skinObject = getSkinObject(PREFIX + "filter-box");
278
 
                if (skinObject != null) {
279
 
                        Control control = skinObject.getControl();
280
 
                        if (control instanceof Composite) {
281
 
                                final Composite composite = (Composite) control;
282
 
                                txtFilter = new Text(composite, SWT.BORDER);
283
 
                                txtFilter.addModifyListener(new ModifyListener() {
284
 
                                        public void modifyText(ModifyEvent e) {
285
 
                                                sLastSearch = ((Text) e.widget).getText();
286
 
                                                updateLastSearch();
287
 
                                        }
288
 
                                });
289
 
                                FormData formData = Utils.getFilledFormData();
290
 
                                formData.top = null;
291
 
                                txtFilter.setLayoutData(formData);
292
 
                                composite.layout();
293
 
 
294
 
                                int h = txtFilter.getClientArea().height - (Constants.isOSX ? 0 : 2);
295
 
                                Font font = Utils.getFontWithHeight(txtFilter.getFont(), null, h);
296
 
 
297
 
                                composite.getParent().layout();
298
 
 
299
 
                                if (font != null) {
300
 
                                        final Font fFont = font;
301
 
                                        txtFilter.setFont(fFont);
302
 
 
303
 
                                        txtFilter.addDisposeListener(new DisposeListener() {
304
 
                                                public void widgetDisposed(DisposeEvent e) {
305
 
                                                        if (fFont != null && !fFont.isDisposed()) {
306
 
                                                                txtFilter.setFont(null);
307
 
                                                                fFont.dispose();
308
 
                                                        }
309
 
                                                }
310
 
                                        });
311
 
                                }
312
 
 
313
 
                                txtFilter.addKeyListener(new KeyListener() {
314
 
                                        public void keyReleased(KeyEvent e) {
315
 
                                        }
316
 
 
317
 
                                        public void keyPressed(KeyEvent e) {
318
 
                                                int key = e.character;
319
 
                                                if (key <= 26 && key > 0)
320
 
                                                        key += 'a' - 1;
321
 
                                                if (e.stateMask == SWT.CONTROL) {
322
 
                                                        if (key == 'x') {
323
 
                                                                bRegexSearch = !bRegexSearch;
324
 
                                                                e.doit = false;
325
 
                                                                updateLastSearch();
326
 
                                                        }
327
 
                                                }
328
 
                                        }
329
 
                                });
330
 
 
331
 
                                view.addKeyListener(new KeyListener() {
332
 
                                        public void keyReleased(KeyEvent e) {
333
 
                                        }
334
 
 
335
 
                                        public void keyPressed(KeyEvent e) {
336
 
                                                int key = e.character;
337
 
                                                if (key <= 26 && key > 0)
338
 
                                                        key += 'a' - 1;
339
 
                                                if (e.stateMask == SWT.CONTROL) {
340
 
                                                        if (key == 'x') {
341
 
                                                                bRegexSearch = !bRegexSearch;
342
 
                                                                e.doit = false;
343
 
                                                                updateLastSearch();
344
 
                                                                return;
345
 
                                                        }
346
 
                                                }
347
 
 
348
 
                                                if (e.keyCode != SWT.BS) {
349
 
                                                        if ((e.stateMask & (~SWT.SHIFT)) != 0 || e.character < 32
350
 
                                                                        || e.character == 127) {
351
 
                                                                return;
352
 
                                                        }
353
 
                                                }
354
 
 
355
 
                                                if (e.keyCode == SWT.BS) {
356
 
                                                        if (e.stateMask == SWT.CONTROL) {
357
 
                                                                sLastSearch = "";
358
 
                                                        } else if (sLastSearch.length() > 0) {
359
 
                                                                sLastSearch = sLastSearch.substring(0, sLastSearch.length() - 1);
360
 
                                                        }
361
 
                                                } else {
362
 
                                                        sLastSearch += String.valueOf(e.character);
363
 
                                                }
364
 
 
365
 
                                                if (txtFilter != null && !txtFilter.isDisposed()) {
366
 
                                                        txtFilter.setFocus();
367
 
                                                }
368
 
                                                updateLastSearch();
369
 
 
370
 
                                                e.doit = false;
371
 
                                        }
372
 
 
373
 
                                });
374
 
                        }
375
 
                }
376
 
 
377
 
                return null;
378
 
        }
379
 
 
380
 
        /**
381
 
         * @param manager
382
 
         *
383
 
         * @since 3.0.4.3
384
 
         */
385
 
        protected void updateRowFGColor(DownloadManager manager) {
386
 
                if (manager == null) {
387
 
                        return;
388
 
                }
389
 
                TableRowSWT row = view.getRowSWT(manager);
390
 
                if (row == null) {
391
 
                        return;
392
 
                }
393
 
                if (manager.isDownloadComplete(false)) {
394
 
                        row.setForeground((Color) null);
395
 
                } else {
396
 
                        Color c = view.getSkinProperties().getColor("color.library.incomplete");
397
 
                        row.setForeground(c);
398
 
                }
399
 
        }
400
 
 
401
 
        /**
402
 
         * 
403
 
         */
404
 
        protected void updateLastSearch() {
405
 
                if (txtFilter != null && !txtFilter.isDisposed()) {
406
 
                        if (!sLastSearch.equals(txtFilter.getText())) {
407
 
                                txtFilter.setText(sLastSearch);
408
 
                                txtFilter.setSelection(sLastSearch.length());
409
 
                        }
410
 
 
411
 
                        if (bRegexSearch) {
412
 
                                try {
413
 
                                        Pattern.compile(sLastSearch, Pattern.CASE_INSENSITIVE);
414
 
                                        Display display = txtFilter.getDisplay();
415
 
                                        txtFilter.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
416
 
                                        txtFilter.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
417
 
                                        Messages.setLanguageTooltip(txtFilter,
418
 
                                                        "MyTorrentsView.filter.tooltip");
419
 
                                } catch (Exception e) {
420
 
                                        txtFilter.setBackground(Colors.colorErrorBG);
421
 
                                        txtFilter.setForeground(null);
422
 
                                        txtFilter.setToolTipText(e.getMessage());
423
 
                                }
424
 
                        } else {
425
 
                                txtFilter.setBackground(null);
426
 
                                txtFilter.setForeground(null);
427
 
                                Messages.setLanguageTooltip(txtFilter, "MyTorrentsView.filter.tooltip");
428
 
                        }
429
 
                }
430
 
                if (lblX != null && !lblX.isDisposed()) {
431
 
                        Image img = ImageRepository.getImage(sLastSearch.length() > 0 ? "smallx"
432
 
                                        : "smallx-gray");
433
 
 
434
 
                        lblX.setImage(img);
435
 
                }
436
 
 
437
 
                if (searchUpdateEvent != null) {
438
 
                        searchUpdateEvent.cancel();
439
 
                }
440
 
                searchUpdateEvent = SimpleTimer.addEvent("SearchUpdate",
441
 
                                SystemTime.getOffsetTime(ASYOUTYPE_UPDATEDELAY),
442
 
                                new TimerEventPerformer() {
443
 
                                        public void perform(TimerEvent event) {
444
 
                                                if (searchUpdateEvent.isCancelled()) {
445
 
                                                        searchUpdateEvent = null;
446
 
                                                        return;
447
 
                                                }
448
 
                                                searchUpdateEvent = null;
449
 
                                                doFilter();
450
 
                                        }
451
 
                                });
452
 
        }
453
 
 
454
 
        /**
455
 
         * 
456
 
         */
457
 
        protected void doFilter() {
458
 
                view.tableStructureChanged();
459
 
        }
460
 
 
461
 
        private void updateDetailsInfo() {
462
 
                if (skinDetailInfo == null) {
463
 
                        return;
464
 
                }
465
 
                int count = view.getSelectedRowsSize();
466
 
                if (count == 0 || count > 1) {
467
 
                        int completed = 0;
468
 
                        ListRow[] rowsUnsorted = view.getRowsUnsorted();
469
 
 
470
 
                        int all = rowsUnsorted.length;
471
 
                        for (int i = 0; i < all; i++) {
472
 
                                ListRow row = rowsUnsorted[i];
473
 
                                DownloadManager dm = (DownloadManager) row.getDataSource(true);
474
 
                                if (dm != null) {
475
 
                                        if (dm.isDownloadComplete(false)) {
476
 
                                                completed++;
477
 
                                        }
478
 
                                }
479
 
 
480
 
                        }
481
 
 
482
 
                        skinDetailInfo.setText(MessageText.getString(
483
 
                                        "v3.MainWindow.myMedia.noneSelected", new String[] {
484
 
                                                "" + all,
485
 
                                                "" + completed
486
 
                                        }));
487
 
                        return;
488
 
                }
489
 
                TableRowCore[] rows = view.getSelectedRows();
490
 
                String sText = "";
491
 
                DownloadManager dm = (DownloadManager) rows[0].getDataSource(true);
492
 
                if (dm != null) {
493
 
                        TOTorrent torrent = dm.getTorrent();
494
 
                        String s;
495
 
                        s = PlatformTorrentUtils.getContentTitle(torrent);
496
 
                        if (s != null) {
497
 
                                sText += s + "\n\n";
498
 
                        }
499
 
 
500
 
                        s = PlatformTorrentUtils.getContentDescription(torrent);
501
 
                        if (s != null) {
502
 
                                sText += s + "\n";
503
 
                        }
504
 
                }
505
 
                skinDetailInfo.setText(sText);
506
 
        }
507
 
 
508
 
        private void update() {
509
 
                Utils.execSWTThread(new AERunnable() {
510
 
                        public void runSupport() {
511
 
                                int count = view.getSelectedRowsSize();
512
 
                                if (count != 1) {
513
 
                                        skinImgThumb.setImage(null);
514
 
                                        return;
515
 
                                }
516
 
                                TableRowCore[] rows = view.getSelectedRows();
517
 
                                Image image = null;
518
 
                                DownloadManager dm = (DownloadManager) rows[0].getDataSource(true);
519
 
                                if (dm != null) {
520
 
                                        byte[] imageBytes = PlatformTorrentUtils.getContentThumbnail(dm.getTorrent());
521
 
                                        if (imageBytes != null) {
522
 
                                                ByteArrayInputStream bais = new ByteArrayInputStream(imageBytes);
523
 
                                                image = new Image(skinImgThumb.getControl().getDisplay(), bais);
524
 
                                        }
525
 
                                }
526
 
                                Image oldImage = skinImgThumb.getImage();
527
 
                                Utils.disposeSWTObjects(new Object[] {
528
 
                                        oldImage
529
 
                                });
530
 
                                skinImgThumb.setImage(image);
531
 
                        }
532
 
                });
533
 
        }
534
 
}