Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / DevelopersGuide / Spreadsheet / ExampleDataPilotSource.java
blobbea38f1ef896316cfdc32a4127f2f8ec547fda84
1 import com.sun.star.sheet.DataPilotFieldFilter;
3 /*************************************************************************
5 * The Contents of this file are made available subject to the terms of
6 * the BSD license.
8 * Copyright 2000, 2010 Oracle and/or its affiliates.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
30 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
32 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
33 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *************************************************************************/
37 // Example DataPilot source component
39 // helper class to hold the settings
41 class ExampleSettings
43 static public final int nDimensionCount = 6;
44 static public final int nValueDimension = 4;
45 static public final int nDataDimension = 5;
46 static public final String [] aDimensionNames = {
47 "ones", "tens", "hundreds", "thousands", "value", "" };
49 static public final String getMemberName( int nMember )
51 return String.valueOf( nMember );
54 public int nMemberCount = 3;
55 public java.util.List<Integer> aColDimensions = new java.util.ArrayList<Integer>();
56 public java.util.List<Integer> aRowDimensions = new java.util.ArrayList<Integer>();
59 // XPropertySetInfo implementation for getPropertySetInfo
61 class ExamplePropertySetInfo implements com.sun.star.beans.XPropertySetInfo
63 private final com.sun.star.beans.Property[] aProperties;
65 public ExamplePropertySetInfo( com.sun.star.beans.Property[] aProps )
67 aProperties = aProps;
70 public com.sun.star.beans.Property[] getProperties()
72 return aProperties;
75 public com.sun.star.beans.Property getPropertyByName( String aName )
76 throws com.sun.star.beans.UnknownPropertyException
78 for ( int i=0; i<aProperties.length; i++ )
79 if ( aProperties[i].Name.equals( aName ) )
80 return aProperties[i];
81 throw new com.sun.star.beans.UnknownPropertyException();
84 public boolean hasPropertyByName( String aName )
86 for ( int i=0; i<aProperties.length; i++ )
87 if ( aProperties[i].Name.equals( aName ) )
88 return true;
89 return false;
93 // implementation of com.sun.star.sheet.DataPilotSourceMember
95 class ExampleMember implements com.sun.star.container.XNamed,
96 com.sun.star.beans.XPropertySet
98 private final int nMember;
100 public ExampleMember( int nMbr )
102 nMember = nMbr;
105 // XNamed
107 public String getName()
109 return ExampleSettings.getMemberName( nMember );
112 public void setName( String aName )
114 // ignored
117 // XPropertySet
119 public com.sun.star.beans.XPropertySetInfo getPropertySetInfo()
121 return new ExamplePropertySetInfo( new com.sun.star.beans.Property[] {
122 new com.sun.star.beans.Property( "IsVisible", -1,
123 new com.sun.star.uno.Type( Boolean.class ), (short) 0),
124 new com.sun.star.beans.Property( "ShowDetails", -1,
125 new com.sun.star.uno.Type( Boolean.class ), (short) 0) });
128 public void setPropertyValue( String aPropertyName, Object aValue )
129 throws com.sun.star.beans.UnknownPropertyException
131 if ( aPropertyName.equals( "IsVisible" ) ||
132 aPropertyName.equals( "ShowDetails" ) )
134 // ignored
136 else
137 throw new com.sun.star.beans.UnknownPropertyException();
140 public Object getPropertyValue( String aPropertyName )
141 throws com.sun.star.beans.UnknownPropertyException
143 if ( aPropertyName.equals( "IsVisible" ) ||
144 aPropertyName.equals( "ShowDetails" ) )
146 return Boolean.TRUE; // always true
148 else
149 throw new com.sun.star.beans.UnknownPropertyException();
152 public void addPropertyChangeListener(
153 String aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener)
156 public void removePropertyChangeListener(
157 String aPropertyName, com.sun.star.beans.XPropertyChangeListener aListener)
160 public void addVetoableChangeListener(
161 String PropertyName, com.sun.star.beans.XVetoableChangeListener aListener)
164 public void removeVetoableChangeListener(
165 String PropertyName, com.sun.star.beans.XVetoableChangeListener aListener)
170 // implementation of com.sun.star.sheet.DataPilotSourceMembers
172 class ExampleMembers implements com.sun.star.container.XNameAccess
174 private final ExampleSettings aSettings;
175 private ExampleMember[] aMembers;
177 public ExampleMembers( ExampleSettings aSet )
179 aSettings = aSet;
180 aMembers = new ExampleMember[ aSettings.nMemberCount ];
183 // XNameAccess
185 public com.sun.star.uno.Type getElementType()
187 return new com.sun.star.uno.Type( com.sun.star.container.XNamed.class );
190 public boolean hasElements()
192 return true; // always has elements
195 public Object getByName( String aName )
196 throws com.sun.star.container.NoSuchElementException
198 int nCount = aSettings.nMemberCount;
199 for ( int i=0; i<nCount; i++ )
200 if ( aName.equals( ExampleSettings.getMemberName( i ) ) )
202 if ( aMembers[i] == null )
203 aMembers[i] = new ExampleMember( i );
204 return aMembers[i];
206 throw new com.sun.star.container.NoSuchElementException();
209 public String[] getElementNames()
211 int nCount = aSettings.nMemberCount;
212 String [] aNames = new String[ nCount ];
213 for ( int i=0; i<nCount; i++ )
214 aNames[i] = ExampleSettings.getMemberName( i );
215 return aNames;
218 public boolean hasByName( String aName )
220 int nCount = aSettings.nMemberCount;
221 for ( int i=0; i<nCount; i++ )
222 if ( aName.equals( ExampleSettings.getMemberName( i ) ) )
223 return true;
224 return false;
228 // implementation of com.sun.star.sheet.DataPilotSourceLevel
230 class ExampleLevel implements
231 com.sun.star.container.XNamed,
232 com.sun.star.sheet.XMembersSupplier,
233 com.sun.star.sheet.XDataPilotMemberResults,
234 com.sun.star.beans.XPropertySet
236 private final ExampleSettings aSettings;
237 private final int nDimension;
238 private ExampleMembers aMembers;
240 public ExampleLevel( ExampleSettings aSet, int nDim )
242 aSettings = aSet;
243 nDimension = nDim;
246 // XNamed
248 public String getName()
250 return ExampleSettings.aDimensionNames[ nDimension ];
253 public void setName( String aName )
255 // ignored
258 // XMembersSupplier
260 public com.sun.star.container.XNameAccess getMembers()
262 if ( aMembers == null )
263 aMembers = new ExampleMembers( aSettings );
264 return aMembers;
267 // XDataPilotMemberResults
269 public com.sun.star.sheet.MemberResult[] getResults()
271 int nDimensions = 0;
272 int nPosition = aSettings.aColDimensions.indexOf( Integer.valueOf(nDimension));
273 if ( nPosition >= 0 )
274 nDimensions = aSettings.aColDimensions.size();
275 else
277 nPosition = aSettings.aRowDimensions.indexOf( Integer.valueOf(nDimension));
278 if ( nPosition >= 0 )
279 nDimensions = aSettings.aRowDimensions.size();
282 if ( nPosition < 0 )
283 return new com.sun.star.sheet.MemberResult[0];
285 int nMembers = aSettings.nMemberCount;
286 int nRepeat = 1;
287 int nFill = 1;
288 for ( int i=0; i<nDimensions; i++ )
290 if ( i < nPosition )
291 nRepeat *= nMembers;
292 else if ( i > nPosition )
293 nFill *= nMembers;
295 int nSize = nRepeat * nMembers * nFill;
297 com.sun.star.sheet.MemberResult[] aResults =
298 new com.sun.star.sheet.MemberResult[nSize];
299 int nResultPos = 0;
300 for (int nOuter=0; nOuter<nRepeat; nOuter++)
302 for (int nMember=0; nMember<nMembers; nMember++)
304 aResults[nResultPos] = new com.sun.star.sheet.MemberResult();
305 aResults[nResultPos].Name = ExampleSettings.getMemberName(nMember);
306 aResults[nResultPos].Caption = aResults[nResultPos].Name;
307 aResults[nResultPos].Flags =
308 com.sun.star.sheet.MemberResultFlags.HASMEMBER;
309 ++nResultPos;
311 for (int nInner=1; nInner<nFill; nInner++)
313 aResults[nResultPos] = new com.sun.star.sheet.MemberResult();
314 aResults[nResultPos].Flags =
315 com.sun.star.sheet.MemberResultFlags.CONTINUE;
316 ++nResultPos;
320 return aResults;
323 // XPropertySet
325 public com.sun.star.beans.XPropertySetInfo getPropertySetInfo()
327 return new ExamplePropertySetInfo( new com.sun.star.beans.Property[] {
328 new com.sun.star.beans.Property( "SubTotals", -1,
329 new com.sun.star.uno.Type(
330 com.sun.star.sheet.GeneralFunction[].class ),
331 (short) 0 ),
332 new com.sun.star.beans.Property( "ShowEmpty", -1,
333 new com.sun.star.uno.Type( Boolean.class ),
334 (short) 0 ) } );
337 public void setPropertyValue( String aPropertyName, Object aValue )
338 throws com.sun.star.beans.UnknownPropertyException
340 if ( aPropertyName.equals( "SubTotals" ) ||
341 aPropertyName.equals( "ShowEmpty" ) )
343 // ignored
345 else
346 throw new com.sun.star.beans.UnknownPropertyException();
349 public Object getPropertyValue( String aPropertyName )
350 throws com.sun.star.beans.UnknownPropertyException
352 if ( aPropertyName.equals( "SubTotals" ) )
353 return new com.sun.star.sheet.GeneralFunction[0];
354 else if ( aPropertyName.equals( "ShowEmpty" ) )
355 return Boolean.TRUE;
356 else
357 throw new com.sun.star.beans.UnknownPropertyException();
360 public void addPropertyChangeListener(
361 String aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener)
364 public void removePropertyChangeListener(
365 String aPropertyName, com.sun.star.beans.XPropertyChangeListener aListener)
368 public void addVetoableChangeListener(
369 String PropertyName, com.sun.star.beans.XVetoableChangeListener aListener)
372 public void removeVetoableChangeListener(
373 String PropertyName, com.sun.star.beans.XVetoableChangeListener aListener)
378 // implementation of com.sun.star.sheet.DataPilotSourceLevels
380 class ExampleLevels implements com.sun.star.container.XNameAccess
382 private final ExampleSettings aSettings;
383 private final int nDimension;
384 private ExampleLevel aLevel;
386 public ExampleLevels( ExampleSettings aSet, int nDim )
388 aSettings = aSet;
389 nDimension = nDim;
392 // XNameAccess
394 public com.sun.star.uno.Type getElementType()
396 return new com.sun.star.uno.Type( com.sun.star.container.XNamed.class );
399 public boolean hasElements()
401 return true; // always has elements
404 public Object getByName( String aName )
405 throws com.sun.star.container.NoSuchElementException
407 // there's only one level with the same name as the dimension / hierarchy
408 if ( aName.equals( ExampleSettings.aDimensionNames[nDimension] ) )
410 if ( aLevel == null )
411 aLevel = new ExampleLevel( aSettings, nDimension );
412 return aLevel;
414 throw new com.sun.star.container.NoSuchElementException();
417 public String[] getElementNames()
419 String [] aNames = new String[ 1 ];
420 aNames[0] = ExampleSettings.aDimensionNames[nDimension];
421 return aNames;
424 public boolean hasByName( String aName )
426 return aName.equals( ExampleSettings.aDimensionNames[nDimension] );
430 // implementation of com.sun.star.sheet.DataPilotSourceHierarchy
432 class ExampleHierarchy implements com.sun.star.container.XNamed,
433 com.sun.star.sheet.XLevelsSupplier
435 private final ExampleSettings aSettings;
436 private final int nDimension;
437 private ExampleLevels aLevels;
439 public ExampleHierarchy( ExampleSettings aSet, int nDim )
441 aSettings = aSet;
442 nDimension = nDim;
445 // XNamed
447 public String getName()
449 return ExampleSettings.aDimensionNames[ nDimension ];
452 public void setName( String aName )
454 // ignored
457 // XLevelsSupplier
459 public com.sun.star.container.XNameAccess getLevels()
461 if ( aLevels == null )
462 aLevels = new ExampleLevels( aSettings, nDimension );
463 return aLevels;
467 // implementation of com.sun.star.sheet.DataPilotSourceHierarchies
469 class ExampleHierarchies implements com.sun.star.container.XNameAccess
471 private final ExampleSettings aSettings;
472 private final int nDimension;
473 private ExampleHierarchy aHierarchy;
475 public ExampleHierarchies( ExampleSettings aSet, int nDim )
477 aSettings = aSet;
478 nDimension = nDim;
481 // XNameAccess
483 public com.sun.star.uno.Type getElementType()
485 return new com.sun.star.uno.Type( com.sun.star.container.XNamed.class );
488 public boolean hasElements()
490 return true; // always has elements
493 public Object getByName( String aName )
494 throws com.sun.star.container.NoSuchElementException
496 // there's only one hierarchy with the same name as the dimension
497 if ( aName.equals( ExampleSettings.aDimensionNames[nDimension] ) )
499 if ( aHierarchy == null )
500 aHierarchy = new ExampleHierarchy( aSettings, nDimension );
501 return aHierarchy;
503 throw new com.sun.star.container.NoSuchElementException();
506 public String[] getElementNames()
508 String [] aNames = new String[ 1 ];
509 aNames[0] = ExampleSettings.aDimensionNames[nDimension];
510 return aNames;
513 public boolean hasByName( String aName )
515 return aName.equals( ExampleSettings.aDimensionNames[nDimension] );
519 // implementation of com.sun.star.sheet.DataPilotSourceDimension
521 class ExampleDimension implements
522 com.sun.star.container.XNamed,
523 com.sun.star.sheet.XHierarchiesSupplier,
524 com.sun.star.util.XCloneable,
525 com.sun.star.beans.XPropertySet
527 private final ExampleSettings aSettings;
528 private final int nDimension;
529 private ExampleHierarchies aHierarchies;
530 private com.sun.star.sheet.DataPilotFieldOrientation eOrientation;
532 public ExampleDimension( ExampleSettings aSet, int nDim )
534 aSettings = aSet;
535 nDimension = nDim;
536 eOrientation = ( nDim == ExampleSettings.nValueDimension ) ?
537 com.sun.star.sheet.DataPilotFieldOrientation.DATA :
538 com.sun.star.sheet.DataPilotFieldOrientation.HIDDEN;
541 // XNamed
543 public String getName()
545 return ExampleSettings.aDimensionNames[ nDimension ];
548 public void setName( String aName )
550 // ignored
553 // XHierarchiesSupplier
555 public com.sun.star.container.XNameAccess getHierarchies()
557 if ( aHierarchies == null )
558 aHierarchies = new ExampleHierarchies( aSettings, nDimension );
559 return aHierarchies;
562 // XCloneable
564 public com.sun.star.util.XCloneable createClone()
566 return null; // not supported
569 // XPropertySet
571 public com.sun.star.beans.XPropertySetInfo getPropertySetInfo()
573 return new ExamplePropertySetInfo( new com.sun.star.beans.Property[] {
574 new com.sun.star.beans.Property( "Original", -1,
575 new com.sun.star.uno.Type( com.sun.star.container.XNamed.class),
576 com.sun.star.beans.PropertyAttribute.READONLY ),
577 new com.sun.star.beans.Property( "IsDataLayoutDimension", -1,
578 new com.sun.star.uno.Type( Boolean.class),
579 com.sun.star.beans.PropertyAttribute.READONLY ),
580 new com.sun.star.beans.Property( "Orientation", -1,
581 new com.sun.star.uno.Type(
582 com.sun.star.sheet.DataPilotFieldOrientation.class), (short) 0),
583 new com.sun.star.beans.Property( "Position", -1,
584 new com.sun.star.uno.Type( Integer.class ), (short) 0),
585 new com.sun.star.beans.Property( "Function", -1,
586 new com.sun.star.uno.Type(com.sun.star.sheet.GeneralFunction.class),
587 (short) 0 ),
588 new com.sun.star.beans.Property( "UsedHierarchy", -1,
589 new com.sun.star.uno.Type( Integer.class ), (short) 0 ),
590 new com.sun.star.beans.Property( "Filter", -1,
591 new com.sun.star.uno.Type(
592 com.sun.star.sheet.TableFilterField[].class), (short) 0) });
595 public void setPropertyValue( String aPropertyName, Object aValue )
596 throws com.sun.star.beans.UnknownPropertyException
598 if ( aPropertyName.equals( "Orientation" ) )
600 com.sun.star.sheet.DataPilotFieldOrientation eNewOrient =
601 (com.sun.star.sheet.DataPilotFieldOrientation) aValue;
602 if ( nDimension != ExampleSettings.nValueDimension &&
603 nDimension != ExampleSettings.nDataDimension &&
604 eNewOrient != com.sun.star.sheet.DataPilotFieldOrientation.DATA )
606 // remove from list for old orientation and add for new one
607 Integer aDimInt = Integer.valueOf(nDimension);
608 if ( eOrientation == com.sun.star.sheet.DataPilotFieldOrientation.COLUMN )
609 aSettings.aColDimensions.remove( aDimInt );
610 else if ( eOrientation == com.sun.star.sheet.DataPilotFieldOrientation.ROW )
611 aSettings.aRowDimensions.remove( aDimInt );
612 if ( eNewOrient == com.sun.star.sheet.DataPilotFieldOrientation.COLUMN )
613 aSettings.aColDimensions.add( aDimInt );
614 else if ( eNewOrient == com.sun.star.sheet.DataPilotFieldOrientation.ROW )
615 aSettings.aRowDimensions.add( aDimInt );
617 // change orientation
618 eOrientation = eNewOrient;
621 else if ( aPropertyName.equals( "Position" ) )
623 int nNewPos = ((Integer) aValue).intValue();
624 Integer aDimInt = Integer.valueOf(nDimension);
625 if ( eOrientation == com.sun.star.sheet.DataPilotFieldOrientation.COLUMN )
627 aSettings.aColDimensions.remove( aDimInt );
628 aSettings.aColDimensions.add( nNewPos, aDimInt );
630 else if ( eOrientation == com.sun.star.sheet.DataPilotFieldOrientation.ROW )
632 aSettings.aRowDimensions.remove( aDimInt );
633 aSettings.aRowDimensions.add( nNewPos, aDimInt );
636 else if ( aPropertyName.equals( "Function" ) || aPropertyName.equals( "UsedHierarchy" ) ||
637 aPropertyName.equals( "Filter" ) )
639 // ignored
641 else
642 throw new com.sun.star.beans.UnknownPropertyException();
645 public Object getPropertyValue( String aPropertyName )
646 throws com.sun.star.beans.UnknownPropertyException
648 if ( aPropertyName.equals( "Original" ) )
649 return null;
650 else if ( aPropertyName.equals( "IsDataLayoutDimension" ) )
651 return Boolean.valueOf( nDimension == ExampleSettings.nDataDimension );
652 else if ( aPropertyName.equals( "Orientation" ) )
653 return eOrientation;
654 else if ( aPropertyName.equals( "Position" ) )
656 int nPosition;
657 if ( eOrientation == com.sun.star.sheet.DataPilotFieldOrientation.COLUMN )
658 nPosition = aSettings.aColDimensions.indexOf( Integer.valueOf(nDimension) );
659 else if ( eOrientation == com.sun.star.sheet.DataPilotFieldOrientation.ROW )
660 nPosition = aSettings.aRowDimensions.indexOf( Integer.valueOf(nDimension) );
661 else
662 nPosition = nDimension;
663 return Integer.valueOf( nPosition );
665 else if ( aPropertyName.equals( "Function" ) )
666 return com.sun.star.sheet.GeneralFunction.SUM;
667 else if ( aPropertyName.equals( "UsedHierarchy" ) )
668 return Integer.valueOf(0);
669 else if ( aPropertyName.equals( "Filter" ) )
670 return new com.sun.star.sheet.TableFilterField[0];
671 else
672 throw new com.sun.star.beans.UnknownPropertyException();
675 public void addPropertyChangeListener(
676 String aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener)
679 public void removePropertyChangeListener(
680 String aPropertyName, com.sun.star.beans.XPropertyChangeListener aListener)
683 public void addVetoableChangeListener(
684 String PropertyName, com.sun.star.beans.XVetoableChangeListener aListener)
687 public void removeVetoableChangeListener(
688 String PropertyName, com.sun.star.beans.XVetoableChangeListener aListener)
693 // implementation of com.sun.star.sheet.DataPilotSourceDimensions
695 class ExampleDimensions implements com.sun.star.container.XNameAccess
697 private final ExampleSettings aSettings;
698 private ExampleDimension[] aDimensions;
700 public ExampleDimensions( ExampleSettings aSet )
702 aSettings = aSet;
705 // XNameAccess
707 public com.sun.star.uno.Type getElementType()
709 return new com.sun.star.uno.Type( com.sun.star.container.XNamed.class );
712 public boolean hasElements()
714 return true; // always has elements
717 public Object getByName( String aName )
718 throws com.sun.star.container.NoSuchElementException
720 for (int i=0; i<ExampleSettings.nDimensionCount; i++)
721 if ( aName.equals( ExampleSettings.aDimensionNames[i] ) )
723 if ( aDimensions == null )
724 aDimensions = new ExampleDimension[ ExampleSettings.nDimensionCount ];
725 if ( aDimensions[i] == null )
726 aDimensions[i] = new ExampleDimension( aSettings, i );
727 return aDimensions[i];
729 throw new com.sun.star.container.NoSuchElementException();
732 public String[] getElementNames()
734 String [] aNames = new String[ ExampleSettings.nDimensionCount ];
735 for (int i=0; i<ExampleSettings.nDimensionCount; i++)
736 aNames[ i ] = ExampleSettings.aDimensionNames[i];
737 return aNames;
740 public boolean hasByName( String aName )
742 for (int i=0; i<ExampleSettings.nDimensionCount; i++)
743 if ( aName.equals( ExampleSettings.aDimensionNames[i] ) )
744 return true;
745 return false;
749 // outer class for service implementation
751 public class ExampleDataPilotSource
753 // implementation of com.sun.star.sheet.DataPilotSource
755 static public class _ExampleDataPilotSource implements
756 com.sun.star.sheet.XDimensionsSupplier,
757 com.sun.star.sheet.XDataPilotResults,
758 com.sun.star.util.XRefreshable,
759 com.sun.star.beans.XPropertySet,
760 com.sun.star.lang.XInitialization,
761 com.sun.star.lang.XServiceInfo
763 static private final String aServiceName = "com.sun.star.sheet.DataPilotSource";
764 static private final String aImplName = _ExampleDataPilotSource.class.getName();
766 private final ExampleSettings aSettings = new ExampleSettings();
767 private ExampleDimensions aDimensions;
769 public _ExampleDataPilotSource( com.sun.star.lang.XMultiServiceFactory xFactory )
773 // XInitialization
775 public void initialize( Object[] aArguments )
777 // If the first argument (Source) is a number between 2 and 10,
778 // use it as member count, otherwise keep the default value.
781 if ( aArguments.length >= 1 )
783 String aSource = com.sun.star.uno.AnyConverter.toString(aArguments[0]);
784 if ( aSource != null && aSource.length() > 0)
786 int nValue = Integer.parseInt( aSource );
787 if ( nValue >= 2 && nValue <= 10 )
788 aSettings.nMemberCount = nValue;
792 catch ( NumberFormatException e )
794 System.out.println( "Error: caught exception in " +
795 "ExampleDataPilotSource.initialize!\nException Message = "
796 + e.getMessage());
797 e.printStackTrace();
799 catch ( com.sun.star.lang.IllegalArgumentException e )
801 System.out.println( "Error: caught exception in " +
802 "ExampleDataPilotSource.initialize!\nException Message = "
803 + e.getMessage());
804 e.printStackTrace();
808 // XDataPilotResults
810 public com.sun.star.sheet.DataResult[][] getResults()
812 int[] nDigits = new int[ExampleSettings.nDimensionCount];
813 int nValue = 1;
814 for (int i=0; i<ExampleSettings.nDimensionCount; i++)
816 nDigits[i] = nValue;
817 nValue *= 10;
820 int nMemberCount = aSettings.nMemberCount;
821 int nRowDimCount = aSettings.aRowDimensions.size();
822 int nColDimCount = aSettings.aColDimensions.size();
824 int nRows = 1;
825 for (int i=0; i<nRowDimCount; i++)
826 nRows *= nMemberCount;
827 int nColumns = 1;
828 for (int i=0; i<nColDimCount; i++)
829 nColumns *= nMemberCount;
831 com.sun.star.sheet.DataResult[][] aResults = new com.sun.star.sheet.DataResult[nRows][];
832 for (int nRow=0; nRow<nRows; nRow++)
834 int nRowVal = nRow;
835 int nRowResult = 0;
836 for (int nRowDim=0; nRowDim<nRowDimCount; nRowDim++)
838 int nDim = aSettings.aRowDimensions.get(nRowDimCount-nRowDim-1).intValue();
839 nRowResult += ( nRowVal % nMemberCount ) * nDigits[nDim];
840 nRowVal /= nMemberCount;
843 aResults[nRow] = new com.sun.star.sheet.DataResult[nColumns];
844 for (int nCol=0; nCol<nColumns; nCol++)
846 int nColVal = nCol;
847 int nResult = nRowResult;
848 for (int nColDim=0; nColDim<nColDimCount; nColDim++)
850 int nDim = aSettings.aColDimensions.get(nColDimCount-nColDim-1).intValue();
851 nResult += ( nColVal % nMemberCount ) * nDigits[nDim];
852 nColVal /= nMemberCount;
855 aResults[nRow][nCol] = new com.sun.star.sheet.DataResult();
856 aResults[nRow][nCol].Flags = com.sun.star.sheet.DataResultFlags.HASDATA;
857 aResults[nRow][nCol].Value = nResult;
860 return aResults;
863 public double[] getFilteredResults(DataPilotFieldFilter[] aFilters) {
864 // FIXME
865 return new double[0];
868 // XDimensionsSupplier
870 public com.sun.star.container.XNameAccess getDimensions()
872 if ( aDimensions == null )
873 aDimensions = new ExampleDimensions( aSettings );
874 return aDimensions;
877 // XPropertySet
879 public com.sun.star.beans.XPropertySetInfo getPropertySetInfo()
881 return new ExamplePropertySetInfo( new com.sun.star.beans.Property[] {
882 new com.sun.star.beans.Property( "ColumnGrand", -1,
883 new com.sun.star.uno.Type( Boolean.class ), (short) 0),
884 new com.sun.star.beans.Property( "RowGrand", -1,
885 new com.sun.star.uno.Type( Boolean.class ), (short) 0) });
888 public void setPropertyValue( String aPropertyName, Object aValue )
889 throws com.sun.star.beans.UnknownPropertyException
891 if ( aPropertyName.equals( "ColumnGrand" ) ||
892 aPropertyName.equals( "RowGrand" ) )
894 // ignored
896 else
897 throw new com.sun.star.beans.UnknownPropertyException();
900 public Object getPropertyValue( String aPropertyName )
901 throws com.sun.star.beans.UnknownPropertyException
903 if ( aPropertyName.equals( "ColumnGrand" ) ||
904 aPropertyName.equals( "RowGrand" ) )
906 return Boolean.FALSE; // always false
908 else
909 throw new com.sun.star.beans.UnknownPropertyException();
912 public void addPropertyChangeListener(
913 String aPropertyName,
914 com.sun.star.beans.XPropertyChangeListener xListener )
917 public void removePropertyChangeListener(
918 String aPropertyName,
919 com.sun.star.beans.XPropertyChangeListener aListener )
922 public void addVetoableChangeListener(
923 String PropertyName,
924 com.sun.star.beans.XVetoableChangeListener aListener )
927 public void removeVetoableChangeListener(
928 String PropertyName,
929 com.sun.star.beans.XVetoableChangeListener aListener )
933 // XRefreshable
935 public void refresh()
938 public void addRefreshListener( com.sun.star.util.XRefreshListener l )
941 public void removeRefreshListener( com.sun.star.util.XRefreshListener l )
945 // XServiceInfo
947 public String getImplementationName()
949 return aImplName;
952 public String[] getSupportedServiceNames()
954 String [] aSupportedServices = new String[ 1 ];
955 aSupportedServices[ 0 ] = aServiceName;
956 return aSupportedServices;
959 public boolean supportsService( String aService )
961 return aService.equals( aServiceName );
965 public static com.sun.star.lang.XSingleServiceFactory __getServiceFactory(
966 String implName,
967 com.sun.star.lang.XMultiServiceFactory multiFactory,
968 com.sun.star.registry.XRegistryKey regKey)
970 com.sun.star.lang.XSingleServiceFactory xSingleServiceFactory = null;
972 if ( implName.equals(_ExampleDataPilotSource.aImplName) )
973 xSingleServiceFactory =
974 com.sun.star.comp.loader.FactoryHelper.getServiceFactory(
975 _ExampleDataPilotSource.class,
976 _ExampleDataPilotSource.aServiceName, multiFactory, regKey);
978 return xSingleServiceFactory;