Commits

radim  committed ce574ba

Add/remove project library dependency added.

  • Participants
  • Parent commits f2dd745

Comments (0)

Files changed (6)

File project/src/org/netbeans/modules/android/project/AndroidGeneralData.java

     data.setPlatform(AndroidProjects.projectPlatform(project));
     data.setMainProjectDirPath(project.evaluator().getProperty(PropertyName.TEST_PROJECT_DIR.getName()));
     data.setLibrary(project.info().isLibrary());
+    data.setReferencedProjects(project.getLookup().lookup(AndroidInfoImpl.class).getDependentProjectDirPaths());
     return data;
   }
 
   private String projectDirPath;
   private DalvikPlatform platform;
   private String mainProjectDirPath;
-  private boolean isLib = false;
+  private boolean library = false;
+  private Iterable<String> referencedProjects;
 
   public AndroidGeneralData() {
   }
   }
 
   public boolean isLibrary() {
-    return isLib;
+    return library;
   }
 
   public void setLibrary(boolean isLib) {
-    this.isLib = isLib;
+    this.library = isLib;
+  }
+
+  public Iterable<String> getReferencedProjects() {
+    return referencedProjects;
+  }
+
+  public void setReferencedProjects(Iterable<String> referencedProjects) {
+    this.referencedProjects = referencedProjects;
   }
 
   @Override
     if (!Objects.equal(this.mainProjectDirPath, other.mainProjectDirPath)) {
       return false;
     }
-    if (this.isLib != other.isLib) {
+    if (this.library != other.library) {
+      return false;
+    }
+    if (!Objects.equal(this.referencedProjects, other.referencedProjects)) {
       return false;
     }
     return true;
     hash = 67 * hash + (this.projectDirPath != null ? this.projectDirPath.hashCode() : 0);
     hash = 67 * hash + (this.platform != null ? this.platform.hashCode() : 0);
     hash = 67 * hash + (this.mainProjectDirPath != null ? this.mainProjectDirPath.hashCode() : 0);
-    hash = 67 * hash + (this.isLib ? 1 : 0);
+    hash = 67 * hash + (this.library ? 1 : 0);
+    hash = 67 * hash + (this.referencedProjects != null ? this.referencedProjects.hashCode() : 0);
     return hash;
   }
+
+  @Override
+  public String toString() {
+    return "AndroidGeneralData{" + 
+        "projectName=" + projectName + 
+        ", projectDirPath=" + projectDirPath + 
+        ", platform=" + platform + 
+        ", mainProjectDirPath=" + mainProjectDirPath + 
+        ", library=" + library + 
+        ", referencedProjects=" + referencedProjects + '}';
+  }
+  
 }

File project/src/org/netbeans/modules/android/project/AndroidInfoImpl.java

 
 package org.netbeans.modules.android.project;
 
+import com.google.common.base.Function;
 import com.google.common.base.Strings;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
 import java.beans.PropertyChangeListener;
 import java.beans.PropertyChangeSupport;
 import java.io.File;
-import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.logging.Level;
     return Boolean.valueOf(project.evaluator().getProperty(ANDROID_LIBRARY_PROPERTY));
   }
   
+  public Iterable<String> getDependentProjectDirPaths() {
+    List<String> prjDirs = Lists.newArrayList();
+    Map<String, String> properties = project.evaluator().getProperties();
+    int i = 1;
+    while (true) {
+      String value = properties.get("android.library.reference." + String.valueOf(i));
+      if (value == null) {
+        break;
+      }
+      prjDirs.add(value);
+      i++;
+    }
+    return prjDirs;
+  }
+  
   @Override
   public Iterable<File> getDependentProjectDirs() {
-    List<File> prjDirs = new ArrayList<File>();
-    for (Map.Entry<String, String> prop : project.evaluator().getProperties().entrySet()) {
-      // TODO sort by number?
-      if (prop.getKey().startsWith("android.library.reference.")) {
-        File libPrjDir = new File(project.getProjectDirectoryFile(), prop.getValue());
-        prjDirs.add(libPrjDir);
+    return Iterables.transform(getDependentProjectDirPaths(), new Function<String, File>() {
+
+      @Override
+      public File apply(String input) {
+        return new File(project.getProjectDirectoryFile(), input);
       }
-    }
-    return prjDirs;
+      
+    });
   }
   
   @Override

File project/src/org/netbeans/modules/android/project/AndroidProjectImpl.java

         propertyHelper.setProperty(
           ProjectProperties.PropertyType.DEFAULT, AndroidInfoImpl.ANDROID_LIBRARY_PROPERTY, null);
       }
+      int i = 1;
+      for (String referencedPrj : data.getReferencedProjects()) {
+        propertyHelper.setProperty(
+          ProjectProperties.PropertyType.DEFAULT, "android.library.reference." + String.valueOf(i), referencedPrj);
+        i++;
+      }
+      // make a hole: acts as a stopmark
+      propertyHelper.setProperty(
+        ProjectProperties.PropertyType.DEFAULT, "android.library.reference." + String.valueOf(i), null);
     } else {
       prjCreator.updateTestProject(data.getProjectDirPath(), data.getMainProjectDirPath(), sdkManager);
     }

File project/src/org/netbeans/modules/android/project/ui/LibrariesNode.java

               project.getLookup().lookup(AndroidClassPath.class);
           if (cpProvider != null) {
             cpProvider.getClassPath(ClassPath.COMPILE).addPropertyChangeListener(this);
+            // TODO lib projects are added to source CP
+            cpProvider.getClassPath(ClassPath.SOURCE).addPropertyChangeListener(this);
           }
           this.setKeys(getKeys ());
         }
           AndroidClassPath cpProvider = project.getLookup().lookup(AndroidClassPath.class);
           if (cpProvider != null) {
             cpProvider.getClassPath(ClassPath.COMPILE).removePropertyChangeListener(this);
+            cpProvider.getClassPath(ClassPath.SOURCE).removePropertyChangeListener(this);
           }
           this.setKeys(Collections.<Key>emptySet());
         }

File project/src/org/netbeans/modules/android/project/ui/customizer/CustomizerLibraries.form

 <?xml version="1.1" encoding="UTF-8" ?>
 
-<Form version="1.2" maxVersion="1.2" type="org.netbeans.modules.form.forminfo.JPanelFormInfo">
+<Form version="1.6" maxVersion="1.7" type="org.netbeans.modules.form.forminfo.JPanelFormInfo">
   <AuxValues>
     <AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
     <AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
     </Component>
     <Component class="javax.swing.JLabel" name="jLabelReferrencedProjects">
       <Properties>
-        <Property name="text" type="java.lang.String" value="Referrenced library projects:"/>
+        <Property name="text" type="java.lang.String" value="Referenced library projects:"/>
       </Properties>
     </Component>
     <Container class="javax.swing.JScrollPane" name="jScrollPane1">
               <TableColumnModel selectionModel="1"/>
             </Property>
             <Property name="columnSelectionAllowed" type="boolean" value="true"/>
+            <Property name="selectionModel" type="javax.swing.ListSelectionModel" editor="org.netbeans.modules.form.editors2.JTableSelectionModelEditor">
+              <JTableSelectionModel selectionMode="0"/>
+            </Property>
             <Property name="tableHeader" type="javax.swing.table.JTableHeader" editor="org.netbeans.modules.form.editors2.JTableHeaderEditor">
               <TableHeader reorderingAllowed="true" resizingAllowed="true"/>
             </Property>
       <Properties>
         <Property name="text" type="java.lang.String" value="Add"/>
       </Properties>
+      <Events>
+        <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jButtonAddPrjActionPerformed"/>
+      </Events>
     </Component>
     <Component class="javax.swing.JButton" name="jButtonRemovePrj">
       <Properties>
         <Property name="text" type="java.lang.String" value="Remove"/>
+        <Property name="enabled" type="boolean" value="false"/>
       </Properties>
+      <Events>
+        <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jButtonRemovePrjActionPerformed"/>
+      </Events>
     </Component>
   </SubComponents>
 </Form>

File project/src/org/netbeans/modules/android/project/ui/customizer/CustomizerLibraries.java

 
 package org.netbeans.modules.android.project.ui.customizer;
 
+import com.google.common.collect.Lists;
 import java.io.File;
 import java.io.IOException;
+import java.util.List;
 import java.util.logging.Level;
 import java.util.logging.Logger;
+import javax.swing.JFileChooser;
 import javax.swing.JPanel;
+import javax.swing.event.ListSelectionEvent;
+import javax.swing.event.ListSelectionListener;
 import javax.swing.table.DefaultTableModel;
 import org.netbeans.api.project.Project;
-import org.netbeans.api.project.ProjectInformation;
 import org.netbeans.api.project.ProjectManager;
 import org.netbeans.modules.android.project.AndroidGeneralData;
 import org.netbeans.modules.android.project.AndroidProject;
-import org.openide.filesystems.FileObject;
+import org.netbeans.spi.project.support.ant.PropertyUtils;
+import org.netbeans.spi.project.ui.support.ProjectChooser;
+import org.openide.DialogDisplayer;
+import org.openide.NotifyDescriptor;
 import org.openide.filesystems.FileUtil;
 
 /** 
   public CustomizerLibraries(AndroidGeneralData data, AndroidProject project) {
     this.data = data;
     this.project = project;
+    String[] referencedProjects = 
+        Lists.newArrayList(data.getReferencedProjects()).toArray(new String[0]);
+    Object[][] referencedProjectsData = new Object[referencedProjects.length][];
+    for (int i = 0; i < referencedProjects.length; i++) {
+      referencedProjectsData[i] = new Object[] { 
+          new File(project.getProjectDirectoryFile(), referencedProjects[i]).getName(), 
+          referencedProjects[i]};
+    }
     libsTableModel = new javax.swing.table.DefaultTableModel(
-        new Object [][] {},
+        referencedProjectsData,
         new String [] {"Name", "Directory"}) {
       Class[] types = new Class [] {
         java.lang.String.class, java.lang.String.class
         return canEdit[columnIndex];
       }
     };
-    initComponents();        
+    initComponents(); 
     initFromProject(project);
+    jTableLibs.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
+
+      @Override
+      public void valueChanged(ListSelectionEvent e) {
+        updateData();
+      }
+    });
   }
         
     /** This method is called from within the constructor to
       }
     });
 
-    org.openide.awt.Mnemonics.setLocalizedText(jLabelReferrencedProjects, "Referrenced library projects:");
+    org.openide.awt.Mnemonics.setLocalizedText(jLabelReferrencedProjects, "Referenced library projects:");
 
     jTableLibs.setModel(libsTableModel);
     jTableLibs.setColumnSelectionAllowed(true);
+    jTableLibs.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
     jScrollPane1.setViewportView(jTableLibs);
     jTableLibs.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
 
     org.openide.awt.Mnemonics.setLocalizedText(jButtonAddPrj, "Add");
+    jButtonAddPrj.addActionListener(new java.awt.event.ActionListener() {
+      public void actionPerformed(java.awt.event.ActionEvent evt) {
+        jButtonAddPrjActionPerformed(evt);
+      }
+    });
 
     org.openide.awt.Mnemonics.setLocalizedText(jButtonRemovePrj, "Remove");
+    jButtonRemovePrj.setEnabled(false);
+    jButtonRemovePrj.addActionListener(new java.awt.event.ActionListener() {
+      public void actionPerformed(java.awt.event.ActionEvent evt) {
+        jButtonRemovePrjActionPerformed(evt);
+      }
+    });
 
     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
     this.setLayout(layout);
   private void jCheckBoxIsLibActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxIsLibActionPerformed
     updateData();
   }//GEN-LAST:event_jCheckBoxIsLibActionPerformed
+
+  private void jButtonRemovePrjActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRemovePrjActionPerformed
+    int row = jTableLibs.getSelectedRow();
+    if (row == -1) {
+      // weird
+      return;
+    }
+    libsTableModel.removeRow(row);
+    updateData();
+  }//GEN-LAST:event_jButtonRemovePrjActionPerformed
+
+  private void jButtonAddPrjActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddPrjActionPerformed
+    JFileChooser chooser = ProjectChooser.projectChooser();
+    chooser.setDialogTitle("Select Android Library Project");
+    int option = chooser.showOpenDialog(null);
+
+    if (option == JFileChooser.APPROVE_OPTION) {
+      File prjDir = chooser.getSelectedFile();
+      try {
+        Project p = ProjectManager.getDefault().findProject(
+            FileUtil.toFileObject(FileUtil.normalizeFile(prjDir)));
+        AndroidProject ap = p.getLookup().lookup(AndroidProject.class);
+        if (ap.info().isLibrary()) {
+          String relativePath = 
+              PropertyUtils.relativizeFile(project.getProjectDirectoryFile(), prjDir);
+          if (relativePath != null) {
+            // TODO check if it is already there
+            libsTableModel.addRow(new Object[] {prjDir.getName(), relativePath});
+            updateData();
+          } else {
+            NotifyDescriptor d = new NotifyDescriptor.Message(
+                "Cannot find relative path to " + prjDir.getAbsolutePath(), 
+                NotifyDescriptor.WARNING_MESSAGE);
+            DialogDisplayer.getDefault().notify(d);
+          }
+          return;
+        }
+      } catch (IOException ex) {
+        LOG.log(Level.WARNING, null, ex);
+      } catch (IllegalArgumentException ex) {
+        LOG.log(Level.WARNING, null, ex);
+      }
+      NotifyDescriptor d = new NotifyDescriptor.Message(
+          "You have to select Android Library Project.", 
+          NotifyDescriptor.WARNING_MESSAGE);
+      DialogDisplayer.getDefault().notify(d);
+    }
+  }//GEN-LAST:event_jButtonAddPrjActionPerformed
     
   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JButton jButtonAddPrj;
         
   private void initFromProject(AndroidProject project) {
     jCheckBoxIsLib.setSelected(project.info().isLibrary());
-    for (File lib : project.info().getDependentProjectDirs()) {
-      FileObject libFo = FileUtil.toFileObject(FileUtil.normalizeFile(lib));
-      Project libPrj = null;
-      try {
-        libPrj = libFo != null ? ProjectManager.getDefault().findProject(libFo) : null;
-      } catch (IOException ex) {
-        LOG.log(Level.INFO, null, ex);
-      } catch (IllegalArgumentException ex) {
-        LOG.log(Level.INFO, null, ex);
-      }
-      String name = libPrj != null ? 
-          libPrj.getLookup().lookup(ProjectInformation.class).getDisplayName() : 
-          lib.getName();
-      String path = lib.getPath();
-      libsTableModel.addRow(new Object[] {name, path});
-    }
+//    for (File lib : project.info().getDependentProjectDirs()) {
+//      FileObject libFo = FileUtil.toFileObject(FileUtil.normalizeFile(lib));
+//      Project libPrj = null;
+//      try {
+//        libPrj = libFo != null ? ProjectManager.getDefault().findProject(libFo) : null;
+//      } catch (IOException ex) {
+//        LOG.log(Level.INFO, null, ex);
+//      } catch (IllegalArgumentException ex) {
+//        LOG.log(Level.INFO, null, ex);
+//      }
+//      String name = libPrj != null ? 
+//          libPrj.getLookup().lookup(ProjectInformation.class).getDisplayName() : 
+//          lib.getName();
+//      String path = lib.getPath();
+//      libsTableModel.addRow(new Object[] {name, path});
+//    }
   }
 
   private void updateData() {
     data.setLibrary(jCheckBoxIsLib.isSelected());
+    List<String> projects = Lists.newArrayList();
+    for (int i = 0; i < libsTableModel.getRowCount(); i++) {
+      projects.add((String) libsTableModel.getValueAt(i, 1));
+    }
+    data.setReferencedProjects(projects);
+    jButtonRemovePrj.setEnabled(jTableLibs.getSelectedRowCount() == 1);
   }
 }