/** * --Copyright notice-- * * Copyright (c) School of Geography, University of Leeds. * http://www.geog.leeds.ac.uk/ * This software is licensed under 'The Artistic License' which can be found at * the Open Source Initiative website at... * http://www.opensource.org/licenses/artistic-license.php * Please note that the optional Clause 8 does not apply to this code. * * The Standard Version source code, and associated documentation can be found at... * [online] http://mass.leeds.ac.uk/ * * * --End of Copyright notice-- * */ import java.util.*; import java.awt.*; import java.awt.event.*; import java.io.*; /** * Main class for an generic data processing tool.
* The tool has an Storage, containing raster data.
* @version 1.0 * @author Andy Evans */ public class Analyst extends Frame implements ActionListener { /** Reader/writer. */ private IO io = new IO(); /** For drawing. */ private Canvas c = new Canvas(); /** For enabling. */ MenuItem runMenuItem = new MenuItem("Run"); /** For enabling. */ MenuItem saveMenuItem = new MenuItem("Save Results..."); /** Environment to store raster data. */ private Storage store = new Storage(); /** Environment to store raster data. */ private Storage resultsStore = new Storage(); /** * Analyst constructor. */ public Analyst () { // Note how simple this is. We keep the complexity elsewhere // so we can see the program flow here. buildGui(); } /** * Builds the GUI. */ private void buildGui() { add(c); setSize(300,300); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ ((Frame)e.getSource()).dispose(); // System.exit(0); } }); MenuBar menuBar = new MenuBar(); Menu fileMenu = new Menu("File"); menuBar.add(fileMenu); MenuItem openMenuItem = new MenuItem("Open..."); fileMenu.add(openMenuItem); openMenuItem.addActionListener(this); fileMenu.add(saveMenuItem); saveMenuItem.addActionListener(this); saveMenuItem.setEnabled(false); Menu toolsMenu = new Menu("Tools"); menuBar.add(toolsMenu); toolsMenu.add(runMenuItem); runMenuItem.addActionListener(this); runMenuItem.setEnabled(false); setMenuBar(menuBar); setLocation( (int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2) - (getWidth() / 2), (int)(Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2) - (getHeight() / 2) ); setVisible(true); } /** * Paints data to the screen. * @param g Graphics context (actually draws on a canvas, so unused) */ public void paint(Graphics g) { Image image = store.getDataAsImage(); Image results = resultsStore.getDataAsImage(); Graphics cg = c.getGraphics(); if (results != null) { cg.drawImage(results, 0, 0, this); } else { if (image != null) { cg.drawImage(image, 0, 0, this); } } } /** * Listens to menus. */ public void actionPerformed(ActionEvent e) { MenuItem clickedMenuItem = (MenuItem)e.getSource(); if (clickedMenuItem.getLabel().equals("Open...")) { FileDialog fd = new FileDialog(this, "Open File", FileDialog.LOAD); fd.setVisible(true); File f = null; if((fd.getDirectory() != null)||( fd.getFile() != null)) { f = new File(fd.getDirectory() + fd.getFile()); store.setData(io.readData(f)); setSize(store.getWidth() + getInsets().left + getInsets().right, store.getHeight() + getInsets().top + getInsets().bottom); } runMenuItem.setEnabled(true); } if (clickedMenuItem.getLabel().equals("Save Results...")) { FileDialog fd = new FileDialog(this, "Save File", FileDialog.SAVE); fd.setVisible(true); File f = null; if((fd.getDirectory() != null)||( fd.getFile() != null)) { f = new File(fd.getDirectory() + fd.getFile()); io.writeData(resultsStore.getData(), f); } } if (clickedMenuItem.getLabel().equals("Run")) { Process p = new Process(); double[][] results = p.runProcess(store.getData()); resultsStore.setData(results); saveMenuItem.setEnabled(true); } repaint(); } /** * Just calls the constructor. * @param args String sequence. Unused. */ public static void main (String args[]) { new Analyst(); } }