Илья Егоров 8 年之前
父节点
当前提交
4f05986a43

+ 2 - 1
.gitignore

@@ -1,2 +1,3 @@
 /nbproject/private/
-/build/
+/build/
+/dist/

+ 1 - 1
nbproject/project.properties

@@ -67,7 +67,7 @@ javadoc.splitindex=true
 javadoc.use=true
 javadoc.version=false
 javadoc.windowtitle=
-main.class=project82usb.main
+main.class=project82usb.devicesmatrix
 manifest.file=manifest.mf
 meta.inf.dir=${src.dir}/META-INF
 mkdist.disabled=false

+ 57 - 2
src/project82usb/devicesmatrix.form

@@ -23,12 +23,35 @@
   <Layout>
     <DimensionLayout dim="0">
       <Group type="103" groupAlignment="0" attributes="0">
-          <Component id="jScrollPane1" alignment="0" pref="400" max="32767" attributes="0"/>
+          <Component id="jScrollPane1" pref="400" max="32767" attributes="0"/>
+          <Group type="102" attributes="0">
+              <EmptySpace max="-2" attributes="0"/>
+              <Group type="103" groupAlignment="0" attributes="0">
+                  <Group type="102" alignment="1" attributes="0">
+                      <EmptySpace min="0" pref="0" max="32767" attributes="0"/>
+                      <Component id="btnRefresh" min="-2" max="-2" attributes="0"/>
+                      <EmptySpace type="separate" max="-2" attributes="0"/>
+                      <Component id="btnClear" min="-2" max="-2" attributes="0"/>
+                  </Group>
+                  <Component id="jScrollPane2" max="32767" attributes="0"/>
+              </Group>
+              <EmptySpace max="-2" attributes="0"/>
+          </Group>
       </Group>
     </DimensionLayout>
     <DimensionLayout dim="1">
       <Group type="103" groupAlignment="0" attributes="0">
-          <Component id="jScrollPane1" alignment="0" pref="300" max="32767" attributes="0"/>
+          <Group type="102" alignment="0" attributes="0">
+              <Component id="jScrollPane1" min="-2" pref="126" max="-2" attributes="0"/>
+              <EmptySpace max="-2" attributes="0"/>
+              <Component id="jScrollPane2" pref="254" max="32767" attributes="0"/>
+              <EmptySpace type="unrelated" max="-2" attributes="0"/>
+              <Group type="103" groupAlignment="3" attributes="0">
+                  <Component id="btnClear" alignment="3" min="-2" max="-2" attributes="0"/>
+                  <Component id="btnRefresh" alignment="3" min="-2" max="-2" attributes="0"/>
+              </Group>
+              <EmptySpace max="-2" attributes="0"/>
+          </Group>
       </Group>
     </DimensionLayout>
   </Layout>
@@ -54,5 +77,37 @@
         </Component>
       </SubComponents>
     </Container>
+    <Container class="javax.swing.JScrollPane" name="jScrollPane2">
+      <AuxValues>
+        <AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/>
+      </AuxValues>
+
+      <Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
+      <SubComponents>
+        <Component class="javax.swing.JTextArea" name="tvOut">
+          <Properties>
+            <Property name="editable" type="boolean" value="false"/>
+            <Property name="columns" type="int" value="20"/>
+            <Property name="rows" type="int" value="5"/>
+          </Properties>
+        </Component>
+      </SubComponents>
+    </Container>
+    <Component class="javax.swing.JButton" name="btnClear">
+      <Properties>
+        <Property name="text" type="java.lang.String" value="Clear"/>
+      </Properties>
+      <Events>
+        <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btnClearActionPerformed"/>
+      </Events>
+    </Component>
+    <Component class="javax.swing.JButton" name="btnRefresh">
+      <Properties>
+        <Property name="text" type="java.lang.String" value="Refresh"/>
+      </Properties>
+      <Events>
+        <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btnRefreshActionPerformed"/>
+      </Events>
+    </Component>
   </SubComponents>
 </Form>

+ 61 - 2
src/project82usb/devicesmatrix.java

@@ -4,13 +4,14 @@
  * and open the template in the editor.
  */
 package project82usb;
+import project82usb.usbDeviceList.*;
 
 /**
  *
  * @author ilysk
  */
 public class devicesmatrix extends javax.swing.JFrame {
-
+    private final usbDeviceList list = new usbDeviceList();
     /**
      * Creates new form devicesmatrix
      */
@@ -29,6 +30,10 @@ public class devicesmatrix extends javax.swing.JFrame {
 
         jScrollPane1 = new javax.swing.JScrollPane();
         jTable1 = new javax.swing.JTable();
+        jScrollPane2 = new javax.swing.JScrollPane();
+        tvOut = new javax.swing.JTextArea();
+        btnClear = new javax.swing.JButton();
+        btnRefresh = new javax.swing.JButton();
 
         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 
@@ -45,20 +50,66 @@ public class devicesmatrix extends javax.swing.JFrame {
         ));
         jScrollPane1.setViewportView(jTable1);
 
+        tvOut.setEditable(false);
+        tvOut.setColumns(20);
+        tvOut.setRows(5);
+        jScrollPane2.setViewportView(tvOut);
+
+        btnClear.setText("Clear");
+        btnClear.addActionListener(new java.awt.event.ActionListener() {
+            public void actionPerformed(java.awt.event.ActionEvent evt) {
+                btnClearActionPerformed(evt);
+            }
+        });
+
+        btnRefresh.setText("Refresh");
+        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
+            public void actionPerformed(java.awt.event.ActionEvent evt) {
+                btnRefreshActionPerformed(evt);
+            }
+        });
+
         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
         getContentPane().setLayout(layout);
         layout.setHorizontalGroup(
             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
             .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
+            .addGroup(layout.createSequentialGroup()
+                .addContainerGap()
+                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
+                        .addGap(0, 0, Short.MAX_VALUE)
+                        .addComponent(btnRefresh)
+                        .addGap(18, 18, 18)
+                        .addComponent(btnClear))
+                    .addComponent(jScrollPane2))
+                .addContainerGap())
         );
         layout.setVerticalGroup(
             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
-            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
+            .addGroup(layout.createSequentialGroup()
+                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
+                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
+                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
+                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
+                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
+                    .addComponent(btnClear)
+                    .addComponent(btnRefresh))
+                .addContainerGap())
         );
 
         pack();
     }// </editor-fold>//GEN-END:initComponents
 
+    private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
+        this.tvOut.setText("");
+    }//GEN-LAST:event_btnClearActionPerformed
+
+    private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
+        list.getUsbDeviceList();
+        list.printDeviceList(this);
+    }//GEN-LAST:event_btnRefreshActionPerformed
+
     /**
      * @param args the command line arguments
      */
@@ -93,9 +144,17 @@ public class devicesmatrix extends javax.swing.JFrame {
             }
         });
     }
+    
+    public void addTextTvOut(String text){
+        tvOut.setText(tvOut.getText()+text+"\n");
+    }
 
     // Variables declaration - do not modify//GEN-BEGIN:variables
+    private javax.swing.JButton btnClear;
+    private javax.swing.JButton btnRefresh;
     private javax.swing.JScrollPane jScrollPane1;
+    private javax.swing.JScrollPane jScrollPane2;
     private javax.swing.JTable jTable1;
+    private javax.swing.JTextArea tvOut;
     // End of variables declaration//GEN-END:variables
 }

+ 0 - 21
src/project82usb/main.java

@@ -1,21 +0,0 @@
-/*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
-package project82usb;
-
-import static project82usb.usbDeviceList.*;
-import java.util.ArrayList;
-
-/**
- *
- * @author ilysk
- */
-
-public class main {
-    public static void main(String[] args){
-        ArrayList<usbDevice> list = getDeviceList();
-        printDeviceList(list);
-    }
-}

+ 4 - 3
src/project82usb/usbDevice.java

@@ -10,9 +10,10 @@ package project82usb;
  * @author ilysk
  */
 public class usbDevice {
-    private short idVendor;
-    private short idProduct;
-    private byte port;
+    private final short idVendor;
+    private final short idProduct;
+    private final byte port;
+    
     public usbDevice(short idVendor, short idProduct, byte port){
         this.idVendor=idVendor;
         this.idProduct=idProduct;

+ 14 - 8
src/project82usb/usbDeviceList.java

@@ -12,14 +12,19 @@ import org.usb4java.DeviceList;
 import org.usb4java.LibUsb;
 import org.usb4java.LibUsbException;
 import java.util.ArrayList;
+import project82usb.devicesmatrix.*;
 
 /**
  *
  * @author ilysk
  */
 public class usbDeviceList {
-    public static ArrayList<usbDevice> getDeviceList() {
-        ArrayList<usbDevice> devList = new ArrayList<usbDevice>();
+    private final ArrayList<usbDevice> devList;
+    public usbDeviceList(){
+        this.devList = new ArrayList<>();
+    }
+    public void getUsbDeviceList() {
+        this.devList.clear();
         Context context = new Context();
         int result = LibUsb.init(context);
         if (result != LibUsb.SUCCESS) {
@@ -35,17 +40,18 @@ public class usbDeviceList {
                 result = LibUsb.getDeviceDescriptor(device, descriptor);
                 if (result != LibUsb.SUCCESS) throw new LibUsbException("Unable to read device descriptor", result);
                 usbDevice usbDev = new usbDevice(descriptor.idVendor(),descriptor.idProduct(), (byte) LibUsb.getPortNumber(device));
-                devList.add(usbDev);
+                this.devList.add(usbDev);
             }
         } finally {
             // Ensure the allocated device list is freed
             LibUsb.freeDeviceList(list, true);
-        }
-        return devList;        
+        }       
     }
-    public static void printDeviceList(ArrayList<usbDevice> list){
-        for(usbDevice device:list){
-            System.out.println("Device "+device.getStringIdVendor()+":"+device.getStringIdProduct()+" on port #"+device.getPort());
+    public void printDeviceList(devicesmatrix out){
+        out.addTextTvOut("Printing device list:");
+        for(usbDevice device:this.devList){
+            out.addTextTvOut("Device "+device.getStringIdVendor()+":"+device.getStringIdProduct()+" on port  #"+device.getPort());
         }
+        out.addTextTvOut("");
     }
 }