1 19 20 21 package org.netbeans.modules.properties; 22 23 24 import java.io.IOException ; 25 import java.lang.ref.SoftReference ; 26 import java.lang.ref.WeakReference ; 27 28 import org.openide.util.RequestProcessor.Task; 29 30 31 37 public class StructHandler { 38 39 40 private PropertiesFileEntry propFileEntry; 41 42 43 private WeakReference <Task> parsingTaskWRef 44 = new WeakReference <Task>(null); 45 46 47 private SoftReference <PropertiesStructure> propStructureSRef 48 = new SoftReference <PropertiesStructure>(null); 49 50 51 private WeakReference <PropertiesParser> parserWRef 52 = new WeakReference <PropertiesParser>(null); 53 54 55 private boolean parsingAllowed = true; 56 57 58 static final long serialVersionUID = -3367087822606643886L; 59 60 61 66 public StructHandler(PropertiesFileEntry propFileEntry) { 67 this.propFileEntry = propFileEntry; 68 } 69 70 71 72 PropertiesStructure reparseNowBlocking() { 73 return reparseNowBlocking(true); 74 } 75 76 81 private synchronized PropertiesStructure reparseNowBlocking(boolean fire) { 82 if (!parsingAllowed) { 83 return null; 84 } 85 86 PropertiesParser parser = new PropertiesParser(propFileEntry); 87 88 try { 89 parserWRef = new WeakReference <PropertiesParser>(parser); 90 91 parser.initParser(); 92 PropertiesStructure propStructure = parser.parseFile(); 93 94 updatePropertiesStructure(propStructure, fire); 95 96 return propStructure; 97 } catch (IOException ioe) { 98 updatePropertiesStructure(null, fire); 99 100 return null; 101 } finally { 102 parser.clean(); 103 } 104 } 105 106 110 synchronized void stopParsing() { 111 parsingAllowed = false; 112 113 PropertiesParser parser = parserWRef.get(); 114 115 if (parser != null) { 116 parser.stop(); 117 } 118 } 119 120 124 synchronized void allowParsing() { 125 parsingAllowed = true; 126 } 127 128 129 public PropertiesFileEntry getEntry() { 130 return propFileEntry; 131 } 132 133 138 void autoParse() { 139 140 if (false == isStructureLoaded()) { 141 return; 142 } 143 Task previousTask = parsingTaskWRef.get(); 144 if (previousTask != null) { 145 previousTask.schedule(500); 147 } else { 148 parsingTaskWRef = new WeakReference <Task>( 150 PropertiesRequestProcessor.getInstance().post( 151 new Runnable () { 152 public void run() { 153 reparseNowBlocking(); 154 } 155 } 156 ) 157 ); 158 } 159 } 160 161 167 private void updatePropertiesStructure(PropertiesStructure newPropStructure, 168 boolean fire) { 169 if (newPropStructure == null) { 170 propStructureSRef = new SoftReference <PropertiesStructure>(null); 171 return; 172 } 173 174 PropertiesStructure propStructure = propStructureSRef.get(); 175 176 if (propStructure == null) { 177 newPropStructure.setParent(this); 179 propStructure = newPropStructure; 180 propStructureSRef = new SoftReference <PropertiesStructure>(propStructure); 181 182 if (fire) { 183 propStructure.structureChanged(); 184 } 185 } else { 186 propStructure.update(newPropStructure); 188 } 189 } 190 191 192 public PropertiesStructure getStructure() { 193 PropertiesStructure propStructure = propStructureSRef.get(); 194 195 if (propStructure != null) { 196 return propStructure; 197 } 198 return reparseNowBlocking(false); 202 } 203 204 207 private boolean isStructureLoaded() { 208 return propStructureSRef.get() != null; 209 } 210 } | Popular Tags |