001package com.ganteater.ae.desktop.editor; 002 003import java.awt.MouseInfo; 004import java.awt.Point; 005import java.awt.PointerInfo; 006import java.awt.Rectangle; 007 008import javax.swing.JDialog; 009import javax.swing.SwingUtilities; 010import javax.swing.text.BadLocationException; 011 012public abstract class HelperDialog extends JDialog { 013 014 private static final long serialVersionUID = 1L; 015 private CodeHelper codeHelper; 016 017 public HelperDialog(CodeHelper codeHelper) { 018 this.codeHelper = codeHelper; 019 } 020 021 public void showDialog() { 022 int mouseX; 023 int mouseY; 024 025 TextEditor editor = getCodeHelper().getEditor(); 026 TaskEditor editorPanel = getCodeHelper().getRecipePanel(); 027 028 PointerInfo pointerInfo = MouseInfo.getPointerInfo(); 029 if (pointerInfo != null) { 030 Point mouseLocation = pointerInfo.getLocation(); 031 mouseX = mouseLocation.x; 032 mouseY = mouseLocation.y; 033 } else { 034 Point magicCaretPosition = editorPanel.getMagicCaretPosition(); 035 magicCaretPosition = magicCaretPosition == null ? new Point() : magicCaretPosition; 036 Point locationOnScreen = editor.getLocationOnScreen(); 037 mouseX = locationOnScreen.x + magicCaretPosition.x; 038 mouseY = locationOnScreen.y + magicCaretPosition.y + editorPanel.getEditor().getFont().getSize() + 4; 039 } 040 041 Rectangle modelToView; 042 try { 043 modelToView = editor.modelToView(editor.getCaretPosition()); 044 Point locationOnScreen = editor.getLocationOnScreen(); 045 mouseX = locationOnScreen.x + modelToView.x; 046 mouseY = locationOnScreen.y + modelToView.y + editorPanel.getEditor().getFont().getSize() + 4; 047 048 setLocation(mouseX, mouseY); 049 } catch (BadLocationException e) { 050 // do nothing 051 } 052 053 pack(); 054 055 setVisible(true); 056 057 SwingUtilities.invokeLater(() -> { 058 requestFocusInWindow(); 059 }); 060 } 061 062 public CodeHelper getCodeHelper() { 063 return codeHelper; 064 } 065 066}