cnd.debugger.gdbserver/src/org/netbeans/modules/cnd/debugger/gdbserver/GdbServerAttachPanel.java
author Alexander Simon <alexvsimon@netbeans.org>
Tue, 22 Jul 2014 11:00:29 +0400
changeset 18201 de3f3986d3a0
parent 18198 3e71abc1fca6
child 18333 086f4a89919f
permissions -rw-r--r--
fixed Bug #245843 ClassCastException: NativeProject.getProject() cannot be cast to org.netbeans.api.project.Project
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2010 Oracle and/or its affiliates. All rights reserved.
     5  *
     6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
     7  * Other names may be trademarks of their respective owners.
     8  *
     9  * The contents of this file are subject to the terms of either the GNU
    10  * General Public License Version 2 only ("GPL") or the Common
    11  * Development and Distribution License("CDDL") (collectively, the
    12  * "License"). You may not use this file except in compliance with the
    13  * License. You can obtain a copy of the License at
    14  * http://www.netbeans.org/cddl-gplv2.html
    15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    16  * specific language governing permissions and limitations under the
    17  * License.  When distributing the software, include this License Header
    18  * Notice in each file and include the License file at
    19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    20  * particular file as subject to the "Classpath" exception as provided
    21  * by Oracle in the GPL Version 2 section of the License file that
    22  * accompanied this code. If applicable, add the following below the
    23  * License Header, with the fields enclosed by brackets [] replaced by
    24  * your own identifying information:
    25  * "Portions Copyrighted [year] [name of copyright owner]"
    26  *
    27  * If you wish your version of this file to be governed by only the CDDL
    28  * or only the GPL Version 2, indicate your decision by adding
    29  * "[Contributor] elects to include this software in this distribution
    30  * under the [CDDL or GPL Version 2] license." If you do not indicate a
    31  * single choice of license, a recipient has the option to distribute
    32  * your version of this file under either the CDDL, the GPL Version 2 or
    33  * to extend the choice of license to its licensees as provided above.
    34  * However, if you add GPL Version 2 code and therefore, elected the GPL
    35  * Version 2 license, then the option applies only if the new code is
    36  * made subject to such option by the copyright holder.
    37  *
    38  * Contributor(s):
    39  *
    40  * Portions Copyrighted 2009 Sun Microsystems, Inc.
    41  */
    42 
    43 /*
    44  * GdbServerAttachPanel.java
    45  *
    46  * Created on Aug 14, 2009, 7:39:50 PM
    47  */
    48 
    49 package org.netbeans.modules.cnd.debugger.gdbserver;
    50 
    51 import java.beans.PropertyChangeListener;
    52 import java.lang.invoke.MethodHandles;
    53 import javax.swing.JPanel;
    54 import org.netbeans.modules.cnd.api.remote.RemoteSyncSupport;
    55 import org.netbeans.modules.cnd.debugger.common2.debugger.NativeDebuggerInfo;
    56 import org.netbeans.modules.cnd.debugger.common2.debugger.NativeDebuggerManager;
    57 import org.netbeans.modules.cnd.debugger.common2.debugger.actions.ExecutableProjectPanel;
    58 import org.netbeans.modules.cnd.debugger.common2.debugger.actions.ExecutableProjectPanel.ProjectCBItem;
    59 import org.netbeans.modules.cnd.debugger.common2.debugger.debugtarget.DebugTarget;
    60 import org.netbeans.modules.cnd.debugger.common2.debugger.remote.CndRemote;
    61 import org.netbeans.modules.cnd.debugger.gdb2.options.GdbDebuggerInfoFactory;
    62 import org.netbeans.modules.cnd.makeproject.api.configurations.ConfigurationSupport;
    63 import org.netbeans.modules.cnd.makeproject.api.configurations.MakeConfiguration;
    64 import org.netbeans.modules.nativeexecution.api.ExecutionEnvironment;
    65 import org.netbeans.spi.debugger.ui.Controller;
    66 import org.openide.util.HelpCtx;
    67 import org.openide.util.Lookup;
    68 import org.openide.util.NbPreferences;
    69 
    70 /**
    71  *
    72  * @author Egor Ushakov
    73  */
    74 public class GdbServerAttachPanel extends JPanel implements HelpCtx.Provider {
    75     private final Controller controller;
    76 
    77     private static final String TARGET_KEY = "last-gdbserver-target"; //NOI18N
    78 
    79     /** Creates new form GdbServerAttachPanel */
    80     public GdbServerAttachPanel() {
    81         controller = new GdbServerAttachController();
    82         initComponents();
    83         // Fill the Projects combo box
    84         ExecutableProjectPanel.fillProjectsCombo(projectCB, null);
    85         targetTF.setText(NbPreferences.forModule(GdbServerAttachPanel.class).get(TARGET_KEY, "remote host:port")); //NOI18N
    86     }
    87 
    88     Controller getController() {
    89         return controller;
    90     }
    91 
    92     /** This method is called from within the constructor to
    93      * initialize the form.
    94      * WARNING: Do NOT modify this code. The content of this method is
    95      * always regenerated by the Form Editor.
    96      */
    97     @SuppressWarnings("unchecked")
    98     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    99     private void initComponents() {
   100 
   101         projectLabel = new javax.swing.JLabel();
   102         projectCB = new javax.swing.JComboBox();
   103         jLabel1 = new javax.swing.JLabel();
   104         targetTF = new javax.swing.JTextField();
   105 
   106         projectLabel.setText(org.openide.util.NbBundle.getMessage(GdbServerAttachPanel.class, "GdbServerAttachPanel.projectLabel.text")); // NOI18N
   107 
   108         jLabel1.setText(org.openide.util.NbBundle.getMessage(GdbServerAttachPanel.class, "GdbServerAttachPanel.jLabel1.text")); // NOI18N
   109 
   110         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
   111         this.setLayout(layout);
   112         layout.setHorizontalGroup(
   113             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   114             .addGroup(layout.createSequentialGroup()
   115                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   116                     .addComponent(projectLabel)
   117                     .addComponent(jLabel1))
   118                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   119                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   120                     .addComponent(targetTF, javax.swing.GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE)
   121                     .addComponent(projectCB, 0, 395, Short.MAX_VALUE)))
   122         );
   123         layout.setVerticalGroup(
   124             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   125             .addGroup(layout.createSequentialGroup()
   126                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
   127                     .addComponent(jLabel1)
   128                     .addComponent(targetTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
   129                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   130                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
   131                     .addComponent(projectLabel)
   132                     .addComponent(projectCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
   133                 .addContainerGap())
   134         );
   135     }// </editor-fold>//GEN-END:initComponents
   136 
   137 
   138     // Variables declaration - do not modify//GEN-BEGIN:variables
   139     private javax.swing.JLabel jLabel1;
   140     private javax.swing.JComboBox projectCB;
   141     private javax.swing.JLabel projectLabel;
   142     private javax.swing.JTextField targetTF;
   143     // End of variables declaration//GEN-END:variables
   144 
   145     private class GdbServerAttachController implements Controller {
   146 
   147         @Override
   148         public boolean cancel() {
   149             return true;
   150         }
   151 
   152         @Override
   153         public boolean ok() {
   154             String targetValue = targetTF.getText();
   155             if (targetValue.length() == 0) {
   156                 return false;
   157             }
   158             
   159             //store last values
   160             NbPreferences.forModule(GdbServerAttachPanel.class).put(TARGET_KEY, targetValue);
   161 
   162             ProjectCBItem pi = (ProjectCBItem) projectCB.getSelectedItem();
   163             if (pi != null) {
   164                 MakeConfiguration conf = ConfigurationSupport.getProjectActiveConfiguration(pi.getProject()).clone();
   165                 DebugTarget dt = new DebugTarget(conf);
   166                 
   167                 // set executable
   168                 String path = conf.getAbsoluteOutputValue().replace("\\", "/"); // NOI18N
   169                 ExecutionEnvironment exEnv = conf.getDevelopmentHost().getExecutionEnvironment();
   170                 path = RemoteSyncSupport.getPathMap(exEnv, pi.getProject()).getRemotePath(path, true);
   171                 dt.setExecutable(path);
   172 
   173                 // always use gdb
   174                 NativeDebuggerInfo gdi = GdbDebuggerInfoFactory.create(dt, 
   175                         CndRemote.userhostFromConfiguration(conf), conf, NativeDebuggerManager.ATTACH, targetValue);                
   176                 NativeDebuggerManager.get().debugNoAsk(gdi);
   177 //                try {
   178 //                    GdbDebugger.attachGdbServer(target, pi.getProjectInformation());
   179 //                } catch (DebuggerStartException dse) {
   180 //                    DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
   181 //                            NbBundle.getMessage(GdbServerAttachPanel.class,
   182 //                           "ERR_UnexpecedAttachGdbServerFailure", target))); // NOI18N
   183 //                }
   184             }
   185             return true;
   186         }
   187         
   188         @Override
   189         public boolean isValid() {
   190             return projectCB.getItemCount() > 0;
   191         }
   192 
   193         @Override
   194         public void addPropertyChangeListener(PropertyChangeListener l) {
   195         }
   196 
   197         @Override
   198         public void removePropertyChangeListener(PropertyChangeListener l) {
   199         }
   200 
   201     }
   202 
   203     @Override
   204     public HelpCtx getHelpCtx() {
   205         return new HelpCtx("gdbserver"); // NOI18N
   206     }
   207 }