001: /*
002: * Created on 11/10/2004
003: *
004: * ============================================================================
005: * GNU Lesser General Public License
006: * ============================================================================
007: *
008: * Swing Components - visit http://sf.net/projects/gfd
009: *
010: * Copyright (C) 2004 Igor Regis da Silva Simões
011: *
012: * This library is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU Lesser General Public
014: * License as published by the Free Software Foundation; either
015: * version 2.1 of the License, or (at your option) any later version.
016: *
017: * This library is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
020: * Lesser General Public License for more details.
021: *
022: * You should have received a copy of the GNU Lesser General Public
023: * License along with this library; if not, write to the Free Software
024: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
025: */
026:
027: package br.com.gfpshare.beans;
028:
029: import java.awt.Container;
030: import java.beans.PropertyVetoException;
031:
032: import javax.swing.DefaultDesktopManager;
033: import javax.swing.JDesktopPane;
034: import javax.swing.JFrame;
035: import javax.swing.JInternalFrame;
036: import javax.swing.UIManager;
037: import javax.swing.plaf.basic.BasicInternalFrameUI;
038:
039: /**
040: * @author Igor Regis da Silva Simoes
041: */
042: public class DesktopManager extends DefaultDesktopManager {
043: private JDesktopPane desktop = null;
044:
045: private Container principal = null;
046:
047: /**
048: * @param desktop
049: */
050: public DesktopManager(JDesktopPane desktop) {
051: super ();
052: this .desktop = desktop;
053: }
054:
055: /**
056: * @see javax.swing.DesktopManager#maximizeFrame(javax.swing.JInternalFrame)
057: */
058: @Override
059: public void maximizeFrame(JInternalFrame f) {
060: super .maximizeFrame(f);
061: f.setBorder(null);
062: setBoundsForFrame(f, 0, -f.getInsets().top, f.getParent()
063: .getBounds().width, f.getParent().getBounds().height
064: + f.getInsets().top);
065:
066: /* TODO REMOVER
067: Component[] c = f.getComponents();
068: for (int i = 0; i < c.length; i++)
069: {
070: if (c[i].getClass().getName().indexOf("TitlePane") != -1)
071: {
072: ((JComponent) c[i]).setVisible(false);
073: }
074: }*/// O código abaixo parece ser mais eficiente
075: if (f.isVisible()) {
076: setNorthPaneVisible(f, false);
077: maximizadoAtivo(f);
078: }
079: }
080:
081: /**
082: * Seta a visibilidade do painel norte do internal frame.
083: * @param f
084: * @param visible
085: */
086: private void setNorthPaneVisible(JInternalFrame f, boolean visible) {
087: try {
088: ((BasicInternalFrameUI) f.getUI()).getNorthPane()
089: .setVisible(visible);
090: } catch (Exception e) {
091: e.printStackTrace();
092: }
093: }
094:
095: /**
096: * @see javax.swing.DesktopManager#activateFrame(javax.swing.JInternalFrame)
097: */
098: @Override
099: public void activateFrame(JInternalFrame f) {
100: super .activateFrame(f);
101: if (f.isVisible() && f.isMaximum()) {
102: f.setBorder(null);
103: setBoundsForFrame(f, 0, -f.getInsets().top, f.getParent()
104: .getBounds().width,
105: f.getParent().getBounds().height
106: + f.getInsets().top);
107:
108: /* TODO REMOVER
109: Component[] c = f.getComponents();
110: for (int i = 0; i < c.length; i++)
111: {
112: if (c[i].getClass().getName().indexOf("TitlePane") != -1)
113: {
114: ((JComponent) c[i]).setVisible(false);
115: }
116: }*/
117: setNorthPaneVisible(f, false);
118: maximizadoAtivo(f);
119: } else {
120: minimizadoAtivo();
121: }
122: }
123:
124: /**
125: * @see javax.swing.DesktopManager#closeFrame(javax.swing.JInternalFrame)
126: */
127: @Override
128: public void closeFrame(JInternalFrame f) {
129: super .closeFrame(f);
130: }
131:
132: /**
133: * @see javax.swing.DesktopManager#deiconifyFrame(javax.swing.JInternalFrame)
134: */
135: @Override
136: public void deiconifyFrame(JInternalFrame f) {
137: f.setClosable(true);
138: super .deiconifyFrame(f);
139: if (f.isVisible() && f.isMaximum()) {
140: maximizadoAtivo(f);
141: }
142: }
143:
144: /**
145: * @see javax.swing.DesktopManager#iconifyFrame(javax.swing.JInternalFrame)
146: */
147: @Override
148: public void iconifyFrame(JInternalFrame f) {
149: f.setClosable(false);
150: super .iconifyFrame(f);
151: minimizadoAtivo();
152: }
153:
154: /**
155: * @see javax.swing.DesktopManager#minimizeFrame(javax.swing.JInternalFrame)
156: */
157: @Override
158: public void minimizeFrame(JInternalFrame f) {
159: super .minimizeFrame(f);
160: /* TODO REMOVER
161: Component[] c = f.getComponents();
162: for (int i = 0; i < c.length; i++)
163: {
164: if (c[i].getClass().getName().indexOf("TitlePane") != -1)
165: {
166: ((JComponent) c[i]).setVisible(true);
167: }
168: }
169: */
170: setNorthPaneVisible(f, true);
171: f.setBorder(UIManager.getBorder("InternalFrame.border"));
172: if (f.isVisible()) {
173: minimizadoAtivo();
174: }
175: }
176:
177: /**
178: * @return Retorna o frame principal da aplicacao
179: */
180: private JFrame getPrincipal() {
181: if (principal == null) {
182: principal = desktop.getParent();
183: while (!(principal instanceof JFrame)) {
184: principal = principal.getParent();
185: }
186: }
187: return (JFrame) principal;
188:
189: }
190:
191: /**
192: * @see javax.swing.DesktopManager#deactivateFrame(javax.swing.JInternalFrame)
193: */
194: @Override
195: public void deactivateFrame(JInternalFrame f) {
196: minimizadoAtivo();
197: if (!f.isVisible()) {
198: myActivateNextFrame(desktop);
199: }
200: }
201:
202: private void maximizadoAtivo(JInternalFrame f) {
203: ((IMenuBar) getPrincipal().getJMenuBar())
204: .setBotoesFrameVisible(true);
205: ((IMenuBar) getPrincipal().getJMenuBar()).setFrame(f);
206: getPrincipal().setTitle(
207: getPrincipal().getTitle() + " -- " + f.getTitle());
208: }
209:
210: private void minimizadoAtivo() {
211: ((IMenuBar) getPrincipal().getJMenuBar())
212: .setBotoesFrameVisible(false);
213: ((IMenuBar) getPrincipal().getJMenuBar()).setFrame(null);
214: if (getPrincipal().getTitle().indexOf(" --") > -1)
215: getPrincipal().setTitle(
216: getPrincipal().getTitle().substring(0,
217: getPrincipal().getTitle().indexOf(" --")));
218: }
219:
220: private void myActivateNextFrame(Container c) {
221: JInternalFrame nextFrame = null;
222: if (c == null)
223: return;
224: for (int i = 0; i < c.getComponentCount(); i++) {
225: if (c.getComponent(i) instanceof JInternalFrame
226: && ((JInternalFrame) c.getComponent(i)).isVisible()) {
227: nextFrame = (JInternalFrame) c.getComponent(i);
228: break;
229: }
230: }
231: if (nextFrame != null) {
232: try {
233: nextFrame.setSelected(true);
234: } catch (PropertyVetoException e2) {
235: //Nao fazemos nada
236: }
237: nextFrame.moveToFront();
238: }
239: }
240: }
|