1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 
25 
26 #include "ctp_factory.hxx"
27 #include "ctp_panel.hxx"
28 
29 /** === begin UNO includes === **/
30 #include <com/sun/star/lang/NotInitializedException.hpp>
31 #include <com/sun/star/lang/IllegalArgumentException.hpp>
32 #include <com/sun/star/lang/XComponent.hpp>
33 /** === end UNO includes === **/
34 
35 //......................................................................................................................
36 namespace sd { namespace colortoolpanel
37 {
38 //......................................................................................................................
39 
40 	/** === begin UNO using === **/
41 	using ::com::sun::star::uno::Reference;
42 	using ::com::sun::star::uno::XInterface;
43 	using ::com::sun::star::uno::UNO_QUERY;
44 	using ::com::sun::star::uno::UNO_QUERY_THROW;
45 	using ::com::sun::star::uno::UNO_SET_THROW;
46 	using ::com::sun::star::uno::Exception;
47 	using ::com::sun::star::uno::RuntimeException;
48 	using ::com::sun::star::uno::Any;
49 	using ::com::sun::star::uno::makeAny;
50 	using ::com::sun::star::uno::Sequence;
51 	using ::com::sun::star::uno::Type;
52     using ::com::sun::star::uno::XComponentContext;
53     using ::com::sun::star::lang::NotInitializedException;
54     using ::com::sun::star::lang::IllegalArgumentException;
55     using ::com::sun::star::lang::XComponent;
56     using ::com::sun::star::ui::XUIElement;
57     using ::com::sun::star::beans::PropertyValue;
58     using ::com::sun::star::container::NoSuchElementException;
59     using ::com::sun::star::beans::PropertyValue;
60     using ::com::sun::star::awt::XWindow;
61 	/** === end UNO using === **/
62 
63 	//==================================================================================================================
64 	//= ToolPanelFactory
65 	//==================================================================================================================
66 	//------------------------------------------------------------------------------------------------------------------
ToolPanelFactory(const Reference<XComponentContext> & i_rContext)67     ToolPanelFactory::ToolPanelFactory( const Reference< XComponentContext >& i_rContext )
68         :m_xContext( i_rContext )
69     {
70     }
71 
72 	//------------------------------------------------------------------------------------------------------------------
~ToolPanelFactory()73     ToolPanelFactory::~ToolPanelFactory()
74     {
75     }
76 
77     //------------------------------------------------------------------------------------------------------------------
createUIElement(const::rtl::OUString & i_rResourceURL,const Sequence<PropertyValue> & i_rArgs)78     Reference< XUIElement > SAL_CALL ToolPanelFactory::createUIElement( const ::rtl::OUString& i_rResourceURL, const Sequence< PropertyValue >& i_rArgs ) throw (NoSuchElementException, IllegalArgumentException, RuntimeException)
79     {
80         ::osl::MutexGuard aGuard( m_aMutex );
81 
82         if ( !i_rResourceURL.matchAsciiL( RTL_CONSTASCII_STRINGPARAM( "private:resource/toolpanel/org.openoffice.example.colorpanel/" ) ) )
83             throw NoSuchElementException( i_rResourceURL, *this );
84 
85         const ::rtl::OUString sColor( i_rResourceURL.copy( i_rResourceURL.lastIndexOf( '/' ) + 1 ) );
86         const sal_Int32 nPanelColor = sColor.toInt32( 16 );
87 
88         // retrieve the parent window
89         Reference< XWindow > xParentWindow;
90         const PropertyValue* pArg = i_rArgs.getConstArray();
91         const PropertyValue* pArgEnd = i_rArgs.getConstArray() + i_rArgs.getLength();
92         for ( ; pArg != pArgEnd; ++pArg )
93         {
94             if ( pArg->Name.equalsAscii( "ParentWindow" ) )
95             {
96                 xParentWindow.set( pArg->Value, UNO_QUERY );
97                 break;
98             }
99         }
100         if ( !xParentWindow.is() )
101         {
102             OSL_ENSURE( false, "ToolPanelFactory::createUIElement: no parent window in the args!" );
103             throw IllegalArgumentException(
104                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "No parent window provided in the creation arguments. Cannot create tool panel." ) ),
105                 *this,
106                 2
107             );
108         }
109 
110         /// create the panel
111         Reference< XUIElement > xUIElement( new PanelUIElement( m_xContext, xParentWindow, i_rResourceURL, nPanelColor ) );
112         return xUIElement;
113     }
114 
115     //------------------------------------------------------------------------------------------------------------------
getImplementationName()116     ::rtl::OUString SAL_CALL ToolPanelFactory::getImplementationName(  ) throw (RuntimeException)
117     {
118         return getImplementationName_static();
119     }
120 
121     //------------------------------------------------------------------------------------------------------------------
getImplementationName_static()122     ::rtl::OUString SAL_CALL ToolPanelFactory::getImplementationName_static(  ) throw (RuntimeException)
123     {
124         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.comp.example.custompanel.ToolPanelFactory" ) );
125     }
126 
127     //------------------------------------------------------------------------------------------------------------------
supportsService(const::rtl::OUString & i_rServiceName)128     ::sal_Bool SAL_CALL ToolPanelFactory::supportsService( const ::rtl::OUString& i_rServiceName ) throw (RuntimeException)
129     {
130         const Sequence< ::rtl::OUString > aServiceNames( getSupportedServiceNames() );
131         for (   const ::rtl::OUString* serviceName = aServiceNames.getConstArray();
132                 serviceName != aServiceNames.getConstArray() + aServiceNames.getLength();
133                 ++serviceName
134             )
135         {
136             if ( i_rServiceName == *serviceName )
137                 return  sal_True;
138         }
139         return sal_False;
140     }
141 
142     //------------------------------------------------------------------------------------------------------------------
getSupportedServiceNames()143     Sequence< ::rtl::OUString > SAL_CALL ToolPanelFactory::getSupportedServiceNames() throw (RuntimeException)
144     {
145         return getSupportedServiceNames_static();
146     }
147 
148     //------------------------------------------------------------------------------------------------------------------
getSupportedServiceNames_static()149     Sequence< ::rtl::OUString > SAL_CALL ToolPanelFactory::getSupportedServiceNames_static() throw (RuntimeException)
150     {
151         Sequence< ::rtl::OUString > aServiceNames(1);
152         aServiceNames[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.example.colorpanel.ToolPanelFactory" ) );
153         return aServiceNames;
154     }
155 
156     //------------------------------------------------------------------------------------------------------------------
Create(const Reference<XComponentContext> & i_rContext)157     Reference< XInterface > SAL_CALL ToolPanelFactory::Create( const Reference< XComponentContext >& i_rContext ) throw (RuntimeException)
158     {
159         return *( new ToolPanelFactory( i_rContext ) );
160     }
161 
162 //......................................................................................................................
163 } } // namespace sd::colortoolpanel
164 //......................................................................................................................
165