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;
014:
015: import java.awt.Color;
016: import java.awt.Component;
017: import java.util.Iterator;
018: import java.util.Map;
019:
020: import org.rapla.client.ClientService;
021: import org.rapla.components.calendar.DateRenderer;
022: import org.rapla.components.calendar.RaplaCalendar;
023: import org.rapla.components.calendar.RaplaTime;
024: import org.rapla.components.calendar.TimeRenderer;
025: import org.rapla.components.util.DateTools;
026: import org.rapla.entities.DependencyException;
027: import org.rapla.entities.User;
028: import org.rapla.entities.configuration.RaplaConfiguration;
029: import org.rapla.facade.RaplaComponent;
030: import org.rapla.framework.RaplaContext;
031: import org.rapla.framework.RaplaException;
032: import org.rapla.gui.toolkit.ErrorDialog;
033: import org.rapla.gui.toolkit.FrameControllerList;
034:
035: /**
036: Base class for most components in the gui package. Eases
037: access to frequently used services, e.g. {@link org.rapla.components.xmlbundle.I18nBundle}.
038: It also provides some methods for Exception displaying.
039: */
040: public class RaplaGUIComponent extends RaplaComponent {
041:
042: public RaplaGUIComponent(RaplaContext context)
043: throws RaplaException {
044: super (context);
045: }
046:
047: /** lookup FrameControllerList from the context */
048: final protected FrameControllerList getFrameList() {
049: checkServiceState();
050: return (FrameControllerList) getService(FrameControllerList.ROLE);
051: }
052:
053: /** Creates a new ErrorDialog with the specified owner and displays the exception
054: @param ex the exception that should be displayed.
055: @param owner the exception that should be displayed. Can be null, but providing
056: a parent-component will lead to a more appropriate display.
057: */
058: public void showException(Exception ex, Component owner) {
059: try {
060: ErrorDialog dialog = new ErrorDialog(getContext());
061: if (ex instanceof DependencyException) {
062: dialog.showWarningDialog(
063: getHTML((DependencyException) ex), owner);
064: } else {
065: dialog.showExceptionDialog(ex, owner);
066: }
067: } catch (RaplaException ex2) {
068: getLogger().error(ex2.getMessage(), ex2);
069: }
070: }
071:
072: public boolean isInvokedOnAWTEventQueue() {
073: return true;
074: }
075:
076: private String getHTML(DependencyException ex) {
077: StringBuffer buf = new StringBuffer();
078: buf.append(getString("error.dependencies") + ":");
079: buf.append("<br>");
080: Iterator it = ex.getDependencies().iterator();
081: int i = 0;
082: while (it.hasNext()) {
083: Object obj = it.next();
084: buf.append((++i));
085: buf.append(") ");
086: buf.append(obj);
087: buf.append("<br>");
088: if (i == 10 && it.hasNext()) {
089: buf.append("... " + (ex.getDependencies().size() - 10)
090: + " more");
091: break;
092: }
093: }
094: return buf.toString();
095: }
096:
097: /** Creates a new ErrorDialog with the specified owner and displays the waring */
098: public void showWarning(String warning, Component owner) {
099: try {
100: ErrorDialog dialog = new ErrorDialog(getContext());
101: dialog.showWarningDialog(warning, owner);
102: } catch (RaplaException ex2) {
103: getLogger().error(ex2.getMessage(), ex2);
104: }
105: }
106:
107: public RaplaCalendar createRaplaCalendar() {
108: RaplaCalendar cal = new RaplaCalendar(getI18n().getLocale());
109: cal.setDateRenderer(getDateRenderer());
110: cal.setTimeZone(DateTools.getTimeZone());
111: return cal;
112: }
113:
114: /** lookup DateRenderer from the serviceManager */
115: final protected DateRenderer getDateRenderer() {
116: checkServiceState();
117: return (DateRenderer) getService(DateRenderer.class.getName());
118: }
119:
120: static Color NON_WORKTIME = new Color(0xcc, 0xcc, 0xcc);
121:
122: final protected TimeRenderer getTimeRenderer() {
123: final int start = getCalendarOptions().getWorktimeStart();
124: final int end = getCalendarOptions().getWorktimeEnd();
125: return new TimeRenderer() {
126: public Color getBackgroundColor(int hourOfDay, int minute) {
127: if (start >= end) {
128: if (hourOfDay >= end && hourOfDay < start) {
129: return NON_WORKTIME;
130: }
131: } else if (hourOfDay < start || hourOfDay >= end) {
132: return NON_WORKTIME;
133: }
134: return null;
135: }
136:
137: public String getToolTipText(int hourOfDay, int minute) {
138: return null;
139: }
140:
141: };
142: }
143:
144: public RaplaTime createRaplaTime() {
145: RaplaTime cal = new RaplaTime(getI18n().getLocale());
146: cal.setTimeRenderer(getTimeRenderer());
147: int rowsPerHour = getCalendarOptions().getRowsPerHour();
148: cal.setRowsPerHour(rowsPerHour);
149: cal.setTimeZone(DateTools.getTimeZone());
150: return cal;
151: }
152:
153: public CalendarOptions getCalendarOptions() {
154: RaplaConfiguration conf = null;
155: try {
156: User user = getUser();
157: if (user != null) {
158: conf = (RaplaConfiguration) getQuery().getPreferences(
159: user).getEntry(
160: CalendarOptionsImpl.CALENDAR_OPTIONS);
161: }
162: if (conf == null) {
163: conf = (RaplaConfiguration) getQuery().getPreferences(
164: null).getEntry(
165: CalendarOptionsImpl.CALENDAR_OPTIONS);
166: }
167: if (conf != null) {
168: return new CalendarOptionsImpl(conf.getConfig());
169: }
170: } catch (RaplaException ex) {
171:
172: }
173: return (CalendarOptions) getService(CalendarOptions.class
174: .getName());
175: }
176:
177: public Map getSessionMap() {
178: return (Map) getService(ClientService.SESSION_MAP);
179: }
180:
181: protected InfoFactory getInfoFactory() {
182: return (InfoFactory) getService(InfoFactory.ROLE);
183: }
184:
185: protected EditController getEditController() {
186: return (EditController) getService(EditController.ROLE);
187: }
188:
189: protected ReservationController getReservationController() {
190: return (ReservationController) getService(ReservationController.ROLE);
191: }
192:
193: public Component getMainComponent() {
194: return (Component) getService(ClientService.MAIN_COMPONENT);
195: }
196:
197: }
|