
Cooking with Eclipse, Part 2
by Steve HolznerEditor's note: If you missed last week's batch of recipes from O'Reilly's Eclipse Cookbook, be sure to check them out. This week, we offer two more sample recipes from the book. Both offer glimpses of Eclipse in action -- the first with CVS, and the second with Swing. Enjoy.
Related Reading ![]() Eclipse Cookbook |
Recipe 6.3: Connecting Eclipse to a CVS Repository
Problem
You want to connect Eclipse to a CVS repository.
Solution
In Eclipse, open the Repositories view, right-click that view, and select New? Repository Location, opening the Add CVS Repository dialog. Enter the required information, and click OK.
Discussion
You have to establish a connection from Eclipse through the CVS server to the CVS repository before working with that repository. First, make sure your CVS server is running.
To connect Eclipse to the CVS repository, select Window? Open Perspective? Other, and select the CVS Repository Exploring perspective. After you do this the first time, Eclipse adds this perspective to the Window? Open Perspective submenu and also adds a shortcut for this perspective to the other perspective shortcuts at the extreme left of the Eclipse window.
When the CVS Repository Exploring perspective opens, right-click the blank CVS repositories view at left, and select New? Repository Location, opening the Add CVS Repository dialog shown in Figure 6-3.
Figure 6-3. Connecting Eclipse to a CVS repository
In the Add CVS Repository dialog, enter the name of the CVS server,
often the name of the machine hosting the CVS server, and the path to
the CVS repository. To connect to the CVS server, you'll also need to supply a username and password,
as shown in Figure 6-3 (in this case we're using integrated Windows NT security, so no
password is needed). You can use two connection protocols with CVS servers, SSH (secure shell) and pserver
. We'll use pserver
here.
TIP: pserver
is a CVS client/server protocol that uses
its own password files and connections. It's more efficient than SSH but less secure. If security is an issue, go with
SSH.
Click Finish after configuring the connection. The new connection to the CVS server should appear in the CVS Repositories view, as shown in Figure 6-4.
Figure 6-4. A new repository created in the CVS Repositories view
TIP: A public CVS server is available that gives you access to the code for Eclipse; go to :pserver:anonymous@dev.eclipse.org:/home/eclipse. If you wish, you can see what commands Eclipse sends to the CVS server. To do so, open the CVS console by selecting Window→ Show View→ Other→ CVS→ CVS Console. The CVS Console view will appear (this view overlaps the standard Console view).
Eclipse 3.0
Eclipse 3.0 also supports CVS SSH2 in addition to the
pserver
and SSH protocols. You can enable SSH2 in
the SSH2 Connection Method preference page (right-click a project and
select Team→ CVS→ SSH2 Connection Method). All CVS
server connections of type extssh
will use SSH2
from that point on.
See Also
Chapter 4 of Eclipse (O'Reilly).
Recipe 9.19: Using Swing and AWT Inside SWT (Eclipse 3.0)
Problem
You want to use Swing or AWT graphical elements inside an SWT application.
Solution
SWT (in Eclipse 3.0) supports embedding Swing/AWT widgets inside SWT widgets. Before Version 3.0, such support worked only on Windows. In Eclipse 3.0, it's now working in Windows for JDK 1.4 and later, as well as in GTK and Motif with early-access JDK 1.5.
Discussion
To work with AWT and Swing elements in SWT, you use the SWT_AWT
class. As an example
(SwingAWTApp at this book's site), we'll create an application with an AWT frame
and panel, as well as a Swing button and text control. We'll start with a new SWT composite widget:
Composite composite = new Composite(shell, SWT.EMBEDDED);
composite.setBounds(20, 20, 300, 200);
composite.setLayout(new RowLayout( ));
Now add an AWT Frame
window object to the
composite using the SWT_AWT.new_Frame
method, and
add an AWT Panel
object to the frame:
java.awt.Frame frame = SWT_AWT.new_Frame(composite);
java.awt.Panel panel = new java.awt.Panel( );
frame.add(panel);
We can now work with Swing controls. In this example, we'll add a Swing button and a Swing text control to the AWT panel:
final javax.swing.JButton button = new javax.swing.JButton("Click Me");
final javax.swing.JTextField text = new javax.swing.JTextField(20);
panel.add(button);
panel.add(text);
All that's left is to connect the button to a listener to display a message when that button is clicked. You can see how that works in Example 9-5.
Example 9-5. Using Swing and AWT in SWT
package org.cookbook.ch09;
import java.awt.event.*;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.awt.SWT_AWT;
public class SwingAWTClass {
public static void main(String[] args) {
final Display display = new Display( );
final Shell shell = new Shell(display);
shell.setText("Using Swing and AWT");
shell.setSize(350, 280);
Composite composite = new Composite(shell, SWT.EMBEDDED);
composite.setBounds(20, 20, 300, 200);
composite.setLayout(new RowLayout( ));
java.awt.Frame frame = SWT_AWT.new_Frame(composite);
java.awt.Panel panel = new java.awt.Panel( );
frame.add(panel);
final javax.swing.JButton button = new javax.swing.JButton("Click Me");
final javax.swing.JTextField text = new javax.swing.JTextField(20);
panel.add(button);
panel.add(text);
button.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent event) {
text.setText("Yep, it works.");
}
});
shell.open( );
while(!shell.isDisposed( )) {
if (!display.readAndDispatch( )) display.sleep( );
}
display.dispose( );
}
}
Running this application gives you the results shown in Figure 9-12; a Swing button and text control at work in an AWT frame in an SWT application. Cool.
Figure 9-12. Mixing AWT, Swing, and SWT
Steve Holzner is the author of O'Reilly's upcoming Eclipse: A Java Developer's Guide.
Return to ONJava.com.
