update dev300-m58
[ooovba.git] / framework / source / accelerators / acceleratorcache.cxx
blobdb62188f8387b8ac6d19f67bc701aa3614210220
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: acceleratorcache.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_framework.hxx"
33 #include <accelerators/acceleratorcache.hxx>
35 //_______________________________________________
36 // own includes
38 #ifndef __FRAMEWORK_XML_ACCELERATORCONFIGURATIONREADER_HXX_
39 #include <xml/acceleratorconfigurationreader.hxx>
40 #endif
41 #include <threadhelp/readguard.hxx>
42 #include <threadhelp/writeguard.hxx>
44 //_______________________________________________
45 // interface includes
47 #ifndef __COM_SUN_STAR_CONTAINER_ELEMENTEXISTEXCEPTION_HPP_
48 #include <com/sun/star/container/ElementExistException.hpp>
49 #endif
51 #ifndef __COM_SUN_STAR_CONTAINER_NOSUCHELEMENTEXCEPTION_HPP_
52 #include <com/sun/star/container/NoSuchElementException.hpp>
53 #endif
55 //_______________________________________________
56 // other includes
57 #include <vcl/svapp.hxx>
59 namespace framework
62 //-----------------------------------------------
63 AcceleratorCache::AcceleratorCache()
64 : ThreadHelpBase(&Application::GetSolarMutex())
68 //-----------------------------------------------
69 AcceleratorCache::AcceleratorCache(const AcceleratorCache& rCopy)
70 : ThreadHelpBase(&Application::GetSolarMutex())
72 m_lCommand2Keys = rCopy.m_lCommand2Keys;
73 m_lKey2Commands = rCopy.m_lKey2Commands;
76 //-----------------------------------------------
77 AcceleratorCache::~AcceleratorCache()
79 // Dont save anything automaticly here.
80 // The user has to do that explicitly!
83 //-----------------------------------------------
84 void AcceleratorCache::takeOver(const AcceleratorCache& rCopy)
86 // SAFE -> ----------------------------------
87 WriteGuard aWriteLock(m_aLock);
89 m_lCommand2Keys = rCopy.m_lCommand2Keys;
90 m_lKey2Commands = rCopy.m_lKey2Commands;
92 aWriteLock.unlock();
93 // <- SAFE ----------------------------------
96 //-----------------------------------------------
97 AcceleratorCache& AcceleratorCache::operator=(const AcceleratorCache& rCopy)
99 takeOver(rCopy);
100 return *this;
103 //-----------------------------------------------
104 sal_Bool AcceleratorCache::hasKey(const css::awt::KeyEvent& aKey) const
106 // SAFE -> ----------------------------------
107 ReadGuard aReadLock(m_aLock);
109 return (m_lKey2Commands.find(aKey) != m_lKey2Commands.end());
110 // <- SAFE ----------------------------------
113 //-----------------------------------------------
114 sal_Bool AcceleratorCache::hasCommand(const ::rtl::OUString& sCommand) const
116 // SAFE -> ----------------------------------
117 ReadGuard aReadLock(m_aLock);
119 return (m_lCommand2Keys.find(sCommand) != m_lCommand2Keys.end());
120 // <- SAFE ----------------------------------
123 //-----------------------------------------------
124 AcceleratorCache::TKeyList AcceleratorCache::getAllKeys() const
126 TKeyList lKeys;
128 // SAFE -> ----------------------------------
129 ReadGuard aReadLock(m_aLock);
130 lKeys.reserve(m_lKey2Commands.size());
132 TKey2Commands::const_iterator pIt;
133 TKey2Commands::const_iterator pEnd = m_lKey2Commands.end();
134 for ( pIt = m_lKey2Commands.begin();
135 pIt != pEnd ;
136 ++pIt )
138 lKeys.push_back(pIt->first);
141 aReadLock.unlock();
142 // <- SAFE ----------------------------------
144 return lKeys;
147 //-----------------------------------------------
148 void AcceleratorCache::setKeyCommandPair(const css::awt::KeyEvent& aKey ,
149 const ::rtl::OUString& sCommand)
151 // SAFE -> ----------------------------------
152 WriteGuard aWriteLock(m_aLock);
154 // register command for the specified key
155 m_lKey2Commands[aKey] = sCommand;
157 // update optimized structure to bind multiple keys to one command
158 TKeyList& rKeyList = m_lCommand2Keys[sCommand];
159 rKeyList.push_back(aKey);
161 aWriteLock.unlock();
162 // <- SAFE ----------------------------------
165 //-----------------------------------------------
166 AcceleratorCache::TKeyList AcceleratorCache::getKeysByCommand(const ::rtl::OUString& sCommand) const
168 TKeyList lKeys;
170 // SAFE -> ----------------------------------
171 ReadGuard aReadLock(m_aLock);
173 TCommand2Keys::const_iterator pCommand = m_lCommand2Keys.find(sCommand);
174 if (pCommand == m_lCommand2Keys.end())
175 throw css::container::NoSuchElementException(
176 ::rtl::OUString(), css::uno::Reference< css::uno::XInterface >());
177 lKeys = pCommand->second;
179 aReadLock.unlock();
180 // <- SAFE ----------------------------------
182 return lKeys;
185 //-----------------------------------------------
186 ::rtl::OUString AcceleratorCache::getCommandByKey(const css::awt::KeyEvent& aKey) const
188 ::rtl::OUString sCommand;
190 // SAFE -> ----------------------------------
191 ReadGuard aReadLock(m_aLock);
193 TKey2Commands::const_iterator pKey = m_lKey2Commands.find(aKey);
194 if (pKey == m_lKey2Commands.end())
195 throw css::container::NoSuchElementException(
196 ::rtl::OUString(), css::uno::Reference< css::uno::XInterface >());
197 sCommand = pKey->second;
199 aReadLock.unlock();
200 // <- SAFE ----------------------------------
202 return sCommand;
205 //-----------------------------------------------
206 void AcceleratorCache::removeKey(const css::awt::KeyEvent& aKey)
208 // SAFE -> ----------------------------------
209 WriteGuard aWriteLock(m_aLock);
211 // check if key exists
212 TKey2Commands::const_iterator pKey = m_lKey2Commands.find(aKey);
213 if (pKey == m_lKey2Commands.end())
214 return;
216 // get its registered command
217 // Because we must know its place inside the optimized
218 // structure, which bind keys to commands, too!
219 ::rtl::OUString sCommand = pKey->second;
220 pKey = m_lKey2Commands.end(); // nobody should use an undefined value .-)
222 // remove key from primary list
223 m_lKey2Commands.erase(aKey);
225 // remove key from optimized command list
226 m_lCommand2Keys.erase(sCommand);
228 aWriteLock.unlock();
229 // <- SAFE ----------------------------------
232 //-----------------------------------------------
233 void AcceleratorCache::removeCommand(const ::rtl::OUString& sCommand)
235 // SAFE -> ----------------------------------
236 WriteGuard aWriteLock(m_aLock);
238 const TKeyList& lKeys = getKeysByCommand(sCommand);
239 AcceleratorCache::TKeyList::const_iterator pKey ;
240 for ( pKey = lKeys.begin();
241 pKey != lKeys.end() ;
242 ++pKey )
244 const css::awt::KeyEvent& rKey = *pKey;
245 removeKey(rKey);
247 m_lCommand2Keys.erase(sCommand);
249 aWriteLock.unlock();
250 // <- SAFE ----------------------------------
253 } // namespace framework