Index: src/main/java/common/org/apache/harmony/x/swing/internal/nls/messages.properties =================================================================== --- src/main/java/common/org/apache/harmony/x/swing/internal/nls/messages.properties (revision 555009) +++ src/main/java/common/org/apache/harmony/x/swing/internal/nls/messages.properties (working copy) @@ -224,3 +224,10 @@ swing.err.1A=Lexical error at line {0}, column {1}. Encountered: swing.err.1B=after : \ swing.err.1C=Cannot calculate ColorType. Key is: +swing.err.1D=Please specify InputStream and resourceBase for SynthLookAndFeel +swing.err.1E=Please, specify font name +swing.err.1F=Property type is incorrect: {0} +swing.err.20=Direction description for imagePainter is incorrect +swing.err.21=State value is incorrect: {0} +swing.err.22=Bind type={0} is incorrect for styleID={1} +swing.err.23=Please specify imagePath and sourceInsets for imagePainter Index: src/main/java/common/javax/swing/plaf/synth/ColorType.java =================================================================== --- src/main/java/common/javax/swing/plaf/synth/ColorType.java (revision 555009) +++ src/main/java/common/javax/swing/plaf/synth/ColorType.java (working copy) @@ -80,6 +80,10 @@ */ static ColorType calculateColorType(String key) { + if (key == null) { + return null; + } + key = key.toUpperCase().intern(); if (key == "BACKGROUND") { //$NON-NLS-1$ @@ -93,7 +97,6 @@ } else if (key == "FOCUS") { //$NON-NLS-1$ return FOCUS; } - throw new IllegalStateException(Messages.getString("swing.err.1C") //$NON-NLS-1$ + key); } Index: src/main/java/common/javax/swing/plaf/synth/SynthConstants.java =================================================================== --- src/main/java/common/javax/swing/plaf/synth/SynthConstants.java (revision 555009) +++ src/main/java/common/javax/swing/plaf/synth/SynthConstants.java (working copy) @@ -24,7 +24,7 @@ */ public interface SynthConstants { - /** The component (usual a button) is marked as default (additional) */ + /** The component (a JButton) is marked as default (additional) */ static int DEFAULT = 1024; /** The component is disabled (primary) */ Index: src/main/java/common/javax/swing/plaf/synth/Region.java =================================================================== --- src/main/java/common/javax/swing/plaf/synth/Region.java (revision 555009) +++ src/main/java/common/javax/swing/plaf/synth/Region.java (working copy) @@ -210,9 +210,10 @@ */ static Region getRegionFromUIID(String ui) { + // Possible improvement: // This method creates additional object(substring) but possibly faster // way is the creating two HashMaps that contains the same values but - // the different keys: ui and name + // the different keys: UI and name return regionsMap.get(ui.substring(0, (ui.length() - 2))); } Index: src/main/java/common/javax/swing/plaf/synth/SynthStyle.java =================================================================== --- src/main/java/common/javax/swing/plaf/synth/SynthStyle.java (revision 555009) +++ src/main/java/common/javax/swing/plaf/synth/SynthStyle.java (working copy) @@ -30,30 +30,34 @@ public abstract class SynthStyle { /** - * The default isOpaque value + * Null style used by StyleFactory to represent styles that not found */ - private boolean isOpaque = true; + static final SynthStyle NULL_STYLE = new SynthStyle() { - /** - * The default graphics utils - */ - private static final SynthGraphicsUtils gUtils = new SynthGraphicsUtils(); + @Override + public SynthPainter getPainter(SynthContext context) { + return new SynthPainter() { + // Empty: Null painter nothing paints + }; + } - /** - * The default Insets - */ - private static final Insets insets = new Insets(0, 0, 0, 0); + @Override + protected Color getColorForState(SynthContext context, ColorType type) { + return null; + } - static int getCommonComponentState(JComponent c) { - int result = c.isEnabled() ? SynthConstants.ENABLED - : SynthConstants.DISABLED; - if (c.isFocusOwner()) { - result |= SynthConstants.FOCUSED; + @Override + protected Font getFontForState(SynthContext context) { + return null; } - return result; - } + }; /** + * The default isOpaque value + */ + private boolean isOpaque = true; + + /** * The default implementation returns null */ @SuppressWarnings("unused") @@ -72,39 +76,36 @@ : defaultValue; } - /** - * Verifies the colors defined in component than calls getColorForState - */ public Color getColor(SynthContext context, ColorType type) { - JComponent c = context.getComponent(); - if (c.isEnabled()) { + Color result = getColorForState(context, type); + + if (result == null) { + + JComponent c = context.getComponent(); + if (type == ColorType.BACKGROUND) { if (c.getBackground() != null) { - return c.getBackground(); + result = c.getBackground(); } } else if (type == ColorType.FOREGROUND) { if (c.getForeground() != null) { - return c.getForeground(); + result = c.getForeground(); } } } - return getColorForState(context, type); + return result; } public Font getFont(SynthContext context) { - Font result = context.getComponent().getFont(); + Font result = getFontForState(context); - if (result == null) { - return getFontForState(context); - } - - return result; + return (result == null) ? context.getComponent().getFont() : result; } protected abstract Font getFontForState(SynthContext context); @@ -127,14 +128,17 @@ } + /** + * The default implementation returns empty insets + */ @SuppressWarnings("unused") public Insets getInsets(SynthContext context, Insets modified) { if (modified == null) { - return (Insets) insets.clone(); + return new Insets(0, 0, 0, 0); } - modified.set(insets.top, insets.left, insets.bottom, insets.right); + modified.set(0, 0, 0, 0); return modified; } @@ -150,8 +154,8 @@ /** * @return The default implementation returns null */ - public SynthPainter getPainter(@SuppressWarnings("unused") - SynthContext context) { + @SuppressWarnings("unused") + public SynthPainter getPainter(SynthContext context) { return null; } @@ -187,6 +191,7 @@ */ public SynthGraphicsUtils getGraphicsUtils(@SuppressWarnings("unused") SynthContext context) { - return gUtils; + return new SynthGraphicsUtils(); } + } Index: src/main/java/common/javax/swing/plaf/synth/SynthGraphicsUtils.java =================================================================== --- src/main/java/common/javax/swing/plaf/synth/SynthGraphicsUtils.java (revision 555009) +++ src/main/java/common/javax/swing/plaf/synth/SynthGraphicsUtils.java (working copy) @@ -76,17 +76,15 @@ @SuppressWarnings("unused") public void drawLine(SynthContext context, Object paintKey, Graphics g, int x1, int y1, int x2, int y2) { - Color oldColor = g.getColor(); g.setColor(context.getStyle().getColor(context, ColorType.FOREGROUND)); g.drawLine(x1, y1, x2, y2); - g.setColor(oldColor); } /** * Calculates the maximum size for the component with given parameters */ @SuppressWarnings("unused") - public Dimension getMaximumSize(SynthContext ss, Font font, String text, + public Dimension getPreferredSize(SynthContext ss, Font font, String text, Icon icon, int hAlign, int vAlign, int hTextPosition, int vTextPosition, int iconTextGap, int mnemonicIndex) { @@ -105,18 +103,18 @@ Icon icon, int hAlign, int vAlign, int hTextPosition, int vTextPosition, int iconTextGap, int mnemonicIndex) { - return getMaximumSize(ss, font, text, icon, hAlign, vAlign, + return getPreferredSize(ss, font, text, icon, hAlign, vAlign, hTextPosition, vTextPosition, iconTextGap, mnemonicIndex); } /** * Calculates the preferred size for the component with given parameters */ - public Dimension getPreferredSize(SynthContext ss, Font font, String text, + public Dimension getMaximumSize(SynthContext ss, Font font, String text, Icon icon, int hAlign, int vAlign, int hTextPosition, int vTextPosition, int iconTextGap, int mnemonicIndex) { - return getMaximumSize(ss, font, text, icon, hAlign, vAlign, + return getPreferredSize(ss, font, text, icon, hAlign, vAlign, hTextPosition, vTextPosition, iconTextGap, mnemonicIndex); } @@ -126,7 +124,7 @@ public String layoutText(SynthContext ss, FontMetrics fm, String text, Icon icon, int hAlign, int vAlign, int hTextPosition, int vTextPosition, Rectangle viewR, Rectangle iconR, - final Rectangle textR, int iconTextGap) { + Rectangle textR, int iconTextGap) { fm = computeFontMetrics(ss, fm); @@ -153,11 +151,11 @@ layoutText(ss, metrics, text, icon, hAlign, vAlign, hTextPosition, vTextPosition, viewR, iconR, textR, iconTextGap); - textR.x += textOffset; - textR.y += textOffset; + textR.translate(textOffset, textOffset); ButtonCommons.paintText(g, metrics, text, mnemonicIndex, textR, text, color); + } public void paintText(SynthContext ss, Graphics g, String text, int x, @@ -225,7 +223,10 @@ if (!Utilities.isEmptyString(text)) { Dimension stringDim = Utilities.getStringSize(text, fm); - return new Rectangle(stringDim.width, Utilities.getTextY(fm, new Rectangle(stringDim))); + Dimension size = new Dimension(stringDim.width, Utilities.getTextY( + fm, new Rectangle(stringDim))); + + return new Rectangle(size); } return new Rectangle(); @@ -243,6 +244,7 @@ SwingUtilities.layoutCompoundLabel(fm, text, icon, SwingConstants.TOP, SwingConstants.LEFT, verticalTextPosition, horizontalTextPosition, viewR, iconR, textR, iconTextGap); + result.width = Math.max(iconR.x + iconR.width, textR.x + textR.width); result.height = Math .max(iconR.y + iconR.height, textR.y + textR.height); Index: src/main/java/common/javax/swing/plaf/synth/SynthLookAndFeel.java =================================================================== --- src/main/java/common/javax/swing/plaf/synth/SynthLookAndFeel.java (revision 555009) +++ src/main/java/common/javax/swing/plaf/synth/SynthLookAndFeel.java (working copy) @@ -18,6 +18,7 @@ package javax.swing.plaf.synth; import java.awt.Component; +import java.io.IOException; import java.io.InputStream; import java.io.Serializable; import java.text.ParseException; @@ -26,13 +27,19 @@ import javax.swing.UIDefaults; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.basic.BasicLookAndFeel; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; -import org.apache.harmony.luni.util.NotImplementedException; import org.apache.harmony.x.swing.internal.nls.Messages; +import org.xml.sax.SAXException; +import org.apache.harmony.luni.util.NotImplementedException; + public class SynthLookAndFeel extends BasicLookAndFeel implements Serializable { - private final static String packageName = "javax.swing.plaf.synth"; + /** Path used in UIDefaults */ + private final static String pathToSynthLaf = "javax.swing.plaf.synth.SynthLookAndFeel"; //$NON-NLS-1$ private static SynthStyleFactory currentFactory; @@ -53,31 +60,116 @@ } /** - * Creates the Synth UI object corresponds JComponent given + * Creates the Synth UI object corresponds JComponent given. (This method + * used by UIManager because all the UIs classes are package-protected + * according to spec, so reference in defaults table points to + * SynthLookAndFeel) + * + * @see SynthLookAndFeel#initClassDefaults(UIDefaults) */ - public static ComponentUI createUI(JComponent c) { + @SuppressWarnings("nls") + public static ComponentUI createUI(JComponent c) + throws NotImplementedException { - try { + // Commented because UI's patch is not ready for now - return (ComponentUI) Class.forName(packageName + c.getUIClassID(), - true, Thread.currentThread().getContextClassLoader()) - .getMethod("createUI", JComponent.class).invoke(null, c); //$NON-NLS-1$ - - } catch (Exception e) { - - return null; - } + // String uiClassID = c.getUIClassID().intern(); + // + // if (uiClassID == "InternalFrameUI") { + // return SynthInternalFrameUI.createUI(c); + // } else if (uiClassID == "ViewportUI") { + // return SynthViewportUI.createUI(c); + // } else if (uiClassID == "ScrollBarUI") { + // SynthScrollBarUI.createUI(c); + // } else if (uiClassID == "ToolTipUI") { + // return SynthToolTipUI.createUI(c); + // } else if (uiClassID == "MenuItemUI") { + // return SynthMenuItemUI.createUI(c); + // } else if (uiClassID == "MenuUI") { + // return SynthMenuUI.createUI(c); + // } else if (uiClassID == "TextAreaUI") { + // return SynthTextAreaUI.createUI(c); + // } else if (uiClassID == "PopupMenuUI") { + // return SynthPopupMenuUI.createUI(c); + // } else if (uiClassID == "ScrollPaneUI") { + // return SynthScrollPaneUI.createUI(c); + // } else if (uiClassID == "SliderUI") { + // return SynthSliderUI.createUI(c); + // } else if (uiClassID == "ComboBoxUI") { + // return SynthComboBoxUI.createUI(c); + // } else if (uiClassID == "RadioButtonUI") { + // return SynthRadioButtonUI.createUI(c); + // } else if (uiClassID == "FormattedTextFieldUI") { + // return SynthFormattedTextFieldUI.createUI(c); + // } else if (uiClassID == "TreeUI") { + // return SynthTreeUI.createUI(c); + // } else if (uiClassID == "MenuBarUI") { + // return SynthMenuBarUI.createUI(c); + // } else if (uiClassID == "RadioButtonMenuItemUI") { + // return SynthRadioButtonMenuItemUI.createUI(c); + // } else if (uiClassID == "ProgressBarUI") { + // return SynthProgressBarUI.createUI(c); + // } else if (uiClassID == "ToolBarUI") { + // return SynthToolBarUI.createUI(c); + // } else if (uiClassID == "ColorChooserUI") { + // return SynthColorChooserUI.createUI(c); + // } else if (uiClassID == "ToolBarSeparatorUI") { + // return SynthToolBarSeparatorUI.createUI(c); + // } else if (uiClassID == "TabbedPaneUI") { + // return SynthTabbedPaneUI.createUI(c); + // } else if (uiClassID == "DesktopPaneUI") { + // return SynthDesktopPaneUI.createUI(c); + // } else if (uiClassID == "TableUI") { + // return SynthTableUI.createUI(c); + // } else if (uiClassID == "PanelUI") { + // return SynthPanelUI.createUI(c); + // } else if (uiClassID == "CheckBoxMenuItemUI") { + // return SynthCheckBoxMenuItemUI.createUI(c); + // } else if (uiClassID == "PasswordFieldUI") { + // return SynthPasswordFieldUI.createUI(c); + // } else if (uiClassID == "CheckBoxUI") { + // return SynthCheckBoxUI.createUI(c); + // } else if (uiClassID == "TableHeaderUI") { + // return SynthTableHeaderUI.createUI(c); + // } else if (uiClassID == "SplitPaneUI") { + // return SynthSplitPaneUI.createUI(c); + // } else if (uiClassID == "EditorPaneUI") { + // return SynthEditorPaneUI.createUI(c); + // } else if (uiClassID == "ListUI") { + // return SynthListUI.createUI(c); + // } else if (uiClassID == "SpinnerUI") { + // return SynthSpinnerUI.createUI(c); + // } else if (uiClassID == "DesktopIconUI") { + // return SynthDesktopIconUI.createUI(c); + // } else if (uiClassID == "TextFieldUI") { + // return SynthTextFieldUI.createUI(c); + // } else if (uiClassID == "TextPaneUI") { + // return SynthTextPaneUI.createUI(c); + // } else if (uiClassID == "ButtonUI") { + // return SynthButtonUI.createUI(c); + // } else if (uiClassID == "LabelUI") { + // return SynthLabelUI.createUI(c); + // } else if (uiClassID == "ToggleButtonUI") { + // SynthToggleButtonUI.createUI(c); + // } else if (uiClassID == "OptionPaneUI") { + // return SynthOptionPaneUI.createUI(c); + // } else if (uiClassID == "PopupMenuSeparatorUI") { + // return SynthPopupMenuSeparatorUI.createUI(c); + // } else if (uiClassID == "RootPaneUI") { + // return SynthRootPaneUI.createUI(c); + // } else if (uiClassID == "SeparatorUI") { + // return SynthSeparatorUI.createUI(c); + // } + // compatible with RI + return null; } /** * Renew the synth styles for the JComponent. This method isn't fully - * correct, but does what needs + * correct, but does what needs (The method is unused in package) */ public static void updateStyles(Component c) { - - if (c instanceof JComponent) { - ((JComponent) c).revalidate(); - } + c.setName(c.getName() + " "); //$NON-NLS-1$ } /** @@ -142,11 +234,26 @@ @SuppressWarnings("unused") public void load(InputStream input, Class resourceBase) - throws NotImplementedException, ParseException, - IllegalArgumentException { - /* - * This class will be implemented with XMLSynthParser - */ + throws ParseException, IllegalArgumentException { + + if (input == null || resourceBase == null) { + throw new IllegalArgumentException(Messages + .getString("swing.err.1D")); //$NON-NLS-1$ + } + + try { + + SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); + saxParser.parse(input, new XMLSynthParser(resourceBase)); + + } catch (ParserConfigurationException e) { + throw new ParseException(e.getMessage(), 0); + } catch (SAXException e) { + e.printStackTrace(); + throw new ParseException(e.getMessage(), 0); + } catch (IOException e) { + throw new ParseException(e.getMessage(), 0); + } } /** The default implementation returns false */ @@ -158,49 +265,30 @@ @SuppressWarnings("nls") @Override protected void initClassDefaults(UIDefaults defaults) { - Object[] initDefaults = { "InternalFrameUI", - packageName + "SynthInternalFrameUI", "ViewportUI", - packageName + "SynthViewportUI", "ScrollBarUI", - packageName + "SynthScrollBarUI", "ToolTipUI", - packageName + "SynthToolTipUI", "MenuItemUI", - packageName + "SynthMenuItemUI", "MenuUI", - packageName + "SynthMenuUI", "TextAreaUI", - packageName + "SynthTextAreaUI", "PopupMenuUI", - packageName + "SynthPopupMenuUI", "ScrollPaneUI", - packageName + "SynthScrollPaneUI", "SliderUI", - packageName + "SynthSliderUI", "ComboBoxUI", - packageName + "SynthComboBoxUI", "RadioButtonUI", - packageName + "SynthRadioButtonUI", "FormattedTextFieldUI", - packageName + "SynthFormattedTextFieldUI", "TreeUI", - packageName + "SynthTreeUI", "MenuBarUI", - packageName + "SynthMenuBarUI", "RadioButtonMenuItemUI", - packageName + "SynthRadioButtonMenuItemUI", "ProgressBarUI", - packageName + "SynthProgressBarUI", "ToolBarUI", - packageName + "SynthToolBarUI", "ColorChooserUI", - packageName + "SynthColorChooserUI", "ToolBarSeparatorUI", - packageName + "SynthToolBarSeparatorUI", "TabbedPaneUI", - packageName + "SynthTabbedPaneUI", "DesktopPaneUI", - packageName + "SynthDesktopPaneUI", "TableUI", - packageName + "SynthTableUI", "PanelUI", - packageName + "SynthPanelUI", "CheckBoxMenuItemUI", - packageName + "SynthCheckBoxMenuItemUI", "PasswordFieldUI", - packageName + "SynthPasswordFieldUI", "CheckBoxUI", - packageName + "SynthCheckBoxUI", "TableHeaderUI", - packageName + "SynthTableHeaderUI", "SplitPaneUI", - packageName + "SynthSplitPaneUI", "EditorPaneUI", - packageName + "SynthEditorPaneUI", "ListUI", - packageName + "SynthListUI", "SpinnerUI", - packageName + "SynthSpinnerUI", "DesktopIconUI", - packageName + "SynthDesktopIconUI", "TextFieldUI", - packageName + "SynthTextFieldUI", "TextPaneUI", - packageName + "SynthTextPaneUI", "LabelUI", - packageName + "SynthLabelUI", "ButtonUI", - packageName + "SynthButtonUI", "ToggleButtonUI", - packageName + "SynthToggleButtonUI", "OptionPaneUI", - packageName + "SynthOptionPaneUI", "PopupMenuSeparatorUI", - packageName + "SynthPopupMenuSeparatorUI", "RootPaneUI", - packageName + "SynthRootPaneUI", "SeparatorUI", - packageName + "SynthSeparatorUI" }; + Object[] initDefaults = { "InternalFrameUI", pathToSynthLaf, + "ViewportUI", pathToSynthLaf, "ScrollBarUI", pathToSynthLaf, + "ToolTipUI", pathToSynthLaf, "MenuItemUI", pathToSynthLaf, + "MenuUI", pathToSynthLaf, "TextAreaUI", pathToSynthLaf, + "PopupMenuUI", pathToSynthLaf, "ScrollPaneUI", pathToSynthLaf, + "SliderUI", pathToSynthLaf, "ComboBoxUI", pathToSynthLaf, + "RadioButtonUI", pathToSynthLaf, "FormattedTextFieldUI", + pathToSynthLaf, "TreeUI", pathToSynthLaf, "MenuBarUI", + pathToSynthLaf, "RadioButtonMenuItemUI", pathToSynthLaf, + "ProgressBarUI", pathToSynthLaf, "ToolBarUI", pathToSynthLaf, + "ColorChooserUI", pathToSynthLaf, "ToolBarSeparatorUI", + pathToSynthLaf, "TabbedPaneUI", pathToSynthLaf, + "DesktopPaneUI", pathToSynthLaf, "TableUI", pathToSynthLaf, + "PanelUI", pathToSynthLaf, "CheckBoxMenuItemUI", + pathToSynthLaf, "PasswordFieldUI", pathToSynthLaf, + "CheckBoxUI", pathToSynthLaf, "TableHeaderUI", pathToSynthLaf, + "SplitPaneUI", pathToSynthLaf, "EditorPaneUI", pathToSynthLaf, + "ListUI", pathToSynthLaf, "SpinnerUI", pathToSynthLaf, + "DesktopIconUI", pathToSynthLaf, "TextFieldUI", pathToSynthLaf, + "TextPaneUI", pathToSynthLaf, "ButtonUI", pathToSynthLaf, + "LabelUI", pathToSynthLaf, "ToggleButtonUI", pathToSynthLaf, + "OptionPaneUI", pathToSynthLaf, "PopupMenuSeparatorUI", + pathToSynthLaf, "RootPaneUI", pathToSynthLaf, "SeparatorUI", + pathToSynthLaf }; defaults.putDefaults(initDefaults); } Index: src/main/java/common/javax/swing/plaf/synth/SynthContext.java =================================================================== --- src/main/java/common/javax/swing/plaf/synth/SynthContext.java (revision 555009) +++ src/main/java/common/javax/swing/plaf/synth/SynthContext.java (working copy) @@ -125,4 +125,16 @@ static boolean isSelected(int verifiedState) { return (verifiedState & SynthConstants.SELECTED) != 0; } + + /** + * Used in UI's. Created to reduce code doubling + * */ + static int getCommonComponentState(JComponent c) { + int result = c.isEnabled() ? SynthConstants.ENABLED + : SynthConstants.DISABLED; + if (c.isFocusOwner()) { + result |= SynthConstants.FOCUSED; + } + return result; + } }