Skip to content

Commit

Permalink
Adding ability to set document namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
yevster committed Sep 14, 2016
1 parent 88318a4 commit 655c867
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
39 changes: 36 additions & 3 deletions src/main/java/spdxedit/MainSceneController.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,14 @@ public class MainSceneController {
@FXML
private Button validateSpdx;

@FXML
private Button btnNewDocument;


@FXML
private ListView<SpdxPackage> addedPackagesUiList;

private SpdxDocument documentToEdit = SpdxLogic.createEmptyDocument("http://url.example.com/spdx/builder");
private SpdxDocument documentToEdit = null;

private static final Logger logger = LoggerFactory.getLogger(MainSceneController.class);

Expand Down Expand Up @@ -104,16 +108,27 @@ void initialize() {
assert dirTree != null : "fx:id=\"dirTree\" was not injected: check your FXML file 'MainScene.fxml'.";
assert chooseDir != null : "fx:id=\"chooseDir\" was not injected: check your FXML file 'MainScene.fxml'.";
assert btnAddPackage != null : "fx:id=\"btnAddPackage\" was not injected: check your FXML file 'MainScene.fxml'.";
assert btnNewDocument != null : "fx:id=\"btnNewDocument\" was not injected: check your FXML file 'MainScene.fxml'.";
assert saveSpdx != null : "fx:id=\"saveSpdx\" was not injected: check your FXML file 'MainScene.fxml'.";
assert saveSpdxTag != null : "fx:id=\"saveSpdxTag\" was not injected: check your FXML file 'MainScene.fxml'.";
assert validateSpdx != null : "fx:id=\"validateSpdx\" was not injected: check your FXML file 'MainScene.fxml'.";
assert txtDocumentName != null : "fx:id=\"txtDocumentName\" was not injected: check your FXML file 'MainScene.fxml'.";
assert addedPackagesUiList != null : "fx:id=\"addedPackagesUiList\" was not injected: check your FXML file 'MainScene.fxml'.";

txtDocumentName.textProperty().addListener((observable, oldValue, newValue) -> handleTxtDocumentNameChanged(newValue));
addedPackagesUiList.setCellFactory(param -> new SpdxPackageListCell());

}

private void enableAllButtons(){
this.btnAddPackage.setDisable(false);
this.validateSpdx.setDisable(false);
this.chooseDir.setDisable(false);
this.txtDocumentName.setDisable(false);
this.saveSpdx.setDisable(false);
this.saveSpdxTag.setDisable(false);
}

private static Optional<Path> selectDirectory(Window parentWindow) {
Objects.requireNonNull(parentWindow);
DirectoryChooser chooser = new DirectoryChooser();
Expand Down Expand Up @@ -142,6 +157,23 @@ private static TreeItem<Path> getTreeForPath(final Path base) throws IOException

}

public void handleNewDocumentClicked(MouseEvent event){
TextInputDialog dialog = new TextInputDialog();
((Stage)dialog.getDialogPane().getScene().getWindow()).getIcons().addAll(UiUtils.ICON_IMAGE_VIEW.getImage());
dialog.setTitle("New SPDX Document");
dialog.setHeaderText("Enter document namespace");
dialog.setResult("http://url.example.com/spdx/builder");
Optional<String> result = dialog.showAndWait();
while (result.isPresent() && !SpdxLogic.validateDocumentNamespace(result.orElse(""))){
dialog.setContentText(result.orElse("")+ "is not a valid document namespace. Please enter a valid document namespace.");
result = dialog.showAndWait();
}
if (result.isPresent()){
SpdxDocument newDocument = SpdxLogic.createEmptyDocument(result.get());
loadSpdxDocument(newDocument);
}

}

public void handleChooseDirectoryClicked(MouseEvent event) {
Optional<Path> chosenPath = selectDirectory(chooseDir.getParent().getScene().getWindow());
Expand All @@ -155,7 +187,7 @@ public void handleChooseDirectoryClicked(MouseEvent event) {


public void handleSaveSpdxClicked(MouseEvent event) {
File targetFile = getSpdxFileChooser("spdx", "rdf").showSaveDialog(saveSpdx.getScene().getWindow());
File targetFile = getSpdxFileChooser("rdf", "spdx").showSaveDialog(saveSpdx.getScene().getWindow());
if (targetFile == null) //Dialog cancelled
return;
try (FileWriter writer = new FileWriter(targetFile)) {
Expand Down Expand Up @@ -198,10 +230,11 @@ private void loadSpdxDocument(SpdxDocument loadedDocument) {
this.documentToEdit = loadedDocument;
this.addedPackagesUiList.getItems().setAll(SpdxLogic.getSpdxPackagesInDocument(loadedDocument).collect(Collectors.toList()));
this.txtDocumentName.setText(loadedDocument.getName());
enableAllButtons();
}

public void handleLoadSpdxClicked(MouseEvent event) {
File targetFile = getSpdxFileChooser("spdx", "rdf").showOpenDialog(saveSpdx.getScene().getWindow());
File targetFile = getSpdxFileChooser("rdf", "spdx").showOpenDialog(saveSpdx.getScene().getWindow());
if (targetFile == null) return; //Cancelled
try {
SpdxDocument loadedDocument = SPDXDocumentFactory.createSpdxDocument(targetFile.getPath());
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/spdxedit/SpdxLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -488,4 +488,17 @@ public static ReferenceType getReferenceType(String string){
throw new RuntimeException(e);
}
}
/**
* Verifies that the provided argument is a legal SPDX document namespace.
* @return true if, and only, if the argument is a valid SPDX document namespace.
*/
public static boolean validateDocumentNamespace(String namespace){
try {
return StringUtils.isNotBlank(namespace)
&& !StringUtils.contains(namespace, "#")
&& (new URI(namespace) != null);
} catch (URISyntaxException e){
return false;
}
}
}
17 changes: 9 additions & 8 deletions src/main/resources/MainScene.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="473.0" prefWidth="180.0">
<children>
<Button fx:id="chooseDir" layoutX="14.0" layoutY="80.0" mnemonicParsing="false" onMouseClicked="#handleChooseDirectoryClicked" prefWidth="145" text="Choose Directory..." />
<Button fx:id="btnAddPackage" layoutX="14.0" layoutY="130.0" mnemonicParsing="false" onMouseClicked="#handleAddPackageClicked" prefHeight="21.0" prefWidth="145.0" text="Add Package" />
<Button fx:id="validateSpdx" layoutX="14.0" layoutY="180.0" mnemonicParsing="false" onMouseClicked="#handleValidateSpdxClicked" prefWidth="145" text="Validate SPDX..." />
<Button fx:id="saveSpdx" layoutX="14.0" layoutY="230.0" mnemonicParsing="false" onMouseClicked="#handleSaveSpdxClicked" prefWidth="145" text="Write SPDX (RDF)..." />
<Button fx:id="loadSpdx" layoutX="14.0" layoutY="280.0" mnemonicParsing="false" onMouseClicked="#handleLoadSpdxClicked" prefWidth="145" text="Load SPDX (RDF)..." />
<Button fx:id="saveSpdxTag" layoutX="14.0" layoutY="330.0" mnemonicParsing="false" onMouseClicked="#handleSaveTagClicked" prefWidth="145" text="Write SPDX (Tag)..." />
<Button fx:id="loadSpdxTag" layoutX="14.0" layoutY="380.0" mnemonicParsing="false" onMouseClicked="#handleLoadSpdxTagClicked" prefWidth="145" text="Load SPDX (Tag)..." />
<TextField fx:id="txtDocumentName" layoutX="14.0" layoutY="25.0" prefHeight="26.0" prefWidth="145.0" promptText="Document name" AnchorPane.leftAnchor="14.0" />
<Button fx:id="chooseDir" disable="true" layoutX="14.0" layoutY="104.0" mnemonicParsing="false" onMouseClicked="#handleChooseDirectoryClicked" prefWidth="145" text="Choose Directory..." />
<Button fx:id="btnAddPackage" disable="true" layoutX="14.0" layoutY="144.0" mnemonicParsing="false" onMouseClicked="#handleAddPackageClicked" prefHeight="21.0" prefWidth="145.0" text="Add Package" />
<Button fx:id="validateSpdx" disable="true" layoutX="14.0" layoutY="184.0" mnemonicParsing="false" onMouseClicked="#handleValidateSpdxClicked" prefWidth="145" text="Validate SPDX..." />
<Button fx:id="saveSpdx" disable="true" layoutX="14.0" layoutY="224.0" mnemonicParsing="false" onMouseClicked="#handleSaveSpdxClicked" prefWidth="145" text="Write SPDX (RDF)..." />
<Button fx:id="loadSpdx" layoutX="14.0" layoutY="264.0" mnemonicParsing="false" onMouseClicked="#handleLoadSpdxClicked" prefWidth="145" text="Load SPDX (RDF)..." />
<Button fx:id="saveSpdxTag" disable="true" layoutX="14.0" layoutY="304.0" mnemonicParsing="false" onMouseClicked="#handleSaveTagClicked" prefWidth="145" text="Write SPDX (Tag)..." />
<Button fx:id="loadSpdxTag" layoutX="14.0" layoutY="344.0" mnemonicParsing="false" onMouseClicked="#handleLoadSpdxTagClicked" prefWidth="145" text="Load SPDX (Tag)..." />
<TextField fx:id="txtDocumentName" disable="true" layoutX="14.0" layoutY="25.0" prefHeight="26.0" prefWidth="145.0" promptText="Document name" AnchorPane.leftAnchor="14.0" />
<Button fx:id="btnNewDocument" layoutX="14.0" layoutY="64.0" mnemonicParsing="false" onMouseClicked="#handleNewDocumentClicked" prefHeight="21.0" prefWidth="145.0" text="New Document..." />
</children>
</AnchorPane>
<AnchorPane prefHeight="200.0" prefWidth="200.0">
Expand Down

0 comments on commit 655c867

Please sign in to comment.