KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > languages > dataobject > LanguagesEditorKit


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.languages.dataobject;
21
22 import java.util.Map JavaDoc;
23 import org.netbeans.modules.languages.*;
24 import javax.swing.Action JavaDoc;
25 import org.netbeans.api.languages.LanguagesManager;
26 import java.awt.Color JavaDoc;
27 import java.awt.Dimension JavaDoc;
28 import java.awt.Font JavaDoc;
29 import javax.swing.JLabel JavaDoc;
30 import org.netbeans.api.languages.DatabaseManager;
31 import org.netbeans.editor.BaseDocument;
32 import org.netbeans.editor.EditorUI;
33 import org.netbeans.editor.Settings;
34 import org.netbeans.editor.SettingsNames;
35 import org.netbeans.editor.Syntax;
36 import org.netbeans.editor.ext.ToolTipSupport;
37 import javax.swing.text.Document JavaDoc;
38 import javax.swing.Action JavaDoc;
39 import javax.swing.ActionMap JavaDoc;
40 import javax.swing.BorderFactory JavaDoc;
41 import javax.swing.JComponent JavaDoc;
42 import javax.swing.JEditorPane JavaDoc;
43 import javax.swing.UIManager JavaDoc;
44 import javax.swing.text.TextAction JavaDoc;
45 import org.netbeans.editor.PopupManager;
46 import org.netbeans.editor.SyntaxSupport;
47 import org.netbeans.editor.ext.plain.PlainSyntax;
48 import org.netbeans.modules.editor.NbEditorUI;
49 import org.netbeans.modules.languages.features.BraceCompletionDeleteAction;
50 import org.netbeans.modules.languages.features.BraceCompletionInsertAction;
51 import org.netbeans.modules.languages.features.BraceHighlighting;
52 import org.netbeans.modules.languages.features.CollapseFoldTypeAction;
53 import org.netbeans.modules.languages.features.DatabaseManagerImpl;
54 import org.netbeans.modules.languages.LanguagesManagerImpl;
55 import org.netbeans.modules.languages.features.AnnotationManager;
56 import org.netbeans.modules.languages.features.ExpandFoldTypeAction;
57 import org.netbeans.modules.languages.features.HyperlinkListener;
58 import org.netbeans.api.languages.ParseException;
59 import org.netbeans.modules.editor.NbEditorKit;
60 import org.netbeans.modules.languages.features.IndentAction;
61 import org.netbeans.modules.languages.features.LanguagesGenerateFoldPopupAction;
62 import org.netbeans.modules.languages.features.MyFirstDrawLayer;
63 import org.netbeans.modules.languages.features.MySecondDrawLayer;
64
65
66 /**
67  *
68  * @author Jan Jancura
69  */

70 public class LanguagesEditorKit extends NbEditorKit {
71
72     private String JavaDoc mimeType;
73     
74     /**
75      * Creates a new instance of LanguagesEditorKit
76      */

77     public LanguagesEditorKit (final String JavaDoc mimeType) {
78         this.mimeType = mimeType;
79         //Settings.setValue (LanguagesEditorKit.class, SettingsNames.CODE_FOLDING_ENABLE, Boolean.TRUE);
80
Settings.addInitializer (new Settings.Initializer () {
81             public String JavaDoc getName() {
82                 return mimeType;
83             }
84
85             public void updateSettingsMap (Class JavaDoc kitClass, Map JavaDoc settingsMap) {
86                 if (kitClass != null && kitClass.equals (LanguagesEditorKit.class)) {
87                     settingsMap.put (SettingsNames.CODE_FOLDING_ENABLE, Boolean.TRUE);
88                 }
89             }
90         });
91     }
92     
93     private JLabel JavaDoc label;
94     
95     private JLabel JavaDoc createToolTipComponent () {
96         if (label == null) {
97             label = new JLabel JavaDoc () {
98                 public void setSize(int width, int height) {
99                     if (getText () == null) {
100                         super.setSize (width, height);
101                         return;
102                     }
103                     int docLen = getText ().length ();
104                     if (docLen > 0) { // nonzero length
105
Dimension JavaDoc prefSize = getPreferredSize();
106                         if (width > prefSize.width) { // given width unnecessarily big
107
width = prefSize.width; // shrink the width to preferred
108
if (height >= prefSize.height) {
109                                 height = prefSize.height;
110                             } else { // height not big enough
111
height = -1;
112                             }
113
114                         } else { // available width not enough - wrap lines
115
super.setSize(width, 100000);
116 // try {
117
//Rectangle r = modelToView(docLen - 1);
118
int prefHeight = getPreferredSize ().height;//r.y + r.height;
119
if (prefHeight < height) {
120                                     height = prefHeight;
121
122                                 } else { // the given height is too small
123
height = -1;
124                                 }
125 // } catch (BadLocationException e) {
126
// }
127
}
128                     }
129
130                     if (height >= 0) { // only for valid height
131
super.setSize(width, height);
132                     } else { // signal that the height is too small to display tooltip
133
putClientProperty(PopupManager.Placement.class, null);
134                     }
135                 }
136             };
137
138             // bugfix of #43174
139
label.setActionMap (new ActionMap JavaDoc ());
140             label.setInputMap (JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null);
141
142             Font JavaDoc font = UIManager.getFont ("ToolTip.font"); // NOI18N
143
Color JavaDoc backColor = UIManager.getColor("ToolTip.background"); // NOI18N
144
Color JavaDoc foreColor = UIManager.getColor("ToolTip.foreground"); // NOI18N
145

146             if (font != null) {
147                 label.setFont(font);
148             }
149             if (foreColor != null) {
150                 label.setForeground(foreColor);
151             }
152             if (backColor != null) {
153                 label.setBackground(backColor);
154             }
155
156             label.setOpaque(true);
157             label.setBorder(BorderFactory.createCompoundBorder(
158                 BorderFactory.createLineBorder(label.getForeground()),
159                 BorderFactory.createEmptyBorder(0, 3, 0, 3)
160             ));
161         }
162         return label;
163     }
164
165     protected Action JavaDoc[] createActions() {
166         Action JavaDoc[] myActions = new Action JavaDoc[] {
167             new BraceCompletionInsertAction (),
168             new BraceCompletionDeleteAction (),
169             new IndentAction (),
170             new LanguagesGenerateFoldPopupAction (),
171         };
172         return TextAction.augmentList (
173             super.createActions (),
174             myActions
175         );
176     }
177     
178     public Action JavaDoc getActionByName(String JavaDoc name) {
179         if (name == null)
180             return super.getActionByName (name);
181         if (name.startsWith("Expand"))
182             return new ExpandFoldTypeAction (name);
183         if (name.startsWith("Collapse"))
184             return new CollapseFoldTypeAction (name);
185         return super.getActionByName (name);
186     }
187     
188     protected EditorUI createEditorUI () {
189         return new NbEditorUI () {
190             private ToolTipSupport toolTipSupport;
191             public ToolTipSupport getToolTipSupport() {
192                 if (toolTipSupport == null) {
193                     toolTipSupport = new ToolTipSupport (this) {
194                         public void setToolTipText (String JavaDoc text) {
195                             if (text == null) return;
196                             JLabel JavaDoc l = createToolTipComponent ();
197                             l.setText (text);
198                             setToolTip (l);
199                         }
200                     };
201                 }
202                 return toolTipSupport;
203             }
204         };
205     }
206     
207     public Document createDefaultDocument() {
208         Document doc = super.createDefaultDocument ();
209         doc.putProperty("mimeType", mimeType); //NOI18N
210
((BaseDocument) doc).addLayer (
211             new MyFirstDrawLayer (mimeType),
212             3000
213         );
214         ((BaseDocument) doc).addLayer (
215             new MySecondDrawLayer (mimeType),
216             1500
217         );
218         new AnnotationManager (doc);
219         ((DatabaseManagerImpl) DatabaseManager.getDefault ()).new Listener (doc);
220         return doc;
221     }
222     
223 // public Syntax createSyntax (Document doc) {
224
// LanguagesSyntax syntax = (LanguagesSyntax) documentToSyntax.get (doc);
225
// if (syntax == null) {
226
// syntax = new LanguagesSyntax (doc);
227
// documentToSyntax.put (doc, syntax);
228
// syntax.init ();
229
// }
230
// return syntax;
231
// }
232

233     public Syntax createSyntax(Document doc) {
234         return new PlainSyntax();
235     }
236
237     public SyntaxSupport createSyntaxSupport(BaseDocument doc) {
238         return new BraceHighlighting (doc);
239     }
240     
241     public void install (JEditorPane JavaDoc c) {
242         super.install (c);
243         try {
244             Language l = ((LanguagesManagerImpl) LanguagesManager.getDefault ()).getLanguage (mimeType);
245             //if (!l.supportsHyperlinks ()) return;
246
HyperlinkListener hl = new HyperlinkListener (l);
247             c.addMouseMotionListener (hl);
248             c.addMouseListener (hl);
249         } catch (ParseException ex) {
250         }
251     }
252     
253     public String JavaDoc getContentType() {
254         return mimeType;
255     }
256     
257     public Object JavaDoc clone () {
258         return new LanguagesEditorKit (mimeType);
259     }
260 }
261
262
Popular Tags