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.metadata;
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.Iterator;
025: import java.util.logging.Logger;
026:
027: import javax.swing.AbstractAction;
028: import javax.swing.Action;
029: import javax.swing.JScrollPane;
030: import javax.swing.ListSelectionModel;
031:
032: import de.finix.contelligent.client.base.ComponentFactory;
033: import de.finix.contelligent.client.event.ContelligentEvent;
034: import de.finix.contelligent.client.gui.AbstractComponentEditor;
035: import de.finix.contelligent.client.gui.ContelligentAction;
036: import de.finix.contelligent.client.i18n.Resources;
037:
038: public class MetadataEditor extends AbstractComponentEditor {
039:
040: public final static String DEVELOP = "develop";
041:
042: public final static String EDIT = "edit";
043:
044: public final static String SYSTEM = "system";
045:
046: public final static String META = "meta";
047:
048: public final static String COMPONENT = "component";
049:
050: private static Logger logger = Logger
051: .getLogger(MetadataEditor.class.getName());
052:
053: private MetadataTableModel metadataTableModel;
054:
055: private MetadataTable metadataTable;
056:
057: private String visibleGroup;
058:
059: private AddMetadataAction addMetadataAction = new AddMetadataAction();
060:
061: private DeleteMetadataAction deleteMetadataAction = new DeleteMetadataAction();
062:
063: public void init() {
064: update();
065:
066: metadataTable
067: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
068: setLayout();
069:
070: JScrollPane scrollPane = new JScrollPane(metadataTable);
071: scrollPane.getViewport().setOpaque(false);
072: scrollPane.setOpaque(false);
073:
074: add(scrollPane, BorderLayout.CENTER);
075:
076: metadataTable.addFocusListener(new FocusAdapter() {
077: public void focusGained(FocusEvent e) {
078: getView().setActions(getActions());
079: }
080: });
081: }
082:
083: public void setLayout() {
084: metadataTable.getColumnModel().getColumn(0).setPreferredWidth(
085: 50);
086: metadataTable.getColumnModel().getColumn(1).setPreferredWidth(
087: 300);
088: }
089:
090: public void update() {
091: if (metadataTableModel == null) {
092: metadataTableModel = new MetadataTableModel(getComponent(),
093: visibleGroup);
094: metadataTableModel.setEditable(isEditable());
095: metadataTable = new MetadataTable(metadataTableModel);
096: } else {
097: metadataTableModel.updateData();
098: metadataTableModel.fireTableDataChanged();
099: }
100: }
101:
102: public void setEditable(boolean editable) {
103: super .setEditable(editable);
104: if (metadataTable != null)
105: metadataTable.stopEditing();
106: if (metadataTableModel != null)
107: metadataTableModel.setEditable(isEditable());
108: }
109:
110: public void setVisibleGroup(String visibleGroup) {
111: this .visibleGroup = visibleGroup;
112: }
113:
114: public void commit() {
115: setEditable(false);
116: metadataTableModel.removeEmptyRows();
117: setLayout();
118: Iterator alteredKeys = metadataTableModel.getAlteredKeys()
119: .iterator();
120: while (alteredKeys.hasNext()) {
121: String key = (String) alteredKeys.next();
122: getComponent().setMetadataKey(key,
123: metadataTableModel.getValuesForKey(key));
124: }
125: getComponent().setModified(true);
126: ComponentFactory.getInstance().save(getComponent());
127: }
128:
129: public Action[] getActions() {
130: if (isEditable())
131: return new Action[] { addMetadataAction,
132: deleteMetadataAction };
133: else
134: return new Action[] {};
135: }
136:
137: protected void updateComponent() {
138: }
139:
140: protected void componentChanged(ContelligentEvent event) {
141: update();
142: }
143:
144: protected void childComponentAdded(ContelligentEvent event) {
145: }
146:
147: protected void childComponentRemoved(ContelligentEvent event) {
148: }
149:
150: protected void childComponentChanged(ContelligentEvent event) {
151: }
152:
153: protected void descendentComponentChanged(ContelligentEvent event) {
154: }
155:
156: public class AddMetadataAction extends AbstractAction implements
157: ContelligentAction {
158: public AddMetadataAction() {
159: super ("add_metadata_action", Resources.genericAddIcon);
160: putValue(TYPE, PUSH_ACTION);
161: putValue(ACTION_TYPE, EDIT_ACTION);
162: putValue(MENU_TARGET, NO_MENU);
163: putValue(BUTTON_TARGET, TOOLBAR);
164: }
165:
166: public void actionPerformed(ActionEvent e) {
167: metadataTable.stopEditing();
168: int row = metadataTableModel.addRow();
169: metadataTableModel.fireTableStructureChanged();
170: setLayout();
171: }
172: }
173:
174: public class DeleteMetadataAction extends AbstractAction implements
175: ContelligentAction {
176: public DeleteMetadataAction() {
177: super ("delete_metadata_action", Resources.genericDeleteIcon);
178: putValue(TYPE, PUSH_ACTION);
179: putValue(ACTION_TYPE, EDIT_ACTION);
180: putValue(MENU_TARGET, NO_MENU);
181: putValue(BUTTON_TARGET, TOOLBAR);
182: }
183:
184: public void actionPerformed(ActionEvent e) {
185: int selectedRow = -1;
186: selectedRow = metadataTable.getSelectedRow();
187: if ((selectedRow >= 0)
188: && (selectedRow <= metadataTableModel.getRowCount())) {
189: metadataTable.stopEditing();
190: metadataTableModel.deleteRow(selectedRow);
191: metadataTableModel.fireTableStructureChanged();
192: setLayout();
193: }
194: }
195: }
196: }
|