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