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.awt.Dimension;
023: import java.awt.event.ActionEvent;
024: import java.text.DateFormat;
025: import java.util.Date;
026: import java.util.logging.Logger;
027:
028: import javax.swing.AbstractAction;
029: import javax.swing.Action;
030: import javax.swing.ImageIcon;
031: import javax.swing.JComponent;
032: import javax.swing.JLabel;
033: import javax.swing.JPanel;
034: import javax.swing.KeyStroke;
035:
036: import de.finix.contelligent.client.base.ComponentFactory;
037: import de.finix.contelligent.client.base.ContelligentComponent;
038: import de.finix.contelligent.client.event.ContelligentEventDispatcher;
039: import de.finix.contelligent.client.event.ContelligentLockEvent;
040: import de.finix.contelligent.client.event.ContextEvent;
041: import de.finix.contelligent.client.event.ContextEventListener;
042: import de.finix.contelligent.client.event.LockEventListener;
043: import de.finix.contelligent.client.gui.delegate.AbstractDelegateGUI;
044: import de.finix.contelligent.client.gui.directory.DirectoryEditor;
045: import de.finix.contelligent.client.base.Context;
046: import de.finix.contelligent.client.i18n.Resources;
047: import de.finix.contelligent.client.modules.ModuleInitException;
048: import de.finix.contelligent.client.util.ExceptionDialog;
049: import de.finix.contelligent.client.util.TableLayout;
050:
051: public class DefaultView extends AbstractView implements
052: ContextEventListener, LockEventListener {
053:
054: private static Logger logger = Logger.getLogger(DefaultView.class
055: .getName());
056:
057: private JLabel cacheSize, lockLabel, contextLabel, typeLabel,
058: modifiedByLabel, lastModifiedLabel;
059:
060: private JPanel serverInfoPanel = new JPanel(new BorderLayout());
061:
062: private ComponentRenderer rootRenderer;
063:
064: public void init() throws ModuleInitException {
065: super .init();
066:
067: serverInfoPanel.setOpaque(false);
068:
069: setLayout(new TableLayout(new double[][] {
070: { 0.33, 0.33, 0.33 },
071: { TableLayout.PREFERRED, TableLayout.PREFERRED,
072: TableLayout.PREFERRED, TableLayout.FILL } }));
073: setPreferredSize(new Dimension(800, 600));
074:
075: // add a location bar
076: ImageIcon myIcon;
077: if (ComponentFactory.getInstance().getCurrentContext()
078: .isRootContext()) {
079: myIcon = Resources.serverIcon;
080: contextLabel = new JLabel(ComponentFactory.getInstance()
081: .getCurrentContext().getShortName(), myIcon,
082: JLabel.LEADING);
083: } else {
084: myIcon = Resources.editServerIcon;
085: contextLabel = new JLabel(Resources
086: .getLocalString("context")
087: + ": "
088: + ComponentFactory.getInstance()
089: .getCurrentContext().getShortName(),
090: myIcon, JLabel.LEADING);
091: }
092: contextLabel.setToolTipText(contextLabel.getText());
093: modifiedByLabel = new JLabel(Resources
094: .getLocalString("modified_by")
095: + ": ");
096: cacheSize = new JLabel(Resources
097: .getLocalString("cached_components")
098: + ": ["
099: + ComponentFactory.getInstance().getCacheSize()
100: + " / "
101: + ComponentFactory.getInstance().getMaxCacheSize()
102: + "]", Resources.treeViewIcon, JLabel.LEFT);
103: cacheSize.setToolTipText(cacheSize.getText());
104: add(contextLabel, "0,0");
105: add(modifiedByLabel, "1,0");
106: add(cacheSize, "2,0");
107:
108: lockLabel = new JLabel(Resources.getLocalString("locked")
109: + ": ");
110: lastModifiedLabel = new JLabel(Resources
111: .getLocalString("last_modified")
112: + ": ");
113: lastModifiedLabel.setIcon(Resources.blankIcon);
114: typeLabel = new JLabel(Resources.getLocalString("type") + ": ");
115: add(typeLabel, "0,1");
116: add(lastModifiedLabel, "1,1");
117: add(lockLabel, "2,1");
118: setInfo(getRootComponent());
119:
120: // GUI Panel contains the GUI of the views root component
121: GUI gui = GUIFactory.getInstance().getGUI(getRootComponent(),
122: this , new GUIDescription[] { getRootGUIDescription() })[0];
123: if (isGUIValid(gui)) {
124: try {
125: // The renderer returned here is a configured DirectoryEditor (or replacement thereof)
126: rootRenderer = gui.getRenderer(GUI.DEFAULT);
127: add((JComponent) rootRenderer, "0,3,2,3");
128: } catch (UnsupportedGUIException e) {
129: ExceptionDialog.show(e);
130: }
131: }
132:
133: // XXX both do use swing, but only for synchronized text methods
134: ComponentFactory.getInstance().addContextEventListener(this ,
135: ContelligentEventDispatcher.DOES_NOT_USE_SWING);
136: ComponentFactory.getInstance().addLockEventListener(this ,
137: ContelligentEventDispatcher.DOES_NOT_USE_SWING);
138: }
139:
140: private boolean isGUIValid(GUI gui) {
141: if (gui == null)
142: return false;
143: if (gui instanceof AbstractDelegateGUI) {
144: return (((AbstractDelegateGUI) gui).getDelegatedGUI() != null);
145: }
146: return true;
147: }
148:
149: public void onComponentLocked(ContelligentLockEvent e) {
150: // update selected component as it might the one for with lock has
151: // changed
152: setInfo(getComponent());
153: }
154:
155: public void onComponentUnlocked(ContelligentLockEvent e) {
156: // update selected component as it might the one for with lock has
157: // changed
158: setInfo(getComponent());
159: }
160:
161: public void onContextCreated(ContextEvent e) {
162: }
163:
164: public void onContextDiscarded(ContextEvent e) {
165: }
166:
167: public void onContextSwitched(ContextEvent event) {
168: ImageIcon myIcon;
169: if (ComponentFactory.getInstance().getCurrentContext()
170: .isRootContext()) {
171: myIcon = Resources.serverIcon;
172: contextLabel.setText(ComponentFactory.getInstance()
173: .getCurrentContext().getShortName());
174: } else {
175: myIcon = Resources.editServerIcon;
176: contextLabel.setText(Resources.getLocalString("context")
177: + ": "
178: + ComponentFactory.getInstance()
179: .getCurrentContext().getShortName());
180: }
181: contextLabel.setIcon(myIcon);
182: contextLabel.setToolTipText(contextLabel.getText());
183: }
184:
185: public void setComponent(ContelligentComponent component) {
186: super .setComponent(component);
187: int usedCache = ComponentFactory.getInstance().getCacheSize();
188: int maxCache = ComponentFactory.getInstance().getMaxCacheSize();
189: if (usedCache == maxCache) {
190: cacheSize.setForeground(Color.red);
191: } else {
192: cacheSize.setForeground(Color.black);
193: }
194: cacheSize.setText(Resources.getLocalString("cached_components")
195: + ": [" + usedCache + " / " + maxCache + "]");
196: cacheSize.setToolTipText(cacheSize.getText());
197: if (!isEditInProgress()) {
198: setInfo(component);
199: }
200: }
201:
202: private void setInfo(ContelligentComponent component) {
203: if (component == null)
204: return;
205: String typeName = component.getType().getName();
206:
207: if (component.isBlueprint()) {
208: typeName = typeName + " (defines "
209: + component.getDefinedBlueprintType() + ')';
210: }
211: if ((component.getPublisherType().length() > 0)
212: && (component.getPublisherName().length() > 0)) {
213: modifiedByLabel.setText(Resources
214: .getLocalString("certified_by")
215: + ": " + component.getPublisherName());
216: // Yeah, these are non-breaking spaces. Unfortunately Swing messes
217: // up the tooltip border if you use multiline tooltips without
218: // adding this.
219: modifiedByLabel.setToolTipText("<html>\u00a0"
220: + modifiedByLabel.getText() + "\u00a0<br>\u00a0"
221: + Resources.getLocalString("certification_level")
222: + ": " + component.getPublisherType()
223: + "\u00a0<br>\u00a0"
224: + Resources.getLocalString("modified_by") + ": "
225: + component.getModifiedBy() + "</html>");
226: modifiedByLabel.setIcon(Resources.sealIcon);
227: } else {
228: modifiedByLabel.setText(Resources
229: .getLocalString("modified_by")
230: + ": " + component.getModifiedBy());
231: modifiedByLabel.setToolTipText(modifiedByLabel.getText());
232: modifiedByLabel.setIcon(Resources.blankIcon);
233: }
234: lastModifiedLabel.setText(Resources
235: .getLocalString("last_modified")
236: + ": "
237: + DateFormat.getDateTimeInstance(DateFormat.SHORT,
238: DateFormat.SHORT, Resources.getLocale())
239: .format(new Date(component.getLastModified())));
240: lastModifiedLabel.setToolTipText(lastModifiedLabel.getText());
241: typeLabel.setIcon(component.getSmallIcon());
242: typeLabel.setText(typeName);
243: if (component.isBlueprint()) {
244: typeLabel
245: .setToolTipText("<html>\u00a0["
246: + component.getDefinedBlueprintType()
247: + "]\u00a0<br>\u00a0"
248: + component.getType().hierarchy()
249: + "\u00a0</html>");
250: } else {
251: typeLabel
252: .setToolTipText("<html>\u00a0"
253: + component.getType().hierarchy()
254: + "\u00a0</html>");
255: }
256:
257: if (ComponentFactory.getInstance().isLocked(component)) {
258: ComponentFactory.Lock lock = ComponentFactory.getInstance()
259: .getLock(component, false);
260: boolean shared = false;
261: if (lock == null) {
262: lock = ComponentFactory.getInstance().getLock(
263: component, true);
264: shared = true;
265: }
266: lockLabel.setIcon(Resources.lockIcon);
267: String context = lock.getContext();
268: if (context.equals(ComponentFactory.getInstance()
269: .getCurrentContext())) {
270: lockLabel.setText((shared ? Resources
271: .getLocalString("locked_shared") : Resources
272: .getLocalString("locked"))
273: + ": " + lock.getOwner());
274: } else {
275: String contextShortName = context;
276: try {
277: contextShortName = Context.get(contextShortName)
278: .getShortName();
279: } catch (Exception e) {
280: ;
281: }
282: lockLabel.setText((shared ? Resources
283: .getLocalString("locked_shared") : Resources
284: .getLocalString("locked"))
285: + ": "
286: + lock.getOwner()
287: + " ("
288: + contextShortName + ")");
289: }
290: lockLabel.setToolTipText("<html>\u00a0"
291: + lockLabel.getText() + "\u00a0<br>\u00a0"
292: + lock.getPath() + "\u00a0</html>");
293: } else {
294: lockLabel.setIcon(Resources.unlockIcon);
295: lockLabel.setText(Resources.getLocalString("no_lock"));
296: lockLabel.setToolTipText(lockLabel.getText());
297: }
298: }
299:
300: public ComponentRenderer getRootComponentRenderer() {
301: return rootRenderer;
302: }
303:
304: public void navigate(String path, boolean attemptOpen) {
305: // Overwrite navigation if we are using a DirectoryEditor (which is the standard case
306: // for this view)
307: ComponentRenderer cr = getRootComponentRenderer();
308: if (cr instanceof DirectoryEditor) {
309: DirectoryEditor directoryEditor = (DirectoryEditor) cr;
310: directoryEditor.setSelectedPath(path);
311: if (attemptOpen && !isEditInProgress()) {
312: directoryEditor.renderSelectedComponent();
313: }
314: } else {
315: super .navigate(path, attemptOpen);
316: }
317: }
318:
319: final public class ServerInfoAction extends AbstractAction
320: implements ContelligentAction {
321: public ServerInfoAction() {
322: super ("server_info_action", Resources.serverIcon);
323: putValue(ROLLOVER_ICON, Resources.serverIcon);
324: putValue(Action.SHORT_DESCRIPTION,
325: "server_info_action_description");
326: putValue(TYPE, TOGGLE_ACTION);
327: putValue(ACTION_TYPE, SERVER_ACTION);
328: putValue(MENU_TARGET, MENU);
329: putValue(BUTTON_TARGET, TOOLBAR);
330: putValue(Action.ACCELERATOR_KEY, KeyStroke
331: .getKeyStroke("control I"));
332: }
333:
334: public void actionPerformed(ActionEvent e) {
335: if (getValue(ContelligentAction.STATE) == ContelligentAction.ON) {
336: remove(serverInfoPanel);
337: putValue(ContelligentAction.STATE,
338: ContelligentAction.OFF);
339: } else {
340: add(serverInfoPanel, BorderLayout.SOUTH);
341: putValue(ContelligentAction.STATE,
342: ContelligentAction.ON);
343: }
344: revalidate();
345: repaint();
346: }
347: }
348:
349: }
|