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.pm.graphic.spreadsheet;
051:
052: import java.util.ArrayList;
053: import java.util.List;
054:
055: import javax.swing.DefaultComboBoxModel;
056: import javax.swing.event.ChangeEvent;
057: import javax.swing.event.ListSelectionEvent;
058: import javax.swing.event.TableColumnModelEvent;
059: import javax.swing.event.TableColumnModelListener;
060: import javax.swing.table.DefaultTableColumnModel;
061: import javax.swing.table.TableColumn;
062:
063: import com.projity.configuration.Configuration;
064: import com.projity.field.Field;
065: import com.projity.graphic.configuration.SpreadSheetFieldArray;
066: import com.projity.pm.graphic.spreadsheet.editor.MoneyEditor;
067: import com.projity.pm.graphic.spreadsheet.editor.RateEditor;
068: import com.projity.pm.graphic.spreadsheet.editor.SimpleComboBoxEditor;
069: import com.projity.pm.graphic.spreadsheet.editor.SimpleEditor;
070: import com.projity.pm.graphic.spreadsheet.editor.SpinEditor;
071: import com.projity.pm.graphic.spreadsheet.editor.SpreadSheetCellEditorAdapter;
072: import com.projity.pm.graphic.spreadsheet.renderer.DateRenderer;
073: import com.projity.pm.graphic.spreadsheet.renderer.IndicatorsRenderer;
074: import com.projity.pm.graphic.spreadsheet.renderer.LookupRenderer;
075: import com.projity.pm.graphic.spreadsheet.renderer.OfflineCapableBooleanRenderer;
076: import com.projity.pm.graphic.spreadsheet.renderer.PercentRenderer;
077: import com.projity.pm.graphic.spreadsheet.renderer.RateRenderer;
078: import com.projity.pm.graphic.spreadsheet.renderer.SimpleRenderer;
079: import com.projity.pm.graphic.spreadsheet.renderer.SpreadSheetCellRendererAdapter;
080: import com.projity.pm.graphic.spreadsheet.renderer.SpreadSheetColumnHeaderRenderer;
081: import com.projity.pm.graphic.spreadsheet.renderer.SpreadSheetNameCellRenderer;
082:
083: /**
084: *
085: */
086: public class SpreadSheetColumnModel extends DefaultTableColumnModel {
087: int columnIndex = 0;
088:
089: int colWidth = 0;
090:
091: private ArrayList fieldArray; //changes when columns are moved - needed to update the current definition
092: private ArrayList originalFieldArray; // will not change
093: private List<Integer> colWidthList;
094:
095: boolean svg;
096:
097: /**
098: * @param fieldArray
099: * TODO
100: *
101: */
102: public SpreadSheetColumnModel(final ArrayList fieldArray) {
103: super ();
104: setFieldArray(fieldArray);
105: // getSelectionModel().addListSelectionListener(new ListSelectionListener(){
106: // public void valueChanged(ListSelectionEvent lse) {
107: //
108: // }
109: // });
110:
111: }
112:
113: public SpreadSheetColumnModel(ArrayList fieldArray,
114: List<Integer> colWidthList) {
115: this (fieldArray);
116: this .colWidthList = colWidthList;
117: }
118:
119: public void addColumn(TableColumn tc) {
120: tc.setHeaderRenderer(new SpreadSheetColumnHeaderRenderer());
121:
122: if (columnIndex == 0) {
123: Field field = (Field) originalFieldArray.get(columnIndex);
124: tc.setIdentifier(field); // store the field with the column
125: // tc.setIdentifier(null); // store the field with the column
126: tc.setPreferredWidth(0);
127:
128: colWidth = 0;
129: // nothing
130: } else {
131: super .addColumn(tc);
132: Field field = (Field) originalFieldArray.get(columnIndex);
133: tc.setIdentifier(field); // store the field with the column
134: // System.out.println("setting column " + columnIndex + " to field " + field + " ok = " + (field == getFieldInColumn(columnIndex)));
135:
136: if (field.isNameField()) {
137: tc.setPreferredWidth((svg) ? 170 : 150);
138: tc.setCellRenderer(new SpreadSheetNameCellRenderer());
139: // tc.setCellEditor(new SpreadSheetNameCellEditor(new SimpleEditor(String.class)));
140: tc.setCellEditor(new SpreadSheetCellEditorAdapter(
141: new SimpleEditor(String.class)));
142: } else if (field == Configuration
143: .getFieldFromId("Field.indicators")) {
144: tc.setPreferredWidth(50);
145: tc.setCellRenderer(new SpreadSheetCellRendererAdapter(
146: new IndicatorsRenderer()));
147: tc
148: .setHeaderRenderer(new SpreadSheetColumnHeaderRenderer(
149: IndicatorsRenderer.getCellHeader()));
150: } else if (field.getLookupTypes() != null) {
151: tc.setCellRenderer(new SpreadSheetCellRendererAdapter(
152: new LookupRenderer()));
153: } else {
154: tc.setPreferredWidth(150);
155: if (field.hasOptions()) {
156: tc.setPreferredWidth(150);
157: tc
158: .setCellRenderer(new SpreadSheetCellRendererAdapter(
159: new SimpleRenderer()));
160: // note that in Spreadsheet, there getCellEditor() is
161: // overridden and dynamic combos are filled there
162: tc.setCellEditor(new SpreadSheetCellEditorAdapter(
163: new SimpleComboBoxEditor(
164: new DefaultComboBoxModel(field
165: .getOptions(null)))));
166: } else if (field.getRange() != null) {
167: if (field.isPercent())
168: tc
169: .setCellRenderer(new SpreadSheetCellRendererAdapter(
170: new PercentRenderer()));
171: else
172: tc
173: .setCellRenderer(new SpreadSheetCellRendererAdapter(
174: new SimpleRenderer()));
175: tc.setCellEditor(new SpreadSheetCellEditorAdapter(
176: new SpinEditor(field)));
177: } else if (field.isRate()) {
178: tc
179: .setCellRenderer(new SpreadSheetCellRendererAdapter(
180: new RateRenderer()));
181: tc.setCellEditor(new SpreadSheetCellEditorAdapter(
182: new RateEditor(null, field.isMoney(), field
183: .isPercent(), true)));
184: } else if (field.isMoney()) {
185: tc
186: .setCellRenderer(new SpreadSheetCellRendererAdapter(
187: new SimpleRenderer()));
188: tc.setCellEditor(new SpreadSheetCellEditorAdapter(
189: new MoneyEditor()));
190: } else if (field.isPercent()) {
191: tc
192: .setCellRenderer(new SpreadSheetCellRendererAdapter(
193: new PercentRenderer()));
194: } else if (field.isDate()) {
195: tc
196: .setCellRenderer(new SpreadSheetCellRendererAdapter(
197: new DateRenderer()));
198: } else if (field.isBoolean()) {
199: tc
200: .setCellRenderer(new SpreadSheetCellRendererAdapter(
201: new OfflineCapableBooleanRenderer()));
202: } else {
203: //SimpleRenderer in other cases, LC 8/2006
204: tc
205: .setCellRenderer(new SpreadSheetCellRendererAdapter(
206: new SimpleRenderer()));
207: tc.setPreferredWidth(field.getColumnWidth(svg));
208: }
209: }
210: if (colWidthList != null)
211: tc.setPreferredWidth(colWidthList.get(columnIndex));
212: colWidth += tc.getPreferredWidth();
213: }
214: columnIndex++;
215: }
216:
217: public void removeColumn(TableColumn column) {
218: columnIndex--;
219: super .removeColumn(column);
220: if (columnIndex == 1)
221: columnIndex = 0;
222: }
223:
224: /***************************************************************************
225: * @see javax.swing.table.TableColumnModel#moveColumn(int, int)
226: */
227: public void moveColumn(int columnIndex, int newIndex) {
228: if (newIndex != -1)
229: super .moveColumn(columnIndex, newIndex);
230:
231: if (columnIndex == newIndex)
232: return;
233: SpreadSheetFieldArray f = (SpreadSheetFieldArray) getFieldArray();
234: fieldArray = f.move(columnIndex + 1, newIndex + 1);
235: }
236:
237: public ArrayList getFieldArray() {
238: return fieldArray;
239: }
240:
241: public void setFieldArray(ArrayList fieldArray) {
242: this .fieldArray = fieldArray;
243: originalFieldArray = (ArrayList) fieldArray.clone();
244: }
245:
246: public int getColWidth() {
247: return colWidth;
248: }
249:
250: public boolean isSvg() {
251: return svg;
252: }
253:
254: public void setSvg(boolean svg) {
255: this .svg = svg;
256: }
257:
258: /**
259: * Normally, JTable automatically translates columns to take care of any columns that may have been moved
260: * However, sometimes, such as when a column is determines from a mouse event, the column is not translated.
261: * @param col
262: * @return
263: */
264: public Field getFieldInNonTranslatedColumn(int col) {
265: return (Field) fieldArray.get(col);
266: }
267:
268: public Field getFieldInColumn(int col) {
269: // return (Field)fieldArray.get(col);
270: return (Field) originalFieldArray.get(col);
271:
272: // if (col >= getColumnCount()) // on initializing
273: // return (Field) fieldArray.get(col);
274: // if (col == 0) // the 0th column isn't displayed and isn't in the table, but calls are made to it
275: // return (Field) fieldArray.get(col);
276: // return (Field) getColumn(col - 1).getIdentifier();
277: }
278:
279: public int findFieldColumn(Field field) {
280: return originalFieldArray.indexOf(field);
281: // Enumeration i = getColumns();
282: // int count = 0;
283: // while (i.hasMoreElements()) {
284: // count++;
285: // TableColumn col = (TableColumn) i.nextElement();
286: // if (col.getIdentifier() == field)
287: // return count;
288: // }
289: // if (field == fieldArray.get(0)) // in case hidden 0th column
290: // return 0;
291: // return -1;
292: }
293:
294: public int getFieldColumnCount() {
295: return getFieldArray().size();
296: }
297:
298: // @Override
299: // protected void fireColumnSelectionChanged(ListSelectionEvent lse) {
300: // System.out.println("Model: "+((lse.getValueIsAdjusting())?"lse=":"LSE=")+lse.getFirstIndex()+", "+lse.getLastIndex());
301: // super.fireColumnSelectionChanged(lse);
302: // }
303:
304: }
|