001: /*
002: * Created on 09/03/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.Rectangle;
030:
031: import javax.swing.JDesktopPane;
032: import javax.swing.JInternalFrame;
033:
034: /**
035: * DesktopPane com a capacidade de organizar as janelas m seu interior
036: * @author Igor Regis da Silva Simões
037: */
038: public class IDesktopPane extends JDesktopPane {
039: /**
040: * Constante que indica uma organização personalizada das janelas do desktop
041: */
042: public static final int PERSONALIZADA = 0;
043:
044: /**
045: * Constante que indica uma organização vertical das janelas do desktop
046: */
047: public static final int VERTICAL = 1;
048:
049: /**
050: * Constante que indica uma organização horizontal das janelas do desktop
051: */
052: public static final int HORIZONTAL = 2;
053:
054: /**
055: * Constante que indica uma organização cascata das janelas do desktop
056: */
057: public static final int CASCATA = 3;
058:
059: /**
060: * Indica a forma como as janelas estão organizadas no desktop
061: */
062: private int organizacaoJanelas = 0;
063:
064: /**
065: * Cria uma nova instancia de IDesktopPane
066: */
067: public IDesktopPane() {
068: //Não fazemos nada
069: setDesktopManager(new DesktopManager(this ));
070: }
071:
072: /**
073: * Determina a forma como as janelas estão organizadas no desktop
074: * @param organizacaoJanelas indica a forma como as janelas estão organizadas no desktop
075: */
076: public void setOrganizacaoJanelas(int organizacaoJanelas) {
077: this .organizacaoJanelas = organizacaoJanelas;
078: organizaJanelas();
079: }
080:
081: /**
082: * Indica forma como as janelas estão organizadas no desktop
083: * @return int indicando a forma como as janelas estão organizadas no desktop
084: */
085: public int getOrganizacaoJanelas() {
086: return this .organizacaoJanelas;
087: }
088:
089: /**
090: * Realiza a organização das janelas
091: */
092: public void organizaJanelas() {
093: switch (organizacaoJanelas) {
094: case IDesktopPane.PERSONALIZADA:
095: break;
096: case IDesktopPane.VERTICAL:
097: organizaVertical();
098: break;
099: case IDesktopPane.HORIZONTAL:
100: organizaHorizontal();
101: break;
102: case IDesktopPane.CASCATA:
103: organizaCascata();
104: break;
105: }
106: }
107:
108: /**
109: * Organiza as janelas verticalmente
110: */
111: private void organizaVertical() {
112: //TODO Não implementado
113: }
114:
115: /**
116: * Organiza as janelas horizontalmente
117: */
118: private void organizaHorizontal() {
119: JInternalFrame[] frames = getAllFrames();
120: Rectangle dBounds = getBounds();
121:
122: int cols = (int) Math.sqrt(frames.length);
123: int rows = (int) (Math.ceil(((double) frames.length) / cols));
124: int lastRow = frames.length - cols * (rows - 1);
125: int width, height;
126:
127: if (lastRow == 0) {
128: rows--;
129: height = dBounds.height / rows;
130: } else {
131: height = dBounds.height / rows;
132: if (lastRow < cols) {
133: rows--;
134: width = dBounds.width / lastRow;
135: for (int i = 0; i < lastRow; i++) {
136: frames[cols * rows + i].setBounds(i * width, rows
137: * height, width, height);
138: }
139: }
140: }
141:
142: width = dBounds.width / cols;
143: for (int j = 0; j < rows; j++) {
144: for (int i = 0; i < cols; i++) {
145: frames[i + j * cols].setBounds(i * width, j * height,
146: width, height);
147: }
148: }
149: }
150:
151: /**
152: * Organiza as janelas em cascata
153: */
154: private void organizaCascata() {
155: JInternalFrame[] frames = getAllFrames();
156: Rectangle dBounds = getBounds();
157: int separation = 24;
158:
159: int margin = frames.length * separation + separation;
160: int width = dBounds.width - margin;
161: int height = dBounds.height - margin;
162: for (int i = 0; i < frames.length; i++) {
163: frames[i]
164: .setBounds(separation + dBounds.x + i * separation,
165: separation + dBounds.y + i * separation,
166: width, height);
167: }
168: }
169: }
|