001: package org.columba.calendar.ui.box;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Point;
005: import java.awt.event.ActionEvent;
006: import java.awt.event.ActionListener;
007: import java.awt.event.MouseAdapter;
008: import java.awt.event.MouseEvent;
009: import java.util.ArrayList;
010: import java.util.Iterator;
011: import java.util.List;
012:
013: import javax.swing.BorderFactory;
014: import javax.swing.ImageIcon;
015: import javax.swing.JComponent;
016: import javax.swing.JLabel;
017: import javax.swing.JMenuItem;
018: import javax.swing.JOptionPane;
019: import javax.swing.JPanel;
020: import javax.swing.JPopupMenu;
021: import javax.swing.JScrollPane;
022:
023: import org.columba.calendar.model.api.IComponent;
024: import org.columba.calendar.model.api.IComponentInfo;
025: import org.columba.calendar.model.api.IComponentInfoList;
026: import org.columba.calendar.model.api.IEvent;
027: import org.columba.calendar.model.api.IEventInfo;
028: import org.columba.calendar.store.CalendarStoreFactory;
029: import org.columba.calendar.store.api.ICalendarStore;
030: import org.columba.calendar.store.api.StoreException;
031: import org.columba.calendar.ui.dialog.EditEventDialog;
032: import org.columba.core.gui.base.DoubleClickListener;
033: import org.columba.core.gui.base.IconTextField;
034: import org.columba.core.gui.frame.api.IComponentBox;
035: import org.columba.core.resourceloader.IconKeys;
036: import org.columba.core.resourceloader.ImageLoader;
037:
038: import com.jgoodies.forms.builder.DefaultFormBuilder;
039: import com.jgoodies.forms.layout.FormLayout;
040:
041: public class CalendarBox extends JPanel implements IComponentBox {
042:
043: private final static ImageIcon icon = ImageLoader
044: .getSmallIcon(IconKeys.EDIT_FIND);
045:
046: private IconTextField textField;
047:
048: private JLabel label;
049:
050: private CalendarList list;
051:
052: private JPopupMenu contextMenu;
053:
054: public CalendarBox() {
055:
056: setLayout(new BorderLayout());
057:
058: label = new JLabel("Quick Find:");
059: label.setDisplayedMnemonic('F');
060:
061: textField = new IconTextField(icon, 10);
062: label.setLabelFor(textField);
063:
064: list = new CalendarList();
065: list.setModel(new FilteringModel());
066: List<IEventInfo> eventList = populateListModel("work");
067: list.addAll(eventList);
068:
069: list.installJTextField(textField);
070:
071: CalendarMenu popup = new CalendarMenu(new ActionListener() {
072: public void actionPerformed(ActionEvent event) {
073: String calendarId = event.getActionCommand();
074:
075: List<IEventInfo> eventList = populateListModel(calendarId);
076: list.setModel(new FilteringModel());
077: list.addAll(eventList);
078: list.installJTextField(textField);
079: textField.setText(textField.getText());
080: }
081: });
082: textField.setPopupMenu(popup);
083:
084: list.addMouseListener(new DoubleClickListener() {
085:
086: @Override
087: public void doubleClick(MouseEvent event) {
088: IEventInfo selected = (IEventInfo) list
089: .getSelectedValue();
090: openEditCalendarEventDialog(selected);
091: }
092: });
093:
094: list.add(getPopupMenu());
095: list.addMouseListener(new MyMouseListener());
096:
097: setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
098:
099: JPanel p = new JPanel();
100: p.setBorder(BorderFactory.createEmptyBorder(0, 0, 4, 0));
101: FormLayout layout = new FormLayout(
102: "pref, 2dlu, fill:default:grow",
103: // 2 columns
104: "fill:default:grow");
105:
106: // create a form builder
107: DefaultFormBuilder builder = new DefaultFormBuilder(layout, p);
108: builder.append(label);
109: builder.append(textField);
110: add(p, BorderLayout.NORTH);
111:
112: JScrollPane scrollPane = new JScrollPane(list);
113: add(scrollPane, BorderLayout.CENTER);
114: }
115:
116: private JPopupMenu getPopupMenu() {
117: if (contextMenu != null)
118: return contextMenu;
119:
120: contextMenu = new JPopupMenu();
121:
122: JMenuItem item = new JMenuItem("Open..");
123: item.addActionListener(new ActionListener() {
124: public void actionPerformed(ActionEvent event) {
125: IEventInfo selected = (IEventInfo) list
126: .getSelectedValue();
127: openEditCalendarEventDialog(selected);
128: }
129: });
130:
131: contextMenu.add(item);
132: return contextMenu;
133: }
134:
135: private List<IEventInfo> populateListModel(String calendarId) {
136: ICalendarStore store = CalendarStoreFactory.getInstance()
137: .getLocaleStore();
138:
139: IComponentInfoList infoList = store
140: .getComponentInfoList(calendarId);
141: List<IEventInfo> eventList = new ArrayList<IEventInfo>();
142: Iterator<IComponentInfo> it = infoList.iterator();
143: while (it.hasNext()) {
144: IComponentInfo info = it.next();
145: if (info.getType().equals(IComponent.TYPE.EVENT)) {
146: eventList.add((IEventInfo) info);
147: }
148: }
149: return eventList;
150: }
151:
152: public String getDescription() {
153: return "Calendar";
154: }
155:
156: public ImageIcon getIcon() {
157: return ImageLoader.getSmallIcon(IconKeys.CALENDAR);
158: }
159:
160: public String getTechnicalName() {
161: return "calendar_box";
162: }
163:
164: public String getName() {
165: return "Calendar";
166: }
167:
168: public JComponent getView() {
169: return this ;
170: }
171:
172: private void openEditCalendarEventDialog(IEventInfo selected) {
173:
174: ICalendarStore store = CalendarStoreFactory.getInstance()
175: .getLocaleStore();
176:
177: // retrieve event from store
178: try {
179: IEventInfo model = (IEventInfo) store.get(selected.getId());
180:
181: EditEventDialog dialog = new EditEventDialog(null, model);
182: if (dialog.success()) {
183: IEventInfo updatedModel = dialog.getModel();
184:
185: // update store
186: store.modify(selected.getId(), updatedModel);
187: }
188:
189: } catch (StoreException e1) {
190: JOptionPane.showMessageDialog(this , e1.getMessage());
191: e1.printStackTrace();
192: }
193: }
194:
195: class MyMouseListener extends MouseAdapter {
196:
197: @Override
198: public void mouseClicked(MouseEvent e) {
199: handleEvent(e);
200: }
201:
202: @Override
203: public void mousePressed(MouseEvent e) {
204: handlePopupEvent(e);
205: }
206:
207: @Override
208: public void mouseReleased(MouseEvent e) {
209: handlePopupEvent(e);
210: }
211:
212: /**
213: * @param e
214: */
215: private void handlePopupEvent(MouseEvent e) {
216: Point p = e.getPoint();
217: if (e.isPopupTrigger()) {
218: // check if a single entry is selected
219: if (list.getSelectedIndices().length <= 1) {
220: // select new item
221: int index = list.locationToIndex(p);
222: list.setSelectedIndex(index);
223: }
224: // show context menu
225: getPopupMenu().show(e.getComponent(), p.x, p.y);
226: }
227: }
228:
229: /**
230: * @param e
231: */
232: private void handleEvent(MouseEvent e) {
233: }
234: }
235: }
|