You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
240 lines
8.2 KiB
240 lines
8.2 KiB
package com.fr.design.mainframe; |
|
|
|
import com.fr.design.designer.creator.XCreator; |
|
import com.fr.design.designer.creator.XCreatorUtils; |
|
import com.fr.design.designer.creator.XLayoutContainer; |
|
import com.fr.design.designer.creator.XWFitLayout; |
|
import com.fr.design.designer.creator.XWParameterLayout; |
|
import com.fr.design.designer.creator.cardlayout.XWCardTagLayout; |
|
import com.fr.form.ui.container.WLayout; |
|
|
|
import java.awt.Rectangle; |
|
import java.util.Arrays; |
|
import java.util.Collections; |
|
import java.util.Comparator; |
|
import java.util.List; |
|
|
|
public class MultiSelectionArrangement { |
|
private FormDesigner designer; |
|
private XLayoutContainer parent; // 当前选中的那些组件所在父容器 |
|
private List<XCreator> selectedCreators; |
|
private Rectangle rec; |
|
|
|
public MultiSelectionArrangement(FormDesigner designer) { |
|
this.designer = designer; |
|
update(); |
|
} |
|
|
|
public void leftAlign() { |
|
for (XCreator creator : selectedCreators) { |
|
creator.setLocation(rec.x, creator.getY()); |
|
} |
|
update(); |
|
} |
|
|
|
public void rightAlign() { |
|
for (XCreator creator : selectedCreators) { |
|
creator.setLocation(rec.x + rec.width - creator.getWidth(), creator.getY()); |
|
} |
|
update(); |
|
} |
|
|
|
public void topAlign() { |
|
for (XCreator creator : selectedCreators) { |
|
creator.setLocation(creator.getX(), rec.y); |
|
} |
|
update(); |
|
} |
|
|
|
public void bottomAlign() { |
|
for (XCreator creator : selectedCreators) { |
|
creator.setLocation(creator.getX(), rec.y + rec.height - creator.getHeight()); |
|
} |
|
update(); |
|
} |
|
|
|
public void horizontalCenterAlign() { |
|
for (XCreator creator : selectedCreators) { |
|
creator.setLocation(rec.x + rec.width / 2 - creator.getWidth() / 2, creator.getY()); |
|
} |
|
update(); |
|
} |
|
|
|
public void verticalCenterAlign() { |
|
for (XCreator creator : selectedCreators) { |
|
creator.setLocation(creator.getX(), rec.y + rec.height / 2 - creator.getHeight() / 2); |
|
} |
|
update(); |
|
} |
|
|
|
// 水平分布,自动,间距由selectedCreators和border共同计算而来 |
|
public void horizontalAutoDistribution() { |
|
int gap = calculateHorizontalGap(); |
|
horizontalDistribution(gap); |
|
} |
|
|
|
// 水平分布,手动,传入一个间距,排列selectedCreators |
|
public void horizontalManualDistribution(int gap) { |
|
reSizeRecByHorizontal(gap); |
|
horizontalDistribution(gap); |
|
} |
|
|
|
private void horizontalDistribution(int gap) { |
|
sortHorizontal(); |
|
for (int i = 1; i < selectedCreators.size() - 1; i++) { |
|
XCreator creator = selectedCreators.get(i); |
|
XCreator preCreator = selectedCreators.get(i - 1); |
|
creator.setLocation(preCreator.getX() + preCreator.getWidth() + gap, creator.getY()); |
|
} |
|
update(); |
|
} |
|
|
|
private void reSizeRecByHorizontal(int gap) { |
|
sortHorizontal(); |
|
int width = 0; |
|
for (XCreator creator : selectedCreators) { |
|
width += creator.getWidth(); |
|
} |
|
width += (selectedCreators.size() - 1) * gap; |
|
rec.x = rec.x + (rec.width - width) / 2; |
|
rec.width = width; |
|
XCreator first = selectedCreators.get(0); |
|
first.setLocation(rec.x, first.getY()); |
|
XCreator last = selectedCreators.get(selectedCreators.size() - 1); |
|
last.setLocation(rec.x + rec.width - last.getWidth(), last.getY()); |
|
} |
|
|
|
private void sortHorizontal() { |
|
Collections.sort(selectedCreators, new Comparator<XCreator>() { |
|
@Override |
|
public int compare(XCreator o1, XCreator o2) { |
|
int diffX = o1.getX() - o2.getX(); |
|
if (diffX > 0) { |
|
return 1; |
|
} else if (diffX < 0) { |
|
return -1; |
|
} else { |
|
int diffY = o1.getY() - o2.getY(); |
|
if (diffY > 0) { |
|
return 1; |
|
} else if (diffY < 0) { |
|
return -1; |
|
} else { |
|
int diffZOrder = o1.getParent().getComponentZOrder(o1) - o2.getParent().getComponentZOrder(o2); |
|
if (diffZOrder > 0) { |
|
return -1; |
|
} else { |
|
return 1; |
|
} |
|
} |
|
} |
|
} |
|
}); |
|
} |
|
|
|
// 计算selectedCreators的均分间距 |
|
private int calculateHorizontalGap() { |
|
int sum = 0; |
|
for (XCreator creator : selectedCreators) { |
|
sum += creator.getWidth(); |
|
} |
|
return (rec.width - sum) / (selectedCreators.size() - 1); |
|
} |
|
|
|
public void verticalAutoDistribution() { |
|
int gap = calculateVerticalGap(); |
|
verticalDistribution(gap); |
|
} |
|
|
|
public void verticalManualDistribution(int gap) { |
|
reSizeRecByVertical(gap); |
|
verticalDistribution(gap); |
|
} |
|
|
|
private void verticalDistribution(int gap) { |
|
sortVertical(); |
|
for (int i = 1; i < selectedCreators.size() - 1; i++) { |
|
XCreator creator = selectedCreators.get(i); |
|
XCreator preCreator = selectedCreators.get(i - 1); |
|
creator.setLocation(creator.getX(), preCreator.getY() + preCreator.getHeight() + gap); |
|
} |
|
update(); |
|
} |
|
|
|
private void reSizeRecByVertical(int gap) { |
|
sortVertical(); |
|
int height = 0; |
|
for (XCreator creator : selectedCreators) { |
|
height += creator.getHeight(); |
|
} |
|
height += (selectedCreators.size() - 1) * gap; |
|
rec.y = rec.y + (rec.height - height) / 2; |
|
rec.height = height; |
|
XCreator first = selectedCreators.get(0); |
|
first.setLocation(first.getX(), rec.y); |
|
XCreator last = selectedCreators.get(selectedCreators.size() - 1); |
|
last.setLocation(last.getX(), rec.y + rec.height - last.getHeight()); |
|
} |
|
|
|
private void sortVertical() { |
|
Collections.sort(selectedCreators, new Comparator<XCreator>() { |
|
@Override |
|
public int compare(XCreator o1, XCreator o2) { |
|
int diffY = o1.getY() - o2.getY(); |
|
if (diffY > 0) { |
|
return 1; |
|
} else if (diffY < 0) { |
|
return -1; |
|
} else { |
|
int diffX = o1.getX() - o2.getX(); |
|
if (diffX > 0) { |
|
return 1; |
|
} else if (diffX < 0) { |
|
return -1; |
|
} else { |
|
int diffZOrder = o1.getParent().getComponentZOrder(o1) - o2.getParent().getComponentZOrder(o2); |
|
if (diffZOrder > 0) { |
|
return -1; |
|
} else { |
|
return 1; |
|
} |
|
} |
|
} |
|
} |
|
}); |
|
} |
|
|
|
private int calculateVerticalGap() { |
|
int sum = 0; |
|
for (XCreator creator : selectedCreators) { |
|
sum += creator.getHeight(); |
|
} |
|
return (rec.height - sum) / (selectedCreators.size() - 1); |
|
} |
|
|
|
private void update() { |
|
FormSelection selection = designer.getSelectionModel().getSelection(); |
|
this.selectedCreators = Arrays.asList(selection.getSelectedCreators()); |
|
this.rec = selection.getSelctionBounds(); |
|
this.parent = getParent(selection.getSelectedCreator()); |
|
|
|
if (parent != null) { |
|
// 这里要修改修改engine里面的对象才能成功保存,光修改设计器对象没用 |
|
WLayout wabs = parent.toData(); |
|
for (XCreator creator : selectedCreators) { |
|
wabs.setBounds(creator.toData(), creator.getBounds()); |
|
} |
|
} |
|
} |
|
|
|
private XLayoutContainer getParent(XCreator source) { |
|
if(source.acceptType(XWCardTagLayout.class)){ |
|
return (XLayoutContainer)source.getParent(); |
|
} |
|
XLayoutContainer container = XCreatorUtils.getParentXLayoutContainer(source); |
|
if (source.acceptType(XWFitLayout.class) || source.acceptType(XWParameterLayout.class)) { |
|
container = null; |
|
} |
|
return container; |
|
} |
|
}
|
|
|