[2033] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
| 2 | //
|
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 4 | // you may not use this file except in compliance with the License.
|
---|
| 5 | // You may obtain a copy of the License at
|
---|
| 6 | //
|
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 8 | //
|
---|
| 9 | // Unless required by applicable law or agreed to in writing, software
|
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 12 | // See the License for the specific language governing permissions and
|
---|
| 13 | // limitations under the License.
|
---|
| 14 |
|
---|
| 15 | package de.ugoe.cs.autoquest.ui.swt.commands;
|
---|
| 16 |
|
---|
| 17 | import java.awt.Frame;
|
---|
| 18 | import java.util.LinkedList;
|
---|
| 19 | import java.util.List;
|
---|
| 20 |
|
---|
| 21 | import org.eclipse.swt.SWT;
|
---|
| 22 | import org.eclipse.swt.awt.SWT_AWT;
|
---|
| 23 | import org.eclipse.swt.events.SelectionAdapter;
|
---|
| 24 | import org.eclipse.swt.events.SelectionEvent;
|
---|
| 25 | import org.eclipse.swt.layout.GridData;
|
---|
| 26 | import org.eclipse.swt.layout.GridLayout;
|
---|
| 27 | import org.eclipse.swt.widgets.Button;
|
---|
| 28 | import org.eclipse.swt.widgets.Composite;
|
---|
| 29 | import org.eclipse.swt.widgets.Display;
|
---|
| 30 | import org.eclipse.swt.widgets.FileDialog;
|
---|
| 31 | import org.eclipse.swt.widgets.Shell;
|
---|
| 32 | import org.jfree.chart.ChartPanel;
|
---|
| 33 | import org.jfree.chart.JFreeChart;
|
---|
| 34 | import org.jfree.ui.RectangleEdge;
|
---|
| 35 |
|
---|
| 36 | import de.ugoe.cs.autoquest.CommandHelpers;
|
---|
| 37 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskModel;
|
---|
| 38 | import de.ugoe.cs.autoquest.ui.swt.TaskTreePlotUtils;
|
---|
| 39 | import de.ugoe.cs.util.console.Command;
|
---|
| 40 | import de.ugoe.cs.util.console.GlobalDataContainer;
|
---|
| 41 |
|
---|
| 42 | /**
|
---|
| 43 | * <p>
|
---|
| 44 | * Command to show sequences.
|
---|
| 45 | * </p>
|
---|
| 46 | *
|
---|
| 47 | * @author Jeffrey Hall, Steffen Herbold
|
---|
| 48 | */
|
---|
| 49 | public class CMDplotTaskTreeCoverage implements Command {
|
---|
| 50 |
|
---|
| 51 | /*
|
---|
| 52 | * (non-Javadoc)
|
---|
| 53 | *
|
---|
| 54 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
| 55 | */
|
---|
| 56 | @Override
|
---|
| 57 | public String help() {
|
---|
| 58 | return "plotTaskTreeCoverage <taskTreeNames>";
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /*
|
---|
| 62 | * (non-Javadoc)
|
---|
| 63 | *
|
---|
| 64 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
| 65 | */
|
---|
| 66 | @Override
|
---|
| 67 | public void run(List<Object> parameters) {
|
---|
| 68 | List<Integer> taskTreeGroups = new LinkedList<>();
|
---|
| 69 | int group = 0;
|
---|
| 70 |
|
---|
| 71 | List<String> taskTreeNames = new LinkedList<>();
|
---|
| 72 | try {
|
---|
| 73 | for (Object param : parameters) {
|
---|
| 74 | if ("#".equals(param)) {
|
---|
| 75 | group++;
|
---|
| 76 | }
|
---|
| 77 | else {
|
---|
| 78 | taskTreeNames.add((String) param);
|
---|
| 79 | taskTreeGroups.add(group);
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | catch (Exception e) {
|
---|
| 84 | throw new IllegalArgumentException();
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | List<ITaskModel> taskModels = new LinkedList<>();
|
---|
| 88 |
|
---|
| 89 | for (String taskTreeName : taskTreeNames) {
|
---|
| 90 | Object dataObject = GlobalDataContainer.getInstance().getData(taskTreeName);
|
---|
| 91 | if (dataObject == null) {
|
---|
| 92 | CommandHelpers.objectNotFoundMessage(taskTreeName);
|
---|
| 93 | return;
|
---|
| 94 | }
|
---|
| 95 | if (!(dataObject instanceof ITaskModel)) {
|
---|
| 96 | CommandHelpers.objectNotType(taskTreeName, "ITaskModel");
|
---|
| 97 | return;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | taskModels.add((ITaskModel) dataObject);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | // create the chart
|
---|
| 104 | final JFreeChart chart = TaskTreePlotUtils.createPlot
|
---|
| 105 | (TaskTreePlotUtils.CUMULATIVE_TASK_COVERAGE_PLOT, taskModels, taskTreeNames,
|
---|
| 106 | taskTreeGroups);
|
---|
| 107 |
|
---|
| 108 | final int width;
|
---|
| 109 | if (chart.getLegend() != null) {
|
---|
| 110 | width = 700;
|
---|
| 111 | chart.getLegend().setPosition(RectangleEdge.RIGHT);
|
---|
| 112 | }
|
---|
| 113 | else {
|
---|
| 114 | width = 500;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | final Shell shell = new Shell(SWT.SHELL_TRIM | SWT.BORDER | SWT.APPLICATION_MODAL);
|
---|
| 118 | shell.layout();
|
---|
| 119 | shell.setSize(width, 500);
|
---|
| 120 | shell.setLayout(new GridLayout(2, false));
|
---|
| 121 |
|
---|
| 122 | Composite parent = new Composite(shell, SWT.EMBEDDED);
|
---|
| 123 | parent.setLayout(new GridLayout(1, false));
|
---|
| 124 | parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
|
---|
| 125 |
|
---|
| 126 | Frame chartFrame = SWT_AWT.new_Frame(parent);
|
---|
| 127 | chartFrame.setLayout(new java.awt.GridLayout());
|
---|
| 128 |
|
---|
| 129 | chartFrame.add(new ChartPanel(chart));
|
---|
| 130 |
|
---|
| 131 | Button btnExport = new Button(shell, SWT.NONE);
|
---|
| 132 | btnExport.addSelectionListener(new SelectionAdapter() {
|
---|
| 133 | @Override
|
---|
| 134 | public void widgetSelected(SelectionEvent e) {
|
---|
| 135 | try {
|
---|
| 136 | FileDialog dialog = new FileDialog(shell, SWT.SAVE);
|
---|
| 137 | dialog.setFilterExtensions(new String [] {"*.pdf"});
|
---|
| 138 |
|
---|
| 139 | TaskTreePlotUtils.saveChartToPDF(chart, dialog.open(), width, 500);
|
---|
| 140 | }
|
---|
| 141 | catch (Exception e1) {
|
---|
| 142 | // TODO Auto-generated catch block
|
---|
| 143 | e1.printStackTrace();
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 | });
|
---|
| 147 | btnExport.setText("export to PDF");
|
---|
| 148 |
|
---|
| 149 | Button btnClose = new Button(shell, SWT.NONE);
|
---|
| 150 | btnClose.addSelectionListener(new SelectionAdapter() {
|
---|
| 151 | @Override
|
---|
| 152 | public void widgetSelected(SelectionEvent e) {
|
---|
| 153 | shell.dispose();
|
---|
| 154 | }
|
---|
| 155 | });
|
---|
| 156 | btnClose.setText("close");
|
---|
| 157 |
|
---|
| 158 | shell.open();
|
---|
| 159 | shell.layout();
|
---|
| 160 | Display display = shell.getDisplay();
|
---|
| 161 | while (!shell.isDisposed()) {
|
---|
| 162 | if (!display.readAndDispatch()) {
|
---|
| 163 | display.sleep();
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | shell.dispose();
|
---|
| 168 | }
|
---|
| 169 | }
|
---|