001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.gui.composed;
019:
020: import java.awt.BorderLayout;
021: import java.awt.event.ActionEvent;
022: import java.awt.event.FocusAdapter;
023: import java.awt.event.FocusEvent;
024: import java.util.List;
025: import java.util.logging.Level;
026: import java.util.logging.Logger;
027:
028: import javax.swing.AbstractAction;
029: import javax.swing.Action;
030: import javax.swing.JScrollPane;
031: import javax.swing.ListSelectionModel;
032:
033: import de.finix.contelligent.client.base.ComponentFactory;
034: import de.finix.contelligent.client.base.ComponentNotFoundException;
035: import de.finix.contelligent.client.base.ContelligentComponent;
036: import de.finix.contelligent.client.event.ContelligentEvent;
037: import de.finix.contelligent.client.gui.AbstractComponentEditor;
038: import de.finix.contelligent.client.gui.ContelligentAction;
039: import de.finix.contelligent.client.i18n.Resources;
040:
041: public class MixedTableEditor extends AbstractComponentEditor {
042:
043: private static Logger logger = Logger
044: .getLogger(MixedTableEditor.class.getName());
045:
046: private MixedTableModel mixedTableModel;
047:
048: private MixedTable mixedTable;
049:
050: private List rows;
051:
052: private int mode;
053:
054: private ExpandTableAction expandTableAction = new ExpandTableAction();
055:
056: private CompressTableAction compressTableAction = new CompressTableAction();
057:
058: public void init() {
059: update();
060:
061: mixedTable.getSelectionModel().setSelectionMode(
062: ListSelectionModel.SINGLE_SELECTION);
063: if (mode == MixedTableModel.TABLE) {
064: mixedTable.getColumnModel().getColumn(0).setMaxWidth(120);
065: }
066:
067: JScrollPane scrollPane = new JScrollPane(mixedTable);
068: add(scrollPane, BorderLayout.CENTER);
069:
070: mixedTable.addFocusListener(new FocusAdapter() {
071: public void focusGained(FocusEvent e) {
072: getView()
073: .setActions(MixedTableEditor.this .getActions());
074: }
075: });
076: }
077:
078: public void update() {
079: if (mixedTableModel == null) {
080: mixedTableModel = new MixedTableModel(rows, mode);
081: mixedTable = new MixedTable(mixedTableModel, getView());
082: } else {
083: mixedTableModel.updateModel();
084: mixedTableModel.fireTableDataChanged();
085: }
086: }
087:
088: public void setRows(List rows) {
089: this .rows = rows;
090: }
091:
092: public void setMode(int mode) {
093: this .mode = mode;
094: }
095:
096: public void setEditable(boolean editable) {
097: super .setEditable(editable);
098: if (mixedTableModel != null) {
099: mixedTableModel.setEditable(isEditable());
100: }
101: }
102:
103: public void rollback() {
104: // reload all subcomponents that might have been edited in the table
105: List components = mixedTableModel.getComponents();
106: for (int i = 0; i < components.size(); i++) {
107: ContelligentComponent component = (ContelligentComponent) components
108: .get(i);
109: if (component.isModified()) {
110: try {
111: ComponentFactory.getInstance().reload(component);
112: } catch (ComponentNotFoundException cnfe) {
113: logger.log(Level.SEVERE, "Could not rollback: ",
114: cnfe);
115: }
116: }
117: }
118: super .rollback();
119: }
120:
121: public void commit() {
122: // save subcomponents that might have been edited in the table
123: List components = mixedTableModel.getComponents();
124: for (int i = 0; i < components.size(); i++) {
125: ComponentFactory.getInstance().save(
126: (ContelligentComponent) components.get(i));
127: }
128: super .commit();
129: }
130:
131: public Action[] getActions() {
132: if (isEditable()) {
133: return new Action[] { expandTableAction,
134: compressTableAction };
135: } else {
136: return new Action[] { expandTableAction,
137: compressTableAction };
138: }
139: }
140:
141: protected void updateComponent() {
142: }
143:
144: protected void componentChanged(ContelligentEvent event) {
145: update();
146: }
147:
148: protected void childComponentAdded(ContelligentEvent event) {
149: }
150:
151: protected void childComponentRemoved(ContelligentEvent event) {
152: }
153:
154: protected void childComponentChanged(ContelligentEvent event) {
155: }
156:
157: protected void descendentComponentChanged(ContelligentEvent event) {
158: }
159:
160: public class ExpandTableAction extends AbstractAction implements
161: ContelligentAction {
162:
163: public ExpandTableAction() {
164: super ("expand", Resources.expandIcon);
165: putValue(ROLLOVER_ICON, Resources.expandIconRollOver);
166: putValue(TYPE, PUSH_ACTION);
167: putValue(ACTION_TYPE, VIEW_ACTION);
168: putValue(MENU_TARGET, MENU);
169: putValue(BUTTON_TARGET, TOOLBAR);
170: }
171:
172: public void actionPerformed(ActionEvent e) {
173: mixedTable.increaseRowHeight();
174: mixedTable.repaint();
175: }
176: }
177:
178: public class CompressTableAction extends AbstractAction implements
179: ContelligentAction {
180:
181: public CompressTableAction() {
182: super ("compress", Resources.compressIcon);
183: putValue(ROLLOVER_ICON, Resources.compressIconRollOver);
184: putValue(TYPE, PUSH_ACTION);
185: putValue(ACTION_TYPE, VIEW_ACTION);
186: putValue(MENU_TARGET, MENU);
187: putValue(BUTTON_TARGET, TOOLBAR);
188: }
189:
190: public void actionPerformed(ActionEvent e) {
191: mixedTable.decreaseRowHeight();
192: mixedTable.repaint();
193: }
194: }
195: }
|