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.JEditorPane; 022import javax.swing.JOptionPane; 023import javax.swing.JPopupMenu; 024import javax.swing.UIManager; 025import javax.swing.UnsupportedLookAndFeelException; 026import javax.swing.event.HyperlinkEvent; 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(JEditorPane txtField) { 134 JPopupMenu popup = new JPopupMenu(); 135 UndoManager undoManager = new UndoManager(); 136 txtField.getDocument().addUndoableEditListener(undoManager); 137 138 Action undoAction = new AbstractAction("Undo") { 139 private static final long serialVersionUID = 1L; 140 141 @Override 142 public void actionPerformed(ActionEvent ae) { 143 if (undoManager.canUndo()) { 144 undoManager.undo(); 145 } else { 146 JOptionPane.showMessageDialog(null, "Undoable: " + undoManager.canUndo(), "Undo Status", 147 JOptionPane.INFORMATION_MESSAGE); 148 } 149 } 150 }; 151 152 Action copyAction = new AbstractAction("Copy") { 153 private static final long serialVersionUID = 1L; 154 155 @Override 156 public void actionPerformed(ActionEvent ae) { 157 txtField.copy(); 158 } 159 }; 160 161 Action cutAction = new AbstractAction("Cut") { 162 private static final long serialVersionUID = 1L; 163 164 @Override 165 public void actionPerformed(ActionEvent ae) { 166 txtField.cut(); 167 } 168 }; 169 170 Action pasteAction = new AbstractAction("Paste") { 171 private static final long serialVersionUID = 1L; 172 173 @Override 174 public void actionPerformed(ActionEvent ae) { 175 txtField.paste(); 176 } 177 }; 178 179 Action selectAllAction = new AbstractAction("Select All") { 180 private static final long serialVersionUID = 1L; 181 182 @Override 183 public void actionPerformed(ActionEvent ae) { 184 txtField.selectAll(); 185 } 186 }; 187 188 popup.add(undoAction); 189 popup.addSeparator(); 190 popup.add(cutAction); 191 popup.add(copyAction); 192 popup.add(pasteAction); 193 popup.addSeparator(); 194 popup.add(selectAllAction); 195 196 txtField.setComponentPopupMenu(popup); 197 } 198 199 public static void open(HyperlinkEvent e) { 200 if (Desktop.isDesktopSupported()) { 201 try { 202 URL url = e.getURL(); 203 if (url != null) { 204 URI uri = url.toURI(); 205 if ("file".equals(uri.getScheme())) { 206 String path = uri.getSchemeSpecificPart(); 207 if (path != null) { 208 FileUtils.forceMkdir(new File(path)); 209 } 210 } 211 Desktop.getDesktop().browse(uri); 212 } else { 213 Desktop.getDesktop().open(new File(e.getDescription())); 214 } 215 } catch (IOException | URISyntaxException e1) { 216 throw new IllegalArgumentException("Can't open URL: " + e.getURL(), e1); 217 } 218 } 219 } 220 221 public static Color getContrastColor(Color color) { 222 double y = (299 * color.getRed() + 587 * color.getGreen() + 114 * color.getBlue()) / 1000; 223 return y >= 128 ? Color.black : Color.white; 224 } 225}