View Javadoc
1 /***************************************************************************** 2 * Virtual Mockup for Machine Vision 3 * Copyright (C) 2001-2003 Fabio R. de Miranda, João E. Kogler Jr., 4 * Carlos S. Santos. 5 * Virtual Mockup for Machine Vision Project funded by SENAC-SP 6 * 7 * Permission is granted to redistribute and/or modify this 8 * software under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This software is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License (http://www.gnu.org/copyleft/lesser.html) 16 * for more details. 17 * 18 *****************************************************************************/ 19 20 package camera3d.gui; 21 22 import java.awt.*; 23 import javax.swing.*; 24 import java.awt.event.*; 25 26 27 /*** 28 * A subclass of JPanel whose content can be switched from this object to one external 29 * frame. 30 * @author Fábio Roberto de Miranda 31 */ 32 public class FrameSwitcherPane extends JPanel implements ActionListener { 33 private BorderLayout borderLayout1 = new BorderLayout(); 34 PanelCommandBar panelCommandBar = new PanelCommandBar(); 35 private boolean isInside = true; 36 JButton button; 37 JFrame frame; 38 Component centerComponent; 39 String title; 40 41 42 public void add(Component comp, Object constraints){ 43 if (constraints instanceof java.lang.String){ 44 if (((String)constraints).equals(BorderLayout.CENTER)){ 45 this.centerComponent = comp; 46 } 47 } 48 super.add(comp, constraints); 49 } 50 51 public FrameSwitcherPane() { 52 button = panelCommandBar.getInOrOutButton(); 53 button.addActionListener(this); 54 try { 55 jbInit(); 56 } 57 catch(Exception e) { 58 e.printStackTrace(); 59 } 60 } 61 62 /* This method is used by this class and others to find out whether the component 63 * once kept inside this container is still in it or in a JFrame outside 64 */ 65 public boolean isInside(){ 66 return this.isInside; 67 } 68 69 public void setInside(boolean inside){ 70 if(inside) this.changeToInside(); 71 else this.changeToOutside(); 72 this.isInside = inside; 73 } 74 75 private void changeToOutside(){ 76 if(this.isInside){ 77 // creates frame if it is null 78 if (frame == null){ 79 frame = new JFrame(); 80 frame.getContentPane().setLayout(borderLayout1); 81 frame.setTitle(title); 82 frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); 83 } 84 // removes pane command bar and center component from this object and 85 // add them to the external frame 86 this.remove(panelCommandBar); 87 this.remove(centerComponent); 88 frame.getContentPane().add(panelCommandBar, BorderLayout.NORTH); 89 frame.getContentPane().add(centerComponent, BorderLayout.CENTER); 90 int width = centerComponent.getWidth(); 91 int height = panelCommandBar.getHeight() + centerComponent.getHeight(); 92 frame.setSize(width, height); 93 frame.setVisible(true); 94 frame.show(); 95 96 button.setToolTipText("Dock this element in the main GUI"); 97 panelCommandBar.setIcon(-1); 98 this.getParent().repaint(); 99 100 this.isInside = false; 101 //this.setInside(false); 102 }else{ 103 // does nothing as we are already outside 104 } 105 } 106 107 private void changeToInside(){ 108 if(this.isInside){ 109 // does nothing as we are already inside 110 }else{ 111 frame.setVisible(false); 112 frame.remove(centerComponent); 113 frame.remove(panelCommandBar); 114 115 this.add(centerComponent, BorderLayout.CENTER); 116 this.add(panelCommandBar, BorderLayout.NORTH); 117 118 panelCommandBar.setIcon(+1); 119 button.setToolTipText("Remove this element from main GUI"); 120 this.getParent().repaint(); 121 122 this.isInside = true; 123 //this.setInside(true); 124 } 125 } 126 127 private void jbInit() throws Exception { 128 this.setLayout(borderLayout1); 129 this.add(panelCommandBar, BorderLayout.NORTH); 130 } 131 132 /*** 133 * Sets the location of the external frame. Only effective when the content is 134 * outside. 135 * @param x new x location of external frame. 136 * @param y new y location of external frame. 137 */ 138 public void setPosition(int x, int y){ 139 if (isInside()){ 140 // does nothing as it must be docked in main window 141 } else { 142 frame.setLocation(x, y); 143 } 144 } 145 146 /*** 147 * Returns the location of this object, when the content is inside, or returns 148 * the location of the external frame, when the content is outside. 149 */ 150 public java.awt.Point getPosition(){ 151 if (isInside()){ 152 return this.getLocation(); 153 } else { 154 return frame.getLocation(); 155 } 156 } 157 158 /*** 159 * Returns the dimension of this object, when the content is inside, or returns 160 * the dimension of the external frame, when the content is outside. 161 */ 162 public java.awt.Dimension getSize(){ 163 if (isInside()){ 164 return super.getSize(); 165 } else { 166 return frame.getSize(); 167 } 168 } 169 170 /*** 171 * Sets the size of this object when the content is inside or sets the size of 172 * the external frame, when the content is outside. 173 */ 174 public void setSize(int width, int height){ 175 if (isInside()){ 176 super.setSize(width,height); 177 } else { 178 frame.setSize(width, height); 179 frame.getContentPane().setSize(width,height-10); 180 } 181 } 182 183 /*** 184 * Sets the title of the external frame. 185 * @param title the frame title. 186 */ 187 public void setTitle(String title){ 188 this.title = title; 189 if (frame != null){ 190 frame.setTitle(title); 191 } 192 } 193 194 /*** 195 * Toggles the inside/outside state of the content in response to the pressing of 196 * the switching button.<BR> 197 * Method from the ActionListener interface. 198 */ 199 public void actionPerformed(ActionEvent event){ 200 if(event.getSource()==this.button){ 201 if (isInside()){ 202 changeToOutside(); 203 } else { 204 changeToInside(); 205 } 206 } 207 } 208 }

This page was automatically generated by Maven