Mercurial Hosting > luan
view src/luan/modules/swing/SwingLuan.java @ 1884:55ad3e7cd01a
swing
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 08 Apr 2025 19:43:00 -0600 |
parents | 3c30ae764004 |
children | a28e22991c58 |
line wrap: on
line source
package luan.modules.swing; import java.lang.reflect.InvocationTargetException; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.KeyStroke; import javax.swing.Action; import javax.swing.AbstractAction; import javax.swing.JComponent; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.text.JTextComponent; import javax.swing.text.Document; import javax.swing.undo.UndoManager; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.WindowListener; import java.awt.event.WindowEvent; import java.awt.event.WindowAdapter; import goodjava.logging.Logger; import goodjava.logging.LoggerFactory; import luan.Luan; import luan.LuanFunction; import luan.LuanTable; import luan.LuanException; import luan.LuanRuntimeException; public class SwingLuan { private static final Logger logger = LoggerFactory.getLogger(SwingLuan.class); static { //System.setProperty("apple.awt.application.name", "MyApplication"); System.setProperty("apple.laf.useScreenMenuBar", "true"); //System.out.println(UIManager.getLookAndFeel()); /* try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(ClassNotFoundException e) { throw new RuntimeException(e); } catch(InstantiationException e) { throw new RuntimeException(e); } catch(IllegalAccessException e) { throw new RuntimeException(e); } catch(UnsupportedLookAndFeelException e) { throw new RuntimeException(e); } */ } private static void exception(LuanException e) { System.err.println(e.getLuanStackTraceString()); System.exit(1); } private static void exception(String msg) { exception( new LuanException(msg) ); } private static Runnable runnable(final Luan luan,final LuanFunction fn) { return new Runnable() { public void run() { try { fn.call(luan); } catch(LuanException e) { exception(e); } } }; } public static void run(Luan luan,LuanFunction fn) throws InterruptedException, InvocationTargetException { SwingUtilities.invokeAndWait(runnable(luan,fn)); } public static ActionListener newActionListener(final Luan luan,final LuanFunction fn) throws LuanException { if( fn == null ) exception("function is null"); if( !fn.canTake(1) ) exception("action listener function must take event parameter"); return new ActionListener() { @Override public void actionPerformed(ActionEvent event) { try { LuanTable t = new LuanTable(); Object source = event.getSource(); if( source instanceof JComponent ) { JComponent jcomponent = (JComponent)source; Object component = jcomponent.getClientProperty("luan"); if( component != null ) t.rawPut("source",component); } fn.call(luan,t); } catch(LuanException e) { //throw new LuanRuntimeException(e); exception(e); } } }; } public static WindowListener newCloseListener(final Luan luan,LuanFunction fn) { return new WindowAdapter() { private void call() { try { fn.call(luan); } catch(LuanException e) { throw new LuanRuntimeException(e); } } @Override public void windowClosed(WindowEvent e) { call(); } }; } public static void fixTextComponent(final JTextComponent tc) { tc.getInputMap().put(KeyStroke.getKeyStroke("meta Z"),"undo"); Action undoAction = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { UndoManager undoManager = (UndoManager)tc.getDocument().getProperty("undo"); if( undoManager.canUndo() ) undoManager.undo(); } }; tc.getActionMap().put("undo",undoAction); tc.getInputMap().put(KeyStroke.getKeyStroke("shift meta Z"),"redo"); Action redoAction = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { UndoManager undoManager = (UndoManager)tc.getDocument().getProperty("undo"); if( undoManager.canRedo() ) undoManager.redo(); } }; tc.getActionMap().put("redo",redoAction); } }