1 | package org.expeditee.gio.swing;
|
---|
2 |
|
---|
3 | import java.awt.Desktop;
|
---|
4 | import java.awt.image.BufferedImage;
|
---|
5 | import java.io.File;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.net.URI;
|
---|
8 | import java.net.URISyntaxException;
|
---|
9 | import java.net.URL;
|
---|
10 |
|
---|
11 | import javax.swing.AbstractButton;
|
---|
12 | import javax.swing.Icon;
|
---|
13 | import javax.swing.ImageIcon;
|
---|
14 | import javax.swing.SwingUtilities;
|
---|
15 |
|
---|
16 | import org.expeditee.Util;
|
---|
17 | import org.expeditee.core.Image;
|
---|
18 | import org.expeditee.gio.EcosystemManager;
|
---|
19 | import org.expeditee.gio.FontManager;
|
---|
20 | import org.expeditee.gio.GraphicsManager;
|
---|
21 | import org.expeditee.gio.ImageManager;
|
---|
22 | import org.expeditee.gio.MiscManager;
|
---|
23 | import org.expeditee.gio.TextLayoutManager;
|
---|
24 |
|
---|
25 | import javafx.embed.swing.SwingFXUtils;
|
---|
26 |
|
---|
27 | public class SwingMiscManager extends MiscManager {
|
---|
28 |
|
---|
29 | private static SwingMiscManager _instance;
|
---|
30 |
|
---|
31 | public static SwingMiscManager getInstance()
|
---|
32 | {
|
---|
33 | if (_instance == null) _instance = new SwingMiscManager();
|
---|
34 |
|
---|
35 | return _instance;
|
---|
36 | }
|
---|
37 |
|
---|
38 | public static void setJButtonIcon(AbstractButton jbutton, Image image)
|
---|
39 | {
|
---|
40 | if (jbutton == null || image == null) return;
|
---|
41 |
|
---|
42 | SwingImageManager manager = getIfUsingSwingImageManager();
|
---|
43 |
|
---|
44 | if (manager == null) return;
|
---|
45 |
|
---|
46 | Icon icon = new ImageIcon(manager.getInternalImage(image));
|
---|
47 |
|
---|
48 | jbutton.setIcon(icon);
|
---|
49 | }
|
---|
50 |
|
---|
51 | public static void setJButtonSelectedIcon(AbstractButton jbutton, Image image)
|
---|
52 | {
|
---|
53 | if (jbutton == null || image == null) return;
|
---|
54 |
|
---|
55 | SwingImageManager manager = getIfUsingSwingImageManager();
|
---|
56 |
|
---|
57 | if (manager == null) return;
|
---|
58 |
|
---|
59 | Icon icon = new ImageIcon(manager.getInternalImage(image));
|
---|
60 |
|
---|
61 | jbutton.setSelectedIcon(icon);
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Returns the SwingImageManager if it is the ImageManager for this session,
|
---|
66 | * or null if another ImageManager is being used.
|
---|
67 | */
|
---|
68 | public static SwingImageManager getIfUsingSwingImageManager()
|
---|
69 | {
|
---|
70 | ImageManager manager = org.expeditee.core.Image.getManager();
|
---|
71 | if (manager instanceof SwingImageManager) {
|
---|
72 | return (SwingImageManager) manager;
|
---|
73 | }
|
---|
74 |
|
---|
75 | return null;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Returns the SwingFontManager if it is the FontManager for this session,
|
---|
80 | * or null if another FontManager is being used.
|
---|
81 | */
|
---|
82 | public static SwingFontManager getIfUsingSwingFontManager()
|
---|
83 | {
|
---|
84 | FontManager manager = EcosystemManager.getFontManager();
|
---|
85 | if (manager instanceof SwingFontManager) {
|
---|
86 | return (SwingFontManager) manager;
|
---|
87 | }
|
---|
88 |
|
---|
89 | return null;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Returns the SwingGraphicsManager if it is the GraphicsManager for this session,
|
---|
94 | * or null if another GraphicsManager is being used.
|
---|
95 | */
|
---|
96 | public static SwingGraphicsManager getIfUsingSwingGraphicsManager()
|
---|
97 | {
|
---|
98 | GraphicsManager manager = EcosystemManager.getGraphicsManager();
|
---|
99 | if (manager instanceof SwingGraphicsManager) {
|
---|
100 | return (SwingGraphicsManager) manager;
|
---|
101 | }
|
---|
102 |
|
---|
103 | return null;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Returns the SwingTextLayoutManager if it is the TextLayoutManager for this session,
|
---|
108 | * or null if another TextLayoutManager is being used.
|
---|
109 | */
|
---|
110 | public static SwingTextLayoutManager getIfUsingSwingTextLayoutManager()
|
---|
111 | {
|
---|
112 | TextLayoutManager manager = EcosystemManager.getTextLayoutManager();
|
---|
113 | if (manager instanceof SwingTextLayoutManager) {
|
---|
114 | return (SwingTextLayoutManager) manager;
|
---|
115 | }
|
---|
116 |
|
---|
117 | return null;
|
---|
118 | }
|
---|
119 |
|
---|
120 | public static org.expeditee.core.Image getImageForJavaFXImage(javafx.scene.image.Image image)
|
---|
121 | {
|
---|
122 | BufferedImage swingImage = SwingFXUtils.fromFXImage(image, null);
|
---|
123 |
|
---|
124 | return getIfUsingSwingImageManager().createImage(swingImage);
|
---|
125 | }
|
---|
126 |
|
---|
127 | @Override
|
---|
128 | public void beep()
|
---|
129 | {
|
---|
130 | java.awt.Toolkit.getDefaultToolkit().beep();
|
---|
131 | }
|
---|
132 |
|
---|
133 | @Override
|
---|
134 | public boolean browse(String url)
|
---|
135 | {
|
---|
136 | try {
|
---|
137 | URL check = new URL(url); // TODO: Why do we need this? cts16
|
---|
138 | Desktop.getDesktop().browse(new URI(url));
|
---|
139 | } catch (IOException | URISyntaxException e) {
|
---|
140 | return false;
|
---|
141 | }
|
---|
142 |
|
---|
143 | return true;
|
---|
144 | }
|
---|
145 |
|
---|
146 | @Override
|
---|
147 | public String print(String file)
|
---|
148 | {
|
---|
149 | try {
|
---|
150 | if (Util.isMinimumVersion6()) {
|
---|
151 | if (Desktop.isDesktopSupported()) {
|
---|
152 | Desktop.getDesktop().print(new File(file));
|
---|
153 | }
|
---|
154 | }
|
---|
155 | } catch (Exception e) {
|
---|
156 | return e.getMessage();
|
---|
157 | }
|
---|
158 |
|
---|
159 | return null;
|
---|
160 | }
|
---|
161 |
|
---|
162 | @Override
|
---|
163 | public String open(String file)
|
---|
164 | {
|
---|
165 | try {
|
---|
166 | if (Util.isMinimumVersion6()) {
|
---|
167 | if (Desktop.isDesktopSupported()) {
|
---|
168 | Desktop.getDesktop().open(new File(file));
|
---|
169 | }
|
---|
170 | }
|
---|
171 | } catch (Exception e) {
|
---|
172 | return e.getStackTrace().toString();
|
---|
173 | }
|
---|
174 |
|
---|
175 | return null;
|
---|
176 | }
|
---|
177 |
|
---|
178 | @Override
|
---|
179 | protected void scheduleRunnable(Runnable routine)
|
---|
180 | {
|
---|
181 | SwingUtilities.invokeLater(routine);
|
---|
182 | }
|
---|
183 |
|
---|
184 | @Override
|
---|
185 | protected boolean isGIOThread()
|
---|
186 | {
|
---|
187 | return SwingUtilities.isEventDispatchThread();
|
---|
188 | }
|
---|
189 | }
|
---|