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.assignment;
051:
052: import java.awt.Component;
053: import java.awt.Dimension;
054: import java.util.ArrayList;
055: import java.util.Iterator;
056: import java.util.List;
057:
058: import javax.swing.JLabel;
059: import javax.swing.JScrollPane;
060: import javax.swing.JViewport;
061: import javax.swing.event.ChangeEvent;
062: import javax.swing.table.TableCellEditor;
063: import javax.swing.table.TableCellRenderer;
064:
065: import org.apache.commons.collections.Closure;
066: import org.apache.commons.collections.Transformer;
067:
068: import com.projity.configuration.Dictionary;
069: import com.projity.datatype.Rate;
070: import com.projity.field.Field;
071: import com.projity.graphic.configuration.GraphicConfiguration;
072: import com.projity.graphic.configuration.SpreadSheetFieldArray;
073: import com.projity.graphic.configuration.shape.Colors;
074: import com.projity.grouping.core.Node;
075: import com.projity.grouping.core.NodeList;
076: import com.projity.grouping.core.event.HierarchyEvent;
077: import com.projity.grouping.core.event.HierarchyListener;
078: import com.projity.grouping.core.model.NodeModel;
079: import com.projity.grouping.core.model.NodeModelFactory;
080: import com.projity.pm.assignment.Assignment;
081: import com.projity.pm.assignment.AssignmentEntry;
082: import com.projity.pm.assignment.HasAssignments;
083: import com.projity.pm.graphic.model.cache.NodeModelCache;
084: import com.projity.pm.graphic.model.cache.NodeModelCacheFactory;
085: import com.projity.pm.graphic.spreadsheet.SpreadSheet;
086: import com.projity.pm.graphic.spreadsheet.SpreadSheetModel;
087: import com.projity.pm.graphic.spreadsheet.editor.RateEditor;
088: import com.projity.pm.graphic.spreadsheet.renderer.RateRenderer;
089: import com.projity.pm.resource.Resource;
090: import com.projity.pm.resource.ResourcePool;
091: import com.projity.pm.task.Project;
092: import com.projity.strings.Messages;
093:
094: /**
095: *
096: */
097: public class AssignmentEntryPane extends JScrollPane implements
098: HierarchyListener {
099: private static final long serialVersionUID = 1L;
100: CommonAssignmentDialog dialog;
101: AssignmentSpreadSheet spreadSheet;
102: NodeModel assignmentModel;
103: Project project;
104: ResourceAssigner resourceAssigner;
105: List taskList = new ArrayList(); // empty selection to start
106: public static final String spreadsheetCategory = "assignmentEntrySpreadsheet";
107: protected NodeModelCache cache;
108: private boolean replace;
109:
110: private static final int REQUEST_DEMAND_TYPE_COLUMN = 1;
111: private static final int UNITS_COLUMN = 2;
112:
113: class AssignmentSpreadSheet extends SpreadSheet {
114: private static final long serialVersionUID = 1L;
115: ResourceAssigner resourceAssigner;
116:
117: /**
118: * @param resourceAssigner
119: */
120: public AssignmentSpreadSheet(ResourceAssigner resourceAssigner) {
121: this .resourceAssigner = resourceAssigner;
122: setCanModifyColumns(false);
123: //setPopupActions(null);
124: setCanSelectFieldArray(false);
125:
126: }
127:
128: private AssignmentEntry getEntryInRow(int row) {
129: Node node = ((SpreadSheetModel) getModel()).getNode(row)
130: .getNode();
131: if (node != null && !node.isVirtual())
132: return (AssignmentEntry) node.getImpl();
133: else
134: return null;
135: }
136:
137: public void setValueAt(Object aValue, int row, int column) {
138: AssignmentEntry entry = getEntryInRow(row);
139: if (entry == null)
140: return;
141:
142: if (!entry.isAssigned()) { // assign it first, then set value
143: if (resourceAssigner != null) {
144: Field field = ((SpreadSheetModel) getModel())
145: .getFieldInColumn(column + 1);
146: double units = 1.0;
147: if (field == AssignmentEntry.getRateField()) {
148: units = ((Rate) aValue).getValue();
149: }
150: resourceAssigner.assign((Resource) entry
151: .getResource(), units);
152: }
153: }
154: super .setValueAt(aValue, row, column);
155: }
156:
157: /**
158: * Gets selected resources on spreadsheet.
159: * @param assignedOnly - if true, only selected resources are returned
160: * @return
161: */
162: List getSelectedResources(boolean assignedOnly) {
163: List list = NodeList.nodeListToImplList(getSelectedNodes());
164: ArrayList resourceList = new ArrayList();
165: Iterator i = list.iterator();
166: AssignmentEntry entry;
167: while (i.hasNext()) {
168: entry = (AssignmentEntry) i.next();
169: if (!assignedOnly || entry.isAssigned()) // see if should add.
170: resourceList.add(entry.getResource());
171: }
172: return resourceList;
173: }
174:
175: public TableCellEditor getCellEditor(int row, int column) {
176: TableCellEditor editor = null;
177: AssignmentEntry entry = getEntryInRow(row);
178:
179: if (entry != null) {
180: Field field = ((SpreadSheetModel) getModel())
181: .getFieldInColumn(column + 1);
182: if (field == AssignmentEntry.getRateField()) {
183: if (entry.getTimeUnitLabel() != null) {
184: boolean labor = ((AssignmentEntry) entry)
185: .getResource().isLabor();
186: editor = new RateEditor(entry
187: .getTimeUnitLabel(), field.isMoney(),
188: labor && field.isPercent(), labor);
189: }
190: }
191: }
192: if (editor == null)
193: editor = super .getCellEditor(row, column);
194: return editor;
195: }
196:
197: public TableCellRenderer getCellRenderer(int row, int column) {
198: TableCellRenderer renderer = null;
199: AssignmentEntry entry = getEntryInRow(row);
200:
201: if (entry != null) {
202: Field field = ((SpreadSheetModel) getModel())
203: .getFieldInColumn(column + 1);
204: if (field == AssignmentEntry.getRateField()) {
205: if (entry.getTimeUnitLabel() != null) {
206: renderer = new RateRenderer();
207: }
208: }
209: }
210: if (renderer == null)
211: renderer = super .getCellRenderer(row, column);
212: return renderer;
213: }
214:
215: int getSelectedCount() {
216: return NodeList.nodeListToImplList(getSelectedNodes())
217: .size(); // doesn't count void nodes
218: }
219:
220: /* (non-Javadoc)
221: * @see javax.swing.JTable#prepareRenderer(javax.swing.table.TableCellRenderer, int, int)
222: */
223: public Component prepareRenderer(TableCellRenderer renderer,
224: int row, int column) {
225: Component component = super .prepareRenderer(renderer, row,
226: column);
227: AssignmentEntry entry = getEntryInRow(row);
228:
229: if (entry != null) {
230: if (entry.isAssigned()) {
231: if (taskList.size() == entry.getAssignmentCount()) { // if all selected tasks are assigned to this resource, show it green
232: //if (column == 0)
233: component.setBackground(Colors.PALE_GREEN);
234: } else {
235: //if (column == 0)
236: component.setBackground(Colors.PALE_YELLOW);
237: if (column != 0) {
238: Field field = ((SpreadSheetModel) getModel())
239: .getFieldInColumn(column);
240: if (field == Assignment
241: .getRequestDemandTypeField()
242: || field == AssignmentEntry
243: .getRateField()) {
244: ((JLabel) component)
245: .setText(Field.MULTIPLE_VALUES);
246: }
247: }
248: }
249: }
250: }
251: return component;
252: }
253:
254: public Component prepareEditor(TableCellEditor editor, int row,
255: int column) {
256: dialog.setEditorButtonsVisible(true);
257: return super .prepareEditor(editor, row, column);
258: }
259:
260: public void editingCanceled(ChangeEvent e) {
261: dialog.setEditorButtonsVisible(false);
262: super .editingCanceled(e);
263: }
264:
265: public void editingStopped(ChangeEvent e) {
266: dialog.setEditorButtonsVisible(false);
267: super .editingStopped(e);
268: }
269:
270: public void doDoubleClick(int row, int col) {
271: if (dialog instanceof AssignmentDialog) {
272: ((AssignmentDialog) dialog).assign();
273: ((AssignmentDialog) dialog).setVisible(false);
274: } else if (dialog instanceof ReplaceAssignmentDialog)
275: ((ReplaceAssignmentDialog) dialog).onOk();
276: }
277: }
278:
279: // A transformer to create new elements from Resources
280: public class NodeFactoryTransformer implements Transformer {
281: public Object transform(Object impl) {
282: if (impl instanceof HasAssignments) {
283: HasAssignments hasAssignments = (HasAssignments) impl;
284: return new AssignmentEntry(hasAssignments, null,
285: project);
286: }
287: return null;
288: }
289: }
290:
291: protected Closure transformerClosure;
292:
293: /**
294: *
295: */
296: public AssignmentEntryPane(CommonAssignmentDialog dialog,
297: Project project, ResourceAssigner resourceAssigner,
298: boolean replace, Closure transformerClosure) {
299: super ();
300: this .replace = replace;
301: //setProject(project);
302: this .resourceAssigner = resourceAssigner;
303: this .dialog = dialog;
304: //setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
305: this .transformerClosure = transformerClosure;
306: }
307:
308: private SpreadSheetFieldArray getFields() {
309: return (SpreadSheetFieldArray) Dictionary
310: .get(
311: spreadsheetCategory,
312: Messages
313: .getString(replace ? "Spreadsheet.AssignmentEntry.replaceResources"
314: : "Spreadsheet.AssignmentEntry.assignResources"));
315: }
316:
317: public void init() {
318: if (project == null)
319: return;
320: ResourcePool pool = project.getResourcePool();
321:
322: // if (assignmentModel==null){
323: pool.getResourceOutline().getHierarchy()
324: .removeHierarchyListener(this );
325: pool.getResourceOutline().getHierarchy().addHierarchyListener(
326: this );
327:
328: assignmentModel = NodeModelFactory.getInstance()
329: .replicate(pool.getResourceOutline(),
330: new NodeFactoryTransformer());
331: assignmentModel.getHierarchy().setNbEndVoidNodes(0); // don't allow blank lines
332: // }
333: if (spreadSheet == null) {
334: spreadSheet = new AssignmentSpreadSheet(resourceAssigner);
335: spreadSheet.setSpreadSheetCategory(spreadsheetCategory); // for columns. Must do first
336: spreadSheet.setActions(new String[] {});
337:
338: }
339:
340: cache = NodeModelCacheFactory.getInstance().createDefaultCache(
341: assignmentModel, pool, NodeModelCache.ASSIGNMENT_TYPE,
342: "AssignmentEntry", transformerClosure);
343: SpreadSheetFieldArray fields = getFields();
344: spreadSheet.setCache(cache, fields, fields.getCellStyle(),
345: fields.getActionList());
346:
347: // set widths of these columns explicitly
348: if (!replace) { //TODO width should come from definition in xml, not hard coded here
349: spreadSheet.getColumnModel().getColumn(
350: REQUEST_DEMAND_TYPE_COLUMN).setPreferredWidth(50);
351: spreadSheet.getColumnModel().getColumn(UNITS_COLUMN)
352: .setPreferredWidth(50);
353: }
354:
355: JViewport viewport = createViewport();
356: viewport.setView(spreadSheet);
357: setViewport(viewport);
358:
359: setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
360:
361: Dimension d = spreadSheet.getPreferredSize();
362: Dimension enclosing = new Dimension();
363:
364: //big awful hack to remove
365: GraphicConfiguration config = GraphicConfiguration
366: .getInstance();
367: int rowHeaderWidth = config.getRowHeaderWidth()
368: + spreadSheet.getRowMargin() * 2; //should be rowHeader's one
369:
370: // TableColumnModel tm=spreadSheet.getColumnModel();
371: // int w=tm.getTotalColumnWidth();
372:
373: enclosing.setSize(d.getWidth()/*+rowHeaderWidth*/, d
374: .getHeight());
375: viewport.setPreferredSize(enclosing);
376:
377: //setBorder(new EmptyBorder(0,0,0,0));
378: updateTable();
379: }
380:
381: /**
382: * @param project The project to set.
383: */
384: public void setProject(Project project) {
385: this .project = project;
386: init();
387: }
388:
389: /**
390: * Functor to call setAssignmentsFromTaskList
391: */
392: private class AssignmentEntrySetter implements Closure {
393: List taskList;
394:
395: AssignmentEntrySetter(List taskList) {
396: this .taskList = taskList;
397: }
398:
399: /* (non-Javadoc)
400: * @see org.apache.commons.collections.Closure#execute(java.lang.Object)
401: */
402: public void execute(Object arg0) {
403: AssignmentEntry entry = (AssignmentEntry) (((Node) arg0)
404: .getImpl());
405: entry.setAssignmentsFromTaskList(taskList);
406: }
407:
408: }
409:
410: /**
411: * Updates the spreadsheet based on the selected tasks
412: * @param taskList
413: */
414: void setSelectedTasks(List taskList) {
415: this .taskList = taskList;
416: updateTable();
417: // disable ss if no tasks selected.
418: boolean enabled = !taskList.isEmpty();
419: spreadSheet.setEnabled(enabled);
420: spreadSheet.getRowHeader().setEnabled(enabled);
421: // ((JDialog)dialog).setEnabled(enabled); // I choose not to disable the dialog so you can still close it
422:
423: }
424:
425: List getSelectedResources(boolean assignedOnly) {
426: return spreadSheet.getSelectedResources(assignedOnly);
427: }
428:
429: int getSelectedCount() {
430: return spreadSheet.getSelectedCount();
431: }
432:
433: void updateTable() {
434: assignmentModel.getHierarchy().visitAll(
435: new AssignmentEntrySetter(taskList));
436: ((SpreadSheetModel) spreadSheet.getModel()).getCache().update();
437: ((SpreadSheetModel) spreadSheet.getModel())
438: .fireTableDataChanged(); // redraw it
439: }
440:
441: public AssignmentSpreadSheet getSpreadSheet() {
442: return spreadSheet;
443: }
444:
445: public void nodesChanged(HierarchyEvent e) {
446: // TODO Auto-generated method stub
447:
448: }
449:
450: public void nodesInserted(HierarchyEvent e) {
451: init();
452: }
453:
454: public void nodesRemoved(HierarchyEvent e) {
455: init();
456: }
457:
458: public void structureChanged(HierarchyEvent e) {
459: // TODO Auto-generated method stub
460:
461: }
462: }
|