posts - 20, comments - 16, trackbacks - 0, articles - 0

          在editor中加入右鍵action的實例代碼

          Posted on 2008-02-19 21:30 Raul Gong 閱讀(1095) 評論(0)  編輯  收藏 所屬分類: eclipsejava
          當需要在eclipse 中自己的editor中加入右鍵菜單的時候,可以參考下面的代碼:



          package com.gongxun.tool.kaudit.ui.reports;

          import java.io.File;
          import java.io.FileNotFoundException;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.net.MalformedURLException;

          import org.eclipse.core.runtime.IProgressMonitor;
          import org.eclipse.jface.action.Action;
          import org.eclipse.jface.action.MenuManager;
          import org.eclipse.jface.dialogs.InputDialog;
          import org.eclipse.jface.viewers.ISelection;
          import org.eclipse.jface.viewers.ISelectionChangedListener;
          import org.eclipse.jface.viewers.ISelectionProvider;
          import org.eclipse.swt.SWT;
          import org.eclipse.swt.SWTError;
          import org.eclipse.swt.browser.Browser;
          import org.eclipse.swt.events.ControlEvent;
          import org.eclipse.swt.events.ControlListener;
          import org.eclipse.swt.events.PaintEvent;
          import org.eclipse.swt.events.PaintListener;
          import org.eclipse.swt.graphics.Image;
          import org.eclipse.swt.graphics.ImageData;
          import org.eclipse.swt.graphics.ImageLoader;
          import org.eclipse.swt.graphics.Rectangle;
          import org.eclipse.swt.layout.FillLayout;
          import org.eclipse.swt.layout.GridData;
          import org.eclipse.swt.layout.GridLayout;
          import org.eclipse.swt.widgets.Button;
          import org.eclipse.swt.widgets.Canvas;
          import org.eclipse.swt.widgets.Composite;
          import org.eclipse.swt.widgets.Display;
          import org.eclipse.swt.widgets.Menu;
          import org.eclipse.ui.IEditorInput;
          import org.eclipse.ui.IEditorSite;
          import org.eclipse.ui.PartInitException;
          import org.eclipse.ui.part.EditorPart;

          import com.lowagie.text.BadElementException;
          import com.lowagie.text.Document;
          import com.lowagie.text.DocumentException;
          import com.lowagie.text.PageSize;
          import com.lowagie.text.pdf.PdfWriter;
          import com.gongxun.tool.kaudit.core.HTMLCreate;
          import com.gongxun.tool.kaudit.core.xmlparser.ProjectDistrData;
          import com.gongxun.tool.kaudit.ui.graphic.Pie;
          import com.gongxun.tool.kaudit.ui.graphic.RepGraphic;



          public class CopyOfReportEditor extends EditorPart implements ISelectionProvider {

              
          public static String id = "com.gongxun.tool.kaudit.ui.reports.reporteditor";

              
          // private Image file = null;

              
          //private Pie pie = null;
              private Image file = null;
              
          private ProjectDistrData pdd = null;
              
          private RepGraphic grap= null;
              
              
          private IEditorInput input;

              
          public void doSave(IProgressMonitor monitor) {
                  
          // TODO Auto-generated method stub

              }


              
          public void doSaveAs() {
                  
          // TODO Auto-generated method stub

              }


              
          public void init(IEditorSite site, IEditorInput input)
                      
          throws PartInitException {
                  setSite(site);
                  setInput(input);
                  
          // this.file = ((ImageInput)input).getFile();
                  
          //this.pie = ((PieInput) input).getPie();
                  this.pdd = ((ReportInput) input).getPdd();
                  
          this.grap = ((ReportInput)input).getGrap();
                  
          this.input = input;
              }


              
          public boolean isDirty() {
                  
          // TODO Auto-generated method stub
                  return false;
              }


              
          public boolean isSaveAsAllowed() {
                  
          // TODO Auto-generated method stub
                  return false;
              }


              Action actionImage 
          = new Action("Export as image"{
                  @Override
                  
          public void run() {
                      
          // ImageDescriptor des = new

                      InputDialog diaImage 
          = new InputDialog(Display.getCurrent()
                              .getActiveShell(), 
          "保存路徑""請輸入圖片保存路徑以及文件名。",
                              
          "c:\\Report.jpg"null);
                      
          if (diaImage.open() == InputDialog.OK) {
                          
          try {
                              FileOutputStream wf 
          = new FileOutputStream(diaImage
                                      .getValue());
                              ImageLoader imageLoader 
          = new ImageLoader();
                              imageLoader.data 
          = new ImageData[] { file.getImageData() };
                              imageLoader.save(wf, SWT.IMAGE_JPEG);
                              wf.flush();
                              wf.close();
                          }
           catch (FileNotFoundException e1) {
                              
          // TODO Auto-generated catch block
                              e1.printStackTrace();
                          }
           catch (IOException e) {
                              
          // TODO Auto-generated catch block
                              e.printStackTrace();
                          }

                      }


                  }

              }
          ;

              Action actionPdf 
          = new Action("Export as pdf"{
                  @Override
                  
          public void run() {
                      
          // TODO Auto-generated method stub

                      Document document 
          = new Document(PageSize.A4, 50505050);

                      InputDialog diaPDF 
          = new InputDialog(Display.getCurrent()
                              .getActiveShell(), 
          "保存路徑""請輸入PDF保存路徑以及文件名。",
                              
          "c:\\Report.pdf"null);
                      
          if (diaPDF.open() == InputDialog.OK) {
                          
          try {
                              
          // 首先保存圖片
                              FileOutputStream wf = new FileOutputStream("c:\\temp.jpg");
                              ImageLoader imageLoader 
          = new ImageLoader();
                              imageLoader.data 
          = new ImageData[] { file.getImageData() };
                              imageLoader.save(wf, SWT.IMAGE_JPEG);
                              wf.flush();
                              wf.close();
                              
          // 然后保存pdf
                              FileOutputStream osPDF = new FileOutputStream(diaPDF
                                      .getValue());
                              PdfWriter writer 
          = PdfWriter.getInstance(document, osPDF);
                              document.open();
                              
          // document.add(new Paragraph("First page of the
                              
          // document."));
                              
          // document
                              
          // .add(new Paragraph(
                              
          // "Some more text on the first page with different color
                              
          // and font type.",
                              
          // FontFactory.getFont(FontFactory.COURIER, 14,
                              
          // Font.BOLD, new Color(255, 150, 200))));
                              com.lowagie.text.Image itextima = com.lowagie.text.Image
                                      .getInstance(
          "c:\\temp.jpg");
                              itextima.scalePercent(
          60);
                              document.add(itextima);
                              document.close();
                              osPDF.flush();
                              osPDF.close();
                              
          // 然后刪除圖片
                              File tmpImg = new File("c:\\temp.jpg");
                              tmpImg.delete();
                              writer.close();
                              System.out.println();
                          }
           catch (FileNotFoundException e) {
                              
          // TODO Auto-generated catch block
                              e.printStackTrace();
                          }
           catch (DocumentException e) {
                              
          // TODO Auto-generated catch block
                              e.printStackTrace();
                          }
           catch (IOException e) {
                              
          // TODO Auto-generated catch block
                              e.printStackTrace();
                          }


                      }

                  }

              }
          ;

              Action actionHtml 
          = new Action("Export as html"{
                  @Override
                  
          public void run() {
                      
          // TODO Auto-generated method stub

                      HTMLCreate htmlCreate 
          = new HTMLCreate();
                      
          try {
                          htmlCreate.createHTML();
                      }
           catch (BadElementException e) {
                          
          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
           catch (MalformedURLException e) {
                          
          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
           catch (DocumentException e) {
                          
          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
           catch (IOException e) {
                          
          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }

                  }

              }
          ;

              
          public void createPartControl(Composite parent) {

                  
                  Composite comp 
          = new Composite(parent, SWT.NONE);
                  GridLayout gl 
          = new GridLayout();
                  gl.numColumns 
          = 3;
                  comp.setLayout(gl);
                  
          new Button(comp,SWT.NONE).setText("導出Html");
                  
          new Button(comp,SWT.NONE).setText("導出Image");
                  
          new Button(comp,SWT.NONE).setText("導出PDF");
                  
                  createMyPartControl(comp);
                  
              }



              
          public void createMyPartControl(Composite parent){
                  

                  
          //new PartCanvas(parent,input);
                  
                  
                  Canvas canvas 
          = new Canvas(parent, SWT.NONE);
                  GridData gd 
          = new GridData(GridData.FILL_HORIZONTAL|GridData.FILL_VERTICAL);
                  gd.horizontalSpan 
          = 3;
                  canvas.setLayoutData(gd);
                  

                  
                  
                  canvas.addPaintListener(
          new PaintListener() {

                      
          public void paintControl(PaintEvent e) {
                          Canvas canvas 
          = (Canvas) e.getSource();
                          Rectangle re 
          = canvas.getClientArea();
                          
          final Rectangle adjustedRe = new Rectangle(00, re.width,
                                  re.height);
                          file 
          = new Image(Display.getCurrent(), adjustedRe);
                          grap.update(file, adjustedRe,pdd);
                          e.gc.drawImage(file, 
          00);

                      }


                  }
          );

                  canvas.addControlListener(
          new ControlListener() {

                      
          public void controlMoved(ControlEvent e) {

                      }


                      
          public void controlResized(ControlEvent e) {

                      }


                  }
          );

                  MenuManager manager 
          = new MenuManager("#PopupMenu");
                  manager.add(actionImage);
                  manager.add(actionPdf);
                  manager.add(actionHtml);
                  Menu menu 
          = manager.createContextMenu(canvas);
                  canvas.setMenu(menu);
                  
          this.getEditorSite().registerContextMenu(manager, this);
                  
              }

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

              }


              @Override
              
          public void addSelectionChangedListener(ISelectionChangedListener listener) {
                  
          // TODO Auto-generated method stub

              }


              @Override
              
          public ISelection getSelection() {
                  
          // TODO Auto-generated method stub
                  return null;
              }


              @Override
              
          public void removeSelectionChangedListener(
                      ISelectionChangedListener listener) 
          {
                  
          // TODO Auto-generated method stub

              }


              @Override
              
          public void setSelection(ISelection selection) {
                  
          // TODO Auto-generated method stub

              }


          }

          主站蜘蛛池模板: 岳普湖县| 威海市| 正镶白旗| 准格尔旗| 福州市| 望都县| 临颍县| 南康市| 定安县| 北安市| 瑞昌市| 白玉县| 渝北区| 西吉县| 汪清县| 临泉县| 剑河县| 昆明市| 万安县| 朝阳县| 新巴尔虎右旗| 镇沅| 阿拉善右旗| 佳木斯市| 西乌珠穆沁旗| 迁安市| 尼木县| 栖霞市| 瑞安市| 石柱| 八宿县| 栾城县| 桐柏县| 大渡口区| 建始县| 庆城县| 乌什县| 新宁县| 东乡族自治县| 苏尼特左旗| 海晏县|