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.Color;
053: import java.awt.Component;
054: import java.awt.Frame;
055: import java.awt.Point;
056: import java.awt.event.KeyAdapter;
057: import java.awt.event.KeyEvent;
058: import java.awt.event.MouseAdapter;
059: import java.awt.event.MouseEvent;
060: import java.util.HashSet;
061: import java.util.List;
062: import java.util.Set;
063:
064: import javax.swing.DefaultListSelectionModel;
065: import javax.swing.JComponent;
066: import javax.swing.JLabel;
067: import javax.swing.JScrollPane;
068: import javax.swing.JTable;
069: import javax.swing.ListSelectionModel;
070: import javax.swing.SwingUtilities;
071: import javax.swing.UIManager;
072: import javax.swing.plaf.UIResource;
073: import javax.swing.table.AbstractTableModel;
074: import javax.swing.table.DefaultTableCellRenderer;
075: import javax.swing.table.DefaultTableColumnModel;
076: import javax.swing.table.JTableHeader;
077: import javax.swing.table.TableColumn;
078:
079: import org.apache.commons.collections.Closure;
080:
081: import com.jgoodies.forms.builder.DefaultFormBuilder;
082: import com.jgoodies.forms.layout.CellConstraints;
083: import com.jgoodies.forms.layout.FormLayout;
084: import com.projity.field.FieldConverter;
085: import com.projity.pm.task.Project;
086: import com.projity.pm.task.ProjectFactory;
087: import com.projity.server.data.DataObject;
088: import com.projity.server.data.ProjectData;
089: import com.projity.strings.Messages;
090:
091: public final class OpenProjectDialog extends AbstractDialog {
092: private static final long serialVersionUID = 1L;
093: private Object[] form;
094: private List projects;
095: private Set currentProjectIds;
096: private boolean allowMaster = true;
097: private boolean allowOpenAs;
098: //private User user;
099:
100: //ActionJList list;
101: OpenProjectTable table;
102: JScrollPane scrollPane;
103:
104: public static OpenProjectDialog getInstance(Frame owner,
105: List projects, String title, boolean allowMaster,
106: boolean allowOpenAs, Project anyProjectButThisOne) {
107: return new OpenProjectDialog(owner, projects, title,
108: allowMaster, allowOpenAs, anyProjectButThisOne);
109: }
110:
111: private OpenProjectDialog(Frame owner, List projects, String title,
112: boolean allowMaster, boolean allowOpenAs,
113: Project anyProjectButThisOne) {
114: super (owner, title, true);
115: this .allowMaster = allowMaster;
116: this .allowOpenAs = allowOpenAs;
117: this .projects = projects;
118: currentProjectIds = new HashSet();
119: if (anyProjectButThisOne != null) {
120: currentProjectIds.add(new Long(anyProjectButThisOne
121: .getUniqueId()));
122: } else {
123: ProjectFactory.getInstance().getPortfolio().forProjects(
124: new Closure() {
125: public void execute(Object impl) {
126: Project project = (Project) impl;
127: currentProjectIds.add(new Long(project
128: .getUniqueId()));
129: }
130: });
131: }
132: }
133:
134: protected void initControls() {
135: // Vector v=new Vector();
136: // v.addAll(projects);
137: // list=new ActionJList(v);
138: // list.addActionListener(
139: // new ActionListener() {
140: // public void actionPerformed(ActionEvent ae) {
141: // OpenProjectDialog.this.onOk();
142: // }
143: // });
144: table = new OpenProjectTable(this );
145: bind(true);
146: }
147:
148: protected void createOkCancelButtons() {
149: if (allowOpenAs)
150: createOkCancelButtons(
151: Messages.getString("ButtonText.Open"), Messages
152: .getString("ButtonText.Cancel"));
153: else
154: super .createOkCancelButtons();
155: }
156:
157: protected boolean bind(boolean get) {
158: if (get) {
159: } else {
160: int row = table.getSelectedRow();
161: if (row < 0 || row >= projects.size())
162: form = null;
163: else {
164: ProjectData project = (ProjectData) projects.get(row);
165: form = new Object[] { project, !canBeUsed(project) };
166: }
167:
168: }
169: return true;
170: }
171:
172: public JComponent createContentPanel() {
173: initControls();
174: //TODO set minimum size
175: FormLayout layout = new FormLayout("400dlu:grow", // cols //$NON-NLS-1$
176: "p"); // rows //$NON-NLS-1$
177:
178: // Create a builder that assists in adding components to the container.
179: // Wrap the panel with a standardized border.
180: DefaultFormBuilder builder = new DefaultFormBuilder(layout);
181: builder.setDefaultDialogBorder();
182: CellConstraints cc = new CellConstraints();
183: builder.add(new JScrollPane(/*list*/table));
184: return builder.getPanel();
185: }
186:
187: /**
188: * @return Returns the form.
189: */
190: public Object[] getForm() {
191: return form;
192: }
193:
194: public Object getBean() {
195: return form;
196: }
197:
198: private class OpenProjectTable extends JTable {
199: OpenProjectDialog dialog;
200:
201: public OpenProjectTable(OpenProjectDialog dialog) {
202: super (new OpenProjectTableModel(),
203: new OpenProjectableColumnModel());
204: this .dialog = dialog;
205: //setCellSelectionEnabled(true);
206:
207: getTableHeader().setDefaultRenderer(new HeaderRenderer());
208:
209: registerEditors();
210: createDefaultColumnsFromModel();
211: setSelectionModel(new OpenProjectListSelectionModel());
212: setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
213: addMouseListener();
214: addKeyListener(new KeyAdapter() { // TODO need to fix focus problems elsewhere for this to always work
215: public void keyPressed(KeyEvent e) {
216: if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
217: OpenProjectDialog.this .onCancel();
218: else if (e.getKeyCode() == KeyEvent.VK_ENTER)
219: OpenProjectDialog.this .onOk();
220: }
221: });
222:
223: }
224:
225: protected void registerEditors() {
226: //setDefaultEditor(Date.class,new DateEditor());
227: }
228:
229: public void setEnabled(boolean enabled) {
230: // TODO Auto-generated method stub
231: super .setEnabled(enabled);
232: getTableHeader().setEnabled(enabled);
233:
234: }
235:
236: private void addMouseListener() {
237: addMouseListener(new MouseAdapter() {
238: public void mousePressed(MouseEvent e) { // changed to mousePressed instead of mouseClicked() for snappier handling 17/5/04 hk
239: if (SwingUtilities.isLeftMouseButton(e)
240: && !e.isConsumed()
241: && e.getClickCount() == 2) {
242: Point p = e.getPoint();
243: int row = rowAtPoint(p);
244: int col = columnAtPoint(p);
245: if (allowOpenAs
246: || canBeUsed((ProjectData) projects
247: .get(row)))
248: dialog.onOk();
249: }
250: }
251: });
252:
253: }
254: }
255:
256: private static class HeaderRenderer extends
257: DefaultTableCellRenderer implements UIResource {
258: public HeaderRenderer() {
259: super ();
260: setHorizontalAlignment(JLabel.CENTER);
261: }
262:
263: public Component getTableCellRendererComponent(JTable table,
264: Object value, boolean isSelected, boolean hasFocus,
265: int row, int column) {
266:
267: setEnabled(table == null || table.isEnabled());
268:
269: if (table != null) {
270: JTableHeader header = table.getTableHeader();
271: if (header != null) {
272: setForeground(header.getForeground());
273: setBackground(header.getBackground());
274: setFont(header.getFont());
275: }
276: }
277:
278: setText((value == null) ? "" : value.toString()); //$NON-NLS-1$
279: setBorder(UIManager.getBorder("TableHeader.cellBorder")); //$NON-NLS-1$
280: return this ;
281: }
282: }
283:
284: private class OpenProjectTableModel extends AbstractTableModel {
285: public int getColumnCount() {
286: return 4;
287: }
288:
289: public int getRowCount() {
290: return projects.size();
291: }
292:
293: public Object getValueAt(int rowIndex, int columnIndex) {
294: ProjectData project = (ProjectData) projects.get(rowIndex);
295: // try {
296: switch (columnIndex) {
297: case 0:
298: return project.getName();
299:
300: case 1:
301: return project.getLockerInfo();
302: case 2:
303: // try {
304: return FieldConverter.toString(project
305: .getLastModificationDate());
306: // } catch (FieldParseException e) {
307: // return null;
308: // }
309:
310: case 3:
311: if (project.isMaster())
312: return null;
313: return FieldConverter.toString(project
314: .getCreationDate());
315:
316: default:
317: break;
318: }
319: return null;
320: }
321:
322: public boolean isCellEditable(int rowIndex, int columnIndex) {
323: return false;//columnIndex==0;
324: }
325:
326: public void setValueAt(Object value, int rowIndex,
327: int columnIndex) {
328: }
329:
330: public void update() {
331: fireTableDataChanged();
332: }
333: }
334:
335: private class OpenProjectableColumnModel extends
336: DefaultTableColumnModel {
337: protected int columnIndex = 0;
338:
339: public OpenProjectableColumnModel() {
340: super ();
341: }
342:
343: public void addColumn(TableColumn tc) {
344: switch (columnIndex) {
345: case 0:
346: tc.setHeaderValue(Messages
347: .getString("OpenProjectDialog.Name")); //$NON-NLS-1$
348: tc.setPreferredWidth(250);
349: break;
350:
351: case 1:
352: tc.setHeaderValue(Messages
353: .getString("OpenProjectDialog.LockedBy")); //$NON-NLS-1$
354: tc.setPreferredWidth(150);
355: break;
356:
357: case 2:
358: tc
359: .setHeaderValue(Messages
360: .getString("OpenProjectDialog.ModificationDate")); //$NON-NLS-1$
361: tc.setPreferredWidth(100);
362: break;
363:
364: case 3:
365: tc.setHeaderValue(Messages
366: .getString("OpenProjectDialog.CreationDate")); //$NON-NLS-1$
367: tc.setPreferredWidth(100);
368: break;
369:
370: default:
371: break;
372: }
373: tc.setCellRenderer(new DefaultTableCellRenderer() {
374: //protected Color defaultColor;
375: public Component getTableCellRendererComponent(
376: JTable table, Object value, boolean selected,
377: boolean focused, int row, int column) {
378: ProjectData project = (ProjectData) projects
379: .get(row);
380: setEnabled(table == null || table.isEnabled());
381: //if (defaultColor==null) defaultColor=getForeground();
382: setForeground((canBeUsed(project)) ? Color.BLACK
383: : Color.GRAY);
384: super .getTableCellRendererComponent(table, value,
385: selected, focused, row, column);
386: return this ;
387: }
388: });
389: super .addColumn(tc);
390: columnIndex++;
391: }
392:
393: //no move
394: public void moveColumn(int columnIndex, int newIndex) {
395: }
396:
397: }
398:
399: private boolean canBeUsed(ProjectData project) {
400: return project.canBeUsed()
401: && (allowMaster || !project.isMaster())
402: && !currentProjectIds.contains(new Long(project
403: .getUniqueId()));
404: }
405:
406: private class OpenProjectListSelectionModel extends
407: DefaultListSelectionModel {
408: public void setSelectionInterval(int index0, int index1) {
409: if (index0 != index1)
410: return;
411: ProjectData project = (ProjectData) projects.get(index0);
412: if (allowOpenAs)
413: ok.setText(canBeUsed(project) ? Messages
414: .getString("ButtonText.Open") : Messages
415: .getString("ButtonText.OpenCopy"));
416: else {
417: if (!canBeUsed(project)) {
418: clearSelection();
419: return;
420: }
421: }
422: super.setSelectionInterval(index0, index1);
423: }
424: }
425:
426: }
|