/* Copyright 2000 Charles G. Wright * This software may be distributed under the terms of the * GNU General Public License. * * $Id: WinCalc.java,v 1.1 2000-06-15 09:12:19-05 chuckles Exp chuckles $ */ // Class to test out the graphing object. import java.awt.*; import java.awt.event.*; import java.io.*; import java.applet.*; import java.util.zip.*; import java.net.*; import java.util.*; public class WinCalc extends Applet implements ItemListener, SimBase{ static Vector monitored_objects = new Vector(); static final int CLOSE = 101; static final int OPEN = 102; static WinCalc theApp; private CWObservable obs; private TMYdata tmystuff; boolean something_loaded = false; private SOLlocationControl loc_ctl; private static boolean is_applet = true; // display objects Frame main_window; GRAgraphs graph_panel; GRAdaygraph p1; GRAhourgraph p2; GRAoutput output_panel; CheckboxMenuItem metric_menu_item; CheckboxMenuItem ip_menu_item; private hvac hvc; // default hvac unit private Utility[] utilities; // available utilities //In case we want to use this as an application, we need // to supply a main(), and the main() needs to create a // instance of the class. public static void main(String[] args) { is_applet = false; theApp = new WinCalc(); theApp.init(); } public void init(){ // This object keeps track of all of the output elements that // need to be updated whenever inputs change. obs = new CWObservable(); main_window = new Frame("WindowCalc"); main_window.addWindowListener(new WindowHandler()); MenuBar menuBar = new MenuBar(); main_window.setMenuBar(menuBar); Menu fileMenu = new Menu("File"); MenuItem item; if (!is_applet){ fileMenu.add(item = new MenuItem("Read TMY File", new MenuShortcut('O'))); item.addActionListener(new FileCommand(OPEN)); } fileMenu.add(item = new MenuItem("Close Browser", new MenuShortcut('C'))); item.addActionListener(new FileCommand(CLOSE)); menuBar.add(fileMenu); // Create a pulldown menu for "view". Menu viewMenu = new Menu("View"); viewMenu.add(metric_menu_item = new CheckboxMenuItem("Use Metric", true)); metric_menu_item.setShortcut(new MenuShortcut('M')); metric_menu_item.addItemListener(this); viewMenu.add(ip_menu_item = new CheckboxMenuItem("Use IP", false)); ip_menu_item.setShortcut(new MenuShortcut('I')); ip_menu_item.addItemListener(this); menuBar.add(viewMenu); // background, axes, grid, data[0], data[1], etc Color colors[] = {Color.white, Color.black, Color.gray, Color.black, Color.darkGray, Color.red, Color.blue, Color.magenta, Color.green, Color.cyan, Color.orange}; main_window.setBackground(Color.lightGray); // Determines initial size and location of window. Toolkit theKit = main_window.getToolkit(); Dimension wsize = theKit.getScreenSize(); main_window.setBounds(40,40, (3 * wsize.width) / 4, (3 * wsize.height) / 4); // Create the control panel. Event handlers in the control panels // tweak obs whenever updates are made. output_panel is // passed so that when new surfaces are created, their monitors // can be installed in the output panel. graph_panel = new GRAgraphs(colors); // New day graph p1 = new GRAdaygraph(colors, graph_panel); // add it to graph_panel graph_panel.addGraph(p1, "daygraph"); // Set up p1 to receive changes reported to this level. obs.addObserver(p1); // Do p2, too. p2 = new GRAhourgraph(colors, graph_panel); graph_panel.addGraph(p2, "hourgraph"); obs.addObserver(p2); output_panel = new GRAoutput(colors); output_panel.addOutputPanel(graph_panel, "Graphs"); // temporarily hardwiring the following: utilities = new Utility[2]; utilities[0] = new Utility(Utility.ELECTRICITY, 0.08, "Electric"); utilities[1] = new Utility(Utility.GAS, 0.08, "Gas"); hvc = new hvac(3.0, 3.0, 20.0, 25.6, utilities[0], utilities[0]); // loc_ctl = new SOLlocationControl(this); GridBagLayout top_layout = new GridBagLayout(); main_window.setLayout(top_layout); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.BOTH; c.gridheight = GridBagConstraints.REMAINDER; c.weightx = 0.3; c.weighty =1.0; top_layout.setConstraints(loc_ctl, c); main_window.add(loc_ctl); c.weightx = 0.7; top_layout.setConstraints(output_panel, c); main_window.add(output_panel); main_window.setVisible(true); //Following code is only for applet if (is_applet){ openURL(); } } private void processTMY(InputStream is, String fname){ try { tmystuff = new TMYdata(); tmystuff.readData(is, fname); //tmystuff.useEnglish(); // Now that we have a tmy loaded loc_ctl.setLocation(tmystuff.location); loc_ctl.setTMY(tmystuff); loc_ctl.addOrientation(); graph_panel.tmystuff = tmystuff; obs.notifyObservers(this); } catch (IOException e) { System.out.println("IOException encountered reading TMY file"); } } private void openFile(){ // This code is commented out for applet operation FileDialog fd = new FileDialog(main_window, "open it", FileDialog.LOAD); fd.show(); String fname = fd.getFile(); if (fname == null){ System.out.println("No file selected..."); } else { try { InputStream is = new FileInputStream(new File(fd.getDirectory(), fname)); processTMY(is, fname); } catch (FileNotFoundException e){ System.out.println("ERROR: File " + fname + " not found"); } } // close the FileDialog fd.dispose(); } // This code for starting from an applet. The applet invocation // has the parameter urlname, which specifies where to go for the // data to be read. public void loadURL(String urlname){ try{ URL url = new URL(urlname); System.out.println("Protocol is " + url.getProtocol()); System.out.println("Host is " + url.getHost()); System.out.println("File is " + url.getFile()); System.out.println("Port is " + url.getPort()); System.out.println("Ref is " + url.getRef()); System.out.println("Now opening input stream for the URL"); InputStream is = url.openStream(); System.out.println("Now processing input stream as TMY file."); processTMY(is, url.getFile()); }catch (MalformedURLException e){ System.out.println("bad URL"); }catch (IOException e){ System.out.println("I/O Exception"); } } private void openURL(){ String urlname = getParameter("urlname"); if (urlname != null){ loadURL(urlname); } else { System.out.println("No URL parameter was found"); } } // Event handler for Metric/IP switch. public void itemStateChanged(ItemEvent e) { Object source = e.getSource(); if (source == metric_menu_item){ metric_menu_item.setState(true); ip_menu_item.setState(false); UnitConverter.setUnitSystem(UnitConverter.METRIC); } else if (source == ip_menu_item){ metric_menu_item.setState(false); ip_menu_item.setState(true); UnitConverter.setUnitSystem(UnitConverter.IP); } } /** Part of the SimBase interface. */ public GRAoutput getOutputPanel(){ return (output_panel); } /** Part of the SimBase interface. */ public hvac getHVAC(){ return (hvc); } /** Part of the SimBase interface. Returns an array of * Utilities. */ public Utility[] getUtilities(){ return (utilities); } /** Part of the SimBase interface. Returns the CWObservable * that causes output screen changes. */ public CWObservable getObservable(){ return (obs); } //------------------ inner classes for event handler -------------------------- class FileCommand implements ActionListener{ MenuItem item; int itemID; // Constructor public FileCommand(int itemID){ this.itemID = itemID; } public void actionPerformed(ActionEvent e) { switch(itemID){ case CLOSE: System.exit(0); break; case OPEN: openFile(); break; default: } } } //window closing handler class WindowHandler extends WindowAdapter{ // overrides empty method provided by WindowAdapter, which // provides empty methods for windowActivated(WindowEvent) // windowClosed(WindowEvent), windowClosing(WindowEvent) // windowDeactivated(WindowEvent), windowDeiconified(WindowEvent) // windowIconified(WindowEvent), windowOpened(WindowEvent) public void windowClosing(WindowEvent e){ if (e.getID() == WindowEvent.WINDOW_CLOSING){ main_window.dispose(); System.exit(0); } } } }