Saturday, April 26, 2008

Applet to Applet Communication

The AppletContext class in the java.applet package contains the two member methods getApplet and getApplets. By using those methods, an applet can find other applets and invoke methods on them.
That can only be done if the following security requirements are met:

  • The applets originate from the same server and from the same directory on the server.
  • The applets are running on the same page and in the same browser window.

Those security restrictions may have been designed that way for good reason; however, the latter requirement limits the ways you can make interesting multiapplet interfaces featuring applet-to-applet (a2a) communication.

Using the AppletContext API
Before I explain the alternative a2a-communication mechanism, I will briefly show how the getApplet and getApplets methods work. An applet can find another applet in the same HTML page by either using the getApplet method to look it up by name or using the getApplets method to find all the applets on the page.

Both methods, if successful, return one or more Applet objects to the caller. Once the caller finds an Applet object, it can invoke the Applet's public methods.

Suppose that a snippet of the HTML page looks like this:

<applet code="Applet1" width="400" height="100" name="app1"> </applet> <br> <applet code="Applet2" width="400" height="100" name="app2"> </applet> <br>

By using the name attribute in the applet tag, you can refer to a specific applet in the following way:

Applet theOtherApplet = getAppletContext().getApplet("app1");
theOtherApplet.anyMethod(); //calling any public method

Or, we can use the following code to retrieve all applets on the page:

Enumeration allAppletsOnSamePage = getAppletContext().getApplets();
while(allAppletsOnSamePage.hasMoreElements()) {
Applet appl = (Applet) allAppletsOnSamePage.nextElement();
appl.anyMethod(); //Calling any public method
}

When the calling applet has retrieved one or several applets on the same HTML page, it can call those applets' public methods.

Example:
First Applet Program
//One.java
import javax.swing.*;
import java.awt.*;
public class One extends JApplet
{
JTextArea taOutput;
public void init()
{ getContentPane().setLayout( new FlowLayout());
taOutput = new JTextArea(5,20);
getContentPane().add(taOutput);
}

public void putText(String str)
{
taOutput.append( str + "\n");
}
}
Second Applet Program:
//Two.java
import javax.swing.*;
import java.awt.event.*;
import java.applet.AppletContext;
import java.awt.*;
public class Two extends JApplet implements ActionListener{
JTextField txtInput;
AppletContext ac;
JButton btnSend;
public void init()
{
getContentPane().setLayout( new FlowLayout());
txtInput = new JTextField(25);

getContentPane().add(txtInput);
btnSend = new JButton("Send");

getContentPane().add(btnSend);
btnSend.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
ac = getAppletContext();
One obj = (One) ac.getApplet("One");
if(obj != null)
{
obj.putText(txtInput.getText());
txtInput.setText("");
} }}
AppletToApplet.html file

<HTML> <HEAD> <TITLE> Applet-Applet Communicaton </TITLE> </HEAD>
<BODY BGCOLOR="#FFFFFF"> <h1> Applet-Applet Communicaton </h1>
<applet code="Two" width="400" height="300" name="Two"> </applet>
<br> <hr> <br>
<applet code="One" width="400" height="300" name="One"> </applet>

</BODY> </HTML>

  • Compile Both Program
  • Run the Program
Output 1:

Output after Click on Send Button


Conclusion:
So we can easily communicate one applet to another applet by using AppletContext with following restrictions:

  • The applets originate from the same server and from the same directory on the server.
  • The applets are running on the same page and in the same browser window

Monday, April 21, 2008

Upgrade to Visual Studio 2008

**Step Ahead to Visual Studio 2008**

Introduction

Visual Studio 2008 is carrying new features for developers and software industry. There are some features of Visual Studio 2008 Beta version discussed below:

Multi Targeting Support.
Web Designer and CSS Support.
Database Support Features.
AJAX, ASP.Net and Java Script Support.
Project Designer Support.
Language Integrated Query.


1. Multi Targeting Support:
In each Visual Studio release Microsoft supported a specific version of .Net Framework. For example, With VS 2003 : .Net 1.1 Framework, and With VS 2005 : .Net 2.0.

But with VS 2008 release, “Multi-Targeting” is supported. This means developers will be able to design application of any supporting framework. Developer can choose any type of supporting framework at design time.

In Visual Studio 2008 developer can not choose framework version 1.1, only 2.0, 3.0, 3.5 is supported.


2. Web Designer and CSS Support:

Split view editing is addition to visual studio 2008, till VS 2005 only designer view and code view were available. Main objective of this view is that developer can view HTML view and Design view at the dame time and easily make changes in any of the views. Like as he select a tag in code view, the corresponding elements/controls are selected in design view.
CSS Style Manager is new tool inside the IDE called Manage Styles which shows all CSS style sheets for the page.
It can be also useful when we are in any of the views like design, code and split view. It can be activated by choosing format->CSS Style->Manage Styles from the menu.



Transparent Intellisense Mode is new feature of Visual Studio 2008 While using VS 2005/2003 we often find ourselves escaping out of intellisense in order to better see the code around, and then go back and complete what we were doing.
But VS 2008 provides a new feature which allows us to quickly make the intellisense drop-down list semi-transparent. Just hold down the "Ctrl" key while the intellisense drop-down is visible and we will be able to switch it into a transparent mode that enables us to look at the code beneath without having to escape out of Intellisense.

3. Database Support Features:

The Object Relational Designer (O/R Designer) assists developers in creating and editing the objects (LINQ to SQL entities) that map between an application and a remote database.

Hierarchical update capabilities in Dataset Designer, providing generated code that includes the save logic required to maintain referential integrity between related tables.

Local database caching incorporates an SQL Server Compact 3.5 database into an application and configures it to periodically synchronize the data with a remote database on a server.


4. AJAX, ASP.Net and Java Script Support:

One new feature that developers will find with VS 2008 is its built-in support for JavaScript Intellisense. This makes using JavaScript and building AJAX applications significantly easier. A double click on HTML control in design mode would automatically create a click event to the button and would create the basic skeleton of the JavaScript function.

One new JavaScript feature in VS 2008 is the much-improved support for JavaScript debugging. This makes debugging AJAX applications significantly easier.

We can now set both client-side JavaScript breakpoints and VB/C# server-side breakpoints at the same time on the same page and use a single debugger to step through both the server-side and client-side code in a single debug session. This feature is extremely useful for AJAX applications.

5. Project Designer Support:
Windows Presentation Foundation (WPF) applications have been added to Visual Studio 2008. There are four WPF project types:
WinFX Windows Application
WinFX Web Browser Application
WinFX Custom Control Library
WinFX Service Library
When a WPF project is loaded in the IDE, the user interface of the Project Designer pages lets us specify properties specific to WPF applications.

6. Language Integrated Query:

LINQ is a new feature of Visual Studio 2008 that brings great querying capabilities into the language syntax. LINQ introduces patterns for querying and updating data. A set of new assemblies are provided that enable the use of LINQ with collections, SQL databases, and XML documents.

Conclusion:
This concludes my discourse on enhancement in Visual Studio .Net to Visual Studio .net 2008 Beta for now. I will continue update this article with new features and actual benchmarks in the future.

Thursday, April 10, 2008

Enhancements in Java SE 6

Java SE 6 contains a number of features that make programming easier with Java technology. In this article, we discuss four new features that allow us to do the following:

  • Set file and directory permissions
  • Obtain the free space and usable space on partitions
  • Add Component objects to the tabs of a JTabbedPane
  • Use the popular SwingWorker class in conjunction with Java Foundation Classes/Swing (JFC/Swing) applications

Setting File and Directory Permissions:
You can now set the readable, writable, and executable flag of a file in the local file system. This functionality has been added to the java.io.File class with the following methods:

public boolean setReadable(boolean readable, boolean ownerOnly)
public boolean setReadable(boolean readable)

public boolean setWritable(boolean writable, boolean ownerOnly)
public boolean setWritable(boolean writable)

public boolean setExecutable(boolean executable, boolean ownerOnly)
public boolean setExecutable(boolean executable)

The methods of determining whether a file is readable, writable, or executable remain from the previous version of this platform, Java 2 Platform, Standard Edition (J2SE) 5.0.

public boolean canRead();
public boolean canWrite();
public boolean canExecute();

Obtaining Space Allocation on Disks
Java SE 6 gives us three new methods to determine the amount of space available on the partition represented by a java.io.File object:

public long getTotalSpace();
public long getFreeSpace();
public long getUsableSpace();

Each of these methods returns the requested size, in bytes, of the partition represented by the java.io.File or 0L if a partition cannot be obtained from the File object.


With getFreeSpace() and getUsableSpace(), the returned number of unallocated bytes is, "a hint, but not a guarantee, that it is possible to use most or all of these bytes. The number of unallocated bytes is most likely to be accurate immediately after this call. It is likely to be made inaccurate by any external I/O operations including those made on the system outside of this virtual machine."


What's the difference between these two methods?
The getFreeSpace() method returns an instantaneous count of the amount of free space on the partition. The getUsableSpace() method, on the other hand, contains extra functionality to check for write permissions and other operating system restrictions, which returns a better estimate of how much space will be available. If you want to determine whether you have enough disk space before writing to a file, getUsableSpace() will typically give you a more accurate estimate. Note that both of these methods can throw a SecurityException.


Representing Tabs in JTabbedPane with Components :
This is a subtle but valuable change in Swing's JTabbedPane. Previously with JTabbedPane, you were limited to representing a tab with a String, an Icon, or a combination of both. In addition, you could add a tooltip to the tab if you wanted. It is now possible to use a Component to represent the tab in a JTabbedPane.


Although this may bring to mind a number of possibilities, the most common use of this feature will be to add a Close button that will remove the tab from the JTabbedPane.

You can set a Component as a tab using the following method:
public void setTabComponentAt(int index, Component component)

You can get this component with the following method:
public Component getTabComponentAt(int index)

You can test whether a component is used in this JTabbedPane with this method:
public int indexOfTabComponent(Component tabComponent)


Here is sample source code for a tabbed pane that allows you to add and remove tabs dynamically from a JTabbedPane. Note that in this example we have created a JPanel that consists of two components: a JLabel on the left side of the panel (BorderLayout.WEST) and a button with an ImageIcon on the right side of the panel (BorderLayout.EAST).


The graphic used is a 10x10 pixel gif that consists of a small X. In order to keep the size of the button small, we reset its preferred size to the icon's width and height plus two.

//code of TabbedPane
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TabbedPaneExample implements ActionListener {

private JFrame frame;
private JTabbedPane tabbedPane;
private JButton addTabButton;

private ImageIcon closeXIcon;
private Dimension closeButtonSize;

private int tabCounter = 0;

public TabbedPaneExample() {

// Create the tabbed pane.
tabbedPane = new JTabbedPane();

// Create a button that users can press to add a tab to the tabbed pane.
addTabButton = new JButton("Add Tab");
addTabButton.addActionListener(this);

// Create a frame to hold the tabbed pane.
frame = new JFrame();

/* Create an image icon of the small 'X' for use with a close button on teach tab. The gif loaded is a 10x10 graphic with transparency on areas that are not black. */

closeXIcon = new ImageIcon("C:/CloseX.gif");

// Create a Dimension that can be used to size the close buttons.

closeButtonSize = new Dimension(
closeXIcon.getIconWidth()+2,
closeXIcon.getIconHeight()+2);

/* Add the tabbed pane to the center of the graphic and the "Add Tab" button to the south. Then pack it, size it, and show it. */
frame.add(tabbedPane, BorderLayout.CENTER);
frame.add(addTabButton, BorderLayout.SOUTH);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();
frame.setMinimumSize(new Dimension(300, 300));
frame.setVisible(true);

}
public void actionPerformed(ActionEvent e) {
final JPanel content = new JPanel();

// Create a panel that represents the tab and ensure that it is transparent.
JPanel tab = new JPanel();
tab.setOpaque(false);

/ * Create a label and a Close button for the tab. Be sure to set its preferred size to nearly the size of the icon, and create an action listener that will locate the tab and remote it from the tabbed pane. */

JLabel tabLabel = new JLabel("Tab " + (++tabCounter));

JButton tabCloseButton = new JButton(closeXIcon);
tabCloseButton.setPreferredSize(closeButtonSize);
tabCloseButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
int closeTabNumber =
tabbedPane.indexOfComponent(content);
tabbedPane.removeTabAt(closeTabNumber);
}
});

tab.add(tabLabel, BorderLayout.WEST);
tab.add(tabCloseButton, BorderLayout.EAST);

/* Add the tab to the tabbed pane. Note that the first parameter, which would ordinarily be a String that represents the tab title, is null. */
tabbedPane.addTab(null, content);

// Instead of using a String/Icon combination for the tab, use our panel instead.
tabbedPane.setTabComponentAt(tabbedPane.getTabCount()-1, tab);
}
public static void main(String[] args) {
TabbedPaneExample main = new TabbedPaneExample();
}
}
The result is shown in Figure 1.

Figure 1. JTabbedPane With Several JComponents as Tabs

SwingWorker Is Now Included:
Most Swing programmers know that whenever they write event-handling code, such as an ActionListener that gets called when a button is pressed, events need to be processed quickly.

You never want to spend any more time than you have to processing on the event-handling thread, or your Swing graphical user interface (GUI) will become non responsive and be unable to repaint itself efficiently. Taking on a larger task from the event dispatch thread often means kicking off a separate "worker" thread from the event dispatch thread and letting that run in the background. Thus, when writing a multithreaded application using Swing, a programmer needs to keep these two rules in mind:
  • Time-consuming tasks should not be run on the event dispatch thread, which causes the application to become unresponsive. They should be run on a worker thread instead.
  • Updates to Swing components should be performed on the event dispatch thread only.

Because this means that there are at least two threads at play, it helps to have a class that can handle interthread communication. The good news is that the SwingWorker class, which has been a popular solution for some time with Swing programmers, is included in the latest release.

Here is the declaration of SwingWorker from the Javadocs.

public abstract class SwingWorker extends Object
implements RunnableFuture

Note that the declaration contains two generic type variables: T and V. [Generics, a feature that was introduced in J2SE 5.0]. Here are the definitions:

T: the result type returned by this SwingWorker's doInBackground() and get() methods
V: the type used for carrying out intermediate results by this SwingWorker's publish() and process() methods

We'll talk about these methods shortly. First, however, let's introduce the thread architecture that is used with a SwingWorker. To quote from the Javadocs: "There are three threads involved in the life cycle of a SwingWorker:

Current thread: The execute() method is called on this thread. It schedules SwingWorker for the execution on a worker thread and returns immediately. One can wait for the SwingWorker to complete using one of two get() methods.

Worker thread: The doInBackground() method is called on this thread. This is where all background activities should happen. To notify PropertyChangeListeners about bound properties changes, use the firePropertyChange and getPropertyChangeSupport() methods. By default, two bound properties are available: state and progress.

Event dispatch thread: All Swing-related activities occur on this thread. SwingWorker invokes the process() and done() methods and notifies any PropertyChangeListeners on this thread."
Typically, the current thread on which you instantiate the SwingWorker subclass is the event dispatch thread. The SwingWorker then usually follows this series of events:

1. The execute() method is called or run on the event dispatch thread.
2. SwingWorker notifies any PropertyChangeListeners that its state has shifted to SwingWorker.StateValue.STARTED.
3. The doInBackground() method is executed on the worker thread.
4. Once the doInBackground() method completes, the done() method is executed on the current thread.
5. SwingWorker notifies any PropertyChangeListeners that its state has shifted to SwingWorker.StateValue.DONE.

While in the doInBackground() method, you can set an integer progress property that indicates the current progress of the worker thread using the following method:
protected void setProgress(int progress);

Once this method is called, a property change event is fired by SwingWorker to all registered listeners to notify them of the updated progress value.

You can set or add to the final results of the worker thread using one of two methods:

protected void process(V... chunks);
protected void publish(V... chunks);

The latter of these methods, publish(), will take a variable number of objects of some intermediate type V and send them to the process() method for processing. You will typically call the process() method from the doInBackground() thread. The process() method should always be overridden to accept the incoming V parameters and concatenate these intermediate objects somehow into a type T. How the process() method accomplishes this task, of course, depends on the type parameters that are specified in your subclass of SwingWorker.

Meanwhile, on the current thread, you can call one of two get() methods to retrieve the results of the worker thread. The first method, get(), blocks indefinitely until the worker thread has completed. The second method will block for the specified amount of time before retrieving whatever results have been processed.

public T get();
public T get(long timeout, TimeUnit unit);

If you wish to cancel the worker thread before it has finished executing, you can call the cancel() method from the current thread.

public final boolean cancel(boolean mayInterruptIfRunning)

Here, the mayInterruptIfRunning parameter specifies whether the thread executing this task should be interrupted in an attempt to stop the task. Note that calling the cancel() method will fail if the task has already completed, if it has already been cancelled, or if it could not be cancelled for some other reason. If, however, calling the method returns true and this task has not already started when the cancel() method is called, the SwingWorker should never execute.

Conclusion :
Although these features are largely independent of each other, they represent the desire of the Java development.