Showing posts with label ide for windows. Show all posts
Showing posts with label ide for windows. Show all posts

Thursday, 10 November 2011

Automatic window lifecycle management


A further major advantage of the NetBeans Window System is that it provides a WindowManager that controls the lifecycle of all the windows defined in the application. The WindowManager notifies allTopComponents about state changes using callback methods, listed as follows:

componentOpened()It is called after the TopComponent has been opened. If multiple TopComponents are opened into the same position (called mode), the NetBeans Window System uses a tabbed container, with one TopComponent per tab. Of all available TopComponents found within a shared tabbed container, only the content of the selected TopComponent is visible.
componentShowing()It notifies the component that its content is now visible. This TopComponent is now either selected or is the only component in a separate container.
componentActivated()It is called after the TopComponent has gained the input focus or has become the selected component.
componentDeactivated()It is called after the TopComponent has lost the input focus.
componentHidden()It notifies the TopComponent that its content is no longer visible.
componentClosed()It is called after the TopComponent has been closed.
Let's illustrate this lifecycle via an example that logs all the callback methods to the output window of NetBeans IDE, which is your development environment on top of the NetBeans Platform.
  1. Create a new NetBeans Platform application and name it WindowSystemExamples.
  2. Add a new module named LifeCycle, with Code Name Base com.netbeansrcp.lifecycle.
  3. Add a TopComponent, with Class Name Prefix prefix LifeCycleDemo, making sure to indicate that it should be automatically opened in the editor area at application startup.
  4. Override the lifecycle methods as follows:
    public void componentOpened() { super.componentOpened(); System.out.println("componentOpened()"); } protected void componentShowing() { super.componentShowing(); System.out.println("componentShowing()"); } protected void componentActivated() { super.componentActivated(); System.out.println("componentActivated()"); } @Override protected void componentDeactivated() { super.componentDeactivated(); System.out.println("componentDeactivated()"); } @Override protected void componentHidden() { super.componentHidden(); System.out.println("componentHidden()"); } @Override public void componentClosed() { super.componentClosed(); System.out.println("componentClosed()"); }
Start the new application. The TopComponent LifeCyleDemoTopComponent is automatically opened at startup. Select the TopComponent and inspect the output in the NetBeans IDE Output Window. You should see the following:
componentOpened() componentShowing() componentActivated()
The TopComponent has passed the first half of its lifecycle and is now activated.
Close the LifeCyleDemoTopComponent and inspect the output again, to understand the second half of the TopComponent lifecycle. You should see the following output:
componentHidden() componentDeactivated() componentClosed()
You have learned that the TopComponent's lifecycle is automatically controlled by the NetBeans Platform. Between the start and the end of the TopComponent's lifecycle are six different states, all managed by the NetBeans Platform, and with notifications sent via callbacks.

Programmatically managing the Window lifecycle

You can manage the lifecycle of a TopComponent programmatically. For this purpose, theTopComponent provides the following methods:
  • open(): Opens the TopComponent
  • requestVisible(): Requests to select to TopComponent
  • requestActive(): Requests to transfer the input focus to the TopComponent
Let's now modify the LifeCycleDemoTopComponent for demonstration purposes.
  1. Add a JButton to the TopComponent, as follows:
  2. Implement an ActionEventListener as follows:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { this.close(); RequestProcessor.getDefault().post(new java.lang.Runnable() { public void run() { java.awt.EventQueue.invokeLater(new java.lang.Runnable() { public void run() { com.netbeansrcp.lifecycle.LifeCycleDemoTopComponent.this.open(); } }); } }, 3000); RequestProcessor.getDefault().post(new java.lang.Runnable() { public void run() { java.awt.EventQueue.invokeLater(new java.lang.Runnable() { public void run() { com.netbeansrcp.lifecycle. LifeCycleDemoTopComponent.this.requestActive(); } }); } }, 6000); }
  3. Restart the application. When you click the button, the LifecycleDemoTopComponent is closed via the close() method, called above in the first line of the code you entered.
The RequestProcessor provides a thread pool. The default instance of this pool lets you execute aRunnable after a short delay, thanks to the post() method. As the TopComponent's lifecycle method should be called from the AWT event thread, you do not call them directly in the run() method, but by posting a new Runnable to the EventQueue, which in the end calls the Window System API methods.
The argument 3000 ensures that the execution of the Runnable is delayed for 3000 ms so that theTopComponent is opened again after 3 s.
After six seconds the second Runnable posted to the EventQueue is executed and requestActive() is called for your LifecycleDemoTopComponent. Your TopComponent is now shown in the foreground, if it had been behind other windows previously.
You have learned how to manage the lifecycle of a TopComponent. Via the example you have seen how to open a TopComponent and make it focusable.

Positioning of windows

The NetBeans Window System divides the available space in the main window into areas that are called "modes". Each mode represents a specific area of the main window, providing a container for windows in a predefined position in the frame. You can add windows, that is, TopComponents, to a mode, either declaratively or programmatically.
A standard layout is provided by the NetBeans Window System, corresponding to the layout of NetBeans IDE. For example, the predefined modes correspond to the names used in the corresponding positions in NetBeans IDE, such as "editor" and "explorer". If needed, you can define your own modes, too. No wizard is provided for this purpose in NetBeans IDE, so you need to create the mode definition files yourself manually.
In the previous section you learned about the layer.xml file. To create a default layout for an application, each TopComponent needs to be declaratively registered within the Windows2 | Modes folder, within a subfolder named after the mode in which the TopComponent should be docked.
To demonstrate declarative registration of TopComponents, edit the layer.xml in the LifeCycle module, changing the folder name Windows2 | Modes | editor to Windows | Modes | rightSlidingSide, as shown in the following code snippet:
layer.xml // <folder name="Windows2"> <folder name="Components"> <file name="LifecycleDemoTopComponent.settings" url="LifecycleDemoTopComponentSettings.xml"/> </folder> <folder name="Modes"> <folder name="rightSlidingSide"> <file name="LifecycleDemoTopComponent.wstcref" url="LifecycleDemoTopComponentWstcref.xml"/> </folder> </folder> </folder> //
Select Clean and Build on the application node in the Projects window, to remove the build folder containing the last used window layout, and start the application again. When the application starts up, notice that the LifecycleDemoTopComponent is not opened in the editor mode. Instead, it is represented by a button on the right sidebar of the application (as shown in the screenshot below). That is therightSlidingSide mode, providing a container for minimized windows.
As you have seen, providing a default layout via declarative registrations of TopComponents is rather easy. You only need to create an entry in the layer.xml for the TopComponent, in a folder with the name of the desired mode, within the Windows2 | Modes folder.
Sometimes declarative registration alone is too static for your business needs. Fortunately, positioning ofTopComponents can also be done programmatically. In the next example, you create a TopComponentthat moves to new modes via a click of a button.
  1. Add to the WindowSystemExamples application a new module named Modes, with the Code Name Base com.netbeansrcp.modes.
  2. Within the module, create a TopComponent called ModesDemo, which is opened when the application starts into the "editor" mode.
Add two JButtons to the TopComponent with the texts Back and Forward, as well as a JLabel with an empty initial text. The TopComponent should look as shown in the following screenshot:
In the Source view, add the following code:
private static final String[] MODES = new String[] { "properties", "commonpalette", "rightSlidingSide", "bottomSlidingSide", "output", "debugger", "navigator", "explorer", "leftSlidingSide", "editor" }; private void changeMode(boolean next) { Mode currentMode = WindowManager.getDefault().findMode(this); String currentModeName = currentMode.getName(); String nextModeName = "editor"; for (int i = 0; i < MODES.length; i++) { String modeName = MODES[i]; if (modeName.equals(currentModeName)) { if (next) { nextModeName = (i + 1 < MODES.length) ? MODES[i + 1] : MODES[0]; } else { nextModeName = (i - 1 >= 0) ? MODES[i - 1] : MODES[MODES.length - 1]; } break; } } Mode nextMode = WindowManager.getDefault().findMode(nextModeName); if (nextMode != null) { this.jLabel1.setText(nextModeName); nextMode.dockInto(this); this.open(); this.requestActive(); } } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { this.changeMode(true); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { this.changeMode(false); }
The static string array contains the names of the most important modes. These modes can also be identified dynamically, by calling WindowManager.getDefault().getModes().
The ActionListener delegates the call to the method changeMode() and gives the desired back/forward direction. This method determines via WindowManager.getDefault ().findMode (this) the mode in which theTopComponent is displayed, as well as the name of the current mode.
The string array is then searched and dockInto(this) is called to dock the TopComponent into a different mode.
How to revert to the default layout?
As the layout of the NetBeans Platform is persisted when the application shuts down, first perform a Clean and Build on the application project. With the removal of the build folder, the layout settings are also deleted, so that the default layout is used when the application starts again.
Using the two buttons, you can let the TopComponent be docked in some of the most commonly used modes (as shown in the screenshot below).
You have programmatically docked a TopComponent into various places within the available modes in the application. In the process, you have learned how TopComponents can be docked dynamically, that is, at runtime, into desired positions. Both the declarative and the programmatic approaches to docking should now be familiar to you.

Environment for PHP Development in Window


This tutorial shows you two ways of configuring your PHP development environment on the Windows operating system. The first and most convenient way is to install and configure an AMP (Apache, MySQL, PHP) package


Using XAMP Package



Apache HTTP Server
  1. Download the Apache2 HTTP server.
  2. Run the installation file .msi. The installation wizard starts. Follow the instructions.
  3. When the installation is completed, restart the Apache server.
  4. To check that the installation is successful, run the browser and enter http://localhost/ in browser
    The welcome test page should open: 

Troubleshoot

By default, the Apache server listens to port 80. This port can be already used by other services, for example Skype. To solve the issue, change the port which the server listens to:
  1. Open the Apache web server configuration file httpd.conf. By default the file is located in C:\Program Files\Apache Software Foundation\Apache<version>\conf\
  2. Locate the line Listen 80 and change the port number, for example 8080. Save the file.
  3. Restart the Apache web server.
  4. To check that the web server works, run the browser and enter the URL and specify the port number explicitly: http://localhost:8080

PHP Engine

  1. Download the PHP5 engine.
  2. When the download is complete, run the .msi installation file. The installation wizard starts.
  3. On the Apache Configuration Directory panel, specify the directory where the httpd.conf file is located, the default setting is C:\Program Files\Apache Software Foundation\Apache<version>\conf\. The PHP processing will be enabled automatically.
  4. If you want to use the MySQL database server, choose the Complete installation option or select the MySQL and MySQLi items in the Extensions list.
  5. After the installation is completed, restart the Apache server.
  6. To check that the PHP engine has been installed successfully and PHP processing has been enabled in the Apache configuration:
    • In the Notepad, create a file and type the following text:
    • <?php 
           echo "PHP has been installed successfully!";
      ?>
    • Save the file in the htdocs folder: C:\Program Files\Apache Software Foundation\Apache<version>\htdocs\test.php
    • Run the browser and enter the following URL: http://localhost:<port>/test.php. The following message should appear "PHP has been successfully installed"

Troubleshoot

If the page does not open:
  1. Restart the Apache server.
  2. Check that the Apache server configuration file httpd.conf contains the following lines:
      AddType Application/x-httpd-php .php 
      LoadModule php5_module "c:/php/sapi/php5apache2_2.dll"
  3. If the lines are missing, add them, save httpd.conf, and restart Apache.
  4. Refresh the http://localhost:<port>/test.php page.

Lastly

  1. Download XDebug.
  2. Install XDebug into the php/ folder. 

Set Up the Environment

  1. With default settings, PHP processing will be enabled automatically.
  2. To attach XDebug to the PHP engine, find the php.ini file and add the following lines to it:
    For a thread-safe PHP 5.2 engine:
  3. zend_extension_ts="<path to the php folder>/php_xdebug-<version-number>.dll"
    xdebug.remote_enable=1
    For a non-thread-safe PHP 5.2 engine:
    zend_extension_nts="<path to the php folder>/php_xdebug-<version-number>.dll"
    xdebug.remote_enable=1
    For any PHP 5.3 engine:
    zend_extension="<path to the php folder>/php_xdebug-<version-number>.dll"
    xdebug.remote_enable=1
    Some users also find that they need to include the following lines, although other users do not:
    xdebug.remote_host=127.0.0.1
    xdebug.remote_port=9000
    ; Port number must match debugger port number in NetBeans IDE Tools > Options > PHP
    xdebug.remote_handler=dbgp
    Make sure the paths you specify match the names and locations of the corresponding files as determined during your installation.
  4. To be sure that previously installed PHP engine supports using the MySQL database server:
    1. Click Start > Control Panel.
    2. On the Control Panel, choose Add or Remove Programs.
    3. On the Add or Remove Programs panel, select the PHP <version number> area and click Change. The PHP Setup Wizard starts. Click Next.
    4. On the Change, repair or remove installation panel, choose Change and click Next.
    5. On the Web Server Setup panel, choose the version of the Apache server - in our example it is Apache 2.2.x Module. Click Next.
    6. On the Apache Configuration Directory panel, specify the directory where the Apache configuration file httpd.conf is located. Click Next.
    7. On the Choose Items to Install panel, expand the Extensions node and choose the MySQL and MySQLi items. Click Next.
    8. On the Ready to change PHP <version number> panel, click Change.
    9. On the Completed the PHP <version number> Setup Wizard panel, click Finish.