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

在editor中加入右键action的实例代码

Posted on 2008-02-19 21:30 Raul Gong 阅读(1066) 评论(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

    }


}


只有注册用户登录后才能发表评论。


网站导航: