11Sep/060
NetBeans Code Folding
Tired of lose the fight against NetBeans MDR stuff, I decided to check out Code Folding. Pretty easy, indeed. I haven't done a full code fold example, but I was able to use NB 'editor-fold' pseudo-tag. Considering that you have the syntax support (see NBP Tutorials), the steps are:
- The first step is not obvious, but it is the most important step. Without it, you will never activate folding (this was hard to find out - like any other "non-layer.xml" stuff). On your SettingsInitializer, you need to add this line:
settingsMap.put(SettingsNames.CODE_FOLDING_ENABLE, Boolean.TRUE);
- Next, we need to tell CustomFold which tokens it must use to find the pseudo-tag. On your EditorKit, you need to implement the SyntaxUpdateTokens class and put it as a document property. Like this:
public class MyEditorKit extends NbEditorKit { [ ... ] protected void initDocument(BaseDocument doc) { doc.putProperty(SyntaxUpdateTokens.class, new SUT()); } public class SUT extends SyntaxUpdateTokens { private List list; public void syntaxUpdateStart() { list = new ArrayList(); } public List syntaxUpdateEnd() { return list; } public void syntaxUpdateToken(TokenID tid, TokenContextPath tcp, int offset, int len) { if (tokenID == MyTokenContext.LINE_COMMENT) { list.add(new TokenInfo(tid, tcp, offset, len)); } } } }
- This is a weird step. I don't know why there's no ready-to-go class to do this job. You need to create a three-line class to add the sidebar to the editor:
public class MyFoldingSideBarFactory implements SideBarFactory { public JComponent createSideBar(JTextComponent jTextComponent) { return new CodeFoldingSideBar(jTextComponent); } }
- Now, glue everything in your layer.xml, under "Editor/MIME" folder:
<folder name="SideBar"> <file name="mypkg-MyFoldingSideBarFactory.instance"/> <attr name="org-netbeans-editor-GlyphGutter.instance /mypkg-MyFoldingSideBarFactory.instance" boolvalue="true"/> </folder> <folder name="FoldManager"> <file name="org-netbeans-editor-CustomFoldManager$Factory.instance"/> </folder>
Small note: implementations of FoldManager can be chained, just like the SideBar. Making it easy to add a "real" code fold based on tokens.
Tagged as: codefold, codefolding, foldmanager, sidebar
No Comments
Pages
Categories
Archives
- July 2011
- April 2011
- October 2010
- September 2010
- August 2010
- July 2010
- May 2010
- April 2010
- March 2010
- December 2009
- November 2009
- October 2009
- September 2009
- July 2009
- June 2009
- May 2009
- March 2009
- February 2009
- January 2009
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- March 2007
- September 2006
Tags
action
actions
ant
arguments
asynchronous
attributes
autowire
binding
build
database
eclipse
error
Gridsphere
hibernate
html
ibm
injection
jaas
jasper
JBoss
jpa
jsf
jtable
Liferay
lift
lookup
migration
nbp
NetBeans
nio
password
platform
Portal
Profiler
project
realm
refactoring
remote
Scala
script
spring
tomcat
tooltip
tutorial
websphere