001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Christopher Kohlhaas |
003: | |
004: | This program is free software; you can redistribute it and/or modify |
005: | it under the terms of the GNU General Public License as published by the |
006: | Free Software Foundation. A copy of the license has been included with |
007: | these distribution in the COPYING file, if not go to www.fsf.org |
008: | |
009: | As a special exception, you are granted the permissions to link this |
010: | program with every library, which license fulfills the Open Source |
011: | Definition as published by the Open Source Initiative (OSI). |
012: *--------------------------------------------------------------------------*/
013: package org.rapla.gui.internal.view;
014:
015: import java.awt.Component;
016: import java.awt.Point;
017: import java.awt.datatransfer.DataFlavor;
018: import java.awt.datatransfer.StringSelection;
019: import java.awt.event.ActionEvent;
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import javax.swing.AbstractAction;
024: import javax.swing.JComponent;
025:
026: import org.rapla.components.iolayer.ComponentPrinter;
027: import org.rapla.components.iolayer.IOInterface;
028: import org.rapla.entities.Category;
029: import org.rapla.entities.Named;
030: import org.rapla.entities.RaplaObject;
031: import org.rapla.entities.User;
032: import org.rapla.entities.domain.Allocatable;
033: import org.rapla.entities.domain.Appointment;
034: import org.rapla.entities.domain.Period;
035: import org.rapla.entities.domain.Reservation;
036: import org.rapla.entities.dynamictype.DynamicType;
037: import org.rapla.facade.Conflict;
038: import org.rapla.framework.RaplaContext;
039: import org.rapla.framework.RaplaException;
040: import org.rapla.gui.InfoFactory;
041: import org.rapla.gui.RaplaGUIComponent;
042: import org.rapla.gui.toolkit.DialogUI;
043: import org.rapla.gui.toolkit.HTMLView;
044:
045: /** The factory can creatres an information-panel or dialog for
046: the entities of rapla.
047: @see ViewTable*/
048: public class InfoFactoryImpl extends RaplaGUIComponent implements
049: InfoFactory
050:
051: {
052: Map views = new HashMap();
053:
054: public InfoFactoryImpl(RaplaContext sm) throws RaplaException {
055: super (sm);
056: views.put(DynamicType.TYPE, new DynamicTypeInfoUI(sm));
057: views.put(Reservation.TYPE, new ReservationInfoUI(sm));
058: views.put(Appointment.TYPE, new AppointmentInfoUI(sm));
059: views.put(Allocatable.TYPE, new AllocatableInfoUI(sm));
060: views.put(User.TYPE, new UserInfoUI(sm));
061: views.put(Period.TYPE, new PeriodInfoUI(sm));
062: views.put(Conflict.TYPE, new ConflictInfoUI(sm));
063: views.put(Category.TYPE, new CategoryInfoUI(sm));
064: }
065:
066: /** this method is used by the viewtable to dynamicaly create an
067: * appropriate HTMLInfo for the passed object
068: */
069: HTMLInfo createView(RaplaObject object) throws RaplaException {
070: if (object == null)
071: throw new RaplaException(
072: "Could not create view for null object");
073:
074: HTMLInfo result = (HTMLInfo) views.get(object.getRaplaType());
075: if (result != null)
076: return result;
077: throw new RaplaException(
078: "Could not create view for this object: "
079: + object.getClass());
080: }
081:
082: public JComponent createInfoComponent(Object object)
083: throws RaplaException {
084: ViewTable viewTable = new ViewTable(getContext());
085: viewTable.updateInfo(object);
086: return viewTable.getComponent();
087: }
088:
089: public String getToolTip(Object obj) {
090: return getToolTip(obj, true);
091: }
092:
093: public String getToolTip(Object obj, boolean wrapHtml) {
094: try {
095: if (!(obj instanceof RaplaObject)) {
096: return null;
097: }
098: RaplaObject o = (RaplaObject) obj;
099: if (!views.containsKey(o.getRaplaType())) {
100: return null;
101: }
102: String text = createView(o).getTooltip(o);
103: if (wrapHtml && text != null)
104: return HTMLView.createHTMLPage(text);
105: else
106: return text;
107: } catch (RaplaException ex) {
108: getLogger().error(ex.getMessage(), ex);
109: }
110: if (obj instanceof Named)
111: return ((Named) obj).getName(getI18n().getLocale());
112: return null;
113: }
114:
115: /* (non-Javadoc)
116: * @see org.rapla.gui.view.IInfoUIFactory#showInfoDialog(java.lang.Object, java.awt.Component)
117: */
118: public void showInfoDialog(Object object, Component owner)
119: throws RaplaException {
120: showInfoDialog(object, owner, null);
121: }
122:
123: /* (non-Javadoc)
124: * @see org.rapla.gui.view.IInfoUIFactory#showInfoDialog(java.lang.Object, java.awt.Component, java.awt.Point)
125: */
126: public void showInfoDialog(Object object, Component owner,
127: Point point) throws RaplaException {
128: if (!(object instanceof RaplaObject)) {
129: throw new RaplaException(
130: "Can only create Dialogs for RaplaObjects");
131: }
132: final ViewTable viewTable = new ViewTable(getContext());
133: final DialogUI dlg = DialogUI.create(getContext(), owner,
134: false, viewTable.getComponent(), new String[] {
135: getString("copy_to_clipboard"),
136: getString("print"), getString("back") });
137:
138: viewTable.updateInfo(object, createView((RaplaObject) object));
139: dlg.setTitle(viewTable.getDialogTitle());
140: dlg.setDefault(2);
141: dlg.start(point);
142:
143: dlg.getButton(0).setAction(new AbstractAction() {
144: private static final long serialVersionUID = 1L;
145:
146: public void actionPerformed(ActionEvent e) {
147: try {
148: DataFlavor.getTextPlainUnicodeFlavor();
149: viewTable.htmlView.selectAll();
150: String plainText = viewTable.htmlView
151: .getSelectedText();
152: //String htmlText = viewTable.htmlView.getText();
153: //InfoSelection selection = new InfoSelection( htmlText, plainText );
154: StringSelection selection = new StringSelection(
155: plainText);
156: IOInterface printTool = (IOInterface) getContext()
157: .lookup(IOInterface.ROLE);
158: printTool.setContents(selection, null);
159: } catch (Exception ex) {
160: showException(ex, dlg);
161: }
162: }
163: });
164: dlg.getButton(1).setAction(new AbstractAction() {
165: private static final long serialVersionUID = 1L;
166:
167: public void actionPerformed(ActionEvent e) {
168: try {
169: IOInterface printTool = (IOInterface) getContext()
170: .lookup(IOInterface.ROLE);
171: printTool.print(new ComponentPrinter(
172: viewTable.htmlView, true), printTool
173: .defaultPage(), true);
174: } catch (Exception ex) {
175: showException(ex, dlg);
176: }
177: }
178: });
179: }
180:
181: /* (non-Javadoc)
182: * @see org.rapla.gui.view.IInfoUIFactory#createDeleteDialog(java.lang.Object[], java.awt.Component)
183: */
184: public DialogUI createDeleteDialog(Object[] deletables,
185: Component owner) throws RaplaException {
186: ViewTable viewTable = new ViewTable(getContext());
187: HTMLInfo deleteView = new DeleteInfoUI(getContext());
188: DialogUI dlg = DialogUI.create(getContext(), owner, true,
189: viewTable.getComponent(), new String[] {
190: getString("delete.ok"),
191: getString("delete.abort") });
192: dlg.setIcon(getIcon("icon.warning"));
193: dlg.getButton(0).setIcon(getIcon("icon.delete"));
194: dlg.getButton(1).setIcon(getIcon("icon.abort"));
195: dlg.setDefault(1);
196: viewTable.updateInfo(deletables, deleteView);
197: dlg.setTitle(viewTable.getDialogTitle());
198: return dlg;
199: }
200: }
|