---

# (DD06) Business Logic — UI.comboBox1() [7 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `com.github.blaxk3.ui.UI` |
| Layer | UI Component / Controller |
| Module | `ui` (Package: `com.github.blaxk3.ui`) |

## 1. Role

### UI.comboBox1()

This method is a UI factory method responsible for constructing and returning a populated **currency code selection combo box** used in the application's financial conversion screen. It creates a new `JComboBox<String>`, configures its preferred display dimensions (300px width × 30px height), and asynchronously populates it with currency code options fetched from an external currency rate API via the `CurrencyCode` `SwingWorker` inner class. The populated combo box is then stored in the instance field `comboBox1` so that other screen methods (such as `rate.convert()`) can read the user's selection to perform currency conversions. The method plays the role of a **UI builder** — it follows a delegation pattern where the actual data fetching is offloaded to a background worker thread (`CurrencyCode`) to avoid blocking the Swing Event Dispatch Thread (EDT), and the `done()` callback on the EDT handles the UI update. This method is an internal assembly step invoked once during the initialization of the main conversion panel (`panelFramePanel1`) and is subsequently accessed via the public `getComboBox1()` accessor.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["comboBox1()"])
    CREATE["Create new JComboBox instance"]
    SIZE["Set preferred size to 300 x 30 pixels"]
    POPULATE["Execute CurrencyCode worker — "
        "fetches currency codes from API"]
    ASSIGN["Store combo box in instance field "
        "comboBox1"]
    RETURN["Return JComboBox as Component"]

    START --> CREATE
    CREATE --> SIZE
    SIZE --> POPULATE
    POPULATE --> ASSIGN
    ASSIGN --> RETURN
```

**CRITICAL — Constant Resolution:**
No constants are referenced within this method. The only value set is the dimension `(300, 30)` which represents the fixed UI pixel size for the combo box.

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| - | (none) | - | This method takes no parameters. It constructs a self-contained UI component using only internal instance fields and a background worker. |

| No | State Source | Field / Object | Business Description |
|----|-------------|----------------|---------------------|
| 1 | Instance Field | `comboBox1` (`JComboBox<String>`) | The combo box instance field that receives the newly constructed and populated currency code selector. |
| 2 | Static Inner Class | `CurrencyCode` (`extends SwingWorker<String[], Void>`) | Background worker responsible for fetching currency codes from an external API and populating the combo box on the EDT via `done()`. |
| 3 | Static Field | `logger` (`Logger`) | SLF4J logger instance used by the `CurrencyCode` inner class to log errors during the asynchronous fetch. |

## 4. CRUD Operations / Called Services

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `CurrencyRateAPI.getCurrencyCode()` | — | External Currency Rate API | Reads the list of available currency codes (e.g., USD, EUR, JPY) from an external web API via `CurrencyRateAPI` inside the `CurrencyCode` SwingWorker's `doInBackground()` method. |

**Notes:**
- This method does not perform any SC/CBS or database-level CRUD operations. It is purely a UI-level factory that delegates to an external HTTP API for data retrieval.
- The `CurrencyCode` worker internally calls `new CurrencyRateAPI().getCurrencyCode()`, which makes an outbound HTTP request to a currency rate web service. This is classified as a **Read (R)** operation because it retrieves a static catalog of currency codes — it does not create, update, or delete any records.

## 5. Dependency Trace

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

Direct callers found: 1 methods. No screen/batch entry points. Terminal operations from this method: `CurrencyRateAPI.getCurrencyCode [R] External API`.

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Internal: UI panel builder | `UI.buildPanel` (L78) → `panelFramePanel1.add(comboBox1())` | `CurrencyRateAPI.getCurrencyCode [R] External Currency API` |

**Notes:**
- `comboBox1()` is a **private** method, so its only caller is within the same `UI` class — specifically the panel assembly code at line 78 which adds the combo box to `panelFramePanel1` (the dark-gray conversion panel).
- The method itself calls `CurrencyCode.execute()` which spawns a background thread that eventually calls `CurrencyRateAPI.getCurrencyCode()`.
- The populated combo box is consumed by `rate.convert()` at line 126 where the user's selected currency code is parsed and used in a currency conversion calculation.

## 6. Per-Branch Detail Blocks

**Block 1** — [ASSIGNMENT / EXEC] `(no condition — sequential execution)` (L95-101)

> Creates a combo box, sets its size, populates it with currency codes from the API, and returns it.

| # | Type | Code |
|---|------|------|
| 1 | SET | `comboBox1 = new JComboBox<>();` // Instantiate an empty combo box |
| 2 | SET | `comboBox1.setPreferredSize(new Dimension(300, 30));` // Set display size to 300×30px |
| 3 | CALL | `new CurrencyCode(comboBox1).execute();` // Spawn background SwingWorker to fetch currency codes and populate the combo box |
| 4 | RETURN | `return comboBox1;` // Return the fully configured and populated combo box |

**Block 1.1** — [nested: CurrencyCode.doInBackground()] `(background thread, L222-224)`

> Fetches currency codes from the external API.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `new CurrencyRateAPI().getCurrencyCode();` // HTTP GET to external currency rate API, returns String[] of currency codes |

**Block 1.2** — [nested: CurrencyCode.done()] `(EDT callback, L226-236)`

> Receives the result from the background thread and populates the combo box. Contains a conditional branch on null safety.

| # | Type | Code |
|---|------|------|
| 1 | SET | `String[] currencyCodes = get();` // Blocking get on SwingWorker result |
| 2 | IF | `if (currencyCodes != null)` // Null safety guard — skip population if fetch failed |
| 2.1 | CALL | `Arrays.stream(currencyCodes).sorted().forEach(comboBox::addItem);` // Stream, sort alphabetically, and add each code to the combo box |
| 2.2 | CATCH | `catch (InterruptedException \| ExecutionException e)` // Handle worker interruption or API fetch failure |
| 2.3 | EXEC | `logger.error("Error occurred while fetching currency codes", e);` // Log the error with full stack trace |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `comboBox1` | Field | Currency code selector — a Swing combo box component that allows the user to select a target currency for conversion |
| `comboBox2` | Field | Base currency selector — a second combo box (parallel to comboBox1) for selecting the source/base currency |
| `JComboBox` | Class | Swing UI component — a dropdown list widget that presents multiple choices to the user |
| `CurrencyCode` | Inner Class | A `SwingWorker` background task that asynchronously fetches currency codes from an external API |
| `CurrencyRateAPI` | Class | An API client that performs an HTTP request to a currency rate web service to retrieve available currency codes |
| `SwingWorker` | Class | Java concurrency utility — executes long-running tasks in a background thread and publishes results on the EDT |
| EDT | Acronym | Event Dispatch Thread — the single-threaded UI thread in Swing applications where all component updates must occur |
| `doInBackground()` | Method | SwingWorker callback — runs on a background thread; used here to make a non-blocking HTTP call to the currency API |
| `done()` | Method | SwingWorker callback — runs on the EDT after `doInBackground()` completes; used here to safely update the combo box UI |
| `rate.convert()` | Method | The currency conversion function that reads the selected values from `comboBox1` and `comboBox2` and computes the converted amount |

---
