001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.modules.xml.jaxb.model;
029:
030: import java.util.ArrayList;
031: import java.util.List;
032: import org.netbeans.api.project.Project;
033: import org.netbeans.modules.xml.jaxb.api.model.JAXBWizModel;
034: import org.netbeans.modules.xml.jaxb.api.model.events.JAXBWizEvent;
035: import org.netbeans.modules.xml.jaxb.api.model.events.JAXBWizEventListener;
036: import org.netbeans.modules.xml.jaxb.cfg.schema.Schema;
037: import org.netbeans.modules.xml.jaxb.cfg.schema.Schemas;
038: import org.netbeans.modules.xml.jaxb.model.events.JAXBWizEventImpl;
039: import org.netbeans.modules.xml.jaxb.util.ProjectHelper;
040: import org.openide.filesystems.FileChangeAdapter;
041: import org.openide.filesystems.FileEvent;
042: import org.openide.filesystems.FileObject;
043: import org.openide.util.Exceptions;
044:
045: /**
046: *
047: * @author gpatil
048: */
049: public class JAXBWizModelImpl implements JAXBWizModel {
050: private List<JAXBWizEventListener> listeners = null;
051: private Project project;
052: // Do not need to listen to Dir see #110406
053: //private JaxbCfgChangeListener jaxbListener = new JaxbCfgChangeListener();
054: private boolean swallowCfgFileEditEvent = false;
055:
056: public JAXBWizModelImpl(Project prj) {
057: this .project = prj;
058: // Do not need to listen to Dir see #110406
059: //ProjectHelper.addCfgFileChangeListener(project, jaxbListener);
060: }
061:
062: public synchronized void addJAXBWizEventListener(
063: JAXBWizEventListener listener) {
064: if (this .listeners == null) {
065: this .listeners = new ArrayList<JAXBWizEventListener>();
066: }
067: this .listeners.add(listener);
068: }
069:
070: public synchronized void removeJAXBWizEventListener(
071: JAXBWizEventListener listener) {
072: if (this .listeners != null) {
073: this .listeners.remove(listener);
074: }
075: }
076:
077: public void fireSchemaAddedEvent(Schemas ss, Schema schema) {
078: JAXBWizEvent event = new JAXBWizEventImpl(ss, null, schema,
079: JAXBWizEvent.JAXBWizEventType.EVENT_BINDING_ADDED);
080: dispatchEvent(event);
081: }
082:
083: public void fireSchemaChangedEvent(Schemas ss, Schema oS, Schema nS) {
084: JAXBWizEvent event = new JAXBWizEventImpl(ss, oS, nS,
085: JAXBWizEvent.JAXBWizEventType.EVENT_BINDING_CHANGED);
086: dispatchEvent(event);
087: }
088:
089: public void fireSchemaDeletedEvent(Schemas ss, Schema oS) {
090: JAXBWizEvent event = new JAXBWizEventImpl(ss, oS, null,
091: JAXBWizEvent.JAXBWizEventType.EVENT_BINDING_DELETED);
092: dispatchEvent(event);
093: }
094:
095: public void fireCfgFileEditedEvent(Schemas ss) {
096: JAXBWizEvent event = new JAXBWizEventImpl(ss, null, null,
097: JAXBWizEvent.JAXBWizEventType.EVENT_CFG_FILE_EDITED);
098: dispatchEvent(event);
099: }
100:
101: private void dispatchEvent(JAXBWizEvent event) {
102: List<JAXBWizEventListener> lss = new ArrayList<JAXBWizEventListener>();
103: synchronized (this ) {
104: if (this .listeners != null) {
105: lss.addAll(this .listeners);
106: }
107: }
108: JAXBWizEvent.JAXBWizEventType eventType = event.getEventType();
109:
110: for (JAXBWizEventListener ls : lss) {
111: try {
112: switch (eventType) {
113: case EVENT_BINDING_ADDED:
114: ls.bindingAdded(event);
115: break;
116: case EVENT_BINDING_CHANGED:
117: ls.bindingChanged(event);
118: break;
119: case EVENT_BINDING_DELETED:
120: ls.bindingDeleted(event);
121: break;
122: case EVENT_CFG_FILE_EDITED:
123: ls.configFileEdited(event);
124: break;
125: }
126: } catch (Exception ex) {
127: Exceptions.printStackTrace(ex);
128: }
129: }
130: }
131:
132: public void addSchema(Schema schema) {
133: Schemas scs = ProjectHelper.getXMLBindingSchemas(project);
134: try {
135: synchronized (this ) {
136: this .swallowCfgFileEditEvent = true;
137: }
138: ProjectHelper.addSchema(project, scs, schema);
139: this .fireSchemaAddedEvent(scs, schema);
140: } finally {
141: synchronized (this ) {
142: this .swallowCfgFileEditEvent = false;
143: }
144: }
145: }
146:
147: public void changeSchema(Schema os, Schema ns) {
148: Schemas scs = ProjectHelper.getXMLBindingSchemas(project);
149: try {
150: synchronized (this ) {
151: this .swallowCfgFileEditEvent = true;
152: }
153:
154: ProjectHelper.removeSchema(project, scs, os);
155: ProjectHelper.addSchema(project, scs, ns);
156: this .fireSchemaChangedEvent(scs, os, ns);
157: } finally {
158: synchronized (this ) {
159: this .swallowCfgFileEditEvent = false;
160: }
161: }
162: }
163:
164: public void deleteSchema(Schema schema) {
165: Schemas scs = ProjectHelper.getXMLBindingSchemas(project);
166: try {
167: synchronized (this ) {
168: this .swallowCfgFileEditEvent = true;
169: }
170:
171: ProjectHelper.removeSchema(project, scs, schema);
172: this .fireSchemaDeletedEvent(scs, schema);
173: } finally {
174: synchronized (this ) {
175: this .swallowCfgFileEditEvent = false;
176: }
177: }
178: }
179:
180: private final class JaxbCfgChangeListener extends FileChangeAdapter {
181: private void refreshNodes() {
182: boolean skipEvent = false;
183: synchronized (JAXBWizModelImpl.this ) {
184: skipEvent = JAXBWizModelImpl.this .swallowCfgFileEditEvent;
185: }
186:
187: if (!skipEvent) {
188: Schemas scs = ProjectHelper
189: .getXMLBindingSchemas(project);
190: JAXBWizModelImpl.this .fireCfgFileEditedEvent(scs);
191: }
192:
193: // SwingUtilities.invokeLater(new Runnable() {
194: //
195: // public void run() {
196: // try {
197: // JAXBRootNodeList.this.rootKeys.clear();
198: // fireChange();
199: // } catch (Exception ex) {
200: // logger.log(Level.WARNING, "refreshing root nodes.", ex);
201: // }
202: //
203: // try {
204: // updateKeys();
205: // } catch (Exception ex) {
206: // logger.log(Level.WARNING, "refreshing root nodes.", ex);
207: // }
208: // fireChange();
209: // }
210: // });
211: }
212:
213: public void fileChanged(FileEvent fe) {
214: refreshNodes();
215: }
216:
217: public void fileRenamed(FileEvent fe) {
218: refreshNodes();
219: }
220:
221: public void fileDataCreated(FileEvent fe) {
222: // New file is created, check if config file is created.
223: FileObject fo = ProjectHelper
224: .getFOForBindingConfigFile(project);
225: if ((fo != null) && (fo.isValid())) {
226: // Remove listening on folder, add for the file
227:
228: // Do not need to listen to Dir see #110406
229: //ProjectHelper.removeModelListner(project, jaxbListener);
230: //ProjectHelper.addCfgFileChangeListener(project, jaxbListener);
231: refreshNodes();
232: }
233: }
234:
235: public void fileDeleted(FileEvent fe) {
236: refreshNodes();
237: }
238: }
239: }
|