001: /*
002: * <copyright>
003: *
004: * Copyright 2000-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.tools.csmart.ui.analyzer;
028:
029: import org.cougaar.tools.csmart.experiment.DBExperiment;
030: import org.cougaar.tools.csmart.experiment.Experiment;
031: import org.cougaar.tools.csmart.recipe.MetricComponent;
032: import org.cougaar.tools.csmart.recipe.RecipeComponent;
033: import org.cougaar.tools.csmart.society.SocietyComponent;
034: import org.cougaar.tools.csmart.ui.Browser;
035: import org.cougaar.tools.csmart.ui.util.NamedFrame;
036: import org.cougaar.tools.csmart.ui.viewer.CSMART;
037: import org.cougaar.util.Parameters;
038: import org.cougaar.util.log.Logger;
039:
040: import javax.swing.*;
041: import java.awt.*;
042: import java.awt.event.ActionEvent;
043: import java.awt.event.ActionListener;
044: import java.io.File;
045: import java.io.IOException;
046: import java.io.ObjectInputStream;
047: import java.net.URL;
048: import java.util.Hashtable;
049:
050: public class Analyzer extends JFrame implements ActionListener {
051: // system property; location of Excel
052: private static final String EXCEL = "excel";
053: // menu items
054: private static final String FILE_MENU = "File";
055: private static final String OPEN_MENU_ITEM = "Open";
056: private static final String WINDOW_MENU = "Window";
057: private static final String EXIT_MENU_ITEM = "Exit";
058: private static final String HELP_MENU = "Help";
059:
060: protected static final String HELP_DOC = "help.html";
061: protected static final String ABOUT_CSMART_ITEM = "About CSMART";
062: protected static final String ABOUT_DOC = "/org/cougaar/tools/csmart/ui/help/about-csmart.html";
063: protected static final String HELP_MENU_ITEM = "Help";
064:
065: private String[] helpMenuItems = { HELP_MENU_ITEM,
066: ABOUT_CSMART_ITEM };
067:
068: // tool buttons
069: private static final String EXCEL_LABEL = "Excel";
070: private CSMART csmart; // top level viewer, gives access to save method, etc.
071: private static JFrame myFrame;
072: private JMenu windowMenu;
073: private Hashtable titleToFrame = new Hashtable();
074: private Process process;
075: private Experiment experiment;
076:
077: private transient Logger log;
078:
079: /**
080: * Display a csv file in excel.
081: * @param csmart Handle to CSMART
082: */
083: public Analyzer(CSMART csmart) {
084: this (csmart, null);
085: }
086:
087: /**
088: * Display a csv file in excel.
089: * @param csmart Handle to CSMART
090: * @param experiment Handle to the Experiment
091: */
092: public Analyzer(CSMART csmart, Experiment experiment) {
093: createLogger();
094: this .csmart = csmart;
095: this .experiment = experiment;
096: myFrame = this ;
097: JMenu fileMenu = new JMenu(FILE_MENU);
098: JMenuItem openMenuItem = new JMenuItem(OPEN_MENU_ITEM);
099: openMenuItem.addActionListener(this );
100: fileMenu.add(openMenuItem);
101: JMenuItem exitMenuItem = new JMenuItem(EXIT_MENU_ITEM);
102: exitMenuItem.addActionListener(this );
103: fileMenu.add(exitMenuItem);
104: JMenuBar menuBar = new JMenuBar();
105:
106: JMenu helpMenu = new JMenu(HELP_MENU);
107: for (int i = 0; i < helpMenuItems.length; i++) {
108: JMenuItem mItem = new JMenuItem(helpMenuItems[i]);
109: mItem.addActionListener(this );
110: helpMenu.add(mItem);
111: }
112:
113: getRootPane().setJMenuBar(menuBar);
114: menuBar.add(fileMenu);
115: menuBar.add(helpMenu);
116:
117: JToolBar toolBar = new JToolBar();
118: toolBar.setLayout(new GridLayout(1, 1, 2, 2));
119:
120: JButton button = null;
121: URL iconURL = getClass().getResource("Excel.gif");
122: if (iconURL == null)
123: button = new JButton(EXCEL_LABEL);
124: else {
125: ImageIcon icon = new ImageIcon(iconURL);
126: button = new JButton(EXCEL_LABEL, icon);
127: }
128: button.setHorizontalTextPosition(JButton.CENTER);
129: button.setVerticalTextPosition(JButton.BOTTOM);
130: button.addActionListener(this );
131: toolBar.add(button);
132: getContentPane().add("North", toolBar);
133:
134: windowMenu = new JMenu(WINDOW_MENU);
135: pack();
136: setSize(400, 120);
137: setVisible(true);
138: }
139:
140: private void createLogger() {
141: log = CSMART.createLogger(this .getClass().getName());
142: }
143:
144: /**
145: * Re-initialize the Experiment
146: *
147: * @param experiment new Experiment
148: */
149: public void reinit(Experiment experiment) {
150: this .experiment = experiment;
151: }
152:
153: /**
154: * Display file chooser on metrics directory specified by experiment
155: * and run excel on file chosen by user, or if no experiment,
156: * then display file chooser, then use default results directory.
157: */
158: private void showExcelFile() {
159: File resultsDir = null;
160: if (experiment == null)
161: resultsDir = csmart.getResultDir();
162: else
163: resultsDir = experiment.getResultDirectory();
164: if (resultsDir == null) {
165: JOptionPane
166: .showMessageDialog(
167: this ,
168: "No Results (Metrics) Directory for Experiment",
169: "No Results Directory",
170: JOptionPane.WARNING_MESSAGE);
171: return;
172: }
173: JFileChooser fileChooser = new JFileChooser(resultsDir);
174: fileChooser.setDialogTitle("Select results file to analyze");
175: if (experiment != null) {
176: resultsDir = new File(resultsDir, experiment
177: .getExperimentName());
178: fileChooser.setFileFilter(new MyFileFilter(experiment));
179: }
180: int result = fileChooser.showOpenDialog(this );
181: if (result != JFileChooser.APPROVE_OPTION)
182: return;
183: if (fileChooser.getSelectedFile().isDirectory())
184: return; // user specified a directory, not a file
185: if (!fileChooser.getSelectedFile().canRead())
186: return; // Cant read that file
187:
188: String filePathName = fileChooser.getSelectedFile().getPath();
189:
190: String excel = Parameters
191: .findParameter("org.cougaar.tools.csmart.excelpath");
192:
193: if (excel == null || excel.equals("")) {
194: if (log.isErrorEnabled()) {
195: log.error("Excel location not specified");
196: }
197: return;
198: }
199:
200: // FIXME: Bug 1886: test that the file pointed to by the excel
201: // parameter exists here. Perhaps prompt for one?
202:
203: String[] cmds = { excel, "" };
204: if (log.isInfoEnabled()) {
205: log.info("Launching excel from: " + cmds[0]);
206: }
207: cmds[1] = filePathName;
208: try {
209: process = Runtime.getRuntime().exec(cmds);
210: } catch (Exception e) {
211: if (log.isErrorEnabled()) {
212: log.error("Analyzer: exception: ", e);
213: }
214: }
215: }
216:
217: public void actionPerformed(ActionEvent e) {
218: String s = ((AbstractButton) e.getSource()).getActionCommand();
219: if (s.equals(OPEN_MENU_ITEM)) {
220: showExcelFile();
221: } else if (s.equals(EXCEL_LABEL)) {
222: showExcelFile();
223: } else if (s.equals(HELP_MENU_ITEM)) {
224: URL help = (URL) getClass().getResource(HELP_DOC);
225: if (help != null)
226: Browser.setPage(help);
227: } else if (s.equals(ABOUT_CSMART_ITEM)) {
228: URL about = (URL) getClass().getResource(ABOUT_DOC);
229: if (about != null)
230: Browser.setPage(about);
231: } else if (s.equals(EXIT_MENU_ITEM)) {
232: // this will destroy the process, but is it the action we want?
233: // the Excel process does not query the user before exiting
234: // process.destroy();
235: int n = windowMenu.getItemCount();
236: for (int i = 0; i < n; i++) {
237: JMenuItem menuItem = windowMenu.getItem(i);
238: JFrame f = (JFrame) titleToFrame
239: .get(menuItem.getText());
240: f.dispose();
241: }
242: NamedFrame.getNamedFrame().removeFrame(this );
243: dispose();
244: }
245: }
246:
247: public static void main(String[] args) {
248: new Analyzer(null, null);
249: }
250:
251: class MyFileFilter extends javax.swing.filechooser.FileFilter {
252: Experiment experiment;
253: static final String description = "results files";
254:
255: MyFileFilter(Experiment experiment) {
256: this .experiment = experiment;
257: }
258:
259: /**
260: * Accept any file accepted by a society in this experiment.
261: */
262: public boolean accept(File f) {
263: if (f.isDirectory())
264: return true; // allow user to go up/down through directory tree
265: SocietyComponent societyComponent = experiment
266: .getSocietyComponent();
267: if (societyComponent != null) {
268: java.io.FileFilter fileFilter = societyComponent
269: .getResultFileFilter();
270: if (fileFilter != null && fileFilter.accept(f))
271: return true;
272: }
273: int m = experiment.getRecipeComponentCount();
274: for (int i = 0; i < m; i++) {
275: RecipeComponent recipeComponent = experiment
276: .getRecipeComponent(i);
277: if (recipeComponent instanceof MetricComponent) {
278: MetricComponent metricComponent = (MetricComponent) recipeComponent;
279: java.io.FileFilter fileFilter = metricComponent
280: .getResultFileFilter();
281: if (fileFilter == null)
282: continue;
283: if (fileFilter.accept(f))
284: return true;
285: }
286: }
287: return false;
288: }
289:
290: public String getDescription() {
291: return description;
292: }
293: }
294:
295: private void readObject(ObjectInputStream ois) throws IOException,
296: ClassNotFoundException {
297: ois.defaultReadObject();
298: createLogger();
299: }
300:
301: }
|