0
			list.clearSelection();
		} 
	} 
 
	private void updateText() { 
		T option = (T) list.getSelectedValue();
		String text = option == null ? userInput : completionProvider
				.toString(option);
		Document d = textField.getDocument();
		d.removeDocumentListener(documentListener);
		textField.setText(text);
		d.addDocumentListener(documentListener);
	} 
 
	private void handleSpecialKeys(KeyEvent keyEvent) {
		if (!isOptionsPopupVisible()) { 
			return; 
		} 
 
		switch (keyEvent.getKeyCode()) {
		case VK_ESCAPE: // [ESC]
			hideOptionsPopup(); 
			keyEvent.consume();
			break; 
		case VK_ENTER: // [ENTER]
			hideOptionsPopup(); 
			textField.selectAll();
			break; 
		case VK_DOWN: // [DOWN]
			navigateRelative(+1); 
			break; 
		case VK_UP: // [UP]
			navigateRelative(-1); 
			break; 
		case VK_PAGE_DOWN: // [PAGE_DOWN]
			navigateRelative(+maxPopupRowCount - 1);
			break; 
		case VK_PAGE_UP: // [PAGE_UP]
			navigateRelative(-maxPopupRowCount + 1);
			break; 
		} 
	} 
 
	private void navigateRelative(int offset) {
		boolean up = offset < 0;
		int end = listModel.getSize() - 1;
		int index = list.getSelectedIndex();
 
		// Wrap around 
		if (-1 == index) {
			index = up ? end : 0;
		} else if (0 == index && up || end == index && !up) {
			index = -1;
		} else { 
			index += offset;
			index = max(0, min(end, index));
		} 
 
		if (-1 == index) {
			list.clearSelection();
		} else { 
			list.setSelectedIndex(index);
			list.ensureIndexIsVisible(index);
		} 
		updateText(); 
	} 
 
	private void handleMouseClick(MouseEvent e) {
Experience pair programming with AI  Get Codota for Java