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.link;
019:
020: import java.awt.BorderLayout;
021: import java.awt.Color;
022: import java.awt.event.ActionEvent;
023: import java.awt.event.ActionListener;
024: import java.awt.event.FocusAdapter;
025: import java.awt.event.FocusEvent;
026: import java.util.logging.Logger;
027:
028: import javax.swing.JComponent;
029: import javax.swing.JLabel;
030: import javax.swing.JPanel;
031: import javax.swing.event.CaretEvent;
032: import javax.swing.event.CaretListener;
033:
034: import de.finix.contelligent.client.base.ComponentFactory;
035: import de.finix.contelligent.client.base.ComponentNotFoundException;
036: import de.finix.contelligent.client.base.ContelligentComponent;
037: import de.finix.contelligent.client.event.ContelligentEvent;
038: import de.finix.contelligent.client.gui.AbstractComponentEditor;
039: import de.finix.contelligent.client.gui.ComponentRenderer;
040: import de.finix.contelligent.client.gui.GUI;
041: import de.finix.contelligent.client.gui.GUIFactory;
042: import de.finix.contelligent.client.gui.UnsupportedGUIException;
043: import de.finix.contelligent.client.i18n.Resources;
044: import de.finix.contelligent.client.modules.preferences.PreferencesModule;
045: import de.finix.contelligent.client.util.dnd.JTextFieldDND;
046:
047: public class LinkEditor extends AbstractComponentEditor {
048:
049: private static Logger logger = Logger.getLogger(LinkEditor.class
050: .getName());
051:
052: private JTextFieldDND linkTarget;
053:
054: private ComponentRenderer linkedRenderer;
055:
056: private JLabel linkTargetLabel;
057:
058: private JPanel rendererPanel;
059:
060: private int type;
061:
062: private boolean showLinkTargets = PreferencesModule
063: .getPreferences().getBoolean(
064: PreferencesModule.SHOW_LINK_TARGETS,
065: PreferencesModule.DEFAULT_SHOW_LINK_TARGETS);
066:
067: private boolean ignoreUpdates = false;
068:
069: public void init() {
070: linkTarget = new JTextFieldDND();
071: linkTargetLabel = new JLabel();
072: rendererPanel = new JPanel(new BorderLayout());
073: rendererPanel.setOpaque(false);
074:
075: update();
076:
077: linkTarget.setEditable(isEditable());
078: JPanel linkPanel = new JPanel(new BorderLayout());
079: linkPanel.setOpaque(false);
080: linkPanel.add(linkTarget, BorderLayout.CENTER);
081: linkPanel.add(linkTargetLabel, BorderLayout.WEST);
082: add(linkPanel, BorderLayout.NORTH);
083: add(rendererPanel, BorderLayout.CENTER);
084:
085: addFocusListener(new FocusAdapter() {
086: public void focusGained(FocusEvent e) {
087: getView().setActions(LinkEditor.this .getActions());
088: }
089: });
090:
091: linkTarget.addActionListener(new ActionListener() {
092: public void actionPerformed(ActionEvent e) {
093: updateComponent();
094: update();
095: notifyListeners();
096: }
097:
098: });
099:
100: linkTarget.addCaretListener(new CaretListener() {
101: public void caretUpdate(CaretEvent e) {
102: updateComponent();
103: }
104: });
105: }
106:
107: public void setType(int type) {
108: this .type = type;
109: }
110:
111: public int getType() {
112: return type;
113: }
114:
115: public void update() {
116:
117: String targetPath = null;
118:
119: ContelligentComponent linkedComponent = null;
120: GUI[] gui = null;
121: linkedRenderer = null;
122: JLabel noRenderer = null;
123:
124: targetPath = getComponent().getPropertyValue(
125: ContelligentComponent.TARGET_PATH);
126: ignoreUpdates = true;
127: linkTarget.setText(targetPath);
128: ignoreUpdates = false;
129:
130: rendererPanel.removeAll();
131:
132: // load linked component
133: try {
134: // linkedComponent =
135: // ComponentFactory.getInstance().getComponent(getComponent().getPath(),
136: // targetPath);
137: linkedComponent = ComponentFactory.getInstance()
138: .getComponent(targetPath);
139: if (linkedComponent != getComponent()) {
140: linkTargetLabel.setIcon(Resources.mergeIcons(
141: linkedComponent.getSmallIcon(),
142: Resources.linkAlias));
143: if (showLinkTargets) {
144: gui = GUIFactory.getInstance().getGUI(
145: linkedComponent, getView());
146: linkedRenderer = gui[0].getRenderer(getType());
147: rendererPanel.add((JComponent) linkedRenderer,
148: BorderLayout.CENTER);
149: } else {
150: noRenderer = new JLabel(Resources
151: .getLocalString("no_preview_error"));
152: noRenderer.setOpaque(true);
153: noRenderer.setBackground(Color.white);
154: rendererPanel.add(noRenderer, BorderLayout.CENTER);
155: }
156: }
157: } catch (UnsupportedGUIException e) {
158: noRenderer = new JLabel(Resources
159: .getLocalString("no_preview_error"));
160: noRenderer.setOpaque(true);
161: noRenderer.setBackground(Color.white);
162: rendererPanel.add(noRenderer, BorderLayout.CENTER);
163: } catch (ComponentNotFoundException cnfe) {
164: linkTargetLabel.setIcon(Resources.componentNotFoundIcon);
165: noRenderer = new JLabel(Resources
166: .getLocalString("invalid_link_error"));
167: noRenderer.setOpaque(true);
168: noRenderer.setBackground(Color.white);
169: noRenderer.setForeground(Color.red);
170: rendererPanel.add(noRenderer, BorderLayout.CENTER);
171: }
172: // needed to fix bug #314: as target has been newly created we need to
173: // set it editable
174: setEditable(isEditable());
175: rendererPanel.validate();
176: rendererPanel.repaint();
177: }
178:
179: public void setEditable(boolean editable) {
180: super .setEditable(editable);
181: if (linkTarget != null) {
182: linkTarget.setDragEnabled(isEditable());
183: linkTarget.setEditable(isEditable());
184: }
185: }
186:
187: protected void updateComponent() {
188: if (!ignoreUpdates) {
189: if (linkTarget.getText().length() > 0) {
190: getComponent().setPropertyValue(
191: ContelligentComponent.TARGET_PATH,
192: linkTarget.getText());
193: } else {
194: // Never store an empty link target
195: getComponent().setPropertyValue(
196: ContelligentComponent.TARGET_PATH, "/");
197: }
198: }
199: }
200:
201: protected void componentChanged(ContelligentEvent event) {
202: update();
203: }
204:
205: protected void childComponentAdded(ContelligentEvent event) {
206: }
207:
208: protected void childComponentRemoved(ContelligentEvent event) {
209: }
210:
211: protected void childComponentChanged(ContelligentEvent event) {
212: }
213:
214: protected void descendentComponentChanged(ContelligentEvent event) {
215: }
216:
217: public boolean isScalable() {
218: if (linkedRenderer != null) {
219: return linkedRenderer.isScalable();
220: }
221: return false;
222: }
223:
224: /**
225: * Overriden for this editor, since the abstract editor calls
226: * updateComponent on commit, which breaks the link editor for
227: * multi-category composed edits since that way the value from the last tab
228: * to be saved is committed to the database.
229: */
230: public void commit() {
231: notifyListeners();
232: ComponentFactory.getInstance().save(getComponent());
233: }
234:
235: }
|