?? associatetool.java
字號:
package com.sunking.tp.tool;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import java.awt.geom.*;
import com.sunking.tp.framework.*;
import com.sunking.tp.util.*;
/**
*
* <p>Title: </p>
* <p>Description:連線工具 </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
* @version 1.0
*/
public class AssociateTool extends AbstractTool {
/**
*畫虛線用的Stroke
*/
BasicStroke stroke = new BasicStroke(1.0f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_ROUND, 140.0f, new float[] {1.0f}
, 150.0f);
Line2D oldLine;
/**
*連線源
*/
JTPComponent source = null;
/**
*連線類型
*/
int assoicateType;
boolean hasRepaint = false;
/**
* @param desk 所屬繪圖桌面
* @param assoicateType 連線類型
* @param icon 圖標
*/
public AssociateTool(Desktop desk, int assoicateType, Icon icon) {
super(desk);
this.assoicateType = assoicateType;
setToolTip(JTPUtil.getString("AssociateTool_TIP"));
setIcon(icon);
}
/**
*啟動工具
*/
public void enabledTool() {
super.enabledTool();
source = null;
oldLine = null;
}
/**
*棄用工具
*/
public void disabledTool() {
super.disabledTool();
source = null;
oldLine = null;
}
public void mouseDragged(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
//設置起點和終點
JTPComponent dest = desk.getJTPComponent(e.getPoint());
if (dest != null) {
if (source == null) {
source = dest;
source.setSelect(true);
Component cSource = (Component) source;
oldLine = new Line2D.Float();
int x = cSource.getX() + (cSource.getWidth() / 2);
int y = cSource.getY() + (cSource.getHeight() / 2);
oldLine.setLine(x, y, x, y);
} else {
if (source != e.getSource()
&& !source.hasAssociator(dest)
&& !dest.hasAssociator(source)) {
int type = UndoableEditFactory.CREATE_ASSOICATORLINE_0;
if (assoicateType == Assoicator.ASSOICATETYPE_2) {
type = UndoableEditFactory.CREATE_ASSOICATORLINE_1;
} else if (assoicateType == Assoicator.ASSOICATETYPE_3) {
type = UndoableEditFactory.CREATE_ASSOICATORLINE_2;
}
UndoableEditFactory.getDefault().startEdit(type, desk,
new AssoicatorLine(source,new Assoicator(dest,assoicateType)));
UndoableEditFactory.getDefault().stopEdit(null);
source.addAssociator(dest, assoicateType);
}
source.setSelect(false);
source = null;
oldLine = null;
desk.fireAssoicatorChanged();
}
desk.mouseSelect(e.getPoint());
} else if (e.getSource()instanceof Desktop) {
if (source != null) {
source.setSelect(false);
source = null;
}
desk.mouseSelect(e.getPoint());
( (Container) desk).repaint();
hasRepaint = true;
}
}
Component getDesktopPane(Component c) {
if (c instanceof Desktop)
return c;
c = c.getParent();
if (c == null)
return null;
return getDesktopPane(c);
}
public void mouseMoved(MouseEvent e) {
if (oldLine == null)
return;
int x = e.getX(), y = e.getY();
//畫虛線
Component desktop = getDesktopPane(e.getComponent());
Graphics2D g2 = (Graphics2D) (desktop.getGraphics());
g2.setStroke(stroke);
g2.setXORMode(desktop.getBackground());
if (oldLine != null && !hasRepaint) {
g2.drawLine( (int) oldLine.getX1(), (int) oldLine.getY1(),
(int) oldLine.getX2(), (int) oldLine.getY2());
}
hasRepaint = false;
oldLine.setLine(oldLine.getX1(), oldLine.getY1(), e.getX(), e.getY());
g2.drawLine( (int) oldLine.getX1(), (int) oldLine.getY1(),
(int) oldLine.getX2(), (int) oldLine.getY2());
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -