001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations
015: * under the License.
016: *
017: */
018:
019: package org.apache.jmeter.report.gui.action;
020:
021: import java.awt.event.ActionEvent;
022: import java.util.HashSet;
023: import java.util.Set;
024:
025: import javax.swing.JComponent;
026: import javax.swing.JFileChooser;
027:
028: import org.apache.jmeter.exceptions.IllegalUserActionException;
029: import org.apache.jmeter.gui.action.Command;
030: import org.apache.jmeter.gui.JMeterGUIComponent;
031: import org.apache.jmeter.gui.ReportGuiPackage;
032: import org.apache.jmeter.gui.util.ReportFileDialoger;
033: import org.apache.jmeter.save.SaveGraphicsService;
034: import org.apache.jmeter.visualizers.Printable;
035:
036: //import org.apache.jorphan.logging.LoggingManager;
037: //import org.apache.log.Logger;
038:
039: /**
040: * SaveGraphics action is meant to be a generic reusable Action. The class will
041: * use GUIPackage to get the current gui. Once it does, it checks to see if the
042: * element implements Printable interface. If it does, it call getPrintable() to
043: * get the JComponent. By default, it will use SaveGraphicsService to save a PNG
044: * file if no extension is provided. If either .png or .tif is in the filename,
045: * it will call SaveGraphicsService to save in the format.
046: */
047: public class ReportSaveGraphics implements Command {
048: //transient private static Logger log = LoggingManager.getLoggerForClass();
049:
050: public final static String SAVE_GRAPHICS = "save_graphics"; // $NON-NLS-1$
051:
052: private static Set commands = new HashSet();
053: static {
054: commands.add(SAVE_GRAPHICS);
055: }
056:
057: private static final String[] extensions = {
058: SaveGraphicsService.TIFF_EXTENSION,
059: SaveGraphicsService.PNG_EXTENSION };
060:
061: /**
062: * Constructor for the Save object.
063: */
064: public ReportSaveGraphics() {
065: }
066:
067: /**
068: * Gets the ActionNames attribute of the Save object.
069: *
070: * @return the ActionNames value
071: */
072: public Set getActionNames() {
073: return commands;
074: }
075:
076: public void doAction(ActionEvent e)
077: throws IllegalUserActionException {
078: JMeterGUIComponent component = null;
079: JComponent comp = null;
080: if (!commands.contains(e.getActionCommand())) {
081: throw new IllegalUserActionException(
082: "Invalid user command:" + e.getActionCommand());
083: }
084: if (e.getActionCommand().equals(SAVE_GRAPHICS)) {
085: component = ReportGuiPackage.getInstance().getCurrentGui();
086: // get the JComponent from the visualizer
087: if (component instanceof Printable) {
088: comp = ((Printable) component).getPrintableComponent();
089:
090: String filename;
091: JFileChooser chooser = ReportFileDialoger
092: .promptToSaveFile(ReportGuiPackage
093: .getInstance().getTreeListener()
094: .getCurrentNode().getName(), extensions);
095: if (chooser == null) {
096: return;
097: }
098: // Get the string given from the choose and check
099: // the file extension.
100: filename = chooser.getSelectedFile().getAbsolutePath();
101: if (filename != null) {
102: SaveGraphicsService save = new SaveGraphicsService();
103: String ext = filename
104: .substring(filename.length() - 4);
105: String name = filename.substring(0, filename
106: .length() - 4);
107: if (ext.equals(SaveGraphicsService.PNG_EXTENSION)) {
108: save.saveJComponent(name,
109: SaveGraphicsService.PNG, comp);
110: } else if (ext
111: .equals(SaveGraphicsService.TIFF_EXTENSION)) {
112: save.saveJComponent(name,
113: SaveGraphicsService.TIFF, comp);
114: } else {
115: save.saveJComponent(filename,
116: SaveGraphicsService.PNG, comp);
117: }
118: }
119: }
120: }
121: }
122: }
|