Download this file

PswDialogView.java    1061 lines (873 with data), 35.9 kB

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
package cologne.eck.all_peas.gui;
/*
* Peafactory - Production of Password Encryption Archives
* Copyright (C) 2015 Axel von dem Bruch
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* See: http://www.gnu.org/licenses/gpl-2.0.html
* You should have received a copy of the GNU General Public License
* along with this library.
*/
/**
* View of the pea dialog to type the password.
*/
import java.awt.*;
import java.awt.event.*;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ResourceBundle;
import javax.accessibility.AccessibleContext;
import javax.imageio.ImageIO;
import javax.swing.*;
import settings.PeaSettings;
import cologne.eck.all_peas.control.PeaControl;
import cologne.eck.all_peas.data.Attachments;
import cologne.eck.all_peas.data.PeaProperties;
import cologne.eck.all_peas.files.FileTypePanel;
import cologne.eck.peafactory.crypto.CipherStuff;
import cologne.eck.peafactory.crypto.HashStuff;
import cologne.eck.peafactory.crypto.kdf.KeyDerivation;
import cologne.eck.tools.EntropyPool;
import cologne.eck.tools.KeyRandomCollector;
import cologne.eck.tools.MouseRandomCollector;
import cologne.eck.tools.Zeroizer;
/*
* View of NotesControl, CalendarControl, FileControl,
* ImageControl
*/
@SuppressWarnings("serial")
public class PswDialogView extends JDialog implements WindowListener,
ActionListener, KeyListener {
private static final Color plainPeaColor = new Color(222, 244, 224);//(242, 255, 243);//(229, 255, 231);
private static final Color cipherPeaColor = new Color(183, 229, 187);//(206, 229, 208);
private static final Color peaColor = new Color(218, 242, 220);//Farbton 125,Saettigung 10, Wert 95
//private static final Color errorColor = new Color(252, 194, 171);
/**
* The used font size in menus and dialogs
*/
private static int fontSize = 12;
// screen:
private static final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
private static final double screenWidth = screenSize.getWidth();
private static final double screenHeight = screenSize.getHeight();
// label above password field (if undefined: type password...)
private static JLabel pswLabel;
// password field
private static JPasswordField pswField;
// Button to start decryption
private static JButton okButton;
// used for notebook/image: displays all available, valid files to decrypt
// private static JPanel availableFilesPanel;
// button group for available files
// private static ButtonGroup fileGroup;
// indicates the type of the PEA (text, file, image...)
// private static String fileTypex = "";
// remember the selected files? -> store in path file
// only for file type
private static JCheckBox rememberCheck;
// static instance
private static PswDialogView dialog;
// display warnings and error messages
private static JLabel errorMessageLabel;
// status/progress
//private static JLabel statusLabel;
// thread while typing the password - still not used
private static WhileTypingThread t;
// pea image icon for frames
private static Image image;
// language support (set in PeaControl)
private static ResourceBundle languagesBundle;
// to check if decryption was startet by ok-button
private static boolean started = false;
// if started first time (no password, no content)
// can be set manually in menu
private static boolean initializing = false;
private PswDialogView() {
// start the EntropyPool:
EntropyPool.getInstance().updateThread(System.nanoTime());
languagesBundle = PeaProperties.getBundle();
// fileType = PeaProperties.getFileType();
dialog = this;
this.setTitle(PeaSettings.getJarFileName() );
//this.setBackground(peaColor);
image = IconManager.loadIcon("pea-lock.png",
PeaProperties.getBundle().getString("pea_with_keyhole"),
null,
PeaProperties.getBundle().getString("logo_of_pea") ).getImage();
this.setIconImage(image);
this.addWindowListener(this);
this.addMouseMotionListener(new MouseRandomCollector() );
JPanel topPanel = (JPanel) this.getContentPane();
topPanel.setBorder(PeaBorderFactory.createBorder(true));//threeBorder);
topPanel.addMouseMotionListener(new MouseRandomCollector() );
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
loadComponents(topPanel, true);
this.setLocation(100,30);
pack();
}
public final static PswDialogView getInstance() {
if (dialog == null) {
dialog = new PswDialogView();
} else {
//return null
}
return dialog;
}
public AccessibleContext getAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = new AccessiblePswDialogView();
}
return accessibleContext;
}
public final void displayErrorMessages(String topic, boolean error) {
setMessage("###" + topic + ":\n", error);
setMessage(CipherStuff.getErrorMessage(), error);
PswDialogView.clearPassword();
}
@Override
public void keyPressed(KeyEvent kpe) {
if (kpe.getKeyChar() == KeyEvent.VK_ENTER && initializing == false){
// ok button
okButton.doClick();
} else if (kpe.isControlDown()) {
if (errorMessageLabel.getText().equals(PeaProperties.getBundle().getString("password_failed"))){
errorMessageLabel.setText(" ");
}
if (kpe.getKeyCode() == KeyEvent.VK_T ) {
CharTable table = new CharTable(this, pswField,0);
table.setVisible(true);
} else if (kpe.getKeyCode() == KeyEvent.VK_K) {
Keyboard keyboard = new Keyboard(this, pswField);
keyboard.setVisible(true);
} else if (kpe.getKeyCode() == KeyEvent.VK_F1 ) {
CharTable table = new CharTable(this, pswField, 0);
table.setVisible(true);
}else if (kpe.getKeyCode() == KeyEvent.VK_F2 ) {
CharTable table = new CharTable(this, pswField, 1);
table.setVisible(true);
}else if (kpe.getKeyCode() == KeyEvent.VK_F3 ) {
CharTable table = new CharTable(this, pswField, 2);
table.setVisible(true);
}else if (kpe.getKeyCode() == KeyEvent.VK_F4 ) {
CharTable table = new CharTable(this, pswField, 3);
table.setVisible(true);
}else if (kpe.getKeyCode() == KeyEvent.VK_F5 ) {
CharTable table = new CharTable(this, pswField, 4);
table.setVisible(true);
}else if (kpe.getKeyCode() == KeyEvent.VK_F6 ) {
CharTable table = new CharTable(this, pswField, 5);
table.setVisible(true);
}else if (kpe.getKeyCode() == KeyEvent.VK_F7 ) {
CharTable table = new CharTable(this, pswField, 6);
table.setVisible(true);
}else if (kpe.getKeyCode() == KeyEvent.VK_F8 ) {
CharTable table = new CharTable(this, pswField, 7);
table.setVisible(true);
}else if (kpe.getKeyCode() == KeyEvent.VK_F9 ) {
CharTable table = new CharTable(this, pswField, 8);
table.setVisible(true);
}else if (kpe.getKeyCode() == KeyEvent.VK_F10 ) {
CharTable table = new CharTable(this, pswField, 9);
table.setVisible(true);
}else if (kpe.getKeyCode() == KeyEvent.VK_F11 ) {
CharTable table = new CharTable(this, pswField, 10);
table.setVisible(true);
}else if (kpe.getKeyCode() == KeyEvent.VK_F12 ) {
CharTable table = new CharTable(this, pswField, 11);
table.setVisible(true);
}
}
}
@Override
public void keyReleased(KeyEvent arg0) {}
@Override
public void keyTyped(KeyEvent arg0) {}
@Override
public void actionPerformed(ActionEvent ape) {
String command = ape.getActionCommand();
//System.out.println("command: " + command);
if(command.equals("open")){
//System.out.println("open files");
PeaControl.getDialog().openEncryptedFiles();
} else if (command.equals("init")){
//System.out.println("initialize");
// PeaProperties.setInsideJar(false);
initialize();
} else if (command.startsWith("FILE")){
String fileName = command.substring(4, command.length());
//System.out.println("selected file: " + fileName);
// use the file inside the jar archive (resources/text.lock)
/* if (fileName.equals(languagesBundle.getString("image_inside_jar_archive"))){
PeaProperties.setInsideJar(true);
} else {
PeaProperties.setInsideJar(false);
} */
PeaSettings.setExternalFilePath(fileName);
// PeaControl.setEncryptedFileName(fileName);
} else if (command.equals("quit")){
System.out.println("Exit");
System.exit(0);
} else if(command.equals("keyboard")){
// clear error messages from previous password errors
if (errorMessageLabel.getText().equals(PeaProperties.getBundle().getString("password_failed"))){
errorMessageLabel.setText(" ");
}
Keyboard keyboard = new Keyboard(this, pswField);
keyboard.setVisible(true);
//PeaSettings.getKeyboard().setVisible(true);
} else if (command.equals("charTable1")){
// clear error messages from previous password errors
if (errorMessageLabel.getText().equals(PeaProperties.getBundle().getString("password_failed"))){
errorMessageLabel.setText(" ");
}
CharTable table = new CharTable(this, pswField,0);
table.setVisible(true);
/* } else if (command.equals("selectAll")){
if (availableFilesPanel != null){
JCheckBox button = (JCheckBox) ape.getSource();
Component[] comps = availableFilesPanel.getComponents();
if (comps != null) {
int len = comps.length;
boolean selected = button.isSelected();
for (int i = 0; i < len; i++) {
if (comps[i] instanceof JCheckBox){
((JCheckBox)comps[i]).setSelected(selected);
}
}
}
} */
} else if (command.equals("ok")){
// reset error message
errorMessageLabel.setText("");
try { // always reset the cursor
this.setCursor(CursorManager.getWaitCursor());//Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
/* if (fileType.equals("text file") || (fileType.equals("image"))) {
Component[] checkBoxes = availableFilesPanel.getComponents();
// clear fileNames:
PeaControl.setAvailableFileNames(new ArrayList<String>());
ArrayList <String> newFileNames = new ArrayList<String>();
for (int i = 0; i < checkBoxes.length; i++) {
JCheckBox check = (JCheckBox) availableFilesPanel.getComponent(i);
if (check.isSelected()){
//System.out.println("ok selected file: " + check.getText());
newFileNames.add(check.getText());
//PeaControl.addAvailableFileName(check.getText());
}
}
// sort new list:
Collections.sort(newFileNames);
// set as avai��able file names:
PeaControl.setAvailableFileNames(newFileNames);
}*/
boolean rememberPath = false;
//if (fileType.equals("file") && rememberCheck != null) {
if (rememberCheck != null) {
if ( rememberCheck.isSelected() == true){
rememberPath = true;
}
}
PeaControl.getDialog().startExecution(rememberPath);
} finally {
// always reset the cursor:
this.setCursor(CursorManager.getDefaultCursor());
}
/* } else if (command.startsWith("lfs")){
System.out.println(command.substring(3, command.length()));
try {
UIManager.setLookAndFeel(command.substring(3, command.length()));
} catch (Exception e) {
System.err.println("Manually changing of Look&Feel failed for \n"
+ UIManager.getSystemLookAndFeelClassName() + "\n"
+ e.toString() + ", " + e.getMessage());
}
System.out.println(UIManager.getLookAndFeel().getName());
SwingUtilities.updateComponentTreeUI(this);
this.pack(); */
}
else {
System.err.println("Invalid command: " + command);
}
}
@Override
public void windowActivated(WindowEvent arg0) {}
@Override
public void windowClosed(WindowEvent arg0) { // dispose
this.dispose();
}
@Override
public void windowClosing(WindowEvent arg0) { // x
if (started == true) {
return;
} else {
// if attacker has access to memory but not to file
if (Attachments.getNonce() != null) {
Zeroizer.zero(Attachments.getNonce());
}
if (PeaControl.getDialog() != null) {
PeaControl.getDialog().clearSecretValues();
}
System.exit(0);
}
}
@Override
public void windowDeactivated(WindowEvent arg0) {}
@Override
public void windowDeiconified(WindowEvent arg0) {}
@Override
public void windowIconified(WindowEvent arg0) {}
@Override
public void windowOpened(WindowEvent arg0) {
// do some expensive computations while user tries to remember the password
t = this.new WhileTypingThread();
t.setPriority( Thread.MAX_PRIORITY );// 10
t.start();
if (initializing == false) {
pswField.requestFocus();
}
}
//=======================================
// Helper Functions
public final static void setUI(boolean encrypted) {
// UIManager.put("OptionPane.messageFont", new Font("System", Font.PLAIN, 14));
/* FontUIResource f = new FontUIResource(Font.SANS_SERIF, Font.PLAIN, fontSize );
java.util.Enumeration<Object> keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof javax.swing.plaf.FontUIResource) {
UIManager.put(key, f);
}
} */
/* if (UIManager.getSystemLookAndFeelClassName().contains("GTK")){
// do not set
} else { */
Color mainColor;
if (encrypted == true) {
mainColor = cipherPeaColor;
} else {
mainColor = plainPeaColor;
}
try {
// for backward compatibility: In Java 6 Nimbus package was renamed
UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
int len = infos.length;
for (int i = 0; i < len; i++) {
if ("Nimbus".equals(infos[i].getName())) {
UIManager.setLookAndFeel(infos[i].getClassName());
// Set the primary colors:
UIManager.put("control", mainColor);//peaColor);
UIManager.put("info", new Color(242,242,189));
UIManager.put("nimbusAlertYellow", new Color(255,220,35));
UIManager.put("nimbusBase", new Color(76, 127, 80));//new Color(255,255,255));// new Color(51, 102, 55));
UIManager.put("nimbusDisabledText", new Color(142,143,145));
UIManager.put("nimbusFocus",new Color(102, 127, 104));//Color.RED);// new Color(114, 229, 124));
UIManager.put("nimbusGreen", new Color(157, 178, 158));
UIManager.put("nimbusInfoBlue", new Color(33, 165, 44));
UIManager.put("nimbusLightBackground", new Color(255,255,255));
UIManager.put("nimbusOrange", new Color(191,98,4));//(148, 178, 0));
UIManager.put("nimbusRed", new Color(169,46,34));
UIManager.put("nimbusSelectedText", new Color(255,255,255));
UIManager.put("nimbusSelectionBackground", new Color(76, 153, 82));//(74, 165, 82));
UIManager.put("text", new Color(0,0,0));
// correction:
UIManager.put("MenuItem.opaque", true); // peaColor background
UIManager.put("Panel.opaque", true); // peaColor background
//UIManager.put("Button.contentMargins", new Insets(0,0,0,0));
break;
}
}
} catch (Exception e) {
System.err.println("Look&Feel Nimbus first try failed");
try {
// works for debian: package name for Nimbus was changed
// Bug JDK-6647622
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
System.err.println("Package name for Nimbus renamed, start with new packege name...");
UIManager.put("control", peaColor);
} catch (Exception e2) {
System.err.println("Setting of Look&Feel failed: \n"
+ UIManager.getSystemLookAndFeelClassName() + "\n"
+ e.toString() + ", " + e.getMessage());
// set Metal Look&Feel
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (Exception e1) {
System.err.println("Setting of Look&Feel failed: \n"
+ UIManager.getSystemLookAndFeelClassName() + "\n"
+ e.toString() + ", " + e.getMessage());
}
}
}
}
private void loadComponents(JPanel topPanel, boolean updateFiles) {
JPanel menuPanel = new JPanel();
menuPanel.setLayout(new BoxLayout(menuPanel, BoxLayout.X_AXIS));
menuPanel.setMinimumSize(new Dimension(300, 40));
menuPanel.setMaximumSize(new Dimension((int)getScreenwidth(), 30));
PeaMenuBar menuBar = new PeaMenuBar();
JMenu menu = new JMenu(languagesBundle.getString("file"));
// menu.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, fontSize));
// menu.setMnemonic(KeyEvent.VK_M);
menu.setMnemonic(KeyStroke.getKeyStroke(languagesBundle.getString("file_mnemonic")).getKeyCode());
menuBar.add(menu);
JMenuItem openItem = new JMenuItem(languagesBundle.getString("open_encrypted_file"));
openItem.setActionCommand("open");
// openItem.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, fontSize));
//openItem.setMnemonic(KeyEvent.VK_O);
openItem.setMnemonic(KeyStroke.getKeyStroke(languagesBundle.getString("open_mnemonic")).getKeyCode());
openItem.addActionListener(dialog);
menu.add(openItem);
JMenuItem initItem = new JMenuItem(languagesBundle.getString("init_new_file"));
initItem.setActionCommand("init");
// initItem.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, fontSize));
//initItem.setMnemonic(KeyEvent.VK_I);
initItem.setMnemonic(KeyStroke.getKeyStroke(languagesBundle.getString("init_mnemonic")).getKeyCode());
initItem.addActionListener(dialog);
menu.add(initItem);
JMenuItem quitItem = new JMenuItem(languagesBundle.getString("quit"));//"Quit");
quitItem.setActionCommand("quit");
// quitItem.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, fontSize));
//quitItem.setMnemonic(KeyEvent.VK_Q);
quitItem.setMnemonic(KeyStroke.getKeyStroke(languagesBundle.getString("quit_mnemonic")).getKeyCode());
quitItem.addActionListener(dialog);
menu.add(quitItem);
JMenu extrasMenu = new JMenu(languagesBundle.getString("extras"));
// extrasMenu.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, fontSize));
//extrasMenu.setMnemonic(KeyEvent.VK_M);
extrasMenu.setMnemonic(KeyStroke.getKeyStroke(languagesBundle.getString("extras_mnemonic")).getKeyCode());
menuBar.add(extrasMenu);
JMenuItem charTableItem = new JMenuItem(languagesBundle.getString("char_table") + " (CTRL + T) / (CTRL + F1...)");
charTableItem.setActionCommand("charTable1");
// charTableItem.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, fontSize));
//charTableItem.setMnemonic(KeyEvent.VK_I);
charTableItem.setMnemonic(KeyStroke.getKeyStroke(languagesBundle.getString("char_table_mnemonic")).getKeyCode());
charTableItem.addActionListener(dialog);
extrasMenu.add(charTableItem);
JMenuItem keyboardItem = new JMenuItem(languagesBundle.getString("keyboard") + " (CTRL + K)");
keyboardItem.setActionCommand("keyboard");
// keyboardItem.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, fontSize));
//keyboardItem.setMnemonic(KeyEvent.VK_I);
keyboardItem.setMnemonic(KeyStroke.getKeyStroke(languagesBundle.getString("keyboard_mnemonic")).getKeyCode());
keyboardItem.addActionListener(dialog);
extrasMenu.add(keyboardItem);
/* JMenu lookAndFeelMenu = new JMenu("Look&Feel");
lookAndFeelMenu.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
//help.setMnemonic(KeyEvent.VK_H);
lookAndFeelMenu.setMnemonic(KeyStroke.getKeyStroke("L").getKeyCode());
extrasMenu.add(lookAndFeelMenu);
LookAndFeelInfo[] lfs = UIManager.getInstalledLookAndFeels();
// List<String> lookAndFeelsDisplay = new ArrayList<>();
// List<String> lookAndFeelsRealNames = new ArrayList<>();
int lfsLen = lfs.length;
for (int i = 0; i < lfsLen; i++) {
JMenuItem item = new JMenuItem(lfs[i].getName());
item.addActionListener(PswDialogView.getInstance());
item.setActionCommand("lfs" + lfs[i].getClassName());
lookAndFeelMenu.add(item);
} */
// if (PeaProperties.getFileType().equals("file")){
JMenu viewMenu = new ViewMenu((FileTypePanel)PeaProperties.getTypePanel(), false);// plainModus false
menuBar.add(viewMenu);
// }
JMenu helpMenu = new HelpMenu();
menuBar.add(helpMenu);
menuPanel.add(menuBar);
menuPanel.add(Box.createHorizontalGlue());
topPanel.add(menuPanel);
topPanel.add(Box.createVerticalStrut(5));
JPanel errorMessagePanel = new JPanel();
errorMessagePanel.setLayout(new BoxLayout(errorMessagePanel, BoxLayout.X_AXIS));
errorMessageLabel = new JLabel(" ");
errorMessageLabel.setForeground(Color.RED);
errorMessagePanel.add(errorMessageLabel);
errorMessagePanel.add(Box.createHorizontalGlue());
topPanel.add(errorMessagePanel);
topPanel.add(Box.createVerticalStrut(5));
okButton = new JButton(languagesBundle.getString("ok"));
okButton.setPreferredSize(new Dimension( 60, 30));
okButton.setActionCommand("ok");
okButton.addActionListener(dialog);
// if there is no password, this might be the only
// chance to start EntropyPool
okButton.addMouseMotionListener(new MouseRandomCollector() );
if (initializing == false) {
if (PeaProperties.getWorkingMode().equals("-t")) { // test mode
JLabel rescueLabel = new JLabel("TEST MODE");
topPanel.add(rescueLabel);
try { //redirect stderr to file
System.setErr(new PrintStream(new FileOutputStream("error_log.txt")));
// print some informations to stderr:
System.err.println(System.getProperty("os.name") + "\n"
+ "Java " + System.getProperty("java.version") + "\n"
+ "PEA type: " + PeaProperties.getFileType() + ", version: " + PeaControl.getDialog().getVersion() );//VERSION);
System.err.println(KeyDerivation.getKdf().getName() + ", "
+ CipherStuff.getCipherAlgo().getAlgorithmName()
+ ", " + HashStuff.getHashAlgo().getAlgorithmName());
} catch (Exception e) {
System.err.println("Redirection of stderr failed: " + e.toString() + ", " + e.getMessage());
}
if (PeaProperties.getFileType().equals("image")) { // test mode
String names[] = ImageIO.getReaderFormatNames();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < names.length; ++i) {
builder.append("\"" + names[i] + "\",");
//System.out.println(names[i]);
}
//System.out.println(builder.substring(0, builder.lastIndexOf(",")));
System.err.println("Supported image formats: " + builder.substring(0, builder.lastIndexOf(",")));// delete the last ','
}
} else if (PeaProperties.getWorkingMode().equals("-r")) { // rescue mode
JLabel rescueLabel = new JLabel(languagesBundle.getString("rescue_mode"));
rescueLabel.setForeground(Color.RED);
topPanel.add(rescueLabel);
}
// if (fileType.equals("file") ) {
JPanel filePanel = (JPanel) PeaProperties.getTypePanel();
filePanel.addMouseMotionListener(new MouseRandomCollector() );
topPanel.add(Box.createVerticalStrut(10));
topPanel.add(filePanel);
/* }
else {
if(availableFilesPanel == null){
availableFilesPanel = new JPanel();
availableFilesPanel.setLayout(new BoxLayout(availableFilesPanel, BoxLayout.Y_AXIS));
if ( ! ( fileType.equals("text file") || fileType.equals("image") ) ){ // not editor
fileGroup = new ButtonGroup();
}
}
topPanel.add(availableFilesPanel);
if (updateFiles == true) {
PeaControl.searchSingleFile();
}
} */
topPanel.add(Box.createVerticalStrut(5));
/* if (fileType.equals("text file") || fileType.equals("image")) {
JPanel togglePanel = new JPanel();
togglePanel.setLayout(new BoxLayout(togglePanel, BoxLayout.X_AXIS));
JCheckBox selectAll = new JCheckBox(languagesBundle.getString("all"));
//selectAll.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 10));
selectAll.setMargin(new Insets(1,1,1,1));
selectAll.addActionListener(dialog);
selectAll.setActionCommand("selectAll");
//togglePanel.add(Box.createHorizontalGlue());
togglePanel.add(selectAll);
togglePanel.add(Box.createHorizontalGlue());
topPanel.add(togglePanel);
topPanel.add(Box.createVerticalStrut(5));
}*/
JPanel pswLabelPanel = new JPanel();
pswLabelPanel.addMouseMotionListener(new MouseRandomCollector() );
pswLabelPanel.setLayout(new BoxLayout(pswLabelPanel, BoxLayout.X_AXIS));
topPanel.add(Box.createVerticalStrut(10));
topPanel.add(pswLabelPanel);
if (PeaSettings.getLabelText() == null) {
pswLabel = new JLabel( languagesBundle.getString("enter_password") );
} else {
pswLabel = new JLabel( PeaSettings.getLabelText() );
}
pswLabel.setPreferredSize(new Dimension( 460, 20));
pswLabelPanel.add(pswLabel);
pswLabelPanel.add(Box.createHorizontalGlue());
JPanel pswPanel = new JPanel();
pswPanel.setLayout(new BoxLayout(pswPanel, BoxLayout.X_AXIS));
pswField = new JPasswordField();
pswField.setBackground(new Color(231, 231, 231) );
pswField.setPreferredSize(new Dimension(400, 30));
pswField.setMinimumSize(new Dimension(400, 30));
pswField.setMaximumSize(new Dimension(400, 30));
pswField.addKeyListener(dialog);
pswField.addKeyListener(new KeyRandomCollector() );
pswPanel.add(pswField);
pswPanel.add(okButton);
topPanel.add(pswPanel);
} else {// initializing
//
}
// if (fileType.equals("file")){
JPanel checkRememberPanel = new JPanel();
checkRememberPanel.setLayout(new BoxLayout(checkRememberPanel, BoxLayout.X_AXIS));
rememberCheck = new JCheckBox();
// rememberCheck.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, fontSize));
rememberCheck.setText(languagesBundle.getString("remember_file_names"));//"remember selected file names");
rememberCheck.setToolTipText(languagesBundle.getString("file_storage_info")// "file names will be saved in the file " +
+ " " + PeaSettings.getJarFileName() + ".path");// in current directory");
checkRememberPanel.add(Box.createVerticalStrut(10));
checkRememberPanel.add(rememberCheck);
checkRememberPanel.add(Box.createHorizontalGlue() );
topPanel.add(checkRememberPanel);
// }
}
public final static void updateView(){
/* problems for editor tabs
dialog.getContentPane().removeAll();
loadComponents((JPanel) dialog.getContentPane(), false);
dialog.pack();*/
dialog.getContentPane().invalidate();
dialog.getContentPane().validate();
dialog.invalidate();
dialog.validate();
dialog.pack();
dialog.repaint();
}
/**
* Initialize the PEA with a new password, new salt and new content
*/
private final static void initialize(){
initializing = true;
NewPasswordDialog.setRandomCollector(true);
String hint = null;
if (PeaProperties.getFileType().equals("file")){
hint = PeaProperties.getBundle().getString("hint_initialization");
}
NewPasswordDialog newPswDialog = NewPasswordDialog.getInstance(
dialog, PeaProperties.getBundle().getString("initialization"),
hint, dialog.getLocation());
PeaControl.getDialog().setInitializedPassword( newPswDialog.getDialogInput() );
NewPasswordDialog.setRandomCollector(false);
if (newPswDialog.getDialogInput() == null) {
System.err.println("initialization canceled, program exits");
System.exit(0);
}
PeaControl.getDialog().initializeNewFiles();
// dialog.setVisible(false);
PeaControl.getDialog().startExecution(false);
}
//===============================================
// Getter & Setter
public final static char[] getPassword() {
return pswField.getPassword();
}
protected final static void setPassword(char[] psw) { // for Keyboard
pswField.setText( new String(psw) );
}
protected final static void setPassword(char pswChar) { // for charTable
pswField.setText("" + pswChar );
}
public final static void clearPassword() {
if (pswField != null) {
pswField.setText("");
}
}
public final static void setMessage(String message, boolean error) {
if (error == true) {
errorMessageLabel.setForeground(Color.RED);
} else {
errorMessageLabel.setForeground(Color.BLUE);
}
//System.out.println("PswDialogView - setMessage: " + message);
if (message != null) {
if (message.startsWith("###")){
errorMessageLabel.setText(message.substring(3, message.length()));
} else {
try {
String i18nMessage = languagesBundle.getString(message);
errorMessageLabel.setText(i18nMessage);
} catch (Exception e) {
errorMessageLabel.setText(message);
}
}
}
}
public final static void setLabelText(String labelText) {
if (initializing == true){
pswLabel.setText(labelText);
}
}
/* public final static boolean validFileAvailable() {
if (fileGroup == null){
return false;
}
if (fileGroup.getButtonCount() > 0){
return true;
} else {
return false;
}
} */
/**
* Adds a file name to display as JRadioButton (simple notebook, image)
* or JCheckBox (editor).
*
* @param fileName the file name to add
* @param newSelected set the JRadioButton or JCheckBox selected
*/
/* public final static void addAvailableFile(String fileName, boolean newSelected) {
if (fileType.equals("text file") || fileType.equals("image")) {
// check if fileName is already listed:
if (availableFilesPanel.getComponentCount() > 0) {
int len = availableFilesPanel.getComponentCount();
for (int i = 0; i < len; i++) {
if (((JCheckBox)availableFilesPanel.getComponent(i)).getText().equals(fileName)){
return;
}
}
}
// register file inside jar
if (fileName.equals(languagesBundle.getString("image_inside_jar_archive"))){
PeaProperties.setInsideJar(true);
} else {
PeaProperties.setInsideJar(false);
}
JCheckBox check = new JCheckBox(fileName);
availableFilesPanel.add(check);
check.setSelected(newSelected);
availableFilesPanel.invalidate();
availableFilesPanel.validate();
updateView();
} else {
JPanel fileRadioPanel = new JPanel();
fileRadioPanel.setLayout(new BoxLayout(fileRadioPanel, BoxLayout.X_AXIS));
JRadioButton fileRadioButton = new JRadioButton();
// if first file: select
if (fileGroup.getButtonCount() == 0){
fileRadioButton.setSelected(true);
if (fileName.equals(languagesBundle.getString("image_inside_jar_archive"))){
PeaProperties.setInsideJar(true);
} else {
PeaProperties.setInsideJar(false);
}
}
fileGroup.add(fileRadioButton);
fileRadioButton.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
fileRadioButton.setText(fileName);//"remember selected file names");
fileRadioButton.addActionListener(dialog);
fileRadioButton.setActionCommand("FILE" + fileName);
availableFilesPanel.add(Box.createVerticalStrut(3));
fileRadioPanel.add(fileRadioButton);
fileRadioPanel.add(Box.createHorizontalGlue() );
availableFilesPanel.add(fileRadioPanel);
if (newSelected == true) {
fileRadioButton.setSelected(true);
availableFilesPanel.revalidate();
//dialog.revalidate();
dialog.repaint();
dialog.pack();
errorMessageLabel.setText(" ");
}
}
} */
/**
* Show a message dialog with links and email, related to this PEA
*
* @param websiteFolder folder with leading slash after the language,
* that is "/html/xxx_pea.html"
* @param downloadLink full link to download the source code
*/
public final static void showInfoDialog(Window parent){
String websiteFolder = PeaControl.getDialog().getWebsite();
String downloadLink = PeaControl.getDialog().getSourceLink();
String lang = System.getProperty("user.language"); // en, de
if (lang != "de" && lang != "en") {
lang = "en";
}
String infoSite = "http://eck.cologne/peafactory/" + lang + websiteFolder;
JOptionPane.showMessageDialog(parent, //PswDialogView.getView(),
PeaProperties.getBundle().getString("find_web_infos") + ": \n"
+ " " + infoSite + "\n\n"
+ PeaProperties.getBundle().getString("general_infos") + ": \n"
+ " http://eck.cologne/peafactory/" + lang + "/index.html\n\n"
+ PeaProperties.getBundle().getString("find_source_code") + ": \n"
+ " " + downloadLink + "\n\n"
+ PeaProperties.getBundle().getString("contact") + ": \n"
+ " peafactory@eck.cologne\n",
PeaProperties.getBundle().getString("help"),
JOptionPane.INFORMATION_MESSAGE);
}
/**
* Show a dialog with informations about the properties of the current PEA
*
* @param peaType the type or name of the PEA
* @param extraLine an optional extra line for PEA dependent properties
*/
public final static void showAboutDialog(String peaType, String extraLine, Window parent) {
String x = PeaProperties.getBundle().getString("with") + " ";
if (PeaSettings.getKeyFileProperty() == false){
x = PeaProperties.getBundle().getString("without") + " ";
}
if (extraLine == null){
extraLine = " ";
}
JOptionPane.showMessageDialog(
parent,//PswDialogView.getView(),
peaType + " - " + PeaProperties.getBundle().getString("part_of") + " PeaFactory\n"
+ PeaProperties.getBundle().getString("version") + ": " + PeaControl.getDialog().getVersion() + " (" + PeaControl.getDialog().getYearOfPublication() + ")\n" +
x + PeaProperties.getBundle().getString("key_file_property") + "\n" +
extraLine + "\n"
+ PeaProperties.getBundle().getString("used_primitives") + "\n"
+ "Key derivation: " + KeyDerivation.getKdf().getName()
+ ", time parameter: " + KeyDerivation.gettCost()
+ ", memory parameter: " + KeyDerivation.getmCost() + "\n"
+ "Cipher: " + CipherStuff.getCipherAlgo().getAlgorithmName() + "\n"
+ "Mode: " + CipherStuff.getCipherMode().getClass().getSimpleName() + "\n"
+ "Hash Function: " + HashStuff.getHashAlgo().getAlgorithmName(),
null, JOptionPane.PLAIN_MESSAGE);
}
public final static WhileTypingThread getThread() {
return t;
}
public final static Image getImage() {
return image;
}
public final static PswDialogView getView() {
return dialog;
}
public final static Color getPeaColor(){
return peaColor;
}
public final static void setStarted(boolean _started) {
started = _started;
}
/**
* @return the initializing
*/
public static boolean isInitializing() {
return initializing;
}
/**
* @param initializing the initializing to set
*/
public static void setInitializing(boolean initialize) {
PswDialogView.initializing = initialize;
}
/**
* @return the screenwidth
*/
public static double getScreenwidth() {
return screenWidth;
}
/**
* @return the screenheight
*/
public static double getScreenheight() {
return screenHeight;
}
/**
* @return the fontsize
*/
public static int getFontsize() {
return fontSize;
}
public static void setFontSize(int newFontSize) {
fontSize = newFontSize;
}
protected class AccessiblePswDialogView extends AccessibleJDialog {
//Inherit everything, override nothing.
}
//================================================
// inner class
public class WhileTypingThread extends Thread {
// some pre-computations while typing the password
@Override
public void run() {
if (PeaControl.getDialog() != null) {
PeaControl.getDialog().preComputeInThread();
}
}
}
}