blob: 60f9dcd861df624ed3abe68ad4251d0021bf58ec (
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
|
/*******************************************************************************
* Copyright (c) 2004, 2007 Mylyn project committers 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
*******************************************************************************/
package org.eclipse.mylyn.internal.tasks.ui;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.mylyn.monitor.core.StatusHandler;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.TitleEvent;
import org.eclipse.swt.browser.TitleListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
/**
* Waits for the title from the browser
*
* @author Wesley Coelho
* @author Mik Kersten
*/
public abstract class RetrieveTitleFromUrlJob extends Job implements TitleListener {
public static final String LABEL_TITLE = "Retrieving summary from URL";
private final static long MAX_WAIT_TIME_MILLIS = 1000 * 10; // (10 Seconds)
private final static long SLEEP_INTERVAL_MILLIS = 500;
private String url = null;
private String pageTitle = null;
private boolean retrievalFailed = false;
private long timeWaitedMillis = 0;
boolean ignoreChangeCall = false;
private boolean titleRetrieved = false;
public RetrieveTitleFromUrlJob(String url) {
super(LABEL_TITLE);
this.url = url;
}
protected abstract void setTitle(String pageTitle);
@Override
public IStatus run(IProgressMonitor monitor) {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
final Shell shell = new Shell(Display.getDefault());
shell.setVisible(false);
Browser browser = new Browser(shell, SWT.NONE);
browser.addTitleListener(RetrieveTitleFromUrlJob.this);
browser.setUrl(url);
}
});
while (pageTitle == null && !retrievalFailed && (timeWaitedMillis <= MAX_WAIT_TIME_MILLIS)) {
try {
Thread.sleep(SLEEP_INTERVAL_MILLIS);
} catch (InterruptedException e) {
StatusHandler.fail(e, "Thread interrupted during sleep", false);
}
timeWaitedMillis += SLEEP_INTERVAL_MILLIS;
}
Display.getDefault().asyncExec(new Runnable() {
public void run() {
if (pageTitle == null) {
pageTitle = url;
titleRetrieved = false;
} else {
titleRetrieved = true;
}
setTitle(pageTitle);
}
});
return Status.OK_STATUS;
}
public void changed(TitleEvent event) {
if (!ignoreChangeCall) {
if (event.title.equals(url)) {
return;
} else {
ignoreChangeCall = true;
if (event.title.equals(url + "/") || event.title.equals("Object not found!")
|| event.title.equals("No page to display") || event.title.equals("Cannot find server")
|| event.title.equals("Invalid Bug ID")) {
retrievalFailed = true;
} else {
pageTitle = event.title;
}
}
}
}
public boolean isTitleRetrieved() {
return titleRetrieved;
}
public String getPageTitle() {
return pageTitle;
}
}
|