📄 stylesheet.java
字号:
* at improper sizes. This method provides a hook to deal with this
* situation.
* <p>
* By default the behavior is to check {@link #isAutopackingEnabled}. If
* autopacking is enabled and the window's current size is smaller than its
* minimum size, it is resized to be at least its minimum dimensions. If
* autopacking is disabled or the window is at least its minimum size, no
* action is taken.
*
*@param window the window which has just been styled
*/
protected void processWindow(Window window) {
Dimension size = window.getMinimumSize();
if (window.getWidth() < size.width ||
window.getHeight() < size.height) {
size.width = Math.max(window.getWidth(), size.width);
size.height = Math.max(window.getHeight(), size.height);
window.setSize(size);
window.validate();
}
}
/**
* Removes this stylesheet from an object tree, removing all styled
* properties and event listeners. The root object must either implement
* {@link Styleable} or have a wrapper class registered with {@link
* TypeManager}. Has no effect if called on a styleable object which has
* not had this stylesheet applied to it.
*
*@throws IllegalArgumentException if root cannot be converted to a
* Styleable
*@throws StylesheetException if an error occurs while removing the
* stylesheet
*/
public void removeFrom(Object root) throws StylesheetException {
Styleable styleable = TypeManager.getStyleable(root);
if (debugWindow != null)
debugWindow.stylesheetRemoved(this, styleable);
PropertyManager.removeStylesheet(this, styleable);
}
/**
* Removes this stylesheet from all objects to which it has been applied,
* removing all styled properties and event listeners.
*
*@throws StylesheetException if an error occurs while removing the
* stylesheet
*/
public void removeFromAll() throws StylesheetException {
Set<Styleable> roots = new HashSet<Styleable>(getRoots().keySet());
for (Styleable r : roots) {
removeFrom(r);
}
}
/**
* Reapplies this stylesheet to all objects which it is currently affecting.
* This will cause any changes which have been made to the stylesheet to
* take effect.
*/
public void reapply() throws StylesheetException {
Set<Styleable> roots = new HashSet<Styleable>(getRoots().keySet());
for (Styleable r : roots) {
applyTo(r);
}
}
// it's a Map simply because there is no WeakHashSet
private Map<Styleable, Styleable> getRoots() {
if (roots == null)
roots = new WeakHashMap<Styleable, Styleable>();
return roots;
}
/**
* Returns the current global stylesheet. As the stylesheet object could
* then be modified and reapplied, this call requires the
* "setGlobalStylesheet" {@link AWTPermission}.
*
*@return the global stylesheet, or <code>null</code> if none
*@throws SecurityException if the required permission is not available
*@see #setGlobalStylesheet
*/
public static Stylesheet getGlobalStylesheet() throws SecurityException {
SecurityManager security = System.getSecurityManager();
if (security != null)
security.checkPermission(new AWTPermission("setGlobalStylesheet"));
return globalStylesheet;
}
/**
* Sets this stylesheet as the global stylesheet. The stylesheet will be
* automatically applied to all newly opened windows, but existing unstyled
* windows are unaffected. If there is already a global stylesheet, it will
* be removed and any windows it is currently styling will also be updated
* to match the new stylesheet.
* <p>
* It is generally preferable to manually apply stylesheets to windows
* rather than rely on <code>setGlobalStylesheet</code>. As global
* stylesheets are applied as the window is being put onscreen, sizing
* changes required by the global stylesheet (e.g. font or border properties
* cause the window's minimum size to increase), the window's rectangle may
* first appear at its "unstyled" size and then immediately snap to its
* "styled" size. Applying the stylesheet
* manually, prior to the initial <code>pack()</code> or other size
* computation, avoids this issue. However global stylesheets are an easy
* way to intercept dialogs such as those displayed by {@link
* javax.swing.JOptionPane} which are not convenient to manually style.
* <p>
* This call requires the "listenToAllAWTEvents" and "setGlobalStylesheet"
* {@link AWTPermission AWTPermissions}. A <code>null</code> parameter may
* be used to remove the global stylesheet.
*
*@param stylesheet the new global stylesheet. May be <code>null</code>.
*@throws SecurityException if the required permission is not available
*@throws StylesheetException if an error occurs applying the stylesheet
*/
public static void setGlobalStylesheet(Stylesheet stylesheet)
throws SecurityException, StylesheetException {
SecurityManager security = System.getSecurityManager();
if (security != null)
security.checkPermission(new AWTPermission("setGlobalStylesheet"));
if (globalStylesheet != null) {
Set<Styleable> oldRoots = new HashSet<Styleable>(
globalStylesheet.getRoots().keySet());
globalStylesheet.removeFromAll();
for (Styleable r : oldRoots)
stylesheet.applyTo(r);
}
globalStylesheet = stylesheet;
if (globalEventListener == null) {
globalEventListener = new AWTEventListener() {
public void eventDispatched(AWTEvent event) {
if (event.getID() == WindowEvent.WINDOW_OPENED) {
try {
Window window = ((WindowEvent) event).getWindow();
if (window != debugWindow)
globalStylesheet.applyTo(window);
}
catch (StylesheetException e) {
throw new RuntimeException(e);
}
}
}
};
Toolkit.getDefaultToolkit().addAWTEventListener(globalEventListener,
AWTEvent.WINDOW_EVENT_MASK);
};
}
/** Returns a string representation of this object. */
public String toString() {
return "Stylesheet" + Arrays.asList(rules);
}
public int getPriority() {
return 0;
}
/**
* This API is subject to change and may change or disappear in a future
* release.
* <p>
* Sets the name of this stylesheet for debugging purposes.
*/
public void setName(String name) {
if (debugWindow != null)
debugWindow.setName(this, name);
}
/**
* This API is subject to change and may change or disappear in a future
* release.
* <p>
* Displays a debug window which allows stylesheets to be edited and the
* styles in effect on individual components to be viewed. This method must
* be called before any stylesheets are created.
*/
public static void enableDebugging() {
debugWindow = new DebugWindow();
debugWindow.setVisible(true);
}
/**
* This API is subject to change and may change or disappear in a future
* release.
* <p>
* Returns the DebugWindow currently in effect, or null if debugging is not
* enabled.
*/
public static DebugWindow getDebugWindow() {
return debugWindow;
}
/**
* For test purposes only, will be removed.
*/
public static void main(String[] arg) throws Exception {
enableDebugging();
Stylesheet global = new Stylesheet();
global.setName("Global");
setGlobalStylesheet(global);
Class mainClass;
if (arg[0].endsWith(".jar")) {
File file = new File(arg[0]);
JarFile jarFile = new JarFile(file);
URLClassLoader classLoader = new URLClassLoader(
new URL[] { file.toURL() });
Attributes attributes = jarFile.getManifest().getMainAttributes();
String mainClassName = attributes.getValue("Main-Class");
mainClass = Class.forName(mainClassName, true, classLoader);
}
else
mainClass = Class.forName(arg[0]);
Method main = mainClass.getMethod("main", String[].class);
main.invoke(null, (Object) new String[0]);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -