1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
33 *************************************************************************/
35 import com
.sun
.star
.uno
.UnoRuntime
;
36 import com
.sun
.star
.container
.XIndexAccess
;
37 import com
.sun
.star
.lib
.uno
.helper
.WeakBase
;
40 import com
.sun
.star
.lang
.XMultiServiceFactory
;
41 import com
.sun
.star
.lang
.XSingleServiceFactory
;
44 import com
.sun
.star
.drawing
.*;
45 import com
.sun
.star
.awt
.Point
;
46 import com
.sun
.star
.awt
.Size
;
49 import com
.sun
.star
.chart
.*;
52 import com
.sun
.star
.beans
.*;
55 import com
.sun
.star
.lang
.XInitialization
;
56 import com
.sun
.star
.util
.XRefreshable
;
57 import com
.sun
.star
.lang
.XServiceName
;
58 import com
.sun
.star
.lang
.XServiceInfo
;
60 import com
.sun
.star
.uno
.Exception
;
61 import com
.sun
.star
.uno
.RuntimeException
;
63 import javax
.swing
.JOptionPane
;
65 public class JavaSampleChartAddIn
extends WeakBase
implements
73 // __________ interface methods __________
76 public void initialize( Object
[] aArguments
)
77 throws Exception
, RuntimeException
79 if( aArguments
.length
> 0 )
81 maChartDocument
= UnoRuntime
.queryInterface(
82 XChartDocument
.class, aArguments
[ 0 ]);
84 XPropertySet aDocProp
= UnoRuntime
.queryInterface(
85 XPropertySet
.class, maChartDocument
);
86 if( aDocProp
!= null )
88 // set base diagram which will be extended in refresh()
89 aDocProp
.setPropertyValue( "BaseDiagram", "com.sun.star.chart.XYDiagram" );
93 XDrawPageSupplier aPageSupp
= UnoRuntime
.queryInterface(
94 XDrawPageSupplier
.class, maChartDocument
);
95 if( aPageSupp
!= null )
96 maDrawPage
= UnoRuntime
.queryInterface(
97 XDrawPage
.class, aPageSupp
.getDrawPage() );
99 // get a factory for creating shapes
100 maShapeFactory
= UnoRuntime
.queryInterface(
101 XMultiServiceFactory
.class, maChartDocument
);
106 public void refresh() throws RuntimeException
108 // recycle shapes in first call, if document was loaded
109 if( maBottomLine
== null ||
112 // try to recycle loaded shapes
113 XPropertySet aDocProp
= UnoRuntime
.queryInterface(
114 XPropertySet
.class, maChartDocument
);
115 if( aDocProp
!= null )
119 XIndexAccess aShapesIA
= UnoRuntime
.queryInterface(
120 XIndexAccess
.class, aDocProp
.getPropertyValue( "AdditionalShapes" ));
121 if( aShapesIA
!= null &&
122 aShapesIA
.getCount() > 0 )
126 for( int i
= aShapesIA
.getCount() - 1; i
>= 0; --i
)
128 aShape
= UnoRuntime
.queryInterface(
129 XShape
.class, aShapesIA
.getByIndex( i
));
132 XPropertySet aProp
= UnoRuntime
.queryInterface(
133 XPropertySet
.class, aShape
);
134 aName
= (String
) aProp
.getPropertyValue( "Name" );
136 if( aName
.equals( "top" ))
140 else if( aName
.equals( "bottom" ))
142 maBottomLine
= aShape
;
148 catch( Exception ex
)
150 JOptionPane
.showMessageDialog( null, ex
, "Exception caught", JOptionPane
.WARNING_MESSAGE
);
155 // create top line if it does not yet exist
158 if( maTopLine
== null )
160 maTopLine
= UnoRuntime
.queryInterface(
161 XShape
.class, maShapeFactory
.createInstance( "com.sun.star.drawing.LineShape" ));
162 maDrawPage
.add( maTopLine
);
164 // make line red and thicker
165 XPropertySet aShapeProp
= UnoRuntime
.queryInterface(
166 XPropertySet
.class, maTopLine
);
168 aShapeProp
.setPropertyValue( "LineColor", Integer
.valueOf( 0xe01010 ));
169 aShapeProp
.setPropertyValue( "LineWidth", Integer
.valueOf( 50 ));
170 aShapeProp
.setPropertyValue( "Name", "top" );
173 catch( Exception ex
)
175 JOptionPane
.showMessageDialog( null, ex
, "Exception caught", JOptionPane
.WARNING_MESSAGE
);
178 // create bottom line if it does not yet exist
181 if( maBottomLine
== null )
183 maBottomLine
= UnoRuntime
.queryInterface(
184 XShape
.class, maShapeFactory
.createInstance( "com.sun.star.drawing.LineShape" ));
185 maDrawPage
.add( maBottomLine
);
187 // make line green and thicker
188 XPropertySet aShapeProp
= UnoRuntime
.queryInterface(
189 XPropertySet
.class, maBottomLine
);
191 aShapeProp
.setPropertyValue( "LineColor", Integer
.valueOf( 0x10e010 ));
192 aShapeProp
.setPropertyValue( "LineWidth", Integer
.valueOf( 50 ));
193 aShapeProp
.setPropertyValue( "Name", "bottom" );
196 catch( Exception ex
)
198 JOptionPane
.showMessageDialog( null, ex
, "Exception caught", JOptionPane
.WARNING_MESSAGE
);
201 if( maTopLine
== null ||
202 maBottomLine
== null )
204 JOptionPane
.showMessageDialog( null, "One of the lines is still null", "Assertion", JOptionPane
.WARNING_MESSAGE
);
212 XChartDataArray aDataArray
= UnoRuntime
.queryInterface(
213 XChartDataArray
.class, maChartDocument
.getData());
214 double aData
[][] = aDataArray
.getData();
217 XDiagram aDiagram
= maChartDocument
.getDiagram();
218 XShape aXAxis
= UnoRuntime
.queryInterface(
219 XShape
.class, UnoRuntime
.queryInterface(
220 XAxisXSupplier
.class, aDiagram
).getXAxis() );
221 XShape aYAxis
= UnoRuntime
.queryInterface(
222 XShape
.class, UnoRuntime
.queryInterface(
223 XAxisYSupplier
.class, aDiagram
).getYAxis() );
225 // calculate points for hull
226 final int nLength
= aData
.length
;
230 Point aMaxPtSeq
[][] = new Point
[ 1 ][];
231 aMaxPtSeq
[ 0 ] = new Point
[ nLength
];
232 Point aMinPtSeq
[][] = new Point
[ 1 ][];
233 aMinPtSeq
[ 0 ] = new Point
[ nLength
];
235 for( i
= 0; i
< nLength
; i
++ )
237 fMin
= fMax
= aData
[ i
][ 1 ];
238 for( j
= 1; j
< aData
[ i
].length
; j
++ )
240 if( aData
[ i
][ j
] > fMax
)
241 fMax
= aData
[ i
][ j
];
242 else if( aData
[ i
][ j
] < fMin
)
243 fMin
= aData
[ i
][ j
];
245 aMaxPtSeq
[ 0 ][ i
] = new Point( getAxisPosition( aXAxis
, aData
[ i
][ 0 ], false ),
246 getAxisPosition( aYAxis
, fMax
, true ));
247 aMinPtSeq
[ 0 ][ i
] = new Point( getAxisPosition( aXAxis
, aData
[ i
][ 0 ], false ),
248 getAxisPosition( aYAxis
, fMin
, true ));
251 // apply point sequences to lines
254 XPropertySet aShapeProp
= UnoRuntime
.queryInterface(
255 XPropertySet
.class, maTopLine
);
256 aShapeProp
.setPropertyValue( "PolyPolygon", aMaxPtSeq
);
258 aShapeProp
= UnoRuntime
.queryInterface(
259 XPropertySet
.class, maBottomLine
);
260 aShapeProp
.setPropertyValue( "PolyPolygon", aMinPtSeq
);
262 catch( Exception ex
)
264 JOptionPane
.showMessageDialog( null, ex
, "Exception caught", JOptionPane
.WARNING_MESSAGE
);
268 public void addRefreshListener( com
.sun
.star
.util
.XRefreshListener aListener
)
269 throws RuntimeException
271 // we don't want this but we have to implement the interface
274 public void removeRefreshListener( com
.sun
.star
.util
.XRefreshListener aListener
)
275 throws RuntimeException
277 // we don't want this but we have to implement the interface
282 public String
getServiceName() throws RuntimeException
284 return smServiceName
;
288 public boolean supportsService( String aServiceName
)
290 String
[] aServices
= getSupportedServiceNames_Static();
291 int i
, nLength
= aServices
.length
;
292 boolean bResult
= false;
294 for( i
= 0; !bResult
&& i
< nLength
; ++i
)
295 bResult
= aServiceName
.equals( aServices
[ i
] );
300 public String
getImplementationName()
302 return( JavaSampleChartAddIn
.class.getName() );
305 public String
[] getSupportedServiceNames()
307 return getSupportedServiceNames_Static();
311 public String
getDiagramType() throws RuntimeException
313 return smServiceName
;
316 public XPropertySet
getDataRowProperties( int nRow
)
317 throws com
.sun
.star
.lang
.IndexOutOfBoundsException
, RuntimeException
319 return maChartDocument
.getDiagram().getDataRowProperties( nRow
);
322 public XPropertySet
getDataPointProperties( int nCol
, int nRow
)
323 throws com
.sun
.star
.lang
.IndexOutOfBoundsException
, RuntimeException
325 return maChartDocument
.getDiagram().getDataPointProperties( nCol
, nRow
);
329 public Size
getSize() throws RuntimeException
331 return UnoRuntime
.queryInterface( XShape
.class, maChartDocument
.getDiagram()).getSize();
333 public void setSize( Size aSize
) throws RuntimeException
, PropertyVetoException
335 UnoRuntime
.queryInterface( XShape
.class, maChartDocument
.getDiagram()).setSize( aSize
);
338 public Point
getPosition() throws RuntimeException
340 return UnoRuntime
.queryInterface( XShape
.class, maChartDocument
.getDiagram()).getPosition();
342 public void setPosition( Point aPos
) throws RuntimeException
344 UnoRuntime
.queryInterface( XShape
.class, maChartDocument
.getDiagram()).setPosition( aPos
);
347 // XShapeDescriptor : XShape : XDiagram
348 public String
getShapeType() throws RuntimeException
350 return "com.sun.star.comp.Chart.JavaSampleDiagramShape";
354 // __________ private members __________
355 private com
.sun
.star
.chart
.XChartDocument maChartDocument
;
356 private com
.sun
.star
.drawing
.XDrawPage maDrawPage
;
357 private com
.sun
.star
.lang
.XMultiServiceFactory maShapeFactory
;
359 // shapes added by add-in
360 private com
.sun
.star
.drawing
.XShape maTopLine
;
361 private com
.sun
.star
.drawing
.XShape maBottomLine
;
363 // __________ private methods __________
365 private int getAxisPosition( XShape aAxis
, double fValue
, boolean bVertical
)
371 XPropertySet aAxisProp
= UnoRuntime
.queryInterface(
372 XPropertySet
.class, aAxis
);
377 fMin
= ((Double
) aAxisProp
.getPropertyValue( "Min" )).doubleValue();
378 fMax
= ((Double
) aAxisProp
.getPropertyValue( "Max" )).doubleValue();
379 double fRange
= fMax
- fMin
;
381 if( fMin
<= fValue
&& fValue
<= fMax
&&
386 nResult
= aAxis
.getPosition().Y
+
387 (int)((aAxis
.getSize().Height
) *
388 (1.0 - (( fValue
- fMin
) / fRange
)));
392 nResult
= aAxis
.getPosition().X
+
393 (int)((aAxis
.getSize().Width
) *
394 (( fValue
- fMin
) / fRange
));
398 catch( Exception ex
)
400 JOptionPane
.showMessageDialog( null, ex
, "Exception caught", JOptionPane
.WARNING_MESSAGE
);
406 // __________ static things __________
408 private static final String smServiceName
= "com.sun.star.comp.Chart.JavaSampleChartAddIn";
410 public static String
[] getSupportedServiceNames_Static()
412 String
[] aResult
= { smServiceName
,
413 "com.sun.star.chart.Diagram",
414 "com.sun.star.chart.ChartAxisYSupplier" };
420 * Returns a factory for creating the service.
421 * This method is called by the <code>JavaLoader</code>
423 * @return returns a <code>XSingleServiceFactory</code> for creating the component
424 * @param implName the name of the implementation for which a service is desired
425 * @param multiFactory the service manager to be used if needed
426 * @param regKey the registryKey
427 * @see com.sun.star.comp.loader.JavaLoader
429 public static XSingleServiceFactory
__getServiceFactory(
431 XMultiServiceFactory multiFactory
,
432 com
.sun
.star
.registry
.XRegistryKey regKey
)
434 XSingleServiceFactory xSingleServiceFactory
= null;
436 if( implName
.equals( JavaSampleChartAddIn
.class.getName()) )
438 xSingleServiceFactory
= com
.sun
.star
.comp
.loader
.FactoryHelper
.getServiceFactory(
439 JavaSampleChartAddIn
.class, smServiceName
,
440 multiFactory
, regKey
);
443 return xSingleServiceFactory
;