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.gui.action;
020:
021: import java.awt.event.ActionEvent;
022: import java.io.File;
023: import java.util.HashSet;
024: import java.util.Set;
025:
026: import javax.swing.JComponent;
027: import javax.swing.JFileChooser;
028: import javax.swing.JOptionPane;
029:
030: import org.apache.jmeter.exceptions.IllegalUserActionException;
031: import org.apache.jmeter.gui.JMeterGUIComponent;
032: import org.apache.jmeter.gui.GuiPackage;
033: import org.apache.jmeter.gui.util.FileDialoger;
034: import org.apache.jmeter.save.SaveGraphicsService;
035: import org.apache.jmeter.util.JMeterUtils;
036: import org.apache.jmeter.visualizers.Printable;
037:
038: /**
039: * SaveGraphics action is meant to be a generic reusable Action. The class will
040: * use GUIPackage to get the current gui. Once it does, it checks to see if the
041: * element implements Printable interface. If it does, it call getPrintable() to
042: * get the JComponent. By default, it will use SaveGraphicsService to save a PNG
043: * file if no extension is provided. If either .png or .tif is in the filename,
044: * it will call SaveGraphicsService to save in the format.
045: */
046: public class SaveGraphics implements Command {
047:
048: private static Set commands = new HashSet();
049: static {
050: commands.add(ActionNames.SAVE_GRAPHICS);
051: commands.add(ActionNames.SAVE_GRAPHICS_ALL);
052: }
053:
054: private static final String[] extensions = {
055: SaveGraphicsService.TIFF_EXTENSION,
056: SaveGraphicsService.PNG_EXTENSION };
057:
058: /**
059: * Constructor for the Save object.
060: */
061: public SaveGraphics() {
062: }
063:
064: /**
065: * Gets the ActionNames attribute of the Save object.
066: *
067: * @return the ActionNames value
068: */
069: public Set getActionNames() {
070: return commands;
071: }
072:
073: public void doAction(ActionEvent e)
074: throws IllegalUserActionException {
075: JMeterGUIComponent component = null;
076: JComponent comp = null;
077: if (!commands.contains(e.getActionCommand())) {
078: throw new IllegalUserActionException(
079: "Invalid user command:" + e.getActionCommand());
080: }
081: if (e.getActionCommand().equals(ActionNames.SAVE_GRAPHICS)) {
082: component = GuiPackage.getInstance().getCurrentGui();
083: // get the JComponent from the visualizer
084: if (component instanceof Printable) {
085: comp = ((Printable) component).getPrintableComponent();
086: saveImage(comp);
087: }
088: }
089: if (e.getActionCommand().equals(ActionNames.SAVE_GRAPHICS_ALL)) {
090: component = GuiPackage.getInstance().getCurrentGui();
091: comp = ((JComponent) component).getRootPane();
092: saveImage(comp);
093: }
094: }
095:
096: private void saveImage(JComponent comp) {
097:
098: String filename;
099: JFileChooser chooser = FileDialoger.promptToSaveFile(GuiPackage
100: .getInstance().getTreeListener().getCurrentNode()
101: .getName(), extensions);
102: if (chooser == null) {
103: return;
104: }
105: // Get the string given from the choose and check
106: // the file extension.
107: filename = chooser.getSelectedFile().getAbsolutePath();
108: if (filename != null) {
109: File f = new File(filename);
110: if (f.exists()) {
111: int response = JOptionPane
112: .showConfirmDialog(
113: GuiPackage.getInstance().getMainFrame(),
114: JMeterUtils
115: .getResString("save_overwrite_existing_file"), // $NON-NLS-1$
116: JMeterUtils.getResString("save?"), // $NON-NLS-1$
117: JOptionPane.YES_NO_OPTION,
118: JOptionPane.QUESTION_MESSAGE);
119: if (response == JOptionPane.CLOSED_OPTION
120: || response == JOptionPane.NO_OPTION) {
121: return; // Do not save, user does not want to overwrite
122: }
123: }
124: SaveGraphicsService save = new SaveGraphicsService();
125: String ext = filename.substring(filename.length() - 4);
126: String name = filename.substring(0, filename.length() - 4);
127: if (ext.equals(SaveGraphicsService.PNG_EXTENSION)) {
128: save
129: .saveJComponent(name, SaveGraphicsService.PNG,
130: comp);
131: } else if (ext.equals(SaveGraphicsService.TIFF_EXTENSION)) {
132: save.saveJComponent(name, SaveGraphicsService.TIFF,
133: comp);
134: } else {
135: save.saveJComponent(filename, SaveGraphicsService.PNG,
136: comp);
137: }
138: }
139:
140: }
141: }
|