1 | /*
|
---|
2 | * 10/23/2011
|
---|
3 | *
|
---|
4 | * FoldCollapser.java - Goes through an RSTA instance and collapses folds of
|
---|
5 | * specific types, such as comments.
|
---|
6 | *
|
---|
7 | * This library is distributed under a modified BSD license. See the included
|
---|
8 | * RSyntaxTextArea.License.txt file for details.
|
---|
9 | */
|
---|
10 | package org.fife.ui.rsyntaxtextarea.folding;
|
---|
11 |
|
---|
12 | import java.util.ArrayList;
|
---|
13 | import java.util.List;
|
---|
14 |
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Collapses folds based on their type. You can create an instance of this
|
---|
18 | * class to collapse all comment blocks when opening a new file, for example.
|
---|
19 | *
|
---|
20 | * @author Robert Futrell
|
---|
21 | * @version 1.0
|
---|
22 | */
|
---|
23 | public class FoldCollapser {
|
---|
24 |
|
---|
25 | private List typesToCollapse;
|
---|
26 |
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Creates an instance that collapses all comment blocks.
|
---|
30 | */
|
---|
31 | public FoldCollapser() {
|
---|
32 | this(FoldType.COMMENT);
|
---|
33 | }
|
---|
34 |
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Creates an instance that collapses all blocks of the specified
|
---|
38 | * type.
|
---|
39 | *
|
---|
40 | * @param typeToCollapse The type to collapse.
|
---|
41 | * @see FoldType
|
---|
42 | */
|
---|
43 | public FoldCollapser(int typeToCollapse) {
|
---|
44 | typesToCollapse = new ArrayList();
|
---|
45 | addTypeToCollapse(typeToCollapse);
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Adds a type of fold to collapse.
|
---|
51 | *
|
---|
52 | * @param typeToCollapse The type of fold to collapse.
|
---|
53 | */
|
---|
54 | public void addTypeToCollapse(int typeToCollapse) {
|
---|
55 | typesToCollapse.add(new Integer(typeToCollapse));
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Collapses any relevant folds known by the fold manager.
|
---|
61 | *
|
---|
62 | * @param fm The fold manager.
|
---|
63 | */
|
---|
64 | public void collapseFolds(FoldManager fm) {
|
---|
65 | for (int i=0; i<fm.getFoldCount(); i++) {
|
---|
66 | Fold fold = fm.getFold(i);
|
---|
67 | collapseImpl(fold);
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Collapses the specified fold, and any of its child folds, as
|
---|
74 | * appropriate.
|
---|
75 | *
|
---|
76 | * @param fold The fold to examine.
|
---|
77 | * @see #getShouldCollapse(Fold)
|
---|
78 | */
|
---|
79 | protected void collapseImpl(Fold fold) {
|
---|
80 | if (getShouldCollapse(fold)) {
|
---|
81 | fold.setCollapsed(true);
|
---|
82 | }
|
---|
83 | for (int i=0; i<fold.getChildCount(); i++) {
|
---|
84 | collapseImpl(fold.getChild(i));
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Returns whether a specific fold should be collapsed.
|
---|
91 | *
|
---|
92 | * @param fold The fold to examine.
|
---|
93 | * @return Whether the fold should be collapsed.
|
---|
94 | */
|
---|
95 | public boolean getShouldCollapse(Fold fold) {
|
---|
96 | int type = fold.getFoldType();
|
---|
97 | for (int i=0; i<typesToCollapse.size(); i++) {
|
---|
98 | if (type==((Integer)typesToCollapse.get(i)).intValue()) {
|
---|
99 | return true;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | return false;
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | } |
---|