?? selectionmodel.java
字號:
//設置位置,移動20x20,防止被粘帖的組件重疊,照顧空布局情況下
comp.setLocation(comp.getX() + 20, comp.getY() + 20);
//使用適配器克隆組件
ComponentAdapter adapter = AdapterBus.getComponentAdapter(designer, comp);
Component clone = adapter.clone();
clone.setName(clone.getName() + "_copy");
//在容器中添加該組件
container.add(clone);
}
clip_board.clear();
//重新布局
Util.layoutContainer(container);
//觸發事件
fireComponentPaste();
designer.repaint();
}
} else {
//不可以粘帖,警告
Toolkit.getDefaultToolkit().beep();
}
}
private void fireComponentPaste() {
DesignerEvent evt = new DesignerEvent(this);
evt.setPastedComponents(clip_board);
designer.getEditListenerTable().fireComponentPasted(evt);
}
/**
* 當前所選中的組件容器是否可以粘帖剪貼版內的組件
*/
public boolean isPasteable() {
if (selection.size() != 1) {
//必須只有一個容器組件
return false;
}
Component comp = selection.get(0);
ComponentAdapter adapter = AdapterBus.getComponentAdapter(designer, comp);
//并且該唯一的組件是個容器
return adapter instanceof ContainerAdapter;
}
//向左對齊
public void adjustLeftAlignment() {
if (isBoundsAdjustable()) {
Component comp = selection.get(0);
int x = comp.getX();
for (Component current : selection) {
current.setLocation(x, current.getY());
}
}
}
//向右對齊
public void adjustRightAlignment() {
if (isBoundsAdjustable()) {
Component comp = selection.get(0);
int right_x = comp.getX() + comp.getWidth();
for (Component current : selection) {
current.setLocation(right_x - current.getWidth(), current.getY());
}
}
}
//向中對齊
public void adjustCenterAlignment() {
if (isBoundsAdjustable()) {
Component comp = selection.get(0);
int center_x = comp.getX() + (comp.getWidth() / 2);
for (Component current : selection) {
current.setLocation(center_x - (current.getWidth() / 2), current.getY());
}
}
}
//向上對齊
public void adjustTopAlignment() {
if (isBoundsAdjustable()) {
Component comp = selection.get(0);
int y = comp.getY();
for (Component current : selection) {
current.setLocation(current.getX(), y);
}
}
}
//向下對齊
public void adjustBottomAlignment() {
if (isBoundsAdjustable()) {
Component comp = selection.get(0);
int bottom_y = comp.getY() + comp.getHeight();
for (Component current : selection) {
current.setLocation(current.getX(), bottom_y - current.getHeight());
}
}
}
//向中對齊
public void adjustMiddleAlignment() {
if (isBoundsAdjustable()) {
Component comp = selection.get(0);
int middle_y = comp.getY() + (comp.getHeight() / 2);
for (Component current : selection) {
current.setLocation(current.getX(), middle_y - (current.getHeight() / 2));
}
}
}
//同一寬度
public void adjustSameWidth() {
if (isBoundsAdjustable()) {
Component comp = selection.get(0);
int width = comp.getWidth();
for (Component current : selection) {
current.setSize(width, current.getHeight());
}
}
}
//同一高度
public void adjustSameHeight() {
if (isBoundsAdjustable()) {
Component comp = selection.get(0);
int height = comp.getHeight();
for (Component current : selection) {
current.setSize(current.getWidth(), height);
}
}
}
public void clearSelection() {
selection.clear();
}
public void clearClipBoard() {
clip_board.clear();
}
public void addComp2ClipBoard(Component comp) {
clip_board.add(comp);
}
public void removeCompFromClipBoard(Component comp) {
clip_board.remove(comp);
}
public void addComp2ClipBoard(Collection<? extends Component> comps) {
clip_board.addAll(comps);
}
public boolean hasSelection() {
return !selection.isEmpty();
}
public boolean isRootSelected() {
return hasSelection() && selection.contains(designer.getRootComponent());
}
public boolean isClipBoardEmpty() {
return clip_board.isEmpty();
}
public boolean isBoundsAdjustable() {
return isSelectedResizable() && (selection.size() > 1);
}
/**
* 刪除當前所有選擇的組件
*/
public void deleteSelection() {
//找出所有選中組件的根部
ArrayList<Component> roots = getSelectedRoots();
if (!roots.isEmpty()) {
for (Component component : roots) {
removeBeanFromContainer(component);
}
//觸發事件
fireComponentDeleted();
//清除被選中的組件
clearSelection();
designer.repaint();
}
}
private void fireComponentDeleted() {
DesignerEvent evt = new DesignerEvent(this);
evt.setDeletedComponents(selection);
designer.getEditListenerTable().fireComponentDeleted(evt);
}
public void removeComponent(Component component) {
if (selection.contains(component)) {
selection.remove(component);
}
//從頂層容器中清除這些根組件
removeBeanFromContainer(component);
designer.repaint();
}
public void setHotspotBounds(Rectangle rect) {
hotspot_bounds = rect;
}
public Rectangle getHotspotBounds() {
return hotspot_bounds;
}
public void setSelectedComponents(Collection<Component> components) {
selection.clear();
selection.addAll(components);
fireComponentSelected();
}
public ArrayList<Component> getSelectedComponents() {
return selection;
}
public ArrayList<Component> getClipBoard() {
return clip_board;
}
private void removeBeanFromContainer(Component component) {
Container parent = component.getParent();
if(parent instanceof JViewport){
parent=parent.getParent();
}
ComponentAdapter adapter = AdapterBus.getComponentAdapter(designer, parent);
if (adapter instanceof ContainerAdapter) {
((ContainerAdapter)adapter).removeComponent(component);
} else {
//刪除其根組件,同時就刪除了同時被選擇的葉子組件
parent.remove(component);
}
LayoutManager layout = parent.getLayout();
if (layout != null) {
//刷新組件容器的布局
Util.layoutContainer(parent);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -