KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > axi > impl > ModelAccessImpl


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 package org.netbeans.modules.xml.axi.impl;
20
21 import java.io.IOException JavaDoc;
22 import javax.swing.event.UndoableEditListener JavaDoc;
23 import org.netbeans.modules.xml.axi.AXIModel;
24 import org.netbeans.modules.xml.axi.SchemaGeneratorFactory;
25 import org.netbeans.modules.xml.schema.model.SchemaModel;
26 import org.netbeans.modules.xml.xam.Model;
27 import org.netbeans.modules.xml.xam.ModelAccess;
28
29 /**
30  * ModelAccess implementation.
31  *
32  * @author Samaresh (Samaresh.Panda@Sun.Com)
33  */

34 public class ModelAccessImpl extends ModelAccess {
35     
36     /**
37      * Creates a new instance of ModelAccessImpl
38      */

39     public ModelAccessImpl(AXIModel model) {
40         this.model = (AXIModelImpl)model;
41     }
42         
43     public void addUndoableEditListener(UndoableEditListener JavaDoc listener) {
44         model.addUndoableEditListener(listener);
45     }
46     
47     public void removeUndoableEditListener(UndoableEditListener JavaDoc listener) {
48         model.removeUndoableEditListener(listener);
49     }
50     
51     public void prepareForUndoRedo() {
52     }
53     
54     public void finishUndoRedo() {
55     }
56     
57     private SchemaModel getSchemaModel() {
58     return model.getSchemaModel();
59     }
60     
61     public Model.State sync() throws IOException JavaDoc {
62         //build the ref cache and listens to referenced AXIModels
63
model.buildReferenceableCache();
64         
65         //run the validator
66
if(!model.validate()) {
67             setAutoSync(true);
68             return Model.State.NOT_WELL_FORMED;
69         }
70         
71         //initialize the AXIDocument for the first time.
72
//and sets auto-sync to true.
73
if(!model.isAXIDocumentInitialized()) {
74             model.initializeAXIDocument();
75             setAutoSync(true);
76             return Model.State.VALID;
77         }
78         
79         if(!model.doSync()) {
80             return Model.State.NOT_SYNCED;
81         }
82         
83         //if everythings goes well, return a valid state.
84
return Model.State.VALID;
85     }
86         
87     public void flush() {
88         try {
89             SchemaGeneratorFactory sgf = SchemaGeneratorFactory.getDefault();
90             sgf.updateSchema(model.getSchemaModel(), model.getSchemaDesignPattern());
91         } catch (Exception JavaDoc ex) {
92             throw new IllegalArgumentException JavaDoc("Exception during flush: ",ex); //NOI18N
93
} finally {
94             model.getPropertyChangeListener().clearEvents();
95         }
96     }
97     
98     /**
99      * Returns length in milliseconds since last edit if the model source buffer
100      * is dirty, or 0 if the model source is not dirty.
101      */

102     private long dirtyTimeMillis = 0;
103     public long dirtyIntervalMillis() {
104         if (dirtyTimeMillis == 0) return 0;
105         return System.currentTimeMillis() - dirtyTimeMillis;
106     }
107     
108     public void setDirty() {
109         dirtyTimeMillis = System.currentTimeMillis();
110     }
111     
112     public void unsetDirty() {
113         dirtyTimeMillis = 0;
114     }
115     
116     private AXIModelImpl model;
117 }
118
Popular Tags