001: package org.columba.calendar.ui.dialog;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Component;
005: import java.awt.Frame;
006: import java.awt.GridLayout;
007: import java.awt.event.ActionEvent;
008: import java.awt.event.ActionListener;
009: import java.awt.event.KeyEvent;
010:
011: import javax.swing.BorderFactory;
012: import javax.swing.ButtonGroup;
013: import javax.swing.JComboBox;
014: import javax.swing.JComponent;
015: import javax.swing.JDialog;
016: import javax.swing.JPanel;
017: import javax.swing.JRadioButton;
018: import javax.swing.JTextField;
019: import javax.swing.KeyStroke;
020: import javax.swing.SwingConstants;
021:
022: import org.columba.calendar.model.Recurrence;
023: import org.columba.calendar.model.api.IEventInfo;
024: import org.columba.calendar.model.api.IRecurrence;
025: import org.columba.calendar.ui.comp.DatePicker;
026: import org.columba.core.gui.base.ButtonWithMnemonic;
027: import org.columba.core.gui.base.SingleSideEtchedBorder;
028: import org.columba.core.gui.util.DialogHeaderPanel;
029: import org.columba.core.help.HelpManager;
030: import org.columba.core.resourceloader.IconKeys;
031: import org.columba.core.resourceloader.ImageLoader;
032: import org.columba.mail.util.MailResourceLoader;
033:
034: import com.jgoodies.forms.builder.PanelBuilder;
035: import com.jgoodies.forms.layout.CellConstraints;
036: import com.jgoodies.forms.layout.FormLayout;
037:
038: public class RecurrenceDialog extends JDialog implements ActionListener {
039:
040: private static final String RECURRENCE_DAILY = "Daily";
041:
042: private static final String RECURRENCE_WEEKLY = "Weekly";
043:
044: private static final String RECURRENCE_MONTHLY = "Monthly";
045:
046: private static final String RECURRENCE_ANNUALLY = "Annually";
047:
048: private static final IRecurrence DEFAULT_RECURRENCE = new Recurrence(
049: Recurrence.RECURRENCE_DAILY);
050:
051: private IEventInfo model;
052:
053: private boolean success = false;
054:
055: // frequency
056: JComboBox freqComboBox = new JComboBox();
057:
058: // no end
059: JRadioButton rNoEnd = new JRadioButton();
060:
061: // maximum occurrences
062: JRadioButton rMaxOccurrences = new JRadioButton();
063: JTextField maxOccurrences = new JTextField("0");
064:
065: // end date
066: JRadioButton rUntilDate = new JRadioButton();
067: DatePicker endDatePicker;
068:
069: public RecurrenceDialog(Frame parentFrame, IEventInfo model) {
070: super (parentFrame, true);
071:
072: endDatePicker = new DatePicker();
073:
074: this .model = model;
075:
076: setLayout(new BorderLayout());
077: getContentPane().add(
078: new DialogHeaderPanel("Recurrence",
079: "Edit Recurrence Properties", ImageLoader
080: .getIcon(IconKeys.USER)),
081: BorderLayout.NORTH);
082: getContentPane().add(createPanel(), BorderLayout.CENTER);
083: getContentPane().add(createButtonPanel(), BorderLayout.SOUTH);
084:
085: getRootPane().registerKeyboardAction(this , "CANCEL",
086: KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
087: JComponent.WHEN_IN_FOCUSED_WINDOW);
088:
089: updateComponents(true);
090:
091: pack();
092: setLocationRelativeTo(null);
093: setVisible(true);
094:
095: }
096:
097: private Component createPanel() {
098: JPanel jpanel1 = new JPanel();
099: FormLayout formlayout1 = new FormLayout(
100: "3dlu,FILL:DEFAULT:NONE,FILL:DEFAULT:GROW(1.0),3dlu",
101: "3dlu,CENTER:DEFAULT:NONE,3dlu,FILL:DEFAULT:NONE,6dlu,FILL:DEFAULT:NONE,3dlu,FILL:DEFAULT:NONE,3dlu,FILL:DEFAULT:NONE,3dlu,FILL:DEFAULT:NONE,3dlu");
102: CellConstraints cc = new CellConstraints();
103: jpanel1.setLayout(formlayout1);
104:
105: PanelBuilder b = new PanelBuilder(formlayout1);
106: b.setDefaultDialogBorder();
107:
108: b.addSeparator("Recurrence", cc.xywh(2, 2, 2, 1));
109:
110: freqComboBox.addItem(RECURRENCE_DAILY);
111: freqComboBox.addItem(RECURRENCE_WEEKLY);
112: freqComboBox.addItem(RECURRENCE_MONTHLY);
113: freqComboBox.addItem(RECURRENCE_ANNUALLY);
114:
115: b.add(freqComboBox, cc.xy(3, 4));
116:
117: b.addSeparator("End", cc.xywh(2, 6, 2, 1));
118:
119: rNoEnd.setActionCommand("forever");
120: rNoEnd.addActionListener(this );
121: rNoEnd.setText("Forever");
122: b.add(rNoEnd, cc.xy(3, 8));
123:
124: b.add(createMaxOccurrencesPanel(), cc.xy(3, 10));
125:
126: b.add(createUntilDatePanel(), cc.xy(3, 12));
127:
128: // enable all radio buttons
129: rNoEnd.setEnabled(true);
130: rMaxOccurrences.setEnabled(true);
131: rUntilDate.setEnabled(true);
132:
133: ButtonGroup group = new ButtonGroup();
134: group.add(rNoEnd);
135: group.add(rMaxOccurrences);
136: group.add(rUntilDate);
137:
138: rNoEnd.setSelected(true);
139:
140: // disable all
141: endDatePicker.setEnabled(false);
142: maxOccurrences.setEnabled(false);
143:
144: return b.getPanel();
145: }
146:
147: private JPanel createMaxOccurrencesPanel() {
148:
149: JPanel panel = new JPanel();
150:
151: FormLayout formlayout1 = new FormLayout(
152: "FILL:DEFAULT:NONE,FILL:DEFAULT:NONE",
153: "CENTER:DEFAULT:NONE");
154: CellConstraints cc = new CellConstraints();
155: panel.setLayout(formlayout1);
156:
157: rMaxOccurrences.setText("Maximum recurrences");
158: rMaxOccurrences.setActionCommand("maximum");
159: rMaxOccurrences.addActionListener(this );
160: panel.add(rMaxOccurrences, cc.xy(1, 1));
161:
162: maxOccurrences.setColumns(2);
163: panel.add(maxOccurrences, cc.xy(2, 1));
164:
165: return panel;
166: }
167:
168: private JPanel createUntilDatePanel() {
169: JPanel panel = new JPanel();
170:
171: FormLayout formlayout1 = new FormLayout(
172: "FILL:DEFAULT:NONE,FILL:DEFAULT:NONE",
173: "CENTER:DEFAULT:NONE");
174: CellConstraints cc = new CellConstraints();
175: panel.setLayout(formlayout1);
176:
177: rUntilDate.setText("Until ");
178: rUntilDate.setActionCommand("until");
179: rUntilDate.addActionListener(this );
180: panel.add(rUntilDate, cc.xy(1, 1));
181: panel.add(endDatePicker, cc.xy(2, 1));
182:
183: return panel;
184:
185: }
186:
187: private void updateComponents(boolean b) {
188: IRecurrence r = model.getEvent().getRecurrence();
189: if (b) {
190: if (r == null) {
191: r = DEFAULT_RECURRENCE;
192: model.getEvent().setRecurrence(r);
193: }
194: if (r.getType() == IRecurrence.RECURRENCE_DAILY) {
195: freqComboBox.setSelectedItem(RECURRENCE_DAILY);
196: } else if (r.getType() == IRecurrence.RECURRENCE_WEEKLY) {
197: freqComboBox.setSelectedItem(RECURRENCE_WEEKLY);
198: } else if (r.getType() == IRecurrence.RECURRENCE_MONTHLY) {
199: freqComboBox.setSelectedItem(RECURRENCE_MONTHLY);
200: } else if (r.getType() == IRecurrence.RECURRENCE_ANNUALLY) {
201: freqComboBox.setSelectedItem(RECURRENCE_ANNUALLY);
202: } else {
203: // it has to be set
204: freqComboBox.setSelectedItem(RECURRENCE_DAILY);
205: r.setType(IRecurrence.RECURRENCE_DAILY);
206: }
207:
208: if (r.getEndType() == IRecurrence.RECURRENCE_END_FOREVER) {
209: rNoEnd.setSelected(true);
210: maxOccurrences.setEnabled(false);
211: endDatePicker.setEnabled(false);
212: } else if (r.getEndType() == IRecurrence.RECURRENCE_END_MAXOCCURRENCES) {
213: rMaxOccurrences.setSelected(true);
214: maxOccurrences.setEnabled(true);
215: endDatePicker.setEnabled(false);
216: maxOccurrences.setText((new Integer(r
217: .getEndMaxOccurrences()).toString()));
218: } else if (r.getEndType() == IRecurrence.RECURRENCE_END_ENDDATE) {
219: rUntilDate.setSelected(true);
220: endDatePicker.setDate(r.getEndDate());
221: maxOccurrences.setEnabled(false);
222: endDatePicker.setEnabled(true);
223: } else {
224: // it has to be set
225: rNoEnd.setSelected(true);
226: maxOccurrences.setEnabled(false);
227: endDatePicker.setEnabled(false);
228: r.setEndType(IRecurrence.RECURRENCE_END_FOREVER);
229: }
230: } else {
231: if (freqComboBox.getSelectedItem().equals(RECURRENCE_DAILY)) {
232: r.setType(IRecurrence.RECURRENCE_DAILY);
233: } else if (freqComboBox.getSelectedItem().equals(
234: RECURRENCE_WEEKLY)) {
235: r.setType(IRecurrence.RECURRENCE_WEEKLY);
236: } else if (freqComboBox.getSelectedItem().equals(
237: RECURRENCE_MONTHLY)) {
238: r.setType(IRecurrence.RECURRENCE_MONTHLY);
239: } else if (freqComboBox.getSelectedItem().equals(
240: RECURRENCE_ANNUALLY)) {
241: r.setType(IRecurrence.RECURRENCE_ANNUALLY);
242: }
243:
244: if (rNoEnd.isSelected()) {
245: r.setEndType(IRecurrence.RECURRENCE_END_FOREVER);
246: } else if (rMaxOccurrences.isSelected()) {
247: r.setEndType(IRecurrence.RECURRENCE_END_MAXOCCURRENCES);
248: try {
249: r.setEndMaxOccurrences(Integer
250: .parseInt(maxOccurrences.getText()));
251: } catch (NumberFormatException e) {
252: // LOG.severe("no number!")
253: r.setEndMaxOccurrences(1);
254: }
255: } else if (rUntilDate.isSelected()) {
256: r.setEndType(IRecurrence.RECURRENCE_END_ENDDATE);
257: r.setEndDate(endDatePicker.getDate());
258: }
259: }
260: }
261:
262: private JPanel createButtonPanel() {
263: JPanel bottom = new JPanel();
264: bottom.setLayout(new BorderLayout());
265: bottom
266: .setBorder(new SingleSideEtchedBorder(
267: SwingConstants.TOP));
268:
269: ButtonWithMnemonic cancelButton = new ButtonWithMnemonic(
270: MailResourceLoader.getString("global", "cancel"));
271:
272: //$NON-NLS-1$ //$NON-NLS-2$
273: cancelButton.addActionListener(this );
274: cancelButton.setActionCommand("CANCEL"); //$NON-NLS-1$
275:
276: ButtonWithMnemonic okButton = new ButtonWithMnemonic(
277: MailResourceLoader.getString("global", "ok"));
278:
279: //$NON-NLS-1$ //$NON-NLS-2$
280: okButton.addActionListener(this );
281: okButton.setActionCommand("OK"); //$NON-NLS-1$
282: okButton.setDefaultCapable(true);
283: getRootPane().setDefaultButton(okButton);
284:
285: ButtonWithMnemonic helpButton = new ButtonWithMnemonic(
286: MailResourceLoader.getString("global", "help"));
287:
288: // associate with JavaHelp
289: HelpManager.getInstance().enableHelpOnButton(helpButton,
290: "configuring_columba");
291:
292: JPanel buttonPanel = new JPanel();
293: buttonPanel.setBorder(BorderFactory.createEmptyBorder(12, 12,
294: 12, 12));
295: buttonPanel.setLayout(new GridLayout(1, 3, 6, 0));
296: buttonPanel.add(okButton);
297: buttonPanel.add(cancelButton);
298: buttonPanel.add(helpButton);
299:
300: bottom.add(buttonPanel, BorderLayout.EAST);
301:
302: return bottom;
303: }
304:
305: public boolean success() {
306: return success;
307: }
308:
309: public void actionPerformed(ActionEvent arg0) {
310: String action = arg0.getActionCommand();
311:
312: if (action.equals("OK")) //$NON-NLS-1$
313: {
314: // check if the user entered valid data
315: success = true;
316:
317: updateComponents(false);
318:
319: setVisible(false);
320: } else if (action.equals("CANCEL")) {
321: success = false;
322: setVisible(false);
323: } else if (action.equals("maximum")) {
324: // disable date picker
325: endDatePicker.setEnabled(false);
326: // enable max occurrences
327: maxOccurrences.setEnabled(true);
328: } else if (action.equals("until")) {
329: // disable max occurrences
330: maxOccurrences.setEnabled(false);
331: // enable until
332: endDatePicker.setEnabled(true);
333: } else if (action.equals("forever")) {
334: // disable other
335: endDatePicker.setEnabled(false);
336: maxOccurrences.setEnabled(false);
337: }
338: }
339:
340: /**
341: * @return Returns the model.
342: */
343: public IEventInfo getModel() {
344: return model;
345: }
346:
347: }
|