001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Christopher Kohlhaas, Bettina Lademann |
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.action;
014:
015: import java.awt.Component;
016: import java.awt.Point;
017: import java.awt.event.ActionEvent;
018: import java.util.Collection;
019: import java.util.Iterator;
020:
021: import org.rapla.entities.Category;
022: import org.rapla.entities.Entity;
023: import org.rapla.entities.RaplaType;
024: import org.rapla.entities.User;
025: import org.rapla.entities.domain.Allocatable;
026: import org.rapla.entities.domain.Period;
027: import org.rapla.entities.domain.Reservation;
028: import org.rapla.entities.dynamictype.Classifiable;
029: import org.rapla.entities.dynamictype.Classification;
030: import org.rapla.entities.dynamictype.DynamicType;
031: import org.rapla.entities.dynamictype.DynamicTypeAnnotations;
032: import org.rapla.facade.ModificationModule;
033: import org.rapla.framework.RaplaContext;
034: import org.rapla.framework.RaplaException;
035: import org.rapla.gui.RaplaAction;
036: import org.rapla.gui.toolkit.DialogUI;
037:
038: public class RaplaObjectAction extends RaplaAction {
039: public final static int DELETE = 1;
040: public final static int COPY = 2;
041: public final static int PASTE = 3;
042: public final static int CUT = 4;
043: public final static int NEW = 5;
044: public final static int EDIT = 6;
045: public final static int VIEW = 7;
046: public final static int DELETE_SELECTION = 8;
047: protected Component parent;
048: Point point;
049: protected int type;
050: boolean isPerson;
051: protected Object object;
052: Collection objectList;
053: protected RaplaType raplaType;
054:
055: public RaplaObjectAction(RaplaContext sm) throws RaplaException {
056: this (sm, null);
057: }
058:
059: public RaplaObjectAction(RaplaContext sm, Component parent)
060: throws RaplaException {
061: this (sm, parent, null);
062: }
063:
064: public RaplaObjectAction(RaplaContext sm, Component parent,
065: Point point) throws RaplaException {
066: super (sm);
067: this .parent = parent;
068: this .point = point;
069: }
070:
071: public RaplaObjectAction setNew(RaplaType raplaType) {
072: this .raplaType = raplaType;
073: this .type = NEW;
074: putValue(NAME, getString("new"));
075: putValue(SMALL_ICON, getIcon("icon.new"));
076: update();
077: return this ;
078: }
079:
080: public RaplaObjectAction setDelete(Object object) {
081: this .type = DELETE;
082: putValue(NAME, getString("delete"));
083: putValue(SMALL_ICON, getIcon("icon.delete"));
084: changeObject(object);
085: return this ;
086: }
087:
088: public RaplaObjectAction setDeleteSelection(Collection selection) {
089: this .type = DELETE_SELECTION;
090: putValue(NAME, getString("delete_selection"));
091: putValue(SMALL_ICON, getIcon("icon.delete"));
092: changeSelection(selection);
093: return this ;
094: }
095:
096: public RaplaObjectAction setView(Object object) {
097: this .type = VIEW;
098: putValue(NAME, getString("view"));
099: putValue(SMALL_ICON, getIcon("icon.help"));
100: changeObject(object);
101: return this ;
102: }
103:
104: public RaplaObjectAction setEdit(Object object) {
105: this .type = EDIT;
106: putValue(NAME, getString("edit"));
107: putValue(SMALL_ICON, getIcon("icon.edit"));
108: changeObject(object);
109: return this ;
110: }
111:
112: public void changeObject(Object object) {
113: this .object = object;
114: if (type == DELETE) {
115: if (object == null)
116: putValue(NAME, getString("delete"));
117: else
118: putValue(NAME, getI18n().format("delete.format",
119: getName(object)));
120: }
121: update();
122: }
123:
124: public void changeSelection(Collection objectList) {
125: this .objectList = objectList;
126: update();
127: }
128:
129: protected void update() {
130: boolean enabled = true;
131: if (type == EDIT || type == DELETE) {
132: enabled = canModify(object);
133: } else if (type == NEW) {
134: enabled = isRegisterer() || isAdmin();
135: } else if (type == DELETE_SELECTION) {
136: if (objectList != null && objectList.size() > 0) {
137: Iterator it = objectList.iterator();
138: while (it.hasNext()) {
139: if (!canModify(it.next())) {
140: enabled = false;
141: break;
142: }
143: }
144: } else {
145: enabled = false;
146: }
147: }
148: setEnabled(enabled);
149: }
150:
151: public void actionPerformed(ActionEvent evt) {
152: try {
153: switch (type) {
154: case DELETE:
155: delete();
156: break;
157: case DELETE_SELECTION:
158: deleteSelection();
159: break;
160: case EDIT:
161: edit();
162: break;
163: case NEW:
164: newEntity();
165: break;
166: case VIEW:
167: view();
168: break;
169: }
170: } catch (RaplaException ex) {
171: showException(ex, parent);
172: } // end of try-catch
173: }
174:
175: public void view() throws RaplaException {
176: getInfoFactory().showInfoDialog(object, parent);
177: }
178:
179: protected Object newEntity(RaplaType raplaType)
180: throws RaplaException {
181: ModificationModule m = getModification();
182: if (Reservation.TYPE.is(raplaType))
183: return m.newReservation();
184: if (Allocatable.TYPE.is(raplaType))
185: return (isPerson) ? m.newPerson() : m.newResource();
186: if (Category.TYPE.is(raplaType))
187: return m.newCategory();
188: if (User.TYPE.is(raplaType))
189: return m.newUser();
190: if (Period.TYPE.is(raplaType))
191: return m.newPeriod();
192:
193: throw new RaplaException("Can't create Entity for " + raplaType
194: + "!");
195: }
196:
197: /** guesses the DynamicType for the new object depending on the selected element.
198: <li>If the selected element is a DynamicType-Folder the DynamicType will be returned.
199: <li>If the selected element has a Classification the appropriatie DynamicType will be returned.
200: <li>else the first matching DynamicType for the passed classificationType will be returned.
201: <li>null if none of the above criterias matched.
202: */
203: public DynamicType guessType() throws RaplaException {
204: DynamicType dynamicType = null;
205: getLogger().debug("Guessing DynamicType from " + object);
206: if (object instanceof DynamicType)
207: dynamicType = (DynamicType) object;
208:
209: if (object instanceof Classifiable) {
210: Classification classification = ((Classifiable) object)
211: .getClassification();
212: dynamicType = classification.getType();
213: }
214: String classificationType = null;
215: if (Reservation.TYPE.is(raplaType)) {
216: classificationType = DynamicTypeAnnotations.VALUE_RESERVATION_CLASSIFICATION;
217: } else if (Allocatable.TYPE.is(raplaType)) {
218: if (isPerson) {
219: classificationType = DynamicTypeAnnotations.VALUE_PERSON_CLASSIFICATION;
220: } else {
221: classificationType = DynamicTypeAnnotations.VALUE_RESOURCE_CLASSIFICATION;
222: }
223: }
224: DynamicType[] dynamicTypes = getQuery().getDynamicTypes(
225: classificationType);
226: if (dynamicType != null) {
227: for (int i = 0; i < dynamicTypes.length; i++) {
228: if (dynamicTypes[i].equals(dynamicType))
229: return dynamicType;
230: }
231: }
232: return dynamicTypes[0];
233: }
234:
235: protected void newEntity() throws RaplaException {
236: Object obj = newEntity(raplaType);
237: if (obj instanceof Classifiable) {
238: DynamicType type = guessType();
239: ((Classifiable) obj).setClassification(type
240: .newClassification());
241: }
242: getEditController().edit((Entity) obj, parent);
243: }
244:
245: protected void edit() throws RaplaException {
246: getEditController().edit((Entity) object, parent);
247: }
248:
249: protected void delete() throws RaplaException {
250: if (object == null)
251: return;
252: Entity[] objects = new Entity[] { (Entity) object };
253: DialogUI dlg = getInfoFactory().createDeleteDialog(objects,
254: parent);
255: dlg.start();
256: if (dlg.getSelectedIndex() != 0)
257: return;
258: getModification().removeObjects(objects);
259: }
260:
261: protected void deleteSelection() throws RaplaException {
262: if (objectList == null || objectList.size() == 0)
263: return;
264: DialogUI dlg = getInfoFactory().createDeleteDialog(
265: objectList.toArray(), parent);
266: dlg.start();
267: if (dlg.getSelectedIndex() != 0)
268: return;
269: getModification().removeObjects(
270: (Entity[]) objectList.toArray(Entity.ENTITY_ARRAY));
271: }
272:
273: public void setPerson(boolean b) {
274: isPerson = b;
275: }
276:
277: }
|