1 /*************************************************************************
3 * $RCSfile: JavaSampleChartAddIn.java,v $
7 * last change: $Author: hr $ $Date: 2003-06-30 15:10:14 $
9 * The Contents of this file are made available subject to the terms of
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
42 import com
.sun
.star
.uno
.XInterface
;
43 import com
.sun
.star
.uno
.UnoRuntime
;
44 import com
.sun
.star
.uno
.Type
;
45 import com
.sun
.star
.container
.XIndexAccess
;
46 import com
.sun
.star
.lib
.uno
.helper
.WeakBase
;
49 import com
.sun
.star
.lang
.XMultiServiceFactory
;
50 import com
.sun
.star
.lang
.XSingleServiceFactory
;
53 import com
.sun
.star
.drawing
.*;
54 import com
.sun
.star
.awt
.Point
;
55 import com
.sun
.star
.awt
.Rectangle
;
56 import com
.sun
.star
.awt
.Size
;
59 import com
.sun
.star
.chart
.*;
62 import com
.sun
.star
.beans
.*;
65 import com
.sun
.star
.lang
.XInitialization
;
66 import com
.sun
.star
.util
.XRefreshable
;
67 import com
.sun
.star
.lang
.XServiceName
;
68 import com
.sun
.star
.lang
.XServiceInfo
;
69 import com
.sun
.star
.lang
.XTypeProvider
;
72 import com
.sun
.star
.uno
.Exception
;
73 import com
.sun
.star
.uno
.RuntimeException
;
75 import javax
.swing
.JOptionPane
;
77 public class JavaSampleChartAddIn
extends WeakBase
implements
84 public JavaSampleChartAddIn()
87 // __________ interface methods __________
90 public void initialize( Object
[] aArguments
)
91 throws Exception
, RuntimeException
93 if( aArguments
.length
> 0 )
95 maChartDocument
= (XChartDocument
) UnoRuntime
.queryInterface(
96 XChartDocument
.class, aArguments
[ 0 ]);
98 XPropertySet aDocProp
= (XPropertySet
) UnoRuntime
.queryInterface(
99 XPropertySet
.class, maChartDocument
);
100 if( aDocProp
!= null )
102 // set base diagram which will be extended in refresh()
103 aDocProp
.setPropertyValue( "BaseDiagram", "com.sun.star.chart.XYDiagram" );
107 XDrawPageSupplier aPageSupp
= (XDrawPageSupplier
) UnoRuntime
.queryInterface(
108 XDrawPageSupplier
.class, maChartDocument
);
109 if( aPageSupp
!= null )
110 maDrawPage
= (XDrawPage
) UnoRuntime
.queryInterface(
111 XDrawPage
.class, aPageSupp
.getDrawPage() );
113 // get a factory for creating shapes
114 maShapeFactory
= (XMultiServiceFactory
) UnoRuntime
.queryInterface(
115 XMultiServiceFactory
.class, maChartDocument
);
120 public void refresh() throws RuntimeException
122 // recycle shapes in first call, if document was loaded
123 if( maBottomLine
== null ||
126 // try to recycle loaded shapes
127 XPropertySet aDocProp
= (XPropertySet
) UnoRuntime
.queryInterface(
128 XPropertySet
.class, maChartDocument
);
129 if( aDocProp
!= null )
133 XIndexAccess aShapesIA
= (XIndexAccess
) UnoRuntime
.queryInterface(
134 XIndexAccess
.class, aDocProp
.getPropertyValue( "AdditionalShapes" ));
135 if( aShapesIA
!= null &&
136 aShapesIA
.getCount() > 0 )
140 for( int i
= aShapesIA
.getCount() - 1; i
>= 0; --i
)
142 aShape
= (XShape
) UnoRuntime
.queryInterface(
143 XShape
.class, aShapesIA
.getByIndex( i
));
146 XPropertySet aProp
= (XPropertySet
) UnoRuntime
.queryInterface(
147 XPropertySet
.class, aShape
);
148 aName
= (String
) aProp
.getPropertyValue( "Name" );
150 if( aName
.equals( "top" ))
154 else if( aName
.equals( "bottom" ))
156 maBottomLine
= aShape
;
162 catch( Exception ex
)
164 JOptionPane
.showMessageDialog( null, ex
, "Exception caught", JOptionPane
.WARNING_MESSAGE
);
169 // create top line if it does not yet exist
172 if( maTopLine
== null )
174 maTopLine
= (XShape
) UnoRuntime
.queryInterface(
175 XShape
.class, maShapeFactory
.createInstance( "com.sun.star.drawing.LineShape" ));
176 maDrawPage
.add( maTopLine
);
178 // make line red and thicker
179 XPropertySet aShapeProp
= (XPropertySet
) UnoRuntime
.queryInterface(
180 XPropertySet
.class, maTopLine
);
182 aShapeProp
.setPropertyValue( "LineColor", new Integer( 0xe01010 ));
183 aShapeProp
.setPropertyValue( "LineWidth", new Integer( 50 ));
184 aShapeProp
.setPropertyValue( "Name", "top" );
187 catch( Exception ex
)
189 JOptionPane
.showMessageDialog( null, ex
, "Exception caught", JOptionPane
.WARNING_MESSAGE
);
192 // create bottom line if it does not yet exist
195 if( maBottomLine
== null )
197 maBottomLine
= (XShape
) UnoRuntime
.queryInterface(
198 XShape
.class, maShapeFactory
.createInstance( "com.sun.star.drawing.LineShape" ));
199 maDrawPage
.add( maBottomLine
);
201 // make line green and thicker
202 XPropertySet aShapeProp
= (XPropertySet
) UnoRuntime
.queryInterface(
203 XPropertySet
.class, maBottomLine
);
205 aShapeProp
.setPropertyValue( "LineColor", new Integer( 0x10e010 ));
206 aShapeProp
.setPropertyValue( "LineWidth", new Integer( 50 ));
207 aShapeProp
.setPropertyValue( "Name", "bottom" );
210 catch( Exception ex
)
212 JOptionPane
.showMessageDialog( null, ex
, "Exception caught", JOptionPane
.WARNING_MESSAGE
);
215 if( maTopLine
== null ||
216 maBottomLine
== null )
218 JOptionPane
.showMessageDialog( null, "One of the lines is still null", "Assertion", JOptionPane
.WARNING_MESSAGE
);
226 XChartDataArray aDataArray
= (XChartDataArray
) UnoRuntime
.queryInterface(
227 XChartDataArray
.class, maChartDocument
.getData());
228 double aData
[][] = aDataArray
.getData();
231 XDiagram aDiagram
= maChartDocument
.getDiagram();
232 XShape aXAxis
= (XShape
) UnoRuntime
.queryInterface(
233 XShape
.class, ((XAxisXSupplier
) UnoRuntime
.queryInterface(
234 XAxisXSupplier
.class, aDiagram
)).getXAxis() );
235 XShape aYAxis
= (XShape
) UnoRuntime
.queryInterface(
236 XShape
.class, ((XAxisYSupplier
) UnoRuntime
.queryInterface(
237 XAxisYSupplier
.class, aDiagram
)).getYAxis() );
239 // calculate points for hull
240 final int nLength
= aData
.length
;
244 Point aMaxPtSeq
[][] = new Point
[ 1 ][];
245 aMaxPtSeq
[ 0 ] = new Point
[ nLength
];
246 Point aMinPtSeq
[][] = new Point
[ 1 ][];
247 aMinPtSeq
[ 0 ] = new Point
[ nLength
];
249 for( i
= 0; i
< nLength
; i
++ )
251 fMin
= fMax
= aData
[ i
][ 1 ];
252 for( j
= 1; j
< aData
[ i
].length
; j
++ )
254 if( aData
[ i
][ j
] > fMax
)
255 fMax
= aData
[ i
][ j
];
256 else if( aData
[ i
][ j
] < fMin
)
257 fMin
= aData
[ i
][ j
];
259 aMaxPtSeq
[ 0 ][ i
] = new Point( getAxisPosition( aXAxis
, aData
[ i
][ 0 ], false ),
260 getAxisPosition( aYAxis
, fMax
, true ));
261 aMinPtSeq
[ 0 ][ i
] = new Point( getAxisPosition( aXAxis
, aData
[ i
][ 0 ], false ),
262 getAxisPosition( aYAxis
, fMin
, true ));
265 // apply point sequences to lines
268 XPropertySet aShapeProp
= (XPropertySet
) UnoRuntime
.queryInterface(
269 XPropertySet
.class, maTopLine
);
270 aShapeProp
.setPropertyValue( "PolyPolygon", aMaxPtSeq
);
272 aShapeProp
= (XPropertySet
) UnoRuntime
.queryInterface(
273 XPropertySet
.class, maBottomLine
);
274 aShapeProp
.setPropertyValue( "PolyPolygon", aMinPtSeq
);
276 catch( Exception ex
)
278 JOptionPane
.showMessageDialog( null, ex
, "Exception caught", JOptionPane
.WARNING_MESSAGE
);
282 public void addRefreshListener( com
.sun
.star
.util
.XRefreshListener aListener
)
283 throws RuntimeException
285 // we don't want this but we have to implement the interface
288 public void removeRefreshListener( com
.sun
.star
.util
.XRefreshListener aListener
)
289 throws RuntimeException
291 // we don't want this but we have to implement the interface
296 public String
getServiceName() throws RuntimeException
298 return new String( smServiceName
);
302 public boolean supportsService( String aServiceName
)
304 String
[] aServices
= getSupportedServiceNames_Static();
305 int i
, nLength
= aServices
.length
;
306 boolean bResult
= false;
308 for( i
= 0; !bResult
&& i
< nLength
; ++i
)
309 bResult
= aServiceName
.equals( aServices
[ i
] );
314 public String
getImplementationName()
316 return( JavaSampleChartAddIn
.class.getName() );
319 public String
[] getSupportedServiceNames()
321 return getSupportedServiceNames_Static();
325 public String
getDiagramType() throws RuntimeException
327 return new String( smServiceName
);
330 public XPropertySet
getDataRowProperties( int nRow
)
331 throws com
.sun
.star
.lang
.IndexOutOfBoundsException
, RuntimeException
333 return maChartDocument
.getDiagram().getDataRowProperties( nRow
);
336 public XPropertySet
getDataPointProperties( int nCol
, int nRow
)
337 throws com
.sun
.star
.lang
.IndexOutOfBoundsException
, RuntimeException
339 return maChartDocument
.getDiagram().getDataPointProperties( nCol
, nRow
);
343 public Size
getSize() throws RuntimeException
345 return ((XShape
) UnoRuntime
.queryInterface( XShape
.class, maChartDocument
.getDiagram())).getSize();
347 public void setSize( Size aSize
) throws RuntimeException
, PropertyVetoException
349 ((XShape
) UnoRuntime
.queryInterface( XShape
.class, maChartDocument
.getDiagram())).setSize( aSize
);
352 public Point
getPosition() throws RuntimeException
354 return ((XShape
) UnoRuntime
.queryInterface( XShape
.class, maChartDocument
.getDiagram())).getPosition();
356 public void setPosition( Point aPos
) throws RuntimeException
358 ((XShape
) UnoRuntime
.queryInterface( XShape
.class, maChartDocument
.getDiagram())).setPosition( aPos
);
361 // XShapeDescriptor : XShape : XDiagram
362 public String
getShapeType() throws RuntimeException
364 return new String( "com.sun.star.comp.Chart.JavaSampleDiagramShape" );
368 // __________ private members __________
369 private com
.sun
.star
.chart
.XChartDocument maChartDocument
;
370 private com
.sun
.star
.drawing
.XDrawPage maDrawPage
;
371 private com
.sun
.star
.lang
.XMultiServiceFactory maShapeFactory
;
373 // shapes added by add-in
374 private com
.sun
.star
.drawing
.XShape maTopLine
;
375 private com
.sun
.star
.drawing
.XShape maBottomLine
;
377 // __________ private methods __________
379 private int getAxisPosition( XShape aAxis
, double fValue
, boolean bVertical
)
385 XPropertySet aAxisProp
= (XPropertySet
) UnoRuntime
.queryInterface(
386 XPropertySet
.class, aAxis
);
391 fMin
= ((Double
) aAxisProp
.getPropertyValue( "Min" )).doubleValue();
392 fMax
= ((Double
) aAxisProp
.getPropertyValue( "Max" )).doubleValue();
393 double fRange
= fMax
- fMin
;
395 if( fMin
<= fValue
&& fValue
<= fMax
&&
400 nResult
= aAxis
.getPosition().Y
+
401 (int)((double)(aAxis
.getSize().Height
) *
402 (1.0 - (( fValue
- fMin
) / fRange
)));
406 nResult
= aAxis
.getPosition().X
+
407 (int)((double)(aAxis
.getSize().Width
) *
408 (( fValue
- fMin
) / fRange
));
412 catch( Exception ex
)
414 JOptionPane
.showMessageDialog( null, ex
, "Exception caught", JOptionPane
.WARNING_MESSAGE
);
420 // __________ static things __________
422 private static final String smServiceName
= "com.sun.star.comp.Chart.JavaSampleChartAddIn";
424 public static String
[] getSupportedServiceNames_Static()
426 String
[] aResult
= { smServiceName
,
427 "com.sun.star.chart.Diagram",
428 "com.sun.star.chart.ChartAxisYSupplier" };
434 * Returns a factory for creating the service.
435 * This method is called by the <code>JavaLoader</code>
437 * @return returns a <code>XSingleServiceFactory</code> for creating the component
438 * @param implName the name of the implementation for which a service is desired
439 * @param multiFactory the service manager to be used if needed
440 * @param regKey the registryKey
441 * @see com.sun.star.comp.loader.JavaLoader
443 public static XSingleServiceFactory
__getServiceFactory(
445 XMultiServiceFactory multiFactory
,
446 com
.sun
.star
.registry
.XRegistryKey regKey
)
448 XSingleServiceFactory xSingleServiceFactory
= null;
450 if( implName
.equals( JavaSampleChartAddIn
.class.getName()) )
452 xSingleServiceFactory
= com
.sun
.star
.comp
.loader
.FactoryHelper
.getServiceFactory(
453 JavaSampleChartAddIn
.class, smServiceName
,
454 multiFactory
, regKey
);
457 return xSingleServiceFactory
;
461 * Writes the service information into the given registry key.
462 * This method is called by the <code>JavaLoader</code>
464 * @return returns true if the operation succeeded
465 * @param regKey the registryKey
466 * @see com.sun.star.comp.loader.JavaLoader
468 public static boolean __writeRegistryServiceInfo( com
.sun
.star
.registry
.XRegistryKey regKey
)
470 boolean bResult
= true;
472 String
[] aServices
= getSupportedServiceNames_Static();
473 int i
, nLength
= aServices
.length
;
475 for( i
= 0; i
< nLength
; ++i
)
477 bResult
= bResult
&& com
.sun
.star
.comp
.loader
.FactoryHelper
.writeRegistryServiceInfo(
478 JavaSampleChartAddIn
.class.getName(), aServices
[ i
], regKey
);