001: /*
002: * Copyright (c) 2005-2008 Substance Kirill Grouchnikov. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of Substance Kirill Grouchnikov nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package org.jvnet.substance;
031:
032: import java.awt.Graphics;
033: import java.util.Iterator;
034: import java.util.Set;
035:
036: import javax.swing.JComponent;
037: import javax.swing.plaf.ComponentUI;
038: import javax.swing.plaf.basic.BasicMenuBarUI;
039:
040: import org.jvnet.lafwidget.*;
041: import org.jvnet.lafwidget.layout.TransitionLayout;
042: import org.jvnet.lafwidget.menu.MenuSearchWidget;
043: import org.jvnet.substance.painter.decoration.DecorationAreaType;
044: import org.jvnet.substance.painter.decoration.SubstanceDecorationUtilities;
045: import org.jvnet.substance.utils.MemoryAnalyzer;
046:
047: /**
048: * UI for menu bars in <b>Substance</b> look and feel.
049: *
050: * @author Kirill Grouchnikov
051: */
052: public class SubstanceMenuBarUI extends BasicMenuBarUI {
053: /**
054: * Set of registered widgets. This UI delegate maintains the widget set on
055: * its own since it needs to reset the relevant ({@link Resettable})
056: * widgets on theme change and on locale change.
057: */
058: protected Set<?> lafWidgets;
059:
060: /**
061: * Delegate for painting the background.
062: */
063: // private static SubstanceFillBackgroundDelegate backgroundDelegate = new
064: // SubstanceFillBackgroundDelegate();
065: // /**
066: // * Locale change listener. Resets all {@link Resettable} widgets.
067: // */
068: // private LocaleChangeListener substanceLocaleChangeListener;
069: //
070: // /**
071: // * Theme change listener. Resets all {@link Resettable} widgets.
072: // */
073: // private ThemeChangeListener substanceThemeChangeListener;
074: /**
075: * Hides search panels on all menu bars (both JFrames and JInternalFrames).
076: *
077: * @param toRepaint
078: * Indication whether the relevant menu bars should be repainted.
079: */
080: public static void hideSearchPanels(final boolean toRepaint) {
081: MenuSearchWidget.hideSearchPanels(toRepaint);
082: }
083:
084: /**
085: * Shows search panels on all menu bars (both JFrames and JInternalFrames).
086: */
087: public static void showSearchPanels() {
088: MenuSearchWidget.showSearchPanels();
089: }
090:
091: /*
092: * (non-Javadoc)
093: *
094: * @see javax.swing.plaf.ComponentUI#createUI(javax.swing.JComponent)
095: */
096: public static ComponentUI createUI(JComponent c) {
097: if (MemoryAnalyzer.isRunning()) {
098: MemoryAnalyzer
099: .enqueueUsage("SubstanceMenuBarUI to be created");
100: }
101: return new SubstanceMenuBarUI();
102: }
103:
104: /*
105: * (non-Javadoc)
106: *
107: * @see javax.swing.plaf.basic.BasicMenuBarUI#installListeners()
108: */
109: @Override
110: protected void installListeners() {
111: super .installListeners();
112:
113: // this.substanceThemeChangeListener = new ThemeChangeListener() {
114: // public void themeChanged() {
115: // for (Iterator<?> it = lafWidgets.iterator(); it.hasNext();) {
116: // LafWidget lw = (LafWidget) it.next();
117: // if (lw instanceof Resettable) {
118: // ((Resettable) lw).reset();
119: // }
120: // }
121: // }
122: // };
123: // SubstanceLookAndFeel
124: // .registerThemeChangeListener(this.substanceThemeChangeListener);
125: //
126: // substanceLocaleChangeListener = new LocaleChangeListener() {
127: // public void localeChanged() {
128: // for (Iterator<?> it = lafWidgets.iterator(); it.hasNext();) {
129: // LafWidget lw = (LafWidget) it.next();
130: // if (lw instanceof Resettable) {
131: // ((Resettable) lw).reset();
132: // }
133: // }
134: // }
135: // };
136: // SubstanceLookAndFeel
137: // .registerLocaleChangeListener(substanceLocaleChangeListener);
138:
139: // Installs listeners on all widgets
140: for (Iterator<?> it = this .lafWidgets.iterator(); it.hasNext();) {
141: LafWidget lw = (LafWidget) it.next();
142: lw.installListeners();
143: }
144:
145: }
146:
147: /*
148: * (non-Javadoc)
149: *
150: * @see javax.swing.plaf.basic.BasicMenuBarUI#uninstallListeners()
151: */
152: @Override
153: protected void uninstallListeners() {
154: // SubstanceLookAndFeel
155: // .unregisterThemeChangeListener(this.substanceThemeChangeListener);
156: // this.substanceThemeChangeListener = null;
157: //
158: // SubstanceLookAndFeel
159: // .unregisterLocaleChangeListener(this.substanceLocaleChangeListener);
160: // this.substanceLocaleChangeListener = null;
161:
162: super .uninstallListeners();
163:
164: // Uninstalls defaults on all widgets
165: for (Iterator<?> it = this .lafWidgets.iterator(); it.hasNext();) {
166: LafWidget lw = (LafWidget) it.next();
167: lw.uninstallDefaults();
168: }
169: }
170:
171: /*
172: * (non-Javadoc)
173: *
174: * @see javax.swing.plaf.basic.BasicMenuBarUI#installUI(javax.swing.JComponent)
175: */
176: @Override
177: public void installUI(JComponent c) {
178: // Fetches all widgets that were registered for the
179: // specified component
180: this .lafWidgets = LafWidgetRepository.getRepository()
181: .getMatchingWidgets(c);
182: super .installUI(c);
183:
184: // Installs all widgets
185: for (Iterator<?> it = this .lafWidgets.iterator(); it.hasNext();) {
186: LafWidget lw = (LafWidget) it.next();
187: lw.installUI();
188: }
189: }
190:
191: /*
192: * (non-Javadoc)
193: *
194: * @see javax.swing.plaf.basic.BasicMenuBarUI#installDefaults()
195: */
196: @Override
197: protected void installDefaults() {
198: super .installDefaults();
199:
200: // Installs defaults on all widgets
201: for (Iterator<?> it = this .lafWidgets.iterator(); it.hasNext();) {
202: LafWidget lw = (LafWidget) it.next();
203: lw.installDefaults();
204: }
205:
206: SubstanceDecorationUtilities.setDecorationType(this .menuBar,
207: DecorationAreaType.HEADER);
208: }
209:
210: /*
211: * (non-Javadoc)
212: *
213: * @see javax.swing.plaf.basic.BasicMenuBarUI#uninstallUI(javax.swing.JComponent)
214: */
215: @Override
216: public void uninstallUI(JComponent c) {
217: // System.out.println("Uninstalling MenuBarUI " + this.menuBar);
218: if (this .menuBar == null)
219: return;
220:
221: super .uninstallUI(c);
222:
223: // Uninstalls all widgets
224: for (Iterator<?> it = this .lafWidgets.iterator(); it.hasNext();) {
225: LafWidget lw = (LafWidget) it.next();
226: lw.uninstallUI();
227: }
228: }
229:
230: /*
231: * (non-Javadoc)
232: *
233: * @see javax.swing.plaf.basic.BasicMenuBarUI#uninstallDefaults()
234: */
235: @Override
236: protected void uninstallDefaults() {
237: SubstanceDecorationUtilities.clearDecorationType(this .menuBar);
238:
239: super .uninstallDefaults();
240:
241: // Uninstalls defaults on all widgets
242: for (Iterator<?> it = this .lafWidgets.iterator(); it.hasNext();) {
243: LafWidget lw = (LafWidget) it.next();
244: lw.uninstallDefaults();
245: }
246: }
247:
248: /*
249: * (non-Javadoc)
250: *
251: * @see javax.swing.plaf.ComponentUI#update(java.awt.Graphics,
252: * javax.swing.JComponent)
253: */
254: @Override
255: public void update(Graphics g, JComponent c) {
256: boolean isOpaque = TransitionLayout.isOpaque(c);
257: if (isOpaque) {
258: SubstanceFillBackgroundDelegate.GLOBAL_INSTANCE.update(g,
259: c, false);
260: } else {
261: super .update(g, c);
262: }
263: // GhostPaintingUtils.paintGhostImages(c, g);
264: }
265:
266: /**
267: * Returns the installed widgets.
268: *
269: * @return Installed widgets.
270: */
271: public Set<?> getLafWidgets() {
272: return lafWidgets;
273: }
274: }
|