fitimageaction.java

来自「mywork是rcp开发的很好的例子」· Java 代码 · 共 61 行

JAVA
61
字号
package net.sf.freenote.action;

import java.io.File;

import net.sf.freenote.FreeNoteConstants;
import net.sf.freenote.commands.ShapeSetConstraintCommand;
import net.sf.freenote.model.ImageFileShape;
import net.sf.freenote.parts.ShapeEditPart;
import net.sf.util.StringUtil;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.ui.actions.SelectionAction;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.ui.IWorkbenchPart;

/**
 * 依据图片调整形状的大小
 * @author levin
 * @since 2008-1-29 下午12:22:39
 */
public class FitImageAction extends SelectionAction {

	public FitImageAction(IWorkbenchPart part) {
		super(part);
		setText("适应图片");
		setToolTipText("调整大小以显示整张图片");
		setId(FreeNoteConstants.FIT_IMAGE);
	}

	@Override
	protected boolean calculateEnabled() {
		if (getSelectedObjects().size() == 1 && (getSelectedObjects().get(0) instanceof ShapeEditPart)) {
			ShapeEditPart part = (ShapeEditPart) getSelectedObjects().get(0);
			if(part.getModel() instanceof ImageFileShape && StringUtil.isNotNull(((ImageFileShape)part.getModel()).getFilePath()))
				return true;
		}
		return false;
	}

	@Override
	public void run() {
		try{
			ShapeEditPart part = (ShapeEditPart) getSelectedObjects().get(0);
			ImageFileShape shape=(ImageFileShape) part.getModel();
			ImageData image = ImageDescriptor.createFromURL(new File(shape.getFilePath()).toURI().toURL()).getImageData();
			if(!(shape.getSize().equals(image.width,image.height))){
				Rectangle rect=new Rectangle();
				rect.setLocation(shape.getLocation());
				rect.setSize(image.width, image.height);
				ShapeSetConstraintCommand command=new ShapeSetConstraintCommand(shape,new ChangeBoundsRequest(RequestConstants.REQ_RESIZE),rect);
				getCommandStack().execute(command);
			}
		}catch(Exception ex){
			
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?