xref: /trunk/main/odk/examples/DevelopersGuide/Forms/ComponentTreeTraversal.java (revision 3309286857f19787ae62bd793a98b5af4edd2ad3)
134dd1e25SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
334dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
434dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
534dd1e25SAndrew Rist  * distributed with this work for additional information
634dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
734dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
834dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
934dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
1134dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
1334dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1434dd1e25SAndrew Rist  * software distributed under the License is distributed on an
1534dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1634dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
1734dd1e25SAndrew Rist  * specific language governing permissions and limitations
1834dd1e25SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
2034dd1e25SAndrew Rist  *************************************************************/
2134dd1e25SAndrew Rist 
2234dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.uno.*;
25cdf0e10cSrcweir import com.sun.star.container.*;
26cdf0e10cSrcweir import com.sun.star.lang.*;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir /**************************************************************************/
29cdf0e10cSrcweir /** an abstract interface for components doing an action on a form component
30cdf0e10cSrcweir */
31cdf0e10cSrcweir interface IFormComponentAction
32cdf0e10cSrcweir {
handle( Object aFormComponent )33cdf0e10cSrcweir     public abstract void handle( Object aFormComponent ) throws java.lang.Exception;
34cdf0e10cSrcweir };
35cdf0e10cSrcweir 
36cdf0e10cSrcweir /**************************************************************************/
37cdf0e10cSrcweir /** a helper class for travelling a form component tree
38cdf0e10cSrcweir */
39cdf0e10cSrcweir class ComponentTreeTraversal implements IFormComponentAction
40cdf0e10cSrcweir {
41cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
42*74cbd1f1SMatthias Seidel     /** Indicator method to decide whether to step down the tree.
43cdf0e10cSrcweir 
44cdf0e10cSrcweir         <p>The default implementation checks if the container given is a grid
45cdf0e10cSrcweir         control model or a <service scope="com.sun.star.form">FormComponents</service>
46cdf0e10cSrcweir         instance.</p>
47cdf0e10cSrcweir     */
shouldStepInto( XIndexContainer xContainer )48cdf0e10cSrcweir     protected boolean shouldStepInto( XIndexContainer xContainer ) throws com.sun.star.uno.Exception
49cdf0e10cSrcweir     {
50cdf0e10cSrcweir         // step down the tree, if possible
51cdf0e10cSrcweir         XServiceInfo xSI = UNO.queryServiceInfo( xContainer );
52cdf0e10cSrcweir         if  (   null != xSI
53cdf0e10cSrcweir             &&  (   xSI.supportsService( "com.sun.star.form.FormComponents" )
54cdf0e10cSrcweir                 ||  xSI.supportsService( "com.sun.star.form.component.GridControl" )
55cdf0e10cSrcweir                 )
56cdf0e10cSrcweir             )
57cdf0e10cSrcweir         {
58cdf0e10cSrcweir             return true;
59cdf0e10cSrcweir         }
60cdf0e10cSrcweir         else
61cdf0e10cSrcweir         {
62cdf0e10cSrcweir             return false;
63cdf0e10cSrcweir         }
64cdf0e10cSrcweir     }
65cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
handle( Object aFormComponent )66cdf0e10cSrcweir     public void handle( Object aFormComponent ) throws com.sun.star.uno.Exception
67cdf0e10cSrcweir     {
68cdf0e10cSrcweir         XIndexContainer xCont = UNO.queryIndexContainer( aFormComponent );
69cdf0e10cSrcweir         if  (   ( null != xCont )
70cdf0e10cSrcweir             &&  shouldStepInto( xCont )
71cdf0e10cSrcweir             )
72cdf0e10cSrcweir         {
73cdf0e10cSrcweir             for ( int i=0; i<xCont.getCount(); ++i )
74cdf0e10cSrcweir             {
75cdf0e10cSrcweir                 handle( xCont.getByIndex( i ) );
76cdf0e10cSrcweir             }
77cdf0e10cSrcweir         }
78cdf0e10cSrcweir     }
79cdf0e10cSrcweir }
80