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.addressbook.gui.table;
019:
020: import java.awt.event.MouseAdapter;
021: import java.awt.event.MouseEvent;
022:
023: import javax.swing.JComponent;
024: import javax.swing.JTable;
025: import javax.swing.event.TreeSelectionEvent;
026: import javax.swing.event.TreeSelectionListener;
027: import javax.swing.table.JTableHeader;
028: import javax.swing.table.TableColumnModel;
029:
030: import org.columba.addressbook.facade.IContactItem;
031: import org.columba.addressbook.folder.AbstractFolder;
032: import org.columba.addressbook.folder.AddressbookTreeNode;
033: import org.columba.addressbook.folder.FolderListener;
034: import org.columba.addressbook.folder.IContactStorage;
035: import org.columba.addressbook.folder.IFolderEvent;
036: import org.columba.addressbook.gui.focus.FocusManager;
037: import org.columba.addressbook.gui.focus.FocusOwner;
038: import org.columba.addressbook.gui.frame.AddressbookFrameMediator;
039: import org.columba.addressbook.gui.table.model.AddressbookTableModel;
040: import org.columba.addressbook.gui.table.model.FilterDecorator;
041: import org.columba.addressbook.gui.table.model.SortDecorator;
042: import org.columba.addressbook.model.IContactModelPartial;
043: import org.columba.core.gui.dialog.ErrorDialog;
044: import org.columba.core.logging.Logging;
045:
046: /**
047: * @author fdietz
048: */
049: public class TableController implements TreeSelectionListener,
050: FolderListener, FocusOwner {
051:
052: private TableView view;
053:
054: private AddressbookFrameMediator mediator;
055:
056: private AddressbookTableModel addressbookModel;
057:
058: private SortDecorator sortDecorator;
059:
060: private FilterDecorator filterDecorator;
061:
062: private AddressbookTreeNode selectedFolder;
063:
064: /**
065: *
066: */
067: public TableController(AddressbookFrameMediator mediator) {
068: super ();
069:
070: this .mediator = mediator;
071:
072: addressbookModel = new AddressbookTableModel();
073:
074: sortDecorator = new SortDecorator(addressbookModel);
075:
076: filterDecorator = new FilterDecorator(sortDecorator);
077:
078: view = new TableView(this , filterDecorator);
079:
080: addMouseListenerToHeaderInTable();
081:
082: view.addMouseListener(new TableMouseListener(this ));
083:
084: // register as focus owner
085: FocusManager.getInstance().registerComponent(this );
086:
087: }
088:
089: /**
090: * Add MouseListener to JTableHeader to sort table based on clicked column
091: * header.
092: *
093: */
094: protected void addMouseListenerToHeaderInTable() {
095: final JTable tableView = getView();
096:
097: tableView.setColumnSelectionAllowed(false);
098:
099: MouseAdapter listMouseListener = new MouseAdapter() {
100: public void mouseClicked(MouseEvent e) {
101: TableColumnModel columnModel = tableView
102: .getColumnModel();
103: int viewColumn = columnModel
104: .getColumnIndexAtX(e.getX());
105: int column = tableView
106: .convertColumnIndexToModel(viewColumn);
107:
108: if ((e.getClickCount() == 1) && (column != -1)) {
109:
110: if (sortDecorator.getSelectedColumn() == column)
111: sortDecorator.setSortOrder(!sortDecorator
112: .isSortOrder());
113:
114: sortDecorator.sort(column);
115:
116: }
117: }
118: };
119:
120: JTableHeader th = tableView.getTableHeader();
121: th.addMouseListener(listMouseListener);
122: }
123:
124: /**
125: * @return AddressbookFrameController
126: */
127: public AddressbookFrameMediator getMediator() {
128: return mediator;
129: }
130:
131: /**
132: * @return TableView
133: */
134: public TableView getView() {
135: return view;
136: }
137:
138: public void setSelectedFolder(IContactStorage store) {
139: filterDecorator.setContactItemMap(((AbstractFolder) store)
140: .getContactItemMap());
141: }
142:
143: /**
144: * Update table on tree selection changes.
145: */
146: public void valueChanged(TreeSelectionEvent e) {
147: AddressbookTreeNode node = (AddressbookTreeNode) e.getPath()
148: .getLastPathComponent();
149:
150: if (node == null) {
151: return;
152: }
153:
154: if (node instanceof IContactStorage) {
155: try {
156:
157: ((AbstractFolder) node).removeFolderListener(this );
158:
159: selectedFolder = node;
160: ((AbstractFolder) node).addFolderListener(this );
161:
162: filterDecorator
163: .setContactItemMap(((AbstractFolder) selectedFolder)
164: .getContactItemMap());
165: } catch (Exception e1) {
166: if (Logging.DEBUG)
167: e1.printStackTrace();
168:
169: ErrorDialog.createDialog(e1.getMessage(), e1);
170: }
171: } else {
172: filterDecorator.setContactItemMap(null);
173: }
174: }
175:
176: /**
177: * Get selected uids.
178: *
179: * @return
180: */
181: public String[] getUids() {
182: int[] rows = getView().getSelectedRows();
183: String[] uids = new String[rows.length];
184:
185: IContactModelPartial item;
186:
187: for (int i = 0; i < rows.length; i++) {
188: item = (IContactModelPartial) filterDecorator
189: .getContactItem(rows[i]);
190: uids[i] = item.getId();
191: }
192:
193: return uids;
194: }
195:
196: /**
197: * @return Returns the addressbookModel.
198: */
199: public AddressbookTableModel getAddressbookModel() {
200: return addressbookModel;
201: }
202:
203: public IContactItem getSelectedItem() {
204: int row = getView().getSelectedRow();
205: if (row == -1)
206: return null;
207:
208: // we use the SortDecorator, because the indices are sorted
209: IContactItem item = (IContactItem) sortDecorator
210: .getContactItem(row);
211:
212: return item;
213: }
214:
215: /** ********************** FolderListener ***************** */
216:
217: /**
218: * @see org.columba.addressbook.folder.FolderListener#itemAdded(org.columba.addressbook.folder.FolderEvent)
219: */
220: public void itemAdded(IFolderEvent e) {
221: getAddressbookModel().update();
222:
223: }
224:
225: /**
226: * @see org.columba.addressbook.folder.FolderListener#itemChanged(org.columba.addressbook.folder.FolderEvent)
227: */
228: public void itemChanged(IFolderEvent e) {
229: getAddressbookModel().update();
230:
231: }
232:
233: /**
234: * @see org.columba.addressbook.folder.FolderListener#itemRemoved(org.columba.addressbook.folder.FolderEvent)
235: */
236: public void itemRemoved(IFolderEvent e) {
237: getAddressbookModel().update();
238:
239: }
240:
241: /** ************* FocusOwner Implementation ****************** */
242:
243: /**
244: * @see org.columba.addressbook.gui.focus.FocusOwner#copy()
245: */
246: public void copy() {
247:
248: }
249:
250: /**
251: * @see org.columba.addressbook.gui.focus.FocusOwner#cut()
252: */
253: public void cut() {
254:
255: }
256:
257: /**
258: * @see org.columba.addressbook.gui.focus.FocusOwner#delete()
259: */
260: public void delete() {
261:
262: }
263:
264: /**
265: * @see org.columba.addressbook.gui.focus.FocusOwner#getComponent()
266: */
267: public JComponent getComponent() {
268: return getView();
269: }
270:
271: /**
272: * @see org.columba.addressbook.gui.focus.FocusOwner#isCopyActionEnabled()
273: */
274: public boolean isCopyActionEnabled() {
275:
276: return false;
277: }
278:
279: /**
280: * @see org.columba.addressbook.gui.focus.FocusOwner#isCutActionEnabled()
281: */
282: public boolean isCutActionEnabled() {
283:
284: return false;
285: }
286:
287: /**
288: * @see org.columba.addressbook.gui.focus.FocusOwner#isDeleteActionEnabled()
289: */
290: public boolean isDeleteActionEnabled() {
291:
292: return false;
293: }
294:
295: /**
296: * @see org.columba.addressbook.gui.focus.FocusOwner#isPasteActionEnabled()
297: */
298: public boolean isPasteActionEnabled() {
299:
300: return false;
301: }
302:
303: /**
304: * @see org.columba.addressbook.gui.focus.FocusOwner#isRedoActionEnabled()
305: */
306: public boolean isRedoActionEnabled() {
307:
308: return false;
309: }
310:
311: /**
312: * @see org.columba.addressbook.gui.focus.FocusOwner#isSelectAllActionEnabled()
313: */
314: public boolean isSelectAllActionEnabled() {
315: if (getView().getRowCount() > 0) {
316: return true;
317: }
318:
319: return false;
320: }
321:
322: /**
323: * @see org.columba.addressbook.gui.focus.FocusOwner#isUndoActionEnabled()
324: */
325: public boolean isUndoActionEnabled() {
326:
327: return false;
328: }
329:
330: /**
331: * @see org.columba.addressbook.gui.focus.FocusOwner#paste()
332: */
333: public void paste() {
334:
335: }
336:
337: /**
338: * @see org.columba.addressbook.gui.focus.FocusOwner#redo()
339: */
340: public void redo() {
341:
342: }
343:
344: /**
345: * @see org.columba.addressbook.gui.focus.FocusOwner#selectAll()
346: */
347: public void selectAll() {
348: getView().selectAll();
349: }
350:
351: /**
352: * @see org.columba.addressbook.gui.focus.FocusOwner#undo()
353: */
354: public void undo() {
355:
356: }
357:
358: /**
359: * @return Returns the sortDecorator.
360: */
361: public SortDecorator getSortDecorator() {
362: return sortDecorator;
363: }
364:
365: /**
366: * @return Returns the filterDecorator.
367: */
368: public FilterDecorator getFilterDecorator() {
369: return filterDecorator;
370: }
371:
372: }
|