Skip to content

Commit

Permalink
Introduce EditorMode enum.
Browse files Browse the repository at this point in the history
  • Loading branch information
DolphFlynn committed Aug 3, 2024
1 parent 1ae4dea commit 1a21024
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.blackberry.jwteditor.utils.Utils;
import com.blackberry.jwteditor.view.dialog.MessageDialogFactory;
import com.blackberry.jwteditor.view.dialog.operations.*;
import com.blackberry.jwteditor.view.editor.EditorMode;
import com.blackberry.jwteditor.view.editor.EditorView;
import com.blackberry.jwteditor.view.utils.ErrorLoggingActionListenerFactory;
import com.nimbusds.jose.util.Base64URL;
Expand Down Expand Up @@ -349,8 +350,9 @@ public void onEncryptClicked() {

// If a JWE was created by the dialog, replace the contents of the editor and change to JWE mode
JWE jwe = encryptDialog.getJWE();

if (jwe != null) {
view.setJWEMode();
view.setMode(EditorMode.JWE);
setJWE(jwe);
}
}
Expand All @@ -374,7 +376,7 @@ public void onDecryptClicked() {

// If decryption was successful, set the contents of the editor to the decrypted JWS and set the editor mode to JWS
if (jws.isPresent()) {
view.setJWSMode();
view.setMode(EditorMode.JWS);
setJWS(jws.get());
} else {
messageDialogFactory.showWarningDialog("error_title_unable_to_decrypt", "error_decryption_all_keys_failed");
Expand Down Expand Up @@ -422,10 +424,10 @@ public void onSelectionChanged() {

// Change to JWE/JWS mode based on the newly selected JOSEObject
if (joseObject instanceof JWS) {
view.setJWSMode();
view.setMode(EditorMode.JWS);
setJWS((JWS) joseObject);
} else {
view.setJWEMode();
view.setMode(EditorMode.JWE);
setJWE((JWE) joseObject);
}

Expand All @@ -441,7 +443,7 @@ public void componentChanged() {
MutableJOSEObject mutableJoseObject = model.getJOSEObject(view.getSelectedJOSEObjectIndex());

//Serialize the text/hex entries to a JWS/JWE in compact form, depending on the editor mode
JOSEObject joseObject = view.getMode() == EditorView.TAB_JWS ? getJWS() : getJWE();
JOSEObject joseObject = view.getMode() == EditorMode.JWS ? getJWS() : getJWE();
//Update the JOSEObjectPair with the change
mutableJoseObject.setModified(joseObject);
//Highlight the serialized text as changed if it differs from the original, and the change wasn't triggered by onSelectionChanging
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/blackberry/jwteditor/view/editor/EditorMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
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.editor;

public enum EditorMode {
JWS, JWE
}
44 changes: 23 additions & 21 deletions src/main/java/com/blackberry/jwteditor/view/editor/EditorView.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@
* View class for the Editor tab
*/
public abstract class EditorView {
public static final int TAB_JWS = 0;
public static final int TAB_JWE = 1;

private static final int JWS_TAB_INDEX = 0;
private static final int JWE_TAB_INDEX = 1;
private static final int MAX_JOSE_OBJECT_STRING_LENGTH = 68;

final EditorPresenter presenter;
Expand All @@ -63,7 +62,7 @@ public abstract class EditorView {
private final HexCodeAreaFactory hexCodeAreaFactory;
private final boolean isProVersion;

private int mode;
private EditorMode mode;
private JTabbedPane tabbedPane;
private JComboBox<String> comboBoxJOSEObject;
private JButton buttonSign;
Expand Down Expand Up @@ -350,18 +349,25 @@ public int getSelectedJOSEObjectIndex() {
* Get the UI mode - JWS or JWE
* @return UI mode value
*/
public int getMode() {
public EditorMode getMode() {
return mode;
}

/**
* Set the UI to JWS mode
*/
public void setJWSMode() {
mode = TAB_JWS;
tabbedPane.setSelectedIndex(TAB_JWS);
tabbedPane.setEnabledAt(TAB_JWS, true);
tabbedPane.setEnabledAt(TAB_JWE, false);
public void setMode(EditorMode mode)
{
this.mode = mode;

if (mode == EditorMode.JWS) {
configureUIForJWS();
} else {
configureUIForJWE();
}
}

private void configureUIForJWS() {
tabbedPane.setSelectedIndex(JWS_TAB_INDEX);
tabbedPane.setEnabledAt(JWS_TAB_INDEX, true);
tabbedPane.setEnabledAt(JWE_TAB_INDEX, false);
buttonAttack.setEnabled(editable);
buttonSign.setEnabled(editable);
buttonVerify.setEnabled(true);
Expand All @@ -378,14 +384,10 @@ public void setJWSMode() {
codeAreaSignature.setEditationAllowed(editable ? ALLOWED : READ_ONLY);
}

/**
* Set the UI to JWE mode
*/
public void setJWEMode() {
mode = TAB_JWE;
tabbedPane.setSelectedIndex(TAB_JWE);
tabbedPane.setEnabledAt(TAB_JWS, false);
tabbedPane.setEnabledAt(TAB_JWE, true);
private void configureUIForJWE() {
tabbedPane.setSelectedIndex(JWE_TAB_INDEX);
tabbedPane.setEnabledAt(JWS_TAB_INDEX, false);
tabbedPane.setEnabledAt(JWE_TAB_INDEX, true);
buttonAttack.setEnabled(false);
buttonSign.setEnabled(false);
buttonVerify.setEnabled(false);
Expand Down

0 comments on commit 1a21024

Please sign in to comment.