001: /*
002: *
003: * JMoney - A Personal Finance Manager
004: * Copyright (c) 2007 Nigel Westbury <westbury@users.sf.net>
005: *
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: *
021: */
022:
023: package net.sf.jmoney.entrytable;
024:
025: import java.util.Comparator;
026:
027: import org.eclipse.swt.SWT;
028: import org.eclipse.swt.events.SelectionAdapter;
029: import org.eclipse.swt.events.SelectionEvent;
030: import org.eclipse.swt.widgets.Composite;
031: import org.eclipse.swt.widgets.Display;
032: import org.eclipse.swt.widgets.Label;
033: import org.eclipse.swt.widgets.Menu;
034: import org.eclipse.swt.widgets.MenuItem;
035: import org.eclipse.swt.widgets.Shell;
036:
037: /**
038: * Represents a column of data that can be displayed in the entries table,
039: * edited by the user, sorted, or used in a filter.
040: * <P>
041: * All columns are managed by an object of this class. Special
042: * implementations exist for the credit, debit, and balance columns.
043: * More generic implementations exist for the other properties.
044: */
045: public abstract class IndividualBlock<T, R> extends CellBlock<T, R> {
046:
047: /**
048: * The localized text to be shown in the header.
049: */
050: private String text;
051:
052: public IndividualBlock(String text, int minimumWidth, int weight) {
053: super (minimumWidth, weight);
054: this .text = text;
055: }
056:
057: @Override
058: public void createHeaderControls(Composite parent, T entryData) {
059: Label label = new Label(parent, SWT.NULL);
060: label.setText(text);
061: label.setBackground(Display.getCurrent().getSystemColor(
062: SWT.COLOR_GRAY));
063:
064: label.setMenu(buildPopupMenu(parent.getShell()));
065: }
066:
067: @Override
068: void layout(int width) {
069: this .width = width;
070: }
071:
072: /**
073: * @return a comparator to be used for sorting rows based on the values in
074: * this column, or null if this column is not suitable for sorting
075: */
076: public Comparator<T> getComparator() {
077: return null;
078: }
079:
080: private Menu buildPopupMenu(Shell shell) {
081: // Bring up a pop-up menu.
082: // It would be a more consistent interface if this menu were
083: // linked to the column header. However, TableColumn has
084: // no setMenu method, nor does the column header respond to
085: // SWT.MenuDetect nor any other event when right clicked.
086: // This code works but does not follow the popup-menu conventions
087: // on even one platform!
088:
089: Menu popupMenu = new Menu(shell, SWT.POP_UP);
090:
091: MenuItem removeColItem = new MenuItem(popupMenu, SWT.NONE);
092:
093: MenuItem shiftColLeftItem = new MenuItem(popupMenu, SWT.NONE);
094:
095: MenuItem shiftColRightItem = new MenuItem(popupMenu, SWT.NONE);
096:
097: Object[] messageArgs = new Object[] { text };
098:
099: removeColItem.setText(new java.text.MessageFormat(
100: "Remove {0} column", java.util.Locale.US)
101: .format(messageArgs));
102: shiftColLeftItem.setText(new java.text.MessageFormat(
103: "Move {0} column left", java.util.Locale.US)
104: .format(messageArgs));
105: shiftColRightItem.setText(new java.text.MessageFormat(
106: "Move {0} column right", java.util.Locale.US)
107: .format(messageArgs));
108:
109: removeColItem.addSelectionListener(new SelectionAdapter() {
110: @Override
111: public void widgetSelected(SelectionEvent e) {
112: // TODO:
113: }
114: });
115:
116: shiftColLeftItem.addSelectionListener(new SelectionAdapter() {
117: @Override
118: public void widgetSelected(SelectionEvent e) {
119: // TODO: shift left if we can
120: }
121: });
122:
123: shiftColRightItem.addSelectionListener(new SelectionAdapter() {
124: @Override
125: public void widgetSelected(SelectionEvent e) {
126: // TODO: shift right if we can
127: }
128: });
129:
130: /* TODO: complete implementation of this.
131: * We need to allow the user to add columns?????
132:
133: new MenuItem(popupMenu, SWT.SEPARATOR);
134:
135: for (final IEntriesTableProperty entriesSectionProperty: entriesContent.getAllEntryDataObjects()) {
136: boolean found = false;
137: for (int index = 0; index < fTable.getColumnCount(); index++) {
138: IEntriesTableProperty entryData2 = (IEntriesTableProperty) (fTable
139: .getColumn(index).getData());
140: if (entryData2 == entriesSectionProperty) {
141: found = true;
142: break;
143: }
144: }
145:
146: if (!found) {
147: Object[] messageArgs2 = new Object[] { entriesSectionProperty
148: .getText() };
149:
150: MenuItem addColItem = new MenuItem(popupMenu,
151: SWT.NONE);
152: addColItem.setText(new java.text.MessageFormat(
153: "Add {0} column", java.util.Locale.US)
154: .format(messageArgs2));
155:
156: addColItem
157: .addSelectionListener(new SelectionAdapter() {
158: public void widgetSelected(
159: SelectionEvent e) {
160: addColumn(entriesSectionProperty,
161: Math.max(1, column));
162: }
163: });
164: }
165: }
166: */
167: // popupMenu.setVisible(true);
168: return popupMenu;
169: }
170:
171: public String getText() {
172: return text;
173: }
174: }
|