Back to index

moin  1.9.0~rc2
AlignCommand.java
Go to the documentation of this file.
00001 /*
00002  * @(#)AlignCommand.java 5.1
00003  *
00004  */
00005 
00006 package CH.ifa.draw.standard;
00007 
00008 import java.util.*;
00009 import java.awt.*;
00010 
00011 import CH.ifa.draw.util.Command;
00012 import CH.ifa.draw.framework.*;
00013 
00017 public class AlignCommand extends Command {
00018 
00019     private DrawingView fView;
00020     private int fOp;
00021 
00025     public final static int LEFTS = 0;
00029     public final static int CENTERS = 1;
00033     public final static int RIGHTS = 2;
00037     public final static int TOPS = 3;
00041     public final static int MIDDLES = 4;
00045     public final static int BOTTOMS = 5;
00046 
00047 
00054     public AlignCommand(String name, DrawingView view, int op) {
00055         super(name);
00056         fView = view;
00057         fOp = op;
00058     }
00059 
00060     public boolean isExecutable() {
00061         return fView.selectionCount() > 1;
00062     }
00063 
00064     public void execute() {
00065         FigureEnumeration selection = fView.selectionElements();
00066         Figure anchorFigure = selection.nextFigure();
00067         Rectangle r = anchorFigure.displayBox();
00068 
00069         while (selection.hasMoreElements()) {
00070             Figure f = selection.nextFigure();
00071             Rectangle rr = f.displayBox();
00072             switch (fOp) {
00073             case LEFTS:
00074                 f.moveBy(r.x-rr.x, 0);
00075                 break;
00076             case CENTERS:
00077                 f.moveBy((r.x+r.width/2) - (rr.x+rr.width/2), 0);
00078                 break;
00079             case RIGHTS:
00080                 f.moveBy((r.x+r.width) - (rr.x+rr.width), 0);
00081                 break;
00082             case TOPS:
00083                 f.moveBy(0, r.y-rr.y);
00084                 break;
00085             case MIDDLES:
00086                 f.moveBy(0, (r.y+r.height/2) - (rr.y+rr.height/2));
00087                 break;
00088             case BOTTOMS:
00089                 f.moveBy(0, (r.y+r.height) - (rr.y+rr.height));
00090                 break;
00091             }
00092         }
00093         fView.checkDamage();
00094     }
00095 }
00096 
00097