KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > editor > EditorFoldingManager


1 package org.antlr.works.editor;
2
3 import org.antlr.works.ate.folding.ATEFoldingEntity;
4 import org.antlr.works.ate.folding.ATEFoldingEntityProxy;
5 import org.antlr.works.ate.folding.ATEFoldingManager;
6 import org.antlr.works.components.grammar.CEditorGrammar;
7 import org.antlr.works.prefs.AWPrefs;
8 import org.antlr.works.syntax.element.ElementAction;
9 import org.antlr.works.syntax.element.ElementRule;
10
11 import java.util.List JavaDoc;
12 /*
13
14 [The "BSD licence"]
15 Copyright (c) 2005 Jean Bovet
16 All rights reserved.
17
18 Redistribution and use in source and binary forms, with or without
19 modification, are permitted provided that the following conditions
20 are met:
21
22 1. Redistributions of source code must retain the above copyright
23 notice, this list of conditions and the following disclaimer.
24 2. Redistributions in binary form must reproduce the above copyright
25 notice, this list of conditions and the following disclaimer in the
26 documentation and/or other materials provided with the distribution.
27 3. The name of the author may not be used to endorse or promote products
28 derived from this software without specific prior written permission.
29
30 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
41 */

42
43 public class EditorFoldingManager extends ATEFoldingManager {
44
45     protected static final int TAG_RULES = 0;
46     protected static final int TAG_ACTIONS = 1;
47
48     protected CEditorGrammar editor;
49
50     public EditorFoldingManager(CEditorGrammar editor) {
51         super(editor.textEditor);
52         this.editor = editor;
53     }
54
55     public void textPaneWillFold() {
56         super.textPaneWillFold();
57         editor.disableTextPaneUndo();
58     }
59
60     public void textPaneDidFold() {
61         super.textPaneDidFold();
62         editor.enableTextPaneUndo();
63         editor.editorIdeas.hide();
64         editor.editorTips.hide();
65     }
66
67     public ATEFoldingEntityProxy createEntityProxy(ATEFoldingEntity entity) {
68         int tag;
69         if(entity instanceof ElementRule)
70             tag = TAG_RULES;
71         else if(entity instanceof ElementAction)
72             tag = TAG_ACTIONS;
73         else
74             return null;
75
76         return new ATEFoldingEntityProxy(this, entity.foldingEntityID(), tag);
77     }
78
79     public void provideFoldingEntities() {
80         List JavaDoc<ElementRule> rules = editor.parserEngine.getRules();
81         if(rules != null) {
82             for(int index=0; index<rules.size(); index++) {
83                 ElementRule rule = rules.get(index);
84                 addEntity(rule);
85             }
86         }
87
88         // Add only actions that are in expanded rules
89
if(AWPrefs.getFoldingEnabled() && AWPrefs.getDisplayActionsAnchorsFolding()) {
90             List JavaDoc<ElementAction> actions = editor.parserEngine.getActions();
91             if(actions != null) {
92                 for(int index=0; index<actions.size(); index++) {
93                     ElementAction action = actions.get(index);
94                     if(action.rule.isExpanded())
95                         addEntity(action);
96                 }
97             }
98         }
99     }
100
101     public ATEFoldingEntity getEntityForIdentifier(List JavaDoc entities, String JavaDoc identifier) {
102         if(entities == null || entities.isEmpty())
103             return null;
104         // @todo optimize using a map ?
105
for(int index=0; index<entities.size(); index++) {
106             ATEFoldingEntity entity = (ATEFoldingEntity)entities.get(index);
107             if(entity.foldingEntityID().equals(identifier))
108                 return entity;
109         }
110         return null;
111     }
112
113     public ATEFoldingEntity getEntityForKey(Object JavaDoc key, int tag) {
114         if(tag == TAG_ACTIONS)
115             return getEntityForIdentifier(editor.parserEngine.getActions(), (String JavaDoc)key);
116         else if(tag == TAG_RULES)
117             return getEntityForIdentifier(editor.parserEngine.getRules(), (String JavaDoc)key);
118         else
119             return null;
120     }
121
122 }
123
Popular Tags