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 #include "precompiled_sfx2.hxx"
23 
24 #include "ContextList.hxx"
25 #include "Context.hxx"
26 
27 using ::rtl::OUString;
28 
29 namespace sfx2 { namespace sidebar {
30 
31 
ContextList(void)32 ContextList::ContextList (void)
33     : maEntries()
34 {
35 }
36 
37 
38 
39 
~ContextList(void)40 ContextList::~ContextList (void)
41 {
42 }
43 
44 
45 
46 
GetMatch(const Context & rContext) const47 const ContextList::Entry* ContextList::GetMatch (const Context& rContext) const
48 {
49     const ::std::vector<Entry>::const_iterator iEntry = FindBestMatch(rContext);
50     if (iEntry != maEntries.end())
51         return &*iEntry;
52     else
53         return NULL;
54 }
55 
56 
57 
58 
GetMatch(const Context & rContext)59 ContextList::Entry* ContextList::GetMatch (const Context& rContext)
60 {
61     const ::std::vector<Entry>::const_iterator iEntry = FindBestMatch(rContext);
62     if (iEntry != maEntries.end())
63         return const_cast<Entry*>(&*iEntry);
64     else
65         return NULL;
66 }
67 
68 
69 
70 
FindBestMatch(const Context & rContext) const71 ::std::vector<ContextList::Entry>::const_iterator ContextList::FindBestMatch (const Context& rContext) const
72 {
73     sal_Int32 nBestMatch (Context::NoMatch);
74     ::std::vector<Entry>::const_iterator iBestMatch (maEntries.end());
75 
76     for (::std::vector<Entry>::const_iterator
77              iEntry(maEntries.begin()),
78              iEnd(maEntries.end());
79          iEntry!=iEnd;
80          ++iEntry)
81     {
82         const sal_Int32 nMatch (rContext.EvaluateMatch(iEntry->maContext));
83         if (nMatch < nBestMatch)
84         {
85             nBestMatch = nMatch;
86             iBestMatch = iEntry;
87         }
88         if (nBestMatch == Context::OptimalMatch)
89             return iEntry;
90     }
91 
92     return iBestMatch;
93 }
94 
95 
96 
97 
AddContextDescription(const Context & rContext,const bool bIsInitiallyVisible,const OUString & rsMenuCommand)98 void ContextList::AddContextDescription (
99     const Context& rContext,
100     const bool bIsInitiallyVisible,
101     const OUString& rsMenuCommand)
102 {
103     maEntries.push_back(Entry());
104     maEntries.back().maContext = rContext;
105     maEntries.back().mbIsInitiallyVisible = bIsInitiallyVisible;
106     maEntries.back().msMenuCommand = rsMenuCommand;
107 }
108 
109 
110 
111 
IsEmpty(void)112 bool ContextList::IsEmpty (void)
113 {
114     return maEntries.empty();
115 }
116 
117 
118 } } // end of namespace sfx2::sidebar
119