001: /*
002: The contents of this file are subject to the Common Public Attribution License
003: Version 1.0 (the "License"); you may not use this file except in compliance with
004: the License. You may obtain a copy of the License at
005: http://www.projity.com/license . The License is based on the Mozilla Public
006: License Version 1.1 but Sections 14 and 15 have been added to cover use of
007: software over a computer network and provide for limited attribution for the
008: Original Developer. In addition, Exhibit A has been modified to be consistent
009: with Exhibit B.
010:
011: Software distributed under the License is distributed on an "AS IS" basis,
012: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
013: specific language governing rights and limitations under the License. The
014: Original Code is OpenProj. The Original Developer is the Initial Developer and
015: is Projity, Inc. All portions of the code written by Projity are Copyright (c)
016: 2006, 2007. All Rights Reserved. Contributors Projity, Inc.
017:
018: Alternatively, the contents of this file may be used under the terms of the
019: Projity End-User License Agreeement (the Projity License), in which case the
020: provisions of the Projity License are applicable instead of those above. If you
021: wish to allow use of your version of this file only under the terms of the
022: Projity License and not to allow others to use your version of this file under
023: the CPAL, indicate your decision by deleting the provisions above and replace
024: them with the notice and other provisions required by the Projity License. If
025: you do not delete the provisions above, a recipient may use your version of this
026: file under either the CPAL or the Projity License.
027:
028: [NOTE: The text of this license may differ slightly from the text of the notices
029: in Exhibits A and B of the license at http://www.projity.com/license. You should
030: use the latest text at http://www.projity.com/license for your modifications.
031: You may not remove this license text from the source files.]
032:
033: Attribution Information: Attribution Copyright Notice: Copyright © 2006, 2007
034: Projity, Inc. Attribution Phrase (not exceeding 10 words): Powered by OpenProj,
035: an open source solution from Projity. Attribution URL: http://www.projity.com
036: Graphic Image as provided in the Covered Code as file: openproj_logo.png with
037: alternatives listed on http://www.projity.com/logo
038:
039: Display of Attribution Information is required in Larger Works which are defined
040: in the CPAL as a work which combines Covered Code or portions thereof with code
041: not governed by the terms of the CPAL. However, in addition to the other notice
042: obligations, all copies of the Covered Code in Executable and Source Code form
043: distributed must, as a form of attribution of the original author, include on
044: each user interface screen the "OpenProj" logo visible to all users. The
045: OpenProj logo should be located horizontally aligned with the menu bar and left
046: justified on the top left of the screen adjacent to the File menu. The logo
047: must be at least 100 x 25 pixels. When users click on the "OpenProj" logo it
048: must direct them back to http://www.projity.com.
049: */
050: package com.projity.dialog;
051:
052: import java.awt.Frame;
053: import java.util.ArrayList;
054: import java.util.Collection;
055: import java.util.Iterator;
056:
057: import javax.swing.InputVerifier;
058: import javax.swing.JComponent;
059: import javax.swing.SwingUtilities;
060: import javax.swing.undo.UndoableEditSupport;
061:
062: import org.apache.commons.lang.StringUtils;
063:
064: import com.jgoodies.forms.builder.DefaultFormBuilder;
065: import com.jgoodies.forms.layout.FormLayout;
066: import com.projity.dialog.util.FieldComponentMap;
067: import com.projity.document.Document;
068: import com.projity.document.ObjectEvent;
069: import com.projity.grouping.core.Node;
070: import com.projity.pm.graphic.frames.DocumentSelectedEvent;
071: import com.projity.pm.graphic.spreadsheet.selection.event.SelectionNodeEvent;
072: import com.projity.pm.graphic.spreadsheet.selection.event.SelectionNodeListener;
073: import com.projity.pm.resource.Resource;
074: import com.projity.pm.scheduling.Schedule;
075: import com.projity.pm.scheduling.ScheduleEvent;
076: import com.projity.pm.scheduling.ScheduleEventListener;
077: import com.projity.pm.task.BelongsToDocument;
078: import com.projity.pm.task.Project;
079: import com.projity.pm.task.Task;
080: import com.projity.strings.Messages;
081: import com.projity.util.DataUtils;
082:
083: /**
084: *
085: */
086: public abstract class FieldDialog extends AbstractDialog implements
087: ObjectEvent.Listener, ScheduleEventListener,
088: SelectionNodeListener, DocumentSelectedEvent.Listener {
089: private boolean multipleObjects;
090: private Class objectClass;
091: private UndoableEditSupport undoableEditSupport;
092:
093: protected FieldDialog(Frame owner, String title, boolean modal,
094: boolean multipleObjects/*,UndoableEditSupport undoableEditSupport*/) {
095: super (owner, title, modal);
096: this .multipleObjects = multipleObjects;
097: //this.undoableEditSupport=undoableEditSupport;
098:
099: // need to update all initially, but do later on once things are set
100: SwingUtilities.invokeLater(new Runnable() {
101: public void run() {
102: updateAll();
103: }
104: });
105: }
106:
107: protected ArrayList maps = new ArrayList();
108: protected Object object;
109: protected ArrayList collection = new ArrayList();
110: private JComponent dirtyComponent;
111: protected JComponent mainComponent = null;
112:
113: protected FieldComponentMap createMap() {
114: FieldComponentMap map;
115: if (multipleObjects)
116: map = new FieldComponentMap(collection);
117: else
118: map = new FieldComponentMap(object);
119:
120: maps.add(map);
121: return map;
122: }
123:
124: protected Object getObject() {
125: return object;
126: }
127:
128: protected Collection getCollection() {
129: return collection;
130: }
131:
132: protected Object getFirstObject() {
133: if (collection == null)
134: return object;
135: Iterator i = collection.iterator();
136: if (i.hasNext())
137: return i.next();
138: return null;
139: }
140:
141: // public void hide() {
142: // setObject(null);
143: // super.hide();
144: // }
145:
146: protected void onCancel() {
147: updateAll();
148: }
149:
150: public void setType(boolean task) {
151: objectClass = (task) ? Task.class : Resource.class;
152: setTitle((task) ? Messages
153: .getString("FieldDialog.TaskInformation") : Messages.getString("FieldDialog.ResourceInformation")); //$NON-NLS-1$ //$NON-NLS-2$
154: }
155:
156: public void setObjectClass(Class objectClass) {
157: this .objectClass = objectClass;
158: }
159:
160: public Class getObjectClass() {
161: return objectClass;
162: }
163:
164: public void objectChanged(ObjectEvent objectEvent) {
165: if (!isVisible())
166: return;
167: if (multipleObjects
168: && collection.contains(objectEvent.getObject())) {
169: updateAll(); // if in list, need to update all
170: } else if (objectEvent.getObject() == getObject()) {
171: updateAll();
172: }
173: }
174:
175: public void scheduleChanged(ScheduleEvent scheduleEvent) {
176: if (!isVisible())
177: return;
178: if (multipleObjects) {
179: //to be more precise could see if one of the collection's objects is dirty
180: updateAll(); // if in list, need to update all
181: } else if (getObject() != null
182: && ((Schedule) getObject()).isJustModified()) {
183: updateAll();
184: }
185: }
186:
187: public void documentSelected(DocumentSelectedEvent evt) {
188: System.out
189: .println(Messages.getString("FieldDialog.document") + evt.getCurrent()); //$NON-NLS-1$
190: }
191:
192: public void selectionChanged(SelectionNodeEvent e) {
193: if (!isVisible())
194: return;
195: Node selected;
196: Object nodeObject;
197: if (multipleObjects) {
198: setCollection(e.getNodes());
199: } else {
200: selected = e.getCurrentNode();
201: if (selected == null)
202: return;
203: nodeObject = selected.getImpl();
204: nodeObject = DataUtils.extractObjectOfClass(nodeObject,
205: objectClass);
206: // if (nodeObject == null)
207: // return;
208: setObject(nodeObject);
209: updateAll();
210: }
211: }
212:
213: public void setCollection(Collection nodeList) {
214: DataUtils.extractObjectsOfClassFromNodeList(collection,
215: nodeList, objectClass);
216: }
217:
218: public void setObject(Object object) {
219: if (object == this .object)
220: return;
221: if (this .object != null
222: && this .object instanceof BelongsToDocument) {
223: Document document = ((BelongsToDocument) this .object)
224: .getDocument();
225: document.removeObjectListener(this );
226: if (document instanceof Project)
227: ((Project) document).removeScheduleListener(this );
228: }
229: this .object = object;
230: if (object != null && object instanceof BelongsToDocument) {
231: Document document = ((BelongsToDocument) this .object)
232: .getDocument();
233: document.addObjectListener(this );
234: if (document instanceof Project)
235: ((Project) document).addScheduleListener(this );
236: }
237: }
238:
239: protected void activateListeners() {
240: }
241:
242: protected void desactivateListeners() {
243: setObject(null);
244: }
245:
246: public JComponent createContentPanel() {
247: // TODO Auto-generated method stub
248: return null;
249: }
250:
251: protected void updateAll() {
252: setVisibleAndEnabledState();
253: Iterator i = maps.iterator();
254: FieldComponentMap map;
255: while (i.hasNext()) {
256: map = (FieldComponentMap) i.next();
257: map.setObject(object);
258: map.updateAll();
259: }
260: }
261:
262: protected void setVisibleAndEnabledState() {
263: boolean showing = (object != null);
264: if (mainComponent != null)
265: mainComponent.setEnabled(showing);
266: }
267:
268: public void setDirtyComponent(JComponent dirtyComponent) {
269: this .dirtyComponent = dirtyComponent;
270: }
271:
272: /**
273: * On pressing enter key, check any unvalidated component
274: */
275: public void onOk() {
276: if (dirtyComponent != null) {
277: InputVerifier verifier = dirtyComponent.getInputVerifier();
278: if (!verifier.shouldYieldFocus(dirtyComponent))
279: return;
280: }
281: super .onOk();
282: }
283:
284: protected JComponent createFieldsPanel(FieldComponentMap map,
285: Collection fields) {
286: if (fields == null || fields.size() == 0)
287: return null;
288:
289: FormLayout layout = new FormLayout("p, 3dlu, fill:160dlu:grow", //$NON-NLS-1$
290: StringUtils.chomp(StringUtils.repeat(
291: "p,3dlu,", fields.size()))); // repeats and gets rid of last comma //$NON-NLS-1$
292: DefaultFormBuilder builder = new DefaultFormBuilder(layout);
293: map.append(builder, fields);
294: return builder.getPanel();
295: }
296: }
|