|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2004 - 2006 University Of British Columbia and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* University Of British Columbia - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
|
| 12 |
package org.eclipse.mylar.internal.sandbox.web; |
| 13 |
|
| 14 |
import java.util.regex.Matcher; |
| 15 |
import java.util.regex.Pattern; |
| 16 |
|
| 17 |
import org.eclipse.jface.wizard.WizardPage; |
| 18 |
import org.eclipse.mylar.provisional.tasklist.AbstractRepositoryQuery; |
| 19 |
import org.eclipse.mylar.provisional.tasklist.MylarTaskListPlugin; |
| 20 |
import org.eclipse.mylar.provisional.tasklist.TaskRepository; |
| 21 |
import org.eclipse.swt.SWT; |
| 22 |
import org.eclipse.swt.custom.SashForm; |
| 23 |
import org.eclipse.swt.events.ModifyEvent; |
| 24 |
import org.eclipse.swt.events.ModifyListener; |
| 25 |
import org.eclipse.swt.events.SelectionAdapter; |
| 26 |
import org.eclipse.swt.events.SelectionEvent; |
| 27 |
import org.eclipse.swt.layout.GridData; |
| 28 |
import org.eclipse.swt.layout.GridLayout; |
| 29 |
import org.eclipse.swt.widgets.Button; |
| 30 |
import org.eclipse.swt.widgets.Composite; |
| 31 |
import org.eclipse.swt.widgets.Label; |
| 32 |
import org.eclipse.swt.widgets.Table; |
| 33 |
import org.eclipse.swt.widgets.TableColumn; |
| 34 |
import org.eclipse.swt.widgets.TableItem; |
| 35 |
import org.eclipse.swt.widgets.Text; |
| 36 |
|
| 37 |
/** |
| 38 |
* |
| 39 |
* Subclipse (IssueZilla) |
| 40 |
* url: http://subclipse.tigris.org/issues/buglist.cgi?issue_status=NEW;issue_status=STARTED;issue_status=REOPENED&order=issues.issue_id |
| 41 |
* regexp: <a href="show_bug.cgi\?id\=(.+?)">.+?<span class="summary">(.+?)</span> |
| 42 |
* task prefix: http://subclipse.tigris.org/issues/show_bug.cgi?id= |
| 43 |
* |
| 44 |
* ASM (GForge) |
| 45 |
* url: http://forge.objectweb.org/tracker/?group_id=23&atid=350023 |
| 46 |
* regexp: <a class="tracker" href="/tracker/index.php\?func=detail&aid=(.+?)&group_id=23&atid=350023">(.+?)</a></td> |
| 47 |
* task prefix: http://forge.objectweb.org/tracker/index.php?func=detail&group_id=23&atid=350023&aid= |
| 48 |
* |
| 49 |
* @author Eugene Kuleshov |
| 50 |
*/ |
| 51 |
public class WebQueryWizardPage extends WizardPage { |
| 52 |
private Text taskPrefixText; |
| 53 |
private Text descriptionText; |
| 54 |
private Text queryUrlText; |
| 55 |
private Text regexpText; |
| 56 |
private Table previewTable; |
| 57 |
|
| 58 |
private StringBuffer webPage; |
| 59 |
|
| 60 |
private TaskRepository repository; |
| 61 |
private WebQuery query; |
| 62 |
|
| 63 |
public WebQueryWizardPage(TaskRepository repository) { |
| 64 |
this(repository, null); |
| 65 |
} |
| 66 |
|
| 67 |
public WebQueryWizardPage(TaskRepository repository, WebQuery query) { |
| 68 |
super("New web query"); |
| 69 |
this.repository = repository; |
| 70 |
this.query = query; |
| 71 |
|
| 72 |
setTitle("Create web query"); |
| 73 |
setDescription("http://subclipse.tigris.org/issues/buglist.cgi?issue_status=NEW;issue_status=STARTED;issue_status=REOPENED&order=issues.issue_id\n" + |
| 74 |
"<a href=\"show_bug.cgi\\?id\\=(.+?)\">.+?<span class=\"summary\">(.+?)</span>"); |
| 75 |
} |
| 76 |
|
| 77 |
public void createControl(Composite parent) { |
| 78 |
SashForm sashForm = new SashForm(parent, SWT.VERTICAL); |
| 79 |
|
| 80 |
Composite composite = new Composite(sashForm, SWT.NONE); |
| 81 |
GridLayout gridLayout = new GridLayout(); |
| 82 |
gridLayout.numColumns = 3; |
| 83 |
composite.setLayout(gridLayout); |
| 84 |
|
| 85 |
Label descriptionLabel = new Label(composite, SWT.NONE); |
| 86 |
descriptionLabel.setLayoutData(new GridData()); |
| 87 |
descriptionLabel.setText("Description:"); |
| 88 |
|
| 89 |
descriptionText = new Text(composite, SWT.BORDER); |
| 90 |
descriptionText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); |
| 91 |
new Label(composite, SWT.NONE); |
| 92 |
|
| 93 |
Label queryUrlLabel = new Label(composite, SWT.NONE); |
| 94 |
queryUrlLabel.setText("URL:"); |
| 95 |
|
| 96 |
queryUrlText = new Text(composite, SWT.BORDER); |
| 97 |
queryUrlText.addModifyListener(new ModifyListener() { |
| 98 |
public void modifyText(final ModifyEvent e) { |
| 99 |
webPage = null; |
| 100 |
} |
| 101 |
}); |
| 102 |
queryUrlText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); |
| 103 |
|
| 104 |
Button preview = new Button(composite, SWT.NONE); |
| 105 |
preview.setText("Preview"); |
| 106 |
preview.addSelectionListener(new SelectionAdapter() { |
| 107 |
public void widgetSelected(final SelectionEvent e) { |
| 108 |
updatePreview(); |
| 109 |
} |
| 110 |
}); |
| 111 |
|
| 112 |
Label taskPrefixLabel = new Label(composite, SWT.NONE); |
| 113 |
taskPrefixLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); |
| 114 |
taskPrefixLabel.setText("Task prefix:"); |
| 115 |
|
| 116 |
taskPrefixText = new Text(composite, SWT.BORDER); |
| 117 |
taskPrefixText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); |
| 118 |
new Label(composite, SWT.NONE); |
| 119 |
|
| 120 |
Label regexpLabel = new Label(composite, SWT.NONE); |
| 121 |
regexpLabel.setText("Regexp:"); |
| 122 |
regexpLabel.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, true)); |
| 123 |
|
| 124 |
regexpText = new Text(composite, SWT.V_SCROLL | SWT.MULTI | SWT.BORDER); |
| 125 |
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true); |
| 126 |
gridData.heightHint = 39; |
| 127 |
regexpText.setLayoutData(gridData); |
| 128 |
regexpText.addModifyListener(new ModifyListener() { |
| 129 |
public void modifyText(final ModifyEvent e) { |
| 130 |
if(webPage!=null) { |
| 131 |
updatePreview(); |
| 132 |
} |
| 133 |
} |
| 134 |
}); |
| 135 |
|
| 136 |
previewTable = new Table(sashForm, SWT.BORDER); |
| 137 |
previewTable.setLinesVisible(true); |
| 138 |
previewTable.setHeaderVisible(true); |
| 139 |
|
| 140 |
TableColumn colId = new TableColumn(previewTable, SWT.NONE); |
| 141 |
colId.setWidth(100); |
| 142 |
colId.setText("Id"); |
| 143 |
|
| 144 |
TableColumn colDescription = new TableColumn(previewTable, SWT.NONE); |
| 145 |
colDescription.setWidth(328); |
| 146 |
colDescription.setText("Description"); |
| 147 |
|
| 148 |
setControl(sashForm); |
| 149 |
|
| 150 |
if(query!=null) { |
| 151 |
descriptionText.setText(query.getDescription()); |
| 152 |
queryUrlText.setText(query.getQueryUrl()); |
| 153 |
taskPrefixText.setText(query.getTaskPrefix()); |
| 154 |
regexpText.setText(query.getRegexp()); |
| 155 |
} |
| 156 |
new Label(composite, SWT.NONE); |
| 157 |
sashForm.setWeights(new int[] {123, 166 }); |
| 158 |
} |
| 159 |
|
| 160 |
public AbstractRepositoryQuery getQuery() { |
| 161 |
String description = descriptionText.getText(); |
| 162 |
String queryUrl = queryUrlText.getText(); |
| 163 |
String taskPrefix = taskPrefixText.getText(); |
| 164 |
String regexp = regexpText.getText(); |
| 165 |
return new WebQuery(description, queryUrl, taskPrefix, regexp, |
| 166 |
MylarTaskListPlugin.getTaskListManager().getTaskList(), repository.getUrl()); |
| 167 |
} |
| 168 |
|
| 169 |
// TODO run asynchronously |
| 170 |
synchronized void updatePreview() { |
| 171 |
String regexp = regexpText.getText(); |
| 172 |
try { |
| 173 |
Pattern p = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL | Pattern.UNICODE_CASE | Pattern.CANON_EQ); |
| 174 |
Matcher matcher = p.matcher(getWebPage()); |
| 175 |
|
| 176 |
previewTable.removeAll(); |
| 177 |
|
| 178 |
while(matcher.find()) { |
| 179 |
if(matcher.groupCount()>0) { |
| 180 |
TableItem item = new TableItem(previewTable, SWT.NONE); |
| 181 |
for (int i = 0; i < matcher.groupCount(); i++) { |
| 182 |
item.setText(i, matcher.group(i+1)); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
if(matcher.groupCount()<2) { |
| 187 |
setErrorMessage("Require two matching groups (id and description)"); |
| 188 |
setPageComplete(false); |
| 189 |
} else { |
| 190 |
setErrorMessage(null); |
| 191 |
setPageComplete(true); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
} catch(Exception ex) { |
| 196 |
setErrorMessage("Parsing error: "+ex.getMessage()); |
| 197 |
setPageComplete(false); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
private StringBuffer getWebPage() { |
| 202 |
if(webPage==null) { |
| 203 |
try { |
| 204 |
webPage = WebRepositoryConnector.fetchResource(queryUrlText.getText()); |
| 205 |
} catch(Exception ex) { |
| 206 |
setErrorMessage("Unable to fetch resource: "+ex.getMessage()); |
| 207 |
setPageComplete(false); |
| 208 |
} |
| 209 |
} |
| 210 |
return webPage; |
| 211 |
} |
| 212 |
|
| 213 |
} |