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;

// AWT
import java.awt.BorderLayout;

// Beans
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

// I/O
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

// Swing
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.text.StyledDocument;

// Struts Console
import org.apache.struts.console.EditorPanel;
import org.apache.struts.console.jdom.ElementObserver;
import org.apache.struts.console.jdom.JDOMUtil;
import org.apache.struts.console.jdom.ObservableElement;

// JDOM
import org.jdom.Document;
import org.jdom.JDOMException;

// NetBeans
import org.openide.TopManager;
import org.openide.NotifyDescriptor;
import org.openide.cookies.EditorCookie;
import org.openide.cookies.SaveCookie;
import org.openide.filesystems.FileObject;
import org.openide.text.NbDocument;
import org.openide.windows.CloneableTopComponent;
import org.openide.windows.Mode;
import org.openide.windows.Workspace;

// XML
import org.xml.sax.SAXParseException;

public class StrutsConfigPanel extends CloneableTopComponent implements ElementObserver, PropertyChangeListener/*, SaveCookie*/
{
        private static final String ICON_BASE = "/org/netbeans/modules/struts/resources/EditIcon.gif";
	private static final String CARD_EDITOR = "EDITOR";
	private static final String CARD_ERROR = "ERROR";
	private static final String STRUTS_CONSOLE_MODE = "STRUTS_CONSOLE_MODE";

	private EditorPanel editorPanel;
	private StrutsConfigDataObject dataObject;
	private Document document;

	// for externalization only
	public StrutsConfigPanel()
	{
	}
        
        public java.awt.Image getIcon() {
            javax.swing.ImageIcon icon = new javax.swing.ImageIcon(this.getClass().getResource(ICON_BASE));
            return icon.getImage();
        }

	public StrutsConfigPanel(StrutsConfigDataObject dataObject)
	{
		super(dataObject);
		this.dataObject = dataObject;
		init();
	}


	public void writeExternal(ObjectOutput oo)
		throws IOException
	{
		super.writeExternal(oo);
		oo.writeObject(dataObject);
	}


	public void readExternal(ObjectInput oi)
		throws IOException, ClassNotFoundException
	{
		super.readExternal(oi);
		dataObject = (StrutsConfigDataObject) oi.readObject();
		init();
	}


	public void open()
	{
            Workspace ws = TopManager.getDefault().getWindowManager().getCurrentWorkspace();
            Mode mode = ws.findMode(org.openide.text.CloneableEditorSupport.EDITOR_MODE);
            if (mode == null)
            {
                    mode = ws.createMode(STRUTS_CONSOLE_MODE, "Struts Console", null);
            }
            mode.dockInto(this);
            super.open();
	}

	protected boolean closeLast()
	{
		if (dataObject.isModified())
		{
                    NotifyDescriptor nd = new StrutsEditorSupport.ConfirmDialog(dataObject);
                    Object ret = TopManager.getDefault().notify(nd);
			if (ret == NotifyDescriptor.CANCEL_OPTION || ret == NotifyDescriptor.CLOSED_OPTION)
			{
				return (false);
			}
			else if (ret == StrutsEditorSupport.ConfirmDialog.SAVE_OPTION)
			{
				// save changes to data object's underlying file
				try
				{
					((SaveCookie)dataObject.getCookie(SaveCookie.class)).save();
				}
				catch (Exception e)
				{
					e.printStackTrace(System.out);
				}
			}
			else 
			{
				// unset modified flag on data object
				// since user has requested not to save
				// the underlying file
                                dataObject.discardDocumentChanges();
				dataObject.setModified(false);
			}
		}

		// remove save cookie so that save
		// button is no longer enabled
		//dataObject.removeSaveCookie(this);

		return (true);
	}
        
	public void elementChanged(ObservableElement e)
	{
		// mark data object as being modified so that
		// the IDE knows to prompt to save changes
		// when the IDE is shutdown, etc.
                
		dataObject.setModified(true);
                dataObject.setPanelChanged(true);
                dataObject.replaceDocument(document);
	}

        public void updateDocument(Document doc) {

            document=doc;          
            if (!dataObject.getPrimaryFile().isReadOnly())
            {
                    JDOMUtil.registerElementObservers(document, this);
            }
            editorPanel.setDocument(doc);  
        }
        
	private void init()
	{
		// setup panel
		super.setLayout(new BorderLayout());
		editorPanel = new EditorPanel();
                super.add(editorPanel, BorderLayout.CENTER);

		// register to receive data object's property changes
		dataObject.addPropertyChangeListener(this);

		FileObject fo = dataObject.getPrimaryFile();
		try
		{       document = dataObject.getJdomDocument();
                        if (document==null) {
                            document = JDOMUtil.loadDocument(fo.getInputStream(), true);
                            dataObject.setJdomDocument(document);
                        }
			// register to be notified of any changes
			// to the document's elements if the buffer
			// for this file is not read only
			if (!fo.isReadOnly())
			{
				JDOMUtil.registerElementObservers(document, this);
			}
			
			editorPanel.setDocument(document);

			// return to preserved state if available
//			if (lastSelectedNodePath != null)
//			{
//				editorPanel.selectNode(lastSelectedNodePath);
//			}

			//cardLayout.show(cardPanel, CARD_EDITOR);
		}
		catch (Exception e)
		{
			// set the line number that the error occured on
			// so that we can display a button to go right
			// to the error in the source file
			int lineNumber = -1;
			if (e instanceof JDOMException)
			{
                            dataObject.setDocumentValid(false);
                            dataObject.setJdomException((JDOMException)e);
                            dataObject.displayErrorMessage();
                            /*
                            if (((JDOMException) e).getCause() instanceof SAXParseException)
                            {
                                    lineNumber = ((SAXParseException) ((JDOMException) e).getCause()).getLineNumber();
                            }
                            dataObject.displayErrorMessage(lineNumber, (JDOMException) e);
                            */
			}
		}
	}


	// update name of compenent based off of
	// whether or not data object has been modified
	private void updateName()
	{
		String name = dataObject.getNodeDelegate().getDisplayName();
		if (dataObject.isModified())
		{
			// indicate that contents have been modified by
			// placing an asterik next to the component's name
			setName(name + '*');
		}
		else
		{
			setName(name);
		}
	}


	// watch for property changes on data object so that
	// the name for this component can be updated when
	// the underlying file is saved, etc.
	public void propertyChange(PropertyChangeEvent e)
	{
            if (org.openide.loaders.DataObject.PROP_MODIFIED.equals(e.getPropertyName()))
		updateName();
	}
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

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.