Ver código fonte

Refactorings

Sebastian Stenzel 10 anos atrás
pai
commit
b2be41e39b

+ 32 - 32
main/ui/src/main/java/org/cryptomator/ui/MainController.java

@@ -58,7 +58,7 @@ public class MainController implements Initializable, InitializationListener, Un
 	private Stage stage;
 
 	@FXML
-	private ContextMenu directoryContextMenu;
+	private ContextMenu vaultListCellContextMenu;
 
 	@FXML
 	private ContextMenu addVaultContextMenu;
@@ -67,7 +67,7 @@ public class MainController implements Initializable, InitializationListener, Un
 	private HBox rootPane;
 
 	@FXML
-	private ListView<Vault> directoryList;
+	private ListView<Vault> vaultList;
 
 	@FXML
 	private ToggleButton addVaultButton;
@@ -94,9 +94,9 @@ public class MainController implements Initializable, InitializationListener, Un
 		this.rb = rb;
 
 		final ObservableList<Vault> items = FXCollections.observableList(settings.getDirectories());
-		directoryList.setItems(items);
-		directoryList.setCellFactory(this::createDirecoryListCell);
-		directoryList.getSelectionModel().getSelectedItems().addListener(this::selectedDirectoryDidChange);
+		vaultList.setItems(items);
+		vaultList.setCellFactory(this::createDirecoryListCell);
+		vaultList.getSelectionModel().getSelectedItems().addListener(this::selectedVaultDidChange);
 	}
 
 	@FXML
@@ -167,55 +167,55 @@ public class MainController implements Initializable, InitializationListener, Un
 		}
 
 		final Vault vault = vaultFactoy.createVault(vaultPath);
-		if (!directoryList.getItems().contains(vault)) {
-			directoryList.getItems().add(vault);
+		if (!vaultList.getItems().contains(vault)) {
+			vaultList.getItems().add(vault);
 		}
-		directoryList.getSelectionModel().select(vault);
+		vaultList.getSelectionModel().select(vault);
 	}
 
 	private ListCell<Vault> createDirecoryListCell(ListView<Vault> param) {
 		final DirectoryListCell cell = new DirectoryListCell();
-		cell.setContextMenu(directoryContextMenu);
+		cell.setContextMenu(vaultListCellContextMenu);
 		return cell;
 	}
 
-	private void selectedDirectoryDidChange(ListChangeListener.Change<? extends Vault> change) {
-		final Vault selectedDir = directoryList.getSelectionModel().getSelectedItem();
-		if (selectedDir == null) {
+	private void selectedVaultDidChange(ListChangeListener.Change<? extends Vault> change) {
+		final Vault selectedVault = vaultList.getSelectionModel().getSelectedItem();
+		if (selectedVault == null) {
 			stage.setTitle(rb.getString("app.name"));
 			showWelcomeView();
-		} else if (!Files.isDirectory(selectedDir.getPath())) {
+		} else if (!Files.isDirectory(selectedVault.getPath())) {
 			Platform.runLater(() -> {
-				directoryList.getItems().remove(selectedDir);
-				directoryList.getSelectionModel().clearSelection();
+				vaultList.getItems().remove(selectedVault);
+				vaultList.getSelectionModel().clearSelection();
 			});
 			stage.setTitle(rb.getString("app.name"));
 			showWelcomeView();
 		} else {
-			stage.setTitle(selectedDir.getName());
-			showDirectory(selectedDir);
+			stage.setTitle(selectedVault.getName());
+			showVault(selectedVault);
 		}
 	}
 
 	@FXML
 	private void didClickRemoveSelectedEntry(ActionEvent e) {
-		final Vault selectedDir = directoryList.getSelectionModel().getSelectedItem();
-		directoryList.getItems().remove(selectedDir);
-		directoryList.getSelectionModel().clearSelection();
+		final Vault selectedDir = vaultList.getSelectionModel().getSelectedItem();
+		vaultList.getItems().remove(selectedDir);
+		vaultList.getSelectionModel().clearSelection();
 	}
 
 	// ****************************************
 	// Subcontroller for right panel
 	// ****************************************
 
-	private void showDirectory(Vault directory) {
+	private void showVault(Vault vault) {
 		try {
-			if (directory.isUnlocked()) {
-				this.showUnlockedView(directory);
-			} else if (directory.containsMasterKey()) {
-				this.showUnlockView(directory);
+			if (vault.isUnlocked()) {
+				this.showUnlockedView(vault);
+			} else if (vault.containsMasterKey()) {
+				this.showUnlockView(vault);
 			} else {
-				this.showInitializeView(directory);
+				this.showInitializeView(vault);
 			}
 		} catch (IOException e) {
 			LOG.error("Failed to analyze directory.", e);
@@ -252,25 +252,25 @@ public class MainController implements Initializable, InitializationListener, Un
 
 	private void showUnlockView(Vault directory) {
 		final UnlockController ctrl = showView("/fxml/unlock.fxml");
-		ctrl.setDirectory(directory);
+		ctrl.setVault(directory);
 		ctrl.setListener(this);
 	}
 
 	@Override
 	public void didUnlock(UnlockController ctrl) {
-		showUnlockedView(ctrl.getDirectory());
+		showUnlockedView(ctrl.getVault());
 		Platform.setImplicitExit(false);
 	}
 
-	private void showUnlockedView(Vault directory) {
+	private void showUnlockedView(Vault vault) {
 		final UnlockedController ctrl = showView("/fxml/unlocked.fxml");
-		ctrl.setDirectory(directory);
+		ctrl.setVault(vault);
 		ctrl.setListener(this);
 	}
 
 	@Override
 	public void didLock(UnlockedController ctrl) {
-		showUnlockView(ctrl.getDirectory());
+		showUnlockView(ctrl.getVault());
 		if (getUnlockedDirectories().isEmpty()) {
 			Platform.setImplicitExit(true);
 		}
@@ -279,7 +279,7 @@ public class MainController implements Initializable, InitializationListener, Un
 	/* Convenience */
 
 	public Collection<Vault> getDirectories() {
-		return directoryList.getItems();
+		return vaultList.getItems();
 	}
 
 	public Collection<Vault> getUnlockedDirectories() {

+ 4 - 2
main/ui/src/main/java/org/cryptomator/ui/MainModule.java

@@ -31,6 +31,7 @@ import org.cryptomator.webdav.WebDavServer;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.google.inject.AbstractModule;
 import com.google.inject.Injector;
+import com.google.inject.Provider;
 import com.google.inject.Provides;
 import com.google.inject.name.Names;
 
@@ -68,8 +69,9 @@ public class MainModule extends AbstractModule {
 	}
 
 	@Provides
-	VaultFactory getVaultFactory(WebDavServer server, Cryptor cryptor, WebDavMounter mounter, DeferredCloser closer) {
-		return new VaultFactory(server, cryptor, mounter, closer);
+	@Singleton
+	VaultFactory getVaultFactory(WebDavServer server, Provider<Cryptor> cryptorProvider, WebDavMounter mounter, DeferredCloser closer) {
+		return new VaultFactory(server, cryptorProvider, mounter, closer);
 	}
 
 	@Provides

+ 19 - 19
main/ui/src/main/java/org/cryptomator/ui/UnlockController.java

@@ -52,7 +52,7 @@ public class UnlockController implements Initializable {
 
 	private ResourceBundle rb;
 	private UnlockListener listener;
-	private Vault directory;
+	private Vault vault;
 
 	@FXML
 	private ComboBox<String> usernameBox;
@@ -108,23 +108,23 @@ public class UnlockController implements Initializable {
 		setControlsDisabled(true);
 		final String masterKeyFileName = usernameBox.getValue() + Aes256Cryptor.MASTERKEY_FILE_EXT;
 		final String masterKeyBackupFileName = masterKeyFileName + Aes256Cryptor.MASTERKEY_BACKUP_FILE_EXT;
-		final Path masterKeyPath = directory.getPath().resolve(masterKeyFileName);
-		final Path masterKeyBackupPath = directory.getPath().resolve(masterKeyBackupFileName);
+		final Path masterKeyPath = vault.getPath().resolve(masterKeyFileName);
+		final Path masterKeyBackupPath = vault.getPath().resolve(masterKeyBackupFileName);
 		final CharSequence password = passwordField.getCharacters();
 		InputStream masterKeyInputStream = null;
 		try {
 			progressIndicator.setVisible(true);
 			masterKeyInputStream = Files.newInputStream(masterKeyPath, StandardOpenOption.READ);
-			directory.getCryptor().decryptMasterKey(masterKeyInputStream, password);
-			if (!directory.startServer()) {
+			vault.getCryptor().decryptMasterKey(masterKeyInputStream, password);
+			if (!vault.startServer()) {
 				messageLabel.setText(rb.getString("unlock.messageLabel.startServerFailed"));
-				directory.getCryptor().swipeSensitiveData();
+				vault.getCryptor().swipeSensitiveData();
 				return;
 			}
 			// at this point we know for sure, that the masterkey can be decrypted, so lets make a backup:
 			Files.copy(masterKeyPath, masterKeyBackupPath, StandardCopyOption.REPLACE_EXISTING);
-			directory.setUnlocked(true);
-			final Future<Boolean> futureMount = exec.submit(() -> directory.mount());
+			vault.setUnlocked(true);
+			final Future<Boolean> futureMount = exec.submit(() -> vault.mount());
 			FXThreads.runOnMainThreadWhenFinished(exec, futureMount, this::didUnlockAndMount);
 			FXThreads.runOnMainThreadWhenFinished(exec, futureMount, (result) -> {
 				setControlsDisabled(false);
@@ -158,7 +158,7 @@ public class UnlockController implements Initializable {
 
 	private void findExistingUsernames() {
 		try {
-			DirectoryStream<Path> ds = MasterKeyFilter.filteredDirectory(directory.getPath());
+			DirectoryStream<Path> ds = MasterKeyFilter.filteredDirectory(vault.getPath());
 			final String masterKeyExt = Aes256Cryptor.MASTERKEY_FILE_EXT.toLowerCase();
 			usernameBox.getItems().clear();
 			for (final Path path : ds) {
@@ -171,7 +171,7 @@ public class UnlockController implements Initializable {
 				usernameBox.getSelectionModel().selectFirst();
 			}
 		} catch (IOException e) {
-			LOG.trace("Invalid path: " + directory.getPath(), e);
+			LOG.trace("Invalid path: " + vault.getPath(), e);
 		}
 	}
 
@@ -184,25 +184,25 @@ public class UnlockController implements Initializable {
 
 	private void didTypeMountName(ObservableValue<? extends String> property, String oldValue, String newValue) {
 		try {
-			directory.setMountName(newValue);
-			if (!newValue.equals(directory.getMountName())) {
-				mountName.setText(directory.getMountName());
+			vault.setMountName(newValue);
+			if (!newValue.equals(vault.getMountName())) {
+				mountName.setText(vault.getMountName());
 			}
 		} catch (IllegalArgumentException e) {
-			mountName.setText(directory.getMountName());
+			mountName.setText(vault.getMountName());
 		}
 	}
 
 	/* Getter/Setter */
 
-	public Vault getDirectory() {
-		return directory;
+	public Vault getVault() {
+		return vault;
 	}
 
-	public void setDirectory(Vault directory) {
-		this.directory = directory;
+	public void setVault(Vault vault) {
+		this.vault = vault;
 		this.findExistingUsernames();
-		this.mountName.setText(directory.getMountName());
+		this.mountName.setText(vault.getMountName());
 	}
 
 	public UnlockListener getListener() {

+ 9 - 17
main/ui/src/main/java/org/cryptomator/ui/UnlockedController.java

@@ -27,7 +27,6 @@ import javafx.util.Duration;
 
 import org.cryptomator.crypto.CryptorIOSampling;
 import org.cryptomator.ui.model.Vault;
-import org.cryptomator.webdav.WebDavServer;
 
 import com.google.inject.Inject;
 
@@ -35,9 +34,8 @@ public class UnlockedController implements Initializable {
 
 	private static final int IO_SAMPLING_STEPS = 100;
 	private static final double IO_SAMPLING_INTERVAL = 0.25;
-	private ResourceBundle rb;
 	private LockListener listener;
-	private Vault directory;
+	private Vault vault;
 	private Timeline ioAnimation;
 
 	@FXML
@@ -49,24 +47,20 @@ public class UnlockedController implements Initializable {
 	@FXML
 	private NumberAxis xAxis;
 
-	private final WebDavServer server;
-
 	@Inject
-	public UnlockedController(WebDavServer server) {
+	public UnlockedController() {
 		super();
-		this.server = server;
 	}
 
 	@Override
 	public void initialize(URL url, ResourceBundle rb) {
-		this.rb = rb;
 	}
 
 	@FXML
 	private void didClickCloseVault(ActionEvent event) {
-		directory.unmount();
-		directory.stopServer();
-		directory.setUnlocked(false);
+		vault.unmount();
+		vault.stopServer();
+		vault.setUnlocked(false);
 		if (listener != null) {
 			listener.didLock(this);
 		}
@@ -128,14 +122,12 @@ public class UnlockedController implements Initializable {
 
 	/* Getter/Setter */
 
-	public Vault getDirectory() {
-		return directory;
+	public Vault getVault() {
+		return vault;
 	}
 
-	public void setDirectory(Vault directory) {
-		this.directory = directory;
-		final String msg = String.format(rb.getString("unlocked.messageLabel.runningOnPort"), server.getPort());
-		messageLabel.setText(msg);
+	public void setVault(Vault directory) {
+		this.vault = directory;
 
 		if (directory.getCryptor() instanceof CryptorIOSampling) {
 			startIoSampling((CryptorIOSampling) directory.getCryptor());

+ 5 - 4
main/ui/src/main/java/org/cryptomator/ui/model/VaultFactory.java

@@ -8,24 +8,25 @@ import org.cryptomator.ui.util.mount.WebDavMounter;
 import org.cryptomator.webdav.WebDavServer;
 
 import com.google.inject.Inject;
+import com.google.inject.Provider;
 
 public class VaultFactory {
 
 	private final WebDavServer server;
-	private final Cryptor cryptor;
+	private final Provider<Cryptor> cryptorProvider;
 	private final WebDavMounter mounter;
 	private final DeferredCloser closer;
 
 	@Inject
-	public VaultFactory(WebDavServer server, Cryptor cryptor, WebDavMounter mounter, DeferredCloser closer) {
+	public VaultFactory(WebDavServer server, Provider<Cryptor> cryptorProvider, WebDavMounter mounter, DeferredCloser closer) {
 		this.server = server;
-		this.cryptor = cryptor;
+		this.cryptorProvider = cryptorProvider;
 		this.mounter = mounter;
 		this.closer = closer;
 	}
 
 	public Vault createVault(Path path) {
-		return new Vault(path, server, cryptor, mounter, closer);
+		return new Vault(path, server, cryptorProvider.get(), mounter, closer);
 	}
 
 }

+ 2 - 2
main/ui/src/main/resources/fxml/main.fxml

@@ -25,7 +25,7 @@
 
 	<fx:define>
 		<fx:include fx:id="welcomeView" source="welcome.fxml" />
-		<ContextMenu fx:id="directoryContextMenu">
+		<ContextMenu fx:id="vaultListCellContextMenu">
 			<items>
 				<MenuItem text="%main.directoryList.contextMenu.remove" onAction="#didClickRemoveSelectedEntry" />
 				<!-- TODO: -->
@@ -44,7 +44,7 @@
 	<children>
 		<VBox prefWidth="200.0">
 			<children>
-				<ListView fx:id="directoryList" VBox.vgrow="ALWAYS" focusTraversable="false" />
+				<ListView fx:id="vaultList" VBox.vgrow="ALWAYS" focusTraversable="false" />
 				<ToolBar VBox.vgrow="NEVER" styleClass="list-related-toolbar">
 					<items>
 						<ToggleButton text="+" fx:id="addVaultButton" onAction="#didClickAddVault" focusTraversable="false"/>

+ 4 - 7
main/ui/src/main/resources/fxml/unlocked.fxml

@@ -30,16 +30,13 @@
 
 	<children>
 		<!-- Row 0 -->
-		<Label fx:id="messageLabel" GridPane.rowIndex="0" GridPane.columnIndex="0" GridPane.columnSpan="2" />
-		
-		<!-- Row 1 -->
-		<Button text="%unlocked.button.lock" defaultButton="true" GridPane.rowIndex="1" GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.halignment="RIGHT" prefWidth="150.0" onAction="#didClickCloseVault" focusTraversable="false"/>
-		
-		<!-- Row 2 -->
-		<LineChart fx:id="ioGraph" GridPane.rowIndex="2" GridPane.columnIndex="0" GridPane.columnSpan="2" animated="false" createSymbols="false" prefHeight="300.0" legendVisible="true" legendSide="BOTTOM" verticalZeroLineVisible="false" verticalGridLinesVisible="false" horizontalGridLinesVisible="true">
+		<LineChart fx:id="ioGraph" GridPane.rowIndex="0" GridPane.columnIndex="0" GridPane.columnSpan="2" animated="false" createSymbols="false" prefHeight="340.0" legendVisible="true" legendSide="BOTTOM" verticalZeroLineVisible="false" verticalGridLinesVisible="false" horizontalGridLinesVisible="true">
 			<xAxis><NumberAxis fx:id="xAxis" forceZeroInRange="false" tickMarkVisible="false" minorTickVisible="false" tickLabelsVisible="false" autoRanging="false"/></xAxis>
         	<yAxis><NumberAxis label="%unlocked.ioGraph.yAxis.label" autoRanging="true" forceZeroInRange="true" /></yAxis>
 		</LineChart>
+		
+		<!-- Row 1 -->
+		<Button text="%unlocked.button.lock" defaultButton="true" GridPane.rowIndex="1" GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.halignment="RIGHT" prefWidth="150.0" onAction="#didClickCloseVault" focusTraversable="false"/>
 	</children>
 </GridPane>