KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > calendar > ui > list > CalendarListController


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.calendar.ui.list;
19
20 import java.awt.Color JavaDoc;
21 import java.awt.event.MouseAdapter JavaDoc;
22 import java.awt.event.MouseEvent JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.util.Enumeration JavaDoc;
25
26 import javax.swing.DefaultListSelectionModel JavaDoc;
27 import javax.swing.JComponent JavaDoc;
28 import javax.swing.JPopupMenu JavaDoc;
29 import javax.swing.event.EventListenerList JavaDoc;
30 import javax.swing.event.ListSelectionEvent JavaDoc;
31 import javax.swing.event.ListSelectionListener JavaDoc;
32
33 import org.columba.calendar.base.api.ICalendarItem;
34 import org.columba.calendar.config.Config;
35 import org.columba.calendar.config.api.ICalendarList;
36 import org.columba.calendar.ui.frame.api.ICalendarMediator;
37 import org.columba.calendar.ui.list.api.CalendarSelectionChangedEvent;
38 import org.columba.calendar.ui.list.api.ICalendarListView;
39 import org.columba.calendar.ui.list.api.ICalendarSelectionChangedListener;
40 import org.columba.core.gui.menu.ExtendablePopupMenu;
41 import org.columba.core.gui.menu.MenuXMLDecoder;
42
43 import com.miginfocom.ashape.AShapeUtil;
44 import com.miginfocom.ashape.shapes.AShape;
45 import com.miginfocom.calendar.category.Category;
46 import com.miginfocom.calendar.category.CategoryDepository;
47 import com.miginfocom.util.gfx.GfxUtil;
48
49 /**
50  * CalendarListController class
51  * @author fdietz
52  *
53  */

54 public class CalendarListController implements ICalendarListView,
55         ListSelectionListener JavaDoc {
56
57     private CheckableList list;
58
59     public static final String JavaDoc PROP_FILTERED = "filterRow";
60
61     private ICalendarMediator frameMediator;
62
63     private CheckableItemListTableModel model;
64
65     private ICalendarItem selection;
66
67     private Category localCategory;
68
69     private Category webCategory;
70
71     private EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
72
73     private ExtendablePopupMenu menu;
74
75     public CalendarListController(ICalendarMediator frameMediator) {
76         super();
77
78         this.frameMediator = frameMediator;
79
80         model = new CheckableItemListTableModel();
81         list = new CheckableList();
82         list.setModel(model);
83         list.getSelectionModel().addListSelectionListener(this);
84
85         // create default root nodes <Local> and <Web>
86
Category rootCategory = CategoryDepository.getRoot();
87
88         localCategory = rootCategory.addSubCategory("local", "Local");
89         webCategory = rootCategory.addSubCategory("web", "Web");
90
91         loadCalendarPreferences();
92
93         list.addMouseListener(new MyMouseListener());
94     }
95
96     /**
97      * Get popup menu
98      *
99      * @return popup menu
100      */

101     public JPopupMenu JavaDoc getPopupMenu() {
102         return menu;
103     }
104
105     /**
106      * create the PopupMenu
107      */

108     public void createPopupMenu(ICalendarMediator mediator) {
109
110         InputStream JavaDoc is = this.getClass().getResourceAsStream(
111                 "/org/columba/calendar/action/contextmenu_list.xml");
112
113         menu = new MenuXMLDecoder(mediator).createPopupMenu(is);
114
115     }
116
117     /* (non-Javadoc)
118      * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
119      */

120     public void valueChanged(ListSelectionEvent JavaDoc e) {
121         if (e.getValueIsAdjusting()) {
122             return;
123         }
124
125         DefaultListSelectionModel JavaDoc theList = (DefaultListSelectionModel JavaDoc) e
126                 .getSource();
127         if (!theList.isSelectionEmpty()) {
128             int index = theList.getAnchorSelectionIndex();
129
130             selection = (ICalendarItem) ((CheckableItemListTableModel) list
131                     .getModel()).getElement(index);
132
133             fireSelectionChanged(selection);
134
135         } else {
136             fireSelectionChanged(null);
137         }
138     }
139
140     /**
141      *
142      */

143     private void loadCalendarPreferences() {
144         ICalendarList list = Config.getInstance().getCalendarList();
145         Enumeration JavaDoc<ICalendarItem> e = list.getElements();
146         while (e.hasMoreElements()) {
147             ICalendarItem item = e.nextElement();
148
149             Category category = createCalendar(item.getId(), item.getName(),
150                     item.getColor().getRGB(), item.getType());
151
152             // if (calendarId.equals("work"))
153
// category.setPropertyDeep(Category.PROP_IS_HIDDEN, Boolean
154
// .valueOf(false), Boolean.TRUE);
155
// else
156
// category.setPropertyDeep(Category.PROP_IS_HIDDEN, Boolean
157
// .valueOf(true), Boolean.TRUE);
158

159             // category filtering is disabled as default
160
category.setPropertyDeep(Category.PROP_IS_HIDDEN, Boolean
161                     .valueOf(true), Boolean.TRUE);
162
163             // calendar is selected as default
164
item.setSelected(true);
165
166             model.addElement(item);
167
168         }
169
170     }
171
172     /**
173      * @param calendarId
174      * @param name
175      * @param colorInt
176      * @param type
177      */

178     public Category createCalendar(String JavaDoc calendarId, String JavaDoc name,
179             int colorInt, ICalendarItem.TYPE type) {
180
181         Category calendar = null;
182         if (type == ICalendarItem.TYPE.LOCAL)
183             calendar = localCategory.addSubCategory(calendarId, name);
184         else if (type == ICalendarItem.TYPE.WEB)
185             calendar = webCategory.addSubCategory(calendarId, name);
186
187         String JavaDoc bgName = AShapeUtil.DEFAULT_BACKGROUND_SHAPE_NAME;
188         String JavaDoc outlineName = AShapeUtil.DEFAULT_OUTLINE_SHAPE_NAME;
189         String JavaDoc titleName = AShapeUtil.DEFAULT_TITLE_TEXT_SHAPE_NAME;
190         String JavaDoc textName = AShapeUtil.DEFAULT_MAIN_TEXT_SHAPE_NAME;
191
192         Color JavaDoc color = new Color JavaDoc(colorInt);
193         Color JavaDoc outlineColor = GfxUtil.tintColor(color, -0.4f);
194
195         CategoryDepository.setOverride(calendarId, bgName, AShape.A_PAINT,
196                 GfxUtil.alphaColor(color, 145));
197         CategoryDepository.setOverride(calendarId, outlineName, AShape.A_PAINT,
198                 outlineColor);
199         CategoryDepository.setOverride(calendarId, titleName, AShape.A_PAINT,
200                 outlineColor);
201         CategoryDepository.setOverride(calendarId, textName, AShape.A_PAINT,
202                 outlineColor);
203
204         return calendar;
205     }
206
207     /* (non-Javadoc)
208      * @see org.columba.calendar.ui.list.api.ICalendarListView#getView()
209      */

210     public JComponent JavaDoc getView() {
211         return list;
212     }
213
214     class MyMouseListener extends MouseAdapter JavaDoc {
215
216         MyMouseListener() {
217
218         }
219
220         /**
221          * @see java.awt.event.MouseAdapter#mouseClicked(java.awt.event.MouseEvent)
222          */

223         @Override JavaDoc
224         public void mouseClicked(MouseEvent JavaDoc e) {
225             handleEvent(e);
226         }
227
228         /**
229          * @see java.awt.event.MouseAdapter#mousePressed(java.awt.event.MouseEvent)
230          */

231         @Override JavaDoc
232         public void mousePressed(MouseEvent JavaDoc e) {
233             handleEvent(e);
234         }
235
236         /**
237          * @see java.awt.event.MouseAdapter#mouseReleased(java.awt.event.MouseEvent)
238          */

239         @Override JavaDoc
240         public void mouseReleased(MouseEvent JavaDoc e) {
241             handleEvent(e);
242         }
243
244         /**
245          * @param e
246          */

247         private void handleEvent(MouseEvent JavaDoc e) {
248             CheckableItemListTableModel model = (CheckableItemListTableModel) list
249                     .getModel();
250
251             int count = model.getRowCount();
252             for (int i = 0; i < count; i++) {
253                 ICalendarItem item = (ICalendarItem) model.getElement(i);
254                 String JavaDoc calendarId = item.getId();
255                 boolean selected = item.isSelected();
256
257                 Category category = CategoryDepository.getCategory(calendarId);
258                 if (category == null)
259                     continue;
260
261                 category.setPropertyDeep(Category.PROP_IS_HIDDEN, Boolean
262                         .valueOf(!selected), Boolean.TRUE);
263
264             }
265
266             frameMediator.fireFilterUpdated();
267
268         }
269     }
270
271     /* (non-Javadoc)
272      * @see org.columba.calendar.ui.list.api.ICalendarListView#getSelected()
273      */

274     public ICalendarItem getSelected() {
275         if (selection == null)
276             return null;
277
278         return selection;
279     }
280
281     /**
282      * Adds a listener.
283      */

284     public void addSelectionChangedListener(
285             ICalendarSelectionChangedListener listener) {
286         listenerList.add(ICalendarSelectionChangedListener.class, listener);
287     }
288
289     /**
290      * Removes a previously registered listener.
291      */

292     public void removeSelectionChangedListener(
293             ICalendarSelectionChangedListener listener) {
294         listenerList.remove(ICalendarSelectionChangedListener.class, listener);
295     }
296
297     /**
298      * Propagates an event to all registered listeners notifying them that this
299      * folder has been renamed.
300      */

301     public void fireSelectionChanged(ICalendarItem selection) {
302         CalendarSelectionChangedEvent e = new CalendarSelectionChangedEvent(
303                 this, selection);
304         // Guaranteed to return a non-null array
305
Object JavaDoc[] listeners = listenerList.getListenerList();
306
307         // Process the listeners last to first, notifying
308
// those that are interested in this event
309
for (int i = listeners.length - 2; i >= 0; i -= 2) {
310             if (listeners[i] == ICalendarSelectionChangedListener.class) {
311                 ((ICalendarSelectionChangedListener) listeners[i + 1])
312                         .selectionChanged(e);
313             }
314         }
315     }
316 }
317
Popular Tags