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.calendar;
051:
052: import java.awt.Component;
053: import java.awt.FlowLayout;
054: import java.awt.Frame;
055: import java.awt.GridLayout;
056: import java.awt.event.ActionEvent;
057: import java.awt.event.ActionListener;
058: import java.beans.PropertyChangeEvent;
059: import java.beans.PropertyChangeListener;
060: import java.text.ParseException;
061: import java.text.SimpleDateFormat;
062: import java.util.ArrayList;
063: import java.util.Calendar;
064: import java.util.GregorianCalendar;
065: import java.util.Iterator;
066: import java.util.List;
067:
068: import javax.swing.ButtonGroup;
069: import javax.swing.ComboBoxModel;
070: import javax.swing.DefaultComboBoxModel;
071: import javax.swing.DefaultListCellRenderer;
072: import javax.swing.Icon;
073: import javax.swing.JButton;
074: import javax.swing.JCheckBox;
075: import javax.swing.JComboBox;
076: import javax.swing.JComponent;
077: import javax.swing.JLabel;
078: import javax.swing.JList;
079: import javax.swing.JPanel;
080: import javax.swing.JRadioButton;
081: import javax.swing.JSplitPane;
082: import javax.swing.JTextField;
083: import javax.swing.event.DocumentEvent;
084: import javax.swing.event.DocumentListener;
085: import javax.swing.undo.UndoableEditSupport;
086:
087: import com.jgoodies.forms.builder.DefaultFormBuilder;
088: import com.jgoodies.forms.layout.CellConstraints;
089: import com.jgoodies.forms.layout.FormLayout;
090: import com.projity.configuration.Settings;
091: import com.projity.dialog.AbstractDialog;
092: import com.projity.dialog.ButtonPanel;
093: import com.projity.dialog.options.CalendarDialogBox;
094: import com.projity.options.CalendarOption;
095: import com.projity.pm.calendar.CalendarService;
096: import com.projity.pm.calendar.DayDescriptor;
097: import com.projity.pm.calendar.InvalidCalendarException;
098: import com.projity.pm.calendar.WorkRangeException;
099: import com.projity.pm.calendar.WorkingCalendar;
100: import com.projity.pm.calendar.WorkingHours;
101: import com.projity.pm.graphic.IconManager;
102: import com.projity.pm.graphic.frames.GraphicManager;
103: import com.projity.pm.task.ProjectFactory;
104: import com.projity.pm.time.HasStartAndEnd;
105: import com.projity.strings.Messages;
106: import com.projity.undo.CalendarEdit;
107: import com.projity.undo.UndoController;
108: import com.projity.util.Alert;
109: import com.projity.util.DateTime;
110:
111: /**
112: *
113: */
114: public class ChangeWorkingTimeDialogBox extends AbstractDialog {
115: private static final long serialVersionUID = 1L;
116:
117: public static class Form {
118: protected WorkingCalendar calendar;
119:
120: public WorkingCalendar getCalendar() {
121: return calendar;
122: }
123:
124: public void setCalendar(WorkingCalendar calendar) {
125: this .calendar = calendar;
126: }
127: }
128:
129: private UndoController undoController;
130: private Form form;
131: WorkingHours defaultWorkingHours = WorkingHours.getDefault();
132: boolean unsaved = false;
133: JComboBox calendarType;
134: CalendarView sdCalendar;
135: JRadioButton unknownWorkingTime;
136: JRadioButton defaultWorkingTime;
137: JRadioButton nonWorking;
138: JRadioButton working;
139: ButtonGroup datesSetting;
140: JTextField[] timeStart;
141: JTextField[] timeEnd;
142: JLabel notEditable;
143: JLabel caution;
144: boolean dirtyWorkingHours = false;
145: Intervals lastSelection = new Intervals();
146: boolean lastWeekSelection[] = new boolean[7];
147: JCheckBox test;
148: JComponent cal;
149: JButton newCalendar;
150: JButton options;
151: SimpleDateFormat hourFormat = DateTime.dateFormatInstance("H:mm"); //$NON-NLS-1$
152: JLabel basedOnText;
153: List documentCalendars;
154: List projectCalendars;
155: WorkingCalendar editedCalendar;
156: boolean restrict;
157:
158: private void setEditable(boolean editable) {
159: //TODO this code should be hooked in when calendars are persisted correclty
160: // notEditable.setVisible(!editable);
161: // caution.setVisible(editable);
162: // sdCalendar.setEnabled(editable);
163: // unknownWorkingTime.setEnabled(editable);
164: // defaultWorkingTime.setEnabled(editable);
165: // nonWorking.setEnabled(editable);
166: // working.setEnabled(editable);
167: // for (int i = 0; i < timeStart.length; i++) {
168: // timeStart[i].setEditable(editable);
169: // timeEnd[i].setEditable(editable);
170: // }
171:
172: }
173:
174: public static ChangeWorkingTimeDialogBox getInstance(Frame owner,
175: WorkingCalendar cal, List documentCalendars,
176: boolean restrict, UndoController undoController) {
177: return new ChangeWorkingTimeDialogBox(owner, cal,
178: documentCalendars, restrict, undoController);
179: }
180:
181: private ChangeWorkingTimeDialogBox(Frame owner,
182: WorkingCalendar cal, List documentCalendars,
183: boolean restrict, UndoController undoController) {
184: super (
185: owner,
186: Messages
187: .getString("ChangeWorkingTimeDialogBox.ChangeWorkingTime"), true); //$NON-NLS-1$
188: this .documentCalendars = documentCalendars;
189: this .restrict = restrict;
190: this .undoController = undoController;
191: // ProjectFactory projectFactory = ((MainFrame)owner).getProjectFactory();
192: ProjectFactory projectFactory = GraphicManager
193: .getInstance(this ).getProjectFactory();
194: ArrayList projCals = projectFactory.getPortfolio()
195: .extractCalendars();
196: projectCalendars = new ArrayList();
197: Iterator i = projCals.iterator();
198: WorkingCalendar current;
199: while (i.hasNext()) { // add all non base cals that are project cals
200: current = (WorkingCalendar) i.next();
201: if (!current.isBaseCalendar())
202: projectCalendars.add(current);
203: }
204:
205: newCalendar = new JButton(Messages
206: .getString("ChangeWorkingTimeDialogBox.New")); //$NON-NLS-1$
207: addDocHelp("Change_Working_Time_Dialog");
208:
209: form = new Form();
210: form.setCalendar(cal);
211: }
212:
213: private void setCal(WorkingCalendar cal) {
214: editedCalendar = cal;
215: form.setCalendar(CalendarService.getInstance().makeScratchCopy(
216: cal));
217: calendarType.setSelectedItem(editedCalendar);
218:
219: }
220:
221: class ListRenderer extends DefaultListCellRenderer {
222: private Icon resourceIcon = IconManager.getIcon("man"); //$NON-NLS-1$
223: private Icon greenCircle = IconManager.getIcon("greenCircle"); //$NON-NLS-1$
224:
225: public Component getListCellRendererComponent(JList arg0,
226: Object arg1, int arg2, boolean arg3, boolean arg4) {
227: // TODO Auto-generated method stub
228: Component c = super .getListCellRendererComponent(arg0,
229: arg1, arg2, arg3, arg4);
230: if (documentCalendars != null
231: && documentCalendars.contains(arg1))
232: setIcon(resourceIcon);
233: else if (projectCalendars.contains(arg1))
234: setIcon(greenCircle);
235: return c;
236: }
237: }
238:
239: private void fillInCalendarNames() {
240: ArrayList all = new ArrayList();
241: CalendarService service = CalendarService.getInstance();
242: all.addAll(service.getBaseCalendars());
243: all.addAll(projectCalendars);
244:
245: if (documentCalendars != null)
246: all.addAll(documentCalendars);
247: ComboBoxModel calModel = new DefaultComboBoxModel(all.toArray());
248: calendarType.setModel(calModel);
249: }
250:
251: private void clearLastSelection() {
252: lastSelection.clear();
253: for (int i = 0; i < 7; i++)
254: lastWeekSelection[i] = false;
255: }
256:
257: protected void initControls() {
258: calendarType = new JComboBox();
259: calendarType.setRenderer(new ListRenderer());
260: fillInCalendarNames();
261: basedOnText = new JLabel();
262:
263: sdCalendar = new CalendarView();
264:
265: unknownWorkingTime = new JRadioButton();
266: notEditable = new JLabel(
267: Messages
268: .getString("ChangeWorkingTimeDialogBox.NotEdiableMessage")); // html provides word wrap //$NON-NLS-1$
269: caution = new JLabel(
270: Messages
271: .getString("ChangeWorkingTimeDialogBox.ModificationMessage")); // html provides word wrap //$NON-NLS-1$
272:
273: // TODO persist calendars correctly and hook this up
274: notEditable.setVisible(false);
275: caution.setVisible(false);
276:
277: defaultWorkingTime = new JRadioButton(Messages
278: .getString("ChangeWorkingTimeDialogBox.UseDefault")); //$NON-NLS-1$
279: nonWorking = new JRadioButton(Messages
280: .getString("ChangeWorkingTimeDialogBox.NonWorkingTime")); //$NON-NLS-1$
281: working = new JRadioButton(
282: Messages
283: .getString("ChangeWorkingTimeDialogBox.NonDefaultWorkingTime")); //$NON-NLS-1$
284: datesSetting = new ButtonGroup();
285: datesSetting.add(unknownWorkingTime);
286: datesSetting.add(defaultWorkingTime);
287: datesSetting.add(nonWorking);
288: datesSetting.add(working);
289:
290: timeStart = new JTextField[Settings.CALENDAR_INTERVALS];
291: timeEnd = new JTextField[Settings.CALENDAR_INTERVALS];
292: DocumentListener makeDirtyListener = new DocumentListener() {
293: public void changedUpdate(DocumentEvent e) {
294: dirtyWorkingHours = true;
295: }
296:
297: public void insertUpdate(DocumentEvent e) {
298: dirtyWorkingHours = true;
299: }
300:
301: public void removeUpdate(DocumentEvent e) {
302: dirtyWorkingHours = true;
303: }
304: };
305:
306: for (int i = 0; i < timeStart.length; i++) {
307: timeStart[i] = new JTextField(""); //$NON-NLS-1$
308: timeStart[i].setEnabled(false);
309: timeStart[i].getDocument().addDocumentListener(
310: makeDirtyListener);
311: timeEnd[i] = new JTextField(""); //$NON-NLS-1$
312: timeEnd[i].setEnabled(false);
313: timeEnd[i].getDocument().addDocumentListener(
314: makeDirtyListener);
315: }
316:
317: defaultWorkingTime.addActionListener(new ActionListener() {
318: public void actionPerformed(ActionEvent e) {
319: setWorkingHours(null);
320: CalendarService service = CalendarService.getInstance();
321: WorkingCalendar wc = form.getCalendar();
322: service.makeDefaultDays(wc, sdCalendar
323: .getSelectedIntervals(), sdCalendar
324: .getSelectedWeekDays());
325: dirtyWorkingHours = false;
326: updateWorkingHours();
327: updateView();
328: clearLastSelection();
329:
330: }
331: });
332:
333: nonWorking.addActionListener(new ActionListener() {
334: public void actionPerformed(ActionEvent e) {
335: setWorkingHours(null);
336: CalendarService service = CalendarService.getInstance();
337: WorkingCalendar wc = form.getCalendar();
338: WorkingCalendar copy = wc.makeScratchCopy();
339: try {
340: // try on copy first
341: service.setDaysNonWorking(copy, sdCalendar
342: .getSelectedIntervals(), sdCalendar
343: .getSelectedWeekDays());
344: service.setDaysNonWorking(wc, sdCalendar
345: .getSelectedIntervals(), sdCalendar
346: .getSelectedWeekDays());
347: } catch (InvalidCalendarException e1) {
348: Alert.error(e1.getMessage(),
349: ChangeWorkingTimeDialogBox.this );
350: return;
351: }
352: dirtyWorkingHours = false;
353: updateWorkingHours();
354: updateView();
355: clearLastSelection();
356: }
357: });
358:
359: working.addActionListener(new ActionListener() {
360: public void actionPerformed(ActionEvent e) {
361: CalendarService service = CalendarService.getInstance();
362: WorkingCalendar wc = form.getCalendar();
363:
364: setWorkingHours(defaultWorkingHours);
365: WorkingCalendar copy = wc.makeScratchCopy();
366:
367: try {
368: service
369: .setDaysWorkingHours(copy, sdCalendar
370: .getSelectedIntervals(), sdCalendar
371: .getSelectedWeekDays(),
372: defaultWorkingHours);
373: service
374: .setDaysWorkingHours(wc, sdCalendar
375: .getSelectedIntervals(), sdCalendar
376: .getSelectedWeekDays(),
377: defaultWorkingHours);
378: dirtyWorkingHours = false;
379: updateWorkingHours();
380: updateView();
381: } catch (WorkRangeException e1) {
382: e1.printStackTrace();
383: } catch (InvalidCalendarException e2) {
384: Alert.error(e2.getMessage(),
385: ChangeWorkingTimeDialogBox.this );
386: return;
387: }
388: clearLastSelection();
389: }
390: });
391: sdCalendar
392: .addPropertyChangeListener(new PropertyChangeListener() {
393: final CalendarService service = CalendarService
394: .getInstance();
395:
396: public void propertyChange(PropertyChangeEvent e) {
397: // System.out.println("propery change");
398: String property = e.getPropertyName();
399: if ("lastDisplayedDate".equals(property) || "firstDisplayedDate".equals(property)) { //$NON-NLS-1$ //$NON-NLS-2$
400: updateView();
401: } else if ("selectedDates".equals(property)) { //$NON-NLS-1$
402: updateWorkingHours();
403: }
404: dirtyWorkingHours = false;
405: }
406: });
407: setCal(form.getCalendar());
408: // add listener at end so above setCal won't trigger update
409: calendarType.addActionListener(new ActionListener() {
410: public void actionPerformed(ActionEvent e) {
411: WorkingCalendar cal = (WorkingCalendar) calendarType
412: .getSelectedItem();
413: if (cal != form.getCalendar()) {
414: setNewCalendar(cal);
415: setEditable(isCalEditable(cal));
416: }
417: }
418: });
419:
420: calendarType.setEnabled(!restrict);
421: setEditable(isCalEditable(form.getCalendar()));
422: newCalendar.setVisible(!restrict);
423: }
424:
425: private boolean isCalEditable(WorkingCalendar cal) {
426: boolean editable = projectCalendars.contains(cal);
427: if (GraphicManager.getInstance().isEditingMasterProject()) // always editable if master project
428: editable = true;
429: return editable;
430:
431: }
432:
433: private void setNewCalendar(WorkingCalendar cal) {
434: saveIfNeeded();
435: setCal(cal);
436: updateView();
437:
438: }
439:
440: private void saveWorkingHoursChanges(boolean saveCalendar) {
441: try {
442: WorkingHours hours = new WorkingHours();
443: String startS, endS;
444: for (int i = 0; i < timeStart.length; i++) {
445: startS = timeStart[i].getText();
446: endS = timeEnd[i].getText();
447: if (startS != null && endS != null
448: && startS.length() > 0 && endS.length() > 0) {
449: hours.setInterval(i, parseTime(startS),
450: parseTime(endS));
451: } else {
452: if (startS.length() == 0 && endS.length() == 0)
453: break;
454: else {
455: Alert
456: .warn(
457: Messages
458: .getString("Message.badTimeFormat"), this ); //$NON-NLS-1$
459: return;
460: }
461: }
462: }
463: CalendarService service = CalendarService.getInstance();
464: WorkingCalendar wc = form.getCalendar();
465: WorkingCalendar copy = wc.makeScratchCopy();
466: service.setDaysWorkingHours(copy, lastSelection,
467: lastWeekSelection, hours);
468: service.setDaysWorkingHours(wc, lastSelection,
469: lastWeekSelection, hours);
470: unsaved = false;
471:
472: if (saveCalendar) {
473: saveCalendar();
474: } else {
475: unsaved = true;
476: }
477: //System.out.println("Saved "+lastSelection);
478: } catch (WorkRangeException e) {
479: Alert
480: .warn(
481: Messages
482: .getString("Message.badTimeIntervals"), this ); //$NON-NLS-1$
483: } catch (ParseException e) {
484: Alert.warn(
485: Messages.getString("Message.badTimeFormat"), this ); //$NON-NLS-1$
486: } catch (InvalidCalendarException e) {
487: Alert.warn(e.getMessage(), this );
488: return;
489: }
490: updateView();
491: }
492:
493: private void saveCalendar() {
494: unsaved = false;
495: CalendarService service = CalendarService.getInstance();
496: WorkingCalendar wc = form.getCalendar();
497: UndoableEditSupport undoableEditSupport = undoController
498: .getEditSupport();
499: if (undoableEditSupport != null) {
500: undoableEditSupport.postEdit(new CalendarEdit(
501: editedCalendar, wc));
502: }
503:
504: service.assignCalendar(editedCalendar, wc);
505: service.saveAndUpdate(editedCalendar);
506:
507: }
508:
509: public void saveIfNeeded() {
510: if (dirtyWorkingHours)
511: saveWorkingHoursChanges(true);
512: else if (unsaved)
513: saveCalendar();
514: }
515:
516: private Calendar _calendar = DateTime.calendarInstance();
517:
518: public long getTimeInMillis(int h, int m) {
519: _calendar.setTimeInMillis(0);
520: _calendar.set(Calendar.HOUR_OF_DAY, h);
521: _calendar.set(Calendar.MINUTE, m);
522: return _calendar.getTimeInMillis();
523: }
524:
525: private String formatTime(long time) {
526: GregorianCalendar cal = DateTime.calendarInstance();
527: ;
528: cal.setTimeInMillis(time);
529: // cal.roll(GregorianCalendar.HOUR_OF_DAY,false);
530: return hourFormat.format(cal.getTime());
531: }
532:
533: private long parseTime(String s) throws ParseException {
534: GregorianCalendar cal = DateTime.calendarInstance();
535: ;
536: cal.setTime(hourFormat.parse(s));
537: // cal.roll(GregorianCalendar.HOUR_OF_DAY,true);
538: return cal.getTimeInMillis();
539: }
540:
541: private void setWorkingHours(WorkingHours hours) {
542: // System.out.println("setting working hours" + hours);
543: // if not working treat as empty
544: if (hours != null && hours.getDuration() == 0)
545: hours = null;
546: for (int i = 0; i < timeStart.length; i++) {
547: timeStart[i].setEnabled(hours != null);
548: timeEnd[i].setEnabled(hours != null);
549: if (hours == null) {
550: timeStart[i].setText(""); //$NON-NLS-1$
551: timeEnd[i].setText(""); //$NON-NLS-1$
552: }
553: }
554:
555: if (hours != null) {
556: HasStartAndEnd interval;
557: // int j=0;
558: int intervals = hours.getIntervals().size();
559: for (int j = 0; j < Settings.CALENDAR_INTERVALS; j++) {
560: // for (Iterator i=hours.getIntervals().iterator();i.hasNext();j++){
561: interval = (HasStartAndEnd) hours.getInterval(j);
562: // i.next();
563: if (interval != null) {
564: String startS = formatTime(interval.getStart());
565: timeStart[j].setText(startS);
566: timeEnd[j].setText(formatTime(interval.getEnd()));
567: } else {
568: timeStart[j].setText(""); //$NON-NLS-1$
569: timeEnd[j].setText(""); //$NON-NLS-1$
570: }
571: }
572: clearLastSelection();
573: lastSelection.addAll(sdCalendar.getSelectedIntervals());
574: for (int i = 0; i < 7; i++)
575: lastWeekSelection[i] = sdCalendar.getSelectedWeekDays()[i];
576: }
577:
578: //Call at the end because setText causes dirtyWorkingHours to become true
579: dirtyWorkingHours = false;
580: }
581:
582: protected void updateView() {
583:
584: CalendarService service = CalendarService.getInstance();
585: WorkingCalendar wc = form.getCalendar();
586: if (wc.isBaseCalendar()) {
587: basedOnText.setText(" "); // a space. need a space for vertical spacing //$NON-NLS-1$
588: } else {
589: basedOnText
590: .setText(Messages
591: .getString("ChangeWorkingTimeDialogBox.BasedOn") + wc.getBaseCalendar().getName()); //$NON-NLS-1$
592: }
593:
594: long first = sdCalendar.getFirstDisplayedDate();
595: long last = sdCalendar.getLastDisplayedDate();
596: Calendar calendar = DateTime.calendarInstance();
597: calendar.setTimeInMillis(first);
598:
599: sdCalendar.setFlaggedDates(null);
600: sdCalendar.setColorDates(null);
601:
602: DayDescriptor day;
603: ArrayList flaggedDates = new ArrayList();
604: ArrayList colorDates = new ArrayList();
605: while (calendar.getTimeInMillis() <= last) {
606: day = service.getDay(wc, calendar.getTimeInMillis());
607: if (day.isModified())
608: flaggedDates.add(new Long(calendar.getTimeInMillis()));
609: if (!day.isWorking())
610: colorDates.add(new Long(calendar.getTimeInMillis()));
611: calendar.add(Calendar.DATE, 1);
612: }
613:
614: if (flaggedDates.size() > 0)
615: sdCalendar.setFlaggedDates(toLongArray(flaggedDates));
616: if (colorDates.size() > 0)
617: sdCalendar.setColorDates(toLongArray(colorDates));
618:
619: boolean colorWeekDates[] = new boolean[7];
620: boolean flaggedWeekDates[] = new boolean[7];
621: for (int i = 0; i < 7; i++) {
622: day = service.getWeekDay(wc, i + 1);
623: if (day.isModified())
624: flaggedWeekDates[i] = true;
625: if (!day.isWorking())
626: colorWeekDates[i] = true;
627: }
628: sdCalendar.setColorWeekDates(colorWeekDates);
629: sdCalendar.setFlaggedWeekDates(flaggedWeekDates);
630: // updateWorkingHours();
631: //System.out.println(service.dump(wc));
632: }
633:
634: //stupid jdnc calendar use long[]
635: public long[] toLongArray(ArrayList list) {
636: //if (list.size()==0) return null;
637: long[] array = new long[list.size()];
638: int j = 0;
639: for (Iterator i = list.iterator(); i.hasNext(); j++)
640: array[j] = ((Long) i.next()).longValue();
641: return array;
642: }
643:
644: private void updateWorkingHours() {
645: // System.out.println("updating working hours");
646: final CalendarService service = CalendarService.getInstance();
647:
648: if (dirtyWorkingHours) {
649: saveWorkingHoursChanges(false);
650: }
651:
652: DayDescriptor day = service.getDay(form.getCalendar(),
653: sdCalendar.getSelectedIntervals(), sdCalendar
654: .getSelectedWeekDays());
655: if (day == null) {
656: // System.out.println("none");
657: ChangeWorkingTimeDialogBox.this .datesSetting.setSelected(
658: ChangeWorkingTimeDialogBox.this .unknownWorkingTime
659: .getModel(), true);
660: setWorkingHours(null);
661: } else if (!day.isModified()) {
662: // System.out.println("default");
663: ChangeWorkingTimeDialogBox.this .datesSetting.setSelected(
664: ChangeWorkingTimeDialogBox.this .defaultWorkingTime
665: .getModel(), true);
666: setWorkingHours(day.getWorkingHours());
667: } else if (!day.isWorking()) {
668: // System.out.println("non working");
669: ChangeWorkingTimeDialogBox.this .datesSetting.setSelected(
670: ChangeWorkingTimeDialogBox.this .nonWorking
671: .getModel(), true);
672: setWorkingHours(null);
673: } else {
674: // System.out.println("working");
675: ChangeWorkingTimeDialogBox.this .datesSetting.setSelected(
676: ChangeWorkingTimeDialogBox.this .working.getModel(),
677: true);
678: setWorkingHours(day.getWorkingHours());
679: }
680: dirtyWorkingHours = true;
681: }
682:
683: private JComponent createSettingsPanel() {
684: FormLayout settingsLayout = new FormLayout(
685: "100dlu", //$NON-NLS-1$
686: "p,0dlu ,p,3dlu ,p,3dlu p,3dlu,p,0dlu ,p,0dlu ,p,3dlu ,p,3dlu ,p,3dlu ,p,3dlu ,p,3dlu ,p,3dlu ,p,3dlu ,p,3dlu ,p"); //$NON-NLS-1$
687: DefaultFormBuilder settingBuilder = new DefaultFormBuilder(
688: settingsLayout);
689:
690: settingBuilder.addLabel(Messages
691: .getString("ChangeWorkingTimeDialogBox.For")); //$NON-NLS-1$
692: settingBuilder.nextLine(2);
693: settingBuilder.add(calendarType);
694: settingBuilder.nextLine(2);
695: settingBuilder.add(basedOnText);
696: settingBuilder.nextLine(2);
697: settingBuilder.add(notEditable);
698: settingBuilder.add(caution);
699: settingBuilder.nextLine(2);
700: settingBuilder.add(defaultWorkingTime);
701: settingBuilder.nextLine(2);
702: settingBuilder.add(nonWorking);
703: settingBuilder.nextLine(2);
704: settingBuilder.add(working);
705: settingBuilder.nextLine(2);
706:
707: JPanel time = new JPanel();
708: time.setLayout(new GridLayout(1, 2));
709: time.add(new JLabel(Messages
710: .getString("ChangeWorkingTimeDialogBox.From"))); //$NON-NLS-1$
711: time.add(new JLabel(Messages
712: .getString("ChangeWorkingTimeDialogBox.To"))); //$NON-NLS-1$
713: settingBuilder.add(time);
714:
715: for (int i = 0; i < timeStart.length; i++) {
716: JPanel timePanel = new JPanel();
717: timePanel.setLayout(new GridLayout(1, 2));
718: timePanel.add(timeStart[i]);
719: timePanel.add(timeEnd[i]);
720: settingBuilder.nextLine(2);
721: settingBuilder.add(timePanel);
722: }
723: return settingBuilder.getPanel();
724: }
725:
726: public JComponent createContentPanel() {
727:
728: initControls();
729:
730: FormLayout layout = new FormLayout("300dlu:grow", //$NON-NLS-1$
731: "p,p,fill:260dlu:grow"); //$NON-NLS-1$
732:
733: DefaultFormBuilder builder = new DefaultFormBuilder(layout);
734: builder.setDefaultDialogBorder();
735: CellConstraints cc = new CellConstraints();
736:
737: // JSplitPane settingsPanel=new JSplitPane(JSplitPane.VERTICAL_SPLIT);
738: // settingsPanel.setTopComponent(createSettingsPanel());
739: // settingsPanel.setBottomComponent(new JPanel());
740: // settingsPanel.setDividerSize(0);
741: JSplitPane panel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
742: // panel.setLeftComponent(settingsPanel);
743: panel.setLeftComponent(createSettingsPanel());
744:
745: panel.setRightComponent(sdCalendar);
746:
747: JPanel buttonPanel = new JPanel();
748: buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
749: JButton backButton = new JButton(IconManager
750: .getIcon("calendar.back")); //$NON-NLS-1$
751: backButton.addActionListener(new ActionListener() {
752: public void actionPerformed(ActionEvent e) {
753: long first = sdCalendar.getFirstDisplayedDate();
754: Calendar calendar = DateTime.calendarInstance();
755: calendar.setTimeInMillis(sdCalendar
756: .getLastDisplayedDate());
757: int nbMonth = 0;
758: while (calendar.getTimeInMillis() > first) {
759: calendar.add(Calendar.MONTH, -1);
760: nbMonth++;
761: }
762: calendar.setTimeInMillis(first);
763: calendar.add(Calendar.MONTH, -nbMonth);
764: sdCalendar.setFirstDisplayedDate(calendar
765: .getTimeInMillis());
766: }
767: });
768: JButton todayButton = new JButton(IconManager
769: .getIcon("calendar.today")); //$NON-NLS-1$
770: todayButton.addActionListener(new ActionListener() {
771: public void actionPerformed(ActionEvent e) {
772: Calendar calendar = DateTime.calendarInstance();
773: calendar.setTimeInMillis(System.currentTimeMillis());
774: calendar.set(Calendar.DATE, 1);
775: sdCalendar.setFirstDisplayedDate(calendar
776: .getTimeInMillis());
777: }
778: });
779: JButton forwardButton = new JButton(IconManager
780: .getIcon("calendar.forward")); //$NON-NLS-1$
781: forwardButton.addActionListener(new ActionListener() {
782: public void actionPerformed(ActionEvent e) {
783: Calendar calendar = DateTime.calendarInstance();
784: calendar.setTimeInMillis(sdCalendar
785: .getLastDisplayedDate());
786: calendar.add(Calendar.DATE, 1);
787: sdCalendar.setFirstDisplayedDate(calendar
788: .getTimeInMillis());
789: }
790: });
791: buttonPanel.add(backButton);
792: buttonPanel.add(todayButton);
793: buttonPanel.add(forwardButton);
794: builder.nextLine();
795: builder.append(buttonPanel);
796: builder.nextLine();
797: builder.append(panel);
798:
799: // builder.append(newCalendar);
800:
801: return builder.getPanel();
802: }
803:
804: public Object getBean() {
805: return form;
806: }
807:
808: public ButtonPanel createButtonPanel() {
809: createOkCancelButtons();
810: newCalendar.addActionListener(new ActionListener() {
811: public void actionPerformed(ActionEvent e) {
812: NewBaseCalendarDialog dialog = NewBaseCalendarDialog
813: .getInstance(owner, null);
814: if (dialog.doModal()) {
815: fillInCalendarNames();
816: WorkingCalendar cal = dialog.getNewCalendar();
817: setNewCalendar(cal);
818: }
819: }
820: });
821:
822: options = new JButton(Messages
823: .getString("ChangeWorkingTimeDialogBox.Options")); //$NON-NLS-1$
824: options.addActionListener(new ActionListener() {
825: public void actionPerformed(ActionEvent e) {
826: CalendarOption option = CalendarOption.getInstance();
827: CalendarDialogBox dialog = CalendarDialogBox
828: .getInstance(
829: (Frame) ChangeWorkingTimeDialogBox.this
830: .getOwner(), option);
831: if (dialog.doModal()) {
832: dialog.getForm().copyToOption(option);
833: }
834: }
835: });
836:
837: ButtonPanel buttonPanel = new ButtonPanel();
838: buttonPanel.addButton(newCalendar);
839: buttonPanel.addButton(options);
840: buttonPanel.addButton(ok);
841: buttonPanel.addButton(cancel);
842: return buttonPanel;
843: }
844:
845: public void onOk() {
846: saveIfNeeded();
847: super.onOk();
848: }
849: }
|