|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.j2ee.deployment.config; import org.openide.ErrorManager; import java.io.File; import java.io.IOException; import java.util.Iterator; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; import org.openide.loaders.*; import org.openide.nodes.CookieSet; import org.openide.nodes.Node; import org.openide.cookies.OpenCookie; import org.openide.cookies.SaveCookie; import org.openide.util.HelpCtx; import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule; import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; import org.netbeans.modules.j2ee.deployment.impl.*; import org.netbeans.modules.j2ee.deployment.config.ui.*; import org.netbeans.api.project.FileOwnerQuery; import org.netbeans.api.project.Project; import javax.enterprise.deploy.spi.exceptions.ConfigurationException; import org.netbeans.api.xml.cookies.CheckXMLCookie; import org.openide.DialogDisplayer; import org.openide.NotifyDescriptor; import org.openide.cookies.*; import org.openide.filesystems.FileChangeListener; import org.openide.filesystems.FileLock; import org.openide.text.DataEditorSupport; import org.openide.util.WeakListeners; import org.openide.windows.CloneableOpenSupport; import org.openide.windows.TopComponent; import org.netbeans.api.xml.cookies.ValidateXMLCookie; import org.netbeans.spi.xml.cookies.*; import org.xml.sax.InputSource; /** Data object representing a deployment plan file. * Only interesting feature is the {@link OpenCookie} * which lets you open it in graphical editor. * @author Pavel Buzek */ public class ConfigDataObject extends XMLDataObject implements ConfigurationSaver, FileChangeListener, OpenCookie { //PENDING: listen on file change and reload //PENDING: create serialVersionUID // private static final long serialVersionUID = -1073885636989804140L; public static final String J2EE_MODULE_PROVIDER = "module_provider"; //NOI18N private ConfigurationStorage storage; private String relPath; private ServerString serverString; private boolean isEdited = false; private ConfigBeanTopComponent openTc = null; private ValidateXMLCookie validateCookie = null; private CheckXMLCookie checkCookie = null; public ConfigDataObject (FileObject pf, MultiFileLoader loader, String relPath, ServerString serverString) throws DataObjectExistsException { super (pf, loader); this.relPath = relPath; this.serverString = serverString; pf.addFileChangeListener ((FileChangeListener) WeakListeners.create (FileChangeListener.class, this, pf)); } public HelpCtx getHelpCtx () { return new HelpCtx (getPrimaryFile().getName()+"_help"); //NOI18N } protected Node createNodeDelegate () { return new ConfigDataNode (this); } public org.openide.nodes.Node.Cookie getCookie (Class c) { Node.Cookie retValue = null; if (OpenCookie.class.isAssignableFrom (c)) { if (!isEdited) { return this; } else { return null; } } else if (EditCookie.class.isAssignableFrom (c)) { boolean isOpen = openTc != null && openTc.isOpened (); if (!isOpen) { return new XMLEditorSupport(this); } else { return null; } } else if (ConfigurationStorage.class.isAssignableFrom (c)) { try { retValue = getStorage (); } catch (ConfigurationException ex) { ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, ex); } } else if (ValidateXMLCookie.class.isAssignableFrom (c)) { if (validateCookie == null) { InputSource in = DataObjectAdapters.inputSource(this); validateCookie = new ValidateXMLSupport(in); } return validateCookie; } else if (CheckXMLCookie.class.isAssignableFrom (c)) { if (checkCookie == null) { InputSource in = DataObjectAdapters.inputSource(this); checkCookie = new CheckXMLSupport(in); } return checkCookie; } if (retValue == null) { retValue = super.getCookie (c); } return retValue; } private ConfigBeanTopComponent checkOpenTC () { Iterator it = TopComponent.getRegistry().getOpened().iterator(); while (it.hasNext()) { TopComponent tc = (TopComponent) it.next(); if (tc instanceof ConfigBeanTopComponent) { ConfigBeanTopComponent beanTC = (ConfigBeanTopComponent) tc; if (beanTC.isFor(getPrimaryFile())) { if (beanTC.isFor(this)) { return beanTC; } else { beanTC.reset(); } } } } return null; } public void open() { getPrimaryFile ().refresh (); //check for external changes ConfigurationStorage configStorage = (ConfigurationStorage) getCookie (ConfigurationStorage.class); if (configStorage == null) { EditCookie editor = (EditCookie) getCookie(EditCookie.class); if (editor != null) editor.edit(); return; } openTc = checkOpenTC (); if (openTc == null) { openTc = new ConfigBeanTopComponent(this); } openTc.open (); openTc.requestActive (); firePropertyChange (PROP_COOKIE, null, null); } public void editorClosed () { if (openTc != null) { openTc.reset(); } openTc = null; firePropertyChange (PROP_COOKIE, null, null); } private J2eeModuleProvider getProvider () throws IOException, DataObjectNotFoundException { FileObject f = getPrimaryFile (); J2eeModuleProvider provider = null; Project p = FileOwnerQuery.getOwner (f); if (p != null) { provider = (J2eeModuleProvider) p.getLookup ().lookup (J2eeModuleProvider.class); } if (provider == null) { throw new RuntimeException("Failed to get J2EE web module for " + f.getPath ()); //NOI18N } return provider; } private ConfigurationStorage getStorage () throws ConfigurationException { getPrimaryFile ().refresh (); //check for external changes if (storage == null) { try { J2eeModuleProvider provider = getProvider (); if (provider == null) { return null; } storage = new ConfigurationStorage (provider, serverString); storage.setSaver (this); } catch (Exception ex) { throw (ConfigurationException) ErrorManager.getDefault().annotate(new ConfigurationException (ex.getLocalizedMessage ()), ex); } } return storage; } public synchronized void resetStorage() { storage = null; if (openTc != null && openTc.isOpened()) { openTc.close(); } } public static FileObject getRelative (FileObject from, String path) throws IOException { FileObject step = from; //up while (path.startsWith ("..")) { step = step.getParent (); path = path.substring (2); if (path.startsWith ("/")) { path = path.substring (1); } } //down if (path.length () > 0) { step = step.getFileObject (path); } return step; } public static String getRelativePath (FileObject from, FileObject to) { String path = ""; //up while (!FileUtil.isParentOf (from, to) || from.equals (to)) { if (from.equals (to)) { break; } path = path + "../"; from = from.getParent (); } //down if (!from.equals (to)) { path = path + FileUtil.getRelativePath (from, to); } return path; } public void resetChanged () { SaveCookie sc = null; while ((sc = (SaveCookie) getCookie (SaveCookie.class)) != null) { getCookieSet ().remove (sc); } this.setModified(false); } public void setChanged () { addSaveCookie(new S()); setModified(true); } public boolean isModified() { return super.isModified() || getCookie (SaveCookie.class) != null; } public void fileAttributeChanged (org.openide.filesystems.FileAttributeEvent fe) { } public void fileChanged (org.openide.filesystems.FileEvent fe) { //PENDING: also secondary files if (fe.getFile ().equals (getPrimaryFile ())) { storage = null; if (openTc != null) { openTc.refresh(); } } } public void fileDataCreated (org.openide.filesystems.FileEvent fe) { } public void fileDeleted (org.openide.filesystems.FileEvent fe) { } public void fileFolderCreated (org.openide.filesystems.FileEvent fe) { } public void fileRenamed (org.openide.filesystems.FileRenameEvent fe) { } private class S implements SaveCookie { public void save () throws java.io.IOException { if (storage != null) { storage.save (); } resetChanged(); setModified(false); } } private final void addSaveCookie (SaveCookie save) { SaveCookie history; while ((history = (SaveCookie)getCookie(SaveCookie.class)) != null) { removeSaveCookie(history); } getCookieSet ().add (save); } private final void removeSaveCookie (SaveCookie save) { getCookieSet ().remove (save); } private static class XMLEditorSupport extends DataEditorSupport implements EditCookie, EditorCookie.Observable, PrintCookie, CloseCookie { public XMLEditorSupport (XMLDataObject obj) { super (obj, new XMLEditorEnv (obj)); setMIMEType ("text/xml"); // NOI18N } class Save implements SaveCookie { public void save () throws IOException { saveDocument (); ((ConfigDataObject)getDataObject()).resetChanged(); getDataObject ().setModified (false); } } protected boolean notifyModified () { if (! super.notifyModified ()) { return false; } ((ConfigDataObject) getDataObject ()).addSaveCookie (new Save ()); getDataObject ().setModified (true); return true; } protected void notifyUnmodified () { super.notifyUnmodified (); ((ConfigDataObject) getDataObject()).resetChanged(); getDataObject ().setModified (false); } public void edit () { ConfigDataObject cdo = (ConfigDataObject) getDataObject (); cdo.isEdited = true; open (); cdo.firePropertyChange (DataObject.PROP_COOKIE, null, null); } protected void notifyClosed () { ConfigDataObject cdo = (ConfigDataObject) getDataObject (); cdo.isEdited = false; cdo.firePropertyChange (DataObject.PROP_COOKIE, null, null); } //!!! it also stays for SaveCookie however does not understand // encoding declared in XML header => need to be rewritten. private static class XMLEditorEnv extends DataEditorSupport.Env { private static final long serialVersionUID = 6593415381104273008L; public XMLEditorEnv (DataObject obj) { super (obj); } protected FileObject getFile () { return getDataObject ().getPrimaryFile (); } protected FileLock takeLock () throws IOException { return ((ConfigDataObject) getDataObject ()).getPrimaryEntry ().takeLock (); } public CloneableOpenSupport findCloneableOpenSupport () { // must be sync with cookies.add(EditorCookie.class, factory); // #12938 XML files do not persist in Source editor return (CloneableOpenSupport) getDataObject ().getCookie (EditorCookie.class); } } } } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.