📄 designpanel.java
字号:
//本面板分为左中右三个部分从左至右分别为 RegisteredClassPane、designScrollPane、propertyEdit;import com.borland.jbcl.layout.XYConstraints;import com.borland.jbcl.layout.XYLayout;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.*;import java.net.URL;import java.util.*;/** * Title: work-flow * Description: * Copyright: Copyright (c) 2002 * Company: CSU * @author smallbeetle * @version 1.0 */public class DesignPanel extends JPanel implements MouseListener, MouseMotionListener, KeyListener{ static HashMap carriers; static HashMap connectors; static String mouseOrient = ""; DeviceCarrier selectedCarrier; DeviceCarrier mouseSelectedCarrier=null; DeviceConnector selectedConnecor; String mouseStatus = null; boolean mouseGetIn = false; boolean yFlag = false; int width,height; XYLayout paneLayout = new XYLayout(2000, 5000); XYConstraints canvasConstraints = new XYConstraints(0, 0, 2000, 5000); RegisteredClassPane registeredClassPane = new RegisteredClassPane(); //显示树形结构的面板// PropertyEdit propertyEdit = new PropertyEdit(this); DrawCanvas drawCanvas = new DrawCanvas(); private MainWindow mainWindow; private JSplitPane jSplitPane1 = new JSplitPane(); private JSplitPane jSplitPane2 = new JSplitPane(); private JScrollPane designScrollPane; private Graphics canvasG; private String dragAction = ""; private Point oldLocation = new Point(0, 0); private DeviceConnector tConnector; private Point hotLeadArea; private boolean editable = true; private File workFile; //used to save the experiment. public DesignPanel(MainWindow pMainWindow) { try { jbInit(pMainWindow); } catch(Exception ex) { ex.printStackTrace(); } }//user wan to start a new experiment.we will clear the experiment.//when user use the same device for more times, we must give the device a new name private String newName(String pref){ if(pref == null) pref = ""; int i = 0; while(this.carriers.get(pref+i) != null || this.connectors.get(pref+i) != null){ i++; } return pref+i; }// private String getFile(){ ComboBoxModel model = new DefaultComboBoxModel(); String msg = "输入文件的internet地址(URL)"; JComboBox cb = new JComboBox(model); return ""; }/* public void saveExperimentAs(){ workFile = null; try{ saveExperiment(); }catch(Exception ex){ } } */ //open new experiment public void loadExperiment(){ } public synchronized boolean getEditable(){ return editable; } public synchronized void setEditable(boolean newEditable){ editable = newEditable; } public void setPaneSize(int width, int height) throws IllegalArgumentException{ if (width > 65535 || width < 1000 || height < 1000 || height > 65535){ throw new IllegalArgumentException("illegal arguments"); }else{ this.paneLayout.setHeight(height); this.paneLayout.setWidth(width); this.canvasConstraints.setHeight(height) ; this.canvasConstraints.setWidth(width); } } public void mousePressed(MouseEvent e){ this.setSelected(e.getPoint(), true) ; if (e.getModifiers()==MouseEvent.BUTTON3_MASK) { dragAction=""; setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); tConnector=null; } if (mouseStatus!=null && mouseStatus.equals("hand_cursor") && (e.getModifiers()==MouseEvent.BUTTON1_MASK)){ ClassNode node = registeredClassPane.getSelectedNode(); if (node!=null && node.name.equals("Connector") && mouseGetIn){ tConnector = new DeviceConnector(); tConnector.connectorStatus="tConnector"; Point[] handler = tConnector.getHandler() ; for (int i = 0; i < handler.length; i++){ handler[i] = new Point(this.selectedCarrier.getX()+this.selectedCarrier.getWidth()/4, this.selectedCarrier.getY()+this.selectedCarrier.getHeight()); } yFlag = true; tConnector.setInDeviceCarrierName(this.mouseSelectedCarrier.getName()) ; this.selectedCarrier = null; tConnector.setSelected(false); dragAction = "AddConnector"; setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); setSelected(e.getPoint(), false); } if (node!=null && !node.name.equals("Connector")) addCarrier(node.name, e.getPoint()); else setSelected(e.getPoint(), false); this.drawCanvas.repaint(); } else{ if (this.selectedConnecor!= null ){ Point[] handler = this.selectedConnecor.getHandler(); Point h1 = this.selectedConnecor.getHandler(e.getPoint()); if (h1 != null){ dragAction = "MoveConnector"; setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR)); return; } } if (this.selectedCarrier != null){ oldLocation.setLocation(e.getPoint()); setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); dragAction = "MoveCarrier"; } } mouseStatus = "null"; //将鼠标地标志设为空,表示已经退出了添加控件的状态 } public void mouseClicked(MouseEvent e){ mainWindow.setStatus(e.getPoint().toString() ); }//show the message in the status bar public void mouseMoved(MouseEvent e){ if (mouseStatus!=null && mouseStatus.equals("hand_cursor")){ ClassNode node = registeredClassPane.getSelectedNode(); if (node!=null && node.name.equals("Connector")){ Set entries = carriers.entrySet(); Iterator iterator =entries.iterator(); this.hotLeadArea = null; mouseGetIn = false; while(iterator.hasNext()){ Map.Entry entry = (Map.Entry)iterator.next(); DeviceCarrier carrier = (DeviceCarrier) entry.getValue(); int x =carrier.getX(); int y =carrier.getY(); Point temp = new Point(e.getPoint()); width = carrier.getWidth(); height = carrier.getHeight(); if (Intersect.isIntersect(temp,new Rectangle(x,y,width,height))) { this.hotLeadArea = new Point(x-2,y-2); this.mouseSelectedCarrier= carrier; mouseGetIn = true; drawCanvas.repaint() ; return; } } } return; } } private void moveConnector(MouseEvent e){ Point[] ha1 = this.selectedConnecor.getHandler(); int y1 = e.getY(); ha1[1].y = y1; ha1[2].y = y1; this.drawCanvas.repaint(); return; } private void moveCarrier(MouseEvent e){ int offsetX = oldLocation.x - e.getX() ; //要移动地相对距离 int offsetY = oldLocation.y - e.getY() ; if (offsetY<0) mouseOrient = "south"; else mouseOrient = "north"; int x = this.selectedCarrier.getX() - offsetX; int y = this.selectedCarrier.getY() - offsetY; if (x < 0 || y < 0 || x > drawCanvas.getWidth() - this.selectedCarrier.getWidth() || y > drawCanvas.getHeight() - this.selectedCarrier.getHeight()) return; Set entries = connectors.entrySet(); Iterator iterator = entries.iterator() ; DeviceConnector connector; while (iterator.hasNext()){ Map.Entry entry = (Map.Entry)iterator.next(); connector =(DeviceConnector)entry.getValue(); if (connector.getInDeviceCarrierName().equals(this.selectedCarrier.getName())){ Point[] handler = connector.getHandler(); if (handler[0].y<handler[3].y){ handler[0] = new Point(handler[0].x - offsetX, handler[0].y - offsetY); int y12 = handler[0].y + 10 > handler[1].y ? handler[0].y + 10 : handler[1].y ; handler[1] = new Point(handler[0].x, y12); handler[2] = new Point(handler[3].x, y12); } else{ handler[0] = new Point(handler[0].x - offsetX, handler[0].y - offsetY); int y12 = handler[0].y - 10 < handler[1].y ? handler[0].y - 10 : handler[1].y ; handler[1] = new Point(handler[0].x, y12); handler[2] = new Point(handler[3].x, y12); } } if (connector.getOutDeviceCarrierName().equals(this.selectedCarrier.getName())){ DeviceCarrier dc = (DeviceCarrier)carriers.get(connector.getInDeviceCarrierName()); Point[] handler = connector.getHandler(); if (handler[0].y<handler[3].y){ handler[3] = new Point(handler[3].x - offsetX, handler[3].y - offsetY); int y12 = handler[3].y - 10 < handler[1].y ? handler[3].y - 10 : handler[1].y ; handler[1] = new Point(handler[0].x, y12); handler[2] = new Point(handler[3].x, y12); }else{ handler[3] = new Point(handler[3].x - offsetX, handler[3].y - offsetY); int y12 = handler[3].y + 10 > handler[1].y ? handler[3].y - 10 : handler[1].y ; handler[1] = new Point(handler[0].x, y12); handler[2] = new Point(handler[3].x, y12); } } DeviceConnector.adjustConnector(connector); } this.selectedCarrier.setX(x) ; this.selectedCarrier.setY(y) ; this.drawCanvas.repaint(); this.oldLocation.setLocation(e.getPoint()) ; } public void mouseDragged(MouseEvent e){ if (dragAction == "MoveConnector" ){ moveConnector(e); }else if (this.dragAction.equals("MoveCarrier")){ moveCarrier(e); }else if (dragAction.equals("AddConnector")){ addConnector(e); } drawCanvas.repaint() ; } public Point caculateLocation(DeviceConnector tConn){ Point tempoint= null; String inDevice = null; inDevice = tConn.getInDeviceCarrierName(); Set entries = carriers.entrySet(); Iterator iterator =entries.iterator(); while(iterator.hasNext()){ Map.Entry entry = (Map.Entry)iterator.next(); DeviceCarrier carrier = (DeviceCarrier) entry.getValue(); int x =carrier.getX(); int y =carrier.getY(); if (carrier.getName().equals(inDevice) && tConn.getHandler()[0].y<=y){ Point[] tempoints = tConn.getHandler(); tempoints[3].x = hotLeadArea.x+width/2; tempoints[3].y = hotLeadArea.y+height+2; this.tConnector.setHandler(tempoints); tempoint = new Point(tempoints[3]); return tempoint; } } return new Point(hotLeadArea.x+width/2,hotLeadArea.y+2); } public void mouseReleased(MouseEvent e){ //防止入口和出口的模块相同 if (tConnector!=null && tConnector.getOutDeviceCarrierName()!=null &&tConnector.getInDeviceCarrierName()==tConnector.getOutDeviceCarrierName()) { return; } if (hotLeadArea != null && tConnector != null){ // connector two DeviceCarriers String name = newName("CN");; DeviceConnector dcon = new DeviceConnector();/*箭头发出的模块为inDevice,箭头进入的模块为outDevice. 所以对inDevice而言记录connectorOut,对outDevice而言记录connectorIn*/ DeviceCarrier outDevice =(DeviceCarrier) this.carriers.get(tConnector.getOutDeviceCarrierName()); DeviceCarrier inDevice = (DeviceCarrier) this.carriers.get(tConnector.getInDeviceCarrierName()); outDevice.connectorNum++; //将与此连接相连的模块的连接数加1 inDevice.connectorNum++; Point[] p = this.tConnector.getHandler() ; p[2].setLocation(hotLeadArea.x+width/2, p[2].y) ; p[3].setLocation(caculateLocation(tConnector)) ; dcon.setHandler(this.tConnector.getHandler()); dcon.setInDeviceCarrierName(this.tConnector.getInDeviceCarrierName()); dcon.setOutDeviceCarrierName(this.tConnector.getOutDeviceCarrierName()); dcon.setName(name); connectors.put(name, dcon); dcon.setSelected(false) ; drawCanvas.repaint(); } this.hotLeadArea = null; this.tConnector = null; registeredClassPane.classTree.setSelectionPath(null); setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); this.dragAction = ""; drawCanvas.repaint(); } /*当鼠标进入部件区域时调用mouseEntered()方法,当鼠标从部件区域里开始调用mouseExited() 取得所要选择的模块,在RegisteredClassPane中valueChanged方法先取得slectedNode,再调用 registeredClassPane中的getSelectedNode方法*/ public void mouseEntered(MouseEvent e){ int type = -1; ClassNode node = registeredClassPane.getSelectedNode(); if (node != null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -