# (DD05) Business Logic — UI.comboBox2() [7 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `com.github.blaxk3.ui.UI` |
| Layer | UI/View (Controller — directly returns Swing UI components) |
| Module | `ui` (Package: `com.github.blaxk3.ui`) |

## 1. Role

### UI.comboBox2()

This method constructs and returns a pre-configured `JComboBox` component that displays a list of ISO 4217 currency codes (e.g., USD, EUR, JPY, GBP) populated from a live foreign exchange rate API. It serves as a **UI factory method** within the `UI` class — acting as a shared utility that produces a ready-to-use dropdown widget for screen layouts. The method follows the **Builder pattern** in a minimal form: it instantiates the component, configures its visual properties (preferred size), and delegates to the inner `CurrencyCode` SwingWorker class to asynchronously fetch and populate the data. Its **role in the larger system** is to provide the "base currency" or "from currency" selector (paired with `comboBox1` which is populated identically) for a currency conversion application. The method itself has no conditional branches — it always executes the same sequence: create, size, populate, return.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["comboBox2()"])
    INIT["Create new JComboBox"]
    SIZE["Set preferred size (300, 30)"]
    POPULATE["CurrencyCode.execute() — fetch currency codes from API"]
    API["CurrencyRateAPI.getCurrencyCode()"]
    FETCH["SwingWorker.fetch latest rates for USD"]
    SORT["Sort codes and add to combo box"]
    RETURN["Return comboBox2"]
    END(["End"])

    START --> INIT
    INIT --> SIZE
    SIZE --> POPULATE
    POPULATE --> API
    API --> FETCH
    FETCH --> SORT
    SORT --> RETURN
    RETURN --> END
```

**Note:** The `CurrencyCode` inner class (`extends SwingWorker<String[], Void>`) runs on a background thread via `SwingWorker.execute()`. Inside `doInBackground()`, it calls `CurrencyRateAPI().getCurrencyCode()` which hits an external exchange rate API (`https://api.exchangerate-api.com/v4/latest/USD`). The `done()` callback then sorts the returned currency code array and adds each entry to the combo box on the EDT.

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| - | (none) | - | This method takes no parameters — it is a zero-argument factory that produces a self-contained UI component. |

**External state read:**
| Source | Type | Business Description |
|--------|------|---------------------|
| `UI.comboBox2` (instance field) | `JComboBox` | The instance-level combo box reference that is assigned and returned |
| `CurrencyRateAPI` (external API) | HTTP service | External exchange rate API endpoint (`https://api.exchangerate-api.com/v4/latest/{base_currency}`) that provides real-time currency conversion rates |
| `CurrencyRateAPI.getURL()` | HTTP service | Base URL for the exchange rate API (returned by `getURL()` method on the API instance) |

## 4. CRUD Operations / Called Services

This method does not perform database operations or internal SC/CBS calls. Instead, it delegates to an **external REST API** for data:

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `CurrencyRateAPI.getCurrencyCode()` | N/A (External HTTP API) | N/A (No DB) | Fetches currency code list from the external exchange rate API endpoint — retrieves ISO 4217 currency codes (e.g., AED, AFN, EUR, JPY) as string array from the `conversion_rates` JSON object |

## 5. Dependency Trace

### Pre-computed evidence from code analysis graph:

No screen/batch entry points found within 8 hops. Direct callers found: 0 methods.

This method is a **private factory method** with no callers identified in the codebase. It is intended to be called from within the `UI` class's own screen/layout construction methods (e.g., a `createContent()` or `buildPanel()` method not yet analyzed) to assemble UI components for display.

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| - | N/A | N/A | No callers found |

**Trace summary:** `UI.comboBox2` is a private method (visibility: `private`). It is called indirectly by the containing `UI` class during screen construction. Its terminal operation is an HTTP GET to the external `CurrencyRateAPI` which returns a `String[]` of currency codes (no database involvement).

## 6. Per-Branch Detail Blocks

**Block 1** — [EXEC/ASSIGN] `(lines 104–106)`

> Creates the combo box, configures its size, and triggers asynchronous data population.

| # | Type | Code |
|---|------|------|
| 1 | SET | `comboBox2 = new JComboBox<String>()` |
| 2 | SET | `comboBox2.setPreferredSize(new Dimension(300, 30))` |
| 3 | CALL | `new CurrencyCode(comboBox2).execute()` — delegates to inner class; runs on background thread, populates combo box via `done()` callback on EDT |

**Block 2** — [RETURN] `(line 108)`

> Returns the fully constructed and populated combo box component.

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return comboBox2` |

### Block 2.1 — Inner Class: CurrencyCode (lines 214–239)

This inner `SwingWorker<String[], Void>` class handles the background fetch and EDT-safe population:

**Block 2.1.1** — [METHOD: doInBackground] `(line 222)`

> Runs on a background thread. Calls the external API.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `new CurrencyRateAPI().getCurrencyCode()` — hits external REST endpoint to get currency codes |

**Block 2.1.2** — [METHOD: done] `(line 227)`

> Runs on the Event Dispatch Thread (EDT) after `doInBackground()` completes. Populates the combo box with sorted currency codes.

| # | Type | Code |
|---|------|------|
| 1 | SET | `String[] currencyCodes = get()` |
| 2 | IF | `currencyCodes != null` |
| 3 | EXEC | `Arrays.stream(currencyCodes).sorted().forEach(comboBox::addItem)` — sorts codes alphabetically and adds each to the combo box |
| 4 | CATCH | `InterruptedException \| ExecutionException` — logs error via `logger.error()` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `JComboBox` | Component | Swing dropdown list widget used for selecting a currency code |
| `SwingWorker` | Java class | Background task executor in Swing that separates long-running operations (API fetch) from the Event Dispatch Thread (EDT) |
| `CurrencyCode` | Inner class | Background worker that fetches currency codes from an external API and populates a combo box on the EDT |
| `CurrencyRateAPI` | External API | REST API service (`https://api.exchangerate-api.com`) that provides real-time foreign exchange rates and currency codes |
| `conversion_rates` | JSON key | The object key in the API response containing all currency codes and their exchange rates relative to the base currency |
| `ISO 4217` | Standard | International standard for currency codes (e.g., USD = US Dollar, EUR = Euro, JPY = Japanese Yen) |
| `base_currency` | API concept | The reference currency against which all other currency rates are expressed (the API uses USD as the base) |
