001 /*
002 * Copyright 2002-2006 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.components;
017
018 import javax.swing.JLabel;
019
020 import org.springframework.richclient.application.ApplicationServices;
021 import org.springframework.richclient.application.ApplicationServicesLocator;
022 import org.springframework.richclient.core.DefaultMessage;
023 import org.springframework.richclient.core.Guarded;
024 import org.springframework.richclient.core.Message;
025 import org.springframework.richclient.core.Severity;
026 import org.springframework.richclient.dialog.Messagable;
027 import org.springframework.richclient.image.IconSource;
028 import org.springframework.richclient.util.OverlayHelper;
029
030 /**
031 * Component which can be used as an overlay for an other component to the content of a message. The severity of the
032 * message will be used to retrive an icon by using the key <code>severity.{severity.label}.overlay</code> where
033 * {severity.label} the content of {@link Severity#getLabel()} is.
034 * <p>
035 * Use {@link OverlayHelper#attachOverlay(javax.swing.JComponent, javax.swing.JComponent, int, int, int)} to put this
036 * component as an overlay of an other component
037 *
038 * @author Oliver Hutchison
039 * @author Mathias Broekelmann
040 */
041 public class MessageReportingOverlay extends JLabel implements Messagable, Guarded {
042 private IconSource iconSource;
043
044 /**
045 * Return the used icon source
046 *
047 * @return the icon source, must not null
048 */
049 public IconSource getIconSource() {
050 if (iconSource == null) {
051 iconSource = (IconSource) ApplicationServicesLocator.services().getService(IconSource.class);
052 }
053 return iconSource;
054 }
055
056 /**
057 * Define the iconsource for getting the icon of the overlay
058 *
059 * @param iconSource
060 * the icon source, if null the default icon source from {@link ApplicationServices} will be used
061 */
062 public void setIconSource(IconSource iconSource) {
063 this.iconSource = iconSource;
064 }
065
066 /**
067 * Returns whether this overlay is enabled (=visible) or not
068 */
069 public boolean isEnabled() {
070 return isVisible();
071 }
072
073 /**
074 * Defines whether this overlay is enabled (=visible) or not
075 */
076 public void setEnabled(boolean enabled) {
077 setVisible(enabled);
078 }
079
080 /**
081 * set the message wich will be used as the content of the overlay. The message text will be used as tooltip and the
082 * severity is used to determine which icon should be shown
083 *
084 * @param message
085 * the message, if null tooltip will be empty and icon will be null
086 */
087 public void setMessage(Message message) {
088 if (message == null) {
089 message = DefaultMessage.EMPTY_MESSAGE;
090 }
091 setToolTipText(message.getMessage());
092 Severity severity = message.getSeverity();
093
094 if (severity != null) {
095 setIcon(getIconSource().getIcon("severity." + severity.getLabel() + ".overlay"));
096 } else {
097 setIcon(null);
098 }
099 }
100 }