/plugins/CandyFolds/trunk/candyfolds/config/ModeConfig.java

# · Java · 160 lines · 121 code · 19 blank · 20 comment · 17 complexity · 155b0d3c9ed2fdc4d6efd63fc4b213a5 MD5 · raw file

  1. /* % [{
  2. % (C) Copyright 2008 Nicolas Carranza and individual contributors.
  3. % See the CandyFolds-copyright.txt file in the CandyFolds distribution for a full
  4. % listing of individual contributors.
  5. %
  6. % This file is part of CandyFolds.
  7. %
  8. % CandyFolds is free software: you can redistribute it and/or modify
  9. % it under the terms of the GNU General Public License as published by
  10. % the Free Software Foundation, either version 3 of the License,
  11. % or (at your option) any later version.
  12. %
  13. % CandyFolds is distributed in the hope that it will be useful,
  14. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. % GNU General Public License for more details.
  17. %
  18. % You should have received a copy of the GNU General Public License
  19. % along with CandyFolds. If not, see <http://www.gnu.org/licenses/>.
  20. % }] */
  21. package candyfolds.config;
  22. import java.io.Writer;
  23. import java.util.ArrayList;
  24. import java.util.Collections;
  25. import java.util.List;
  26. import java.util.Properties;
  27. import javax.swing.text.Segment;
  28. public final class ModeConfig {
  29. public static final int MAX_STRIP_CONFIGS=25;
  30. public final Config config;
  31. public final String name;
  32. private final List<StripConfig> stripConfigs=new ArrayList<StripConfig>();
  33. public final List<StripConfig> stripConfigsA=Collections.unmodifiableList(stripConfigs);
  34. private boolean enabled=true;
  35. private boolean useBigLinesForStripConfigs=true;
  36. private boolean showStripOn0Indent=false;
  37. ModeConfig(Config config, String name) {
  38. this.config=config;
  39. this.name=name;
  40. }
  41. public boolean getEnabled() {
  42. return enabled;
  43. }
  44. public void setEnabled(boolean enabled) {
  45. if(config.defaultModeConfig==this)
  46. return;
  47. this.enabled=enabled;
  48. }
  49. public boolean getUseBigLinesForStripConfigs(){
  50. return useBigLinesForStripConfigs;
  51. }
  52. public void setUseBigLinesForStripConfigs(boolean useBigLinesForStripConfigs){
  53. this.useBigLinesForStripConfigs=useBigLinesForStripConfigs;
  54. }
  55. public boolean getShowStripOn0Indent(){
  56. return showStripOn0Indent;
  57. }
  58. public void setShowStripOn0Indent(boolean showStripOn0Indent){
  59. this.showStripOn0Indent=showStripOn0Indent;
  60. }
  61. public StripConfig addStripConfig() {
  62. if((config.defaultModeConfig==this && !stripConfigs.isEmpty())
  63. || stripConfigs.size()==MAX_STRIP_CONFIGS)
  64. return null;
  65. StripConfig stripConfig=new StripConfig();
  66. stripConfigs.add(stripConfig);
  67. return stripConfig;
  68. }
  69. public void removeStripConfig(int index) {
  70. if(config.defaultModeConfig==this && stripConfigs.size()==1)
  71. return;
  72. stripConfigs.remove(index);
  73. }
  74. public boolean moveUp(int stripConfigIndex) {
  75. if(stripConfigIndex>0 && stripConfigIndex<stripConfigs.size()) {
  76. StripConfig toMove=stripConfigs.remove(stripConfigIndex);
  77. stripConfigs.add(stripConfigIndex-1, toMove);
  78. return true;
  79. }
  80. return false;
  81. }
  82. public boolean moveDown(int stripConfigIndex) {
  83. if(stripConfigIndex>=0 && stripConfigIndex<stripConfigs.size()-1) {
  84. StripConfig toMove=stripConfigs.remove(stripConfigIndex);
  85. stripConfigs.add(stripConfigIndex+1, toMove);
  86. return true;
  87. }
  88. return false;
  89. }
  90. public StripConfig evalStripConfig(Segment segment) {
  91. for(StripConfig stripConfig: stripConfigs) {
  92. if(stripConfig.matches(segment))
  93. return stripConfig;
  94. }
  95. return config.defaultModeConfig.stripConfigs.get(0);
  96. }
  97. void store(Properties ps) {
  98. StringBuilder sb=new StringBuilder();
  99. ps.clear();
  100. ps.setProperty(getPropertyName(sb, "enabled"), String.valueOf(enabled));
  101. ps.setProperty(getPropertyName(sb, "bigLines"), String.valueOf(useBigLinesForStripConfigs));
  102. ps.setProperty(getPropertyName(sb, "showStripOn0Indent"), String.valueOf(showStripOn0Indent));
  103. for(int i=0, size=stripConfigs.size(); i<size && i<MAX_STRIP_CONFIGS; i++) {
  104. stripConfigs.get(i).store(ps, this, sb, i);
  105. }
  106. }
  107. private String getPropertyName(StringBuilder sb, String firstName) {
  108. return getPropertyNameB(sb, firstName).toString();
  109. }
  110. StringBuilder getPropertyNameB(StringBuilder sb, String firstName) {
  111. return getPropertyPrefix(sb).append(firstName);
  112. }
  113. private StringBuilder getPropertyPrefix(StringBuilder sb) {
  114. sb.setLength(0);
  115. sb.append(name);
  116. sb.append(".");
  117. return sb;
  118. }
  119. void load(Properties ps) {
  120. try {
  121. StringBuilder sb=new StringBuilder();
  122. if(this!=config.defaultModeConfig)
  123. setEnabled(Boolean.valueOf(
  124. ps.getProperty(getPropertyName(sb, "enabled"))));
  125. setUseBigLinesForStripConfigs(Boolean.valueOf(
  126. ps.getProperty(getPropertyName(sb, "bigLines"))));
  127. setShowStripOn0Indent(Boolean.valueOf(
  128. ps.getProperty(getPropertyName(sb, "showStripOn0Indent"))));
  129. stripConfigs.clear();
  130. for(int i=0; i<MAX_STRIP_CONFIGS; i++) {
  131. StripConfig stripConfig=new StripConfig();
  132. if(stripConfig.load(ps, this, sb, i)) {
  133. stripConfigs.add(stripConfig);
  134. } else
  135. break;
  136. }
  137. } catch(Exception ex) {
  138. ex.printStackTrace();
  139. }
  140. }
  141. }