001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.gui;
019:
020: import java.awt.Color;
021: import java.util.Iterator;
022: import java.util.Map;
023: import java.util.logging.Logger;
024:
025: import javax.swing.JComponent;
026: import javax.swing.border.Border;
027: import javax.swing.border.LineBorder;
028: import javax.swing.border.TitledBorder;
029:
030: import de.finix.contelligent.client.base.ComponentFactory;
031: import de.finix.contelligent.client.base.category.ContelligentCategoryManager;
032: import de.finix.contelligent.client.event.ContelligentEventDispatcher;
033: import de.finix.contelligent.client.event.ResourceEventListener;
034: import de.finix.contelligent.client.event.ResourceModifiedEvent;
035: import de.finix.contelligent.client.i18n.Resources;
036:
037: public abstract class AbstractComponentResourceEditor extends
038: AbstractComponentEditor implements ResourceEventListener {
039:
040: private static Logger logger = Logger
041: .getLogger(AbstractComponentResourceEditor.class.getName());
042:
043: protected Color inheritedForeground = Color.lightGray;
044:
045: protected Color defaultForeground;
046:
047: protected JComponent resourceComponent;
048:
049: public AbstractComponentResourceEditor() {
050: super ();
051: ComponentFactory.getInstance().addResourceEventListener(this ,
052: ContelligentEventDispatcher.DOES_USE_SWING);
053: }
054:
055: public void onResourceModified(ResourceModifiedEvent event) {
056: if (event.getSource() != this
057: && event.getTarget().equals(getComponent().getPath())
058: && event.getMode() == getGUI().getResourceMode()
059: && (getGUI().isResourceInherited() || event
060: .getIdentifier().equals(
061: getGUI().getResourceIdentifier()))) {
062: update(true);
063: }
064: }
065:
066: public void update() {
067: update(true);
068: }
069:
070: public void update(boolean updateResource) {
071: if (!getGUI().isResourceDefined()) {
072: resourceComponent.setBorder(UNDEFINED_BORDER);
073: } else {
074: resourceComponent.setBorder(DEFAULT_BORDER);
075: }
076: // foreground is always set to default now
077: resourceComponent.setForeground(defaultForeground);
078: if (getGUI().isResourceInherited()) {
079: Border innerBorder = new LineBorder(Color.blue, 2);
080: String id = getGUI().getFallbackResourceIdentifier();
081: Border border = new TitledBorder(innerBorder, Resources
082: .getLocalString("inherited_from")
083: + ": " + id);
084: resourceComponent.setBorder(border);
085: } else if (getGUI().getDefaultedCategoryNames().size() > 0) {
086: Border innerBorder = new LineBorder(Color.red, 2);
087: StringBuffer missingCats = new StringBuffer();
088: Iterator defaulted = getGUI().getDefaultedCategoryNames()
089: .iterator();
090: Map catMap = getGUI().getResourceCategoryMap();
091: boolean allDefaults = true;
092: while (defaulted.hasNext()) {
093: String catName = (String) defaulted.next();
094: String catValue = (String) catMap.get(catName);
095: if (missingCats.length() > 0) {
096: missingCats.append(", ");
097: }
098: missingCats.append(catName);
099: missingCats.append("=");
100: missingCats.append(catValue);
101: if (ContelligentCategoryManager.getInstance()
102: .getCategoryByName(catName).getDefaultValue()
103: .equals(catValue)) {
104: missingCats.append(" ");
105: missingCats.append(Resources
106: .getLocalString("category_default"));
107: } else {
108: allDefaults = false;
109: }
110: }
111: Border border = new TitledBorder(innerBorder, Resources
112: .getLocalString("category_defaults_for")
113: + ": " + missingCats);
114: resourceComponent.setBorder(border);
115: if (!allDefaults) {
116: resourceComponent.setBackground(Color.lightGray);
117: resourceComponent.setEnabled(false);
118: }
119: }
120: if (updateResource) {
121: displayResource();
122: }
123: }
124:
125: protected void setResourceComponent(JComponent resourceComponent) {
126: this .resourceComponent = resourceComponent;
127: defaultForeground = resourceComponent.getForeground();
128: }
129:
130: abstract protected void displayResource();
131:
132: public void commit() {
133: // Override the commit, so disabled editors wont overwrite changes from
134: // enabled ones
135: if (resourceComponent.isEnabled()) {
136: super.commit();
137: }
138: }
139:
140: }
|