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