JPopupMenuの使用例 -- Graph構造とステートチャートの作成など

戻る






:::::::::::::: AbstractStep.java :::::::::::::: public abstract class AbstractStep { public abstract void addNextStep( AbstractStep nextStep ); } :::::::::::::: DrawingPanel.java :::::::::::::: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.util.*; /** $Id: StepView.html,v 1.1 2009/06/22 16:11:58 kishi Exp kishi $ @author KISHI Yasuhiro */ public class DrawingPanel extends JPanel implements ActionListener, MouseListener { /** ポップアップメニューのインスタンス */ private JPopupMenu popup = null; private int xPos = 0; private int yPos = 0; /** 操作モード(リンク、整列)を表す値 */ private Operation.Mode mode = Operation.Mode.DEFAULT; private StepView linkSource = null; private StepView linkTarget = null; /** 各ステップのリストを格納するマネージャクラス */ private StepManager stepManager; /** コンストラクタ */ public DrawingPanel() { popup = new JPopupMenu(); popup.setPopupSize( 120, 50 ); JMenuItem menuItem1 = new JMenuItem( "ステップ作成" ); menuItem1.setActionCommand( "CREATE_STEP" ); menuItem1.addActionListener( this ); popup.add( menuItem1 ); JMenuItem menuItem2 = new JMenuItem( "シリアライズ" ); menuItem2.setActionCommand( "SERIALIZE" ); menuItem2.addActionListener( this ); popup.add( menuItem2 ); // レイアウトマネージャを使用しない setLayout( null ); addMouseListener( this ); stepManager = new StepManager(); } public void actionPerformed( ActionEvent e ) { if ( e.getSource() instanceof JMenuItem ) { // // 右クリックからのメニューを選択した場合 // JMenuItem source = ( JMenuItem ) ( e.getSource() ); System.out.println( "Action Command is " + source.getText() ); String command = e.getActionCommand(); // ポップアップメニューを開いた座標値を求める System.out.printf( "x=%3d, y=%3d: commnad=%s\n", xPos, yPos, command ); if ( "CREATE_STEP".equals( command ) ) { StepView stepView = new StepView(); // setBounds()はJButtonから継承したメソッド stepView.setBounds( xPos, yPos, 100, 20 ); stepView.addActionListener( this ); this.add( stepView ); // マウスドラッグのモーションリスナを登録 MouseMotionListener motionListener = new MouseDraggedEventHandler( this, stepView ); stepView.addMouseMotionListener( motionListener ); // ステップマネージャーに登録 stepManager.addStepView( stepView ); } } if ( e.getSource() instanceof StepView ) { // // ボタンをクリックした場合 // StepView source = ( StepView ) ( e.getSource() ); System.out.println( source ); if ( mode == Operation.Mode.LINK ) { linkTarget = source; // 色を元に戻す linkSource.restoreBackground(); // sourceとtargetをリンクする stepManager.connect( linkSource, linkTarget ); stepManager.dumpList(); // 再描画 repaint(); System.out.println( "リンク先が設定されました!" ); // 設定を元にもどす restoreMode(); } if ( mode == Operation.Mode.DELINK ) { linkTarget = source; // 色を元に戻す linkSource.restoreBackground(); // sourceとtargetをリンクする stepManager.disconnect( linkSource, linkTarget ); stepManager.dumpList(); // 再描画 repaint(); System.out.println( "リンク先が解除されました!" ); // 設定を元にもどす restoreMode(); } } } /** 設定を元に戻す */ private void restoreMode() { mode = Operation.Mode.DEFAULT; linkSource = null; linkTarget = null; } public void mousePressed( MouseEvent e ) { if ( e.isPopupTrigger() ) { /* JPopupMenuを表示 */ popup.show( e.getComponent(), e.getX(), e.getY() ); xPos = ( int ) e.getX(); yPos = ( int ) e.getY(); popup.pack(); } } public void mouseReleased( MouseEvent e ) { if ( e.isPopupTrigger() ) { /* JPopupMenuを表示 */ popup.show( e.getComponent(), e.getX(), e.getY() ); xPos = ( int ) e.getX(); yPos = ( int ) e.getY(); popup.pack(); } } /** マウスがクリックされた時の処理 */ public void mouseClicked( MouseEvent e ) { int modifier = e.getModifiers(); // SHIFT,ALT,CTRLキーの押下の判別 String s = getShiftAltCtrl( modifier ); System.out.println( s ); if ( ( modifier & InputEvent.BUTTON1_MASK ) != 0 ) { /** 左クリック **/ System.out.println( "LEFT CLICK!" ); } else if ( ( modifier & InputEvent.BUTTON2_MASK ) != 0 ) { /** DO NOTHING */ } else if ( ( modifier & InputEvent.BUTTON3_MASK ) != 0 ) { /** 右クリック **/ // ここは通らなくなってしまいます System.out.println( "ポップアップメニューが表示されます!" ); } } private String getShiftAltCtrl( int modifier ) { StringBuffer sb = new StringBuffer(); if ( ( modifier & InputEvent.SHIFT_MASK ) != 0 ) { sb.append( "SHIFT " ); } if ( ( modifier & InputEvent.ALT_MASK ) != 0 ) { sb.append( "ALT " ); } if ( ( modifier & InputEvent.CTRL_MASK ) != 0 ) { sb.append( "CTRL " ); } return ( new String( sb ) ); } public void mouseEntered( MouseEvent e ) {} public void mouseExited( MouseEvent e ) {} /** in case when repaint() method is invoked, this method is also executed. */ public void paintComponent( Graphics g ) { System.out.println( "paintComponet:" ); int width = getWidth(); int height = getHeight(); // 画面クリア g.setColor( Color.black ); // このコンポーネントのサイズを取得する g.fillRect( 0 , 0 , width , height ); //=====================================- // 各オブジェクトを線で繋ぐ //=====================================- g.setColor( Color.blue ); java.util.List connections = stepManager.getList(); for ( int i = 0;i < connections.size();i++ ) { JComponent from = ( JComponent ) connections.get( i ); java.util.List links = ( ( StepView ) from ).getLinks(); Iterator iterator = links.iterator(); while ( iterator.hasNext() ) { StepView link = ( StepView ) iterator.next(); if ( !connections.contains( link ) ) continue; JComponent to = ( JComponent ) link; Rectangle fromRect = from.getBounds(); Rectangle toRect = to.getBounds(); System.out.println( fromRect + "\t" + toRect ); int fromX = ( int ) ( fromRect.x + fromRect.width / 2.0 ); int fromY = ( int ) ( fromRect.y + fromRect.height / 2.0 ); int toX = ( int ) ( toRect.x + toRect.width / 2.0 ); int toY = ( int ) ( toRect.y + toRect.height / 2.0 ); g.drawLine( fromX, fromY, toX, toY ); } } } /** リンク元の選択 */ public void setLinkSource( StepView stepView, Operation.Mode mode ) { System.out.println( "リンク元が選択されました" ); linkSource = stepView; // mode should be either Operation.Mode.LINK or Operation.Mode.DELINK this.mode = mode; } public Operation.Mode getOperationMode() { return mode; } public void disposeStepView( StepView stepView ) { stepManager.removeStepView( stepView ); // 再描画 repaint(); } } :::::::::::::: EntryPoint.java :::::::::::::: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; /** $Id: StepView.html,v 1.1 2009/06/22 16:11:58 kishi Exp kishi $ */ public class EntryPoint extends JComponent implements ActionListener { public void paintComponent( Graphics g ) { int width = getWidth(); int height = getHeight(); // 画面クリア g.setColor( Color.red ); // このコンポーネントのサイズを取得する g.fillRect( 0 , 0 , width , height ); } public void addActionListener( ActionListener l ) { listenerList.add( ActionListener.class, l ); } public void actionPerformed( ActionEvent e ) { System.out.println( this ); } } :::::::::::::: ExitPoint.java :::::::::::::: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; /** $Id: StepView.html,v 1.1 2009/06/22 16:11:58 kishi Exp kishi $ */ public class ExitPoint extends JComponent implements ActionListener { public void paintComponent( Graphics g ) { int width = getWidth(); int height = getHeight(); // 画面クリア g.setColor( Color.white ); // このコンポーネントのサイズを取得する g.fillRect( 0 , 0 , width , height ); } public void actionPerformed( ActionEvent e ) { System.out.println( this ); } } :::::::::::::: MouseDraggedEventHandler.java :::::::::::::: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.io.*; /** $Id: StepView.html,v 1.1 2009/06/22 16:11:58 kishi Exp kishi $ @author KISHI Yasuhiro */ public class MouseDraggedEventHandler implements MouseMotionListener { private JComponent component; private Container container; public MouseDraggedEventHandler( Container container, JComponent component ) { this.container = container; this.component = component; } public void mouseDragged( MouseEvent argEvent ) { System.out.printf( "(%3d,%3d)\n", argEvent.getX() , argEvent.getY() ); if ( argEvent.getX() < 0 || argEvent.getY() < 0 ) { Rectangle objBounds = component.getBounds(); component.setLocation( objBounds.x + argEvent.getX(), objBounds.y + argEvent.getY() ); } else { Rectangle objBounds = component.getBounds(); if ( objBounds.x <= objBounds.x + argEvent.getX() && objBounds.x + argEvent.getX() <= objBounds.x + objBounds.width && objBounds.y <= objBounds.y + argEvent.getY() && objBounds.y + argEvent.getY() <= objBounds.y + objBounds.height ) { component.setLocation( objBounds.x + argEvent.getX(), objBounds.y + argEvent.getY() ); } else { component.setLocation( argEvent.getX(), argEvent.getY() ); } } container.repaint(); } public void mouseMoved( MouseEvent argEvent ) { } } :::::::::::::: MyAbstractAction.java :::::::::::::: import javax.swing.*; import java.awt.*; import java.awt.event.*; /** $Id: StepView.html,v 1.1 2009/06/22 16:11:58 kishi Exp kishi $ */ public class MyAbstractAction extends AbstractAction { public MyAbstractAction() { super(); } public void actionPerformed( ActionEvent event ) { String eventname = event.getActionCommand(); // 実行されたコマンドをダンプします System.out.println( "..." ); } } :::::::::::::: Operation.java :::::::::::::: /** $Id: StepView.html,v 1.1 2009/06/22 16:11:58 kishi Exp kishi $ */ public class Operation { static public enum Mode {DEFAULT /** デフォルト */ , LINK /** リンク設定モード */ , DELINK /** リンク解除モード */ , ALIGNMENT /** 整列 */} } :::::::::::::: StepDrawingTest.java :::::::::::::: import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; /** $Id: StepView.html,v 1.1 2009/06/22 16:11:58 kishi Exp kishi $ */ public class StepDrawingTest extends JFrame { private int width = 640; private int height = 480; public StepDrawingTest() { super( "フローチャートのステップを追加したり変更・削除してみたりする。" ); Container contentPane = getContentPane(); contentPane.setLayout( new FlowLayout() ); DrawingPanel dp = new DrawingPanel(); dp.setPreferredSize( new Dimension( width, height ) ); contentPane.add( dp ); } public static void main( String[] args ) { StepDrawingTest frame = new StepDrawingTest(); /* 終了処理を追加 */ frame.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); /** サイズと位置を指定 */ frame.setBounds( 0, 0, frame.width, frame.height ); frame.setVisible( true ); frame.pack(); } } :::::::::::::: StepManager.java :::::::::::::: import java.util.*; /** $Id: StepView.html,v 1.1 2009/06/22 16:11:58 kishi Exp kishi $ */ public class StepManager { private List list; public StepManager() { list = new LinkedList(); } public void addStepView( StepView stepView ) { list.add( stepView ); } public void removeStepView( StepView stepView ) { list.remove( stepView ); } /** リンクする */ public void connect( StepView one, StepView another ) { if ( !list.contains( one ) ) { System.err.println( one.getText() + " はリンク対象外です" ); return ; } if ( !list.contains( another ) ) { System.err.println( another.getText() + " はリンク対象外です" ); return ; } try { one.link( another ); another.link( one ); } catch ( Exception e ) { e.printStackTrace(); } } /** リンク解除する */ public void disconnect( StepView one, StepView another ) { try { one.delink( another ); another.delink( one ); } catch ( Exception e ) { e.printStackTrace(); } } public List getList() { return list; } public void dumpList() { Iterator iterator = list.iterator(); while ( iterator.hasNext() ) { StepView view = ( StepView ) iterator.next(); System.out.print( view.getText() + ": " ); Iterator itrt = view.getLinks().iterator(); while ( itrt.hasNext() ) { StepView link = ( StepView ) itrt.next(); if ( list.contains( link ) ) { // マネージャが管轄しているものだけを取り出す System.out.print( link.getText() + " " ); } } System.out.println(); } System.out.println(); } public static void main( String[] args ) { StepManager manager = new StepManager(); StepView view1 = new StepView(); StepView view2 = new StepView(); StepView view3 = new StepView(); StepView view4 = new StepView(); manager.addStepView( view1 ); manager.addStepView( view2 ); manager.addStepView( view3 ); manager.addStepView( view4 ); manager.dumpList(); manager.removeStepView( view2 ); manager.dumpList(); manager.connect( view1, view2 ); manager.connect( view1, view1 ); manager.connect( view1, view3 ); manager.connect( view1, view3 ); manager.dumpList(); manager.disconnect( view1, view3 ); manager.disconnect( view1, view3 ); manager.dumpList(); } } :::::::::::::: StepModel.java :::::::::::::: import java.io.*; import java.util.*; /** * $Id: StepView.html,v 1.1 2009/06/22 16:11:58 kishi Exp kishi $ */ public class StepModel { private int x; private int y; private String name; private List nextSteps; public void setName( String name ) { this.name = name; } public void addNextStep( StepModel nextStep ) { if ( nextSteps == null ) { nextSteps = new LinkedList(); } nextSteps.add( nextStep ); } } :::::::::::::: StepView.java :::::::::::::: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.util.*; /** $Id: StepView.html,v 1.1 2009/06/22 16:11:58 kishi Exp kishi $ @author KISHI Yasuhiro */ public class StepView extends JButton implements ActionListener { static int serialNo = 0; /** ポップアップメニューのインスタンス */ private JPopupMenu popup = null; /** もともとの背景色 */ private Color originalBackgroundColor; /** リンク先 */ private java.util.List links; /** コンストラクタ */ public StepView() { super(); serialNo ++; super.setText( "step" + serialNo ); originalBackgroundColor = this.getBackground(); popup = new JPopupMenu(); // popup.setPopupSize( 120, 50 ); popup.setBackground( Color.yellow ); addMenuItem( "プロパティ編集", "EDIT" ); addMenuItem( "削除", "KILL_ITSELF" ); addMenuItem( "リンク設定", "LINK" ); addMenuItem( "リンク解除", "DELINK" ); addMouseListener( new MouseAdapter() { public void mousePressed( MouseEvent e ) { if ( e.isPopupTrigger() ) { /* JPopupMenuを表示 */ popup.show( e.getComponent(), e.getX(), e.getY() ); } } public void mouseReleased( MouseEvent e ) { if ( e.isPopupTrigger() ) { /* JPopupMenuを表示 */ popup.show( e.getComponent(), e.getX(), e.getY() ); } } } ); links = new LinkedList(); } /** リンクを設定する */ public void link( StepView stepView ) throws Exception { if ( stepView.equals( this ) ) { throw new StepViewLinkException( "自分自身にはリンク設定できません" ); } if ( links.contains( stepView ) ) { throw new StepViewLinkException( "既にリンクされています" ); } links.add( stepView ); } /** リンクを解除する */ public void delink( StepView stepView ) throws Exception { if ( stepView.equals( this ) ) { throw new StepViewLinkException( "自分自身はリンク解除できません" ); } if ( !links.contains( stepView ) ) { throw new StepViewLinkException( "リンクされていません" ); } links.remove( stepView ); } /** リンク先一覧を返す */ public java.util.List getLinks() { return links; } /** ポップアップメニューにメニューアイテムを追加する */ private void addMenuItem( String text, String commandName ) { JMenuItem menuItem = new JMenuItem( text ); menuItem.setBackground( Color.green ); menuItem.setActionCommand( commandName ); menuItem.addActionListener( this ); popup.add( menuItem ); } /** ポップアップメニュー内で選択した結果により実行する内容を決定 */ public void actionPerformed( ActionEvent e ) { Container parent = this.getParent(); DrawingPanel drawingPanel = ( DrawingPanel ) parent; String command = e.getActionCommand(); System.out.println( "COMMAND=" + command + " : " + this ); if ( "KILL_ITSELF".equals( command ) ) { // 親オブジェクトからこのインスタンスを削除する parent.remove( this ); // このメソッドを呼ばないと即時に反映されないので必ず再描画する! parent.repaint(); // ステップマネージャの管理対象外とする drawingPanel.disposeStepView( this ); } if ( "LINK".equals( command ) ) { if ( drawingPanel.getOperationMode() == Operation.Mode.DEFAULT ) { drawingPanel.setLinkSource( this, Operation.Mode.LINK ); this.setBackground( Color.yellow ); } } if ( "DELINK".equals( command ) ) { if ( drawingPanel.getOperationMode() == Operation.Mode.DEFAULT ) { drawingPanel.setLinkSource( this, Operation.Mode.DELINK ); this.setBackground( Color.red ); } } } /** 背景色を元に戻す */ public void restoreBackground() { this.setBackground( originalBackgroundColor ); } } :::::::::::::: StepViewLinkException.java :::::::::::::: /** $Id: StepView.html,v 1.1 2009/06/22 16:11:58 kishi Exp kishi $ */ public class StepViewLinkException extends Exception { public StepViewLinkException() { super(); } public StepViewLinkException( String message ) { super( message ); } }
戻る inserted by FC2 system