package interfaces.JGUI; import java.awt.*; import java.util.*; /* Part of the GUI used . This extend Panel class so that it can be scrolled */ public class ScrollPanel extends Panel { GridBagLayout gbl; GridBagConstraints gbc; private Scrollbar m_sbh, m_sbv; private int m_viewWidth = 0, m_viewHeight = 0; //the dimension of the tree view private ViewPanel m_viewPanel; private Panel m_panel; private Dimension m_curDim; ScrollPanel() { setLayout(null); m_panel = new Panel(); add(m_panel); gbl = new GridBagLayout(); gbc = new GridBagConstraints(); m_panel.setLayout(gbl); //setLayout(new FlowLayout(FlowLayout.LEFT)); gbc.fill = GridBagConstraints.BOTH; gbc.anchor = GridBagConstraints.CENTER; gbc.weightx = 1; gbc.weighty = 1; m_viewPanel = new ViewPanel(this); add(m_panel, m_viewPanel, gbc, gbl, 0, 0, 10, 10); Dimension d = this.size(); m_curDim = d; m_sbh = new Scrollbar(Scrollbar.HORIZONTAL, 0, d.width, 0, 0); m_sbv = new Scrollbar(Scrollbar.VERTICAL, 0, d.height, 0, 0); m_sbh.setPageIncrement(100); m_sbh.setLineIncrement(10); m_sbv.setPageIncrement(100); m_sbv.setLineIncrement(10); gbc.weightx = 1; gbc.weighty = 0; add(m_panel, m_sbh, gbc, gbl, 0, 11, 10, 1); gbc.weightx = 0; gbc.weighty = 1; add(m_panel, m_sbv, gbc, gbl, 11, 0, 1, 10); validate(); } public void insertItem(Component c) { m_viewPanel.insertItem(c); } public void insertItem(Component c, int w, int h) { m_viewPanel.insertItem(c, w, h); } public void insertItem(Component c, int w, int h, Component c2, int w2, int h2) { m_viewPanel.insertItem(c, w, h, c2, w2, h2); } public void removeAllItem() { m_viewPanel.removeAllItem(); } public void scrollUpdate() { m_viewPanel.translate(m_sbh.getValue(), m_sbv.getValue()); validate(); } public void setScroll(int x, int y) { m_viewWidth = x; m_viewHeight = y; Dimension d, dsbh, dsbv; int visWidth, visHeight; d = this.size(); dsbh = m_sbh.size(); dsbv = m_sbv.size(); visWidth = d.width - dsbv.width - 20; visHeight = d.height - dsbh.height - 20; if (x > visWidth) m_sbh.enable(); else m_sbh.disable(); if (y > visHeight) m_sbv.enable(); else m_sbv.disable(); m_sbh.setValues(m_sbh.getValue(), visWidth, 0, m_viewWidth - visWidth + dsbv.width); m_sbv.setValues(m_sbv.getValue(), visHeight, 0, m_viewHeight - visHeight + dsbh.height); validate(); } //overrides to update scrollbar info public void validate() { super.validate(); Dimension d; d = size(); if (d.width == m_curDim.width && d.height == m_curDim.height) return; m_curDim = d; m_panel.reshape(1, 1, d.width - 2, d.height - 2); add(m_panel); } public boolean handleEvent(Event evt) { if (evt.target instanceof Scrollbar) { Scrollbar sb = (Scrollbar)evt.target; if (sb == m_sbh || sb == m_sbv) { scrollUpdate(); return true; } } return super.handleEvent(evt); } //public void update(Graphics g) { // paint(g); //} public void paint(Graphics g) { Dimension d = size(); g.setColor(Color.black); g.drawRect(0, 0, d.width - 1, d.height - 1); } public void add(Container p, Component c, GridBagConstraints gbc, GridBagLayout gbl, int x, int y, int w, int h) { gbc.gridx = x; gbc.gridy = y; gbc.gridwidth = w; gbc.gridheight = h; gbl.setConstraints(c, gbc); p.add(c); } } class ViewPanel extends Panel { public int m_childW = 100; public int m_childH = 20; private int m_curWidth = 2; private int m_curHeight = 2; private int m_dx2 = 0, m_dx = 0; private int m_dy2 = 0, m_dy = 0; private ScrollPanel m_parent; private MoveablePanel m_panel; private Vector m_itemQ; ViewPanel(ScrollPanel parent) { m_parent = parent; setLayout(null); m_itemQ = new Vector(); m_panel = new MoveablePanel(m_itemQ); m_panel.setLayout(null); add(m_panel); } public void insertItem(Component c, int w, int h, Component c2, int w2, int h2) { int totalW, totalH; Dimension d = c.size(); totalW = w + w2; if (h > h2) totalH = h; else totalH = h2; if (totalW > m_curWidth) m_curWidth = totalW; m_itemQ.addElement(c); m_itemQ.addElement(c2); c.reshape(2, m_curHeight, w, h); c2.reshape(w + 7, m_curHeight, w2, h2); m_panel.add(c); m_panel.add(c2); m_curHeight += totalH + 2; m_parent.setScroll(m_curWidth, m_curHeight); m_panel.resize(m_curWidth, m_curHeight); } public void insertItem(Component c, int w, int h) { int num; Dimension d = c.size(); if (w > m_curWidth) m_curWidth = w; m_itemQ.addElement(c); num = m_itemQ.size(); c.reshape(2, m_curHeight, w, h); m_panel.add(c); m_curHeight += h + 2; m_parent.setScroll(m_curWidth, m_curHeight); m_panel.resize(m_curWidth, m_curHeight); } public void insertItem(Component c) { int num; Dimension d = c.size(); m_itemQ.addElement(c); num = m_itemQ.size(); c.reshape(2, m_curHeight, m_childW, m_childH); m_panel.add(c); m_curHeight += m_childH + 2; m_parent.setScroll(m_curWidth, m_curHeight); m_panel.resize(m_curWidth, m_curHeight); } public void removeAllItem() { m_itemQ.removeAllElements(); m_panel.removeAll(); m_curHeight = 2; m_curWidth = 2; m_parent.setScroll(m_curWidth, m_curHeight); m_panel.resize(m_curWidth, m_curHeight); } public void translate(int x, int y) { m_dx2 += x - m_dx; m_dy2 += y - m_dy; m_dx = x; m_dy = y; repaint(); } public void paint(Graphics g) { int i, numPoint; Point p, start, end; Dimension d = size(); //g.setColor(Color.green); //g.drawRect(0, 0, d.width - 1, d.height - 1); //g.translate(-m_dx, -m_dy); //g.drawString("ScrollView", 0, 10); p = m_panel.location(); p.translate(-m_dx2, -m_dy2); m_panel.move(p.x, p.y); m_dx2 = m_dy2 = 0; } } class MoveablePanel extends Panel { Vector m_itemQ; MoveablePanel(Vector items) { m_itemQ = items; } public void paint(Graphics g) { //Dimension d = size(); //g.setColor(Color.red); //g.drawRect(0, 0, d.width - 1, d.height - 1); /* int i, num; Point start, end; Component c; num = m_itemQ.size(); for (i = 0; i < num; i++) { c = (Component) m_itemQ.elementAt(i); c.repaint(); } */ } }