import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.lang.reflect.Field;
import javax.swing.*;
import org.eclipse.swt.program.Program;
class AviTest {
static final String FN_AVI = "c:\\Test.avi";
static JLabel lbErr = new JLabel();
static AppLabel lbApp;
static int wApp, wErr;
public static void main(String[] args) throws Exception {
final JFrame f = new JFrame("AVI Test");
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
f.setLayout(null);
lbApp = new AppLabel(20, 20);
wApp = lbApp.update();
f.add(lbApp);
JButton btnSWT = new JButton("SWT execute");
btnSWT.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
/*
* SWT
*/
wApp = lbApp.update();
f.setSize(new Dimension(Math.max(wApp, wErr)+100, 200));
lbApp.getAviApp().execute(FN_AVI); }
});
btnSWT.setBounds(20, 50, 150, 30);
f.add(btnSWT);
lbErr.setLocation(20, 140);
lbErr.setForeground(Color.RED);
f.add(lbErr);
JButton btnAWT = new JButton("AWT open");
btnAWT.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
/*
* AWT
*/
Desktop.getDesktop().open(new File(FN_AVI));
}
catch (IOException | IllegalArgumentException err) {
String s = err.getMessage();
wErr = lbErr.getFontMetrics(lbErr.getFont()).stringWidth(s);
f.setSize(new Dimension(Math.max(wApp, wErr)+100, 200));
lbErr.setSize(wErr, 20);
lbErr.setText(s);
}
}
});
btnAWT.setBounds(20, 100, 150, 30);
f.add(btnAWT);
f.setSize(new Dimension(Math.max(wApp, wErr)+100, 200));
f.setVisible(true);
}
}
class AppLabel extends JLabel {
private static final long serialVersionUID = 1L;
private Program aviApp;
private int x, y;
AppLabel(int x, int y) {
this.x = x;
this.y = y;
}
int update() {
aviApp = Program.findProgram(".avi");
Field commandField = null;
String cmdText = null;
try {
commandField = Program.class.getDeclaredField("command");
} catch (NoSuchFieldException | SecurityException e) {e.printStackTrace(); }
commandField.setAccessible(true);
try {
cmdText = (String)commandField.get(aviApp);
} catch (IllegalArgumentException | IllegalAccessException e) { e.printStackTrace(); }
int w = getFontMetrics(getFont()).stringWidth(cmdText);
setBounds(x, y, w, 20);
setText(cmdText);
return w;
}
Program getAviApp() { return aviApp; };
}