From 1171c1820445492e384f0d9debc3e4ed1f381f1e Mon Sep 17 00:00:00 2001 From: tobias <tobias.ullerich@icloud.com> Date: Sun, 17 Jul 2016 21:49:40 +0200 Subject: [PATCH] Code refractoring --- .../playpad/viewcontroller/LaunchDialog.java | 83 ++++++++++--------- .../viewcontroller/cell/ProjectCell.java | 18 ++-- .../dialog/MappingListViewController.java | 6 ++ .../{ => dialog}/PluginViewController.java | 2 +- .../{ => dialog}/PrintDialog.java | 2 +- .../main/MainMenuBarController.java | 4 +- 6 files changed, 60 insertions(+), 55 deletions(-) rename PlayWall/src/de/tobias/playpad/viewcontroller/{ => dialog}/PluginViewController.java (98%) rename PlayWall/src/de/tobias/playpad/viewcontroller/{ => dialog}/PrintDialog.java (98%) diff --git a/PlayWall/src/de/tobias/playpad/viewcontroller/LaunchDialog.java b/PlayWall/src/de/tobias/playpad/viewcontroller/LaunchDialog.java index 5f8e136b..44603daf 100644 --- a/PlayWall/src/de/tobias/playpad/viewcontroller/LaunchDialog.java +++ b/PlayWall/src/de/tobias/playpad/viewcontroller/LaunchDialog.java @@ -1,8 +1,11 @@ package de.tobias.playpad.viewcontroller; import java.io.File; +import java.io.IOException; import java.nio.file.Path; +import org.dom4j.DocumentException; + import de.tobias.playpad.PlayPadMain; import de.tobias.playpad.Strings; import de.tobias.playpad.project.ProfileChooseable; @@ -19,7 +22,7 @@ import de.tobias.playpad.viewcontroller.dialog.ProfileChooseDialog; import de.tobias.utils.application.App; import de.tobias.utils.application.ApplicationUtils; import de.tobias.utils.ui.ViewController; -import de.tobias.utils.util.Localization; +import static de.tobias.utils.util.Localization.getString; import de.tobias.utils.util.Worker; import javafx.event.ActionEvent; import javafx.fxml.FXML; @@ -59,7 +62,9 @@ public class LaunchDialog extends ViewController implements ProfileChooseable { @Override public void init() { App app = ApplicationUtils.getApplication(); - infoLabel.setText(Localization.getString(Strings.UI_Dialog_Launch_Info, app.getInfo().getName(), app.getInfo().getVersion())); + + // Setup launchscreen labels and image + infoLabel.setText(getString(Strings.UI_Dialog_Launch_Info, app.getInfo().getName(), app.getInfo().getVersion())); try { imageView.setImage(new Image(IMAGE)); } catch (Exception e) { @@ -69,10 +74,12 @@ public class LaunchDialog extends ViewController implements ProfileChooseable { openButton.setDisable(true); deleteButton.setDisable(true); - projectListView.setPlaceholder(new Label(Localization.getString(Strings.UI_Placeholder_Project))); + // Load project to list + projectListView.setPlaceholder(new Label(getString(Strings.UI_Placeholder_Project))); projectListView.setId("list"); projectListView.setCellFactory(list -> new ProjectCell()); + // List selection listener projectListView.getSelectionModel().selectedItemProperty().addListener((a, b, c) -> { openButton.setDisable(c == null); @@ -85,8 +92,7 @@ public class LaunchDialog extends ViewController implements ProfileChooseable { if (mouseEvent.getButton().equals(MouseButton.PRIMARY)) { if (mouseEvent.getClickCount() == 2) { if (!projectListView.getSelectionModel().isEmpty()) { - ProjectReference ref = projectListView.getSelectionModel().getSelectedItem(); - launchProject(ref); + launchProject(getSelectedProject()); } } } @@ -98,7 +104,7 @@ public class LaunchDialog extends ViewController implements ProfileChooseable { setCSS("style.css", "de/tobias/playpad/assets/"); setCSS("launchDialog_style.css", "de/tobias/playpad/assets/style/"); - stage.setTitle(Localization.getString(Strings.UI_Dialog_Launch_Title)); + stage.setTitle(getString(Strings.UI_Dialog_Launch_Title)); PlayPadMain.stageIcon.ifPresent(stage.getIcons()::add); stage.setOnCloseRequest(e -> @@ -126,19 +132,18 @@ public class LaunchDialog extends ViewController implements ProfileChooseable { @FXML private void importProfileButtonHandler(ActionEvent event) { FileChooser chooser = new FileChooser(); - chooser.getExtensionFilters() - .add(new ExtensionFilter(Localization.getString(Strings.File_Filter_ZIP), PlayPadMain.projectZIPType)); + chooser.getExtensionFilters().add(new ExtensionFilter(getString(Strings.File_Filter_ZIP), PlayPadMain.projectZIPType)); File file = chooser.showOpenDialog(getStage()); if (file != null) { Path zipFile = file.toPath(); try { - ProjectReference ref = ProjectImporter.importProject(zipFile, ImportDialog.getInstance(getStage()), - ImportDialog.getInstance(getStage())); + ImportDialog importDialog = ImportDialog.getInstance(getStage()); + ProjectReference ref = ProjectImporter.importProject(zipFile, importDialog, importDialog); if (ref != null) { launchProject(ref); } - } catch (Exception e) { - showErrorMessage(Localization.getString(Strings.Error_Project_Open, e.getLocalizedMessage())); + } catch (DocumentException | IOException e) { + showErrorMessage(getString(Strings.Error_Project_Open, e.getLocalizedMessage())); e.printStackTrace(); } } @@ -146,36 +151,16 @@ public class LaunchDialog extends ViewController implements ProfileChooseable { @FXML private void openButtonHandler(ActionEvent event) { - ProjectReference ref = projectListView.getSelectionModel().getSelectedItem(); - try { - if (ref != null) { - Project project = Project.load(ref, true, this); - PlayPadMain.launchProject(project); - getStage().close(); - } - } catch (ProfileNotFoundException e) { - e.printStackTrace(); - showErrorMessage(Localization.getString(Strings.Error_Profile_NotFound, ref.getProfileReference(), e.getLocalizedMessage())); - - // Neues Profile wählen - Profile profile = getUnkownProfile(); - ref.setProfileReference(profile.getRef()); - } catch (ProjectNotFoundException e) { - e.printStackTrace(); - showErrorMessage(Localization.getString(Strings.Error_Project_NotFound, ref, e.getLocalizedMessage())); - } catch (Exception e) { - e.printStackTrace(); - showErrorMessage(Localization.getString(Strings.Error_Project_Open, ref, e.getLocalizedMessage())); - } + launchProject(getSelectedProject()); } @FXML private void deleteButtonHandler(ActionEvent event) { - if (projectListView.getSelectionModel().getSelectedItem() != null) { - ProjectReference ref = projectListView.getSelectionModel().getSelectedItem(); + if (getSelectedProject() != null) { + ProjectReference ref = getSelectedProject(); Alert alert = new Alert(AlertType.CONFIRMATION); - alert.setContentText(Localization.getString(Strings.UI_Dialog_ProjectManager_Delete_Content, ref)); + alert.setContentText(getString(Strings.UI_Dialog_ProjectManager_Delete_Content, ref)); Stage dialog = (Stage) alert.getDialogPane().getScene().getWindow(); PlayPadMain.stageIcon.ifPresent(dialog.getIcons()::add); @@ -187,13 +172,28 @@ public class LaunchDialog extends ViewController implements ProfileChooseable { try { ProjectReference.removeDocument(ref); projectListView.getItems().remove(ref); // VIEW - } catch (Exception e) { - showErrorMessage(Localization.getString(Strings.Error_Project_Delete, e.getLocalizedMessage())); + } catch (DocumentException | IOException e) { + showErrorMessage(getString(Strings.Error_Project_Delete, e.getLocalizedMessage())); } }); } } + /** + * Gibt das ausgewählte Projekt zurück. + * + * @return Projekt + */ + private ProjectReference getSelectedProject() { + return projectListView.getSelectionModel().getSelectedItem(); + } + + /** + * Launch a project and close this view. + * + * @param ref + * Project to launch + */ private void launchProject(ProjectReference ref) { try { Project project = Project.load(ref, true, this); @@ -201,20 +201,21 @@ public class LaunchDialog extends ViewController implements ProfileChooseable { getStage().close(); } catch (ProfileNotFoundException e) { e.printStackTrace(); - showErrorMessage(Localization.getString(Strings.Error_Profile_NotFound, ref.getProfileReference(), e.getLocalizedMessage())); + showErrorMessage(getString(Strings.Error_Profile_NotFound, ref.getProfileReference(), e.getLocalizedMessage())); // Neues Profile wählen Profile profile = getUnkownProfile(); ref.setProfileReference(profile.getRef()); } catch (ProjectNotFoundException e) { e.printStackTrace(); - showErrorMessage(Localization.getString(Strings.Error_Project_NotFound, ref, e.getLocalizedMessage())); + showErrorMessage(getString(Strings.Error_Project_NotFound, ref, e.getLocalizedMessage())); } catch (Exception e) { e.printStackTrace(); - showErrorMessage(Localization.getString(Strings.Error_Project_Open, ref, e.getLocalizedMessage())); + showErrorMessage(getString(Strings.Error_Project_Open, ref, e.getLocalizedMessage())); } } + // Zeigt dialog für das Ausfählen eines neuen Profiles. @Override public Profile getUnkownProfile() { ProfileChooseDialog dialog = new ProfileChooseDialog(getStage()); diff --git a/PlayWall/src/de/tobias/playpad/viewcontroller/cell/ProjectCell.java b/PlayWall/src/de/tobias/playpad/viewcontroller/cell/ProjectCell.java index 5ee0a1ee..57d9105b 100644 --- a/PlayWall/src/de/tobias/playpad/viewcontroller/cell/ProjectCell.java +++ b/PlayWall/src/de/tobias/playpad/viewcontroller/cell/ProjectCell.java @@ -5,8 +5,6 @@ import java.nio.file.Path; import de.tobias.playpad.Displayable; import de.tobias.playpad.project.ProjectReference; -import de.tobias.utils.application.ApplicationUtils; -import de.tobias.utils.application.container.PathType; import de.tobias.utils.ui.icon.FontAwesomeType; import de.tobias.utils.ui.icon.FontIcon; import javafx.scene.control.ContentDisplay; @@ -15,14 +13,14 @@ import javafx.scene.paint.Color; public class ProjectCell extends ListCell<ProjectReference> { - private Displayable action; + private Displayable ref; @Override - protected void updateItem(ProjectReference action, boolean empty) { - super.updateItem(action, empty); + protected void updateItem(ProjectReference ref, boolean empty) { + super.updateItem(ref, empty); if (!empty) { - if (this.action == null || this.action != action) { - Path path = ApplicationUtils.getApplication().getPath(PathType.DOCUMENTS, action.getFileName()); + if (this.ref == null || this.ref != ref) { + Path path = ref.getProjectPath(); if (Files.notExists(path)) { FontIcon graphics = new FontIcon(FontAwesomeType.WARNING); graphics.setColor(Color.RED); @@ -30,11 +28,11 @@ public class ProjectCell extends ListCell<ProjectReference> { } setContentDisplay(ContentDisplay.RIGHT); - textProperty().bind(action.displayProperty()); - this.action = action; + textProperty().bind(ref.displayProperty()); + this.ref = ref; } } else { - this.action = null; + this.ref = null; textProperty().unbind(); setGraphic(null); diff --git a/PlayWall/src/de/tobias/playpad/viewcontroller/dialog/MappingListViewController.java b/PlayWall/src/de/tobias/playpad/viewcontroller/dialog/MappingListViewController.java index e82fd677..b59e10fc 100644 --- a/PlayWall/src/de/tobias/playpad/viewcontroller/dialog/MappingListViewController.java +++ b/PlayWall/src/de/tobias/playpad/viewcontroller/dialog/MappingListViewController.java @@ -26,6 +26,12 @@ import javafx.stage.Modality; import javafx.stage.Stage; import javafx.stage.Window; +/** + * Dieser ViewController listet alle Mappings auf. Man kann diese importieren, exportieren und aktivieren/deaktivieren. + * + * @author tobias + * + */ public class MappingListViewController extends ViewController { @FXML private ComboBox<Mapping> presetsListView; diff --git a/PlayWall/src/de/tobias/playpad/viewcontroller/PluginViewController.java b/PlayWall/src/de/tobias/playpad/viewcontroller/dialog/PluginViewController.java similarity index 98% rename from PlayWall/src/de/tobias/playpad/viewcontroller/PluginViewController.java rename to PlayWall/src/de/tobias/playpad/viewcontroller/dialog/PluginViewController.java index 7b016bad..e1718119 100644 --- a/PlayWall/src/de/tobias/playpad/viewcontroller/PluginViewController.java +++ b/PlayWall/src/de/tobias/playpad/viewcontroller/dialog/PluginViewController.java @@ -1,4 +1,4 @@ -package de.tobias.playpad.viewcontroller; +package de.tobias.playpad.viewcontroller.dialog; import java.io.IOException; import java.util.Collections; diff --git a/PlayWall/src/de/tobias/playpad/viewcontroller/PrintDialog.java b/PlayWall/src/de/tobias/playpad/viewcontroller/dialog/PrintDialog.java similarity index 98% rename from PlayWall/src/de/tobias/playpad/viewcontroller/PrintDialog.java rename to PlayWall/src/de/tobias/playpad/viewcontroller/dialog/PrintDialog.java index 4b88bd8e..25185126 100644 --- a/PlayWall/src/de/tobias/playpad/viewcontroller/PrintDialog.java +++ b/PlayWall/src/de/tobias/playpad/viewcontroller/dialog/PrintDialog.java @@ -1,4 +1,4 @@ -package de.tobias.playpad.viewcontroller; +package de.tobias.playpad.viewcontroller.dialog; import com.hp.gagawa.java.elements.Body; import com.hp.gagawa.java.elements.Div; diff --git a/PlayWall/src/de/tobias/playpad/viewcontroller/main/MainMenuBarController.java b/PlayWall/src/de/tobias/playpad/viewcontroller/main/MainMenuBarController.java index 53057225..56f9b0b0 100644 --- a/PlayWall/src/de/tobias/playpad/viewcontroller/main/MainMenuBarController.java +++ b/PlayWall/src/de/tobias/playpad/viewcontroller/main/MainMenuBarController.java @@ -24,12 +24,12 @@ import de.tobias.playpad.settings.Profile; import de.tobias.playpad.settings.ProfileListener; import de.tobias.playpad.settings.ProfileNotFoundException; import de.tobias.playpad.settings.ProfileSettings; -import de.tobias.playpad.viewcontroller.PluginViewController; -import de.tobias.playpad.viewcontroller.PrintDialog; import de.tobias.playpad.viewcontroller.SettingsTabViewController; import de.tobias.playpad.viewcontroller.dialog.ErrorSummaryDialog; import de.tobias.playpad.viewcontroller.dialog.ImportDialog; import de.tobias.playpad.viewcontroller.dialog.NewProjectDialog; +import de.tobias.playpad.viewcontroller.dialog.PluginViewController; +import de.tobias.playpad.viewcontroller.dialog.PrintDialog; import de.tobias.playpad.viewcontroller.dialog.ProfileViewController; import de.tobias.playpad.viewcontroller.dialog.ProjectManagerDialog; import de.tobias.playpad.viewcontroller.option.SettingsViewController; -- GitLab