|
Lines 1-5
Link Here
|
| 1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2000, 2008 IBM Corporation and others. |
2 |
* Copyright (c) 2000, 2010 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
|
Lines 10-22
Link Here
|
| 10 |
*******************************************************************************/ |
10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.team.internal.ui.wizards; |
11 |
package org.eclipse.team.internal.ui.wizards; |
| 12 |
|
12 |
|
| 13 |
import java.io.File; |
13 |
import java.io.*; |
|
|
14 |
import java.lang.reflect.InvocationTargetException; |
| 15 |
import java.lang.reflect.Method; |
| 16 |
import java.net.*; |
| 14 |
|
17 |
|
| 15 |
import org.eclipse.core.resources.ResourcesPlugin; |
18 |
import org.eclipse.core.resources.ResourcesPlugin; |
|
|
19 |
import org.eclipse.core.runtime.*; |
| 16 |
import org.eclipse.jface.dialogs.Dialog; |
20 |
import org.eclipse.jface.dialogs.Dialog; |
| 17 |
import org.eclipse.jface.dialogs.IDialogConstants; |
21 |
import org.eclipse.jface.dialogs.IDialogConstants; |
|
|
22 |
import org.eclipse.jface.operation.IRunnableWithProgress; |
| 18 |
import org.eclipse.jface.resource.ImageDescriptor; |
23 |
import org.eclipse.jface.resource.ImageDescriptor; |
| 19 |
import org.eclipse.swt.SWT; |
24 |
import org.eclipse.swt.SWT; |
|
|
25 |
import org.eclipse.swt.dnd.Clipboard; |
| 26 |
import org.eclipse.swt.dnd.TextTransfer; |
| 20 |
import org.eclipse.swt.events.SelectionAdapter; |
27 |
import org.eclipse.swt.events.SelectionAdapter; |
| 21 |
import org.eclipse.swt.events.SelectionEvent; |
28 |
import org.eclipse.swt.events.SelectionEvent; |
| 22 |
import org.eclipse.swt.layout.GridData; |
29 |
import org.eclipse.swt.layout.GridData; |
|
Lines 32-47
Link Here
|
| 32 |
String file = ""; //$NON-NLS-1$ |
39 |
String file = ""; //$NON-NLS-1$ |
| 33 |
Button browseButton; |
40 |
Button browseButton; |
| 34 |
|
41 |
|
|
|
42 |
String urlString = ""; //$NON-NLS-1$ |
| 43 |
Combo urlCombo; |
| 44 |
|
| 45 |
//input type radios |
| 46 |
private Button fileInputButton; |
| 47 |
private Button urlInputButton; |
| 48 |
|
| 49 |
//input type |
| 50 |
public static final int InputType_file = 0; |
| 51 |
public static final int InputType_URL = 1; |
| 52 |
private int inputType = InputType_file; |
| 53 |
|
| 35 |
private boolean runInBackground = isRunInBackgroundPreferenceOn(); |
54 |
private boolean runInBackground = isRunInBackgroundPreferenceOn(); |
| 36 |
// a wizard shouldn't be in an error state until the state has been modified by the user |
55 |
// a wizard shouldn't be in an error state until the state has been modified by the user |
| 37 |
private int messageType = NONE; |
56 |
private int messageType = NONE; |
| 38 |
private WorkingSetGroup workingSetGroup; |
57 |
private WorkingSetGroup workingSetGroup; |
| 39 |
|
58 |
|
|
|
59 |
private PsfFilenameStore psfFilenameStore = PsfFilenameStore.getInstance(); |
| 60 |
private PsfUrlStore psfUrlStore = PsfUrlStore.getInstance(); |
| 61 |
|
| 40 |
public ImportProjectSetMainPage(String pageName, String title, ImageDescriptor titleImage) { |
62 |
public ImportProjectSetMainPage(String pageName, String title, ImageDescriptor titleImage) { |
| 41 |
super(pageName, title, titleImage); |
63 |
super(pageName, title, titleImage); |
| 42 |
setDescription(TeamUIMessages.ImportProjectSetMainPage_description); |
64 |
setDescription(TeamUIMessages.ImportProjectSetMainPage_description); |
| 43 |
} |
65 |
} |
| 44 |
|
66 |
|
|
|
67 |
private void setInputType(int inputTypeSelected){ |
| 68 |
this.inputType = inputTypeSelected; |
| 69 |
//reset the message type and give the user fresh chance to input correct data |
| 70 |
messageType = NONE; |
| 71 |
//update controls |
| 72 |
fileInputButton.setSelection(inputType==InputType_file); |
| 73 |
fileCombo.setEnabled(inputType==InputType_file); |
| 74 |
browseButton.setEnabled(inputType==InputType_file); |
| 75 |
urlInputButton.setSelection(inputType==InputType_URL); |
| 76 |
urlCombo.setEnabled(inputType==InputType_URL); |
| 77 |
//validate field |
| 78 |
if(inputType==InputType_file) updateFileEnablement(); |
| 79 |
if(inputType==InputType_URL) updateUrlEnablement(); |
| 80 |
|
| 81 |
} |
| 82 |
|
| 45 |
/* |
83 |
/* |
| 46 |
* @see IDialogPage#createControl(Composite) |
84 |
* @see IDialogPage#createControl(Composite) |
| 47 |
*/ |
85 |
*/ |
|
Lines 60-80
Link Here
|
| 60 |
layout.marginWidth = 0; |
98 |
layout.marginWidth = 0; |
| 61 |
inner.setLayout(layout); |
99 |
inner.setLayout(layout); |
| 62 |
|
100 |
|
| 63 |
createLabel(inner, TeamUIMessages.ImportProjectSetMainPage_Project_Set_File_Name__2); |
101 |
fileInputButton = new Button(inner, SWT.RADIO); |
|
|
102 |
fileInputButton.setText(TeamUIMessages.ImportProjectSetMainPage_Project_Set_File); |
| 103 |
fileInputButton.setEnabled(true); |
| 104 |
fileInputButton.addSelectionListener(new SelectionAdapter() { |
| 105 |
public void widgetSelected(SelectionEvent e) { |
| 106 |
setInputType(InputType_file); |
| 107 |
} |
| 108 |
}); |
| 64 |
|
109 |
|
| 65 |
fileCombo = createDropDownCombo(inner); |
110 |
fileCombo = createDropDownCombo(inner); |
| 66 |
file = PsfFilenameStore.getSuggestedDefault(); |
111 |
file = psfFilenameStore.getSuggestedDefault(); |
| 67 |
fileCombo.setItems(PsfFilenameStore.getHistory()); |
112 |
fileCombo.setItems(psfFilenameStore.getHistory()); |
| 68 |
fileCombo.setText(file); |
113 |
fileCombo.setText(file); |
| 69 |
fileCombo.addListener(SWT.Modify, new Listener() { |
114 |
fileCombo.addListener(SWT.Modify, new Listener() { |
| 70 |
public void handleEvent(Event event) { |
115 |
public void handleEvent(Event event) { |
| 71 |
file = fileCombo.getText(); |
116 |
file = fileCombo.getText(); |
| 72 |
updateEnablement(); |
117 |
updateFileEnablement(); |
| 73 |
} |
118 |
} |
| 74 |
}); |
119 |
}); |
| 75 |
|
120 |
|
| 76 |
browseButton = new Button(inner, SWT.PUSH); |
121 |
browseButton = new Button(inner, SWT.PUSH); |
| 77 |
browseButton.setText(TeamUIMessages.ImportProjectSetMainPage_Browse_3); |
122 |
browseButton.setText(TeamUIMessages.ImportProjectSetMainPage_Browse_3); |
|
|
123 |
|
| 124 |
urlInputButton = new Button(inner, SWT.RADIO); |
| 125 |
urlInputButton.setText(TeamUIMessages.ImportProjectSetMainPage_Project_Set_Url); |
| 126 |
urlInputButton.addSelectionListener(new SelectionAdapter() { |
| 127 |
public void widgetSelected(SelectionEvent e) { |
| 128 |
setInputType(InputType_URL); |
| 129 |
} |
| 130 |
}); |
| 131 |
urlCombo = createDropDownCombo(inner); |
| 132 |
urlString = psfUrlStore.getSuggestedDefault(); |
| 133 |
urlCombo.setItems(psfUrlStore.getHistory()); |
| 134 |
urlCombo.setText(urlString); |
| 135 |
GridData gd = new GridData(GridData.FILL_HORIZONTAL); |
| 136 |
gd.horizontalSpan=2; |
| 137 |
urlCombo.setLayoutData(gd); |
| 138 |
urlCombo.addListener(SWT.Modify, new Listener() { |
| 139 |
public void handleEvent(Event event) { |
| 140 |
urlString=urlCombo.getText(); |
| 141 |
updateUrlEnablement(); |
| 142 |
} |
| 143 |
}); |
| 144 |
|
| 145 |
|
| 78 |
GridData data = new GridData(); |
146 |
GridData data = new GridData(); |
| 79 |
data.horizontalAlignment = GridData.FILL; |
147 |
data.horizontalAlignment = GridData.FILL; |
| 80 |
int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); |
148 |
int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); |
|
Lines 115-126
Link Here
|
| 115 |
}); |
183 |
}); |
| 116 |
|
184 |
|
| 117 |
setControl(composite); |
185 |
setControl(composite); |
| 118 |
updateEnablement(); |
186 |
setDefaultInputType(); |
| 119 |
Dialog.applyDialogFont(parent); |
187 |
Dialog.applyDialogFont(parent); |
| 120 |
// future messages will be of type error |
|
|
| 121 |
messageType = ERROR; |
| 122 |
} |
188 |
} |
| 123 |
|
189 |
|
|
|
190 |
private void setDefaultInputType(){ |
| 191 |
//check for clipboard contents |
| 192 |
Control c = getControl(); |
| 193 |
if (c != null) { |
| 194 |
Clipboard clipboard= new Clipboard(c.getDisplay()); |
| 195 |
Object o= clipboard.getContents(TextTransfer.getInstance()); |
| 196 |
clipboard.dispose(); |
| 197 |
if (o instanceof String) { |
| 198 |
try { |
| 199 |
URL url = new URL((String)o); |
| 200 |
if(url != null) { |
| 201 |
setInputType(InputType_URL); |
| 202 |
urlCombo.setText((String)o); |
| 203 |
return; |
| 204 |
} |
| 205 |
} catch (MalformedURLException e) { |
| 206 |
// ignore, it's not and URL |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
setInputType(InputType_file); |
| 211 |
} |
| 212 |
|
| 124 |
private void addWorkingSetSection(Composite composite) { |
213 |
private void addWorkingSetSection(Composite composite) { |
| 125 |
workingSetGroup = new WorkingSetGroup( |
214 |
workingSetGroup = new WorkingSetGroup( |
| 126 |
composite, |
215 |
composite, |
|
Lines 129-137
Link Here
|
| 129 |
"org.eclipse.jdt.ui.JavaWorkingSetPage" /* JavaWorkingSetUpdater.ID */}); //$NON-NLS-1$ |
218 |
"org.eclipse.jdt.ui.JavaWorkingSetPage" /* JavaWorkingSetUpdater.ID */}); //$NON-NLS-1$ |
| 130 |
} |
219 |
} |
| 131 |
|
220 |
|
| 132 |
private void updateEnablement() { |
221 |
private void updateUrlEnablement() { |
|
|
222 |
boolean complete = false; |
| 223 |
setMessage(null); |
| 224 |
setErrorMessage(null); |
| 225 |
|
| 226 |
if (urlString.length() == 0) { |
| 227 |
setMessage(TeamUIMessages.ImportProjectSetMainPage_specifyURL, messageType); |
| 228 |
complete = false; |
| 229 |
} else { |
| 230 |
|
| 231 |
try { |
| 232 |
new URL(urlString); |
| 233 |
// the URL is correct, we can clear the error message |
| 234 |
complete = true; |
| 235 |
} catch (MalformedURLException e) { |
| 236 |
messageType = ERROR; |
| 237 |
setMessage(TeamUIMessages.ImportProjectSetDialog_malformed_url, messageType); |
| 238 |
complete = false; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
if (complete) { |
| 243 |
setErrorMessage(null); |
| 244 |
setDescription(TeamUIMessages.ImportProjectSetMainPage_description); |
| 245 |
} |
| 246 |
|
| 247 |
setPageComplete(complete); |
| 248 |
} |
| 249 |
|
| 250 |
private void updateFileEnablement() { |
| 133 |
boolean complete = false; |
251 |
boolean complete = false; |
| 134 |
setMessage(null); |
252 |
setMessage(null); |
|
|
253 |
setErrorMessage(null); |
| 135 |
|
254 |
|
| 136 |
if (file.length() == 0) { |
255 |
if (file.length() == 0) { |
| 137 |
setMessage(TeamUIMessages.ImportProjectSetMainPage_specifyFile, messageType); |
256 |
setMessage(TeamUIMessages.ImportProjectSetMainPage_specifyFile, messageType); |
|
Lines 141-154
Link Here
|
| 141 |
// See if the file exists |
260 |
// See if the file exists |
| 142 |
File f = new File(file); |
261 |
File f = new File(file); |
| 143 |
if (!f.exists()) { |
262 |
if (!f.exists()) { |
|
|
263 |
messageType = ERROR; |
| 144 |
setMessage(TeamUIMessages.ImportProjectSetMainPage_The_specified_file_does_not_exist_4, messageType); |
264 |
setMessage(TeamUIMessages.ImportProjectSetMainPage_The_specified_file_does_not_exist_4, messageType); |
| 145 |
setPageComplete(false); |
265 |
setPageComplete(false); |
| 146 |
return; |
266 |
return; |
| 147 |
} else if (f.isDirectory()) { |
267 |
} else if (f.isDirectory()) { |
|
|
268 |
messageType = ERROR; |
| 148 |
setMessage(TeamUIMessages.ImportProjectSetMainPage_You_have_specified_a_folder_5, messageType); |
269 |
setMessage(TeamUIMessages.ImportProjectSetMainPage_You_have_specified_a_folder_5, messageType); |
| 149 |
setPageComplete(false); |
270 |
setPageComplete(false); |
| 150 |
return; |
271 |
return; |
| 151 |
} else if (!ProjectSetImporter.isValidProjectSetFile(file)) { |
272 |
} else if (!ProjectSetImporter.isValidProjectSetFile(file)) { |
|
|
273 |
messageType = ERROR; |
| 152 |
setMessage(TeamUIMessages.ImportProjectSetMainPage_projectSetFileInvalid, messageType); |
274 |
setMessage(TeamUIMessages.ImportProjectSetMainPage_projectSetFileInvalid, messageType); |
| 153 |
setPageComplete(false); |
275 |
setPageComplete(false); |
| 154 |
return; |
276 |
return; |
|
Lines 167-172
Link Here
|
| 167 |
public String getFileName() { |
289 |
public String getFileName() { |
| 168 |
return file; |
290 |
return file; |
| 169 |
} |
291 |
} |
|
|
292 |
|
| 293 |
public String getUrl(){ |
| 294 |
return urlString; |
| 295 |
} |
| 170 |
|
296 |
|
| 171 |
public void setVisible(boolean visible) { |
297 |
public void setVisible(boolean visible) { |
| 172 |
super.setVisible(visible); |
298 |
super.setVisible(visible); |
|
Lines 193-196
Link Here
|
| 193 |
public boolean isRunInBackgroundOn() { |
319 |
public boolean isRunInBackgroundOn() { |
| 194 |
return runInBackground; |
320 |
return runInBackground; |
| 195 |
} |
321 |
} |
|
|
322 |
|
| 323 |
public int getInputType(){ |
| 324 |
return inputType; |
| 325 |
} |
| 326 |
|
| 327 |
public String getURLContents() { |
| 328 |
final URL url; |
| 329 |
try { |
| 330 |
url = new URL(urlString); |
| 331 |
PsfUrlStore.getInstance().remember(urlString); |
| 332 |
final String[] result = new String[1]; |
| 333 |
try { |
| 334 |
getContainer().run(true, true, new IRunnableWithProgress() { |
| 335 |
public void run(IProgressMonitor monitor) |
| 336 |
throws InvocationTargetException, |
| 337 |
InterruptedException { |
| 338 |
SubMonitor progress = SubMonitor |
| 339 |
.convert( |
| 340 |
monitor, |
| 341 |
TeamUIMessages.ImportProjectSetDialog_URLConnecting, |
| 342 |
100); |
| 343 |
try { |
| 344 |
URLConnection connection = url.openConnection(); |
| 345 |
progress.worked(10); |
| 346 |
if (monitor.isCanceled()) |
| 347 |
throw new OperationCanceledException(); |
| 348 |
setReadTimeout(connection, (60 * 1000)); |
| 349 |
progress.setTaskName(TeamUIMessages.ImportProjectSetDialog_URLFetchingContent); |
| 350 |
String enc = connection.getContentEncoding(); |
| 351 |
if (enc == null) |
| 352 |
enc = ResourcesPlugin.getEncoding(); |
| 353 |
result[0] = readPsfFromURL( |
| 354 |
connection.getInputStream(), enc, |
| 355 |
connection.getContentLength(), |
| 356 |
progress.newChild(90)); |
| 357 |
} catch (SocketTimeoutException e) { // timeout |
| 358 |
throw new InvocationTargetException(e); |
| 359 |
} catch (IOException e) { //cannot load file |
| 360 |
throw new InvocationTargetException(e); |
| 361 |
}finally{ |
| 362 |
monitor.done(); |
| 363 |
} |
| 364 |
} |
| 365 |
}); |
| 366 |
|
| 367 |
if(ProjectSetImporter.isValidProjectSetString(result[0])){ |
| 368 |
return result[0]; |
| 369 |
}else{ |
| 370 |
messageType = ERROR; |
| 371 |
setMessage(TeamUIMessages.ImportProjectSetMainPage_projectSetFileInvalid, messageType); |
| 372 |
setPageComplete(false); |
| 373 |
return null; |
| 374 |
} |
| 375 |
|
| 376 |
} catch (OperationCanceledException e) { // ignore |
| 377 |
} catch (InvocationTargetException e) { // ignore |
| 378 |
messageType = ERROR; |
| 379 |
setMessage(TeamUIMessages.ImportProjectSetMainPage_The_given_URL_cannot_be_loaded, messageType); |
| 380 |
setPageComplete(false); |
| 381 |
} catch (InterruptedException e) { // ignore |
| 382 |
} |
| 383 |
} catch (MalformedURLException e) { |
| 384 |
// ignore as we tested it with modify listener on combo |
| 385 |
} |
| 386 |
return null; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
private boolean setReadTimeout(URLConnection connection, int timeout) { |
| 391 |
//check if setReadTimeout method is available for current java |
| 392 |
//and if it is call it |
| 393 |
Method[] methods = connection.getClass().getMethods(); |
| 394 |
for (int i = 0; i < methods.length; i++) { |
| 395 |
if (methods[i].getName().equals("setReadTimeout")) //$NON-NLS-1$ |
| 396 |
try { |
| 397 |
methods[i].invoke(connection, new Object[] {new Integer(timeout)}); |
| 398 |
return true; |
| 399 |
} catch (IllegalArgumentException e) { // ignore |
| 400 |
} catch (IllegalAccessException e) { // ignore |
| 401 |
} catch (InvocationTargetException e) { // ignore |
| 402 |
} |
| 403 |
} |
| 404 |
return false; |
| 405 |
} |
| 406 |
|
| 407 |
private String readPsfFromURL(InputStream is, String encoding, int length, |
| 408 |
IProgressMonitor monitor) throws IOException { |
| 409 |
SubMonitor progress = SubMonitor.convert(monitor); |
| 410 |
progress.setWorkRemaining(length); |
| 411 |
if (is == null) |
| 412 |
return null; |
| 413 |
BufferedReader reader = null; |
| 414 |
try { |
| 415 |
StringBuffer buffer = new StringBuffer(); |
| 416 |
char[] part = new char[2048]; |
| 417 |
int read = 0; |
| 418 |
reader = new BufferedReader(new InputStreamReader(is, encoding)); |
| 419 |
while ((read = reader.read(part)) != -1) { |
| 420 |
buffer.append(part, 0, read); |
| 421 |
progress.worked(2048); |
| 422 |
if (progress.isCanceled()) |
| 423 |
throw new OperationCanceledException(); |
| 424 |
} |
| 425 |
|
| 426 |
return buffer.toString(); |
| 427 |
} finally { |
| 428 |
if (reader != null) { |
| 429 |
try { |
| 430 |
reader.close(); |
| 431 |
} catch (IOException ex) { |
| 432 |
// silently ignored |
| 433 |
} |
| 434 |
} |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 196 |
} |
438 |
} |