/* Copyright 2000 Charles G. Wright * This software may be distributed under the terms of the * GNU General Public License. * * $Id: SOLorientationControl.java,v 1.1 2000-06-15 10:15:03-05 chuckles Exp chuckles $ */ import java.awt.*; import java.awt.event.*; import java.math.*; import java.util.*; //--------------------------- SOLorientationControl ----------------------- /**
This class provides a control panel for a SOLorientation, allowing * specification of its name, azimuth, and tilt. It also provides an * area in which is displayed the control panels (SOLsurfaceControl) * for one or more surface with this orientation. These are placed * using the CardLayout manager, and the SOLorientationControl provides * buttons for scrolling through the possible surfaces. * Constructors allow it to be created using an existing SOLorientation, * or for the SOLorientation to be created on the fly. */ public class SOLorientationControl extends Panel implements ActionListener{ private CWObservable obs1; // This one is used just to notify the // surfaces of this orientation when the orientation changes. private static int instance_number = 1; private TextField tf0; private InputField p1; private InputField p2; private Button bleft; private Button bright; private Button bnew_surface; private Button bnew_window; private int surfnum = 2; private Panel cp; private CardLayout cl; private SimBase top; private TMYdata tmystuff; SOLorientation orient; //--------------- Constructors ----------------------------------------// /** Create a SOLorientationControl object, given a SOLlocation, an azimuth, * a tilt, and a name. */ public SOLorientationControl(TMYdata tmystuff, SOLlocation location, double surface_azimuth, double surface_tilt, String name, SimBase top){ this.top = top; this.tmystuff = tmystuff; orient = new SOLorientation(location, surface_azimuth, surface_tilt, name); System.out.println("Created new orientation with azimuth = " + orient.surface_azimuth + " , and surface tilt = " + orient.surface_tilt); buildit(); } // ------------------ Methods --------------------------------// /** create the user interface */ private void buildit(){ obs1 = new CWObservable(); // Layout manager for horizontal panels: FlowLayout fl = new FlowLayout(FlowLayout.LEFT, 2, 2); GridBagLayout main_layout = new GridBagLayout(); setLayout(main_layout); // Line up elements along a vertical line. // Fill horizontal space. // Use minimal vertical space for each element // (except last). GridBagConstraints c = new GridBagConstraints(); c.gridy = GridBagConstraints.RELATIVE; c.gridwidth = GridBagConstraints.REMAINDER; c.fill = GridBagConstraints.HORIZONTAL; c.weighty = 0.0; c.anchor = GridBagConstraints.NORTHWEST; c.insets = new Insets(3,3,3,3); // main panel Panel p0 = new Panel(fl); p0.add(new Label("Orientation:")); tf0 = new TextField(10); tf0.addActionListener(this); tf0.setText(orient.name); p0.add(tf0); main_layout.setConstraints(p0, c); add(p0); // azimuth p1 = new InputField(UnitConverter.NOTHING, 0.0, 1.0); p1.setNameText("Azimuth"); p1.setLabelText("Degrees"); p1.addActionListener(this); main_layout.setConstraints(p1, c); add(p1); // tilt p2 = new InputField(UnitConverter.NOTHING, 90.0, 1.0); p2.setNameText("Tilt"); p2.setLabelText("Degrees"); p2.addActionListener(this); main_layout.setConstraints(p2, c); add(p2); Panel p3 = new Panel(fl); bleft = new Button("<"); bleft.addActionListener(this); p3.add(bleft); bright = new Button(">"); bright.addActionListener(this); p3.add(bright); bnew_surface = new Button("New Surface"); bnew_window = new Button("New Window"); bnew_surface.addActionListener(this); bnew_window.addActionListener(this); p3.add(bnew_surface); p3.add(bnew_window); main_layout.setConstraints(p3, c); add(p3); // Panel p5 holds one or more surfaces, displaying 1 at a time. One is // created at start, more may be added later. cl = new CardLayout(); cp = new Panel(cl); addWindow(); // Arrange this one to take up the rest of the space. c.fill = GridBagConstraints.BOTH; c.weighty = 1.0; main_layout.setConstraints(cp, c); add(cp); cl.first(cp); instance_number++; repaint(); } public void paint(Graphics g){ setForeground(Color.black); Dimension d = getSize(); g.drawRect(0, 0, d.width - 1, d.height - 1); } // a class method telling which instance number is next. static public int nextInstanceNumber(){ return (instance_number); } public void addSurface(){ String surf_name = "Surface " + SOLsurface.nextInstanceNumber(); SOLsurfaceControl surf = new SOLsurfaceControl(tmystuff, orient, 1.0, 0.15, surf_name, top); obs1.addObserver(surf); cp.add(surf, surf_name); } public void addWindow(){ String surf_name = "Window " + SOLwindow.nextInstanceNumber(); SOLwindowControl surf = new SOLwindowControl(tmystuff, orient, 1.0, 0.15, 1.5, 0.8, 25.0, surf_name, top); obs1.addObserver(surf); cp.add(surf, surf_name); } public void newTilt(double tilt){ orient.surface_tilt = tilt; } /** Here is where we come when the enter key is pressed in the text field. */ public void actionPerformed(ActionEvent e){ Object source = e.getSource(); if (source == tf0) { // Name has changed. Get/set new value. orient.name = tf0.getText(); } else if (source == p1) { // azimuth has changed. Get the new value. orient.surface_azimuth = p1.getValue(); top.getObservable().setChanged(); obs1.setChanged(); top.getObservable().notifyObservers(); obs1.notifyObservers(); } else if (source == p2){ // new surface tilt orient.surface_tilt = p2.getValue(); System.out.println("setting surface tilt to "+orient.surface_tilt); top.getObservable().setChanged(); obs1.setChanged(); top.getObservable().notifyObservers(); obs1.notifyObservers(); } else if (source == bleft){ cl.previous(cp); } else if (source == bright){ cl.next(cp); } else if (source == bnew_surface){ addSurface(); } else if (source == bnew_window){ addWindow(); } } } /* end of class */