Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » swt_awt and batik
swt_awt and batik [message #268314] Mon, 23 August 2004 21:18 Go to next message
Eclipse UserFriend
Originally posted by: user.domain.invalid

I am porting the batik svgcanvas.html demo to eclipse 3.0.0 and win98.
1. Cutting the code from the html page and pasting into an eclipse java
project runs fine.
2. Cutting the core and pasting into the eclipse HelloWorld2.java swt
example with SWT_AWT runs fine.
3. Changing the status line from awt to swt by adding asyncExec runs fine.
4. I am now trying to change the awt load file button to an swt file
menu by adding the file menu from the ImageAnalyzer.java eclipse example
as shown below. When I load files with the awt load button, things run
fine; when I load files with the swt file menu, the system stops after 2
to 5 loads and my not even respond to the three finger salute. Can
anyone help?

Thank you
Norwood Sisson

/*********************************************************** ********************
* Copyright (c) 2000, 2003 IBM Corporation and others. All rights
reserved.
* This program and the accompanying materials are made available under the
* terms of the Common Public License v1.0 which accompanies this
distribution,
* and is available at http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors: IBM Corporation - initial API and implementation

************************************************************ ******************/
package svgcanvas;

import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.gvt.GVTTreeRendererAdapter;
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
import org.apache.batik.swing.svg.GVTTreeBuilderAdapter;
import org.apache.batik.swing.svg.GVTTreeBuilderEvent;
import org.apache.batik.swing.svg.SVGDocumentLoaderAdapter;
import org.apache.batik.swing.svg.SVGDocumentLoaderEvent;
import org.eclipse.swt.*;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;

/*
* This example builds on HelloWorld2 and demonstrates the minimum
amount of
* code required to open an SWT Shell with an AWT canvas and process
the events.
*/

public class SVGApplication3 {
/*
* delete HelloWorld2 line private static ResourceBundle resHello =
* ResourceBundle .getBundle("examples_helloworld");
*/
static Shell shell;

static Display display;

public static void main(String[] args) {
display = new Display();
shell = new SVGApplication3().open(display);
shell.setText("swt shell, swt menu bar, awt load button, awt canvas
and swt status");
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

Label label;

public Shell open(Display display) {
shell = new Shell(display);
/*
* delete HelloWorld2 lines Label label = new Label(shell, SWT.CENTER);
* label.setText(resHello.getString("Hello_world"));
* label.setBounds(shell.getClientArea());
*
*
*/
GridLayout gridLayout = new GridLayout();
shell.setLayout(gridLayout);
GridData gridData = new GridData();
gridData.verticalAlignment = GridData.FILL;
gridData.grabExcessVerticalSpace = true;
gridData.horizontalAlignment = GridData.FILL;
gridData.grabExcessHorizontalSpace = true;
//replace awt load button with a menu bar taken from ImageAnalyzer
// example
createMenuBar();
//add svgCanvas
Composite composite = new Composite(shell, SWT.EMBEDDED);
composite.setLayoutData(gridData);
Frame frame = SWT_AWT.new_Frame(composite);
frame.add(createComponents());
//add status message
label = new Label(shell, SWT.RIGHT);
shell.pack();
shell.open();
return shell;
}

Menu createMenuBar() {
// Menu bar.
Menu menuBar = new Menu(shell, SWT.BAR);
shell.setMenuBar(menuBar);
createFileMenu(menuBar);
return menuBar;
}

void createFileMenu(Menu menuBar) {
// File menu
MenuItem item = new MenuItem(menuBar, SWT.CASCADE);
item.setText("File");
Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
item.setMenu(fileMenu);

// File -> Open File...

item = new MenuItem(fileMenu, SWT.PUSH);
item.setText("OpenFile");
item.setAccelerator(SWT.MOD1 + 'O');
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
menuOpenFile();
}
});

new MenuItem(fileMenu, SWT.SEPARATOR);

// File -> Exit

item = new MenuItem(fileMenu, SWT.PUSH);
item.setText("Exit");
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
shell.close();
}
});
}

String lastPath = null;

void menuOpenFile() {
File f;
// Get the user to choose an svg file.
FileDialog fileChooser = new FileDialog(shell, SWT.OPEN);
if (lastPath != null)
fileChooser.setFilterPath(lastPath);
fileChooser.setFilterExtensions(new String[] { "*.svg", "*.svg" });
fileChooser.setFilterNames(new String[] { " (svg)", "svg (*.svg)" });
String filename = fileChooser.open();
lastPath = fileChooser.getFilterPath();
if (filename == null)
return;
f = new File(filename);
try {
svgCanvas.setURI(f.toURL().toString());
} catch (IOException ex) {
ex.printStackTrace();
}
}

public void updateStatus(final String text) {
display.asyncExec(new Runnable() {
public void run() {
label.setText(text);
label.pack();
}
});
}

JButton button = new JButton("Load...");
JSVGCanvas svgCanvas = new JSVGCanvas();

public Component createComponents() {
final JPanel panel = new JPanel(new BorderLayout());

JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
p.add(button);
panel.add("North", p);
panel.add("Center", svgCanvas);

// Set the button action.
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFileChooser fc = new JFileChooser(".");
int choice = fc.showOpenDialog(panel);
if (choice == JFileChooser.APPROVE_OPTION) {
File f = fc.getSelectedFile();
try {
svgCanvas.setURI(f.toURL().toString());
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
});

// Set the JSVGCanvas listeners.
svgCanvas.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter() {
public void documentLoadingStarted(SVGDocumentLoaderEvent e) {
updateStatus("Document Loading...");
}

public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
updateStatus("Document Loaded.");
}
});

svgCanvas.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {
public void gvtBuildStarted(GVTTreeBuilderEvent e) {
updateStatus("Build Started...");
}

public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
updateStatus("Build Done.");
}
});

svgCanvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {
public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
updateStatus("Rendering Started...");
}

public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
updateStatus("");
}
});
return panel;
}
}
Re: swt_awt and batik [message #268335 is a reply to message #268314] Mon, 23 August 2004 23:50 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: user.example.net

i'm just guessing - but it looks like menuOpenFile is an SWT thing and
svgCanvas is s Swing thing - to connect the two the SWT menuOpenFile
will have to use a swing EventQueue.invokeLater(Runnable) to get on the
swing ui thread.

best guess-
david
Re: swt_awt and batik [message #268454 is a reply to message #268335] Tue, 24 August 2004 16:51 Go to previous message
Eclipse UserFriend
Originally posted by: user.domain.invalid

David Scott wrote:
> i'm just guessing - but it looks like menuOpenFile is an SWT thing and
> svgCanvas is s Swing thing - to connect the two the SWT menuOpenFile
> will have to use a swing EventQueue.invokeLater(Runnable) to get on the
> swing ui thread.
>
> best guess-
> david
>
this certainly explains the symptoms; back to the books.

thank you
norwood sisson
Previous Topic:Can't open a project
Next Topic:can't get defined Actions
Goto Forum:
  


Current Time: Thu Apr 25 03:51:45 GMT 2024

Powered by FUDForum. Page generated in 0.03677 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top