KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > properties > StructHandler


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.properties;
22
23
24 import java.io.IOException JavaDoc;
25 import java.lang.ref.SoftReference JavaDoc;
26 import java.lang.ref.WeakReference JavaDoc;
27
28 import org.openide.util.RequestProcessor.Task;
29
30
31 /**
32  * Class for handling structure of a single <tt>.properties</tt> file.
33  *
34  * @author Petr Hamernik, Petr Jiricka
35  * @see PropertiesStructure
36  */

37 public class StructHandler {
38
39     /** Appropriate properties file entry. */
40     private PropertiesFileEntry propFileEntry;
41
42     /** Weak reference to parsing task. */
43     private WeakReference JavaDoc<Task> parsingTaskWRef
44             = new WeakReference JavaDoc<Task>(null);
45
46     /** Soft reference to the underlying properties structure. */
47     private SoftReference JavaDoc<PropertiesStructure> propStructureSRef
48             = new SoftReference JavaDoc<PropertiesStructure>(null);
49
50     /** Parser performing actual parsing task. */
51     private WeakReference JavaDoc<PropertiesParser> parserWRef
52             = new WeakReference JavaDoc<PropertiesParser>(null);
53     
54     /** Flag indicating if parsing isAllowed. */
55     private boolean parsingAllowed = true;
56
57     /** Generated serialized version UID. */
58     static final long serialVersionUID = -3367087822606643886L;
59
60     
61     /**
62      * Creates a new handler for a given data object entry.
63      *
64      * @param propFileEntry entry to create a handler for
65      */

66     public StructHandler(PropertiesFileEntry propFileEntry) {
67         this.propFileEntry = propFileEntry;
68     }
69
70     
71     /** Reparses file. Fires changes. */
72     PropertiesStructure reparseNowBlocking() {
73         return reparseNowBlocking(true);
74     }
75     
76     /**
77      * Reparses file.
78      *
79      * @param fire true if should fire changes
80      */

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 JavaDoc<PropertiesParser>(parser);
90
91             parser.initParser();
92             PropertiesStructure propStructure = parser.parseFile();
93             
94             updatePropertiesStructure(propStructure, fire);
95             
96             return propStructure;
97         } catch (IOException JavaDoc ioe) {
98             updatePropertiesStructure(null, fire);
99             
100             return null;
101         } finally {
102             parser.clean();
103         }
104     }
105     
106     /**
107      * Stops parsing and prevent any other sheduled ones.
108      * File object is going to be deleted. Due actual delete or move operation.
109      */

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     /**
121      * Allows parsing when error during deleting or moving occured and
122      * the operation didn't succed.
123      */

124     synchronized void allowParsing() {
125         parsingAllowed = true;
126     }
127
128     /** Getter for <code>propFileEntry</code> property. */
129     public PropertiesFileEntry getEntry() {
130         return propFileEntry;
131     }
132
133     /**
134      * Starts parsing task. Tries to cancel previous parsing task if
135      * it is not running yet. Never create new structure if it does not
136      * exist.
137      */

138     void autoParse() {
139
140         if (false == isStructureLoaded()) {
141             return;
142         }
143         Task previousTask = parsingTaskWRef.get();
144         if (previousTask != null) {
145             // There was previous task already, reschedule it 500 ms later.
146
previousTask.schedule(500);
147         } else {
148             // Create a new task, and schedule it immediatelly.
149
parsingTaskWRef = new WeakReference JavaDoc<Task>(
150                 PropertiesRequestProcessor.getInstance().post(
151                     new Runnable JavaDoc() {
152                         public void run() {
153                             reparseNowBlocking();
154                         }
155                     }
156                 )
157             );
158         }
159     }
160
161     /**
162      * When the parser finishes its job, it calls this method to set new values.
163      *
164      * @param newPropStructure new properties structure
165      * @param fire if should fire change when structure created anew
166      */

167     private void updatePropertiesStructure(PropertiesStructure newPropStructure,
168                                            boolean fire) {
169         if (newPropStructure == null) {
170             propStructureSRef = new SoftReference JavaDoc<PropertiesStructure>(null);
171             return;
172         }
173         
174         PropertiesStructure propStructure = propStructureSRef.get();
175
176         if (propStructure == null) {
177             // Set the parent.
178
newPropStructure.setParent(this);
179             propStructure = newPropStructure;
180             propStructureSRef = new SoftReference JavaDoc<PropertiesStructure>(propStructure);
181             
182             if (fire) {
183                 propStructure.structureChanged();
184             }
185         } else {
186             // Update calls notification methods according to changes.
187
propStructure.update(newPropStructure);
188         }
189     }
190
191     /** Gets properties structure handled by this handler. */
192     public PropertiesStructure getStructure() {
193         PropertiesStructure propStructure = propStructureSRef.get();
194         
195         if (propStructure != null) {
196             return propStructure;
197         }
198         // No data available -> reparse file.
199
// PENDING don't send change event when requesting data only.
200
// They could be garbaged before so no fire changes.
201
return reparseNowBlocking(false);
202     }
203
204     /**
205      * Determine wheteher somebody have already asked for the model.
206      */

207     private boolean isStructureLoaded() {
208         return propStructureSRef.get() != null;
209     }
210 }
Popular Tags