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.BorderLayout;
021: import java.awt.Color;
022: import java.util.logging.Level;
023: import java.util.logging.Logger;
024:
025: import javax.swing.Action;
026: import javax.swing.JPanel;
027: import javax.swing.border.Border;
028: import javax.swing.border.LineBorder;
029:
030: import de.finix.contelligent.client.base.ComponentFactory;
031: import de.finix.contelligent.client.base.ComponentPath;
032: import de.finix.contelligent.client.base.ContelligentComponent;
033: import de.finix.contelligent.client.event.ComponentEventListener;
034: import de.finix.contelligent.client.event.ContelligentComponentEvent;
035: import de.finix.contelligent.client.event.ContelligentEvent;
036: import de.finix.contelligent.client.event.ContelligentEventDispatcher;
037:
038: public abstract class AbstractComponentRenderer extends JPanel
039: implements ComponentRenderer, ComponentEventListener {
040:
041: private static Logger logger = Logger
042: .getLogger(AbstractComponentEditor.class.getName());
043:
044: protected final static Border UNDEFINED_BORDER = new LineBorder(
045: Color.orange, 2);
046:
047: protected final static Border UNKNOWN_BORDER = new LineBorder(
048: Color.red, 1);
049:
050: protected final Border DEFAULT_BORDER = getBorder();
051:
052: private ContelligentComponent component = null;
053:
054: private View view = null;
055:
056: private GUI gui;
057:
058: public AbstractComponentRenderer() {
059: super (new BorderLayout());
060: setOpaque(false);
061: ComponentFactory.getInstance().addComponentEventListener(this ,
062: ContelligentEventDispatcher.DOES_USE_SWING);
063: }
064:
065: /**
066: * Counter-part to {@link AbstractComponentEditor#updateComponent}. Loads
067: * values from component to be displayed in renderer. Needs to be implemented
068: * by sub class.
069: */
070: abstract public void update();
071:
072: public void setComponent(ContelligentComponent component) {
073: this .component = component;
074: }
075:
076: public ContelligentComponent getComponent() {
077: return component;
078: }
079:
080: public void setView(View view) {
081: this .view = view;
082: }
083:
084: public View getView() {
085: return view;
086: }
087:
088: public void setGUI(GUI gui) {
089: this .gui = gui;
090: }
091:
092: public GUI getGUI() {
093: return gui;
094: }
095:
096: /**
097: * Reacts on the event that the component represented by this gui has
098: * changed.
099: */
100: abstract protected void componentChanged(ContelligentEvent event);
101:
102: /**
103: * Reacts on the event that a child has been added to the component
104: * represented by this gui.
105: */
106: abstract protected void childComponentAdded(ContelligentEvent event);
107:
108: /**
109: * Reacts on the event that a child has been removed to the component
110: * represented by this gui.
111: */
112: abstract protected void childComponentRemoved(
113: ContelligentEvent event);
114:
115: /**
116: * Reacts on the event that a child of the component represented by this gui
117: * has changed.
118: */
119: abstract protected void childComponentChanged(
120: ContelligentEvent event);
121:
122: /**
123: * Reacts on the event that a descendent - i.e not a direct, but indirect
124: * child - of the component represented by this gui has changed.
125: */
126: abstract protected void descendentComponentChanged(
127: ContelligentEvent event);
128:
129: public void onComponentAdded(ContelligentComponentEvent event) {
130: logger.log(Level.FINE, "event: " + event);
131:
132: // How am I related to this event?
133: boolean isMyChild = isMyChild(event.getTarget());
134: boolean isMyDescendent = isMyDescendent(event.getTarget());
135:
136: if (isMyChild) {
137: logger.log(Level.FINE, "component added: " + event);
138: childComponentAdded(event);
139: } else if (isMyDescendent) {
140: logger.log(Level.FINE, "descendent component changed: "
141: + event);
142: descendentComponentChanged(event);
143: }
144:
145: }
146:
147: public void onComponentRemoved(ContelligentComponentEvent event) {
148: logger.log(Level.FINE, "event: " + event);
149:
150: // How am I related to this event?
151: boolean isMyChild = isMyChild(event.getTarget());
152: boolean isMyDescendent = isMyDescendent(event.getTarget());
153:
154: if (isMyChild) {
155: logger.log(Level.FINE, "child component removed: " + event);
156: childComponentRemoved(event);
157: } else if (isMyDescendent) {
158: logger.log(Level.FINE, "descendent component changed: "
159: + event);
160: descendentComponentChanged(event);
161: }
162: }
163:
164: public void onComponentChanged(ContelligentComponentEvent event) {
165: logger.log(Level.FINE, "event: " + event);
166:
167: // How am I related to this event?
168: boolean isMe = isMe(event.getTarget());
169: boolean isMyChild = isMyChild(event.getTarget());
170: boolean isMyDescendent = isMyDescendent(event.getTarget());
171:
172: if (isMe) {
173: logger.log(Level.FINE, "component changed: " + event);
174: componentChanged(event);
175: } else if (isMyChild) {
176: logger.log(Level.FINE, "child component changed: " + event);
177: childComponentChanged(event);
178: } else if (isMyDescendent) {
179: logger.log(Level.FINE, "descendent component changed: "
180: + event);
181: descendentComponentChanged(event);
182: }
183: }
184:
185: /** default is to have no actions */
186: public Action[] getActions() {
187: return new Action[] {};
188: }
189:
190: /**
191: * This method is used to determine whether the given server and path equals
192: * the server and path of the component of this renderer.
193: */
194: private boolean isMe(String path) {
195: if (path == null)
196: return false;
197: String myPath = getMyPath();
198: return (myPath.equals(ComponentPath.toComponentPath(path)));
199: }
200:
201: /**
202: * This method is used to determine whether the given server and path
203: * represents a child component of the component of this renderer.
204: */
205: private boolean isMyChild(String path) {
206: if (path == null)
207: return false;
208: String myPath = getMyPath();
209: return (myPath.equals(ComponentPath.getComponentDir(path)));
210: }
211:
212: /**
213: * This method is used to determine whether the given server and path
214: * represents a descendent component of the component of this renderer.
215: */
216: private boolean isMyDescendent(String path) {
217: if (path == null)
218: return false;
219: String myPath = getMyPath();
220:
221: return ((ComponentPath.toComponentPath(path).length() > myPath
222: .length()) && ComponentPath.toComponentPath(path)
223: .startsWith(myPath));
224: }
225:
226: private String getMyPath() {
227: String myPath = ComponentPath.toComponentPath(getComponent()
228: .getPath());
229: return myPath;
230: }
231:
232: public boolean isScalable() {
233: return false;
234: }
235: }
|