Skip to content

Commit

Permalink
Create KeysDialogFactory.
Browse files Browse the repository at this point in the history
  • Loading branch information
DolphFlynn committed Oct 5, 2024
1 parent e56fa45 commit dfb16c7
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 45 deletions.
54 changes: 11 additions & 43 deletions src/main/java/com/blackberry/jwteditor/presenter/KeysPresenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@
import com.blackberry.jwteditor.model.persistence.KeysModelPersistence;
import com.blackberry.jwteditor.utils.PEMUtils;
import com.blackberry.jwteditor.utils.Utils;
import com.blackberry.jwteditor.view.dialog.keys.AsymmetricKeyDialogFactory;
import com.blackberry.jwteditor.view.dialog.keys.KeyDialog;
import com.blackberry.jwteditor.view.dialog.keys.PasswordDialog;
import com.blackberry.jwteditor.view.dialog.keys.SymmetricKeyDialog;
import com.blackberry.jwteditor.view.dialog.keys.KeysDialogFactory;
import com.blackberry.jwteditor.view.keys.KeysView;
import com.blackberry.jwteditor.view.rsta.RstaFactory;
import com.nimbusds.jose.jwk.*;
import com.nimbusds.jose.jwk.JWK;

import javax.swing.*;
import java.util.List;
Expand All @@ -46,25 +43,15 @@ public class KeysPresenter {

private final KeysModel model;
private final KeysView view;
private final RstaFactory rstaFactory;
private final AsymmetricKeyDialogFactory asymmetricKeyDialogFactory;
private final KeysDialogFactory keysDialogFactory;

/**
* Create a new KeysPresenter
*
* @param view the KeysView to associate with the presenter
* @param keysModelPersistence class used to persist keys model
* @param keysModel KeysModel to use (or null to create a new one)
* @param rstaFactory Factory to create RSyntaxTextArea
*/
public KeysPresenter(KeysView view,
KeysModelPersistence keysModelPersistence,
KeysModel keysModel,
RstaFactory rstaFactory) {
KeysDialogFactory keysDialogFactory) {
this.view = view;
this.rstaFactory = rstaFactory;
this.model = keysModel;
this.asymmetricKeyDialogFactory = new AsymmetricKeyDialogFactory(view.getParent(), model, rstaFactory);
this.keysDialogFactory = keysDialogFactory;

model.addKeyModelListener(new KeysModelListener() {
@Override
Expand Down Expand Up @@ -94,7 +81,7 @@ public void notifyKeyDeleted(int rowIndex) {
*/
public void onTableKeysDoubleClick() {
Key key = model.getKey(view.getSelectedRow());
KeyDialog dialog = buildKeyDialog(key);
KeyDialog dialog = keysDialogFactory.dialogFor(key);

if (dialog == null) {
return;
Expand All @@ -111,25 +98,6 @@ public void onTableKeysDoubleClick() {
}
}

private KeyDialog buildKeyDialog(Key key) {
if (key instanceof JWKKey jwkKey) {
return switch (jwkKey.getJWK()) {
case RSAKey rsaKey -> asymmetricKeyDialogFactory.rsaKeyDialog(rsaKey);
case ECKey ecKey -> asymmetricKeyDialogFactory.ecKeyDialog(ecKey);
case OctetKeyPair octetKeyPair -> asymmetricKeyDialogFactory.okpDialog(octetKeyPair);
case OctetSequenceKey octetSequenceKey -> new SymmetricKeyDialog(view.getParent(), model, rstaFactory, octetSequenceKey);
default -> null;
};
}

if (key instanceof PasswordKey passwordKey) {
return new PasswordDialog(view.getParent(), model, passwordKey);
}

return null;
}


private void onButtonNewClicked(KeyDialog d) {
d.display();

Expand All @@ -143,35 +111,35 @@ private void onButtonNewClicked(KeyDialog d) {
* Handler for button clicks for new symmetric keys
*/
public void onButtonNewSymmetricClick() {
onButtonNewClicked(new SymmetricKeyDialog(view.getParent(), model, rstaFactory, null));
onButtonNewClicked(keysDialogFactory.symmetricKeyDialog());
}

/**
* Handler for button clicks for new RSA keys
*/
public void onButtonNewRSAClick() {
onButtonNewClicked(asymmetricKeyDialogFactory.rsaKeyDialog());
onButtonNewClicked(keysDialogFactory.rsaKeyDialog());
}

/**
* Handler for button clicks for new EC keys
*/
public void onButtonNewECClick() {
onButtonNewClicked(asymmetricKeyDialogFactory.ecKeyDialog());
onButtonNewClicked(keysDialogFactory.ecKeyDialog());
}

/**
* Handler for button clicks for new OKPs
*/
public void onButtonNewOKPClick() {
onButtonNewClicked(asymmetricKeyDialogFactory.okpDialog());
onButtonNewClicked(keysDialogFactory.okpDialog());
}

/**
* Handler for button clicks for new passwords
*/
public void onButtonNewPasswordClick() {
onButtonNewClicked(new PasswordDialog(view.getParent(), model));
onButtonNewClicked(keysDialogFactory.passwordDialog());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Author : Dolph Flynn
Copyright 2024 Dolph Flynn
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.blackberry.jwteditor.view.dialog.keys;

import com.blackberry.jwteditor.model.keys.JWKKey;
import com.blackberry.jwteditor.model.keys.Key;
import com.blackberry.jwteditor.model.keys.KeysModel;
import com.blackberry.jwteditor.model.keys.PasswordKey;
import com.blackberry.jwteditor.view.rsta.RstaFactory;
import com.nimbusds.jose.jwk.ECKey;
import com.nimbusds.jose.jwk.OctetKeyPair;
import com.nimbusds.jose.jwk.OctetSequenceKey;
import com.nimbusds.jose.jwk.RSAKey;

import java.awt.*;

public class KeysDialogFactory {
private final KeysModel model;
private final RstaFactory rstaFactory;
private final AsymmetricKeyDialogFactory asymmetricKeyDialogFactory;
private final Window window;

public KeysDialogFactory(KeysModel model, RstaFactory rstaFactory, Window window) {
this.model = model;
this.rstaFactory = rstaFactory;
this.window = window;
this.asymmetricKeyDialogFactory = new AsymmetricKeyDialogFactory(window, model, rstaFactory);
}

public KeyDialog dialogFor(Key key) {
if (key instanceof JWKKey jwkKey) {
return switch (jwkKey.getJWK()) {
case RSAKey rsaKey -> asymmetricKeyDialogFactory.rsaKeyDialog(rsaKey);
case ECKey ecKey -> asymmetricKeyDialogFactory.ecKeyDialog(ecKey);
case OctetKeyPair octetKeyPair -> asymmetricKeyDialogFactory.okpDialog(octetKeyPair);
case OctetSequenceKey octetSequenceKey -> new SymmetricKeyDialog(window, model, rstaFactory, octetSequenceKey);
default -> null;
};
}

if (key instanceof PasswordKey passwordKey) {
return new PasswordDialog(window, model, passwordKey);
}

return null;
}

public KeyDialog rsaKeyDialog() {
return asymmetricKeyDialogFactory.rsaKeyDialog();
}

public KeyDialog ecKeyDialog() {
return asymmetricKeyDialogFactory.ecKeyDialog();
}

public KeyDialog okpDialog() {
return asymmetricKeyDialogFactory.okpDialog();
}

public KeyDialog passwordDialog() {
return new PasswordDialog(window, model);
}

public KeyDialog symmetricKeyDialog() {
return new SymmetricKeyDialog(window, model, rstaFactory, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.blackberry.jwteditor.model.persistence.KeysModelPersistence;
import com.blackberry.jwteditor.presenter.KeysPresenter;
import com.blackberry.jwteditor.utils.Utils;
import com.blackberry.jwteditor.view.dialog.keys.KeysDialogFactory;
import com.blackberry.jwteditor.view.rsta.RstaFactory;
import com.blackberry.jwteditor.view.utils.AlternateRowBackgroundDecoratingTableCellRenderer;
import com.blackberry.jwteditor.view.utils.PercentageBasedColumnWidthTable;
Expand Down Expand Up @@ -72,12 +73,15 @@ public KeysView(
keysTableModel = new KeysTableModel(keysModel.keys());
tableKeys.setModel(keysTableModel);

// Initialise the presenter
presenter = new KeysPresenter(
this,
keysModelPersistence,
keysModel,
rstaFactory
new KeysDialogFactory(
keysModel,
rstaFactory,
parent
)
);

// Attach event handlers for button clicks
Expand Down

0 comments on commit dfb16c7

Please sign in to comment.