001 /* 002 * Copyright 2002-2004 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy of 006 * the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations under 014 * the License. 015 */ 016 package org.springframework.richclient.application.config; 017 018 import org.springframework.richclient.application.ApplicationException; 019 import org.springframework.util.Assert; 020 import org.springframework.util.StringUtils; 021 022 import javax.swing.*; 023 import javax.swing.UIManager.LookAndFeelInfo; 024 import java.util.Iterator; 025 import java.util.Map; 026 import java.util.Properties; 027 028 /** 029 * Configuerer for specifying global UIManager defaults. 030 * 031 * @author Keith Donald 032 */ 033 public class UIManagerConfigurer { 034 035 private static final String CROSS_PLATFORM_LOOK_AND_FEEL_NAME = "crossPlatform"; 036 037 private static final String SYSTEM_LOOK_AND_FEEL_NAME = "system"; 038 039 public UIManagerConfigurer() { 040 this(true); 041 } 042 043 public UIManagerConfigurer(boolean installPrePackagedDefaults) { 044 if (installPrePackagedDefaults) { 045 installPrePackagedUIManagerDefaults(); 046 } 047 try { 048 doInstallCustomDefaults(); 049 } 050 catch (Exception e) { 051 throw new ApplicationException("Unable to install subclass custom defaults", e); 052 } 053 } 054 055 /** 056 * Template method subclasses may override to install custom look and feels 057 * or UIManager defaults. 058 * 059 * @throws Exception 060 */ 061 protected void doInstallCustomDefaults() throws Exception { 062 063 } 064 065 /** 066 * Initializes the UIManager defaults to values based on recommended, 067 * best-practices user interface design. This should generally be called 068 * once by an initializing application class. 069 */ 070 private void installPrePackagedUIManagerDefaults() { 071 try { 072 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 073 UIManager.put("Tree.line", "Angled"); 074 UIManager.put("Tree.leafIcon", null); 075 UIManager.put("Tree.closedIcon", null); 076 UIManager.put("Tree.openIcon", null); 077 UIManager.put("Tree.rightChildIndent", new Integer(10)); 078 } 079 catch (Exception e) { 080 throw new ApplicationException("Unable to set defaults", e); 081 } 082 } 083 084 public void setProperties(Properties properties) { 085 Iterator i = properties.entrySet().iterator(); 086 while (i.hasNext()) { 087 Map.Entry entry = (Map.Entry)i.next(); 088 UIManager.put(entry.getKey(), entry.getValue()); 089 } 090 } 091 092 public void setInstallCustomLookAndFeels(String[] customLookAndFeels) { 093 for (int i = 0; i < customLookAndFeels.length; i++) { 094 String[] feels = StringUtils.commaDelimitedListToStringArray(customLookAndFeels[i]); 095 Assert.isTrue(feels.length > 0, "LookAndFeelInfo definition should be in form: [name],<classname>"); 096 String name = null; 097 String className; 098 if (feels.length == 1) { 099 className = feels[0]; 100 } 101 else if (feels.length > 1) { 102 name = feels[0]; 103 className = feels[1]; 104 } 105 else { 106 throw new RuntimeException("Should not happen"); 107 } 108 UIManager.installLookAndFeel(name, className); 109 } 110 } 111 112 public void setLookAndFeel(Class lookAndFeel) { 113 setLookAndFeel(lookAndFeel.getName()); 114 } 115 116 public void setLookAndFeel(String className) { 117 try { 118 UIManager.setLookAndFeel(className); 119 } 120 catch (Exception e) { 121 throw new ApplicationException("Unable to set look and feel", e); 122 } 123 } 124 125 public void setLookAndFeelWithName(String lookAndFeelName) { 126 try { 127 if (lookAndFeelName.equalsIgnoreCase(SYSTEM_LOOK_AND_FEEL_NAME)) { 128 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 129 } 130 else if (lookAndFeelName.equalsIgnoreCase(CROSS_PLATFORM_LOOK_AND_FEEL_NAME)) { 131 UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); 132 } 133 else { 134 LookAndFeelInfo[] feels = UIManager.getInstalledLookAndFeels(); 135 for (LookAndFeelInfo feel : feels) 136 { 137 if (feel.getName().equalsIgnoreCase(lookAndFeelName)) 138 { 139 UIManager.setLookAndFeel(feel.getClassName()); 140 break; 141 } 142 } 143 } 144 } 145 catch (Exception e) { 146 throw new ApplicationException("Unable to set look and feel", e); 147 } 148 } 149 }