001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.calendar.ui.list;
019:
020: import java.awt.Color;
021: import java.awt.event.MouseAdapter;
022: import java.awt.event.MouseEvent;
023: import java.io.InputStream;
024: import java.util.Enumeration;
025:
026: import javax.swing.DefaultListSelectionModel;
027: import javax.swing.JComponent;
028: import javax.swing.JPopupMenu;
029: import javax.swing.event.EventListenerList;
030: import javax.swing.event.ListSelectionEvent;
031: import javax.swing.event.ListSelectionListener;
032:
033: import org.columba.calendar.base.api.ICalendarItem;
034: import org.columba.calendar.config.Config;
035: import org.columba.calendar.config.api.ICalendarList;
036: import org.columba.calendar.ui.frame.api.ICalendarMediator;
037: import org.columba.calendar.ui.list.api.CalendarSelectionChangedEvent;
038: import org.columba.calendar.ui.list.api.ICalendarListView;
039: import org.columba.calendar.ui.list.api.ICalendarSelectionChangedListener;
040: import org.columba.core.gui.menu.ExtendablePopupMenu;
041: import org.columba.core.gui.menu.MenuXMLDecoder;
042:
043: import com.miginfocom.ashape.AShapeUtil;
044: import com.miginfocom.ashape.shapes.AShape;
045: import com.miginfocom.calendar.category.Category;
046: import com.miginfocom.calendar.category.CategoryDepository;
047: import com.miginfocom.util.gfx.GfxUtil;
048:
049: /**
050: * CalendarListController class
051: * @author fdietz
052: *
053: */
054: public class CalendarListController implements ICalendarListView,
055: ListSelectionListener {
056:
057: private CheckableList list;
058:
059: public static final String PROP_FILTERED = "filterRow";
060:
061: private ICalendarMediator frameMediator;
062:
063: private CheckableItemListTableModel model;
064:
065: private ICalendarItem selection;
066:
067: private Category localCategory;
068:
069: private Category webCategory;
070:
071: private EventListenerList listenerList = new EventListenerList();
072:
073: private ExtendablePopupMenu menu;
074:
075: public CalendarListController(ICalendarMediator frameMediator) {
076: super ();
077:
078: this .frameMediator = frameMediator;
079:
080: model = new CheckableItemListTableModel();
081: list = new CheckableList();
082: list.setModel(model);
083: list.getSelectionModel().addListSelectionListener(this );
084:
085: // create default root nodes <Local> and <Web>
086: Category rootCategory = CategoryDepository.getRoot();
087:
088: localCategory = rootCategory.addSubCategory("local", "Local");
089: webCategory = rootCategory.addSubCategory("web", "Web");
090:
091: loadCalendarPreferences();
092:
093: list.addMouseListener(new MyMouseListener());
094: }
095:
096: /**
097: * Get popup menu
098: *
099: * @return popup menu
100: */
101: public JPopupMenu getPopupMenu() {
102: return menu;
103: }
104:
105: /**
106: * create the PopupMenu
107: */
108: public void createPopupMenu(ICalendarMediator mediator) {
109:
110: InputStream 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 e) {
121: if (e.getValueIsAdjusting()) {
122: return;
123: }
124:
125: DefaultListSelectionModel theList = (DefaultListSelectionModel) 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<ICalendarItem> e = list.getElements();
146: while (e.hasMoreElements()) {
147: ICalendarItem item = e.nextElement();
148:
149: Category category = createCalendar(item.getId(), item
150: .getName(), item.getColor().getRGB(), item
151: .getType());
152:
153: // if (calendarId.equals("work"))
154: // category.setPropertyDeep(Category.PROP_IS_HIDDEN, Boolean
155: // .valueOf(false), Boolean.TRUE);
156: // else
157: // category.setPropertyDeep(Category.PROP_IS_HIDDEN, Boolean
158: // .valueOf(true), Boolean.TRUE);
159:
160: // category filtering is disabled as default
161: category.setPropertyDeep(Category.PROP_IS_HIDDEN, Boolean
162: .valueOf(true), Boolean.TRUE);
163:
164: // calendar is selected as default
165: item.setSelected(true);
166:
167: model.addElement(item);
168:
169: }
170:
171: }
172:
173: /**
174: * @param calendarId
175: * @param name
176: * @param colorInt
177: * @param type
178: */
179: public Category createCalendar(String calendarId, String name,
180: int colorInt, ICalendarItem.TYPE type) {
181:
182: Category calendar = null;
183: if (type == ICalendarItem.TYPE.LOCAL)
184: calendar = localCategory.addSubCategory(calendarId, name);
185: else if (type == ICalendarItem.TYPE.WEB)
186: calendar = webCategory.addSubCategory(calendarId, name);
187:
188: String bgName = AShapeUtil.DEFAULT_BACKGROUND_SHAPE_NAME;
189: String outlineName = AShapeUtil.DEFAULT_OUTLINE_SHAPE_NAME;
190: String titleName = AShapeUtil.DEFAULT_TITLE_TEXT_SHAPE_NAME;
191: String textName = AShapeUtil.DEFAULT_MAIN_TEXT_SHAPE_NAME;
192:
193: Color color = new Color(colorInt);
194: Color outlineColor = GfxUtil.tintColor(color, -0.4f);
195:
196: CategoryDepository.setOverride(calendarId, bgName,
197: AShape.A_PAINT, GfxUtil.alphaColor(color, 145));
198: CategoryDepository.setOverride(calendarId, outlineName,
199: AShape.A_PAINT, outlineColor);
200: CategoryDepository.setOverride(calendarId, titleName,
201: AShape.A_PAINT, outlineColor);
202: CategoryDepository.setOverride(calendarId, textName,
203: AShape.A_PAINT, outlineColor);
204:
205: return calendar;
206: }
207:
208: /* (non-Javadoc)
209: * @see org.columba.calendar.ui.list.api.ICalendarListView#getView()
210: */
211: public JComponent getView() {
212: return list;
213: }
214:
215: class MyMouseListener extends MouseAdapter {
216:
217: MyMouseListener() {
218:
219: }
220:
221: /**
222: * @see java.awt.event.MouseAdapter#mouseClicked(java.awt.event.MouseEvent)
223: */
224: @Override
225: public void mouseClicked(MouseEvent e) {
226: handleEvent(e);
227: }
228:
229: /**
230: * @see java.awt.event.MouseAdapter#mousePressed(java.awt.event.MouseEvent)
231: */
232: @Override
233: public void mousePressed(MouseEvent e) {
234: handleEvent(e);
235: }
236:
237: /**
238: * @see java.awt.event.MouseAdapter#mouseReleased(java.awt.event.MouseEvent)
239: */
240: @Override
241: public void mouseReleased(MouseEvent e) {
242: handleEvent(e);
243: }
244:
245: /**
246: * @param e
247: */
248: private void handleEvent(MouseEvent e) {
249: CheckableItemListTableModel model = (CheckableItemListTableModel) list
250: .getModel();
251:
252: int count = model.getRowCount();
253: for (int i = 0; i < count; i++) {
254: ICalendarItem item = (ICalendarItem) model
255: .getElement(i);
256: String calendarId = item.getId();
257: boolean selected = item.isSelected();
258:
259: Category category = CategoryDepository
260: .getCategory(calendarId);
261: if (category == null)
262: continue;
263:
264: category.setPropertyDeep(Category.PROP_IS_HIDDEN,
265: Boolean.valueOf(!selected), Boolean.TRUE);
266:
267: }
268:
269: frameMediator.fireFilterUpdated();
270:
271: }
272: }
273:
274: /* (non-Javadoc)
275: * @see org.columba.calendar.ui.list.api.ICalendarListView#getSelected()
276: */
277: public ICalendarItem getSelected() {
278: if (selection == null)
279: return null;
280:
281: return selection;
282: }
283:
284: /**
285: * Adds a listener.
286: */
287: public void addSelectionChangedListener(
288: ICalendarSelectionChangedListener listener) {
289: listenerList.add(ICalendarSelectionChangedListener.class,
290: listener);
291: }
292:
293: /**
294: * Removes a previously registered listener.
295: */
296: public void removeSelectionChangedListener(
297: ICalendarSelectionChangedListener listener) {
298: listenerList.remove(ICalendarSelectionChangedListener.class,
299: listener);
300: }
301:
302: /**
303: * Propagates an event to all registered listeners notifying them that this
304: * folder has been renamed.
305: */
306: public void fireSelectionChanged(ICalendarItem selection) {
307: CalendarSelectionChangedEvent e = new CalendarSelectionChangedEvent(
308: this , selection);
309: // Guaranteed to return a non-null array
310: Object[] listeners = listenerList.getListenerList();
311:
312: // Process the listeners last to first, notifying
313: // those that are interested in this event
314: for (int i = listeners.length - 2; i >= 0; i -= 2) {
315: if (listeners[i] == ICalendarSelectionChangedListener.class) {
316: ((ICalendarSelectionChangedListener) listeners[i + 1])
317: .selectionChanged(e);
318: }
319: }
320: }
321: }
|