1b9b79128SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3b9b79128SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4b9b79128SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5b9b79128SAndrew Rist  * distributed with this work for additional information
6b9b79128SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7b9b79128SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8b9b79128SAndrew Rist  * "License"); you may not use this file except in compliance
9b9b79128SAndrew Rist  * with the License.  You may obtain a copy of the License at
10b9b79128SAndrew Rist  *
11b9b79128SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12b9b79128SAndrew Rist  *
13b9b79128SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14b9b79128SAndrew Rist  * software distributed under the License is distributed on an
15b9b79128SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16b9b79128SAndrew Rist  * KIND, either express or implied.  See the License for the
17b9b79128SAndrew Rist  * specific language governing permissions and limitations
18b9b79128SAndrew Rist  * under the License.
19b9b79128SAndrew Rist  *
20b9b79128SAndrew Rist  *************************************************************/
21b9b79128SAndrew Rist 
22b9b79128SAndrew Rist 
23cdf0e10cSrcweir package complex.forms;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.beans.Property;
26cdf0e10cSrcweir import com.sun.star.beans.PropertyAttribute;
27cdf0e10cSrcweir import com.sun.star.beans.PropertyChangeEvent;
28cdf0e10cSrcweir import com.sun.star.beans.XMultiPropertySet;
29cdf0e10cSrcweir import com.sun.star.beans.XPropertiesChangeListener;
30cdf0e10cSrcweir import com.sun.star.lang.EventObject;
31cdf0e10cSrcweir import com.sun.star.drawing.XControlShape;
32cdf0e10cSrcweir import com.sun.star.lang.XComponent;
33cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
34cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
35cdf0e10cSrcweir import com.sun.star.util.CloseVetoException;
36cdf0e10cSrcweir import com.sun.star.util.XCloseable;
37cdf0e10cSrcweir import java.util.Vector;
38cdf0e10cSrcweir import java.util.logging.Level;
39cdf0e10cSrcweir import java.util.logging.Logger;
40cdf0e10cSrcweir import util.FormTools;
41cdf0e10cSrcweir import util.SOfficeFactory;
42cdf0e10cSrcweir import util.ValueChanger;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir import org.junit.After;
45cdf0e10cSrcweir import org.junit.AfterClass;
46cdf0e10cSrcweir import org.junit.Before;
47cdf0e10cSrcweir import org.junit.BeforeClass;
48cdf0e10cSrcweir import org.junit.Test;
49cdf0e10cSrcweir import org.openoffice.test.OfficeConnection;
50cdf0e10cSrcweir import static org.junit.Assert.*;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir /**
53cdf0e10cSrcweir  */
54cdf0e10cSrcweir public class CheckOGroupBoxModel
55cdf0e10cSrcweir {
56cdf0e10cSrcweir 
57cdf0e10cSrcweir     private XMultiPropertySet m_xPropSet;
58cdf0e10cSrcweir     private XComponent m_xDrawDoc;
59cdf0e10cSrcweir 
before()60cdf0e10cSrcweir     @Before public void before()
61cdf0e10cSrcweir     {
62cdf0e10cSrcweir         // XComponent xDrawDoc = null;
63cdf0e10cSrcweir         SOfficeFactory SOF = SOfficeFactory.getFactory(getMSF());
64cdf0e10cSrcweir 
65cdf0e10cSrcweir         try
66cdf0e10cSrcweir         {
67cdf0e10cSrcweir             System.out.println("creating a draw document");
68cdf0e10cSrcweir             m_xDrawDoc = SOF.createDrawDoc(null);
69cdf0e10cSrcweir         }
70cdf0e10cSrcweir         catch (com.sun.star.uno.Exception e)
71cdf0e10cSrcweir         {
72cdf0e10cSrcweir             fail("Couldn't create document.");
73cdf0e10cSrcweir         }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir         String objName = "GroupBox";
76cdf0e10cSrcweir         XControlShape shape = FormTools.insertControlShape(m_xDrawDoc, 5000, 7000, 2000, 2000, objName);
77cdf0e10cSrcweir         m_xPropSet = UnoRuntime.queryInterface(XMultiPropertySet.class, shape.getControl());
78cdf0e10cSrcweir     }
79cdf0e10cSrcweir 
after()80cdf0e10cSrcweir     @After public void after()
81cdf0e10cSrcweir     {
82cdf0e10cSrcweir         XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, m_xDrawDoc);
83cdf0e10cSrcweir         if (xClose != null)
84cdf0e10cSrcweir         {
85cdf0e10cSrcweir             try
86cdf0e10cSrcweir             {
87cdf0e10cSrcweir                 xClose.close(true);
88cdf0e10cSrcweir             }
89cdf0e10cSrcweir             catch (CloseVetoException ex)
90cdf0e10cSrcweir             {
91cdf0e10cSrcweir                 fail("Can't close document. Exception caught: " + ex.getMessage());
92cdf0e10cSrcweir                 /* ignore! */
93cdf0e10cSrcweir             }
94cdf0e10cSrcweir         }
95cdf0e10cSrcweir     }
setPropertyValues()96cdf0e10cSrcweir     @Test public void setPropertyValues()
97cdf0e10cSrcweir     {
98cdf0e10cSrcweir         String[] boundPropsToTest = getBoundPropsToTest();
99cdf0e10cSrcweir 
100cdf0e10cSrcweir         MyChangeListener ml = new MyChangeListener();
101cdf0e10cSrcweir         m_xPropSet.addPropertiesChangeListener(boundPropsToTest, ml);
102cdf0e10cSrcweir 
103cdf0e10cSrcweir         Object[] gValues = m_xPropSet.getPropertyValues(boundPropsToTest);
104cdf0e10cSrcweir         Object[] newValue = new Object[gValues.length];
105cdf0e10cSrcweir         System.out.println("Trying to change all properties.");
106cdf0e10cSrcweir         for (int i = 0; i < boundPropsToTest.length; i++)
107cdf0e10cSrcweir         {
108cdf0e10cSrcweir             newValue[i] = ValueChanger.changePValue(gValues[i]);
109cdf0e10cSrcweir         }
110cdf0e10cSrcweir         try
111cdf0e10cSrcweir         {
112cdf0e10cSrcweir             m_xPropSet.setPropertyValues(boundPropsToTest, newValue);
113cdf0e10cSrcweir         }
114cdf0e10cSrcweir         catch (com.sun.star.beans.PropertyVetoException e)
115cdf0e10cSrcweir         {
116*07a3d7f1SPedro Giffuni             fail("Exception occurred while trying to change the properties.");
117cdf0e10cSrcweir         }
118cdf0e10cSrcweir         catch (com.sun.star.lang.IllegalArgumentException e)
119cdf0e10cSrcweir         {
120*07a3d7f1SPedro Giffuni             fail("Exception occurred while trying to change the properties.");
121cdf0e10cSrcweir         }
122cdf0e10cSrcweir         catch (com.sun.star.lang.WrappedTargetException e)
123cdf0e10cSrcweir         {
124*07a3d7f1SPedro Giffuni             fail("Exception occurred while trying to change the properties.");
125cdf0e10cSrcweir         } // end of try-catch
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         assertTrue("Listener was not called.", ml.wasListenerCalled());
128cdf0e10cSrcweir         m_xPropSet.removePropertiesChangeListener(ml);
129cdf0e10cSrcweir     }
130cdf0e10cSrcweir 
getBoundPropsToTest()131cdf0e10cSrcweir     private String[] getBoundPropsToTest()
132cdf0e10cSrcweir     {
133cdf0e10cSrcweir         Property[] properties = m_xPropSet.getPropertySetInfo().getProperties();
134cdf0e10cSrcweir         String[] testPropsNames = null;
135cdf0e10cSrcweir 
136cdf0e10cSrcweir         Vector<String> tNames = new Vector<String>();
137cdf0e10cSrcweir 
138cdf0e10cSrcweir         for (int i = 0; i < properties.length; i++)
139cdf0e10cSrcweir         {
140cdf0e10cSrcweir 
141cdf0e10cSrcweir             Property property = properties[i];
142cdf0e10cSrcweir             String name = property.Name;
143cdf0e10cSrcweir             boolean isWritable = ((property.Attributes
144cdf0e10cSrcweir                     & PropertyAttribute.READONLY) == 0);
145cdf0e10cSrcweir             boolean isNotNull = ((property.Attributes
146cdf0e10cSrcweir                     & PropertyAttribute.MAYBEVOID) == 0);
147cdf0e10cSrcweir             boolean isBound = ((property.Attributes
148cdf0e10cSrcweir                     & PropertyAttribute.BOUND) != 0);
149cdf0e10cSrcweir 
150cdf0e10cSrcweir             if (isWritable && isNotNull && isBound)
151cdf0e10cSrcweir             {
152cdf0e10cSrcweir                 tNames.add(name);
153cdf0e10cSrcweir             }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir         } // endfor
156cdf0e10cSrcweir 
157cdf0e10cSrcweir         //get a array of bound properties
158cdf0e10cSrcweir         testPropsNames = new String[tNames.size()];
159cdf0e10cSrcweir         testPropsNames = tNames.toArray(testPropsNames);
160cdf0e10cSrcweir         return testPropsNames;
161cdf0e10cSrcweir     }
162cdf0e10cSrcweir 
163cdf0e10cSrcweir     /**
164cdf0e10cSrcweir      * Listener implementation which sets a flag when
165cdf0e10cSrcweir      * listener was called.
166cdf0e10cSrcweir      */
167cdf0e10cSrcweir     public class MyChangeListener implements XPropertiesChangeListener
168cdf0e10cSrcweir     {
169cdf0e10cSrcweir 
170cdf0e10cSrcweir         boolean propertiesChanged = false;
171cdf0e10cSrcweir 
propertiesChange(PropertyChangeEvent[] e)172cdf0e10cSrcweir         public void propertiesChange(PropertyChangeEvent[] e)
173cdf0e10cSrcweir         {
174cdf0e10cSrcweir             propertiesChanged = true;
175cdf0e10cSrcweir         }
176cdf0e10cSrcweir 
disposing(EventObject obj)177cdf0e10cSrcweir         public void disposing(EventObject obj)
178cdf0e10cSrcweir         {
179cdf0e10cSrcweir         }
180cdf0e10cSrcweir 
wasListenerCalled()181cdf0e10cSrcweir         public boolean wasListenerCalled()
182cdf0e10cSrcweir         {
183cdf0e10cSrcweir             return propertiesChanged;
184cdf0e10cSrcweir         }
185cdf0e10cSrcweir 
reset()186cdf0e10cSrcweir         public void reset()
187cdf0e10cSrcweir         {
188cdf0e10cSrcweir             propertiesChanged = false;
189cdf0e10cSrcweir         }
190cdf0e10cSrcweir     };
191cdf0e10cSrcweir 
getMSF()192cdf0e10cSrcweir     private XMultiServiceFactory getMSF()
193cdf0e10cSrcweir     {
194cdf0e10cSrcweir         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
195cdf0e10cSrcweir         return xMSF1;
196cdf0e10cSrcweir     }
197cdf0e10cSrcweir 
198cdf0e10cSrcweir     // setup and close connections
199cdf0e10cSrcweir     @BeforeClass
setUpConnection()200cdf0e10cSrcweir     public static void setUpConnection() throws Exception
201cdf0e10cSrcweir     {
202cdf0e10cSrcweir         System.out.println("setUpConnection()");
203cdf0e10cSrcweir         connection.setUp();
204cdf0e10cSrcweir     }
205cdf0e10cSrcweir 
206cdf0e10cSrcweir     @AfterClass
tearDownConnection()207cdf0e10cSrcweir     public static void tearDownConnection()
208cdf0e10cSrcweir             throws InterruptedException, com.sun.star.uno.Exception
209cdf0e10cSrcweir     {
210cdf0e10cSrcweir         System.out.println("tearDownConnection()");
211cdf0e10cSrcweir         connection.tearDown();
212cdf0e10cSrcweir     }
213cdf0e10cSrcweir     private static final OfficeConnection connection = new OfficeConnection();
214cdf0e10cSrcweir }
215