alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

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-2002 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.struts;

import java.io.IOException;
import java.util.ResourceBundle;

import org.openide.cookies.*;
import org.openide.filesystems.FileLock;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataObject;
import org.openide.text.DataEditorSupport;
import org.openide.windows.CloneableOpenSupport;
import org.openide.windows.CloneableTopComponent;
import org.openide.text.CloneableEditorSupport;
import org.openide.TopManager;
import org.openide.NotifyDescriptor;
import org.openide.ErrorManager;
import org.openide.util.NbBundle;


/** Support for editing a data object as text.
 *
 * @author mk115033
 */
// Replace OpenCookie with EditCookie or maybe ViewCookie as desired:
public class StrutsEditorSupport extends DataEditorSupport implements EditorCookie, EditCookie, CloseCookie, PrintCookie {
    
    /** Create a new editor support.
     * @param obj the data object whose primary file will be edited as text
     */
    public StrutsEditorSupport(StrutsConfigDataObject obj) {
        super(obj, new StrutsEnv(obj));
        // Set a MIME type as needed, e.g.:
        setMIMEType("text/xml"); // NOI18N
    }
    
    /** Called when the document is modified.
     * Here, adding a save cookie to the object and marking it modified.
     * @return true if the modification is acceptable
     */
    protected boolean notifyModified() {
        if (! super.notifyModified()) {
            return false;
        }
        StrutsConfigDataObject obj = (StrutsConfigDataObject) getDataObject();
        if (obj.getCookie(SaveCookie.class) == null) {
            obj.setModified(true);
            // You must implement this method on the object:
            obj.addSaveCookie(new Save());
        }
        if (!obj.isPanelChanged()) obj.updatePanel();
        return true;
    }
    
    /** Called when the document becomes unmodified.
     * Here, removing the save cookie from the object and marking it unmodified.
     */
    protected void notifyUnmodified() {
        StrutsConfigDataObject obj = (StrutsConfigDataObject) getDataObject();
        SaveCookie save = (SaveCookie) obj.getCookie(SaveCookie.class);
        if (save != null) {
            // You must implement this method on the object:
            obj.removeSaveCookie(save);
            obj.setModified(false);
        }
        super.notifyUnmodified();
    }
    
    /** A save cookie to use for the editor support.
     * When saved, saves the document to disk and marks the object unmodified.
     */
    private class Save implements SaveCookie {
        public void save() throws IOException {
            saveDocument();
            getDataObject().setModified(false);

        }
    }
    /** Should test whether all data is saved, and if not, prompt the user
    * to save.
    *
    * @return true if everything can be closed
    */
    protected boolean canClose () {
        if (isModified ()) {
            NotifyDescriptor nd = new ConfirmDialog(getDataObject());
            Object ret = TopManager.getDefault().notify(nd);

            if (NotifyDescriptor.CANCEL_OPTION.equals(ret)
                    || NotifyDescriptor.CLOSED_OPTION.equals(ret)
               ) {
                return false;
            }
            if (ConfirmDialog.SAVE_OPTION.equals(ret)) {
                try {
                    saveDocument ();
                } catch (IOException e) {
                    ErrorManager.getDefault().notify(e);
                    return false;
                }
            } else { // discard option
                StrutsConfigDataObject obj = (StrutsConfigDataObject) getDataObject();
                obj.discardDocumentChanges();
            }
        }
         
        return true;
    }
    /** Constructs message that should be displayed when the data object
    * is modified and is being closed.
    *
    * @return text to show to the user
    */
    protected String messageSave () {
        return super.messageSave();
    }    
    /** A description of the binding between the editor support and the object.
     * Note this may be serialized as part of the window system and so
     * should be static, and use the transient modifier where needed.
     */
    private static class StrutsEnv extends DataEditorSupport.Env {
        
        //private static final long serialVersionUID = ...L;
        
        /** Create a new environment based on the data object.
         * @param obj the data object to edit
         */
        public StrutsEnv(StrutsConfigDataObject obj) {
            super(obj);
        }
        
        /** Get the file to edit.
         * @return the primary file normally
         */
        protected FileObject getFile() {
            return getDataObject().getPrimaryFile();
        }
        
        /** Lock the file to edit.
         * Should be taken from the file entry if possible, helpful during
         * e.g. deletion of the file.
         * @return a lock on the primary file normally
         * @throws IOException if the lock could not be taken
         */
        protected FileLock takeLock() throws IOException {
            return ((StrutsConfigDataObject) getDataObject()).getPrimaryEntry().takeLock();
        }
        
        /** Find the editor support this environment represents.
         * Note that we have to look it up, as keeping a direct
         * reference would not permit this environment to be serialized.
         * @return the editor support
         */
        public CloneableOpenSupport findCloneableOpenSupport() {
            return (StrutsEditorSupport) getDataObject().getCookie(StrutsEditorSupport.class);
        }
        
    }
    
    static class ConfirmDialog extends NotifyDescriptor {

        static final String SAVE_OPTION = 
            NbBundle.getBundle(CloneableEditorSupport.class).getString("CTL_Save");
        static final String DISCARD_OPTION =
            NbBundle.getBundle(CloneableEditorSupport.class).getString("CTL_Discard");
            
        public ConfirmDialog(Object message, String title, int optionType, int messageType, Object[] options, Object initialValue) {
            super(message, title, optionType, messageType, options, initialValue);
        }
        
        public ConfirmDialog(DataObject obj) {
            super(
                NbBundle.getMessage (
                    CloneableEditorSupport.class,
                    "MSG_SaveFile", // NOI18N
                    obj.getName()
                ),
                NbBundle.getMessage (CloneableEditorSupport.class,"LBL_SaveFile_Title"),
                NotifyDescriptor.YES_NO_CANCEL_OPTION,
                NotifyDescriptor.QUESTION_MESSAGE,
                new Object[] {SAVE_OPTION, DISCARD_OPTION, NotifyDescriptor.CANCEL_OPTION},
                SAVE_OPTION
            );
        }
    }
    
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 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.