This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
/
RemoteResourceLookupPage.java
398 lines (334 loc) · 13 KB
/
RemoteResourceLookupPage.java
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package org.maven.ide.eclipse.ui.common.wizards;
import java.lang.reflect.InvocationTargetException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.forms.events.ExpansionAdapter;
import org.eclipse.ui.forms.events.ExpansionEvent;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.maven.ide.eclipse.swtvalidation.SwtValidationGroup;
import org.maven.ide.eclipse.swtvalidation.SwtValidationUI;
import org.maven.ide.eclipse.ui.common.ErrorHandlingUtils;
import org.maven.ide.eclipse.ui.common.Messages;
import org.maven.ide.eclipse.ui.common.authentication.UrlInputComposite;
import org.netbeans.validation.api.Problem;
import org.netbeans.validation.api.Problems;
import org.netbeans.validation.api.Severity;
import org.netbeans.validation.api.Validator;
import org.netbeans.validation.api.ui.ValidationListener;
import org.netbeans.validation.api.ui.ValidationUI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
abstract public class RemoteResourceLookupPage
extends WizardPage
{
private Logger log = LoggerFactory.getLogger( RemoteResourceLookupPage.class );
private String serverUrl;
private String selectMessage;
private String readyToLoadMessage;
private String serverName;
private String resourceLabelText;
private String loadButtonText;
private Object input;
private ExpandableComposite expandableComposite;
private UrlInputComposite urlInputComposite;
private Composite resourceComposite;
private Button loadButton;
private SwtValidationGroup loadButtonGroup;
private SwtValidationGroup okButtonGroup;
private SwtValidationGroup rootGroup;
public RemoteResourceLookupPage( String serverUrl )
{
super( RemoteResourceLookupPage.class.getName() );
this.serverUrl = serverUrl;
setPageComplete( false );
loadButtonGroup = SwtValidationGroup.create( new LoadButtonValidationUI() );
rootGroup = SwtValidationGroup.create(SwtValidationUI.createUI(this, SwtValidationUI.MESSAGE));
rootGroup.addItem(loadButtonGroup, false);
okButtonGroup = SwtValidationGroup.create(SwtValidationUI.createUI(this, SwtValidationUI.BUTTON));
rootGroup.addItem(okButtonGroup, false);
}
public void setServerName( String name )
{
serverName = name;
}
public void setSelectMessage( String message )
{
selectMessage = message;
setDescription( selectMessage );
}
public void setReadyToLoadMessage( String message )
{
readyToLoadMessage = message;
}
public void setResourceLabelText( String text )
{
resourceLabelText = text;
}
public void setLoadButtonText( String text )
{
loadButtonText = text;
}
public void createControl( Composite parent )
{
Composite panel = new Composite( parent, SWT.NONE );
GridLayout gl = new GridLayout( 1, false );
gl.marginLeft = 10;
gl.marginRight = 10;
panel.setLayout( gl );
GridData gd = new GridData( SWT.FILL, SWT.FILL, true, true );
gd.heightHint = 400;
panel.setLayoutData( gd );
createExpandableComposite( panel );
resourceComposite = createResourcePanel( panel );
setControl( panel );
rootGroup.performValidation();
if ( serverUrl != null )
{
reload();
}
}
protected UrlInputComposite createUrlInputComposite( Composite parent )
{
return new UrlInputComposite( parent, null, getLoadButtonValidationGroup(), UrlInputComposite.ALLOW_ANONYMOUS );
}
private void createExpandableComposite( final Composite parent )
{
expandableComposite =
new ExpandableComposite( parent, ExpandableComposite.COMPACT | ExpandableComposite.TWISTIE );
expandableComposite.setLayoutData( new GridData( SWT.FILL, SWT.TOP, true, false ) );
urlInputComposite = createUrlInputComposite( expandableComposite );
urlInputComposite.setUrlLabelText( NLS.bind( Messages.remoteResourceLookupPage_server_label, serverName ) );
urlInputComposite.setUrl( serverUrl );
Composite reloadPanel = new Composite( parent, SWT.NONE );
GridLayout gridLayout = new GridLayout( 2, false );
gridLayout.marginWidth = 0;
reloadPanel.setLayout( gridLayout );
reloadPanel.setLayoutData( new GridData( SWT.FILL, SWT.BOTTOM, false, false ) );
Label label = new Label( reloadPanel, SWT.NONE );
label.setText( resourceLabelText );
label.setLayoutData( new GridData( SWT.LEFT, SWT.BOTTOM, true, false ) );
loadButton = new Button( reloadPanel, SWT.PUSH );
loadButton.setText( loadButtonText );
loadButton.setLayoutData( new GridData( SWT.RIGHT, SWT.BOTTOM, false, false ) );
loadButton.addSelectionListener( new SelectionAdapter()
{
@Override
public void widgetSelected( SelectionEvent e )
{
reload();
}
} );
rootGroup.addItem( new UrlValidationListener( urlInputComposite ), false );
String url = urlInputComposite.getUrlText();
if ( url.length() > 0 )
{
serverUrl = url;
}
expandableComposite.setExpanded( serverUrl == null );
expandableComposite.setClient( urlInputComposite );
expandableComposite.addExpansionListener( new ExpansionAdapter()
{
public void expansionStateChanged( ExpansionEvent e )
{
updateExpandableState();
}
} );
updateExpandableTitle();
}
private class UrlValidationListener
extends ValidationListener<UrlInputComposite>
implements ModifyListener
{
protected UrlValidationListener( UrlInputComposite urlInputComposite )
{
super( UrlInputComposite.class, ValidationUI.NO_OP, urlInputComposite );
urlInputComposite.addModifyListener( this );
}
@Override
protected void performValidation( Problems problems )
{
String url = urlInputComposite.getUrlText();
if ( url.length() != 0 )
{
if ( !url.equals( serverUrl ) )
{
setInput( input = null );
serverUrl = url;
problems.add( readyToLoadMessage, Severity.INFO );
}
}
}
public void modifyText( ModifyEvent e )
{
performValidation();
}
}
private void updateExpandableState()
{
updateExpandableTitle();
// Shell shell = getShell();
// Point minSize = shell.getMinimumSize();
// shell.setMinimumSize( shell.getSize().x, minSize.y );
// shell.pack();
expandableComposite.getParent().layout();
// shell.setMinimumSize( minSize );
}
private void updateExpandableTitle()
{
expandableComposite.setText( expandableComposite.isExpanded() ? NLS.bind(
Messages.remoteResourceLookupPage_server_expanded,
serverName )
: NLS.bind( Messages.remoteResourceLookupPage_server_collapsed, serverName,
urlInputComposite.getUrl() ) );
}
private void reload()
{
input = null;
final String url = urlInputComposite.getUrl();
updateLoadControls( false, Messages.remoteResourceLookupPage_loading, IMessageProvider.INFORMATION );
final Exception[] exception = new Exception[1];
try
{
getContainer().run( true, true, new IRunnableWithProgress()
{
public void run( IProgressMonitor monitor )
throws InvocationTargetException, InterruptedException
{
monitor.beginTask( Messages.remoteResourceLookupPage_loading, 2 );
try
{
input = loadResources( url, monitor );
}
catch ( Exception e )
{
exception[0] = e;
}
finally
{
monitor.done();
}
}
} );
}
catch ( InvocationTargetException e )
{
exception[0] = e;
}
catch ( InterruptedException e )
{
exception[0] = e;
}
if ( exception[0] == null )
{
updateLoadControls( true, selectMessage, IMessageProvider.NONE );
}
else
{
String message = exceptionToUIText( exception[0] );
if ( message == null )
{
message = NLS.bind( Messages.remoteResourceLookupPage_error_other, exception[0].getMessage() );
}
log.error( message, exception[0] );
updateLoadControls( true, message, IMessageProvider.ERROR );
}
}
protected String exceptionToUIText( Exception e )
{
// TODO mkleint: this sort of won't work in governor use cases
return ErrorHandlingUtils.convertNexusIOExceptionToUIText( e );
}
private void updateLoadControls( final boolean enable, final String message, final int messageType )
{
if ( resourceComposite.isDisposed() )
{
return;
}
getShell().getDisplay().syncExec( new Runnable()
{
public void run()
{
loadButton.setEnabled( enable );
setInput( input );
if ( messageType == IMessageProvider.ERROR )
{
expandableComposite.setExpanded( true );
updateExpandableState();
urlInputComposite.setFocus();
setMessage( message, messageType );
setPageComplete( false );
}
else
{
resourceComposite.setFocus();
if ( rootGroup.performValidation() == null )
{
setMessage( message, messageType );
}
}
}
} );
}
protected String getServerUrl()
{
return urlInputComposite.getUrlText();
}
@SuppressWarnings( "unchecked" )
protected void addToValidationGroup( Control control, Validator<String> validator )
{
loadButtonGroup.add( control, validator );
}
/**
* this is the validation group root validation group, contains the load label group as well..
* and handles error messages
* @return
*/
protected SwtValidationGroup getRootValidationGroup()
{
return rootGroup;
}
/**
* this is the validation group associated with the url input composite and handles load button enablement
* @return
*/
protected SwtValidationGroup getLoadButtonValidationGroup()
{
return loadButtonGroup;
}
/**
* this is the validation group for handling the ok/finish button
* @return
*/
protected SwtValidationGroup getFinishButtonValidationGroup()
{
return okButtonGroup;
}
abstract protected Composite createResourcePanel( Composite parent );
abstract protected void setInput( Object input );
abstract protected Object loadResources( String url, IProgressMonitor monitor )
throws Exception;
private class LoadButtonValidationUI implements ValidationUI {
public void showProblem(Problem problem) {
if (loadButton == null) return;
loadButton.setEnabled( !problem.isFatal() );
}
public void clearProblem() {
if (loadButton == null) return;
loadButton.setEnabled( true );
}
};
}