eclipse开发视图

来源:互联网 发布:php培训哪家好 编辑:程序博客网 时间:2024/04/30 05:14

打开eclipse 或者myeclipse  选中任意位置 new puginproject

输入工程名称 其他默认

选择向导里面的一项  plugin in with a view 右边的框中 有一项

我们看到右边的拓展点 是
Extensions Used
org.eclipse.ui.views

 

我们来开发一个播放MP3和视频的视图插件

我们先到 pugin.xml里面 添加拓展

 

 <extension
         point="org.eclipse.ui.views">
      <category
            name="LH娱乐图"
            id="media">
      </category>
      <view
            name="LH电影视图"
            icon="icons/sample.gif"
            category="media"
            class="media.views.MediaView"
            id="media.views.MediaView">
      </view>
      <view
            name="LH歌曲视图"
            icon="icons/sample.gif"
            category="media"
            class="media.views.SongView"
            id="media.views.SongView">
      </view>
   </extension>
   <extension
         point="org.eclipse.ui.perspectiveExtensions">
      <perspectiveExtension
            targetID="org.eclipse.ui.resourcePerspective">
         <view
               ratio="0.5"
               relative="org.eclipse.ui.views.TaskList"
               relationship="right"
               id="media.views.MediaView">
         </view>
          <view
               ratio="0.5"
               relative="org.eclipse.ui.views.TaskList"
               relationship="right"
               id="media.views.SongView">
         </view>
      </perspectiveExtension>
   </extension>

 

拓展点就是 eclipse加载的时候 要判断加载的是视图还是其他的东西

同时也是要说明操作这些拓展时 要做什么操作 操作就由类去完成

 

视图操作类 必须继承ViewPart类

播放视频的视图类

package media.views;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.internal.win32.TCHAR;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.OleClientSite;
import org.eclipse.swt.ole.win32.OleControlSite;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.ole.win32.Variant;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.part.ViewPart;

/**
 * This sample class demonstrates how to plug-in a new workbench view. The view
 * shows data obtained from the model. The sample creates a dummy model on the
 * fly, but a real implementation would connect to the model available either in
 * this or another plug-in (e.g. the workspace). The view is connected to the
 * model using a content provider.
 * <p>
 * The view uses a label provider to define how model objects should be
 * presented in the view. Each view can present the same model objects using
 * different labels and icons, if needed. Alternatively, a single label provider
 * can be shared between views in order to ensure that objects of the same type
 * are presented in the same way everywhere.
 * <p>
 */

public class MediaView extends ViewPart {

 /**
  * The constructor.
  */
 public MediaView() {
 }

 /**
  * This is a callback that will allow us to create the viewer and initialize
  * it.
  */
 public void createPartControl(Composite parent) {

  container = new Composite(parent, SWT.NULL);
  container.setLayout(null);

  Button button1 = null;
  {
   button1 = new Button(container, SWT.PUSH | SWT.CENTER);
   button1.setText("打开视频");
   button1.setBounds(0, 296, 10, 10);
   button1.addSelectionListener(new SelectionAdapter() {

    @Override
    public void widgetSelected(SelectionEvent e) {
     // TODO Auto-generated method stub
     int[] urlIDs = oleAutoForControl
       .getIDsOfNames(new String[] { "URL" });
     int[] testId = oleAutoForControl
       .getIDsOfNames(new String[] { "openPlayer" });
     FileDialog dialog = new FileDialog(container.getShell(),
       SWT.OPEN);

     try {
      String path = dialog.open();
      File f = new File(path);
      URL u1 = new URL("file:/" + f.getPath());
      URL u2 = new URL(u1, f.getName());
      System.out.println("u1 : " + u2.toString());

      // 呼叫function URL()並且傳入參數
      oleAutoForControl.setProperty(urlIDs[0], new Variant(u2
        .toString()));
     }

     catch (MalformedURLException urlE) {
     }
     // System.out.println("file :
     // "+u2.toString()+"/turlIDs : "+urlIDs[0]);

    }

   });
  }

  container.getShell().getDisplay().asyncExec(new Runnable() {
   public void run() {
    oleAutoForControl = embedPlayer(container);
    fullScreen(true);
   }
  });

 }
 Composite container=null;
 private Button fileChoiceButton = null;
 private static OleFrame oleF = null;
 private static OleClientSite oleCSite = null;
 private static OleControlSite oleCtrlSite = null;
 private static OleAutomation oleAuto = null; // @jve:decl-index=0:
 private static OleAutomation oleAutoForControl = null;
 private Button embadedWMPBut = null;

 // 取得windows media player之後將視窗嵌入應用程式
 // 回傳控制ocx的物件
 private static OleAutomation embedPlayer(Composite container) {
  // container是view元件
  // oleFrm是為了接從ocx傳來的windows media player播放介面,並放置於container中
  final OleFrame oleFrm = new OleFrame(container, SWT.NONE);
  // 設大小以及在容器中的相對位置
  oleFrm.setBounds(new Rectangle(0, 0, 300, 295));
//  OS.SetWindowLong(oleFrm.handle, OS.GWL_EXSTYLE, OS.GetWindowLong(
//    oleFrm.handle, OS.GWL_EXSTYLE) ^ 0x80000);
//  TCHAR lpLibFileName = new TCHAR(0, "User32.dll", true);
//  int hInst = OS.LoadLibrary(lpLibFileName);
//  if (hInst != 0) {
//   // 设定调用函数名称
//   String name = "SetLayeredWindowAttributes?";
//   byte[] lpProcName = new byte[name.length()];
//   for (int i = 0; i < lpProcName.length; i++) {
//    lpProcName[i] = (byte) name.charAt(i);
//   }
//   // 检索DLL输出函数地址
//   int fun = OS.GetProcAddress(hInst, lpProcName);
//   // 当函数存在
//   if (fun != 0) {
//    // 150为透明度,在0-255之间
//    OS.CallWindowProc(fun, oleFrm.handle, 0, 150, 2);
//   }
//   OS.FreeLibrary(hInst);
//  }
  // 連接ocx並且指定存放的容器為oleFrm
  oleCtrlSite = new OleControlSite(oleFrm, SWT.NONE, "WMPlayer.OCX.7");
  // 設定動作,這個地方我不知道詳細的動作流程,希望懂得這個大大來幫忙補槍
  oleCtrlSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
  // 定義取得的wmp播放介面在oleFrm容器中的相對位置以及大小
  oleCtrlSite.setSize(new Point(300, 295));
  oleCtrlSite.setLocation(0, 0);

  oleCtrlSite.setVisible(true);

  // 建立操作ocx內部方法與變數的實體
  final OleAutomation automation = new OleAutomation(oleCtrlSite);

  // 這個我也還沒查出來><",有看到的話在上來補槍
  container.getShell().pack(true);

  return automation;
 }

   public void setPostion(int s)  
     {  
         int ids[] = oleAutoForControl.getIDsOfNames(new String[] { "currentPosition" });  
         if (ids != null)  
         {  
          oleAutoForControl.setProperty(ids[0], new Variant(s));  
         }  
     }  
  
     public void play()  
     {   
         int ids[] = oleAutoForControl.getIDsOfNames(new String[] { "play" });  
         if (ids != null)  
         {  
          oleAutoForControl.invoke(ids[0]);  
         }  
     }  
  
     public void stop()  
     {  
         int ids[] = oleAutoForControl.getIDsOfNames(new String[] { "stop" });  
         if (ids != null)  
         {  
          oleAutoForControl.invoke(ids[0]);  
         }  
     }  
  
     public void pause()  
     {   
         int ids[] = oleAutoForControl.getIDsOfNames(new String[] { "pause" });  
         if (ids != null)  
         {  
          oleAutoForControl.invoke(ids[0]);  
         }  
     }  
  
     public void fullScreen(boolean b)  
     {  
         if (true && container.getSize().x == 0)  
         {  
             return;  
         }  
         int ids[] = oleAutoForControl.getIDsOfNames(new String[] { "fullScreen" });  
         if (ids != null)  
         {  
          oleAutoForControl.setProperty(ids[0], new Variant(b));  
         }  
     }  
  
     public int getPlayState()  
     {  
         int state = 0;  
         int ids[] = oleAutoForControl.getIDsOfNames(new String[] { "playState" });  
         if (ids != null)  
         {  
             Variant sv = oleAutoForControl.getProperty(ids[0]);  
             if (sv != null)  
                 state = sv.getInt();  
         }  
         return state;  
     }  
  
     public void closeMedia()  
     {  
         int ids[] = oleAutoForControl.getIDsOfNames(new String[] { "close" });  
         if (ids != null)  
         {  
          oleAutoForControl.invoke(ids[0]);  
         }  
  
     }  
     public void addPlayList(File urls[])  
     {  
         int ids[] = oleAutoForControl.getIDsOfNames(new String[] { "currentPlaylist" });  
         if (ids != null)  
         {  
             OleAutomation o = oleAutoForControl.getProperty(ids[0]).getAutomation();  
             int idsaddma[] = o.getIDsOfNames(new String[] { "appendItem" });  
             int idsma[] = oleAutoForControl.getIDsOfNames(new String[] { "newMedia" });  
             if (idsaddma != null && idsma != null)  
             {  
                 for (File url : urls)  
                 {  
                     Variant media = oleAutoForControl.invoke(idsma[0], new Variant[] { new Variant(url.getAbsolutePath()) });  
                     if (media != null)  
                     {  
                         o.invoke(idsaddma[0], new Variant[] { media });  
                     }  
  
                 }  
             }  
  
         }  
     }  
     public void play(String url)  
     {  
         int idsma[] = oleAutoForControl.getIDsOfNames(new String[] { "newMedia" });  
         if (idsma != null)  
         {  
             Variant media = oleAutoForControl.invoke(idsma[0], new Variant[] { new Variant(url) });  
             int cmedia[] = oleAutoForControl.getIDsOfNames(new String[] { "currentMedia" });  
             if (cmedia != null)  
             {  
              oleAutoForControl.setProperty(cmedia[0], media);  
                 play();  
             }  
         }  
     }  
     public int getVolume()  
     {  
         int value = 0;   
         int id[] = oleAutoForControl.getIDsOfNames(new String[] { "volume" });  
         if (id != null)  
         {  
             Variant vv = oleAutoForControl.getProperty(id[0]);  
             if (vv != null)  
                 value = vv.getInt();  
         }  
         return value;  
     } 
     public void setMode(String m, boolean flag)  
     {    
         int ids[] = oleAutoForControl.getIDsOfNames(new String[] { "setMode" });  
         if (ids != null)  
         {  
          oleAutoForControl.invoke(ids[0], new Variant[] { new Variant(m), new Variant(flag) });  
         }  
  
     }  
     public void playList()  
     {  
         File file = new File("");  
         if (file.exists())  
         {  
             File fs[] = file.listFiles();  
             addPlayList(fs);  
         }  
         setMode("loop", true);  
         play();  
     }

  @Override
  public void setFocus() {
   // TODO Auto-generated method stub
   
  }  


}

 

 

播放歌曲的类

 

 

package media.views;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.OleClientSite;
import org.eclipse.swt.ole.win32.OleControlSite;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.ole.win32.Variant;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.List;
import org.eclipse.ui.part.ViewPart;

/**
 * This sample class demonstrates how to plug-in a new workbench view. The view
 * shows data obtained from the model. The sample creates a dummy model on the
 * fly, but a real implementation would connect to the model available either in
 * this or another plug-in (e.g. the workspace). The view is connected to the
 * model using a content provider.
 * <p>
 * The view uses a label provider to define how model objects should be
 * presented in the view. Each view can present the same model objects using
 * different labels and icons, if needed. Alternatively, a single label provider
 * can be shared between views in order to ensure that objects of the same type
 * are presented in the same way everywhere.
 * <p>
 */

public class SongView extends ViewPart {

 /**
  * The constructor.
  */
 public SongView() {
 }
 private List songList;
 private Button button1;
 private Button button3;
 private Button button2;
 /**
  * This is a callback that will allow us to create the viewer and initialize
  * it.
  */
 public void createPartControl(Composite parent) {

  container = new Composite(parent, SWT.NULL);
  try {
   FormLayout thisLayout = new FormLayout();
   container.setLayout(thisLayout);
   container.setSize(291, 295);
   {
    FormData songListLData = new FormData();
    songListLData.left =  new FormAttachment(0, 1000, 17);
    songListLData.top =  new FormAttachment(0, 1000, 16);
    songListLData.width = 184;
    songListLData.height = 216;
    songList = new List(container, SWT.BORDER | SWT.MULTI|SWT.V_SCROLL);
    songList.setLayoutData(songListLData);
    songList.addMouseListener(new MouseListener(){

     public void mouseDoubleClick(MouseEvent arg0) {
      // TODO Auto-generated method stub
      String[] se = songList.getSelection();
      if (se != null && se.length > 0) {
       String text = se[0];
       play(text);
      }
     }

     public void mouseDown(MouseEvent arg0) {
      // TODO Auto-generated method stub
      
     }

     public void mouseUp(MouseEvent arg0) {
      // TODO Auto-generated method stub
      
     }
     
    });
    
   }
   {
    button1 = new Button(container, SWT.PUSH | SWT.CENTER);
    FormData button1LData = new FormData();
    button1LData.left = new FormAttachment(0, 1000, 234);
    button1LData.top = new FormAttachment(0, 1000, 16);
    button1LData.width = 19;
    button1LData.height = 22;
    button1.setLayoutData(button1LData);
    button1.setText("+");
    button1.addSelectionListener(new SelectionAdapter() {

     @Override
     public void widgetSelected(SelectionEvent e) {
      // TODO Auto-generated method stub
      FileDialog dlg = new FileDialog(container
        .getShell(),SWT.MULTI);
      
      String text = dlg.open();
      String path=new File(text).getParentFile().getAbsolutePath();
      String[] files=dlg.getFileNames();
      for(String tmp:files)
      {
          String[] f=songList.getItems();
          boolean ishave=false;
       for(String ff:f){
        if(ff.equals(path+"//"+tmp)){
         ishave=true;
         break;
        }
       }
       if(!ishave)
        songList.add(path+"//"+tmp);
      }
      
     }

    });
   }

   {
    button2 = new Button(container, SWT.PUSH | SWT.CENTER);
    FormData button2LData = new FormData();
    button2LData.left = new FormAttachment(0, 1000, 234);
    button2LData.top = new FormAttachment(0, 1000, 42);
    button2LData.width = 19;
    button2LData.height = 22;
    button2.setLayoutData(button2LData);
    button2.setText("-");
    button2.addSelectionListener(new SelectionAdapter() {
     public void widgetSelected(SelectionEvent e) {
      String[] se = songList.getSelection();
      if (se != null && se.length > 0) {
       for(String kk:se)
       songList.remove(kk);
      }
     }
    });
   }
   {
    button3 = new Button(container, SWT.PUSH | SWT.CENTER);
    FormData button3LData = new FormData();
    button3LData.left = new FormAttachment(0, 1000, 234);
    button3LData.top = new FormAttachment(0, 1000, 72);
    button3LData.width = 19;
    button3LData.height = 22;
    button3.setLayoutData(button3LData);
    button3.setText("go");
    button3.addSelectionListener(new SelectionAdapter() {

     @Override
     public void widgetSelected(SelectionEvent e) {
      // TODO Auto-generated method stub
      String[] se = songList.getSelection();
      if (se != null && se.length > 0) {
       String text = se[0];
       play(text);
      }
     }

    });
   }
   final OleFrame oleFrm = new OleFrame(container, SWT.NONE);
   // 設大小以及在容器中的相對位置
   oleFrm.setBounds(new Rectangle(0, 0, 0, 0));
   oleCtrlSite = new OleControlSite(oleFrm, SWT.NONE, "WMPlayer.OCX.7");
   // 設定動作,這個地方我不知道詳細的動作流程,希望懂得這個大大來幫忙補槍
   oleCtrlSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
   // 定義取得的wmp播放介面在oleFrm容器中的相對位置以及大小
   oleCtrlSite.setSize(new Point(300, 295));
   oleCtrlSite.setLocation(0, 0);

   oleCtrlSite.setVisible(true);

   // 建立操作ocx內部方法與變數的實體
   oleAutoForControl = new OleAutomation(oleCtrlSite);
   container.layout();
  } catch (Exception e) {
   e.printStackTrace();
  }

 }
 public void play(String path){
  int[] urlIDs = oleAutoForControl
    .getIDsOfNames(new String[] { "URL" });
  int[] testId = oleAutoForControl
    .getIDsOfNames(new String[] { "openPlayer" });

  try {
   File f = new File(path);
   URL u1 = new URL("file:/" + f.getPath());
   URL u2 = new URL(u1, f.getName());
   System.out.println("u1 : " + u2.toString());

   // 呼叫function URL()並且傳入參數
   oleAutoForControl.setProperty(urlIDs[0],
     new Variant(u2.toString()));
  } catch (MalformedURLException urlE) {
  }
 }
 Composite container=null;
 private Button fileChoiceButton = null;
 private static OleFrame oleF = null;
 private static OleClientSite oleCSite = null;
 private static OleControlSite oleCtrlSite = null;
 private static OleAutomation oleAuto = null; // @jve:decl-index=0:
 private static OleAutomation oleAutoForControl = null;
 private Button embadedWMPBut = null;


  @Override
  public void setFocus() {
   // TODO Auto-generated method stub
   
  }  


}

原创粉丝点击