Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/virtuosotechnologies/asaph/notationmanager/NotationManagerImpl.java


1   /*
2   ================================================================================
3   
4     FILE:  NotationManagerImpl.java
5     
6     PROJECT:
7     
8       Asaph
9     
10    CONTENTS:
11    
12      Implementation for NotationManager
13    
14    PROGRAMMERS:
15    
16      Daniel Azuma (DA)  <dazuma@kagi.com>
17    
18    COPYRIGHT:
19    
20      Copyright (C) 2003  Daniel Azuma  (dazuma@kagi.com)
21      
22      This program is free software; you can redistribute it and/or
23      modify it under the terms of the GNU General Public License as
24      published by the Free Software Foundation; either version 2
25      of the License, or (at your option) any later version.
26      
27      This program is distributed in the hope that it will be useful,
28      but WITHOUT ANY WARRANTY; without even the implied warranty of
29      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30      GNU General Public License for more details.
31      
32      You should have received a copy of the GNU General Public
33      License along with this program; if not, write to
34        Free Software Foundation, Inc.
35        59 Temple Place, Suite 330
36        Boston, MA 02111-1307 USA
37  
38  ================================================================================
39  */
40  
41  
42  package com.virtuosotechnologies.asaph.notationmanager;
43  
44  
45  import java.util.Locale;
46  import java.util.Map;
47  import java.util.LinkedHashMap;
48  import java.util.Iterator;
49  
50  import com.virtuosotechnologies.asaph.model.notation.NotationFactory;
51  
52  
53  /**
54   * Implementation for NotationManager
55   */
56  /*package*/ class NotationManagerImpl
57  implements
58    NotationManager
59  {
60    private Map locales_;
61    private NotationFactory defaultFactory_;
62    
63    
64    /*package*/ NotationManagerImpl()
65    {
66      locales_ = new LinkedHashMap();
67      defaultFactory_ = new StandardNotationFactory(false);
68      locales_.put(Locale.ENGLISH, new FilterAndFactory(
69        new LanguageLocaleFilterImpl(Locale.ENGLISH), defaultFactory_));
70      locales_.put(Locale.GERMAN, new FilterAndFactory(
71        new LanguageLocaleFilterImpl(Locale.GERMAN), new StandardNotationFactory(true)));
72    }
73    
74    
75    /**
76     * Get the NotationFactory for the given locale. If called multiple times for the same locale, it will
77     * return the same NotationFactory object.
78     *
79     * @param locale locale to use, or null to specify the default locale
80     * @return the NotationFactory for this locale.
81     */
82    public synchronized NotationFactory getNotationFactoryForLocale(
83      Locale locale)
84    {
85      NotationFactory ret = defaultFactory_;
86      for (Iterator iter = locales_.entrySet().iterator(); iter.hasNext(); )
87      {
88        Map.Entry entry = (Map.Entry)iter.next();
89        FilterAndFactory ff = (FilterAndFactory)entry.getValue();
90        if (ff.filter.isLocaleRelevant(locale))
91        {
92          ret = ff.factory;
93        }
94      }
95      return ret;
96    }
97    
98    
99    /**
100    * Register a NotationFactory to use for a given locale.
101    *
102    * @param filter identifier for the relevant locales
103    * @param representative a representative locale for use in choosers
104    * @param factory NotationFactory to use
105    */
106   public void setNotationFactoryForLocale(
107     LocaleFilter filter,
108     Locale representative,
109     NotationFactory factory)
110   {
111     locales_.remove(representative);
112     locales_.put(representative, new FilterAndFactory(filter, factory));
113   }
114   
115   
116   /**
117    * Get an array of representative locales, used for choosing a NotationFactory.
118    * Commonly used to build guis.
119    *
120    * @return an array of Locale
121    */
122   public synchronized Locale[] getRepresentativeLocales()
123   {
124     Locale[] ret = new Locale[locales_.size()];
125     int index = 0;
126     for (Iterator iter = locales_.keySet().iterator(); iter.hasNext(); ++index)
127     {
128       ret[index] = (Locale)iter.next();
129     }
130     return ret;
131   }
132   
133   
134   private static class FilterAndFactory
135   {
136     /*package*/ LocaleFilter filter;
137     /*package*/ NotationFactory factory;
138     
139     /*package*/ FilterAndFactory(
140       LocaleFilter filt,
141       NotationFactory fact)
142     {
143       filter = filt;
144       factory = fact;
145     }
146   }
147   
148   
149   private static class LanguageLocaleFilterImpl
150   implements
151     LocaleFilter
152   {
153     private Locale language_;
154     
155     
156     /*package*/ LanguageLocaleFilterImpl(
157       Locale language)
158     {
159       language_ = language;
160     }
161     
162     
163     public boolean isLocaleRelevant(
164       Locale locale)
165     {
166       return locale.getLanguage().equals(language_.getLanguage());
167     }
168   }
169 }