package com.dwave.gui; /* * DWaveToolbar * * Copyright (C) 2000 D-Wave Systems Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ import javax.swing.*; import java.util.Vector; import com.dwave.layout.*; import java.awt.*; import java.awt.event.*; import com.dwave.util.*; /** * Work still needs to be done to make this class a little easier to integrate * into someone's program, but it should work fine. DWaveToolbar resides in * a DockingPanel. These DockingPanels will generally be added to a window * using BorderLayout. The frame that you're adding a DWaveToolbar to would * have code resembling: * * DockingPanel topDock = new DockingPanel(CircuitInfo.HORIZONTAL); * DockingPanel bottomDock = new DockingPanel(CircuitInfo.HORIZONTAL); * DockingPanel leftDock = new DockingPanel(CircuitInfo.VERTICAL); * DockingPanel rightDock = new DockingPanel(CircuitInfo.VERTICAL); * JButton [] bArray = { newButton, openFileButton, saveFileButton, helpButton }; * DWaveToolbar fileBar = new DWaveToolbar(bArray, "Program", defaultDock); * * topDock.dockToolbar(fileBar); * fileBar.setDocked (true); * fileBar.setOwner (topDock); * * this.getContentPane().setLayout(new BorderLayout()); * this.getContentPane().add("North", topDock); * this.getContentPane().add("South", bottomDock); * this.getContentPane().add("East", rightDock); * this.getContentPane().add("West", leftDock); * * There's not a lot of documentation/commenting. This will be remedied * when we have more time to work on it. We will eventually be adding support * for other component types besides buttons, but for now only buttons work. * Eventually, we'll probably be using a different layout manager than XYLayout, * possibly null or BoxLayout. * * Please feel free to make use of/improve these classes, but we request that you * leave these comments intact. If you make any improvements, or if you have * questions/bugs to report/etc. regarding this class, please contact us at: * mcoury@dwavesys.com * * * @author Michael Coury * @author J.P. Hilton * @version 1.0 10/06/2000 * * @see com.dwave.util.DockingPanel */ // need to implement MouseListener for serialization public class DWaveToolbar extends JInternalFrame implements java.io.Serializable, MouseListener, MouseMotionListener { private Vector buttonStorage = new Vector(1,1); private JPanel thePanel; private AbstractButton dragButton; private int orientation=0, hgap=0, vgap=0; private DockingPanel owner; private int numRowsOrCols=1, dragButtonSize = 5; private String title; private boolean docked, wasDocked; private boolean mousePressed = false; public static final int VERTICAL = 0, HORIZONTAL = 1; /** * constructors */ public DWaveToolbar() { this (new AbstractButton [0]); } public DWaveToolbar(AbstractButton [] buttons) { this(buttons, ""); } public DWaveToolbar(AbstractButton [] buttons, String title) { this(buttons, title, null); } public DWaveToolbar(AbstractButton [] buttons, String title, DockingPanel owner) { super(title, false, false, false, true); this.setRootPaneCheckingEnabled(false); for (int i = 0; i < buttons.length; i++) buttonStorage.addElement (buttons[i]); thePanel = new JPanel(new XYLayout ()); thePanel.setBorder(BorderFactory.createEtchedBorder()); this.title = title; this.getContentPane().setLayout(new BorderLayout()); this.setBorder(BorderFactory.createRaisedBevelBorder()); dragButton = new JButton (); dragButton.setBackground(java.awt.Color.darkGray); dragButton.setToolTipText("Toolbar drag handle"); dragButton.addMouseMotionListener (this); dragButton.addMouseListener(this); if(owner != null) { this.owner = owner; docked = false; } else docked = false; try { this.toolbarLayout(); } catch(DockingException e) { e.printStackTrace(); } } /** * layout the toolbar */ public void toolbarLayout () throws DockingException { int width=0, height=0, tempx=hgap, tempy=vgap, rowColNum=0; AbstractButton tempButton = new JButton (); boolean first = true; int size = buttonStorage.size (), row = numRowsOrCols, col = size/row, rem = size%row, rowCount = 1; if(owner != null) { switch(owner.getOrientation()) { case(DWaveToolbar.VERTICAL): tempy += dragButtonSize; int y = 0; for (int j = 0; j < row; j++) { for(int i = 0; i < col; i++) { tempButton = (AbstractButton) buttonStorage.elementAt(y); tempButton.setSize (tempButton.getIcon ().getIconWidth() + 10,tempButton.getIcon ().getIconHeight() + 10); thePanel.add(tempButton, new XYConstraints(tempx, tempy, tempButton.getWidth(), tempButton.getHeight())); tempy += tempButton.getHeight () + vgap; y++; } height = tempy; tempy = vgap + 10; tempx += tempButton.getWidth () + vgap; } thePanel.setSize (tempx + hgap, height + vgap + 20); thePanel.add(dragButton, new XYConstraints(0, 0, tempx, dragButtonSize)); break; case(DWaveToolbar.HORIZONTAL): tempx += dragButtonSize; int x = 0; for (int j = 0; j < row; j++) { for(int i = 0; i < col; i++) { if(buttonStorage.elementAt(x) instanceof JLabel) { JLabel seperator = (JLabel) buttonStorage.elementAt(x); thePanel.add(seperator, new XYConstraints(tempx, tempy,10,10)); tempx += 10 + hgap; } else { tempButton = (AbstractButton) buttonStorage.elementAt(x); tempButton.setSize (tempButton.getIcon ().getIconWidth() + 10,tempButton.getIcon ().getIconHeight() + 10); thePanel.add(tempButton, new XYConstraints(tempx, tempy, tempButton.getWidth(), tempButton.getHeight())); tempx += tempButton.getWidth ()+ hgap; } x++; } width = tempx; tempx = hgap + 10; tempy += tempButton.getHeight () + vgap; } thePanel.setSize (width + hgap, tempy + 25 + vgap); thePanel.add(dragButton, new XYConstraints(0,0,dragButtonSize,tempy)); break; default: throw new DockingException("Owner does not seem to have any orientation"); } } else throw new DockingException("No owner for toolbar"); this.getContentPane().add("Center", thePanel); this.setSize (thePanel.getWidth (), thePanel.getHeight ()); this.setVisible(true); } /** * get/set methods */ public String getTitle() { return title; } public JPanel getPanel() { return thePanel; } public void setNumberOfRowsOrColumns(int aNum) { numRowsOrCols = aNum; } public int getNumberOfRowsOrColumns() { return numRowsOrCols; } public void setOwner (DockingPanel newOwner) { owner = newOwner; } public DockingPanel getOwner () { return owner; } public void setDocked(boolean isDocked) { docked = isDocked; if(docked) this.setVisible(false); else { try { this.setVisible(true); this.getContentPane().add("Center", thePanel); this.toolbarLayout(); } catch(Exception e) { e.printStackTrace(); } } } public boolean isDocked() { return docked; } public void setWasDocked(boolean wasDocked) { this.wasDocked = wasDocked; } public boolean wasDocked() { return wasDocked; } public void setOrientation(int orientation) { this.orientation = orientation; } public int getOrientation() { return orientation; } public void setHorizontalSpacing(int spacing) { hgap = spacing; } public void setVerticalSpacing(int spacing) { vgap = spacing; } public void setDragHandleColor(Color color) { dragButton.setBackground(color); } public int getToolbarHeight () { if (docked) return thePanel.getHeight (); return this.getHeight (); } public int getToolbarWidth () { if (docked) return thePanel.getWidth (); return this.getWidth (); } public void addButton (AbstractButton newButton) { buttonStorage.addElement (newButton); } public void addButton (AbstractButton newButton, int position) { buttonStorage.insertElementAt (newButton, position); } public void removeButtonAt (int position) { buttonStorage.removeElementAt (position); } public void insertSeperator(int index) { buttonStorage.insertElementAt(new JLabel(), index); } /** * Mouse listening methods */ public void mouseReleased(MouseEvent e) { stopDragHandleEvent(e); } public void mousePressed(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseDragged(MouseEvent e) { processDragHandleEvent(e); } public void mouseMoved(MouseEvent e) { } private void processDragHandleEvent(MouseEvent me) { // uncomment the following line and replace 'yourFrame' with the name of your // frame..... // yourFrame.processHandleEvent(me, this, docked); } private void stopDragHandleEvent(MouseEvent me) { // uncomment the following line and replace 'yourFrame' with the name of your // frame..... // yourFrame.stopHandleEvent(me, this, docked); } } /** * This code should be copied into the frame where you are adding the toolbar. * It may need a little fiddling. TOP, LEFT, BOTTOM, RIGHT are constants which * set the active margin at which the toolbars will dock to the sides. I'm * sure that there's a better way to do this, but I just ripped the code out * of what we're doing. * * public void processHandleEvent (MouseEvent me, DWaveToolbar bar, boolean docked) { if(!docked) { Rectangle r = bar.getBounds (); int tempx = r.x + me.getX(); int tempy = r.y + me.getY(); bar.setLocation(tempx, tempy); if(tempy < TOP) { topDock.trackToolbar(me, bar); } else if (tempx < LEFT) { leftDock.trackToolbar(me, bar); } else if(tempy > BOTTOM - bar.getHeight()) { bottomDock.trackToolbar(me, bar); } else if (tempx > RIGHT - bar.getWidth()) { rightDock.trackToolbar(me, bar); } else bar.setDragHandleColor(Color.darkGray); } else bar.getOwner().undockToolbar(bar); bar.setOwner(topDock); bar.setDocked(false); } public void stopHandleEvent(MouseEvent me, DWaveToolbar bar, boolean docked) { Rectangle r = bar.getBounds (); int tempx = r.x + me.getX(); int tempy = r.y + me.getY(); if(tempy < TOP) { this.getContentPane().remove(topDock); topDock.dockToolbar(bar); bar.setDocked(true); bar.setOwner(topDock); this.getContentPane().add("North", topDock); } else if (tempx < LEFT) { this.getContentPane().remove(leftDock); leftDock.dockToolbar(bar); bar.setDocked(true); this.getContentPane().add("West", leftDock); } else if(tempy > BOTTOM - bar.getHeight()) { this.getContentPane().remove(bottomDock); bottomDock.dockToolbar(bar); bar.setDocked(true); this.getContentPane().add("South", bottomDock); } else if (tempx > RIGHT - bar.getWidth()) { this.getContentPane().remove(rightDock); rightDock.dockToolbar(bar); bar.setDocked(true); this.getContentPane().add("East", rightDock); } } * * */