-
Notifications
You must be signed in to change notification settings - Fork 385
/
ExtensionPointManager.java
554 lines (503 loc) · 15.7 KB
/
ExtensionPointManager.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*******************************************************************************
* Copyright (c) 2004 Actuate Corporation.
* 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
*
* Contributors:
* Actuate Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.birt.report.designer.internal.ui.extension;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.eclipse.birt.core.preference.IPreferenceChangeListener;
import org.eclipse.birt.core.preference.PreferenceChangeEvent;
import org.eclipse.birt.report.designer.internal.ui.util.ExceptionHandler;
import org.eclipse.birt.report.designer.internal.ui.util.Policy;
import org.eclipse.birt.report.designer.ui.ReportPlatformUIImages;
import org.eclipse.birt.report.designer.ui.extensions.IExtensionConstants;
import org.eclipse.birt.report.designer.ui.extensions.IMenuBuilder;
import org.eclipse.birt.report.designer.ui.extensions.IProviderFactory;
import org.eclipse.birt.report.designer.util.DEUtil;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
/**
* The ExtensionPoinyManager is utility class to retrieve IExtendedElementUI
* extensions by model extension ID, or full list.It caches the information to
* avoid reading the extensions each time.
*/
public class ExtensionPointManager implements IPreferenceChangeListener
{
private Map<String, ExtendedElementUIPoint> reportItemUIMap = null;
private Map<String, IMenuBuilder> menuBuilderMap = null;
private Map<String, IProviderFactory> providerFactoryMap = null;
private Map<String, List<String>> prefereces = new HashMap<String, List<String>>( );
private Map<String, List<IPreferenceChangeListener>> prefListeners = new HashMap<String, List<IPreferenceChangeListener>>( );
private volatile static ExtensionPointManager instance = null;
private ExtensionPointManager( )
{
}
public static ExtensionPointManager getInstance( )
{
if ( instance == null )
{
synchronized ( ExtensionPointManager.class )
{
if ( instance == null )
{
instance = new ExtensionPointManager( );
}
}
}
return instance;
}
/**
* Gets the list of all the extended element points.
*
* @return Returns the list of all the extended element point
* (ExtendedElementUIPoint).
*/
public List<ExtendedElementUIPoint> getExtendedElementPoints( )
{
return Arrays.asList( getReportItemUIMap( ).values( )
.toArray( new ExtendedElementUIPoint[getReportItemUIMap( ).size( )] ) );
}
/**
* Gets the extended element point with the specified extension name.
*
* @param extensionName
* the extension name of the extended element
*
* @return Returns the extended element point, or null if any problem exists
*/
public ExtendedElementUIPoint getExtendedElementPoint( String extensionName )
{
assert extensionName != null;
return getReportItemUIMap( ).get( extensionName );
}
/**
* Returns the menu builder for the given element.
*
* @param elementName
* the name of the element
* @return the menu builder, or null if there's no builder defined for the
* element
*/
public IMenuBuilder getMenuBuilder( String elementName )
{
return getMenuBuilderMap( ).get( elementName );
}
/**
* Returns the provider factory for the given element.
*
* @param elementName
* the name of the element
* @return the provider factory, or null if there's no factory defined for
* the element
*/
public IProviderFactory getProviderFactory( String elementName )
{
return getProviderFactoryMap( ).get( elementName );
}
private Map<String, ExtendedElementUIPoint> getReportItemUIMap( )
{
synchronized ( this )
{
if ( reportItemUIMap == null )
{
reportItemUIMap = new HashMap<String, ExtendedElementUIPoint>( );
for ( Iterator<IExtension> iter = getExtensionElements( IExtensionConstants.EXTENSION_REPORT_ITEM_UI ).iterator( ); iter.hasNext( ); )
{
IExtension extension = iter.next( );
ExtendedElementUIPoint point = createReportItemUIPoint( extension );
if ( point != null )
{
reportItemUIMap.put( point.getExtensionName( ), point );
}
}
}
return reportItemUIMap;
}
}
private Map<String, IMenuBuilder> getMenuBuilderMap( )
{
synchronized ( this )
{
if ( menuBuilderMap == null )
{
menuBuilderMap = new HashMap<String, IMenuBuilder>( );
for ( Iterator<IExtension> iter = getExtensionElements( IExtensionConstants.EXTENSION_MENU_BUILDERS ).iterator( ); iter.hasNext( ); )
{
IExtension extension = iter.next( );
IConfigurationElement[] elements = extension.getConfigurationElements( );
for ( int i = 0; i < elements.length; i++ )
{
if ( IExtensionConstants.ELEMENT_MENU_BUILDER.equals( elements[i].getName( ) ) )
{
String elementId = elements[i].getAttribute( IExtensionConstants.ATTRIBUTE_ELEMENT_NAME );
try
{
Object menuBuilder = elements[i].createExecutableExtension( IExtensionConstants.ATTRIBUTE_CLASS );
if ( menuBuilder instanceof IMenuBuilder )
{
menuBuilderMap.put( elementId,
(IMenuBuilder) menuBuilder );
}
}
catch ( CoreException e )
{
}
}
}
}
}
return menuBuilderMap;
}
}
private Map<String, IProviderFactory> getProviderFactoryMap( )
{
synchronized ( this )
{
if ( providerFactoryMap == null )
{
providerFactoryMap = new HashMap<String, IProviderFactory>( );
for ( Iterator<IExtension> iter = getExtensionElements( IExtensionConstants.EXTENSION_PROVIDER_FACTORIES ).iterator( ); iter.hasNext( ); )
{
IExtension extension = iter.next( );
IConfigurationElement[] elements = extension.getConfigurationElements( );
for ( int i = 0; i < elements.length; i++ )
{
if ( IExtensionConstants.ELEMENT_PROVIDER_FACTORY.equals( elements[i].getName( ) ) )
{
String elementId = elements[i].getAttribute( IExtensionConstants.ATTRIBUTE_ELEMENT_NAME );
try
{
Object factory = elements[i].createExecutableExtension( IExtensionConstants.ATTRIBUTE_CLASS );
if ( factory instanceof IProviderFactory )
{
providerFactoryMap.put( elementId,
(IProviderFactory) factory );
}
}
catch ( CoreException e )
{
}
}
}
}
}
return providerFactoryMap;
}
}
private List<IExtension> getExtensionElements( String id )
{
IExtensionRegistry registry = Platform.getExtensionRegistry( );
if ( registry == null )
{// extension registry cannot be resolved
return Collections.emptyList( );
}
IExtensionPoint extensionPoint = registry.getExtensionPoint( id );
if ( extensionPoint == null )
{// extension point cannot be resolved
return Collections.emptyList( );
}
return Arrays.asList( extensionPoint.getExtensions( ) );
}
private ExtendedElementUIPoint createReportItemUIPoint( IExtension extension )
{
IConfigurationElement[] elements = extension.getConfigurationElements( );
if ( elements != null && elements.length > 0 )
{
return loadElements( elements );
}
return null;
}
private ExtendedElementUIPoint loadElements(
IConfigurationElement[] elements )
{
ExtendedElementUIPoint newPoint = new ExtendedElementUIPoint( );
if ( elements != null )
{
try
{
for ( int i = 0; i < elements.length; i++ )
{
loadAttributes( newPoint, elements[i] );
}
}
catch ( Exception e )
{
ExceptionHandler.handle( e );
return null;
}
}
if ( DEUtil.getMetaDataDictionary( )
.getExtension( newPoint.getExtensionName( ) ) == null )
{
// Non-defined element. Ignore
return null;
}
if ( Policy.TRACING_EXTENSION_LOAD )
{
System.out.println( "GUI Extesion Manager >> Loads " //$NON-NLS-1$
+ newPoint.getExtensionName( ) );
}
return newPoint;
}
private void loadAttributes( ExtendedElementUIPoint newPoint,
IConfigurationElement element ) throws CoreException
{
String elementName = element.getName( );
if ( IExtensionConstants.ELEMENT_MODEL.equals( elementName ) )
{
String value = element.getAttribute( IExtensionConstants.ATTRIBUTE_EXTENSION_NAME );
newPoint.setExtensionName( value );
}
else if ( IExtensionConstants.ELEMENT_REPORT_ITEM_FIGURE_UI.equals( elementName )
|| IExtensionConstants.ELEMENT_REPORT_ITEM_IMAGE_UI.equals( elementName )
|| IExtensionConstants.ELEMENT_REPORT_ITEM_LABEL_UI.equals( elementName ) )
{
String className = element.getAttribute( IExtensionConstants.ATTRIBUTE_CLASS );
if ( className != null )
{
Object ui = element.createExecutableExtension( IExtensionConstants.ATTRIBUTE_CLASS );
newPoint.setReportItemUI( new ExtendedUIAdapter( ui ) );
}
}
else if ( IExtensionConstants.ELEMENT_BUILDER.equals( elementName ) )
{
loadClass( newPoint,
element,
IExtensionConstants.ATTRIBUTE_CLASS,
IExtensionConstants.ELEMENT_BUILDER );
}
else if ( IExtensionConstants.ELEMENT_PROPERTYEDIT.equals( elementName ) )
{
loadClass( newPoint,
element,
IExtensionConstants.ATTRIBUTE_CLASS,
IExtensionConstants.ELEMENT_PROPERTYEDIT );
}
else if ( IExtensionConstants.ELEMENT_PALETTE.equals( elementName ) )
{
loadIconAttribute( newPoint,
element,
IExtensionConstants.ATTRIBUTE_KEY_PALETTE_ICON,
IExtensionConstants.ATTRIBUTE_ICON,
false );
loadIconAttribute( newPoint,
element,
IExtensionConstants.ATTRIBUTE_KEY_PALETTE_ICON_LARGE,
IExtensionConstants.ATTRIBUTE_ICON_LARGE,
false );
loadStringAttribute( newPoint,
element,
IExtensionConstants.ATTRIBUTE_PALETTE_CATEGORY );
loadStringAttribute( newPoint,
element,
IExtensionConstants.ATTRIBUTE_PALETTE_CATEGORY_DISPLAYNAME );
}
else if ( IExtensionConstants.ELEMENT_EDITOR.equals( elementName ) )
{
loadBooleanAttribute( newPoint,
element,
IExtensionConstants.ATTRIBUTE_EDITOR_SHOW_IN_DESIGNER );
loadStringAttribute( newPoint,
element,
IExtensionConstants.ATTRIBUTE_EDITOR_SHOW_IN_DESIGNER_BY_PREFERENCE );
loadBooleanAttribute( newPoint,
element,
IExtensionConstants.ATTRIBUTE_EDITOR_SHOW_IN_MASTERPAGE );
loadBooleanAttribute( newPoint,
element,
IExtensionConstants.ATTRIBUTE_EDITOR_CAN_RESIZE );
loadStringAttribute( newPoint,
element,
IExtensionConstants.ATTRIBUTE_EDITOR_MENU_LABEL );
}
else if ( IExtensionConstants.ELEMENT_OUTLINE.equals( elementName ) )
{
loadIconAttribute( newPoint,
element,
IExtensionConstants.ATTRIBUTE_KEY_OUTLINE_ICON,
IExtensionConstants.ATTRIBUTE_ICON,
true );
}
else if ( IExtensionConstants.ELEMENT_DESCRIPTION.equals( elementName ) )
{
String value = element.getValue( );
if ( value != null )
{
newPoint.setAttribute( IExtensionConstants.ATTRIBUTE_KEY_DESCRIPTION,
value );
}
}
}
/**
* @param newPoint
* the extension point instance
* @param element
* the configuration element
* @param className
* the name of the class attribute
*/
private void loadClass( ExtendedElementUIPoint newPoint,
IConfigurationElement element, String className,
String attributeName )
{
String value = element.getAttribute( className );
if ( value != null )
{
try
{
newPoint.setClass( attributeName,
element.createExecutableExtension( className ) );
}
catch ( CoreException e )
{
}
}
}
private ImageDescriptor getImageDescriptor( IConfigurationElement element, String attrName )
{
assert element != null;
IExtension extension = element.getDeclaringExtension( );
String iconPath = element.getAttribute( attrName );
if ( iconPath == null )
{
return null;
}
URL path = Platform.getBundle( extension.getNamespace( ) )
.getEntry( "/" ); //$NON-NLS-1$
try
{
return ImageDescriptor.createFromURL( new URL( path, iconPath ) );
}
catch ( MalformedURLException e )
{
}
return null;
}
private void loadStringAttribute( ExtendedElementUIPoint newPoint,
IConfigurationElement element, String attributeName )
{
String value = element.getAttribute( attributeName );
if ( value != null )
{
newPoint.setAttribute( attributeName, value );
}
}
private void loadBooleanAttribute( ExtendedElementUIPoint newPoint,
IConfigurationElement element, String attributeName )
{
String value = element.getAttribute( attributeName );
if ( value != null )
{
newPoint.setAttribute( attributeName, Boolean.valueOf( value ) );
}
}
private void loadIconAttribute( ExtendedElementUIPoint newPoint,
IConfigurationElement element, String keyName, String attributeName, boolean shared )
{
ImageDescriptor imageDescriptor = getImageDescriptor( element, attributeName );
if ( imageDescriptor != null )
{
if ( shared )
{
String symbolName = ReportPlatformUIImages.getIconSymbolName( newPoint.getExtensionName( ),
keyName );
ReportPlatformUIImages.declareImage( symbolName,
imageDescriptor );
}
newPoint.setAttribute( keyName, imageDescriptor );
}
}
public void preferenceChange( PreferenceChangeEvent event )
{
Iterator<String> exntesions = prefereces.keySet( ).iterator( );
while ( exntesions.hasNext( ) )
{
String extension = exntesions.next( );
List<String> prefs = prefereces.get( extension );
if ( prefs.contains( event.getKey( ) ) )
{
List<IPreferenceChangeListener> listeners = prefListeners.get( extension );
if ( listeners != null )
{
for ( int i = 0; i < listeners.size( ); i++ )
{
listeners.get( i ).preferenceChange( event );
}
}
}
}
}
public void addPreference( String extension, String preference )
{
if ( preference != null && extension != null )
{
if ( !prefereces.containsKey( extension ) )
{
prefereces.put( extension, new ArrayList<String>( ) );
}
List<String> prefs = prefereces.get( extension );
if ( !prefs.contains( preference ) )
prefs.add( preference );
}
}
public void removePreference( String extension, String preference )
{
if ( preference != null && extension != null )
{
if ( !prefereces.containsKey( extension ) )
{
return;
}
List<String> prefs = prefereces.get( extension );
if ( prefs.contains( preference ) )
prefs.remove( preference );
}
}
public void addPreferenceChangeListener( String extension,
IPreferenceChangeListener listener )
{
if ( listener != null && extension != null )
{
if ( !prefListeners.containsKey( extension ) )
{
prefListeners.put( extension,
new ArrayList<IPreferenceChangeListener>( ) );
}
List<IPreferenceChangeListener> lisnteners = prefListeners.get( extension );
if ( !lisnteners.contains( listener ) )
lisnteners.add( listener );
}
}
public void removePreferenceChangeListener( String extension,
IPreferenceChangeListener listener )
{
if ( listener != null && extension != null )
{
if ( !prefListeners.containsKey( extension ) )
{
return;
}
List<IPreferenceChangeListener> listeners = prefListeners.get( extension );
if ( listeners.contains( listener ) )
listeners.remove( listener );
}
}
}