import javax.swing.*; //import com.sun.java.swing.*; import java.awt.event.*; import java.awt.*; import java.applet.*; public class JModalPanel extends JDesktopPane { private int containerWidth, containerHeight; private boolean comboBoxVersion; private String enterButtonText; private JComboBox comboBox; private JControlPanel controlPanel; private Container container; private CoverableContainer ret; private Image buffer; private Graphics bg; private Image background; MoveListener ml; Component[] components; private LayoutManager layoutManager; public JModalPanel(Container container, CoverableContainer ret, String text, String initText) { super(); this.container = container; while(!(this.container instanceof Applet) && !(this.container instanceof JApplet) && !(this.container instanceof Frame) && !(this.container instanceof JFrame)) this.container = this.container.getParent(); this.ret = ret; this.containerWidth = this.container.getSize().width; this.containerHeight = this.container.getSize().height; setVisible(false); container.add(this); background = createImage(containerWidth, containerHeight); Graphics backG = background.getGraphics(); this.container.paint(backG); backG.dispose(); if(this.container instanceof JApplet) this.container = ((JApplet)this.container).getContentPane(); if(this.container instanceof JFrame) this.container = ((JFrame)this.container).getContentPane(); // this is to fix a bug in the jdk1.2, next two lines must be here setVisible(true); setBounds(0, 0, this.containerWidth, this.containerHeight); components = this.container.getComponents(); this.container.removeAll(); this.container.add(this); if(this.container.getLayout() != null) layoutManager = this.container.getLayout(); this.container.setLayout(null); buffer = createImage(containerWidth, containerHeight); bg = buffer.getGraphics(); bg.drawImage(background,0,0,this); enterButtonText = "Enter"; comboBoxVersion = false; controlPanel = new JControlPanel(text, initText); add(controlPanel); controlPanel.validate(); setVisible(true); requestFocus(); controlPanel.requestFocus(); controlPanel.userEntry.requestFocus(); } public JModalPanel(Container container, CoverableContainer ret, String text) { this(container, ret, text, ""); } public JModalPanel(Container container, CoverableContainer ret, String text, JComboBox comboBox, String enterButtonText) { super(); comboBoxVersion = true; this.comboBox = comboBox; this.enterButtonText = enterButtonText; this.container = container; while(!(this.container instanceof Applet) && !(this.container instanceof JApplet) && !(this.container instanceof Frame) && !(this.container instanceof JFrame)) this.container = this.container.getParent(); this.ret = ret; this.containerWidth = this.container.getSize().width; this.containerHeight = this.container.getSize().height; setVisible(false); container.add(this); background = createImage(containerWidth, containerHeight); Graphics backG = background.getGraphics(); this.container.paint(backG); backG.dispose(); if(this.container instanceof JApplet) this.container = ((JApplet)this.container).getContentPane(); if(this.container instanceof JFrame) this.container = ((JFrame)this.container).getContentPane(); // this is to fix a bug in the jdk1.2, next two lines must be here setVisible(true); setBounds(0, 0, this.containerWidth, this.containerHeight); components = this.container.getComponents(); this.container.removeAll(); this.container.add(this); buffer = createImage(containerWidth, containerHeight); bg = buffer.getGraphics(); bg.drawImage(background,0,0,this); enterButtonText = "Enter"; controlPanel = new JControlPanel(text, comboBox); add(controlPanel); controlPanel.validate(); setVisible(true); if(this.container.getLayout() != null) layoutManager = this.container.getLayout(); this.container.setLayout(null); requestFocus(); controlPanel.requestFocus(); } public void paint(Graphics g) { g.setClip(0,0,containerWidth,containerHeight); g.drawImage(buffer, 0, 0, this); g.setClip(controlPanel.getBounds()); super.paint(g); } public class JControlPanel extends JInternalFrame { JTextField userEntry; int width = 250; int height = 150; public JControlPanel(String text, String initText) { super(); System.out.println("bye, after"); getContentPane().setLayout(new BorderLayout()); JPanel basePanel = new JPanel(new BorderLayout()); JPanel buttonPanel = new JPanel(new FlowLayout(), false); buttonPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); JButton enterButton = new JButton(enterButtonText); enterButton.addActionListener(new SubmitTextButtonListener()); JButton cancelButton = new JButton("Cancel"); cancelButton.addActionListener(new CancelButtonListener()); buttonPanel.add(enterButton); buttonPanel.add(cancelButton); basePanel.add(buttonPanel,BorderLayout.SOUTH); JLabel textLabel = new JLabel(text); textLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); userEntry = new JTextField(initText); userEntry.setPreferredSize(new Dimension(textLabel.getSize().width-10, userEntry.getPreferredSize().height)); userEntry.addActionListener(new SubmitTextButtonListener()); basePanel.add(textLabel,BorderLayout.NORTH); basePanel.add(userEntry,BorderLayout.CENTER); basePanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); getContentPane().add(basePanel, BorderLayout.CENTER); userEntry.requestFocus(); if(getPreferredSize().width+30 > containerWidth-100) { setSize(containerWidth-100, getPreferredSize().height); } else { setSize(getPreferredSize().width+30, getPreferredSize().height); } setLocation((container.getSize().width-getSize().width)/2, (container.getSize().height-getSize().height)/2); } public JControlPanel(String text, JComboBox comboBox) { super(); getContentPane().setLayout(new BorderLayout()); JPanel basePanel = new JPanel(new BorderLayout()); JPanel buttonPanel = new JPanel(new FlowLayout(), false); buttonPanel.setBorder(BorderFactory.createEmptyBorder(10,5,10,5)); JButton enterButton = new JButton(enterButtonText); enterButton.addActionListener(new SubmitComboBoxButtonListener()); JButton cancelButton = new JButton("Cancel"); cancelButton.addActionListener(new CancelButtonListener()); buttonPanel.add(enterButton); buttonPanel.add(cancelButton); basePanel.add(buttonPanel,BorderLayout.SOUTH); JLabel textLabel = new JLabel(text); textLabel.setBorder(BorderFactory.createEmptyBorder(10,5,10,5)); comboBox.setMaximumRowCount(3); basePanel.add(textLabel,BorderLayout.NORTH); basePanel.add(comboBox,BorderLayout.CENTER); basePanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); getContentPane().add(basePanel, BorderLayout.CENTER); if(getPreferredSize().width+50 > containerWidth-100) { setSize(containerWidth-100, getPreferredSize().height+5); } else { setSize(getPreferredSize().width+50, getPreferredSize().height+5); } setLocation((container.getSize().width-getSize().width)/2, (container.getSize().height-getSize().height)/2); } public void requestFocus() { super.requestFocus(); } } public class MoveListener extends MouseAdapter implements MouseMotionListener { int dx, dy; int pWidth, pHeight; boolean moving = false; Graphics g; public void mousePressed(MouseEvent e) { moving = true; pWidth = controlPanel.getSize().width; pHeight = controlPanel.getSize().height; dx = e.getX()- controlPanel.getLocation().x; dy = e.getY()- controlPanel.getLocation().y; g = getGraphics(); bg.drawImage(background,0,0,null); bg.setColor(Color.blue); bg.drawRect(e.getX()-dx,e.getY()-dy,pWidth,pHeight); g.drawImage(buffer,0,0,null); } public void mouseDragged(MouseEvent e) { if(moving) { int newX = e.getX()-dx; int newY = e.getY()-dy; if(newX < 0) newX = 0; if((newX+pWidth)>containerWidth) newX = containerWidth-pWidth; if(newY < 0) newY = 0; if((newY+pHeight)>containerHeight) newY = containerHeight-pHeight; bg.drawImage(background,0,0,null); bg.drawRect(newX,newY,pWidth,pHeight); g.drawImage(buffer,0,0,null); } } public void mouseReleased(MouseEvent e) { if(moving) { int newX = e.getX()-dx; int newY = e.getY()-dy; if(newX < 0) newX = 0; if((newX+pWidth)>containerWidth) newX = containerWidth-pWidth; if(newY < 0) newY = 0; if((newY+pHeight)>containerHeight) newY = containerHeight-pHeight; controlPanel.setLocation(newX,newY); bg.drawImage(background,0,0,null); g.drawImage(buffer,0,0,null); } moving = false; } public void mouseMoved(MouseEvent e) {} } public void dispose() { try { container.setLayout(layoutManager); for(int i=0; i