001: //$Header$
002: /*
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: */
019:
020: package org.apache.jmeter.report.gui.action;
021:
022: import java.awt.event.ActionEvent;
023: import java.io.FileOutputStream;
024: import java.util.HashSet;
025: import java.util.Iterator;
026: import java.util.LinkedList;
027: import java.util.Set;
028:
029: import javax.swing.JFileChooser;
030:
031: import org.apache.jmeter.exceptions.IllegalUserActionException;
032: import org.apache.jmeter.gui.ReportGuiPackage;
033: import org.apache.jmeter.report.gui.action.ReportActionRouter;
034: import org.apache.jmeter.gui.action.Command;
035: import org.apache.jmeter.gui.util.ReportFileDialoger;
036: import org.apache.jmeter.report.gui.tree.ReportTreeNode;
037: import org.apache.jmeter.save.OldSaveService;
038: import org.apache.jmeter.save.SaveService;
039: import org.apache.jmeter.testelement.TestElement;
040: import org.apache.jorphan.collections.HashTree;
041: import org.apache.jorphan.logging.LoggingManager;
042: import org.apache.jorphan.util.JOrphanUtils;
043: import org.apache.log.Logger;
044:
045: /**
046: * @author Peter Lin
047: * @version $Revision: 533867 $ updated on $Date: 2007-04-30 23:13:40 +0100 (Mon, 30 Apr 2007) $
048: */
049: public class ReportSave implements Command {
050: transient private static Logger log = LoggingManager
051: .getLoggerForClass();
052:
053: public final static String SAVE_ALL_AS = "save_all_as";
054:
055: public final static String SAVE_AS = "save_as";
056:
057: public final static String SAVE = "save";
058:
059: // NOTUSED private String chosenFile;
060:
061: private static Set commands = new HashSet();
062: static {
063: commands.add(SAVE_AS);
064: commands.add(SAVE_ALL_AS);
065: commands.add(SAVE);
066: }
067:
068: /**
069: * Constructor for the Save object.
070: */
071: public ReportSave() {
072: }
073:
074: /**
075: * Gets the ActionNames attribute of the Save object.
076: *
077: * @return the ActionNames value
078: */
079: public Set getActionNames() {
080: return commands;
081: }
082:
083: public void doAction(ActionEvent e)
084: throws IllegalUserActionException {
085: HashTree subTree = null;
086: if (!commands.contains(e.getActionCommand())) {
087: throw new IllegalUserActionException(
088: "Invalid user command:" + e.getActionCommand());
089: }
090: if (e.getActionCommand().equals(SAVE_AS)) {
091: subTree = ReportGuiPackage.getInstance()
092: .getCurrentSubTree();
093: } else {
094: subTree = ReportGuiPackage.getInstance().getTreeModel()
095: .getReportPlan();
096: }
097:
098: String updateFile = ReportGuiPackage.getInstance()
099: .getReportPlanFile();
100: if (!SAVE.equals(e.getActionCommand()) || updateFile == null) {
101: JFileChooser chooser = ReportFileDialoger
102: .promptToSaveFile(ReportGuiPackage.getInstance()
103: .getTreeListener().getCurrentNode()
104: .getName()
105: + ".jmr");
106: if (chooser == null) {
107: return;
108: }
109: updateFile = chooser.getSelectedFile().getAbsolutePath();
110: if (!e.getActionCommand().equals(SAVE_AS)) {
111: ReportGuiPackage.getInstance().setReportPlanFile(
112: updateFile);
113: }
114: }
115: // TODO: doesn't putting this here mark the tree as
116: // saved even though a failure may occur later?
117:
118: ReportActionRouter.getInstance().doActionNow(
119: new ActionEvent(subTree, e.getID(),
120: ReportCheckDirty.SUB_TREE_SAVED));
121: try {
122: convertSubTree(subTree);
123: } catch (Exception err) {
124: }
125: FileOutputStream ostream = null;
126: try {
127: ostream = new FileOutputStream(updateFile);
128: if (SaveService.isSaveTestPlanFormat20()) {
129: OldSaveService.saveSubTree(subTree, ostream);
130: log.info("saveSubTree");
131: } else {
132: SaveService.saveTree(subTree, ostream);
133: log.info("saveTree");
134: }
135: } catch (Throwable ex) {
136: ReportGuiPackage.getInstance().setReportPlanFile(null);
137: log.error("", ex);
138: throw new IllegalUserActionException(
139: "Couldn't save test plan to file: " + updateFile);
140: } finally {
141: JOrphanUtils.closeQuietly(ostream);
142: }
143: }
144:
145: private void convertSubTree(HashTree tree) {
146: Iterator iter = new LinkedList(tree.list()).iterator();
147: while (iter.hasNext()) {
148: ReportTreeNode item = (ReportTreeNode) iter.next();
149: convertSubTree(tree.getTree(item));
150: TestElement testElement = item.getTestElement();
151: tree.replace(item, testElement);
152: }
153: }
154: }
|