1 package com.generalrobotix.ui.view.tdview;
4 import java.lang.reflect.InvocationTargetException;
6 import java.awt.BorderLayout;
8 import java.awt.Component;
9 import java.awt.Dimension;
10 import java.awt.Frame;
11 import java.awt.Graphics2D;
12 import java.awt.Image;
13 import java.awt.event.ActionEvent;
14 import java.awt.event.ActionListener;
15 import java.awt.image.BufferedImage;
17 import javax.media.Buffer;
18 import javax.media.ConfigureCompleteEvent;
19 import javax.media.Controller;
20 import javax.media.ControllerEvent;
21 import javax.media.ControllerListener;
22 import javax.media.Manager;
23 import javax.media.MediaLocator;
24 import javax.media.PrefetchCompleteEvent;
25 import javax.media.Processor;
26 import javax.media.RealizeCompleteEvent;
27 import javax.media.ResourceUnavailableEvent;
28 import javax.media.control.FrameGrabbingControl;
29 import javax.media.control.FramePositioningControl;
30 import javax.media.control.TrackControl;
31 import javax.media.format.VideoFormat;
32 import javax.swing.JButton;
33 import javax.swing.JLabel;
34 import javax.swing.JPanel;
35 import javax.swing.SwingUtilities;
38 import com.sun.image.codec.jpeg.*;
40 import org.eclipse.jface.dialogs.MessageDialog;
41 import org.eclipse.swt.SWT;
42 import org.eclipse.swt.awt.SWT_AWT;
43 import org.eclipse.swt.events.ControlEvent;
44 import org.eclipse.swt.events.ControlListener;
45 import org.eclipse.swt.events.SelectionAdapter;
46 import org.eclipse.swt.events.SelectionEvent;
47 import org.eclipse.swt.events.ShellEvent;
48 import org.eclipse.swt.events.ShellListener;
49 import org.eclipse.swt.layout.FillLayout;
50 import org.eclipse.swt.widgets.Composite;
51 import org.eclipse.swt.widgets.Display;
52 import org.eclipse.swt.widgets.FileDialog;
53 import org.eclipse.swt.widgets.Menu;
54 import org.eclipse.swt.widgets.MenuItem;
55 import org.eclipse.swt.widgets.Shell;
84 window_ =
new Shell( shell, SWT.SHELL_TRIM );
85 window_.setSize(200, 200);
90 FillLayout layout =
new FillLayout();
91 window_.setLayout(layout);
96 comp_ =
new Composite( window_, SWT.EMBEDDED );
97 frame_ = SWT_AWT.new_Frame( comp_ );
99 comp_.addControlListener(
new ControlListener() {
100 public void controlMoved(ControlEvent e) {}
101 public void controlResized(ControlEvent e) {
102 frame_.setBounds(0, 0, comp_.getSize().x, comp_.getSize().y );
107 window_.addShellListener(
new ShellListener(){
108 public void shellActivated(ShellEvent e) {
111 public void shellClosed(ShellEvent e) {
115 public void shellDeactivated(ShellEvent e) {
119 public void shellDeiconified(ShellEvent e) {
123 public void shellIconified(ShellEvent e) {
129 contentPane_ =
new JPanel();
130 frame_.add(contentPane_);
133 window_.setSize(contentPane_.getPreferredSize().width, contentPane_.getPreferredSize().height);
136 frameX = window_.getSize().x - comp_.getSize().x;
137 frameY = window_.getSize().y - comp_.getSize().y;
138 System.out.println(
"frame size="+frameX+
"-"+frameY);
145 Display display = Display.getDefault();
147 if ( display!=
null && !display.isDisposed())
152 window_.setSize(contentPane_.getPreferredSize().width+frameX, contentPane_.getPreferredSize().height+frameY);
160 Menu menubar =
new Menu(window_,SWT.BAR);
161 window_.setMenuBar(menubar);
163 MenuItem item1 =
new MenuItem(menubar,SWT.CASCADE);
166 Menu menu1 =
new Menu(item1);
167 item1.setMenu(menu1);
169 MenuItem item1_1 =
new MenuItem(menu1,SWT.PUSH);
171 item1_1.addSelectionListener(
new SelectionAdapter(){
172 public void widgetSelected(SelectionEvent e) {
177 MenuItem item1_3 =
new MenuItem(menu1,SWT.PUSH);
179 item1_3.addSelectionListener(
new SelectionAdapter(){
180 public void widgetSelected(SelectionEvent e) {
185 MenuItem item1_4 =
new MenuItem(menu1,SWT.PUSH);
187 item1_4.addSelectionListener(
new SelectionAdapter(){
188 public void widgetSelected(SelectionEvent e) {
202 FileDialog fDialog =
new FileDialog(window_,SWT.OPEN);
204 String [] exts = {
"*.mpg;*.avi;*.mov"};
205 String [] filterNames = {
MessageBundle.
get(
"SWTMoviePlayer.dialog.filter")};
206 fDialog.setFilterExtensions(exts);
207 fDialog.setFilterNames(filterNames);
209 String openPath = fDialog.open();
210 if( openPath !=
null ) {
211 currentFile =
new File( openPath );
219 SwingUtilities.invokeAndWait(
new Runnable(){
221 if(!
_load(
"file:" + currentFile.getAbsolutePath())){
226 }
catch (InterruptedException e1) {
228 e1.printStackTrace();
229 }
catch (InvocationTargetException e1) {
231 e1.printStackTrace();
267 if (cmpVisual_!=
null){
268 contentPane_.remove(cmpVisual_);
272 contentPane_.remove(cmpOpe_);
277 p_.removeControllerListener(
this);
296 if (url.indexOf(
":") > 0 && (ml =
new MediaLocator(url)) !=
null)
299 if (url.startsWith(
File.separator)) {
300 if ((ml =
new MediaLocator(
"file:" + url)) !=
null)
303 String file =
"file:" + System.getProperty(
"user.dir") +
File.separator + url;
304 if ((ml =
new MediaLocator(file)) !=
null)
322 private boolean _load(String fileName) {
323 Component newVisual=
null;
324 Component newOpe=
null;
328 System.out.println(
"filename is "+fileName);
333 System.out.println(
"ML="+ml);
337 newVisual =
new JPanel();
338 ((JPanel)newVisual).setPreferredSize(
new Dimension(160,120));
339 newVisual.setBackground(Color.black);
340 newOpe=
new JLabel(STR_RIGHT_);
345 p_ = Manager.createProcessor(ml);
346 }
catch (Exception e) {
347 System.err.println(
"Failed to create a processor from the given url: " + e);
351 p_.addControllerListener(
this);
356 System.err.println(
"Failed to configure the processor.");
361 p_.setContentDescriptor(
null);
364 TrackControl tc[] = p_.getTrackControls();
367 System.err.println(
"Failed to obtain track controls from the processor.");
372 TrackControl videoTrack =
null;
374 for (
int i = 0;
i < tc.length;
i++) {
375 if (tc[
i].getFormat() instanceof VideoFormat) {
381 if (videoTrack ==
null) {
382 System.err.println(
"The input media does not contain a video track.");
390 cnts=p_.getControls();
391 for(
int i=0;
i<cnts.length;
i++){
392 if(cnts[
i] instanceof FramePositioningControl){
393 frameCtrl_=(FramePositioningControl)cnts[
i];
403 System.err.println(
"Failed to realize the processor.");
408 javax.media.Renderer renderer=
null;
410 cnts=videoTrack.getControls();
411 for(
int i=0;
i<cnts.length;
i++){
412 if(cnts[
i] instanceof javax.media.Renderer){
413 renderer=(javax.media.Renderer)cnts[
i];
420 cnts=renderer.getControls();
421 for(
int i=0;
i<cnts.length;
i++){
422 if(cnts[
i] instanceof FrameGrabbingControl){
423 frameGrabCtrl_=(FrameGrabbingControl)cnts[
i];
429 newVisual = p_.getVisualComponent();
431 JPanel panel=
new JPanel();
432 panel.setLayout(
new BorderLayout());
434 if ((cc = p_.getControlPanelComponent()) !=
null) {
435 panel.add(
"Center", cc);
438 btn.addActionListener(
new ActionListener(){
439 public void actionPerformed(ActionEvent e){
444 panel.add(
"East", btn);
452 contentPane_.setLayout(
new BorderLayout());
453 cmpVisual_=newVisual;
454 if (cmpVisual_!=
null) {
455 contentPane_.add(
"Center", cmpVisual_);
459 if (cmpOpe_ !=
null) {
460 contentPane_.add(
"South", cmpOpe_);
464 if(frameCtrl_!=
null){
485 System.out.println(
"Can't create Image in this format!");
488 BufferedImage bimg=
new BufferedImage(img.getWidth(
null),img.getHeight(
null),BufferedImage.TYPE_INT_RGB);
489 Graphics2D g=bimg.createGraphics();
490 g.drawImage(img,0,0,
null);
499 JPEGImageEncoder enc;
502 System.out.println(
"not init.");
506 SwingUtilities.invokeAndWait(
new Runnable(){
508 if (p_.getState()==Controller.Started)
512 }
catch (Exception e) {
516 FileDialog fDialog =
new FileDialog(window_,SWT.SAVE);
518 String [] exts = {
"*.jpg"};
519 String [] filterNames = {
MessageBundle.
get(
"SWTMoviePlayer.dialog.filter.jpeg")};
520 fDialog.setFilterExtensions(exts);
521 fDialog.setFilterNames(filterNames);
523 String openPath = fDialog.open();
524 if( openPath !=
null ) {
528 new FileOutputStream(
531 enc=JPEGCodec.createJPEGEncoder(output);
534 SwingUtilities.invokeAndWait(
new Runnable(){
538 }
catch (Exception e) {
544 }
catch (Exception e) {
574 }
catch(Exception exception){
583 MessageDialog.openError(window_,
MessageBundle.
get(
"SWTMoviePlayer.dialog.title.error"),
MessageBundle.
get(
"SWTMoviePlayer.dialog.message.save") + exception.getMessage() );
601 }
catch (Exception e) {}
612 if (evt instanceof ConfigureCompleteEvent ||
613 evt instanceof RealizeCompleteEvent ||
614 evt instanceof PrefetchCompleteEvent) {
616 stateTransitionOK =
true;
617 waitSync.notifyAll();
619 }
else if (evt instanceof ResourceUnavailableEvent) {
621 stateTransitionOK =
false;
622 waitSync.notifyAll();
SWTMoviePlayer(Shell shell, String fileName)
static final String get(String key)
Image createImage(Buffer buffer)
#define null
our own NULL pointer
void controllerUpdate(ControllerEvent evt)
MediaLocator _createMediaLocator(String url)
boolean _load(String fileName)
FrameGrabbingControl frameGrabCtrl_
boolean stateTransitionOK
FramePositioningControl frameCtrl_
boolean _waitForState(int state)
BufferedImage _convertBufferedImage(Buffer buf)
output(gif_dest_ptr dinfo, int code)