001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.crm.report.configuration;
015:
016: import org.jetbrains.annotations.NotNull;
017: import org.jetbrains.annotations.Nullable;
018: import org.jfree.base.config.HierarchicalConfiguration;
019: import org.jfree.report.JFreeReportBoot;
020: import org.jfree.report.modules.gui.config.ConfigEditor;
021: import org.jfree.report.modules.gui.config.model.ConfigTreeModel;
022: import org.jfree.report.modules.gui.config.model.ConfigTreeModelException;
023: import org.jfree.report.modules.gui.config.model.ConfigTreeModuleNode;
024: import org.jfree.report.modules.gui.config.model.ConfigTreeRootNode;
025: import org.jfree.report.modules.gui.config.model.ConfigTreeSectionNode;
026: import org.jfree.util.ObjectUtilities;
027:
028: import javax.swing.*;
029: import javax.swing.event.TreeSelectionEvent;
030: import javax.swing.event.TreeSelectionListener;
031: import javax.swing.tree.DefaultTreeCellRenderer;
032: import javax.swing.tree.DefaultTreeSelectionModel;
033: import javax.swing.tree.TreePath;
034: import javax.swing.tree.TreeSelectionModel;
035: import java.awt.*;
036: import java.io.InputStream;
037: import java.util.Enumeration;
038: import java.util.HashMap;
039: import java.util.Set;
040:
041: /**
042: * User: Martin
043: * Date: 17.03.2007
044: * Time: 11:10:53
045: */
046: public class ReportConfigurationEditor extends JPanel {
047: @NotNull
048: private final ConfigEditorPanel detailEditorPane;
049:
050: @NotNull
051: private ConfigTreeModel treeModel;
052:
053: @NotNull
054: private final HierarchicalConfiguration currentReportConfiguration;
055:
056: public ReportConfigurationEditor(@NotNull
057: ReportConfiguration reportConfiguration)
058: throws ConfigTreeModelException {
059: currentReportConfiguration = new HierarchicalConfiguration(
060: JFreeReportBoot.getInstance().getGlobalConfig());
061:
062: HashMap<String, String> configProperties = reportConfiguration
063: .getConfigProperties();
064: Set<String> keys = configProperties.keySet();
065: for (String key : keys) {
066: String value = configProperties.get(key);
067: currentReportConfiguration.setConfigProperty(key, value);
068: }
069:
070: detailEditorPane = new ConfigEditorPanel();
071:
072: final JSplitPane splitPane = new JSplitPane(
073: JSplitPane.HORIZONTAL_SPLIT, createEntryTree(),
074: detailEditorPane);
075: splitPane.setDividerLocation(250);
076:
077: setLayout(new BorderLayout());
078: add(splitPane, BorderLayout.CENTER);
079: }
080:
081: @NotNull
082: private JComponent createEntryTree()
083: throws ConfigTreeModelException {
084: final InputStream in = ObjectUtilities
085: .getResourceRelativeAsStream("config-description.xml",
086: ConfigEditor.class);//NON-NLS
087: if (in == null) {
088: throw new IllegalStateException(
089: "Missing resource 'config-description.xml'");
090: }
091: treeModel = new ConfigTreeModel(in);
092: treeModel.init(currentReportConfiguration);
093:
094: final TreeSelectionModel selectionModel = new DefaultTreeSelectionModel();
095: selectionModel
096: .setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
097:
098: final JTree tree = new JTree(treeModel);
099: tree.setSelectionModel(selectionModel);
100: tree.setCellRenderer(new DefaultTreeCellRenderer() {
101: @NotNull
102: public Component getTreeCellRendererComponent(@NotNull
103: JTree tree, @Nullable
104: Object value, boolean sel, boolean expanded, boolean leaf,
105: int row, boolean hasFocus) {
106: Object v = value;
107: if (value instanceof ConfigTreeRootNode) {
108: v = "<Root>";//NON-NLS
109: } else if (value instanceof ConfigTreeSectionNode) {
110: ConfigTreeSectionNode node = (ConfigTreeSectionNode) value;
111: v = node.getName();
112: } else if (value instanceof ConfigTreeModuleNode) {
113: ConfigTreeModuleNode node = (ConfigTreeModuleNode) value;
114: v = node.getModule().getName();
115: }
116: return super .getTreeCellRendererComponent(tree, v, sel,
117: expanded, leaf, row, hasFocus);
118: }
119: });
120:
121: tree.setRootVisible(false);
122: tree.setShowsRootHandles(true);
123: tree.addTreeSelectionListener(new ModuleTreeSelectionHandler());
124:
125: for (int i = 0; i < 100; i++) {
126: tree.expandRow(i);
127: }
128:
129: tree.setSelectionRow(1);
130:
131: return new JScrollPane(tree,
132: JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
133: JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
134: }
135:
136: @NotNull
137: public ReportConfiguration getReportConfiguration() {
138: detailEditorPane.store();
139:
140: HashMap<String, String> configProperties = new HashMap<String, String>();
141: Enumeration<?> enumeration = currentReportConfiguration
142: .getConfigProperties();
143: while (enumeration.hasMoreElements()) {
144: String key = (String) enumeration.nextElement();
145: configProperties.put(key, currentReportConfiguration
146: .getConfigProperty(key));
147: }
148: return new ReportConfiguration(configProperties);
149: }
150:
151: private class ModuleTreeSelectionHandler implements
152: TreeSelectionListener {
153:
154: private ModuleTreeSelectionHandler() {
155: }
156:
157: public void valueChanged(@NotNull
158: final TreeSelectionEvent e) {
159: final TreePath path = e.getPath();
160: final Object lastPathElement = path.getLastPathComponent();
161: if (lastPathElement instanceof ConfigTreeModuleNode) {
162: final ConfigTreeModuleNode node = (ConfigTreeModuleNode) lastPathElement;
163: detailEditorPane.store();
164: detailEditorPane.editModule(node.getModule(), node
165: .getConfiguration(), node.getAssignedKeys());
166: } else {
167: detailEditorPane.store();
168: detailEditorPane.clear();
169: }
170: }
171: }
172:
173: }
|