Partial fix for P3 #108002 (Breakpoint|Enabled context menu item disabled in gutter) version-2-3-109
authorzgursky@netbeans.org
Wed, 01 Aug 2007 19:29:32 +0000
changeset 9255308ec1e1791
parent 924 e6859b13563b
child 926 b44958f8646e
Partial fix for P3 #108002 (Breakpoint|Enabled context menu item disabled in gutter)
bpel.debugger.ui/src/org/netbeans/modules/bpel/debugger/ui/action/Breakpoint.java
bpel.debugger.ui/src/org/netbeans/modules/bpel/debugger/ui/action/BreakpointEnableAction.java
bpel.debugger.ui/src/org/netbeans/modules/bpel/debugger/ui/action/Bundle.properties
bpel.debugger.ui/src/org/netbeans/modules/bpel/debugger/ui/resources/layer.xml
     1.1 --- a/bpel.debugger.ui/src/org/netbeans/modules/bpel/debugger/ui/action/Breakpoint.java	Wed Aug 01 19:24:48 2007 +0000
     1.2 +++ b/bpel.debugger.ui/src/org/netbeans/modules/bpel/debugger/ui/action/Breakpoint.java	Wed Aug 01 19:29:32 2007 +0000
     1.3 @@ -57,7 +57,7 @@
     1.4  
     1.5  /**
     1.6   * @author Vladimir Yaroslavskiy
     1.7 - * @version 2005.10.19
     1.8 + * @author Alexander Zgursky
     1.9   */
    1.10  public class Breakpoint extends ActionsProviderSupport {
    1.11      
    1.12 @@ -87,7 +87,7 @@
    1.13              return;
    1.14          }
    1.15          
    1.16 -        DataObject dataObject = getDataObject(node);
    1.17 +        DataObject dataObject = node.getLookup().lookup(DataObject.class);
    1.18          if (dataObject == null) {
    1.19              return;
    1.20          }
    1.21 @@ -105,14 +105,10 @@
    1.22          if ((node instanceof InstanceRef) && !isInSourceEditor()) {
    1.23              Object modelReference = ((InstanceRef)node).getReference();
    1.24              
    1.25 -            if (modelReference == null) {
    1.26 +            if (modelReference == null || !(modelReference instanceof Activity)) {
    1.27                  return;
    1.28              }
    1.29  
    1.30 -            if (!(modelReference instanceof Activity)) {
    1.31 -                return;
    1.32 -            }
    1.33 -            
    1.34              Activity activity = (Activity)modelReference;
    1.35              bpelEntityId = activity.getUID();
    1.36          } else {
    1.37 @@ -142,7 +138,7 @@
    1.38          }
    1.39          
    1.40          DebuggerManager debuggerManager = DebuggerManager.getDebuggerManager();
    1.41 -        LineBreakpoint breakpoint =
    1.42 +        LineBreakpoint breakpoint = getBreakpointAnnotationListener().
    1.43                  findBreakpoint(url, xpath);
    1.44          if (breakpoint != null) {
    1.45              // Breakpoint already exists
    1.46 @@ -151,15 +147,10 @@
    1.47          }
    1.48          
    1.49          // No breakpoint exists - add the breakpoint.
    1.50 -        LineBreakpoint newBreakpoint =
    1.51 -                LineBreakpoint.create(url, xpath);
    1.52 +        LineBreakpoint newBreakpoint = LineBreakpoint.create(url, xpath);
    1.53          debuggerManager.addBreakpoint(newBreakpoint);
    1.54      }
    1.55      
    1.56 -    private LineBreakpoint findBreakpoint(String url, String xpath) {
    1.57 -        return getBreakpointAnnotationListener().findBreakpoint(url, xpath);
    1.58 -    }
    1.59 -    
    1.60      /**{@inheritDoc}*/
    1.61      public Set getActions() {
    1.62          return Collections.singleton(ActionsManager.ACTION_TOGGLE_BREAKPOINT);
    1.63 @@ -168,16 +159,7 @@
    1.64      private Node getCurrentNode() {
    1.65          Node [] nodes = WindowManager.getDefault().getRegistry().getCurrentNodes();
    1.66          
    1.67 -        if (nodes == null) {
    1.68 -            Log.out("Current nodes are null"); // NOI18N
    1.69 -            return null;
    1.70 -        }
    1.71 -        if (nodes.length == 0) {
    1.72 -            Log.out("There is no current node"); // NOI18N
    1.73 -            return null;
    1.74 -        }
    1.75 -        if (nodes.length != 1) {
    1.76 -            Log.out("There are too many current nodes"); // NOI18N
    1.77 +        if (nodes == null || nodes.length != 1 ) {
    1.78              return null;
    1.79          }
    1.80          return nodes [0];
    1.81 @@ -202,55 +184,38 @@
    1.82          if (dataObject == null) {
    1.83              return null;
    1.84          }
    1.85 +        
    1.86          FileObject fileObject = dataObject.getPrimaryFile();
    1.87 -        
    1.88          if (fileObject == null) {
    1.89 -            Log.out("fileObject is null"); // NOI18N
    1.90              return null;
    1.91          }
    1.92 +        
    1.93          return fileObject.getExt();
    1.94      }
    1.95      
    1.96 -    private DataObject getDataObject(Node node) {
    1.97 -        if (node == null) {
    1.98 -            return null;
    1.99 -        }
   1.100 -        return (DataObject)node.getLookup().lookup(DataObject.class);
   1.101 -    }
   1.102 -    
   1.103      private int getCurrentLineNumber(Node node) {
   1.104 -        EditorCookie editorCookie =
   1.105 -            (EditorCookie) node.getLookup().lookup(EditorCookie.class);
   1.106 -        
   1.107 +        EditorCookie editorCookie = node.getLookup().lookup(EditorCookie.class);
   1.108          if (editorCookie == null) {
   1.109 -            Log.out("Editor cookie is null"); // NOI18N
   1.110 -            return -1;
   1.111 -        }
   1.112 -        StyledDocument document = editorCookie.getDocument();
   1.113 -        
   1.114 -        if (document == null) {
   1.115 -            Log.out("Document is null"); // NOI18N
   1.116              return -1;
   1.117          }
   1.118          
   1.119          JEditorPane[] editorPanes = editorCookie.getOpenedPanes();
   1.120 -
   1.121 -        if (editorPanes == null) {
   1.122 -            Log.out("Editor panes are null"); // NOI18N
   1.123 +        if (editorPanes == null || editorPanes.length == 0) {
   1.124              return -1;
   1.125          }
   1.126 -        if (editorPanes.length == 0) {
   1.127 -            Log.out("There is no editor pane"); // NOI18N
   1.128 +        
   1.129 +        Caret caret = editorPanes[0].getCaret();
   1.130 +        if (caret == null) {
   1.131              return -1;
   1.132          }
   1.133 -        Caret caret = editorPanes [0].getCaret();
   1.134 -
   1.135 -        if (caret == null) {
   1.136 -            Log.out("Caret is null"); // NOI18N
   1.137 +        
   1.138 +        int offset = caret.getDot();
   1.139 +        
   1.140 +        StyledDocument document = editorCookie.getDocument();
   1.141 +        if (document == null) {
   1.142              return -1;
   1.143          }
   1.144 -        int offset = caret.getDot();
   1.145 -
   1.146 +        
   1.147          return NbDocument.findLineNumber(document, offset) + 1;
   1.148      }
   1.149      
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/bpel.debugger.ui/src/org/netbeans/modules/bpel/debugger/ui/action/BreakpointEnableAction.java	Wed Aug 01 19:29:32 2007 +0000
     2.3 @@ -0,0 +1,189 @@
     2.4 +/*
     2.5 + * The contents of this file are subject to the terms of the Common Development
     2.6 + * and Distribution License (the License). You may not use this file except in
     2.7 + * compliance with the License.
     2.8 + *
     2.9 + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
    2.10 + * or http://www.netbeans.org/cddl.txt.
    2.11 + *
    2.12 + * When distributing Covered Code, include this CDDL Header Notice in each file
    2.13 + * and include the License file at http://www.netbeans.org/cddl.txt.
    2.14 + * If applicable, add the following below the CDDL Header, with the fields
    2.15 + * enclosed by brackets [] replaced by your own identifying information:
    2.16 + * "Portions Copyrighted [year] [name of copyright owner]"
    2.17 + *
    2.18 + * The Original Software is NetBeans. The Initial Developer of the Original
    2.19 + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
    2.20 + * Microsystems, Inc. All Rights Reserved.
    2.21 + */
    2.22 +
    2.23 +package org.netbeans.modules.bpel.debugger.ui.action;
    2.24 +
    2.25 +import javax.swing.JEditorPane;
    2.26 +import javax.swing.text.Caret;
    2.27 +import javax.swing.text.StyledDocument;
    2.28 +import org.netbeans.api.debugger.DebuggerManager;
    2.29 +import org.netbeans.modules.bpel.debugger.api.breakpoints.LineBreakpoint;
    2.30 +import org.netbeans.modules.bpel.debugger.ui.breakpoint.BpelBreakpointListener;
    2.31 +import org.netbeans.modules.bpel.debugger.ui.util.EditorUtil;
    2.32 +import org.netbeans.modules.bpel.debugger.ui.util.ModelUtil;
    2.33 +import org.netbeans.modules.bpel.model.api.BpelModel;
    2.34 +import org.netbeans.modules.bpel.model.api.support.UniqueId;
    2.35 +import org.openide.cookies.EditorCookie;
    2.36 +import org.openide.filesystems.FileObject;
    2.37 +import org.openide.filesystems.FileUtil;
    2.38 +import org.openide.loaders.DataObject;
    2.39 +import org.openide.nodes.Node;
    2.40 +import org.openide.text.NbDocument;
    2.41 +import org.openide.util.HelpCtx;
    2.42 +import org.openide.util.NbBundle;
    2.43 +import org.openide.util.actions.BooleanStateAction;
    2.44 +import org.openide.windows.WindowManager;
    2.45 +
    2.46 +/**
    2.47 + * Enables or disables breakpoints.
    2.48 + *
    2.49 + * @author Alexander Zgursky
    2.50 + */
    2.51 +public class BreakpointEnableAction extends BooleanStateAction {
    2.52 +    private BpelBreakpointListener myBreakpointAnnotationListener;
    2.53 +
    2.54 +//TODO: the whole class needs refactoring - most of the code was copy-pasted from
    2.55 +//org.netbeans.modules.bpel.debugger.ui.action.Breakpoint
    2.56 +
    2.57 +    public boolean isEnabled() {
    2.58 +        LineBreakpoint b = findCurrentBreakpoint();
    2.59 +        if (b != null) {
    2.60 +            boolean value = b.isEnabled();
    2.61 +            super.setBooleanState(value);
    2.62 +            return true;
    2.63 +        }
    2.64 +        return false;
    2.65 +    }
    2.66 +    
    2.67 +    public String getName() {
    2.68 +        return NbBundle.getMessage(BreakpointEnableAction.class, "CTL_enabled");
    2.69 +    }
    2.70 +    
    2.71 +    public void setBooleanState(boolean value) {
    2.72 +        LineBreakpoint b = findCurrentBreakpoint();
    2.73 +        if (value) {
    2.74 +            b.enable();
    2.75 +        } else {
    2.76 +            b.disable();
    2.77 +        }
    2.78 +        super.setBooleanState(value);
    2.79 +    }
    2.80 +    
    2.81 +    public HelpCtx getHelpCtx() {
    2.82 +        return null;
    2.83 +    }
    2.84 +    
    2.85 +    private LineBreakpoint findCurrentBreakpoint() {
    2.86 +        Node node = getCurrentNode();
    2.87 +        if (node == null) {
    2.88 +            return null;
    2.89 +        }
    2.90 +        
    2.91 +        DataObject dataObject = node.getLookup().lookup(DataObject.class);
    2.92 +        if (dataObject == null) {
    2.93 +            return null;
    2.94 +        }
    2.95 +        
    2.96 +        String ext = getFileExt(dataObject);
    2.97 +        if (!"bpel".equals(ext)) {                  // NOI18N
    2.98 +            return null;
    2.99 +        }
   2.100 +        
   2.101 +        int lineNumber = getCurrentLineNumber(node);
   2.102 +        if (lineNumber < 1) {
   2.103 +            return null;
   2.104 +        }
   2.105 +        
   2.106 +        StyledDocument doc = EditorUtil.getDocument(dataObject);
   2.107 +        if (doc == null) {
   2.108 +            return null;
   2.109 +        }
   2.110 +        
   2.111 +        int offset = EditorUtil.findOffset(doc, lineNumber);
   2.112 +        BpelModel model = EditorUtil.getBpelModel(dataObject);
   2.113 +        if (model == null) {
   2.114 +            return null;
   2.115 +        }
   2.116 +        
   2.117 +        UniqueId bpelEntityId = ModelUtil.getBpelEntityId(model, offset);
   2.118 +        
   2.119 +        String url = FileUtil.toFile(dataObject.getPrimaryFile()).getPath();
   2.120 +        //TODO:consider using FileUtil.normalizeFile()
   2.121 +        url = url.replace("\\", "/"); // NOI18N
   2.122 +        if (bpelEntityId == null) {
   2.123 +            return null;
   2.124 +        }
   2.125 +        
   2.126 +        String xpath = ModelUtil.getXpath(bpelEntityId);
   2.127 +        if (xpath == null) {
   2.128 +            return null;
   2.129 +        }
   2.130 +        
   2.131 +        return getBreakpointAnnotationListener().findBreakpoint(url, xpath);
   2.132 +    }
   2.133 +
   2.134 +    private Node getCurrentNode() {
   2.135 +        Node [] nodes = WindowManager.getDefault().getRegistry().getCurrentNodes();
   2.136 +        
   2.137 +        if (nodes == null || nodes.length != 1 ) {
   2.138 +            return null;
   2.139 +        }
   2.140 +        return nodes [0];
   2.141 +    }
   2.142 +    
   2.143 +    private String getFileExt(DataObject dataObject) {
   2.144 +        if (dataObject == null) {
   2.145 +            return null;
   2.146 +        }
   2.147 +        
   2.148 +        FileObject fileObject = dataObject.getPrimaryFile();
   2.149 +        if (fileObject == null) {
   2.150 +            return null;
   2.151 +        }
   2.152 +        
   2.153 +        return fileObject.getExt();
   2.154 +    }
   2.155 +    
   2.156 +    private int getCurrentLineNumber(Node node) {
   2.157 +        EditorCookie editorCookie = node.getLookup().lookup(EditorCookie.class);
   2.158 +        if (editorCookie == null) {
   2.159 +            return -1;
   2.160 +        }
   2.161 +        
   2.162 +        
   2.163 +        JEditorPane[] editorPanes = editorCookie.getOpenedPanes();
   2.164 +        if (editorPanes == null || editorPanes.length == 0) {
   2.165 +            return -1;
   2.166 +        }
   2.167 +        
   2.168 +        Caret caret = editorPanes[0].getCaret();
   2.169 +        if (caret == null) {
   2.170 +            return -1;
   2.171 +        }
   2.172 +        
   2.173 +        int offset = caret.getDot();
   2.174 +        
   2.175 +        StyledDocument document = editorCookie.getDocument();
   2.176 +        if (document == null) {
   2.177 +            return -1;
   2.178 +        }
   2.179 +        
   2.180 +        return NbDocument.findLineNumber(document, offset) + 1;
   2.181 +    }
   2.182 +    
   2.183 +    private BpelBreakpointListener getBreakpointAnnotationListener () {
   2.184 +        if (myBreakpointAnnotationListener == null) {
   2.185 +            myBreakpointAnnotationListener = (BpelBreakpointListener) 
   2.186 +                    DebuggerManager.getDebuggerManager ().lookupFirst 
   2.187 +                    (null, BpelBreakpointListener.class);
   2.188 +        }
   2.189 +        return myBreakpointAnnotationListener;
   2.190 +    }
   2.191 +
   2.192 +}
     3.1 --- a/bpel.debugger.ui/src/org/netbeans/modules/bpel/debugger/ui/action/Bundle.properties	Wed Aug 01 19:24:48 2007 +0000
     3.2 +++ b/bpel.debugger.ui/src/org/netbeans/modules/bpel/debugger/ui/action/Bundle.properties	Wed Aug 01 19:29:32 2007 +0000
     3.3 @@ -31,4 +31,7 @@
     3.4  
     3.5  #AttachDebuggerAction
     3.6  CTL_Connect=&Attach Debugger...
     3.7 -CTL_Connect_Title=Attach Debugger
     3.8 \ No newline at end of file
     3.9 +CTL_Connect_Title=Attach Debugger
    3.10 +
    3.11 +# BreakpointEnableAction
    3.12 +CTL_enabled=Enabled
     4.1 --- a/bpel.debugger.ui/src/org/netbeans/modules/bpel/debugger/ui/resources/layer.xml	Wed Aug 01 19:24:48 2007 +0000
     4.2 +++ b/bpel.debugger.ui/src/org/netbeans/modules/bpel/debugger/ui/resources/layer.xml	Wed Aug 01 19:29:32 2007 +0000
     4.3 @@ -30,6 +30,9 @@
     4.4                  </folder>
     4.5              </folder>
     4.6          </folder>
     4.7 +        <folder name="Debug">
     4.8 +            <file name="org-netbeans-modules-bpel-debugger-ui-action-BreakpointEnableAction.instance"/>
     4.9 +        </folder>
    4.10      </folder>
    4.11  
    4.12      <folder name="Menu">
    4.13 @@ -112,14 +115,22 @@
    4.14                  </folder>
    4.15              </folder>
    4.16          </folder>
    4.17 +        <folder name="AnnotationTypes">
    4.18 +            <!--
    4.19 +            Hide this action for now - see issue 108002
    4.20 +            <folder name="BreakpointActions">
    4.21 +                <file name="org-netbeans-modules-bpel-debugger-ui-action-BreakpointEnableAction.instance">
    4.22 +                    <attr name="position" intvalue="100"/>
    4.23 +                </file>
    4.24 +            </folder>
    4.25 +            -->
    4.26  <!--  Don't need activity annotations for source editor (they are used on diagram only
    4.27 -        <folder name="AnnotationTypes">
    4.28              <file name="NeverExecutedElement.xml" url="NeverExecutedElement.xml"/>
    4.29              <file name="StartedElement.xml" url="StartedElement.xml"/>
    4.30              <file name="CompletedElement.xml" url="CompletedElement.xml"/>
    4.31              <file name="FaultedElement.xml" url="FaultedElement.xml"/>
    4.32 +-->    
    4.33          </folder>
    4.34 --->    
    4.35      </folder>
    4.36      
    4.37  </filesystem>