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