001package com.ganteater.ae.desktop.util; 002 003import java.awt.Color; 004import java.awt.Desktop; 005import java.awt.Dimension; 006import java.awt.GraphicsEnvironment; 007import java.awt.Point; 008import java.awt.Toolkit; 009import java.awt.Window; 010import java.awt.event.ActionEvent; 011import java.io.File; 012import java.io.IOException; 013import java.net.URI; 014import java.net.URISyntaxException; 015import java.net.URL; 016import java.util.List; 017 018import javax.swing.AbstractAction; 019import javax.swing.Action; 020import javax.swing.JDialog; 021import javax.swing.JOptionPane; 022import javax.swing.JPopupMenu; 023import javax.swing.UIManager; 024import javax.swing.UnsupportedLookAndFeelException; 025import javax.swing.event.HyperlinkEvent; 026import javax.swing.text.JTextComponent; 027import javax.swing.undo.UndoManager; 028 029import org.apache.commons.io.FileUtils; 030import org.apache.commons.lang.StringUtils; 031 032import com.ganteater.ae.AEWorkspace; 033 034public class UIUtils { 035 036 private static final String DIALOG_Y_NAME = ":@Dialog_Y:"; 037 private static final String DIALOG_X_NAME = ":@Dialog_X:"; 038 039 private UIUtils() { 040 super(); 041 } 042 043 public static void applyPreferedView(Window dialog, String name, int height, int width) { 044 int screenDevicesCount = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices().length; 045 046 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 047 048 int x = (screenSize.width - dialog.getWidth()) / 2; 049 int y = (screenSize.height - dialog.getHeight()) / 2; 050 051 x = Integer.valueOf(AEWorkspace.getInstance() 052 .getDefaultUserConfiguration(name + DIALOG_X_NAME + screenDevicesCount, Integer.toString(x))); 053 y = Integer.valueOf(AEWorkspace.getInstance() 054 .getDefaultUserConfiguration(name + DIALOG_Y_NAME + screenDevicesCount, Integer.toString(y))); 055 056 if (screenDevicesCount == 1) { 057 if (x < 0) { 058 x = 0; 059 } 060 061 if (y < 0) { 062 y = 0; 063 } 064 } 065 066 dialog.setLocation(x, y); 067 068 width = Integer.valueOf(AEWorkspace.getInstance() 069 .getDefaultUserConfiguration(name + ":@Dialog_Width:" + screenDevicesCount, Integer.toString(width))); 070 height = Integer.valueOf(AEWorkspace.getInstance() 071 .getDefaultUserConfiguration(name + ":@Dialog_Height:" + screenDevicesCount, Integer.toString(height))); 072 073 if ((height + y) > screenSize.height) { 074 height = screenSize.height - y - 20; 075 } 076 077 Dimension size = new Dimension(width, height); 078 dialog.setSize(size); 079 } 080 081 public static void saveDialogPreferedView(Window dialog, String name) { 082 Point location = dialog.getLocation(); 083 int screenDevicesCount = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices().length; 084 085 String propertyName = name + DIALOG_X_NAME + screenDevicesCount; 086 AEWorkspace.getInstance().setDefaultUserConfiguration(propertyName, Integer.toString(location.x)); 087 propertyName = name + DIALOG_Y_NAME + screenDevicesCount; 088 AEWorkspace.getInstance().setDefaultUserConfiguration(propertyName, Integer.toString(location.y)); 089 Dimension size = dialog.getSize(); 090 propertyName = name + ":@Dialog_Width:" + screenDevicesCount; 091 AEWorkspace.getInstance().setDefaultUserConfiguration(propertyName, Integer.toString(size.width)); 092 propertyName = name + ":@Dialog_Height:" + screenDevicesCount; 093 AEWorkspace.getInstance().setDefaultUserConfiguration(propertyName, Integer.toString(size.height)); 094 } 095 096 @SuppressWarnings("unchecked") 097 public static void applyLookAndFeel(Object className) { 098 String lookAndFeelClassName = null; 099 if (className instanceof String) { 100 lookAndFeelClassName = (String) className; 101 } else if (className instanceof List) { 102 lookAndFeelClassName = ((List<String>) className).get(0); 103 } 104 105 if (StringUtils.isBlank(lookAndFeelClassName)) { 106 lookAndFeelClassName = UIManager.getCrossPlatformLookAndFeelClassName(); 107 } 108 try { 109 UIManager.setLookAndFeel(lookAndFeelClassName); 110 111 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException 112 | UnsupportedLookAndFeelException e) { 113 e.printStackTrace(); 114 } 115 } 116 117 public static boolean applyPreferedView(JDialog dialog, String name) { 118 String defaultX = null; 119 try { 120 int screenDevicesCount = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices().length; 121 defaultX = AEWorkspace.getInstance().getDefaultUserConfiguration(name + DIALOG_X_NAME + screenDevicesCount, 122 null); 123 if (defaultX != null) { 124 applyPreferedView(dialog, name, 400, 600); 125 } 126 } catch (Exception e) { 127 defaultX = null; 128 } 129 130 return defaultX != null; 131 } 132 133 public static void addJPopupMenuTo(JTextComponent jTextField) { 134 addJPopupMenuTo(jTextField, new JPopupMenu()); 135 } 136 137 public static void addJPopupMenuTo(JTextComponent txtField, JPopupMenu popup) { 138 UndoManager undoManager = new UndoManager(); 139 txtField.getDocument().addUndoableEditListener(undoManager); 140 141 Action undoAction = new AbstractAction("Undo") { 142 private static final long serialVersionUID = 1L; 143 144 @Override 145 public void actionPerformed(ActionEvent ae) { 146 if (undoManager.canUndo()) { 147 undoManager.undo(); 148 } else { 149 JOptionPane.showMessageDialog(null, "Undoable: " + undoManager.canUndo(), "Undo Status", 150 JOptionPane.INFORMATION_MESSAGE); 151 } 152 } 153 }; 154 155 Action copyAction = new AbstractAction("Copy") { 156 private static final long serialVersionUID = 1L; 157 158 @Override 159 public void actionPerformed(ActionEvent ae) { 160 txtField.copy(); 161 } 162 }; 163 164 Action cutAction = new AbstractAction("Cut") { 165 private static final long serialVersionUID = 1L; 166 167 @Override 168 public void actionPerformed(ActionEvent ae) { 169 txtField.cut(); 170 } 171 }; 172 173 Action pasteAction = new AbstractAction("Paste") { 174 private static final long serialVersionUID = 1L; 175 176 @Override 177 public void actionPerformed(ActionEvent ae) { 178 txtField.paste(); 179 } 180 }; 181 182 Action selectAllAction = new AbstractAction("Select All") { 183 private static final long serialVersionUID = 1L; 184 185 @Override 186 public void actionPerformed(ActionEvent ae) { 187 txtField.selectAll(); 188 } 189 }; 190 191 popup.add(undoAction); 192 popup.addSeparator(); 193 popup.add(cutAction); 194 popup.add(copyAction); 195 popup.add(pasteAction); 196 popup.addSeparator(); 197 popup.add(selectAllAction); 198 199 txtField.setComponentPopupMenu(popup); 200 } 201 202 public static void open(HyperlinkEvent e) { 203 if (Desktop.isDesktopSupported()) { 204 try { 205 URL url = e.getURL(); 206 if (url != null) { 207 URI uri = url.toURI(); 208 if ("file".equals(uri.getScheme())) { 209 String path = uri.getSchemeSpecificPart(); 210 if (path != null) { 211 FileUtils.forceMkdir(new File(path)); 212 } 213 } 214 Desktop.getDesktop().browse(uri); 215 } else { 216 File file = new File(e.getDescription()); 217 Desktop.getDesktop().open(file); 218 } 219 } catch (IOException | URISyntaxException e1) { 220 throw new IllegalArgumentException("Can't open URL: " + e.getURL(), e1); 221 } 222 } 223 } 224 225 public static Color getContrastColor(Color color) { 226 double y = (299 * color.getRed() + 587 * color.getGreen() + 114 * color.getBlue()) / 1000; 227 return y >= 128 ? Color.black : Color.white; 228 } 229}