1 19 20 package org.netbeans.modules.debugger.ui.actions; 21 22 import java.awt.Dialog ; 23 import java.awt.event.ActionListener ; 24 import java.awt.event.ActionEvent ; 25 import java.beans.PropertyChangeEvent ; 26 import java.beans.PropertyChangeListener ; 27 import javax.swing.AbstractAction ; 28 import javax.swing.Action ; 29 import javax.swing.JButton ; 30 import org.netbeans.modules.debugger.ui.Utils; 31 import org.netbeans.spi.debugger.ui.Controller; 32 import org.openide.DialogDescriptor; 33 import org.openide.DialogDisplayer; 34 import org.openide.util.NbBundle; 35 36 37 44 public final class ConnectAction extends AbstractAction { 45 46 private Dialog dialog; 47 private JButton bOk; 48 private JButton bCancel; 49 50 51 public ConnectAction () { 52 putValue ( 53 Action.NAME, 54 NbBundle.getMessage ( 55 ConnectAction.class, 56 "CTL_Connect" 57 ) 58 ); 59 putValue ( 60 Action.SMALL_ICON, 61 Utils.getIcon ( 62 "org/netbeans/modules/debugger/resources/actions/Attach" ) 64 ); 65 putValue ( 66 "iconBase", "org/netbeans/modules/debugger/resources/actions/Attach.gif" ); 69 } 70 71 public void actionPerformed (ActionEvent evt) { 72 bOk = new JButton (NbBundle.getMessage (ConnectAction.class, "CTL_Ok")); bCancel = new JButton (NbBundle.getMessage (ConnectAction.class, "CTL_Cancel")); bOk.getAccessibleContext ().setAccessibleDescription (NbBundle.getMessage (ConnectAction.class, "ACSD_CTL_Ok")); bCancel.getAccessibleContext ().setAccessibleDescription (NbBundle.getMessage (ConnectAction.class, "ACSD_CTL_Cancel")); ConnectorPanel cp = new ConnectorPanel (); 77 DialogDescriptor descr = new DialogDescriptor ( 78 cp, 79 NbBundle.getMessage (ConnectAction.class, "CTL_Connect_to_running_process"), 80 true, new ConnectListener (cp) 82 ); 83 descr.setOptions (new JButton [] { 84 bOk, bCancel 85 }); 86 descr.setClosingOptions (new Object [0]); 87 dialog = DialogDisplayer.getDefault ().createDialog (descr); 88 dialog.setVisible(true); 89 } 90 91 92 private class ConnectListener implements ActionListener , PropertyChangeListener { 94 95 ConnectorPanel connectorPanel; 96 Controller controller; 97 98 ConnectListener (ConnectorPanel connectorPanel) { 99 this.connectorPanel = connectorPanel; 100 startListening(); 101 setValid(); 102 connectorPanel.addPropertyChangeListener(this); 103 } 104 105 public void actionPerformed (ActionEvent e) { 106 boolean okPressed = bOk.equals (e.getSource ()); 107 Controller controller = connectorPanel.getController (); 108 boolean close = false; 109 if (okPressed) { 110 close = controller.ok (); 111 } else { 112 close = controller.cancel (); 113 } 114 if (!close) return; 115 connectorPanel.removePropertyChangeListener (this); 116 stopListening (); 117 dialog.setVisible (false); 118 dialog.dispose (); 119 dialog = null; 120 } 121 122 void startListening () { 123 controller = connectorPanel.getController (); 124 if (controller == null) return; 125 controller.addPropertyChangeListener (this); 126 } 127 128 void stopListening () { 129 if (controller == null) return; 130 controller.removePropertyChangeListener (this); 131 controller = null; 132 } 133 134 void setValid () { 135 Controller controller = connectorPanel.getController (); 136 if (controller == null) { 137 bOk.setEnabled (false); 138 return; 139 } 140 bOk.setEnabled (controller.isValid ()); 141 } 142 143 public void propertyChange(PropertyChangeEvent evt) { 144 if (evt.getPropertyName () == ConnectorPanel.PROP_TYPE) { 145 stopListening (); 146 setValid (); 147 startListening (); 148 } else if (evt.getPropertyName () == Controller.PROP_VALID) { 149 setValid (); 150 } 151 } 152 153 } 154 } 155 156 157 | Popular Tags |