2
* Copyright (C) 2006 Aelitis, All Rights Reserved.
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.
16
* AELITIS, SAS au capital de 63.529,40 euros
17
* 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
21
package com.aelitis.azureus.ui.swt.views.skin;
23
import java.io.ByteArrayInputStream;
24
import java.util.regex.Pattern;
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.*;
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;
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;
62
* @created Oct 12, 2006
65
public class MediaList
68
private static final int ASYOUTYPE_UPDATEDELAY = 150;
70
private SWTSkinObjectText lblCountAreaNotOurs;
72
private SWTSkinObjectText lblCountAreaOurs;
74
private TorrentListView view;
76
private String PREFIX = "my-media-";
78
private AzureusCore core;
80
private SWTSkinObjectImage skinImgThumb;
82
private SWTSkinObjectText skinDetailInfo;
84
private MetaDataUpdateListener listener;
86
protected String sLastSearch = "";
88
private Text txtFilter;
90
private boolean bRegexSearch;
94
private TimerEvent searchUpdateEvent;
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);
100
core = AzureusCoreFactory.getSingleton();
101
view = new TorrentListView(this, PREFIX, TorrentListView.VIEW_MY_MEDIA,
103
public boolean isOurDownload(DownloadManager dm) {
104
if (PlatformTorrentUtils.getAdId(dm.getTorrent()) != null) {
107
if (sLastSearch.length() == 0) {
111
boolean bOurs = true;
120
dm.getTorrent().getAnnounceURL().getHost()
128
String name = names[0][1];
129
String tmpSearch = sLastSearch;
131
for (int i = 0; i < names.length; i++) {
132
if (tmpSearch.startsWith(names[i][0])) {
133
tmpSearch = tmpSearch.substring(names[i][0].length());
138
String s = bRegexSearch ? tmpSearch : "\\Q"
139
+ tmpSearch.replaceAll("[|;]", "\\\\E|\\\\Q") + "\\E";
140
Pattern pattern = Pattern.compile(s, Pattern.CASE_INSENSITIVE);
142
if (!pattern.matcher(name).find()) {
145
} catch (Exception e) {
146
// Future: report PatternSyntaxException message to user.
152
if (view instanceof ListView) {
153
((ListView) view).addCountChangeListener(new TableCountChangeListener() {
155
public void rowRemoved(TableRowCore row) {
158
public void rowAdded(TableRowCore row) {
159
Object dataSource = row.getDataSource(true);
160
if (dataSource instanceof DownloadManager) {
161
updateRowFGColor((DownloadManager) dataSource);
167
view.addListener(new TorrentListViewListener() {
168
boolean countChanging = false;
170
// @see com.aelitis.azureus.ui.swt.views.TorrentListViewListener#stateChanged(org.gudy.azureus2.core3.download.DownloadManager)
172
public void stateChanged(final DownloadManager manager) {
173
updateRowFGColor(manager);
176
// @see com.aelitis.azureus.ui.swt.views.TorrentListViewListener#countChanged()
177
public void countChanged() {
182
countChanging = true;
183
Utils.execSWTThreadLater(0, new AERunnable() {
184
public void runSupport() {
185
countChanging = false;
188
long totalNotOurs = 0;
190
GlobalManager globalManager = core.getGlobalManager();
191
Object[] dms = globalManager.getDownloadManagers().toArray();
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)) {
204
if (lblCountAreaOurs != null) {
205
lblCountAreaOurs.setText(MessageText.getString("v3.MainWindow."
206
+ PREFIX + "ours.count", new String[] {
210
if (lblCountAreaNotOurs != null) {
211
lblCountAreaNotOurs.setText(MessageText.getString(
212
"v3.MainWindow." + PREFIX + "notours.count", new String[] {
215
lblCountAreaNotOurs.getControl().getParent().layout(true, true);
218
int count = view.getSelectedRowsSize();
219
if (count == 0 || count > 1) {
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)) {
240
PlatformTorrentUtils.addListener(listener);
242
skinImgThumb = (SWTSkinObjectImage) skinObject;
243
view.addSelectionListener(new TableSelectionAdapter() {
244
public void deselected(TableRowCore[] rows) {
248
public void selected(TableRowCore[] rows) {
252
public void focusChanged(TableRowCore focusedRow) {
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) {
267
public void selected(TableRowCore[] rows) {
271
public void focusChanged(TableRowCore focusedRow) {
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();
289
FormData formData = Utils.getFilledFormData();
291
txtFilter.setLayoutData(formData);
294
int h = txtFilter.getClientArea().height - (Constants.isOSX ? 0 : 2);
295
Font font = Utils.getFontWithHeight(txtFilter.getFont(), null, h);
297
composite.getParent().layout();
300
final Font fFont = font;
301
txtFilter.setFont(fFont);
303
txtFilter.addDisposeListener(new DisposeListener() {
304
public void widgetDisposed(DisposeEvent e) {
305
if (fFont != null && !fFont.isDisposed()) {
306
txtFilter.setFont(null);
313
txtFilter.addKeyListener(new KeyListener() {
314
public void keyReleased(KeyEvent e) {
317
public void keyPressed(KeyEvent e) {
318
int key = e.character;
319
if (key <= 26 && key > 0)
321
if (e.stateMask == SWT.CONTROL) {
323
bRegexSearch = !bRegexSearch;
331
view.addKeyListener(new KeyListener() {
332
public void keyReleased(KeyEvent e) {
335
public void keyPressed(KeyEvent e) {
336
int key = e.character;
337
if (key <= 26 && key > 0)
339
if (e.stateMask == SWT.CONTROL) {
341
bRegexSearch = !bRegexSearch;
348
if (e.keyCode != SWT.BS) {
349
if ((e.stateMask & (~SWT.SHIFT)) != 0 || e.character < 32
350
|| e.character == 127) {
355
if (e.keyCode == SWT.BS) {
356
if (e.stateMask == SWT.CONTROL) {
358
} else if (sLastSearch.length() > 0) {
359
sLastSearch = sLastSearch.substring(0, sLastSearch.length() - 1);
362
sLastSearch += String.valueOf(e.character);
365
if (txtFilter != null && !txtFilter.isDisposed()) {
366
txtFilter.setFocus();
385
protected void updateRowFGColor(DownloadManager manager) {
386
if (manager == null) {
389
TableRowSWT row = view.getRowSWT(manager);
393
if (manager.isDownloadComplete(false)) {
394
row.setForeground((Color) null);
396
Color c = view.getSkinProperties().getColor("color.library.incomplete");
397
row.setForeground(c);
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());
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());
425
txtFilter.setBackground(null);
426
txtFilter.setForeground(null);
427
Messages.setLanguageTooltip(txtFilter, "MyTorrentsView.filter.tooltip");
430
if (lblX != null && !lblX.isDisposed()) {
431
Image img = ImageRepository.getImage(sLastSearch.length() > 0 ? "smallx"
437
if (searchUpdateEvent != null) {
438
searchUpdateEvent.cancel();
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;
448
searchUpdateEvent = null;
457
protected void doFilter() {
458
view.tableStructureChanged();
461
private void updateDetailsInfo() {
462
if (skinDetailInfo == null) {
465
int count = view.getSelectedRowsSize();
466
if (count == 0 || count > 1) {
468
ListRow[] rowsUnsorted = view.getRowsUnsorted();
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);
475
if (dm.isDownloadComplete(false)) {
482
skinDetailInfo.setText(MessageText.getString(
483
"v3.MainWindow.myMedia.noneSelected", new String[] {
489
TableRowCore[] rows = view.getSelectedRows();
491
DownloadManager dm = (DownloadManager) rows[0].getDataSource(true);
493
TOTorrent torrent = dm.getTorrent();
495
s = PlatformTorrentUtils.getContentTitle(torrent);
500
s = PlatformTorrentUtils.getContentDescription(torrent);
505
skinDetailInfo.setText(sText);
508
private void update() {
509
Utils.execSWTThread(new AERunnable() {
510
public void runSupport() {
511
int count = view.getSelectedRowsSize();
513
skinImgThumb.setImage(null);
516
TableRowCore[] rows = view.getSelectedRows();
518
DownloadManager dm = (DownloadManager) rows[0].getDataSource(true);
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);
526
Image oldImage = skinImgThumb.getImage();
527
Utils.disposeSWTObjects(new Object[] {
530
skinImgThumb.setImage(image);