1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18  *     contributors may be used to endorse or promote products derived
19  *     from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  *************************************************************************/
34 
35 //  Example DataPilot source component
36 
37 //  helper class to hold the settings
38 
39 class ExampleSettings
40 {
41     static public final int nDimensionCount = 6;
42     static public final int nValueDimension = 4;
43     static public final int nDataDimension = 5;
44     static public final String [] aDimensionNames = {
45         "ones", "tens", "hundreds", "thousands", "value", "" };
46 
47     static public final String getMemberName( int nMember )
48     {
49         return String.valueOf( nMember );
50     }
51 
52     public int nMemberCount = 3;
53     public java.util.List aColDimensions = new java.util.ArrayList();
54     public java.util.List aRowDimensions = new java.util.ArrayList();
55 }
56 
57 //  XPropertySetInfo implementation for getPropertySetInfo
58 
59 class ExamplePropertySetInfo implements com.sun.star.beans.XPropertySetInfo
60 {
61     private com.sun.star.beans.Property[] aProperties;
62 
63     public ExamplePropertySetInfo( com.sun.star.beans.Property[] aProps )
64     {
65         aProperties = aProps;
66     }
67 
68     public com.sun.star.beans.Property[] getProperties()
69     {
70         return aProperties;
71     }
72 
73     public com.sun.star.beans.Property getPropertyByName( String aName )
74                     throws com.sun.star.beans.UnknownPropertyException
75     {
76         for ( int i=0; i<aProperties.length; i++ )
77             if ( aProperties[i].Name.equals( aName ) )
78                 return aProperties[i];
79         throw new com.sun.star.beans.UnknownPropertyException();
80     }
81 
82     public boolean hasPropertyByName( String aName )
83     {
84         for ( int i=0; i<aProperties.length; i++ )
85             if ( aProperties[i].Name.equals( aName ) )
86                 return true;
87         return false;
88     }
89 }
90 
91 //  implementation of com.sun.star.sheet.DataPilotSourceMember
92 
93 class ExampleMember implements com.sun.star.container.XNamed,
94       com.sun.star.beans.XPropertySet
95 {
96     private ExampleSettings aSettings;
97     private int nDimension;
98     private int nMember;
99 
100     public ExampleMember( ExampleSettings aSet, int nDim, int nMbr )
101     {
102         aSettings = aSet;
103         nDimension = nDim;
104         nMember = nMbr;
105     }
106 
107     //  XNamed
108 
109     public String getName()
110     {
111         return ExampleSettings.getMemberName( nMember );
112     }
113 
114     public void setName( String aName )
115     {
116         // ignored
117     }
118 
119     //  XPropertySet
120 
121     public com.sun.star.beans.XPropertySetInfo getPropertySetInfo()
122     {
123         return new ExamplePropertySetInfo( new com.sun.star.beans.Property[] {
124             new com.sun.star.beans.Property( "IsVisible",   -1,
125                         new com.sun.star.uno.Type( Boolean.class ), (short) 0),
126             new com.sun.star.beans.Property( "ShowDetails", -1,
127                         new com.sun.star.uno.Type( Boolean.class ), (short) 0) });
128     }
129 
130     public void setPropertyValue( String aPropertyName, Object aValue )
131         throws com.sun.star.beans.UnknownPropertyException
132     {
133         if ( aPropertyName.equals( "IsVisible" ) ||
134              aPropertyName.equals( "ShowDetails" ) )
135         {
136             // ignored
137         }
138         else
139             throw new com.sun.star.beans.UnknownPropertyException();
140     }
141 
142     public Object getPropertyValue( String aPropertyName )
143         throws com.sun.star.beans.UnknownPropertyException
144     {
145         if ( aPropertyName.equals( "IsVisible" ) ||
146              aPropertyName.equals( "ShowDetails" ) )
147         {
148             return new Boolean( true );     // always true
149         }
150         else
151             throw new com.sun.star.beans.UnknownPropertyException();
152     }
153 
154     public void addPropertyChangeListener(
155         String aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener)
156     {
157     }
158     public void removePropertyChangeListener(
159         String aPropertyName, com.sun.star.beans.XPropertyChangeListener aListener)
160     {
161     }
162     public void addVetoableChangeListener(
163         String PropertyName, com.sun.star.beans.XVetoableChangeListener aListener)
164     {
165     }
166     public void removeVetoableChangeListener(
167         String PropertyName, com.sun.star.beans.XVetoableChangeListener aListener)
168     {
169     }
170 }
171 
172 //  implementation of com.sun.star.sheet.DataPilotSourceMembers
173 
174 class ExampleMembers implements com.sun.star.container.XNameAccess
175 {
176     private ExampleSettings aSettings;
177     private int nDimension;
178     private ExampleMember[] aMembers;
179 
180     public ExampleMembers( ExampleSettings aSet, int nDim )
181     {
182         aSettings = aSet;
183         nDimension = nDim;
184         aMembers = new ExampleMember[ aSettings.nMemberCount ];
185     }
186 
187     //  XNameAccess
188 
189     public com.sun.star.uno.Type getElementType()
190     {
191         return new com.sun.star.uno.Type( com.sun.star.container.XNamed.class );
192     }
193 
194     public boolean hasElements()
195     {
196         return true;    // always has elements
197     }
198 
199     public Object getByName( String aName )
200         throws com.sun.star.container.NoSuchElementException
201     {
202         int nCount = aSettings.nMemberCount;
203         for ( int i=0; i<nCount; i++ )
204             if ( aName.equals( ExampleSettings.getMemberName( i ) ) )
205             {
206                 if ( aMembers[i] == null )
207                     aMembers[i] = new ExampleMember( aSettings, nDimension, i );
208                 return aMembers[i];
209             }
210         throw new com.sun.star.container.NoSuchElementException();
211     }
212 
213     public String[] getElementNames()
214     {
215         int nCount = aSettings.nMemberCount;
216         String [] aNames = new String[ nCount ];
217         for ( int i=0; i<nCount; i++ )
218             aNames[i] = ExampleSettings.getMemberName( i );
219         return aNames;
220     }
221 
222     public boolean hasByName( String aName )
223     {
224         int nCount = aSettings.nMemberCount;
225         for ( int i=0; i<nCount; i++ )
226             if ( aName.equals( ExampleSettings.getMemberName( i ) ) )
227                 return true;
228         return false;
229     }
230 }
231 
232 //  implementation of com.sun.star.sheet.DataPilotSourceLevel
233 
234 class ExampleLevel implements
235                     com.sun.star.container.XNamed,
236                     com.sun.star.sheet.XMembersSupplier,
237                     com.sun.star.sheet.XDataPilotMemberResults,
238                     com.sun.star.beans.XPropertySet
239 {
240     private ExampleSettings aSettings;
241     private int nDimension;
242     private ExampleMembers aMembers;
243 
244     public ExampleLevel( ExampleSettings aSet, int nDim )
245     {
246         aSettings = aSet;
247         nDimension = nDim;
248     }
249 
250     // XNamed
251 
252     public String getName()
253     {
254         return ExampleSettings.aDimensionNames[ nDimension ];
255     }
256 
257     public void setName( String aName )
258     {
259         // ignored
260     }
261 
262     // XMembersSupplier
263 
264     public com.sun.star.container.XNameAccess getMembers()
265     {
266         if ( aMembers == null )
267             aMembers = new ExampleMembers( aSettings, nDimension );
268         return aMembers;
269     }
270 
271     // XDataPilotMemberResults
272 
273     public com.sun.star.sheet.MemberResult[] getResults()
274     {
275         int nDimensions = 0;
276         int nPosition = aSettings.aColDimensions.indexOf( new Integer(nDimension));
277         if ( nPosition >= 0 )
278             nDimensions = aSettings.aColDimensions.size();
279         else
280         {
281             nPosition = aSettings.aRowDimensions.indexOf( new Integer(nDimension));
282             if ( nPosition >= 0 )
283                 nDimensions = aSettings.aRowDimensions.size();
284         }
285 
286         if ( nPosition < 0 )
287             return new com.sun.star.sheet.MemberResult[0];
288 
289         int nMembers = aSettings.nMemberCount;
290         int nRepeat = 1;
291         int nFill = 1;
292         for ( int i=0; i<nDimensions; i++ )
293         {
294             if ( i < nPosition )
295                 nRepeat *= nMembers;
296             else if ( i > nPosition )
297                 nFill *= nMembers;
298         }
299         int nSize = nRepeat * nMembers * nFill;
300 
301         com.sun.star.sheet.MemberResult[] aResults =
302             new com.sun.star.sheet.MemberResult[nSize];
303         int nResultPos = 0;
304         for (int nOuter=0; nOuter<nRepeat; nOuter++)
305         {
306             for (int nMember=0; nMember<nMembers; nMember++)
307             {
308                 aResults[nResultPos] = new com.sun.star.sheet.MemberResult();
309                 aResults[nResultPos].Name = ExampleSettings.getMemberName(nMember);
310                 aResults[nResultPos].Caption = aResults[nResultPos].Name;
311                 aResults[nResultPos].Flags =
312                     com.sun.star.sheet.MemberResultFlags.HASMEMBER;
313                 ++nResultPos;
314 
315                 for (int nInner=1; nInner<nFill; nInner++)
316                 {
317                     aResults[nResultPos] = new com.sun.star.sheet.MemberResult();
318                     aResults[nResultPos].Flags =
319                         com.sun.star.sheet.MemberResultFlags.CONTINUE;
320                     ++nResultPos;
321                 }
322             }
323         }
324         return aResults;
325     }
326 
327     //  XPropertySet
328 
329     public com.sun.star.beans.XPropertySetInfo getPropertySetInfo()
330     {
331         return new ExamplePropertySetInfo( new com.sun.star.beans.Property[] {
332             new com.sun.star.beans.Property( "SubTotals", -1,
333                             new com.sun.star.uno.Type(
334                                 com.sun.star.sheet.GeneralFunction[].class ),
335                                              (short) 0 ),
336             new com.sun.star.beans.Property( "ShowEmpty", -1,
337                              new com.sun.star.uno.Type( Boolean.class ),
338                                              (short) 0 ) } );
339     }
340 
341     public void setPropertyValue( String aPropertyName, Object aValue )
342         throws com.sun.star.beans.UnknownPropertyException
343     {
344         if ( aPropertyName.equals( "SubTotals" ) ||
345              aPropertyName.equals( "ShowEmpty" ) )
346         {
347             // ignored
348         }
349         else
350             throw new com.sun.star.beans.UnknownPropertyException();
351     }
352 
353     public Object getPropertyValue( String aPropertyName )
354         throws com.sun.star.beans.UnknownPropertyException
355     {
356         if ( aPropertyName.equals( "SubTotals" ) )
357             return new com.sun.star.sheet.GeneralFunction[0];
358         else if ( aPropertyName.equals( "ShowEmpty" ) )
359             return new Boolean( true );
360         else
361             throw new com.sun.star.beans.UnknownPropertyException();
362     }
363 
364     public void addPropertyChangeListener(
365         String aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener)
366     {
367     }
368     public void removePropertyChangeListener(
369         String aPropertyName, com.sun.star.beans.XPropertyChangeListener aListener)
370     {
371     }
372     public void addVetoableChangeListener(
373         String PropertyName, com.sun.star.beans.XVetoableChangeListener aListener)
374     {
375     }
376     public void removeVetoableChangeListener(
377         String PropertyName, com.sun.star.beans.XVetoableChangeListener aListener)
378     {
379     }
380 }
381 
382 //  implementation of com.sun.star.sheet.DataPilotSourceLevels
383 
384 class ExampleLevels implements com.sun.star.container.XNameAccess
385 {
386     private ExampleSettings aSettings;
387     private int nDimension;
388     private ExampleLevel aLevel;
389 
390     public ExampleLevels( ExampleSettings aSet, int nDim )
391     {
392         aSettings = aSet;
393         nDimension = nDim;
394     }
395 
396     // XNameAccess
397 
398     public com.sun.star.uno.Type getElementType()
399     {
400         return new com.sun.star.uno.Type( com.sun.star.container.XNamed.class );
401     }
402 
403     public boolean hasElements()
404     {
405         return true;    // always has elements
406     }
407 
408     public Object getByName( String aName )
409         throws com.sun.star.container.NoSuchElementException
410     {
411         //  there's only one level with the same name as the dimension / hierarchy
412         if ( aName.equals( ExampleSettings.aDimensionNames[nDimension] ) )
413         {
414             if ( aLevel == null )
415                 aLevel = new ExampleLevel( aSettings, nDimension );
416             return aLevel;
417         }
418         throw new com.sun.star.container.NoSuchElementException();
419     }
420 
421     public String[] getElementNames()
422     {
423         String [] aNames = new String[ 1 ];
424         aNames[0] = ExampleSettings.aDimensionNames[nDimension];
425         return aNames;
426     }
427 
428     public boolean hasByName( String aName )
429     {
430         return aName.equals( ExampleSettings.aDimensionNames[nDimension] );
431     }
432 }
433 
434 //  implementation of com.sun.star.sheet.DataPilotSourceHierarchy
435 
436 class ExampleHierarchy implements com.sun.star.container.XNamed,
437       com.sun.star.sheet.XLevelsSupplier
438 {
439     private ExampleSettings aSettings;
440     private int nDimension;
441     private ExampleLevels aLevels;
442 
443     public ExampleHierarchy( ExampleSettings aSet, int nDim )
444     {
445         aSettings = aSet;
446         nDimension = nDim;
447     }
448 
449     // XNamed
450 
451     public String getName()
452     {
453         return ExampleSettings.aDimensionNames[ nDimension ];
454     }
455 
456     public void setName( String aName )
457     {
458         // ignored
459     }
460 
461     // XLevelsSupplier
462 
463     public com.sun.star.container.XNameAccess getLevels()
464     {
465         if ( aLevels == null )
466             aLevels = new ExampleLevels( aSettings, nDimension );
467         return aLevels;
468     }
469 }
470 
471 //  implementation of com.sun.star.sheet.DataPilotSourceHierarchies
472 
473 class ExampleHierarchies implements com.sun.star.container.XNameAccess
474 {
475     private ExampleSettings aSettings;
476     private int nDimension;
477     private ExampleHierarchy aHierarchy;
478 
479     public ExampleHierarchies( ExampleSettings aSet, int nDim )
480     {
481         aSettings = aSet;
482         nDimension = nDim;
483     }
484 
485     //  XNameAccess
486 
487     public com.sun.star.uno.Type getElementType()
488     {
489         return new com.sun.star.uno.Type( com.sun.star.container.XNamed.class );
490     }
491 
492     public boolean hasElements()
493     {
494         return true;    // always has elements
495     }
496 
497     public Object getByName( String aName )
498         throws com.sun.star.container.NoSuchElementException
499     {
500         //  there's only one hierarchy with the same name as the dimension
501         if ( aName.equals( ExampleSettings.aDimensionNames[nDimension] ) )
502         {
503             if ( aHierarchy == null )
504                 aHierarchy = new ExampleHierarchy( aSettings, nDimension );
505             return aHierarchy;
506         }
507         throw new com.sun.star.container.NoSuchElementException();
508     }
509 
510     public String[] getElementNames()
511     {
512         String [] aNames = new String[ 1 ];
513         aNames[0] = ExampleSettings.aDimensionNames[nDimension];
514         return aNames;
515     }
516 
517     public boolean hasByName( String aName )
518     {
519         return aName.equals( ExampleSettings.aDimensionNames[nDimension] );
520     }
521 }
522 
523 //  implementation of com.sun.star.sheet.DataPilotSourceDimension
524 
525 class ExampleDimension implements
526                     com.sun.star.container.XNamed,
527                     com.sun.star.sheet.XHierarchiesSupplier,
528                     com.sun.star.util.XCloneable,
529                     com.sun.star.beans.XPropertySet
530 {
531     private ExampleSettings aSettings;
532     private int nDimension;
533     private ExampleHierarchies aHierarchies;
534     private com.sun.star.sheet.DataPilotFieldOrientation eOrientation;
535 
536     public ExampleDimension( ExampleSettings aSet, int nDim )
537     {
538         aSettings = aSet;
539         nDimension = nDim;
540         eOrientation = ( nDim == ExampleSettings.nValueDimension ) ?
541             com.sun.star.sheet.DataPilotFieldOrientation.DATA :
542             com.sun.star.sheet.DataPilotFieldOrientation.HIDDEN;
543     }
544 
545     //  XNamed
546 
547     public String getName()
548     {
549         return ExampleSettings.aDimensionNames[ nDimension ];
550     }
551 
552     public void setName( String aName )
553     {
554         // ignored
555     }
556 
557     //  XHierarchiesSupplier
558 
559     public com.sun.star.container.XNameAccess getHierarchies()
560     {
561         if ( aHierarchies == null )
562             aHierarchies = new ExampleHierarchies( aSettings, nDimension );
563         return aHierarchies;
564     }
565 
566     //  XCloneable
567 
568     public com.sun.star.util.XCloneable createClone()
569     {
570         return null;        // not supported
571     }
572 
573     //  XPropertySet
574 
575     public com.sun.star.beans.XPropertySetInfo getPropertySetInfo()
576     {
577         return new ExamplePropertySetInfo( new com.sun.star.beans.Property[] {
578             new com.sun.star.beans.Property( "Original", -1,
579                 new com.sun.star.uno.Type( com.sun.star.container.XNamed.class),
580                 (short) com.sun.star.beans.PropertyAttribute.READONLY ),
581             new com.sun.star.beans.Property( "IsDataLayoutDimension", -1,
582                 new com.sun.star.uno.Type( Boolean.class),
583                 (short) com.sun.star.beans.PropertyAttribute.READONLY ),
584             new com.sun.star.beans.Property( "Orientation", -1,
585                 new com.sun.star.uno.Type(
586                   com.sun.star.sheet.DataPilotFieldOrientation.class), (short) 0),
587             new com.sun.star.beans.Property( "Position", -1,
588                 new com.sun.star.uno.Type( Integer.class ), (short) 0),
589             new com.sun.star.beans.Property( "Function", -1,
590                 new com.sun.star.uno.Type(com.sun.star.sheet.GeneralFunction.class),
591                                              (short) 0 ),
592             new com.sun.star.beans.Property( "UsedHierarchy", -1,
593                 new com.sun.star.uno.Type( Integer.class ), (short) 0 ),
594             new com.sun.star.beans.Property( "Filter", -1,
595                 new com.sun.star.uno.Type(
596                     com.sun.star.sheet.TableFilterField[].class), (short) 0) });
597     }
598 
599     public void setPropertyValue( String aPropertyName, Object aValue )
600                     throws com.sun.star.beans.UnknownPropertyException
601     {
602         if ( aPropertyName.equals( "Orientation" ) )
603         {
604             com.sun.star.sheet.DataPilotFieldOrientation eNewOrient =
605                         (com.sun.star.sheet.DataPilotFieldOrientation) aValue;
606             if ( nDimension != ExampleSettings.nValueDimension &&
607                  nDimension != ExampleSettings.nDataDimension &&
608                  eNewOrient != com.sun.star.sheet.DataPilotFieldOrientation.DATA )
609             {
610                 // remove from list for old orientation and add for new one
611                 Integer aDimInt = new Integer(nDimension);
612                 if ( eOrientation == com.sun.star.sheet.DataPilotFieldOrientation.COLUMN )
613                     aSettings.aColDimensions.remove( aDimInt );
614                 else if ( eOrientation == com.sun.star.sheet.DataPilotFieldOrientation.ROW )
615                     aSettings.aRowDimensions.remove( aDimInt );
616                 if ( eNewOrient == com.sun.star.sheet.DataPilotFieldOrientation.COLUMN )
617                     aSettings.aColDimensions.add( aDimInt );
618                 else if ( eNewOrient == com.sun.star.sheet.DataPilotFieldOrientation.ROW )
619                     aSettings.aRowDimensions.add( aDimInt );
620 
621                 // change orientation
622                 eOrientation = eNewOrient;
623             }
624         }
625         else if ( aPropertyName.equals( "Position" ) )
626         {
627             int nNewPos = ((Integer) aValue).intValue();
628             Integer aDimInt = new Integer(nDimension);
629             if ( eOrientation == com.sun.star.sheet.DataPilotFieldOrientation.COLUMN )
630             {
631                 aSettings.aColDimensions.remove( aDimInt );
632                 aSettings.aColDimensions.add( nNewPos, aDimInt );
633             }
634             else if ( eOrientation == com.sun.star.sheet.DataPilotFieldOrientation.ROW )
635             {
636                 aSettings.aRowDimensions.remove( aDimInt );
637                 aSettings.aRowDimensions.add( nNewPos, aDimInt );
638             }
639         }
640         else if ( aPropertyName.equals( "Function" ) || aPropertyName.equals( "UsedHierarchy" ) ||
641                   aPropertyName.equals( "Filter" ) )
642         {
643             // ignored
644         }
645         else
646             throw new com.sun.star.beans.UnknownPropertyException();
647     }
648 
649     public Object getPropertyValue( String aPropertyName )
650                     throws com.sun.star.beans.UnknownPropertyException
651     {
652         if ( aPropertyName.equals( "Original" ) )
653             return null;
654         else if ( aPropertyName.equals( "IsDataLayoutDimension" ) )
655             return new Boolean( nDimension == ExampleSettings.nDataDimension );
656         else if ( aPropertyName.equals( "Orientation" ) )
657             return eOrientation;
658         else if ( aPropertyName.equals( "Position" ) )
659         {
660             int nPosition;
661             if ( eOrientation == com.sun.star.sheet.DataPilotFieldOrientation.COLUMN )
662                 nPosition = aSettings.aColDimensions.indexOf( new Integer(nDimension) );
663             else if ( eOrientation == com.sun.star.sheet.DataPilotFieldOrientation.ROW )
664                 nPosition = aSettings.aRowDimensions.indexOf( new Integer(nDimension) );
665             else
666                 nPosition = nDimension;
667             return new Integer( nPosition );
668         }
669         else if ( aPropertyName.equals( "Function" ) )
670             return com.sun.star.sheet.GeneralFunction.SUM;
671         else if ( aPropertyName.equals( "UsedHierarchy" ) )
672             return new Integer(0);
673         else if ( aPropertyName.equals( "Filter" ) )
674             return new com.sun.star.sheet.TableFilterField[0];
675         else
676             throw new com.sun.star.beans.UnknownPropertyException();
677     }
678 
679     public void addPropertyChangeListener(
680         String aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener)
681     {
682     }
683     public void removePropertyChangeListener(
684         String aPropertyName, com.sun.star.beans.XPropertyChangeListener aListener)
685     {
686     }
687     public void addVetoableChangeListener(
688         String PropertyName, com.sun.star.beans.XVetoableChangeListener aListener)
689     {
690     }
691     public void removeVetoableChangeListener(
692         String PropertyName, com.sun.star.beans.XVetoableChangeListener aListener)
693     {
694     }
695 }
696 
697 //  implementation of com.sun.star.sheet.DataPilotSourceDimensions
698 
699 class ExampleDimensions implements com.sun.star.container.XNameAccess
700 {
701     private ExampleSettings aSettings;
702     private ExampleDimension[] aDimensions;
703 
704     public ExampleDimensions( ExampleSettings aSet )
705     {
706         aSettings = aSet;
707     }
708 
709     //  XNameAccess
710 
711     public com.sun.star.uno.Type getElementType()
712     {
713         return new com.sun.star.uno.Type( com.sun.star.container.XNamed.class );
714     }
715 
716     public boolean hasElements()
717     {
718         return true;    // always has elements
719     }
720 
721     public Object getByName( String aName )
722         throws com.sun.star.container.NoSuchElementException
723     {
724         for (int i=0; i<ExampleSettings.nDimensionCount; i++)
725             if ( aName.equals( ExampleSettings.aDimensionNames[i] ) )
726             {
727                 if ( aDimensions == null )
728                     aDimensions = new ExampleDimension[ ExampleSettings.nDimensionCount ];
729                 if ( aDimensions[i] == null )
730                     aDimensions[i] = new ExampleDimension( aSettings, i );
731                 return aDimensions[i];
732             }
733         throw new com.sun.star.container.NoSuchElementException();
734     }
735 
736     public String[] getElementNames()
737     {
738         String [] aNames = new String[ ExampleSettings.nDimensionCount ];
739         for (int i=0; i<ExampleSettings.nDimensionCount; i++)
740             aNames[ i ] = ExampleSettings.aDimensionNames[i];
741         return aNames;
742     }
743 
744     public boolean hasByName( String aName )
745     {
746         for (int i=0; i<ExampleSettings.nDimensionCount; i++)
747             if ( aName.equals( ExampleSettings.aDimensionNames[i] ) )
748                 return true;
749         return false;
750     }
751 }
752 
753 //  outer class for service implementation
754 
755 public class ExampleDataPilotSource
756 {
757     //  implementation of com.sun.star.sheet.DataPilotSource
758 
759     static public class _ExampleDataPilotSource implements
760                         com.sun.star.sheet.XDimensionsSupplier,
761                         com.sun.star.sheet.XDataPilotResults,
762                         com.sun.star.util.XRefreshable,
763                         com.sun.star.beans.XPropertySet,
764                         com.sun.star.lang.XInitialization,
765                         com.sun.star.lang.XServiceInfo
766     {
767         static private final String aServiceName = "com.sun.star.sheet.DataPilotSource";
768         static private final String aImplName =  _ExampleDataPilotSource.class.getName();
769 
770         private ExampleSettings aSettings = new ExampleSettings();
771         private ExampleDimensions aDimensions;
772 
773         public _ExampleDataPilotSource( com.sun.star.lang.XMultiServiceFactory xFactory )
774         {
775         }
776 
777         //  XInitialization
778 
779         public void initialize( Object[] aArguments )
780         {
781             //  If the first argument (Source) is a number between 2 and 10,
782             //  use it as member count, otherwise keep the default value.
783             try
784             {
785 		if ( aArguments.length >= 1 )
786 		{
787 		    String aSource = com.sun.star.uno.AnyConverter.toString(aArguments[0]);
788 		    if ( aSource != null && aSource.length() > 0)
789 		    {
790 			int nValue = Integer.parseInt( aSource );
791 			if ( nValue >= 2 && nValue <= 10 )
792 			    aSettings.nMemberCount = nValue;
793 		    }
794 		}
795 	    }
796 	    catch ( NumberFormatException e )
797 	    {
798 		System.out.println( "Error: caught exception in " +
799 				    "ExampleDataPilotSource.initialize!\nException Message = "
800 				    + e.getMessage());
801 		e.printStackTrace();
802 	    }
803 	    catch ( com.sun.star.lang.IllegalArgumentException e )
804 	    {
805 		System.out.println( "Error: caught exception in " +
806 				    "ExampleDataPilotSource.initialize!\nException Message = "
807 				    + e.getMessage());
808 		e.printStackTrace();
809 	    }
810         }
811 
812         //  XDataPilotResults
813 
814         public com.sun.star.sheet.DataResult[][] getResults()
815         {
816             int[] nDigits = new int[ExampleSettings.nDimensionCount];
817             int nValue = 1;
818             for (int i=0; i<ExampleSettings.nDimensionCount; i++)
819             {
820                 nDigits[i] = nValue;
821                 nValue *= 10;
822             }
823 
824             int nMemberCount = aSettings.nMemberCount;
825             int nRowDimCount = aSettings.aRowDimensions.size();
826             int nColDimCount = aSettings.aColDimensions.size();
827 
828             int nRows = 1;
829             for (int i=0; i<nRowDimCount; i++)
830                 nRows *= nMemberCount;
831             int nColumns = 1;
832             for (int i=0; i<nColDimCount; i++)
833                 nColumns *= nMemberCount;
834 
835             com.sun.star.sheet.DataResult[][] aResults = new com.sun.star.sheet.DataResult[nRows][];
836             for (int nRow=0; nRow<nRows; nRow++)
837             {
838                 int nRowVal = nRow;
839                 int nRowResult = 0;
840                 for (int nRowDim=0; nRowDim<nRowDimCount; nRowDim++)
841                 {
842                     int nDim = ((Integer)aSettings.aRowDimensions.get(nRowDimCount-nRowDim-1)).intValue();
843                     nRowResult += ( nRowVal % nMemberCount ) * nDigits[nDim];
844                     nRowVal /= nMemberCount;
845                 }
846 
847                 aResults[nRow] = new com.sun.star.sheet.DataResult[nColumns];
848                 for (int nCol=0; nCol<nColumns; nCol++)
849                 {
850                     int nColVal = nCol;
851                     int nResult = nRowResult;
852                     for (int nColDim=0; nColDim<nColDimCount; nColDim++)
853                     {
854                         int nDim = ((Integer)aSettings.aColDimensions.get(nColDimCount-nColDim-1)).intValue();
855                         nResult += ( nColVal % nMemberCount ) * nDigits[nDim];
856                         nColVal /= nMemberCount;
857                     }
858 
859                     aResults[nRow][nCol] = new com.sun.star.sheet.DataResult();
860                     aResults[nRow][nCol].Flags = com.sun.star.sheet.DataResultFlags.HASDATA;
861                     aResults[nRow][nCol].Value = nResult;
862                 }
863             }
864             return aResults;
865         }
866 
867         //  XDimensionsSupplier
868 
869         public com.sun.star.container.XNameAccess getDimensions()
870         {
871             if ( aDimensions == null )
872                 aDimensions = new ExampleDimensions( aSettings );
873             return aDimensions;
874         }
875 
876         //  XPropertySet
877 
878         public com.sun.star.beans.XPropertySetInfo getPropertySetInfo()
879         {
880             return new ExamplePropertySetInfo( new com.sun.star.beans.Property[] {
881                 new com.sun.star.beans.Property( "ColumnGrand", -1,
882                        new com.sun.star.uno.Type( Boolean.class ), (short) 0),
883                 new com.sun.star.beans.Property( "RowGrand", -1,
884                        new com.sun.star.uno.Type( Boolean.class ), (short) 0) });
885         }
886 
887         public void setPropertyValue( String aPropertyName, Object aValue )
888             throws com.sun.star.beans.UnknownPropertyException
889         {
890             if ( aPropertyName.equals( "ColumnGrand" ) ||
891                  aPropertyName.equals( "RowGrand" ) )
892             {
893                 // ignored
894             }
895             else
896                 throw new com.sun.star.beans.UnknownPropertyException();
897         }
898 
899         public Object getPropertyValue( String aPropertyName )
900             throws com.sun.star.beans.UnknownPropertyException
901         {
902             if ( aPropertyName.equals( "ColumnGrand" ) ||
903                  aPropertyName.equals( "RowGrand" ) )
904             {
905                 return new Boolean( false );        // always false
906             }
907             else
908                 throw new com.sun.star.beans.UnknownPropertyException();
909         }
910 
911         public void addPropertyChangeListener(
912             String aPropertyName,
913             com.sun.star.beans.XPropertyChangeListener xListener )
914         {
915         }
916         public void removePropertyChangeListener(
917             String aPropertyName,
918             com.sun.star.beans.XPropertyChangeListener aListener )
919         {
920         }
921         public void addVetoableChangeListener(
922             String PropertyName,
923             com.sun.star.beans.XVetoableChangeListener aListener )
924         {
925         }
926         public void removeVetoableChangeListener(
927             String PropertyName,
928             com.sun.star.beans.XVetoableChangeListener aListener )
929         {
930         }
931 
932         //  XRefreshable
933 
934         public void refresh()
935         {
936         }
937         public void addRefreshListener( com.sun.star.util.XRefreshListener l )
938         {
939         }
940         public void removeRefreshListener( com.sun.star.util.XRefreshListener l )
941         {
942         }
943 
944         //  XServiceInfo
945 
946         public String getImplementationName()
947         {
948             return aImplName;
949         }
950 
951         public String[] getSupportedServiceNames()
952         {
953             String [] aSupportedServices = new String[ 1 ];
954             aSupportedServices[ 0 ] = aServiceName;
955             return aSupportedServices;
956         }
957 
958         public boolean supportsService( String aService )
959         {
960             return aService.equals( aServiceName );
961         }
962     }
963 
964     public static com.sun.star.lang.XSingleServiceFactory __getServiceFactory(
965         String implName,
966         com.sun.star.lang.XMultiServiceFactory multiFactory,
967         com.sun.star.registry.XRegistryKey regKey)
968     {
969         com.sun.star.lang.XSingleServiceFactory xSingleServiceFactory = null;
970 
971         if ( implName.equals(_ExampleDataPilotSource.aImplName) )
972             xSingleServiceFactory =
973                 com.sun.star.comp.loader.FactoryHelper.getServiceFactory(
974                     _ExampleDataPilotSource.class,
975                     _ExampleDataPilotSource.aServiceName, multiFactory, regKey);
976 
977         return xSingleServiceFactory;
978     }
979 
980     // This method not longer necessary since OOo 3.4 where the component registration
981     // was changed to passive component registration. For more details see
982     // http://wiki.services.openoffice.org/wiki/Passive_Component_Registration
983 
984 //     public static boolean __writeRegistryServiceInfo(
985 //         com.sun.star.registry.XRegistryKey regKey)
986 //     {
987 //         return com.sun.star.comp.loader.FactoryHelper.writeRegistryServiceInfo(
988 //                     _ExampleDataPilotSource.aImplName,
989 //                     _ExampleDataPilotSource.aServiceName, regKey);
990 //     }
991 }
992 
993