Copyright (C) 2000, 2001, Geert Vernaeve. All Rights Reserved.
package gve.calc; import awt.*; import awt.Button; import java.awt.*; import java.awt.FileDialog; import java.awt.event.*; import java.applet.Applet; import java.io.*; import java.util.Vector; public class StackWindow extends Frame implements WindowListener,AdjustmentListener,ActionListener { private StackCanvas stack; private Scrollbar scroller,horscroller; private static Applet applet; private MenuButton insertmenu; protected Button undoButton,cutButton,copyButton,pasteButton; private static int count = 0; // # of StackWindows open private static Vector instances = new Vector(); public StackWindow(Applet app) { super("SICECAS " + Main.versionString); count++; applet = app; Panel menuBar = new Panel(); menuBar.setLayout(new FlowLayout(FlowLayout.LEFT)); menuBar.setBackground(Color.lightGray); MenuButton mb; menuBar.add(mb = new MenuButton("Project",this,MenuButton.DOWN)); Button b; mb.add(b = new awt.Button("New")); b.addActionListener(this); mb.add(b = new awt.Button("Save LaTeX ...")); b.addActionListener(this); MenuButton fontmenu = new MenuButton("Font",this,MenuButton.RIGHT); mb.add(fontmenu); mb.add(b = new awt.Button("About ...")); b.addActionListener(this); mb.add(b = new awt.Button("Quit")); b.addActionListener(this); mb.pack(); menuBar.add(mb = new MenuButton("Edit",this,MenuButton.DOWN)); mb.add(b = new awt.Button("Undo")); b.addActionListener(this); undoButton = b; b.setEnabled(false); mb.add(b = new awt.Button("Cut")); b.addActionListener(this); cutButton = b; b.setEnabled(false); mb.add(b = new awt.Button("Copy")); b.addActionListener(this); copyButton = b; b.setEnabled(false); mb.add(b = new awt.Button("Paste")); b.addActionListener(this); pasteButton = b; b.setEnabled(false); mb.pack(); menuBar.add(mb = new MenuButton("Insert",this,MenuButton.DOWN)); insertmenu = mb; mb.add(b = new awt.Button("New Graph")); b.addActionListener(this); mb.pack(); add("North",menuBar); add("East",scroller = new Scrollbar(Scrollbar.VERTICAL)); scroller.addAdjustmentListener(this); add("South",horscroller = new Scrollbar(Scrollbar.HORIZONTAL)); horscroller.addAdjustmentListener(this); add("Center",stack = new StackCanvas(scroller,horscroller,this)); { // font menu String [] fonts = Toolkit.getDefaultToolkit().getFontList(); for (int i = 0; i < fonts.length; i++) { fontmenu.add(new FontButton(fonts[i],stack)); } fontmenu.add(b = new Button("Size ...")); b.addActionListener(this); fontmenu.pack(); } setSize(new Dimension(300,200)); setVisible(true); // menuBar.addKeyListener(stack); // Very strange bug (linux, jdk1.1.6): only 1st added component gets keypresses // addKeyListener(stack); // I hope this stabilizes mswindows behaviour addWindowListener(this); instances.add(this); stack.requestFocus(); stack.importpkg("gve.calc.formula"); stack.importpkg("gve.calc.logic"); }
str: button label text name: fully qualified name of class
public void addInsertItem(String str,String name) { Button b = new awt.Button(str); insertmenu.add(b); b.setName("insert new "+name); b.addActionListener(this); insertmenu.pack(); } public void dispose() { awt.MenuButton.closepopups(); super.dispose(); } public StackCanvas getCanvas() { return stack; } public void windowActivated(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowClosing(WindowEvent e) { quit(); } public void windowDeactivated(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowOpened(WindowEvent e) {}
Call adjustMenus() for each StackWindow's canvas
public static void adjustAllMenus() { for (int i = instances.size()-1; i>=0; i--) { Object o = instances.elementAt(i); if (!(o instanceof StackWindow)) continue; StackWindow sw = (StackWindow)o; sw.getCanvas().adjustMenus(); } } public void quit() { setVisible(false); dispose(); count--; if (count == 0) { try { System.exit(0); } catch (Exception exc) {} // applets cannot exit() } instances.remove(this); } public void adjustmentValueChanged(AdjustmentEvent e) { if (e.getSource() == scroller) stack.moveY(e.getValue()); else if (e.getSource() == horscroller) stack.moveX(e.getValue()); } public static StackWindow createNew() { return new StackWindow(applet); } public void actionPerformed(ActionEvent evt) { String label = evt.getActionCommand(); if (evt.getSource() instanceof Button) { Button b = (Button)evt.getSource(); String name = b.getName(); if (name!=null && name.startsWith("insert new ")) { name = name.substring(11); stack.newObject(name); return; } } if (label.equals("New")) { new StackWindow(applet); } else if (label.equals("Save LaTeX ...")) { FileDialog fd = new FileDialog(this,"Save LaTex",FileDialog.SAVE); fd.show(); if (fd.getDirectory()==null || fd.getFile()==null) return; try { BufferedWriter w; w = new BufferedWriter(new FileWriter(new File(fd.getDirectory(),fd.getFile()))); stack.saveLatex(w); w.close(); } catch (IOException exc) { new SimpleDialog(this,"Save error",exc,false); } } else if (label.equals("Quit")) { quit(); } else if (label.equals("About ...")) new AboutWindow(applet); else if (label.equals("New Graph")) stack.newGraph(); else if (label.equals("Size ...")) new FontSizeFrame(stack); else if (label.equals("Undo")) stack.undo(); else if (label.equals("Cut")) stack.cut(); else if (label.equals("Copy")) stack.copy(); else if (label.equals("Paste")) stack.paste(); else System.out.println("StackWindow: unknown item "+label); } } class FontSizeFrame extends Frame implements ActionListener { private Component comp; private TextField tf; public FontSizeFrame(Component c) { super("Set font size"); setBackground(Color.lightGray); comp = c; Panel p = new Panel(); Button b; p.add(b = new Button("--")); b.addActionListener(this); p.add(b = new Button("-")); b.addActionListener(this); tf = new TextField(""+c.getFont().getSize()) ; p.add(tf); tf.addActionListener(this); p.add(b = new Button("+")); b.addActionListener(this); p.add(b = new Button("++")); b.addActionListener(this); add("Center",p); p = new Panel(); p.add(b = new Button("OK")); b.addActionListener(this); p.add(b = new Button("Apply")); b.addActionListener(this); p.add(b = new Button("Cancel")); b.addActionListener(this); add("South",p); pack(); show(); } public void actionPerformed(ActionEvent evt) { String cmd = evt.getActionCommand(); if (cmd.equals("Cancel")) { dispose(); return; } int newsize; try { newsize = Integer.parseInt(tf.getText()); } catch (Exception exc) { return; } Font font = comp.getFont(); if (cmd.equals("+")) newsize++; else if (cmd.equals("++")) newsize *= 2; else if (cmd.equals("-") && newsize > 5) newsize--; else if (cmd.equals("--") && newsize > 10) newsize /= 2; else if (cmd.equals("OK")) dispose(); if (evt.getSource()==tf || cmd.equals("OK") || cmd.equals("Apply")) { comp.setFont(new Font(font.getName(),font.getStyle(),newsize)); // Show the new font on the screen FontButton.invalidateTree(comp); comp.validate(); } tf.setText(""+newsize); } } class FontButton extends awt.Button implements ActionListener { private Component comp; public FontButton(String label,Component comp) { super(label); this.comp = comp; addActionListener(this); } public static void invalidateTree(Component comp) { comp.invalidate(); if (comp instanceof Container) { Container cont = (Container)comp; int i = 0; while (true) { Component child = null; try { child = cont.getComponent(i++); } catch (ArrayIndexOutOfBoundsException exc) { break; } invalidateTree(child); } } } public void actionPerformed(ActionEvent evt) { Font font = comp.getFont(); comp.setFont(new Font(getLabel(),font.getStyle(),font.getSize())); // Show the new font on the screen invalidateTree(comp); comp.validate(); } }