PhpProjectListener.java Sample

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

Sample Location

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

BEA_HOME/weblogic81/samples/workshop/ExtensionDevKit/IdeDevKit/CustomProject/src/ideExtensions/customProject/

Sample Source Code


001 package ideExtensions.customProject; 
002 
003 import com.bea.ide.Application;
004 import com.bea.ide.core.MessageSvc;
005 import com.bea.ide.filesystem.IFile;
006 import com.bea.ide.util.swing.DialogUtil;
007 import com.bea.ide.workspace.IProject;
008 import com.bea.ide.workspace.IWorkspace;
009 import com.bea.ide.workspace.project.IProjectInfo;
010 import java.beans.PropertyChangeEvent;
011 import java.beans.PropertyChangeListener;
012 import javax.swing.JLabel;
013 import javax.swing.JPanel;
014 import javax.swing.JTextArea;
015 
016 /**
017  * A property change listener to listen for IDE startup
018  * and display a message about PHP projects in the application.
019  
020  * Note that this listener will only be used when the IDE has just
021  * finished starting up, and its message will only be displayed if 
022  * there is at least one PHP project in the application. If you 
023  * have not yet done so, the easiest way to execute this code
024  * is to begin debugging the CustomProject application. That will
025  * build the extension and start a new instance of the IDE with
026  * the extension added.
027  */
028 public class PhpProjectListener implements PropertyChangeListener
029 
030     IProject m_phpProject = null;
031     
032     public PhpProjectListener (IProject phpProject)
033     {
034         m_phpProject = phpProject;
035     }    
036     
037     /**
038      * Called by the IDE on changes to properties with which this
039      * listener is associated. The PhpProjectDriver associates
040      * this listener with changes to the Application.PROP_InitLevel
041      * property. That property's value changes to track the 
042      * IDE's startup progress. 
043      
044      * If the property's new value is Application.INIT_StartupComplete,
045      * indicating that the IDE has finished starting up and is ready
046      * for use, this listener will display an informational dialog
047      * listing the contents of the PHP projects in this application.
048      * The dialog will be displayed once for each project of the PHP
049      * type in the application -- each, after all, has its own instance
050      * of the driver by which this listener was created.
051      
052      * Needless to say, this would be annoying in a real-world
053      * scenario, but it suggests the potential of a listener.
054      
055      @param event The event with which this listener is
056      * associated.
057      */
058     public void propertyChange(PropertyChangeEvent event)
059     {
060         IWorkspace workspace = Application.getWorkspace();
061 
062         /**
063          * If the received event is "startup complete", put together
064          * a message and display it in a dialog.
065          */
066         if (event.getNewValue().equals(Application.INIT_StartupComplete))
067         {
068             /**
069              * Get an array of projects whose types have an extension.xml 
070              * containing an <attribute> element whose name attribute value is
071              * "supportsPhp". Note that the IProjectInfo interface provides
072              * other methods for retrieving the contents of the extension.xml
073              * file for the project type.
074              */ 
075             IProject[] phpProjects = workspace.getProjects("supportsPhp");
076 
077             // A string buffer to contain an assembled message.            
078             StringBuffer messageBuffer = new StringBuffer();
079 
080             // Add the message's introduction.
081             messageBuffer.append("Information about a PHP project in this application: \n\n");
082             
083             /**
084              * Get the IProjectInfo for this project. An instance of this
085              * interface contains information stored in the extension.xml
086              * for the project type.
087              */
088             IProjectInfo projectInfo = m_phpProject.getProjectInfo();
089 
090             // Get the type's URN.
091             String typeId = projectInfo.getTypeId();
092             
093             /**
094              * Confirm that the project really is a PHP project as 
095              * specified by the project type URN given in the project 
096              * type's extension.xml file. This is unnecessary because this 
097              * listener is only associated with PHP projects by the project
098              * driver, but the code shows how you can get the ID.
099              */
100             if (typeId.equals("urn:com-bea-ide:project.type:php"))
101             {                
102                 // Collect the project's name, path, and contents.
103                 String projectName = m_phpProject.getName();
104                 String projectPath = m_phpProject.getPath();
105                 IFile projectIFile = m_phpProject.getIFile();
106                 IFile[] projectIFiles = projectIFile.listIFiles();
107 
108                 // Put project info into the message.
109                 messageBuffer.append("Name: " + projectName + "\n");
110                 messageBuffer.append("Path: " + projectPath + "\n");
111                 if (projectIFiles.length > 0)
112                 {
113                     messageBuffer.append("Contains these directories and files:\n");
114 
115                     // Add to the message the name of each file in the project.
116                     for (int j = 0; j < projectIFiles.length; j++)
117                     {
118                         messageBuffer.append("  " + projectIFiles[j].getName() "\n");
119                     }
120                 }
121                 messageBuffer.append("\n");
122             }
123             
124             // Show the collected information.
125             DialogUtil.showInfoDialog(Application.getRootFrame(), messageBuffer.toString());
126         }
127     }
128