1 19 20 package org.netbeans.modules.languages.dataobject; 21 22 import java.util.Map ; 23 import org.netbeans.modules.languages.*; 24 import javax.swing.Action ; 25 import org.netbeans.api.languages.LanguagesManager; 26 import java.awt.Color ; 27 import java.awt.Dimension ; 28 import java.awt.Font ; 29 import javax.swing.JLabel ; 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 ; 38 import javax.swing.Action ; 39 import javax.swing.ActionMap ; 40 import javax.swing.BorderFactory ; 41 import javax.swing.JComponent ; 42 import javax.swing.JEditorPane ; 43 import javax.swing.UIManager ; 44 import javax.swing.text.TextAction ; 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 70 public class LanguagesEditorKit extends NbEditorKit { 71 72 private String mimeType; 73 74 77 public LanguagesEditorKit (final String mimeType) { 78 this.mimeType = mimeType; 79 Settings.addInitializer (new Settings.Initializer () { 81 public String getName() { 82 return mimeType; 83 } 84 85 public void updateSettingsMap (Class kitClass, Map 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 label; 94 95 private JLabel createToolTipComponent () { 96 if (label == null) { 97 label = new JLabel () { 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) { Dimension prefSize = getPreferredSize(); 106 if (width > prefSize.width) { width = prefSize.width; if (height >= prefSize.height) { 109 height = prefSize.height; 110 } else { height = -1; 112 } 113 114 } else { super.setSize(width, 100000); 116 int prefHeight = getPreferredSize ().height; if (prefHeight < height) { 120 height = prefHeight; 121 122 } else { height = -1; 124 } 125 } 128 } 129 130 if (height >= 0) { super.setSize(width, height); 132 } else { putClientProperty(PopupManager.Placement.class, null); 134 } 135 } 136 }; 137 138 label.setActionMap (new ActionMap ()); 140 label.setInputMap (JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null); 141 142 Font font = UIManager.getFont ("ToolTip.font"); Color backColor = UIManager.getColor("ToolTip.background"); Color foreColor = UIManager.getColor("ToolTip.foreground"); 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 [] createActions() { 166 Action [] myActions = new Action [] { 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 getActionByName(String 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 text) { 195 if (text == null) return; 196 JLabel 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); ((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 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 c) { 242 super.install (c); 243 try { 244 Language l = ((LanguagesManagerImpl) LanguagesManager.getDefault ()).getLanguage (mimeType); 245 HyperlinkListener hl = new HyperlinkListener (l); 247 c.addMouseMotionListener (hl); 248 c.addMouseListener (hl); 249 } catch (ParseException ex) { 250 } 251 } 252 253 public String getContentType() { 254 return mimeType; 255 } 256 257 public Object clone () { 258 return new LanguagesEditorKit (mimeType); 259 } 260 } 261 262 | Popular Tags |