Skip to content

Commit 53fb421

Browse files
committed
Fix #54; Update UsbConnect messages format
1 parent 272702f commit 53fb421

File tree

4 files changed

+83
-26
lines changed

4 files changed

+83
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Sometimes I add new posts about this project [on my home page](https://developer
2323

2424
#### License
2525

26-
[GNU General Public License version 3]((https://github.com/developersu/ns-usbloader/blob/master/LICENSE)), or (at your option) any later version.
26+
[GNU General Public License version 3](https://github.com/developersu/ns-usbloader/blob/master/LICENSE), or (at your option) any later version.
2727

2828
#### Used libraries & resources
2929
* [OpenJFX](https://wiki.openjdk.java.net/display/OpenJFX/Main)

src/main/java/nsusbloader/COM/USB/UsbConnect.java

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -107,27 +107,23 @@ private void createContextAndInitLibUSB() throws Exception{
107107

108108
returningValue = LibUsb.init(contextNS);
109109
if (returningValue != LibUsb.SUCCESS)
110-
throw new Exception("libusb initialization\n Returned: "+UsbErrorCodes.getErrCode(returningValue));
110+
throw new Exception("LibUSB initialization failed: "+UsbErrorCodes.getErrCode(returningValue));
111111
}
112112

113113
private void getDeviceList() throws Exception{
114114
deviceList = new DeviceList();
115115
returningValue = LibUsb.getDeviceList(contextNS, deviceList);
116116
if (returningValue < 0)
117-
throw new Exception("Get device list\n Returned: "+UsbErrorCodes.getErrCode(returningValue));
117+
throw new Exception("Can't get device list: "+UsbErrorCodes.getErrCode(returningValue));
118118
}
119119

120120
private void findDevice() throws Exception{
121121
// Searching for NS in devices: looking for NS
122122
DeviceDescriptor descriptor;
123123

124124
for (Device device: deviceList){
125-
descriptor = new DeviceDescriptor();
126-
returningValue = LibUsb.getDeviceDescriptor(device, descriptor);
127-
if (returningValue != LibUsb.SUCCESS){
128-
this.freeDeviceList();
129-
throw new Exception("Read file descriptors for USB devices\n Returned: "+UsbErrorCodes.getErrCode(returningValue));
130-
}
125+
descriptor = getDeviceDescriptor(device);
126+
131127
if ((descriptor.idVendor() == VENDOR_ID) && descriptor.idProduct() == PRODUCT_ID){
132128
deviceNS = device;
133129
break;
@@ -139,6 +135,18 @@ private void findDevice() throws Exception{
139135
}
140136
}
141137

138+
private DeviceDescriptor getDeviceDescriptor(Device device) throws Exception{
139+
DeviceDescriptor descriptor = new DeviceDescriptor();
140+
141+
returningValue = LibUsb.getDeviceDescriptor(device, descriptor);
142+
143+
if (returningValue != LibUsb.SUCCESS){
144+
this.freeDeviceList();
145+
throw new Exception("Get USB device descriptor failure: "+UsbErrorCodes.getErrCode(returningValue));
146+
}
147+
return descriptor;
148+
}
149+
142150
private void openDevice() throws Exception{
143151
// Handle NS device
144152
handlerNS = new DeviceHandle();
@@ -150,15 +158,15 @@ private void openDevice() throws Exception{
150158
handlerNS = null; // Avoid issue on close();
151159
if (returningValue == LibUsb.ERROR_ACCESS) {
152160
throw new Exception(String.format(
153-
"Open NS USB device\n Returned: %s\n" +
161+
"Can't open NS USB device: %s\n" +
154162
"Double check that you have administrator privileges (you're 'root') or check 'udev' rules set for this user (linux only)!\n\n" +
155163
"Steps to set 'udev' rules:\n" +
156164
"root # vim /etc/udev/rules.d/99-NS" + ((RCM_VID == VENDOR_ID) ? "RCM" : "") + ".rules\n" +
157165
"SUBSYSTEM==\"usb\", ATTRS{idVendor}==\"%04x\", ATTRS{idProduct}==\"%04x\", GROUP=\"plugdev\"\n" +
158166
"root # udevadm control --reload-rules && udevadm trigger\n", UsbErrorCodes.getErrCode(returningValue), VENDOR_ID, PRODUCT_ID));
159167
}
160168
else
161-
throw new Exception("Open NS USB device\n Returned: "+UsbErrorCodes.getErrCode(returningValue));
169+
throw new Exception("Can't open NS USB device: "+UsbErrorCodes.getErrCode(returningValue));
162170
}
163171

164172
private void freeDeviceList(){
@@ -182,13 +190,13 @@ private void resetDevice(){
182190
private void setConfiguration(int configuration) throws Exception{
183191
returningValue = LibUsb.setConfiguration(handlerNS, configuration);
184192
if (returningValue != LibUsb.SUCCESS)
185-
throw new Exception("Set active configuration to device\n Returned: "+UsbErrorCodes.getErrCode(returningValue));
193+
throw new Exception("Unable to set active configuration on device: "+UsbErrorCodes.getErrCode(returningValue));
186194
}
187195
private void claimInterface() throws Exception{
188196
// Claim interface
189197
returningValue = LibUsb.claimInterface(handlerNS, DEFAULT_INTERFACE);
190198
if (returningValue != LibUsb.SUCCESS)
191-
throw new Exception("Claim interface\n Returned: "+UsbErrorCodes.getErrCode(returningValue));
199+
throw new Exception("Claim interface failure: "+UsbErrorCodes.getErrCode(returningValue));
192200
}
193201

194202
/**
@@ -222,18 +230,16 @@ public void close(){
222230
// Try to release interface
223231
returningValue = LibUsb.releaseInterface(handlerNS, DEFAULT_INTERFACE);
224232

225-
if (returningValue != LibUsb.SUCCESS)
226-
logPrinter.print("Release interface\n Returned: "+returningValue+" (sometimes it's not an issue)", EMsgType.WARNING);
227-
else
228-
logPrinter.print("Release interface", EMsgType.PASS);
233+
if (returningValue != LibUsb.SUCCESS) {
234+
logPrinter.print("Release interface failure: " +
235+
UsbErrorCodes.getErrCode(returningValue) +
236+
" (sometimes it's not an issue)", EMsgType.WARNING);
237+
}
229238

230239
LibUsb.close(handlerNS);
231-
logPrinter.print("Requested handler close", EMsgType.INFO);
232240
}
233241
// Close context in the end
234-
if (contextNS != null) {
242+
if (contextNS != null)
235243
LibUsb.exit(contextNS);
236-
logPrinter.print("Requested context close", EMsgType.INFO);
237-
}
238244
}
239245
}

src/main/java/nsusbloader/Controllers/FrontController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ private void selectFilesBtnAction(){
197197
fileChooser.setTitle(resourceBundle.getString("btn_OpenFile"));
198198

199199
File validator = new File(previouslyOpenedPath);
200-
if (validator.exists())
200+
if (validator.exists() && validator.isDirectory())
201201
fileChooser.setInitialDirectory(validator);
202202
else
203203
fileChooser.setInitialDirectory(new File(System.getProperty("user.home")));
@@ -227,7 +227,7 @@ private void selectSplitBtnAction(){
227227
dirChooser.setTitle(resourceBundle.getString("btn_OpenFile"));
228228

229229
File validator = new File(previouslyOpenedPath);
230-
if (validator.exists())
230+
if (validator.exists() && validator.isDirectory())
231231
dirChooser.setInitialDirectory(validator);
232232
else
233233
dirChooser.setInitialDirectory(new File(System.getProperty("user.home")));

src/main/java/nsusbloader/Controllers/NSTableViewController.java

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@
2424
import javafx.collections.ObservableList;
2525
import javafx.fxml.FXML;
2626
import javafx.fxml.Initializable;
27+
import javafx.scene.SnapshotParameters;
2728
import javafx.scene.control.*;
2829
import javafx.scene.control.cell.CheckBoxTableCell;
2930
import javafx.scene.control.cell.PropertyValueFactory;
30-
import javafx.scene.input.KeyCode;
31-
import javafx.scene.input.MouseButton;
31+
import javafx.scene.input.*;
32+
import javafx.scene.paint.Paint;
3233
import nsusbloader.MediatorControl;
3334
import nsusbloader.NSLDataTypes.EFileStatus;
3435

@@ -39,6 +40,8 @@
3940
import java.util.ResourceBundle;
4041

4142
public class NSTableViewController implements Initializable {
43+
private static final DataFormat SERIALIZED_MIME_TYPE = new DataFormat("application/x-java-serialized-object");
44+
4245
@FXML
4346
private TableView<NSLRowModel> table;
4447
private ObservableList<NSLRowModel> rowsObsLst;
@@ -124,6 +127,51 @@ protected void updateItem(Long length, boolean empty) {
124127
table.setRowFactory( // this shit is made to implement context menu. It's such a pain..
125128
nslRowModelTableView -> {
126129
final TableRow<NSLRowModel> row = new TableRow<>();
130+
/*
131+
// Add ability to move rows by mouse hold and move
132+
row.setOnDragDetected(mouseEvent -> {
133+
if (! row.isEmpty()){
134+
Integer rowIndex = row.getIndex();
135+
Dragboard dragboard = row.startDragAndDrop(TransferMode.MOVE);
136+
dragboard.setDragView(row.snapshot(null, null));
137+
ClipboardContent cContent = new ClipboardContent();
138+
cContent.put(SERIALIZED_MIME_TYPE, rowIndex);
139+
dragboard.setContent(cContent);
140+
mouseEvent.consume();
141+
}
142+
});
143+
144+
row.setOnDragOver(dragEvent -> {
145+
Dragboard dragboard = dragEvent.getDragboard();
146+
if (! dragboard.hasContent(SERIALIZED_MIME_TYPE))
147+
return;
148+
if (row.getIndex() != (Integer) dragboard.getContent(SERIALIZED_MIME_TYPE)) {
149+
dragEvent.acceptTransferModes(TransferMode.COPY_OR_MOVE);
150+
dragEvent.consume();
151+
}
152+
});
153+
154+
row.setOnDragDropped(dragEvent -> {
155+
Dragboard dragboard = dragEvent.getDragboard();
156+
if (! dragboard.hasContent(SERIALIZED_MIME_TYPE))
157+
return;
158+
int draggedRowIndex = (Integer) dragboard.getContent(SERIALIZED_MIME_TYPE);
159+
NSLRowModel draggedRow = table.getItems().remove(draggedRowIndex);
160+
161+
int dropIndex;
162+
163+
if (row.isEmpty())
164+
dropIndex = table.getItems().size();
165+
else
166+
dropIndex = row.getIndex();
167+
168+
table.getItems().add(dropIndex, draggedRow);
169+
dragEvent.setDropCompleted(true);
170+
table.getSelectionModel().select(dropIndex);
171+
dragEvent.consume();
172+
});
173+
//*/
174+
127175
ContextMenu contextMenu = new ContextMenu();
128176
MenuItem deleteMenuItem = new MenuItem(resourceBundle.getString("tab1_table_contextMenu_Btn_BtnDelete"));
129177
deleteMenuItem.setOnAction(actionEvent -> {
@@ -254,7 +302,10 @@ public void setNewProtocol(String newProtocol){
254302
current.getNspFileName().toLowerCase().endsWith("xcz"));
255303
}
256304
else
257-
rowsObsLst.removeIf(current -> ! current.getNspFileName().toLowerCase().endsWith("nsp"));
305+
rowsObsLst.removeIf(current -> (! current.getNspFileName().toLowerCase().endsWith("nsp")) ||
306+
(! current.getNspFileName().toLowerCase().endsWith("nsz")) ||
307+
(! current.getNspFileName().toLowerCase().endsWith("xci")) ||
308+
(! current.getNspFileName().toLowerCase().endsWith("xcz")));
258309
table.refresh();
259310
}
260311
/**

0 commit comments

Comments
 (0)