KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > lib2 > EditorImplementation


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.lib2;
21
22 import java.util.Collection JavaDoc;
23 import java.util.ResourceBundle JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25 import javax.swing.Action JavaDoc;
26 import javax.swing.text.JTextComponent JavaDoc;
27 import org.netbeans.spi.editor.EditorImplementationProvider;
28 import org.openide.util.Lookup;
29 import org.openide.util.NbBundle;
30
31 /** This is provider of implementation. This package (org.netbeans.editor)
32  * represent editor core which can be used independently on the rest of NetBeans.
33  * However this core needs access to higher level functionality like access
34  * to localized bundles, access to settings storage, etc. which can be implemented
35  * differently by the applications which uses this editor core. For this purpose
36  * was created this abstract class and it can be extended with any other methods which
37  * are more and more often required by core editor. Example implementation
38  * of this provider can be found in org.netbeans.modules.editor package
39  *
40  * @author David Konecny
41  * @since 10/2001
42  */

43
44 public final class EditorImplementation {
45
46     private static final Logger JavaDoc LOG = Logger.getLogger(EditorImplementation.class.getName());
47     private static final EditorImplementationProvider DEFAULT = new DefaultImplementationProvider();
48     
49     private static EditorImplementation instance = null;
50     
51     private static EditorImplementationProvider externalProvider = null;
52     private Lookup.Result<EditorImplementationProvider> result = null;
53     
54     /** Returns currently registered provider */
55     public static synchronized EditorImplementation getDefault() {
56         if (instance == null) {
57             instance = new EditorImplementation();
58         }
59         return instance;
60     }
61
62     /**
63      * <p><b>IMPORTANT:</b> This method is here only for supporting the backwards
64      * compatibility of the {@link org.netbeans.editor.DialogSupport} class.
65      *
66      */

67     public void setExternalProvider(EditorImplementationProvider provider) {
68         this.externalProvider = provider;
69     }
70     
71     /** Returns ResourceBundle for the given class.*/
72     public ResourceBundle JavaDoc getResourceBundle(String JavaDoc localizer) {
73         return getProvider().getResourceBundle(localizer);
74     }
75
76     /** This is temporary method which allows core editor to access
77      * glyph gutter action. These actions are then used when user clicks
78      * on glyph gutter. In next version this should be removed and redesigned
79      * as suggested in issue #16762 */

80     public Action JavaDoc[] getGlyphGutterActions(JTextComponent JavaDoc target) {
81         return getProvider().getGlyphGutterActions(target);
82     }
83
84     /** Activates the given component or one of its ancestors.
85      * @return whether the component or one of its ancestors was succesfuly activated
86      * */

87     public boolean activateComponent(JTextComponent JavaDoc c) {
88         return getProvider().activateComponent(c);
89     }
90
91     private EditorImplementation() {
92         result = Lookup.getDefault().lookup(
93             new Lookup.Template<EditorImplementationProvider>(EditorImplementationProvider.class));
94     }
95
96     private EditorImplementationProvider getProvider() {
97         if (externalProvider != null) {
98             return externalProvider;
99         } else {
100             Collection JavaDoc<? extends EditorImplementationProvider> providers = result.allInstances();
101             if (providers.isEmpty()) {
102                 LOG.warning("Can't find any EditorImplementationProvider; using default.");
103                 return DEFAULT;
104             } else {
105                 return providers.iterator().next();
106             }
107         }
108     }
109     
110     private static final class DefaultImplementationProvider implements EditorImplementationProvider {
111         private static final Action JavaDoc [] NOACTIONS = new Action JavaDoc[0];
112         
113         public ResourceBundle JavaDoc getResourceBundle(String JavaDoc localizer) {
114             return NbBundle.getBundle(localizer);
115         }
116
117         public Action JavaDoc[] getGlyphGutterActions(JTextComponent JavaDoc target) {
118             return NOACTIONS;
119         }
120
121         public boolean activateComponent(JTextComponent JavaDoc c) {
122             return false;
123         }
124     } // End of DefaultImplementationProvider class
125
}
126
Popular Tags