001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * Portions Copyrighted 2007 Sun Microsystems, Inc.
016: */
017: package org.netbeans.modules.xslt.mapper.model;
018:
019: import java.awt.event.ActionEvent;
020: import java.awt.event.ActionListener;
021: import org.netbeans.modules.xml.xam.ComponentListener;
022: import java.beans.PropertyChangeEvent;
023: import java.beans.PropertyChangeListener;
024: import java.util.List;
025: import javax.swing.Timer;
026: import javax.swing.JTree;
027: import javax.swing.tree.TreePath;
028: import org.netbeans.modules.xml.axi.AXIComponent;
029: import org.netbeans.modules.xml.xam.ComponentEvent;
030: import org.netbeans.modules.xml.xam.Model;
031: import org.netbeans.modules.xslt.mapper.model.nodes.TreeNode;
032: import org.netbeans.modules.xslt.mapper.view.XsltMapper;
033: import org.netbeans.modules.xslt.model.Stylesheet;
034: import org.netbeans.modules.xslt.model.Template;
035: import org.netbeans.modules.xslt.model.XslModel;
036: import org.openide.util.NbBundle;
037:
038: /**
039: *
040: * @author Alexey
041: */
042: public abstract class ModelBridge implements ComponentListener,
043: PropertyChangeListener {
044:
045: private UpdateTimer updateTimer = new UpdateTimer();
046: private XsltMapper mapper;
047:
048: public ModelBridge(XsltMapper mapper) {
049: this .mapper = mapper;
050: }
051:
052: public XsltMapper getMapper() {
053: return this .mapper;
054: }
055:
056: protected void subscribe(Model model) {
057: if (model != null) {
058: model.addComponentListener(this );
059: model.addPropertyChangeListener(this );
060: }
061: }
062:
063: private class UpdateTimer {
064:
065: private Timer timer;
066:
067: public UpdateTimer() {
068: timer = new Timer(100, new ActionListener() {
069:
070: public void actionPerformed(ActionEvent e) {
071: onModelChanged();
072: }
073: });
074: timer.setRepeats(false);
075: }
076:
077: public void onEvent() {
078: if (timer.isRunning()) {
079: timer.restart();
080: } else {
081: timer.start();
082: }
083: }
084: }
085:
086: /**
087: * This method is called whenever the model is change.
088: */
089: protected abstract void onModelChanged();
090:
091: protected boolean checkErrors() {
092: String errorMessages = "";
093: if (getMapper().getContext() != null) {
094: String valMessage = getMapper().getContext()
095: .getValidationMessage();
096: if (valMessage != null) {
097: errorMessages += valMessage;
098: }
099:
100: XslModel xslModel = getMapper().getContext().getXSLModel();
101: if (xslModel == null
102: || xslModel.getState() != XslModel.State.VALID) {
103: errorMessages += NbBundle.getMessage(
104: XsltModelBridge.class, "MSG_Error_BadXSL"); // NOI18N
105: } else {
106: Stylesheet stylesheet = xslModel.getStylesheet();
107: if (stylesheet == null) {
108: errorMessages += NbBundle.getMessage(
109: XsltModelBridge.class,
110: "MSG_Error_NoStylesheet"); // NOI18N
111: } else {
112: List<Template> templates = stylesheet
113: .getChildren(Template.class);
114: boolean templateFound = false;
115: for (Template t : templates) {
116: if (t.getMatch().equals("/")) {
117: templateFound = true;
118: break;
119: }
120: }
121: //
122: if (!templateFound) {
123: errorMessages += NbBundle.getMessage(
124: XsltModelBridge.class,
125: "MSG_Error_NoRootTemplate"); // NOI18N
126: }
127: }
128: }
129: //
130: AXIComponent typeIn = getMapper().getContext()
131: .getSourceType();
132: if (typeIn == null
133: || typeIn.getModel() == null
134: || typeIn.getModel().getState() != XslModel.State.VALID) {
135: // errorMessages += NbBundle.getMessage(XsltModelBridge.class, "MSG_Error_BadInputSchema"); // NOI18N
136: }
137: AXIComponent typeOut = getMapper().getContext()
138: .getTargetType();
139: if (typeOut == null
140: || typeOut.getModel() == null
141: || typeOut.getModel().getState() != XslModel.State.VALID) {
142: // errorMessages += NbBundle.getMessage(XsltModelBridge.class, "MSG_Error_BadOutputSchema"); // NOI18N
143: }
144: } else {
145: errorMessages += NbBundle.getMessage(XsltModelBridge.class,
146: "MSG_Error_BadXSLTMAP"); // NOI18N
147: }
148:
149: // if (!errorMessages.isEmpty()){
150: if (errorMessages != null && !"".equals(errorMessages)) {
151: // NOI18N
152: mapper.setError(NbBundle.getMessage(XsltModelBridge.class,
153: "MSG_Error_Diagram", errorMessages)); // NOI18N);
154: return false;
155: } else {
156: mapper.setError(null);
157: return true;
158: }
159: }
160:
161: protected void reloadTree(JTree tree) {
162:
163: TreeExpandedState expandedState = new TreeExpandedState(tree);
164: expandedState.save();
165:
166: XsltNodesTreeModel treeModel = (XsltNodesTreeModel) tree
167: .getModel();
168:
169: treeModel.resetRoot();
170:
171: TreeNode treeRoot = (TreeNode) tree.getModel().getRoot();
172:
173: TreePath startFrom_tp = TreeNode.getTreePath(treeRoot);
174:
175: /*
176: * trigger tree reload
177: * TreeNode.reload() implementation shoukd try to keep old nodes as much as possible
178: * to be able to restore selection state and preserve links on diagram
179: */
180:
181: treeRoot.reload();
182:
183: ((XsltNodesTreeModel) tree.getModel())
184: .fireTreeChanged(startFrom_tp);
185:
186: expandedState.restore();
187: }
188:
189: public void valueChanged(ComponentEvent componentEvent) {
190: updateDiagram();
191: }
192:
193: public void childrenAdded(ComponentEvent componentEvent) {
194: updateDiagram();
195: }
196:
197: public void childrenDeleted(ComponentEvent componentEvent) {
198: updateDiagram();
199: }
200:
201: public void propertyChange(PropertyChangeEvent evt) {
202: updateDiagram();
203: }
204:
205: public void updateDiagram() {
206: updateTimer.onEvent();
207: }
208: }
|