Mercurial > hg > jslab
diff src/samer/core_/util/swing/Frame.java @ 0:bf79fb79ee13
Initial Mercurial check in.
author | samer |
---|---|
date | Tue, 17 Jan 2012 17:50:20 +0000 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/samer/core_/util/swing/Frame.java Tue Jan 17 17:50:20 2012 +0000 @@ -0,0 +1,122 @@ +/* + * Frame.java + * + * Copyright (c) 2000, Samer Abdallah, King's College London. + * All rights reserved. + * + * This software is provided AS iS and WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + */ + +package samer.core.util.swing; +import samer.core.*; +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; + +/** + <p> + This is a useful kind of framed window that uses + the property set to get window size and position + and colours. It can also send window closing events + to the default command handler (see Agency) as + "exit" actions. + + <p> + Addition: frame can dispose of itself when last component + is removed. + */ + +public class Frame extends JFrame + implements ComponentListener, ActionListener, Shell.Window +{ + Node node; + + public Frame() { this("frame"); } + public Frame( String nm) { + // remove "." from start of name + if (nm.startsWith(".")) nm=nm.substring(1); + node=new Node(nm); init(); + } + + public Container container() { return getContentPane(); } + public void dispose() { super.dispose(); } + + public void expose() { + // this makes sure we don't show a zero-sized window + if (getBounds().isEmpty()) pack(); + setVisible(true); + } + + public Node getNode() { return node; } + + // ..... Event Listener bits ....... + + public void componentHidden(ComponentEvent e) {} + public void componentShown(ComponentEvent e) {} + public void componentMoved(ComponentEvent e) { savepos(); } + public void componentResized(ComponentEvent e) { savepos(); } + public void actionPerformed(ActionEvent e) { + if (e.getActionCommand().equals("pack")) pack(); + } + + // override pack to stop window being bigger than screen + public void pack() { + super.pack(); + + Dimension d=getToolkit().getScreenSize(); + Rectangle b=getBounds(); + boolean f=false; + if (b.y+b.height>d.height) { + b.height=d.height-b.y; + b.width+=16; // this is to make room for vertical scroll bar (hack - sorry) + f=true; + } + if (b.x+b.width>d.width) { b.width=d.width-b.x; f=true; } + if (f) setBounds(b); + } + + // ........... private bits ............... + + private void savepos() { X.store(node.fullNameFor("bounds"),getBounds()); } + + private void init() + { + Shell.push(node); + try { + String tit = Shell.getString("title",null); + Color bg = Shell.getColor("background",SystemColor.control); + Color fg = Shell.getColor("foreground",SystemColor.controlText); + Rectangle pos = X.rect(Shell.datum("bounds"),null); + // Font font = X.font(Shell.datum("font"),null); + + if (tit!=null) setTitle(tit); + else { + // remove "." from start of name + String n=node.fullName(); + if (n.startsWith(".")) n=n.substring(1); + // remove ".window" from end of name + if (n.endsWith(".window")) n=n.substring(0,n.length()-7); + setTitle(n); + } + + if (pos!=null) setBounds(pos); + // if (font!=null) setFont(font); + + // setBackground(bg); + // setForeground(fg); + } finally { Shell.pop(); } + + setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); + + JPopupMenu p=new JPopupMenu( "Frame"); + p.add("pack").addActionListener(this); + + addComponentListener(this); + MenuBuilder.addPopup(p,getContentPane()).setBackstop(true); + } + + +} +