|
| 1 | + |
| 2 | +# File: GeneratorPanel.java |
| 3 | + |
| 4 | +## Overview |
| 5 | +`GeneratorPanel` is a Java class extending `JPanel`. It serves as the UI panel for generating secure passwords, copying them to the clipboard, and saving them. The class interacts with `PasswordManager` and uses auxiliary tools like `PasswordGenerator` and `PasswordStrengthCalculator`. |
| 6 | + |
| 7 | +## Dependencies |
| 8 | +- `javax.swing.*`: For UI components (JTextField, JCheckBox, JButton, etc.). |
| 9 | +- `java.awt.*`: For layout management and styling. |
| 10 | +- `java.awt.datatransfer.StringSelection`: For clipboard functionality. |
| 11 | +- `javax.swing.event.ChangeEvent`, `ChangeListener`: For slider events. |
| 12 | +- `javax.swing.border.*`: For component styling. |
| 13 | + |
| 14 | +## Fields |
| 15 | + |
| 16 | +| Modifier | Type | Name | Description | |
| 17 | +|----------------|------------------------|-----------------------|--------------------------------------| |
| 18 | +| `private` | `JTextField` | `passwordField` | Displays the generated password. | |
| 19 | +| `private` | `JSlider` | `lengthSlider` | Controls the password length. | |
| 20 | +| `private` | `JLabel` | `lengthValueLabel` | Displays the current slider value. | |
| 21 | +| `private` | `JCheckBox` | `includeUppercase` | Toggle to include uppercase letters. | |
| 22 | +| `private` | `JCheckBox` | `includeLowercase` | Toggle to include lowercase letters. | |
| 23 | +| `private` | `JCheckBox` | `includeNumbers` | Toggle to include numbers. | |
| 24 | +| `private` | `JCheckBox` | `includeSymbols` | Toggle to include symbols. | |
| 25 | +| `private` | `JProgressBar` | `strengthIndicator` | Indicates the strength of the password.| |
| 26 | +| `private` | `PasswordManager` | `passwordManager` | Manages password storage. | |
| 27 | +| `private` | `Color` | `successColor` | Color for strong passwords. | |
| 28 | +| `private` | `Color` | `warningColor` | Color for medium passwords. | |
| 29 | +| `private` | `Color` | `errorColor` | Color for weak passwords. | |
| 30 | +| `private` | `Color` | `textColor` | Default text color. | |
| 31 | + |
| 32 | +## Methods |
| 33 | + |
| 34 | +### `GeneratorPanel(PasswordManager passwordManager)` |
| 35 | +- **Purpose**: Constructor for the `GeneratorPanel`. |
| 36 | +- **Parameters**: |
| 37 | + - `passwordManager`: Instance of `PasswordManager` for saving passwords. |
| 38 | +- **Description**: Initializes the panel, creates UI components, and sets up listeners. |
| 39 | + |
| 40 | +### `private void initializeComponents()` |
| 41 | +- **Purpose**: Sets up the UI layout, components, and event listeners. |
| 42 | +- **Description**: |
| 43 | + - Creates a card-like styled UI for password generation. |
| 44 | + - Adds a password display field, length slider, and checkboxes for customization. |
| 45 | + - Provides buttons to generate, copy, and save passwords. |
| 46 | + |
| 47 | +### `private void setupListeners()` |
| 48 | +- **Purpose**: Adds event listeners for the length slider and checkboxes. |
| 49 | +- **Listeners**: |
| 50 | + - Updates the password field dynamically when sliders or checkboxes change. |
| 51 | + |
| 52 | +### `private void generatePassword()` |
| 53 | +- **Purpose**: Generates a password based on user settings. |
| 54 | +- **Dependencies**: Uses `PasswordGenerator.generatePassword` to create a password. |
| 55 | +- **Steps**: |
| 56 | + 1. Retrieves user-selected options (length, character types). |
| 57 | + 2. Updates `passwordField` with the new password. |
| 58 | + 3. Calculates and updates the password strength using `PasswordStrengthCalculator`. |
| 59 | + |
| 60 | +### `private void copyPassword()` |
| 61 | +- **Purpose**: Copies the generated password to the clipboard. |
| 62 | +- **Behavior**: |
| 63 | + - Retrieves the password from `passwordField`. |
| 64 | + - Copies it to the system clipboard. |
| 65 | + - Displays a confirmation dialog. |
| 66 | + |
| 67 | +### `private void savePassword()` |
| 68 | +- **Purpose**: Saves the generated password using the `SavePasswordDialog`. |
| 69 | +- **Behavior**: |
| 70 | + - Prompts the user with a dialog to save the password. |
| 71 | + - Validates if a password exists before saving. |
| 72 | + |
| 73 | +### `private JButton createStyledButton(String text)` |
| 74 | +- **Purpose**: Creates a styled button for the UI. |
| 75 | +- **Parameters**: |
| 76 | + - `text`: Label for the button. |
| 77 | +- **Returns**: A button with predefined colors, font, and size. |
| 78 | + |
| 79 | +### `private JCheckBox createStyledCheckBox(String text, boolean selected)` |
| 80 | +- **Purpose**: Creates a styled checkbox. |
| 81 | +- **Parameters**: |
| 82 | + - `text`: Label for the checkbox. |
| 83 | + - `selected`: Default selection state. |
| 84 | + |
| 85 | +### `private void updateStrengthMeter(JProgressBar meter, int strength)` |
| 86 | +- **Purpose**: Updates the strength indicator based on the password's strength score. |
| 87 | +- **Behavior**: |
| 88 | + - Sets the value, string description, and color of the progress bar. |
| 89 | +- **Strength Ranges**: |
| 90 | + - `0-2`: Weak (Red color). |
| 91 | + - `3-4`: Medium (Yellow color). |
| 92 | + - `5`: Strong (Green color). |
| 93 | + |
| 94 | +## Event Listeners |
| 95 | +- **Slider**: Updates the password length dynamically. |
| 96 | +- **Checkboxes**: Triggers password regeneration when toggled. |
| 97 | +- **Buttons**: |
| 98 | + - **Generate**: Generates a new password. |
| 99 | + - **Copy**: Copies the password to the clipboard. |
| 100 | + - **Save**: Saves the password via `SavePasswordDialog`. |
| 101 | + |
| 102 | +## UI Components Layout |
| 103 | +1. **Password Display Panel**: |
| 104 | + - Displays generated passwords. |
| 105 | + - Includes a "Copy" button. |
| 106 | +2. **Password Length Section**: |
| 107 | + - Slider for password length with a dynamic value display. |
| 108 | +3. **Character Types Section**: |
| 109 | + - Four checkboxes: Uppercase, Lowercase, Numbers, Symbols. |
| 110 | +4. **Password Strength Section**: |
| 111 | + - Progress bar to indicate strength visually and textually. |
| 112 | +5. **Buttons**: |
| 113 | + - "Generate Password" and "Save Password". |
| 114 | + |
| 115 | +## Color Customization |
| 116 | +- **Success (Green)**: For strong passwords. |
| 117 | +- **Warning (Yellow)**: For medium passwords. |
| 118 | +- **Error (Red)**: For weak passwords. |
| 119 | +- **Default Text Color (Gray)**: Used for labels and checkboxes. |
| 120 | + |
| 121 | +## Notes |
| 122 | +- `PasswordGenerator` and `PasswordStrengthCalculator` are required utilities for this class. |
| 123 | +- This class focuses solely on UI and event-driven logic; generation and strength calculation are handled externally. |
| 124 | + |
| 125 | +## Example Usage |
| 126 | +```java |
| 127 | +PasswordManager passwordManager = new PasswordManager(); |
| 128 | +GeneratorPanel generatorPanel = new GeneratorPanel(passwordManager); |
| 129 | +``` |
0 commit comments