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.configuration;
019:
020: import java.awt.BorderLayout;
021: import java.awt.event.ActionEvent;
022: import java.io.IOException;
023: import java.io.StringReader;
024: import java.io.StringWriter;
025: import java.util.logging.Level;
026: import java.util.logging.Logger;
027:
028: import javax.swing.AbstractAction;
029: import javax.swing.AbstractButton;
030: import javax.swing.Box;
031: import javax.swing.JPanel;
032: import javax.swing.JScrollPane;
033: import javax.swing.JTable;
034: import javax.swing.JTextField;
035: import javax.swing.JToolBar;
036: import javax.swing.ListSelectionModel;
037: import javax.swing.border.TitledBorder;
038: import javax.swing.table.DefaultTableModel;
039: import javax.xml.parsers.ParserConfigurationException;
040:
041: import org.xml.sax.InputSource;
042: import org.xml.sax.SAXException;
043: import org.xml.sax.helpers.AttributesImpl;
044:
045: import de.finix.contelligent.client.base.ComponentFactory;
046: import de.finix.contelligent.client.base.resource.ContelligentTextResource;
047: import de.finix.contelligent.client.event.ContelligentEvent;
048: import de.finix.contelligent.client.event.ResourceModifiedEvent;
049: import de.finix.contelligent.client.gui.AbstractComponentResourceEditor;
050: import de.finix.contelligent.client.gui.ContelligentAction;
051: import de.finix.contelligent.client.i18n.Resources;
052: import de.finix.contelligent.client.util.ButtonComposer;
053: import de.finix.contelligent.client.util.TableLayout;
054: import de.zeigermann.xml.XMLWriter;
055: import de.zeigermann.xml.simpleImporter.DefaultSimpleImportHandler;
056: import de.zeigermann.xml.simpleImporter.SimpleImporter;
057: import de.zeigermann.xml.simpleImporter.SimplePath;
058:
059: public class HotlinkConfigurationEditor extends
060: AbstractComponentResourceEditor {
061:
062: private static Logger logger = Logger
063: .getLogger(HotlinkConfigurationEditor.class.getName());
064:
065: private String hotlinkName;
066:
067: private JTextField hotlinkNameField, hotlinkURLField,
068: parameterName;
069:
070: private JTable table;
071:
072: private DefaultTableModel tableModel;
073:
074: private DeleteParameterAction deleteParameterAction = new DeleteParameterAction();
075:
076: private AddParameterAction addParameterAction = new AddParameterAction();
077:
078: public void init() {
079: setResourceComponent(this );
080: JPanel messagePanel = new JPanel(new TableLayout(
081: new double[][] {
082: { TableLayout.FILL },
083: { TableLayout.PREFERRED, 5,
084: TableLayout.MINIMUM, 3,
085: TableLayout.FILL } }));
086: messagePanel.setOpaque(false);
087: JPanel hotlinkNamePanel = new JPanel(new BorderLayout());
088: hotlinkNamePanel.setOpaque(false);
089: hotlinkNamePanel.setBorder(new TitledBorder(Resources
090: .getLocalString("hotlink_name")));
091: hotlinkNameField = new JTextField();
092: hotlinkNamePanel.add(hotlinkNameField, BorderLayout.CENTER);
093: messagePanel.add(hotlinkNamePanel, "0,0");
094: JPanel hotlinkURLPanel = new JPanel(new BorderLayout());
095: hotlinkURLPanel.setOpaque(false);
096: hotlinkURLPanel.setBorder(new TitledBorder(Resources
097: .getLocalString("hotlink_url")));
098: hotlinkURLField = new JTextField();
099: hotlinkURLPanel.add(hotlinkURLField, BorderLayout.CENTER);
100: messagePanel.add(hotlinkURLPanel, "0,2");
101: JPanel parameterPanel = new JPanel(new BorderLayout());
102: parameterPanel.setOpaque(false);
103: parameterPanel.setBorder(new TitledBorder(Resources
104: .getLocalString("hotlink_parameter")));
105: JToolBar parameterModificationPanel = new JToolBar();
106: parameterModificationPanel.setRollover(true);
107: parameterModificationPanel.setFloatable(false);
108: AbstractButton addParameter = ButtonComposer.createButton(
109: addParameterAction, false, null, true, true);
110: AbstractButton deleteParameter = ButtonComposer.createButton(
111: deleteParameterAction, false, null, true, true);
112: parameterName = new JTextField();
113: parameterModificationPanel.add(parameterName);
114: parameterModificationPanel.add(Box.createHorizontalGlue());
115: parameterModificationPanel.add(addParameter);
116: parameterModificationPanel.add(deleteParameter);
117: parameterModificationPanel.add(Box.createVerticalStrut(28));
118: parameterPanel.add(parameterModificationPanel,
119: BorderLayout.NORTH);
120: tableModel = new ParameterTableModel();
121: tableModel.setDataVector(new Object[0][0],
122: new String[] { Resources
123: .getLocalString("hotlink_parameter_name") });
124: table = new JTable(tableModel);
125: table.setOpaque(false);
126: table.setRowHeight(25);
127: table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
128: JScrollPane scroller = new JScrollPane(table);
129: scroller.getViewport().setOpaque(false);
130: scroller.setOpaque(false);
131: parameterPanel.add(scroller, BorderLayout.CENTER);
132: messagePanel.add(parameterPanel, "0,4");
133: add(messagePanel, BorderLayout.CENTER);
134: update();
135: }
136:
137: public void setEditable(boolean editable) {
138: super .setEditable(editable);
139: if (hotlinkNameField != null)
140: hotlinkNameField.setEditable(editable);
141: if (hotlinkURLField != null)
142: hotlinkURLField.setEditable(editable);
143: if (table != null)
144: table.setEnabled(editable);
145: if (parameterName != null)
146: parameterName.setEnabled(editable);
147: if (addParameterAction != null)
148: addParameterAction.setEnabled(editable);
149: if (deleteParameterAction != null)
150: deleteParameterAction.setEnabled(editable);
151: }
152:
153: protected void displayResource() {
154: // Parse resource and update editor
155: tableModel.setDataVector(new Object[0][0],
156: new String[] { Resources
157: .getLocalString("hotlink_parameter_name") });
158: HotlinkHandler handler = new HotlinkHandler();
159: ContelligentTextResource resource = (ContelligentTextResource) getGUI()
160: .getResource();
161: if (resource != null) {
162: SimpleImporter importer = new SimpleImporter();
163: importer.addSimpleImportHandler(handler);
164: try {
165: importer.parse(new InputSource(new StringReader(
166: resource.getText())));
167: } catch (ParserConfigurationException e) {
168: } catch (SAXException e) {
169: } catch (IOException e) {
170: }
171: }
172: }
173:
174: protected void updateResource() {
175: ContelligentTextResource resource = (ContelligentTextResource) getGUI()
176: .getResource();
177: if (resource == null || getGUI().isResourceInherited()) {
178: resource = new ContelligentTextResource(getGUI()
179: .getResourceCategoryMap(), getPreviewAsXML());
180: resource.setModified(true);
181: getGUI().setResource(resource);
182: } else {
183: resource.setText(getPreviewAsXML());
184: }
185: if (resource.isModified()) {
186: getComponent().setResourceModified(true);
187: }
188: ComponentFactory.getInstance().fireResourceModifyEvent(
189: new ResourceModifiedEvent(this , getComponent()
190: .getPath(), getGUI().getResourceIdentifier(),
191: resource, getGUI().getResourceMode()));
192: }
193:
194: private String getPreviewAsXML() {
195: StringWriter writer = new StringWriter();
196: XMLWriter xmlWriter = new XMLWriter(writer);
197: try {
198: xmlWriter.writeStartTag(XMLWriter.createStartTag("hotlink",
199: new String[][] {
200: { "name", hotlinkNameField.getText() },
201: { "url", hotlinkURLField.getText() } }));
202: for (int i = 0; i < tableModel.getRowCount(); i++) {
203: String parameter = (String) tableModel.getValueAt(i, 0);
204: xmlWriter.writeEmptyElement(XMLWriter.createEmptyTag(
205: "parameter", new String[][] { { "name",
206: parameter } }));
207: }
208: xmlWriter.writeEndTag(XMLWriter.createEndTag("hotlink"));
209: return writer.toString();
210: } catch (IOException e) {
211: logger.log(Level.SEVERE,
212: "Error while creating XML from preview-editor!", e);
213: }
214: return null;
215: }
216:
217: protected void updateComponent() {
218: updateResource();
219: ContelligentTextResource resource = (ContelligentTextResource) getGUI()
220: .getResource();
221: if (resource != null && !getGUI().isResourceInherited()) {
222: resource.setText(getPreviewAsXML());
223: if (resource.isModified()) {
224: getComponent().setResourceModified(true);
225: }
226: }
227: }
228:
229: protected void componentChanged(ContelligentEvent event) {
230: update();
231: }
232:
233: protected void childComponentAdded(ContelligentEvent event) {
234: }
235:
236: protected void childComponentRemoved(ContelligentEvent event) {
237: }
238:
239: protected void childComponentChanged(ContelligentEvent event) {
240: }
241:
242: protected void descendentComponentChanged(ContelligentEvent event) {
243: }
244:
245: public final class HotlinkHandler extends
246: DefaultSimpleImportHandler {
247: public void startElement(SimplePath path, String name,
248: AttributesImpl attrs, String leadingCData) {
249: try {
250: if (path.matches("hotlink")) {
251: hotlinkNameField.setText(attrs.getValue("name"));
252: hotlinkURLField.setText(attrs.getValue("url"));
253: } else if (path.matches("hotlink/parameter")) {
254: String parameterName = attrs.getValue("name");
255: tableModel.addRow(new String[] { parameterName });
256: tableModel.fireTableDataChanged();
257: }
258: } catch (Throwable t) {
259: logger.log(Level.SEVERE,
260: "Caught exception while preview import", t);
261: }
262: }
263: }
264:
265: private final class AddParameterAction extends AbstractAction
266: implements ContelligentAction {
267: public AddParameterAction() {
268: super ("add", Resources.genericAddIcon);
269: putValue(ROLLOVER_ICON, Resources.genericAddIconRollOver);
270: putValue(TYPE, PUSH_ACTION);
271: putValue(ACTION_TYPE, EDIT_ACTION);
272: putValue(MENU_TARGET, MENU);
273: putValue(BUTTON_TARGET, TOOLBAR);
274: }
275:
276: public void actionPerformed(ActionEvent e) {
277: if (parameterName.getText().length() > 0) {
278: tableModel.addRow(new String[] { parameterName
279: .getText() });
280: }
281: }
282: }
283:
284: private final class DeleteParameterAction extends AbstractAction
285: implements ContelligentAction {
286: public DeleteParameterAction() {
287: super ("delete", Resources.genericDeleteIcon);
288: putValue(ROLLOVER_ICON, Resources.genericDeleteIconRollOver);
289: putValue(TYPE, PUSH_ACTION);
290: putValue(ACTION_TYPE, EDIT_ACTION);
291: putValue(MENU_TARGET, MENU);
292: putValue(BUTTON_TARGET, TOOLBAR);
293: }
294:
295: public void actionPerformed(ActionEvent e) {
296: if (table.getSelectedRow() >= 0)
297: tableModel.removeRow(table.getSelectedRow());
298: }
299: }
300:
301: private final class ParameterTableModel extends DefaultTableModel {
302: public boolean isCellEditable(int row, int column) {
303: return false;
304: }
305: }
306: }
|