-
Notifications
You must be signed in to change notification settings - Fork 384
/
UIUtil.java
3177 lines (2913 loc) · 83.1 KB
/
UIUtil.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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* 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.util;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.Bidi;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.Stack;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.stream.ImageInputStream;
import org.eclipse.birt.core.format.DateFormatter;
import org.eclipse.birt.report.data.adapter.api.DataRequestSession;
import org.eclipse.birt.report.data.adapter.api.DataSessionContext;
import org.eclipse.birt.report.data.adapter.api.ICubeQueryUtil;
import org.eclipse.birt.report.designer.core.DesignerConstants;
import org.eclipse.birt.report.designer.core.model.SessionHandleAdapter;
import org.eclipse.birt.report.designer.core.runtime.ErrorStatus;
import org.eclipse.birt.report.designer.core.runtime.GUIException;
import org.eclipse.birt.report.designer.internal.ui.dialogs.DeleteWarningDialog;
import org.eclipse.birt.report.designer.internal.ui.dialogs.ImportLibraryDialog;
import org.eclipse.birt.report.designer.internal.ui.dialogs.expression.ExpressionButton;
import org.eclipse.birt.report.designer.internal.ui.dialogs.expression.IExpressionButtonProvider;
import org.eclipse.birt.report.designer.internal.ui.editors.IRelatedFileChangeResolve;
import org.eclipse.birt.report.designer.internal.ui.editors.IReportEditor;
import org.eclipse.birt.report.designer.internal.ui.editors.parts.DeferredGraphicalViewer;
import org.eclipse.birt.report.designer.internal.ui.editors.parts.GraphicalEditorWithFlyoutPalette;
import org.eclipse.birt.report.designer.internal.ui.editors.parts.event.IModelEventManager;
import org.eclipse.birt.report.designer.internal.ui.editors.schematic.editparts.DummyEditpart;
import org.eclipse.birt.report.designer.internal.ui.editors.schematic.editparts.GridEditPart;
import org.eclipse.birt.report.designer.internal.ui.editors.schematic.editparts.ListBandEditPart;
import org.eclipse.birt.report.designer.internal.ui.editors.schematic.editparts.ListEditPart;
import org.eclipse.birt.report.designer.internal.ui.editors.schematic.editparts.MultipleEditPart;
import org.eclipse.birt.report.designer.internal.ui.editors.schematic.editparts.ReportElementEditPart;
import org.eclipse.birt.report.designer.internal.ui.editors.schematic.editparts.TableCellEditPart;
import org.eclipse.birt.report.designer.internal.ui.editors.schematic.editparts.TableEditPart;
import org.eclipse.birt.report.designer.internal.ui.extension.ExtendedDataModelUIAdapterHelper;
import org.eclipse.birt.report.designer.internal.ui.extension.ExtendedElementUIPoint;
import org.eclipse.birt.report.designer.internal.ui.extension.ExtensionPointManager;
import org.eclipse.birt.report.designer.internal.ui.extension.IExtendedDataModelUIAdapter;
import org.eclipse.birt.report.designer.internal.ui.extension.experimental.EditpartExtensionManager;
import org.eclipse.birt.report.designer.internal.ui.extension.experimental.PaletteEntryExtension;
import org.eclipse.birt.report.designer.internal.ui.util.bidi.BidiUIUtils;
import org.eclipse.birt.report.designer.nls.Messages;
import org.eclipse.birt.report.designer.ui.IPreferenceConstants;
import org.eclipse.birt.report.designer.ui.IReportGraphicConstants;
import org.eclipse.birt.report.designer.ui.ReportPlatformUIImages;
import org.eclipse.birt.report.designer.ui.ReportPlugin;
import org.eclipse.birt.report.designer.ui.dialogs.GroupDialog;
import org.eclipse.birt.report.designer.ui.editors.AbstractMultiPageEditor;
import org.eclipse.birt.report.designer.ui.extensions.IExtensionConstants;
import org.eclipse.birt.report.designer.ui.newelement.DesignElementFactory;
import org.eclipse.birt.report.designer.ui.preferences.PreferenceFactory;
import org.eclipse.birt.report.designer.ui.views.ElementAdapterManager;
import org.eclipse.birt.report.designer.ui.views.attributes.providers.ChoiceSetFactory;
import org.eclipse.birt.report.designer.util.ColorManager;
import org.eclipse.birt.report.designer.util.DEUtil;
import org.eclipse.birt.report.designer.util.FontManager;
import org.eclipse.birt.report.model.api.AbstractScalarParameterHandle;
import org.eclipse.birt.report.model.api.ActionHandle;
import org.eclipse.birt.report.model.api.ColumnHintHandle;
import org.eclipse.birt.report.model.api.DataSetHandle;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.DesignFileException;
import org.eclipse.birt.report.model.api.ElementDetailHandle;
import org.eclipse.birt.report.model.api.ErrorDetail;
import org.eclipse.birt.report.model.api.GroupHandle;
import org.eclipse.birt.report.model.api.IResourceLocator;
import org.eclipse.birt.report.model.api.LibraryHandle;
import org.eclipse.birt.report.model.api.ListHandle;
import org.eclipse.birt.report.model.api.ModuleHandle;
import org.eclipse.birt.report.model.api.ParameterGroupHandle;
import org.eclipse.birt.report.model.api.ParameterHandle;
import org.eclipse.birt.report.model.api.PropertyHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.ReportElementHandle;
import org.eclipse.birt.report.model.api.ReportItemHandle;
import org.eclipse.birt.report.model.api.ResultSetColumnHandle;
import org.eclipse.birt.report.model.api.SlotHandle;
import org.eclipse.birt.report.model.api.StyleHandle;
import org.eclipse.birt.report.model.api.TableHandle;
import org.eclipse.birt.report.model.api.ThemeHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.birt.report.model.api.core.IAccessControl;
import org.eclipse.birt.report.model.api.elements.DesignChoiceConstants;
import org.eclipse.birt.report.model.api.elements.structures.ColumnHint;
import org.eclipse.birt.report.model.api.elements.structures.ConfigVariable;
import org.eclipse.birt.report.model.api.metadata.IChoice;
import org.eclipse.birt.report.model.api.metadata.IElementDefn;
import org.eclipse.birt.report.model.api.util.StringUtil;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceStatus;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceConverter;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.viewers.AbstractTreeViewer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.osgi.service.environment.Constants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
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.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IPathEditorInput;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.widgets.ColumnLayout;
import org.eclipse.ui.forms.widgets.ILayoutExtension;
import org.eclipse.ui.preferences.ScopedPreferenceStore;
import org.eclipse.ui.texteditor.AbstractTextEditor;
import org.osgi.framework.Bundle;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import com.ibm.icu.text.Collator;
/**
* Utility class for UI related routines.
*/
public class UIUtil
{
protected static final Logger logger = Logger.getLogger( UIUtil.class.getName( ) );
private static final String MSG_DIALOG_TITLE = Messages.getString( "ImportLibraryAction.Title.ImportSuccessfully" ); //$NON-NLS-1$
private static final String MSG_DIALOG_MSG = Messages.getString( "ImportLibraryAction.Message.ImportSuccessfully" ); //$NON-NLS-1$
private static String[] EDITOR_IDS = {
"org.eclipse.birt.report.designer.ui.editors.ReportEditor", //$NON-NLS-1$
"org.eclipse.birt.report.designer.ui.editors.LibraryEditor", //$NON-NLS-1$
"org.eclipse.birt.report.designer.ui.editors.TemplateEditor" //$NON-NLS-1$
};
/**
* Regex pattern for neutral chars in Bidi Algorithm.
*/
static Pattern punctuation = Pattern.compile( "\\p{Punct}||\\p{Blank}||\\p{Space}" ); //$NON-NLS-1$
private static boolean embeddedBrowserTested = false;
private static boolean embeddedBrowserAvailable = false;
/**
* Returns the length in pixels of given string in a control.
*
* @return the length in pixels
*/
public static int getStringWidth( String string, Control control )
{
int width = 0;
GC gc = new GC( control );
width = gc.textExtent( string ).x;
gc.dispose( );
return width;
}
/**
* Returns the maximum length in pixels of given strings in a control. The
* string value in the array should not be null.
*
* @return the length in pixels
*/
public static int getMaxStringWidth( String[] strArray, Control control )
{
int maxWidth = 0;
GC gc = new GC( control );
for ( int i = 0; i < strArray.length; i++ )
{
int width = gc.textExtent( strArray[i] ).x;
maxWidth = maxWidth >= width ? maxWidth : width;
}
gc.dispose( );
return maxWidth;
}
/**
* Returns if current active editor is reportEditor.
*
* @return true if current active editor is reportEditor, or false else.
*/
public static boolean isReportEditorActivated( )
{
return getActiveReportEditor( ) != null;
}
/**
* Returns the current active report editor. The same as getActiveEditor(
* true ).
*
* @return the current active report editor, or null if no report editor is
* active.
*/
public static FormEditor getActiveReportEditor( )
{
return getActiveReportEditor( true );
}
/**
* Returns the current active report editor in current active page or
* current active workbench.
*
* @param activePageOnly
* If this is true, only search the current active page, or will
* search all pages in current workbench, returns the first
* active report or null if not found.
* @return the current active report editor, or null if no report editor is
* active.
*/
public static FormEditor getActiveReportEditor( boolean activePageOnly )
{
IWorkbenchWindow window = PlatformUI.getWorkbench( )
.getActiveWorkbenchWindow( );
if ( window != null )
{
if ( activePageOnly )
{
IWorkbenchPage pg = window.getActivePage( );
if ( pg != null )
{
IEditorPart editor = pg.getActiveEditor( );
if ( editor != null )
{
if ( editor instanceof IReportEditor )
{
IEditorPart part = ( (IReportEditor) editor ).getEditorPart( );
if ( part instanceof FormEditor )
{
return (FormEditor) part;
}
}
else if ( editor instanceof FormEditor )
{
return (FormEditor) editor;
}
}
}
}
else
{
IWorkbenchPage[] pgs = window.getPages( );
for ( int i = 0; i < pgs.length; i++ )
{
IWorkbenchPage pg = pgs[i];
if ( pg != null )
{
IEditorPart editor = pg.getActiveEditor( );
if ( editor instanceof IReportEditor )
{
IEditorPart part = ( (IReportEditor) editor ).getEditorPart( );
if ( part instanceof FormEditor )
{
return (FormEditor) part;
}
}
else if ( editor instanceof FormEditor )
{
return (FormEditor) editor;
}
}
}
}
}
return null;
}
/**
* Returns the current active editor part in current active page or current
* active workbench.
*
* @param activePageOnly
* If this is true, only search the current active page, or will
* search all pages in current workbench, returns the first
* active editor part or null if not found.
* @return the current active editor part, or null if no editor part is
* active.
*/
public static IEditorPart getActiveEditor( boolean activePageOnly )
{
IWorkbenchWindow window = PlatformUI.getWorkbench( )
.getActiveWorkbenchWindow( );
if ( window != null )
{
if ( activePageOnly )
{
IWorkbenchPage pg = window.getActivePage( );
if ( pg != null )
{
return pg.getActiveEditor( );
}
}
else
{
IWorkbenchPage[] pgs = window.getPages( );
for ( int i = 0; i < pgs.length; i++ )
{
IWorkbenchPage pg = pgs[i];
if ( pg != null )
{
IEditorPart editor = pg.getActiveEditor( );
if ( editor != null )
{
return editor;
}
}
}
}
}
return null;
}
/**
* Returns current project according to current selection. 1. If current
* selection is editPart, get editor input and return associated project. 2.
* If current selection is not ediPart, use first selected element, query
* from its IAdaptable interface to get associated project. 3. If the above
* is not working, get the first accessible project in the current workspace
* and return it. 4. If none is accessible, returns null.
*
* @return the default project according to current selection.
*/
public static IProject getDefaultProject( )
{
IWorkbenchWindow benchWindow = PlatformUI.getWorkbench( )
.getActiveWorkbenchWindow( );
IWorkbenchPart part = benchWindow.getPartService( ).getActivePart( );
Object selection = null;
if ( part instanceof IEditorPart )
{
selection = ( (IEditorPart) part ).getEditorInput( );
}
else
{
ISelection sel = benchWindow.getSelectionService( ).getSelection( );
if ( ( sel != null ) && ( sel instanceof IStructuredSelection ) )
{
selection = ( (IStructuredSelection) sel ).getFirstElement( );
}
}
if ( selection instanceof IAdaptable )
{
IResource resource = (IResource) ( (IAdaptable) selection ).getAdapter( IResource.class );
if ( resource != null
&& resource.getProject( ) != null
&& resource.getProject( ).isAccessible( ) )
{
return resource.getProject( );
}
}
IProject[] pjs = ResourcesPlugin.getWorkspace( )
.getRoot( )
.getProjects( );
for ( int i = 0; i < pjs.length; i++ )
{
if ( pjs[i].isAccessible( ) )
{
return pjs[i];
}
}
return null;
}
public static IProject getCurrentProject( )
{
IWorkbench iworkbench = PlatformUI.getWorkbench( );
if ( iworkbench == null )
{
return null;
}
IWorkbenchWindow iworkbenchwindow = iworkbench.getActiveWorkbenchWindow( );
if ( iworkbenchwindow == null )
{
return null;
}
IWorkbenchPage iworkbenchpage = iworkbenchwindow.getActivePage( );
if ( iworkbenchpage == null )
{
return null;
}
IEditorPart ieditorpart = iworkbenchpage.getActiveEditor( );
if ( ieditorpart == null )
{
return null;
}
IEditorInput input = ieditorpart.getEditorInput( );
if ( input == null )
{
return null;
}
return (IProject) ElementAdapterManager.getAdapter( input,
IProject.class );
}
/**
* Returns the default shell used by dialogs
*
* @return the active shell of the current display
*/
public static Shell getDefaultShell( )
{
Shell shell = null;
try
{
shell = PlatformUI.getWorkbench( ).getDisplay( ).getActiveShell( );
if ( shell == null )
{
shell = Display.getCurrent( ).getActiveShell( );
}
if ( shell == null )
{
shell = PlatformUI.getWorkbench( )
.getActiveWorkbenchWindow( )
.getShell( );
}
}
catch ( Exception e )
{
// do nothing
}
if ( shell == null )
{
return new Shell( );
}
return shell;
}
/**
* Creates a new group under the given parent
*
* @param parent
* The parent of the new group, it should be a table or a list
* and should not be null.
* @return true if the group created successfully, false if the creation is
* cancelled or some error occurred.
*/
public static boolean createGroup( DesignElementHandle parent )
{
assert parent != null;
try
{
return addGroup( parent, -1 );
}
catch ( SemanticException e )
{
ExceptionHandler.handle( e );
return false;
}
}
/**
* Creates a new group in the position under the given parent
*
* @param parent
* The parent of the new group, it should be a table or a list
* and should not be null.
* @param position
* insert position
* @return true if the group created successfully, false if the creation is
* cancelled or some error occurred.
*/
public static boolean createGroup( DesignElementHandle parent, int position )
{
assert parent != null;
try
{
return addGroup( parent, position );
}
catch ( SemanticException e )
{
ExceptionHandler.handle( e );
return false;
}
}
private static boolean addGroup( DesignElementHandle parent, int position )
throws SemanticException
{
GroupHandle groupHandle = null;
SlotHandle slotHandle = null;
// ElementFactory factory = parent.getElementFactory( );
DesignElementFactory factory = DesignElementFactory.getInstance( parent.getModuleHandle( ) );
if ( parent instanceof TableHandle )
{
groupHandle = factory.newTableGroup( );
slotHandle = ( (TableHandle) parent ).getGroups( );
int columnCount = ( (TableHandle) parent ).getColumnCount( );
groupHandle.getHeader( ).add( factory.newTableRow( columnCount ) );
groupHandle.getFooter( ).add( factory.newTableRow( columnCount ) );
}
else if ( parent instanceof ListHandle )
{
groupHandle = factory.newListGroup( );
slotHandle = ( (ListHandle) parent ).getGroups( );
}
if ( groupHandle != null && slotHandle != null )
{
slotHandle.add( groupHandle, position );
// if ( !DEUtil.getDataSetList( parent ).isEmpty( ) )
{// If data set can be found or a blank group will be inserted.
GroupDialog dialog = new GroupDialog( getDefaultShell( ),
GroupDialog.GROUP_DLG_TITLE_NEW );
// dialog.setDataSetList( DEUtil.getDataSetList( parent ) );
dialog.setInput( groupHandle );
if ( dialog.open( ) == Window.CANCEL )
{// Cancel the action
return false;
}
}
return true;
}
return false;
}
/**
* Gets the first selected edit part in layout editor. Whenever the user has
* deselected all edit parts, the contents edit part should be returned.
*
* @return the first selected EditPart or root edit part
*/
public static EditPart getCurrentEditPart( )
{
EditPartViewer viewer = getLayoutEditPartViewer( );
if ( viewer == null )
return null;
IStructuredSelection targets = (IStructuredSelection) viewer.getSelection( );
if ( targets.isEmpty( ) )
return null;
return (EditPart) targets.getFirstElement( );
}
/**
* Gets EditPartViewer in layout editor.
*
* @return the EditPartViewer in layout editor, or null if not found.
*/
public static EditPartViewer getLayoutEditPartViewer( )
{
IEditorPart part = PlatformUI.getWorkbench( )
.getActiveWorkbenchWindow( )
.getActivePage( )
.getActiveEditor( );
AbstractMultiPageEditor reportEditor = null;
if ( part instanceof AbstractMultiPageEditor )
{
reportEditor = (AbstractMultiPageEditor) part;
}
else if ( part instanceof IReportEditor )
{
IEditorPart activeEditor = ( (IReportEditor) part ).getEditorPart( );
if ( activeEditor instanceof AbstractMultiPageEditor )
{
reportEditor = (AbstractMultiPageEditor) activeEditor;
}
}
if ( reportEditor == null
|| !( reportEditor.getActivePageInstance( ) instanceof GraphicalEditorWithFlyoutPalette ) )
{
return null;
}
return ( (GraphicalEditorWithFlyoutPalette) reportEditor.getActivePageInstance( ) ).getGraphicalViewer( );
}
/**
* Creates a new grid layout without margins by default
*
* @return the layout created
*/
public static GridLayout createGridLayoutWithoutMargin( )
{
GridLayout layout = new GridLayout( );
layout.marginHeight = layout.marginWidth = 0;
return layout;
}
public static GridLayout createGridLayoutWithMargin( int margin )
{
GridLayout layout = new GridLayout( );
layout.marginHeight = layout.marginWidth = margin;
return layout;
}
/**
* Creates a new grid layout without margins with given the number of
* columns, and whether or not the columns should be forced to have the same
* width
*
* @param numsColumn
* the number of columns in the grid
* @param makeColumnsEqualWidth
* whether or not the columns will have equal width
*
* @return the layout created
*/
public static GridLayout createGridLayoutWithoutMargin( int numsColumn,
boolean makeColumnsEqualWidth )
{
GridLayout layout = new GridLayout( numsColumn, makeColumnsEqualWidth );
layout.marginHeight = layout.marginWidth = 0;
return layout;
}
/**
* Convert the give string to GUI style, which cannot be null
*
* @param string
* the string to convert
* @return the string, or an empty string for null
*/
public static String convertToGUIString( String string )
{
if ( string == null )
{
string = ""; //$NON-NLS-1$
}
return string;
}
/**
* Convert the give string to Model style
*
* @param string
* the string to convert
* @param trim
* specify if the string needs to be trimmed
* @return the string, or null for an empty string
*/
public static String convertToModelString( String string, boolean trim )
{
if ( string == null )
{
return null;
}
if ( trim )
{
string = string.trim( );
}
if ( string.length( ) == 0 )
{
string = null;
}
return string;
}
/**
* Returns the width hint for the given control.
*
* @param wHint
* the width hint
* @param c
* the control
*
* @return the width hint
*/
public static int getWidthHint( int wHint, Control c )
{
boolean wrap = isWrapControl( c );
return wrap ? wHint : SWT.DEFAULT;
}
/**
* Returns the height hint for the given control.
*
* @param hHint
* the width hint
* @param c
* the control
*
* @return the height hint
*/
public static int getHeightHint( int hHint, Control c )
{
if ( c instanceof Composite )
{
Layout layout = ( (Composite) c ).getLayout( );
if ( layout instanceof ColumnLayout )
return hHint;
}
return SWT.DEFAULT;
}
/**
* Updates the page scroll increment for given composite.
*
* @param scomp
*/
public static void updatePageIncrement( ScrolledComposite scomp )
{
ScrollBar vbar = scomp.getVerticalBar( );
if ( vbar != null )
{
Rectangle clientArea = scomp.getClientArea( );
int increment = clientArea.height - 5;
vbar.setPageIncrement( increment );
}
}
private static boolean isWrapControl( Control c )
{
if ( c instanceof Composite )
{
return ( (Composite) c ).getLayout( ) instanceof ILayoutExtension;
}
return ( c.getStyle( ) & SWT.WRAP ) != 0;
}
/**
* Returns table editpart.
*
* @param editParts
* a list of editpart
* @return the current selected table editpart, null if no table editpart,
* more than one table, or other non-table editpart. Cell editpart
* is also a type of table editpart.
*/
public static TableEditPart getTableEditPart( List editParts )
{
if ( editParts == null || editParts.isEmpty( ) )
return null;
int size = editParts.size( );
TableEditPart part = null;
for ( int i = 0; i < size; i++ )
{
Object obj = editParts.get( i );
TableEditPart currentEditPart = null;
if ( obj instanceof TableEditPart )
{
currentEditPart = (TableEditPart) obj;
}
else if ( obj instanceof TableCellEditPart )
{
currentEditPart = (TableEditPart) ( (TableCellEditPart) obj ).getParent( );
}
else if ( obj instanceof DummyEditpart )
{
continue;
}
if ( part == null )
{
part = currentEditPart;
}
// Check if select only one table
if ( currentEditPart == null
|| currentEditPart != null
&& part != currentEditPart )
{
return null;
}
}
// Only table permitted
if ( part instanceof GridEditPart )
return null;
return part;
}
/**
* @param editParts
* @return
*/
public static ReportElementEditPart getTableMultipleEditPart( List editParts )
{
if ( editParts == null || editParts.isEmpty( ) )
return null;
int size = editParts.size( );
ReportElementEditPart part = null;
for ( int i = 0; i < size; i++ )
{
Object obj = editParts.get( i );
ReportElementEditPart currentEditPart = null;
if ( obj instanceof MultipleEditPart
&& ( (MultipleEditPart) obj ).getModel( ) instanceof TableHandle )
{
currentEditPart = (ReportElementEditPart) obj;
}
else if ( obj instanceof DummyEditpart )
{
continue;
}
if ( part == null )
{
part = currentEditPart;
}
// Check if select only one table
if ( currentEditPart == null
|| currentEditPart != null
&& part != currentEditPart )
{
return null;
}
}
// Only table permitted
if ( part instanceof GridEditPart )
return null;
return part;
}
/**
* Returns list editpart.
*
* @param editParts
* a list of editpart
* @return the current selected list editpart, null if no list editpart,
* more than one list, or other list editpart. List band editpart is
* also a type of list editpart.
*/
public static ListEditPart getListEditPart( List editParts )
{
if ( editParts == null || editParts.isEmpty( ) )
return null;
int size = editParts.size( );
ListEditPart part = null;
for ( int i = 0; i < size; i++ )
{
Object obj = editParts.get( i );
ListEditPart currentEditPart = null;
if ( obj instanceof ListEditPart )
{
currentEditPart = (ListEditPart) obj;
}
else if ( obj instanceof ListBandEditPart )
{
currentEditPart = (ListEditPart) ( (ListBandEditPart) obj ).getParent( );
}
if ( part == null )
{
part = currentEditPart;
}
// Check if select only one list
if ( currentEditPart == null
|| currentEditPart != null
&& part != currentEditPart )
{
return null;
}
}
return part;
}
/**
* Tests if the specified element is on the given tree viewer
*
* @param treeViewer
* the tree viewer
* @param element
* the element
*
* @return true if the element is on the tree, or false else.
*/
public static boolean containElement( AbstractTreeViewer treeViewer,
Object element )
{
ITreeContentProvider provider = (ITreeContentProvider) treeViewer.getContentProvider( );
Object input = treeViewer.getInput( );
if ( input instanceof Object[] )
{
Object[] inputs = (Object[]) input;
for ( int i = 0; i < inputs.length; i++ )
{
if ( containElement( inputs[i], provider, element ) )
{
return true;
}
}
return false;
}
return containElement( input, provider, element );
}
private static boolean containElement( Object parent,
ITreeContentProvider provider, Object element )
{
if ( parent == null )
{
return false;
}
if ( parent == element || parent.equals( element ) )
{
return true;
}
if ( provider == null )
{
return false;
}
Object[] children = provider.getChildren( parent );
for ( int i = 0; i < children.length; i++ )
{
if ( containElement( children[i], provider, element ) )
{
return true;
}
}