Mercurial > hg > ltpda
view src/MPipeline/src/mpipeline/MBlockCore.java @ 46:ca0b8d4dcdb6 database-connection-manager
Fix
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Tue, 06 Dec 2011 19:07:27 +0100 |
parents | f0afece42f48 |
children |
line wrap: on
line source
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package mpipeline; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.io.Serializable; import java.util.Iterator; import javax.swing.BorderFactory; import javax.swing.JColorChooser; import javax.swing.JDesktopPane; import javax.swing.JEditorPane; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.border.BevelBorder; import javax.swing.border.Border; /** * Create the core area of an MBlock. * * @author hewitson */ public class MBlockCore extends JPanel implements Serializable { /** * */ protected JPopupMenu blockCoreContextMenu = new JPopupMenu(); /** * */ protected Dimension size = new Dimension(10, 10); /** * */ protected Color color = new Color(170, 200, 255); //Color.lightGray; /** * */ protected int type; // 0=input, 1=output /** * */ protected int number; /** * */ protected Border myBorder = BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.LIGHT_GRAY, Color.darkGray); /** * */ protected ActionListener bcDeleteListener = new blockcoreDeleteActionListener(); /** * */ protected ActionListener bcAddInputListener = new blockcoreAddInputActionListener(); /** * */ protected ActionListener bcAddOutputListener = new blockcoreAddOutputActionListener(); /** * */ protected ActionListener bcBringForwardListener = new blockcoreBringForwardActionListener(); /** * */ protected ActionListener bcSendBackwardListener = new blockcoreSendBackwardActionListener(); /** * */ protected ActionListener bcCopyListener = new blockcoreCopyActionListener(); /** * */ protected ActionListener bcHelpListener = new blockcoreHelpActionListener(); /** * */ protected ActionListener bcColorListener = new blockcoreColorActionListener(); /** * */ protected JMenuItem addInputMenuItem = new JMenuItem("Add input"); /** * */ protected JMenuItem addOutputMenuItem = new JMenuItem("Add output"); /** * */ protected JMenuItem bringForwardMenuItem = new JMenuItem("Bring forward"); /** * */ protected JMenuItem sendBackwardMenuItem = new JMenuItem("Send backward"); /** * */ protected JMenuItem copyMenuItem = new JMenuItem("Copy"); /** * */ protected JMenuItem helpMenuItem = new JMenuItem("Help"); /** * */ protected JMenuItem deleteMenuItem = new JMenuItem("Delete"); /** * */ protected JMenuItem colorMenuItem = new JMenuItem("Set output pipe color"); /** * */ protected Cursor myCursor = new Cursor(Cursor.HAND_CURSOR); /** * */ protected Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR); /** * */ protected float progress = 0.0f; protected JMenuItem defaultSizeMenuItem = new JMenuItem("Default Size"); private ActionListener bcDefaultSizeListener = new blockcoreDefaultSizeActionListener(); /** * Create a new MBlockCore object * */ public MBlockCore() { setupCore(); } /** * Create a new MBlockCore object with the specified color. * * @param c */ public MBlockCore(Color c) { color = c; setupCore(); } /** * Setup this blockcore. */ private void setupCore() { setBackground(color); setVisible(true); setSize(size); setBorder(myBorder); addMouseListener(new MBlockCoreMouseEvents()); addMouseMotionListener(new MBlockCoreMouseEvents()); // setupBlockContextMenu(); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setColor(MElementWithPorts.getExecutedColor()); g2d.fillRect(0, 0, (int) Math.round(progress * getWidth()), getHeight()); } /** * Context menu for canvas */ public void setupBlockContextMenu() { blockCoreContextMenu.removeAll(); MElementWithPorts el = (MElementWithPorts) getParent(); /** add input **/ if (!(el instanceof MATBlock) && !(el instanceof MConstant)) { addInputMenuItem.addActionListener(bcAddInputListener); blockCoreContextMenu.add(addInputMenuItem); } /** add output **/ if (!(el instanceof MConstant)) { addOutputMenuItem.addActionListener(bcAddOutputListener); blockCoreContextMenu.add(addOutputMenuItem); } // /** bring forward **/ // bringForwardMenuItem.addActionListener(bcBringForwardListener); // blockCoreContextMenu.add(bringForwardMenuItem); // /** send backwards **/ // sendBackwardMenuItem.addActionListener(bcSendBackwardListener); // blockCoreContextMenu.add(sendBackwardMenuItem); /** Copy me **/ copyMenuItem.addActionListener(bcCopyListener); blockCoreContextMenu.add(copyMenuItem); if (getParent() instanceof MBlock) { /** Help */ helpMenuItem.addActionListener(bcHelpListener); blockCoreContextMenu.add(helpMenuItem); } /** Delete me */ deleteMenuItem.addActionListener(bcDeleteListener); blockCoreContextMenu.add(deleteMenuItem); /** Set Output Color */ if (!(el instanceof MConstant)) { colorMenuItem.addActionListener(bcColorListener); blockCoreContextMenu.add(colorMenuItem); } /** Set default size */ defaultSizeMenuItem.addActionListener(bcDefaultSizeListener); blockCoreContextMenu.add(defaultSizeMenuItem); } /** * Set the default background color for this blockcore. * @param c */ public void setDefaultBackgroundColor(Color c) { color = c; setBackground(color); repaint(); } /** * Return the default background color for this blockcore. * @return */ public Color getDefaultBackgroundColor() { return color; } /** * Action listener for setting block to default size. */ private class blockcoreDefaultSizeActionListener implements ActionListener, Serializable { public blockcoreDefaultSizeActionListener() { } public void actionPerformed(java.awt.event.ActionEvent evt) { // copy this block MElementWithPorts b = (MElementWithPorts) getParent(); b.setToDefaultSize(); } } /** * Action listener for bringing a block forwards. */ private class blockcoreBringForwardActionListener implements ActionListener, Serializable { public blockcoreBringForwardActionListener() { } public void actionPerformed(java.awt.event.ActionEvent evt) { // copy this block MElementWithPorts b = (MElementWithPorts) getParent(); b.bringForward(); } } /** * Action listener for sending a block backwards. */ private class blockcoreSendBackwardActionListener implements ActionListener, Serializable { public blockcoreSendBackwardActionListener() { } public void actionPerformed(java.awt.event.ActionEvent evt) { // copy this block MElementWithPorts b = (MElementWithPorts) getParent(); b.sendBackward(); } } /** * Action listener for copy of blocks. */ private class blockcoreCopyActionListener implements ActionListener, Serializable { public blockcoreCopyActionListener() { } public void actionPerformed(java.awt.event.ActionEvent evt) { // copy this block // Get canvas MElementWithPorts b = (MElementWithPorts) getParent(); MCanvas c = (MCanvas) b.getParent(); c.clearSelection(); b.setSelected(true); c.addToSelected(b); c.copyElements(); } } /** * Action listener for setting output pipe color. */ private class blockcoreColorActionListener implements ActionListener, Serializable { public blockcoreColorActionListener() { } public void actionPerformed(java.awt.event.ActionEvent evt) { ColorActionPerformed(evt); } private void ColorActionPerformed(ActionEvent evt) { // get a color from the user: Color newColor = JColorChooser.showDialog( null, "Choose Output Pipe Color", null); MElementWithPorts b = (MElementWithPorts) getParent(); Iterator it = b.getOutputs().iterator(); while (it.hasNext()) { MPort p = (MPort) it.next(); p.setMyPipesColor(newColor); } } } /** * Action listener for deleting a block. */ private class blockcoreDeleteActionListener implements ActionListener, Serializable { public blockcoreDeleteActionListener() { } public void actionPerformed(java.awt.event.ActionEvent evt) { DeleteActionPerformed(evt); } private void DeleteActionPerformed(ActionEvent evt) { // get the block MElementWithPorts b = (MElementWithPorts) getParent(); MCanvas c = (MCanvas) b.getParent(); c.removeElement(b, true); } } /** * Action listener for displaying the help text of this block. */ private class blockcoreHelpActionListener implements ActionListener, Serializable { public blockcoreHelpActionListener() { } public void actionPerformed(java.awt.event.ActionEvent evt) { HelpActionPerformed(evt); } private void HelpActionPerformed(ActionEvent evt) { // get the block MBlock b = (MBlock) getParent(); if (b != null) { System.out.println(b.getMinfo().getHelpTxt()); MCanvas c = (MCanvas) b.getParent(); if (c != null) { BlockDiagram diag = (BlockDiagram) c.getRootPane().getParent(); if (diag != null) { JDesktopPane dp = diag.getDesktopPane(); if (dp != null) { MainWindow mw = (MainWindow) dp.getRootPane().getParent(); if (mw != null) { JEditorPane ep = mw.getHelpDisplayPane(); if (ep != null) { ep.setText(b.getMinfo().getHelpTxt()); } } } } } } } } /** * Action listener for adding inputs to blocks. */ private class blockcoreAddInputActionListener implements ActionListener, Serializable { public blockcoreAddInputActionListener() { } public void actionPerformed(java.awt.event.ActionEvent evt) { AddInputActionPerformed(evt); } private void AddInputActionPerformed(ActionEvent evt) { // get the block MElementWithPorts b = (MElementWithPorts) getParent(); b.addInput(); b.revalidate(); b.repaint(); } } /** * Action listener for adding output to block. */ private class blockcoreAddOutputActionListener implements ActionListener, Serializable { public blockcoreAddOutputActionListener() { } public void actionPerformed(java.awt.event.ActionEvent evt) { AddOutputActionPerformed(evt); } private void AddOutputActionPerformed(ActionEvent evt) { // get the block MElementWithPorts b = (MElementWithPorts) getParent(); b.addOutput(); b.revalidate(); b.repaint(); } } /** * Define the mouse events for an MBlockCore object. * * @author hewitson */ public class MBlockCoreMouseEvents implements Serializable, MouseListener, MouseMotionListener { /** * Create a new MBlockCoreMouseEvents listener. */ public MBlockCoreMouseEvents() { } public void mouseClicked(MouseEvent me) { if (me.getClickCount() == 2) { // if (getParent() instanceof MSubsystem || getParent() instanceof MATBlock || getParent() instanceof MConstant) { // pass to parent element Point pt = me.getPoint(); // the point that was clicked MElementWithPorts b = (MElementWithPorts) getParent(); // get parent block int newX = getX() + pt.x; // transform to canvas coords int newY = getY() + pt.y; // transform to canvas coords // create a new mouse event... MouseEvent transformed = new MouseEvent(b, MouseEvent.MOUSE_CLICKED, me.getWhen(), me.getModifiers(), newX, newY, me.getClickCount(), me.isPopupTrigger(), me.getButton()); // ... and pass it to the canvas b.dispatchEvent(transformed); // } } else { /* Pass to canvas */ Point pt = me.getPoint(); // the point that was clicked MElementWithPorts b = (MElementWithPorts) getParent(); // get parent block System.out.println("var name: " + b.getVarName(0)); for (int kk = 0; kk < b.getNumInputs(); kk++) { MElementWithPorts sb = b.getSourceBlock(kk); int sbn = b.getSourceBlockPortNumber(kk); if (sb != null) { System.out.println("source block for input " + kk + ": " + sb.getName()); System.out.println("source port for input " + kk + ": " + sbn); } } // System.out.println("block size: " + b.getBounds().toString()); // System.out.println(" font size: " + b.getAlgoView().getFont().getSize()); // System.out.println(" port size: " + b.getOutput(0).getBounds().toString()); // System.out.println(" hMargin: " + b.getHMargin()); // System.out.println(" vMargin: " + b.getVMargin()); MCanvas c = (MCanvas) b.getParent(); // get canvas int newX = getX() + b.getX() + pt.x; // transform to canvas coords int newY = getY() + b.getY() + pt.y; // transform to canvas coords // create a new mouse event... MouseEvent transformed = new MouseEvent(c, MouseEvent.MOUSE_CLICKED, me.getWhen(), me.getModifiers(), newX, newY, me.getClickCount(), me.isPopupTrigger(), me.getButton()); // ... and pass it to the canvas c.dispatchEvent(transformed); } } public void mouseEntered(MouseEvent me) { MElementWithPorts b = (MElementWithPorts) getParent(); // get parent block setCursor(myCursor); setBackground(getBackground().darker()); repaint(); } public void mouseExited(MouseEvent me) { setCursor(defaultCursor); setBackground(getDefaultBackgroundColor()); repaint(); } public void mousePressed(MouseEvent me) { if (me.isPopupTrigger()) { // show context menu blockCoreContextMenu.show(me.getComponent(), me.getX(), me.getY()); } else { /* Pass to block */ Point pt = me.getPoint(); // the point that was clicked MElementWithPorts b = (MElementWithPorts) getParent(); // get parent block MCanvas c = (MCanvas) b.getParent(); // get canvas int newX = getX() + b.getX() + pt.x; // transform to canvas coords int newY = getY() + b.getY() + pt.y; // transform to canvas coords // create a new mouse event... MouseEvent transformed = new MouseEvent(c, MouseEvent.MOUSE_PRESSED, me.getWhen(), me.getModifiers(), newX, newY, me.getClickCount(), me.isPopupTrigger(), me.getButton()); // ... and pass it to the canvas c.dispatchEvent(transformed); } } public void mouseReleased(MouseEvent me) { if (me.isPopupTrigger()) { // show context menu blockCoreContextMenu.show(me.getComponent(), me.getX(), me.getY()); } else { /* Pass to canvas */ Point pt = me.getPoint(); // the point that was clicked MElementWithPorts b = (MElementWithPorts) getParent(); // get parent block MCanvas c = (MCanvas) b.getParent(); // get canvas int newX = getX() + b.getX() + pt.x; // transform to canvas coords int newY = getY() + b.getY() + pt.y; // transform to canvas coords // create a new mouse event... MouseEvent transformed = new MouseEvent(c, MouseEvent.MOUSE_RELEASED, me.getWhen(), me.getModifiers(), newX, newY, me.getClickCount(), me.isPopupTrigger(), me.getButton()); // ... and pass it to the canvas c.dispatchEvent(transformed); } } public void mouseDragged(MouseEvent me) { // pass this through to canvas underneath Point pt = me.getPoint(); MElementWithPorts b = (MElementWithPorts) getParent(); // get parent block MCanvas c = (MCanvas) b.getParent(); // get canvas int newX = b.getX() + getX() + pt.x; // transform to block coords int newY = b.getY() + getY() + pt.y; // transform to block coords // create a new mouse event... MouseEvent transformed = new MouseEvent(c, MouseEvent.MOUSE_DRAGGED, me.getWhen(), me.getModifiers(), newX, newY, me.getClickCount(), me.isPopupTrigger(), me.getButton()); // ... and pass it to the canvas c.dispatchEvent(transformed); } public void mouseMoved(MouseEvent me) { // pass this through to canvas underneath Point pt = me.getPoint(); MElementWithPorts b = (MElementWithPorts) getParent(); // get parent block MCanvas c = (MCanvas) b.getParent(); // get canvas int newX = b.getX() + getX() + pt.x; // transform to block coords int newY = b.getY() + getY() + pt.y; // transform to block coords // create a new mouse event... MouseEvent transformed = new MouseEvent(c, MouseEvent.MOUSE_MOVED, me.getWhen(), me.getModifiers(), newX, newY, me.getClickCount(), me.isPopupTrigger(), me.getButton()); // ... and pass it to the canvas c.dispatchEvent(transformed); } } /** * * @return */ public JMenuItem getAddInputMenuItem() { return addInputMenuItem; } /** * * @param addInputMenuItem */ public void setAddInputMenuItem(JMenuItem addInputMenuItem) { this.addInputMenuItem = addInputMenuItem; } /** * * @return */ public JMenuItem getAddOutputMenuItem() { return addOutputMenuItem; } /** * * @param addOutputMenuItem */ public void setAddOutputMenuItem(JMenuItem addOutputMenuItem) { this.addOutputMenuItem = addOutputMenuItem; } /** * * @return */ public ActionListener getBcAddInputListener() { return bcAddInputListener; } /** * * @param bcAddInputListener */ public void setBcAddInputListener(ActionListener bcAddInputListener) { this.bcAddInputListener = bcAddInputListener; } /** * * @return */ public ActionListener getBcAddOutputListener() { return bcAddOutputListener; } /** * * @param bcAddOutputListener */ public void setBcAddOutputListener(ActionListener bcAddOutputListener) { this.bcAddOutputListener = bcAddOutputListener; } /** * * @return */ public ActionListener getBcColorListener() { return bcColorListener; } /** * * @param bcColorListener */ public void setBcColorListener(ActionListener bcColorListener) { this.bcColorListener = bcColorListener; } /** * * @return */ public ActionListener getBcCopyListener() { return bcCopyListener; } /** * * @param bcCopyListener */ public void setBcCopyListener(ActionListener bcCopyListener) { this.bcCopyListener = bcCopyListener; } /** * * @return */ public ActionListener getBcDeleteListener() { return bcDeleteListener; } /** * * @param bcDeleteListener */ public void setBcDeleteListener(ActionListener bcDeleteListener) { this.bcDeleteListener = bcDeleteListener; } /** * * @return */ public ActionListener getBcHelpListener() { return bcHelpListener; } /** * * @param bcHelpListener */ public void setBcHelpListener(ActionListener bcHelpListener) { this.bcHelpListener = bcHelpListener; } /** * * @return */ public JPopupMenu getBlockCoreContextMenu() { return blockCoreContextMenu; } /** * * @param blockCoreContextMenu */ public void setBlockCoreContextMenu(JPopupMenu blockCoreContextMenu) { this.blockCoreContextMenu = blockCoreContextMenu; } /** * * @return */ public Color getColor() { return color; } /** * * @param color */ public void setColor(Color color) { this.color = color; } /** * * @return */ public JMenuItem getColorMenuItem() { return colorMenuItem; } /** * * @param colorMenuItem */ public void setColorMenuItem(JMenuItem colorMenuItem) { this.colorMenuItem = colorMenuItem; } /** * * @return */ public JMenuItem getCopyMenuItem() { return copyMenuItem; } /** * * @param copyMenuItem */ public void setCopyMenuItem(JMenuItem copyMenuItem) { this.copyMenuItem = copyMenuItem; } /** * * @return */ public Cursor getDefaultCursor() { return defaultCursor; } /** * * @param defaultCursor */ public void setDefaultCursor(Cursor defaultCursor) { this.defaultCursor = defaultCursor; } /** * * @return */ public JMenuItem getDeleteMenuItem() { return deleteMenuItem; } /** * * @param deleteMenuItem */ public void setDeleteMenuItem(JMenuItem deleteMenuItem) { this.deleteMenuItem = deleteMenuItem; } /** * * @return */ public JMenuItem getHelpMenuItem() { return helpMenuItem; } /** * * @param helpMenuItem */ public void setHelpMenuItem(JMenuItem helpMenuItem) { this.helpMenuItem = helpMenuItem; } /** * * @return */ public Border getMyBorder() { return myBorder; } /** * * @param myBorder */ public void setMyBorder(Border myBorder) { this.myBorder = myBorder; setBorder(myBorder); repaint(); } /** * * @return */ public Cursor getMyCursor() { return myCursor; } /** * * @param myCursor */ public void setMyCursor(Cursor myCursor) { this.myCursor = myCursor; } /** * * @return */ public int getNumber() { return number; } /** * * @param number */ public void setNumber(int number) { this.number = number; } /** * * @return */ public int getType() { return type; } /** * * @param type */ public void setType(int type) { this.type = type; } /** * * @return */ public ActionListener getBcBringForwardListener() { return bcBringForwardListener; } /** * * @param bcBringForwardListener */ public void setBcBringForwardListener(ActionListener bcBringForwardListener) { this.bcBringForwardListener = bcBringForwardListener; } /** * * @return */ public ActionListener getBcSendBackwardListener() { return bcSendBackwardListener; } /** * * @param bcSendBackwardListener */ public void setBcSendBackwardListener(ActionListener bcSendBackwardListener) { this.bcSendBackwardListener = bcSendBackwardListener; } /** * * @return */ public JMenuItem getBringForwardMenuItem() { return bringForwardMenuItem; } /** * * @param bringForwardMenuItem */ public void setBringForwardMenuItem(JMenuItem bringForwardMenuItem) { this.bringForwardMenuItem = bringForwardMenuItem; } /** * * @return */ public float getProgress() { return progress; } /** * * @param progress */ public void setProgress(float progress) { this.progress = progress; } /** * * @return */ public JMenuItem getSendBackwardMenuItem() { return sendBackwardMenuItem; } /** * * @param sendBackwardMenuItem */ public void setSendBackwardMenuItem(JMenuItem sendBackwardMenuItem) { this.sendBackwardMenuItem = sendBackwardMenuItem; } }