📄 sequencediagram.java
字号:
/* * USE - UML based specification environment * Copyright (C) 1999-2004 Mark Richters, University of Bremen * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* $ProjectHeader: use 2-3-0-release.1 Mon, 12 Sep 2005 20:18:33 +0200 green $ */package org.tzi.use.gui.views;import java.awt.*;import java.awt.event.*;import java.awt.print.*;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Stack;import javax.swing.*;import org.tzi.use.gui.util.PopupListener;import org.tzi.use.uml.ocl.value.Value;import org.tzi.use.uml.sys.MCmd;import org.tzi.use.uml.sys.MCmdOpEnter;import org.tzi.use.uml.sys.MCmdOpExit;import org.tzi.use.uml.sys.MObject;import org.tzi.use.uml.sys.MOperationCall;import org.tzi.use.uml.sys.MSystem;import org.tzi.use.util.StringUtil;/** * A SequenceDiagram shows a UML sequence diagramm of events. * * @version $ProjectVersion: 2-3-0-release.1 $ * @author Mark Richters */public class SequenceDiagram extends JPanel implements Printable { private MSystem fSystem; private Font fFont; private int fNumSteps; // number of time steps private Map fLifelines; // (MObject -> Lifeline) private JPopupMenu fPopupMenu; // context menu on right mouse click private boolean fDoAntiAliasing = true; private boolean fShowValues = true; // args and return values? private boolean fCompactDisplay = false; // omit steps not related to opcalls? private static final int Y_START = 60; // start of lifelines private static final int Y_STEP = 25; // step for time axis private static final int X_STEP = 140; // distance between lifelines private static final int FRAME_OFFSET = 4; // offset for nested activation frames private static final float dash1[] = { 5.0f }; private static final BasicStroke dashedStroke = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 5.0f, dash1, 0.0f); public SequenceDiagram(MSystem system) { fSystem = system; setBorder(BorderFactory.createEmptyBorder()); fFont = Font.getFont("use.gui.view.sequencediagram", getFont()); setBackground(Color.white); setLayout(null); setMinimumSize(new Dimension(50, 50)); setPreferredSize(new Dimension(200, 100)); // create the popup menu for options fPopupMenu = new JPopupMenu(); final JCheckBoxMenuItem cbAntiAliasing = new JCheckBoxMenuItem("Anti-aliasing"); cbAntiAliasing.setState(fDoAntiAliasing); cbAntiAliasing.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent ev) { fDoAntiAliasing = ev.getStateChange() == ItemEvent.SELECTED; repaint(); } }); fPopupMenu.add(cbAntiAliasing); final JCheckBoxMenuItem cbShowValues = new JCheckBoxMenuItem("Show values"); cbShowValues.setState(fShowValues); cbShowValues.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent ev) { fShowValues = ev.getStateChange() == ItemEvent.SELECTED; repaint(); } }); fPopupMenu.add(cbShowValues); final JCheckBoxMenuItem cbCompactDisplay = new JCheckBoxMenuItem("Compact display"); cbCompactDisplay.setState(fCompactDisplay); cbCompactDisplay.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent ev) { fCompactDisplay = ev.getStateChange() == ItemEvent.SELECTED; update(); repaint(); } }); fPopupMenu.add(cbCompactDisplay); addMouseListener(new PopupListener(fPopupMenu)); } /** * Draws the panel. */ public void paintComponent(Graphics g) { super.paintComponent(g); Font oldFont = g.getFont(); g.setFont(fFont); drawDiagram((Graphics2D) g); // restore font g.setFont(oldFont); } /** * Draws the diagram. */ public void drawDiagram(Graphics2D g) { if (fDoAntiAliasing ) g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); FontMetrics fm = g.getFontMetrics(); // Log.trace(this, getBounds().toString()); // Log.trace(this, getInsets().toString()); // respect borders Insets insets = getInsets(); Rectangle r = getBounds(); r.x += insets.left; r.y += insets.top; r.width -= insets.left + insets.right; r.height -= insets.top + insets.bottom; // System.out.println("paintComponent" + count++); int h = r.height - 10; int w = r.width - 10; g.setColor(Color.black); // g.drawString("foobar", r.x, r.y + 10); int x = r.x + 20; int y = r.y + 20; /* draw actor */ // head g.drawOval(x - 5, y - 10, 10, 10); // body g.drawLine(x, y, x, y + 20); // arms g.drawLine(x - 10, y + 10, x + 10, y + 10); // legs g.drawLine(x, y + 20, x - 10, y + 30); g.drawLine(x, y + 20, x + 10, y + 30); Iterator lifelineIter = fLifelines.values().iterator(); while (lifelineIter.hasNext() ) { Lifeline ll = (Lifeline) lifelineIter.next(); ll.draw(g, fm); } // lifeline g.setColor(Color.white); g.fillRect(x - 5, Y_START, 10, fNumSteps * Y_STEP); g.setColor(Color.black); g.drawRect(x - 5, Y_START, 10, fNumSteps * Y_STEP); } /** * Each column in the sequence diagram is represented by a * lifeline. */ private class Lifeline { private int fColumn; private MObject fObj; private List fActivations; private int fActivationNesting; Lifeline(int col, MObject obj) { fColumn = col; fObj = obj; fActivations = new ArrayList(); } int column() { return fColumn; } void enterActivation(Activation a) { fActivations.add(a); a.setNesting(fActivationNesting++); } void exitActivation() { fActivationNesting--; } void draw(Graphics2D g, FontMetrics fm) { int x = 20 + fColumn * X_STEP; int y = Y_START - 30; g.setColor(Color.black); // draw object name String label = fObj.name() + ":" + fObj.cls().name(); int labelWidth = fm.stringWidth(label); int boxWidth = labelWidth + 10; g.drawString(label, x - labelWidth / 2, y + 15); // underline object name g.drawLine(x - labelWidth / 2, y + 17, x + labelWidth / 2, y + 17); // draw object box g.drawRect(x - boxWidth / 2, y, boxWidth, 20); // draw dashed lifeline Stroke oldStroke = g.getStroke(); g.setStroke(dashedStroke); g.drawLine(x, Y_START - 10, x, Y_START + fNumSteps * Y_STEP); g.setStroke(oldStroke); // draw activations Iterator activationIter = fActivations.iterator(); while (activationIter.hasNext() ) { Activation a = (Activation) activationIter.next(); a.drawFrame(g, x); } // draw message sends activationIter = fActivations.iterator(); while (activationIter.hasNext() ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -