# PropertyListenerAction.java Sample

This topic inludes the source code for the PropertyListenerAction.java Sample.

## Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/ExtensionDevKit/IdeDevKit/PropertyListener/src/ideExtensions/propertyListener/

## Sample Source Code

`001 package ideExtensions.propertyListener; 002 003 import com.bea.ide.Application; 004 import com.bea.ide.actions.DefaultAction; 005 import com.bea.ide.core.LookAndFeelConstants; 006 import com.bea.ide.document.IDocument; 007 import com.bea.ide.ui.IURISelectionContext; 008 import com.bea.ide.ui.output.OutputMessage; 009 import com.bea.ide.ui.output.OutputSvc; 010 import java.awt.Color; 011 import java.awt.event.ActionEvent; 012 import java.beans.PropertyChangeEvent; 013 import java.beans.PropertyChangeListener; 014 import java.net.URI; 015 import javax.swing.UIManager; 016 017 /** 018 * An action class that listens for changes to properties defined by WebLogic 019 * Workshop. 020 */ 021 public class PropertyListenerAction extends DefaultAction 022 { 023 // A window for displaying messages. 024 private OutputSvc.IOutputWindow m_win; 025 // Descriptive information about the window. 026 private OutputSvc.OutputWindowDescription m_desc; 027 028 /** 029 * Constructs an instance of this action class, initializing 030 * an output window for messages and adding listeners for property 031 * changes. 032 */ 033 public PropertyListenerAction() 034 { 035 // Initialize the output window. 036 m_desc = new OutputSvc.OutputWindowDescription(); 037 m_desc.title = "PropertyListener"; 038 m_desc.destination = "PropertyListener"; 039 m_win = OutputSvc.get().getWindow(m_desc, true, false); 040 041 // Add a listener that will receive an event if a file is edited or saved. 042 Application.get().addPropertyChangeListener(Application.EVENT_DocumentDirtyChange, m_eventListener); 043 // Add a listener that will receive an event if the URI of the selection changes. 044 Application.get().addPropertyChangeListener(Application.PROP_FocusedURIs, m_eventListener); 045 } 046 047 /** 048 * Called by the IDE when the Properties -> Property Listender 049 * menu command is clicked. 050 * 051 * @param e The menu command click event. 052 */ 053 public void actionPerformed(ActionEvent e) 054 { 055 // Display a window for messages. 056 m_win = OutputSvc.get().getWindow(m_desc, true, false); 057 } 058 059 /** 060 * Creates a PropertyChangeListener instance whose propertyChange method 061 * handles changes to URI focus and document dirty state. 062 */ 063 private PropertyChangeListener m_eventListener = new PropertyChangeListener() 064 { 065 /** 066 * Called by the IDE when an event occurs for which this listener is 067 * registered. 068 * 069 * @param e The change event that occurred. 070 */ 071 public void propertyChange(PropertyChangeEvent e) 072 { 073 String propertyName = e.getPropertyName(); 074 Object oldValueObj = e.getOldValue(); 075 Object newValueObj = e.getNewValue(); 076 077 /** 078 * If the property change event is due to a change in selection context 079 * (such as a change in which file is selected in the Application 080 * window), then print a message describing what is selected. 081 */ 082 if (propertyName.equals(Application.PROP_FocusedURIs)) 083 { 084 if(newValueObj != null && newValueObj instanceof IURISelectionContext) 085 { 086 IURISelectionContext c = (IURISelectionContext)newValueObj; 087 // Get the URIs for selected items. 088 URI[] uris = c.getFileURIs(); 089 if(uris.length > 0) 090 { 091 m_win.addMessage("The following files are selected in the Application tree:"); 092 // For each URI, print the URI to the window. 093 for(int i = 0; i < uris.length; i++) 094 { 095 // Display a message describing the current selected context. 096 m_win.addMessage(new OutputMessage(getAppRelativePath(uris[i]), 097 UIManager.getColor(LookAndFeelConstants.ALERT_COLOR), null)); 098 } 099 m_win.addMessage(""); 100 } 101 } 102 } 103 /** 104 * If the property change event is due to the open document getting edited 105 * (ie, becoming "dirty") or saved, then print a message describing 106 * what happened. 107 */ 108 if (propertyName.equals(Application.EVENT_DocumentDirtyChange)) 109 { 110 if(newValueObj != null && newValueObj instanceof IDocument) 111 { 112 IDocument doc = (IDocument)newValueObj; 113 // Find out if the document has been dirtied (true) or saved (false). 114 Boolean dirty =(Boolean)doc.getProperty(IDocument.PROP_DocumentDirty); 115 // Display a message about the document's "dirty state". 116 m_win.addMessage("The following document was just " + 117 (dirty.booleanValue() ? "dirtied" : "saved") + ":"); 118 // Display the document's URI. 119 m_win.addMessage(new OutputMessage(getAppRelativePath(doc.getURI()), 120 UIManager.getColor(LookAndFeelConstants.ALERT_COLOR), null)); 121 m_win.addMessage(""); 122 } 123 } 124 } 125 }; 126 /** 127 * Gets the path of <em>uri</em> relative to the 128 * application root. This assumes that the URI corresponds 129 * to a file. 130 * 131 * @param uri A URI corresponding to the file whose path 132 * should be retrieved. 133 * @return The relative path. 134 */ 135 private String getAppRelativePath(URI uri) 136 { 137 if(Application.getWorkspace() == null) { return ""; } 138 URI appURI = Application.getWorkspace().getDirectory().getAbsoluteURI(); 139 return "$APP_HOME/" + uri.toString().substring(appURI.toString().length()); 140 } 141 }`