View Javadoc
1 /* 2 * JScroll - the scrollable desktop pane for Java. 3 * Copyright (C) 2003 Tom Tessier 4 * 5 * This program is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU General Public License 7 * as published by the Free Software Foundation; either version 2 8 * of the License, or (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18 * 19 */ 20 21 package org.jscroll; 22 23 import org.jscroll.widgets.*; 24 25 import javax.swing.*; 26 27 import java.awt.BorderLayout; 28 29 30 /*** 31 * The main scrollable desktop class. 32 * <BR><BR> 33 * JScrollDesktopPane builds upon JDesktopPane and JScrollPane to provide 34 * a complete virtual desktop environment that enables easy access to internal 35 * frames that may have been positioned offscreen. This access is made 36 * possible via real-time creation and manipulation of the desktop preferred size. 37 * <BR><BR> 38 * A toolbar provides a set of buttons along the top of the screen, with each 39 * button matched to a corresponding internal frame. When one of these buttons 40 * is clicked, the associated frame is centered upon the virtual desktop and 41 * selected. The buttons within the toolbar automatically resize as more buttons 42 * are added beyond the width of the container. 43 * <BR><BR> 44 * A JMenuBar may be registered with the scrollable desktop so that the 45 * application can provide access to the internal frames via its own menu bar. 46 * When the registration is complete, a new JMenu entitled "Window" is added to 47 * the supplied JMenuBar, a menu containing <code>Tile</code>, 48 * <code>Cascade</code>, and <code>Close</code> options along with dynamically 49 * updated shortcuts to any internal frames currently upon the scrollable 50 * desktop. The <code>Tile</code> and <code>Cascade</code> options provided by 51 * the "Window" menu affect the positions of the internal frames upon the 52 * scrollable desktop. <code>Cascade</code> positions each internal frame one 53 * after the other in a diagonal sequence crosswise the screen, while 54 * <code>Tile</code> positions and resizes the internal frames to fill up 55 * all available screen real estate, with no single frame overlapping any other. 56 * <BR><BR> 57 * JScrollDesktopPane is simply a JPanel and as such may be added to any 58 * suitable JPanel container, such as a JFrame. The addition of new internal 59 * frames to the JScrollDesktopPane and the registration of menu bars for 60 * use by the scrollable desktop is relatively simple: The <code>add</code> 61 * method creates a new internal frame and returns a reference to the 62 * JInternalFrame instance that was created, while the 63 * <code>registerMenuBar</code> method registers the menubar for use by the 64 * scrollable desktop. A JMenuBar object may also be registered by passing it 65 * as a constructor parameter to the JScrollDesktopPane. 66 * <BR><BR> 67 * An example usage follows: 68 * <BR><BR> 69 * <code><pre> 70 * JFrame f = new JFrame("Scrollable Desktop"); 71 * f.setSize(300,300); 72 * // prepare the menuBar 73 * JMenuBar menuBar = new JMenuBar(); 74 * f.setJMenuBar(menuBar); 75 * 76 * // create the scrollable desktop instance and add it to the JFrame 77 * JScrollDesktopPane scrollableDesktop = 78 * new JScrollDesktopPane(menuBar); 79 * f.getContentPane().add(scrollableDesktop); 80 * f.setVisible(true); 81 * 82 * // add a frame to the scrollable desktop 83 * JPanel frameContents = new JPanel(); 84 * frameContents.add( 85 * new JLabel("Hello and welcome to JScrollDesktopPane.")); 86 * 87 * scrollableDesktop.add(frameContents); 88 * </pre></code> 89 * 90 * JScrollDesktopPane has been tested under Java 2 JDK versions 91 * 1.3.1-b24 on Linux and jdk1.3.0_02 on Windows and Intel Solaris. 92 * 93 * As of March 14, 2003 it has also been tested on JDK 1.4.1 for Windows. 94 * 95 * @author <a href="mailto:tessier@gabinternet.com">Tom Tessier</a> 96 * @version 1.0 12-Aug-2001 97 */ 98 public class JScrollDesktopPane extends JPanel implements DesktopConstants { 99 private static int count; // count used solely to name untitled frames 100 private DesktopMediator desktopMediator; 101 private ImageIcon defaultFrameIcon; 102 103 /*** 104 * creates the JScrollDesktopPane object, registers a menubar, and assigns 105 * a default internal frame icon. 106 * 107 * @param mb the menubar with which to register the scrollable desktop 108 * @param defaultFrameIcon the default icon to use within the title bar of 109 * internal frames. 110 */ 111 public JScrollDesktopPane(JMenuBar mb, ImageIcon defaultFrameIcon) { 112 this(); 113 registerMenuBar(mb); 114 this.defaultFrameIcon = defaultFrameIcon; 115 } 116 117 /*** 118 * creates the JScrollDesktopPane object and registers a menubar. 119 * 120 * @param mb the menubar with which to register the scrollable desktop 121 */ 122 public JScrollDesktopPane(JMenuBar mb) { 123 this(); 124 registerMenuBar(mb); 125 } 126 127 /*** 128 * creates the JScrollDesktopPane object. 129 */ 130 public JScrollDesktopPane() { 131 setLayout(new BorderLayout()); 132 desktopMediator = new DesktopMediator(this); 133 } 134 135 /*** 136 * adds an internal frame to the scrollable desktop 137 * 138 * @param frameContents the contents of the internal frame 139 * 140 * @return the JInternalFrame that was created 141 */ 142 public JInternalFrame add(JPanel frameContents) { 143 return add("Untitled " + count++, defaultFrameIcon, frameContents, 144 true, -1, -1); 145 } 146 147 /*** 148 * adds an internal frame to the scrollable desktop 149 * 150 * @param title the title displayed in the title bar of the internal frame 151 * @param frameContents the contents of the internal frame 152 * 153 * @return the JInternalFrame that was created 154 */ 155 public JInternalFrame add(String title, JPanel frameContents) { 156 return add(title, defaultFrameIcon, frameContents, true, -1, -1); 157 } 158 159 /*** 160 * adds an internal frame to the scrollable desktop 161 * 162 * @param title the title displayed in the title bar of the internal frame 163 * @param frameContents the contents of the internal frame 164 * @param isClosable <code>boolean</code> indicating whether internal frame 165 * is closable 166 * 167 * @return the JInternalFrame that was created 168 */ 169 public JInternalFrame add(String title, JPanel frameContents, 170 boolean isClosable) { 171 return add(title, defaultFrameIcon, frameContents, isClosable, -1, -1); 172 } 173 174 /*** 175 * adds an internal frame to the scrollable desktop 176 * 177 * @param title the title displayed in the title bar of the internal frame 178 * @param icon the icon displayed in the title bar of the internal frame 179 * @param frameContents the contents of the internal frame 180 * @param isClosable <code>boolean</code> indicating whether internal frame 181 * is closable 182 * 183 * @return the JInternalFrame that was created 184 */ 185 public JInternalFrame add(String title, ImageIcon icon, 186 JPanel frameContents, boolean isClosable) { 187 return add(title, icon, frameContents, isClosable, -1, -1); 188 } 189 190 /*** 191 * adds an internal frame to the scrollable desktop. 192 * <BR><BR> 193 * Propogates the call to DesktopMediator. 194 * 195 * @param title the title displayed in the title bar of the internal frame 196 * @param icon the icon displayed in the title bar of the internal frame 197 * @param frameContents the contents of the internal frame 198 * @param isClosable <code>boolean</code> indicating whether internal frame 199 * is closable 200 * @param x x coordinates of internal frame within the scrollable desktop. 201 * @param y y coordinates of internal frame within the scrollable desktop 202 * 203 * @return the JInternalFrame that was created 204 */ 205 public JInternalFrame add(String title, ImageIcon icon, 206 JPanel frameContents, boolean isClosable, int x, int y) { 207 return desktopMediator.add(title, icon, frameContents, isClosable, x, y); 208 } 209 210 /*** 211 * adds a JInternalFrame to the scrollable desktop. 212 * 213 * @param f the internal frame of class JScrollInternalFrame to add 214 */ 215 public void add(JInternalFrame f) { 216 add(getWrappedFrame(f), -1, -1); 217 } 218 219 /*** 220 * adds a JInternalFrame to the scrollable desktop. 221 * 222 * @param f the internal frame of class JScrollInternalFrame to add 223 * @param x x coordinates of internal frame within the scrollable desktop. 224 * @param y y coordinates of internal frame within the scrollable desktop 225 */ 226 public void add(JInternalFrame f, int x, int y) { 227 desktopMediator.add(getWrappedFrame(f), x, y); 228 } 229 230 /*** 231 * wraps a given internal frame in a JScrollInternalFrame for use 232 * by JScrollDesktopPane 233 * 234 * @param f the JInternalFrame reference 235 * 236 * @return 237 */ 238 private JScrollInternalFrame getWrappedFrame(JInternalFrame f) { 239 // wrap it in a JScrollInternalFrame 240 JScrollInternalFrame b = new JScrollInternalFrame(); 241 b.setContentPane(f.getContentPane()); 242 b.setTitle(f.getTitle()); 243 b.setResizable(f.isResizable()); 244 b.setClosable(f.isClosable()); 245 b.setMaximizable(f.isMaximizable()); 246 b.setIconifiable(f.isIconifiable()); 247 b.setFrameIcon(f.getFrameIcon()); 248 b.pack(); 249 b.saveSize(); 250 251 b.setVisible(f.isVisible()); 252 253 return b; 254 } 255 256 /*** 257 * removes the specified internal frame from the scrollable desktop 258 * 259 * @param f the internal frame to remove 260 */ 261 public void remove(JInternalFrame f) { 262 f.doDefaultCloseAction(); 263 } 264 265 /*** 266 * registers a menubar to which the "Window" menu may be applied. 267 * <BR><BR> 268 * Propogates the call to DesktopMediator. 269 * 270 * @param mb the menubar to register 271 */ 272 public void registerMenuBar(JMenuBar mb) { 273 desktopMediator.registerMenuBar(mb); 274 } 275 276 /*** 277 * registers a default icon for display in the title bars of 278 * internal frames 279 * 280 * @param defaultFrameIcon the default icon 281 */ 282 public void registerDefaultFrameIcon(ImageIcon defaultFrameIcon) { 283 this.defaultFrameIcon = defaultFrameIcon; 284 } 285 286 /*** 287 * returns the internal frame currently selected upon the 288 * virtual desktop. 289 * <BR><BR> 290 * Propogates the call to DesktopMediator. 291 * 292 * @return a reference to the active JInternalFrame 293 */ 294 public JInternalFrame getSelectedFrame() { 295 return desktopMediator.getSelectedFrame(); 296 } 297 298 /*** 299 * selects the specified internal frame upon the virtual desktop. 300 * <BR><BR> 301 * Propogates the call to DesktopMediator. 302 * 303 * @param f the internal frame to select 304 */ 305 public void setSelectedFrame(JInternalFrame f) { 306 desktopMediator.setSelectedFrame(f); 307 } 308 309 /*** 310 * flags the specified internal frame as "contents changed." Used to 311 * notify the user when the contents of an inactive internal frame 312 * have changed. 313 * <BR><BR> 314 * Propogates the call to DesktopMediator. 315 * 316 * @param f the internal frame to flag as "contents changed" 317 */ 318 public void flagContentsChanged(JInternalFrame f) { 319 desktopMediator.flagContentsChanged(f); 320 } 321 }

This page was automatically generated by Maven