001: /*
002: * Created on 14/03/2004
003: *
004: * Swing Components - visit http://sf.net/projects/gfd
005: *
006: * Copyright (C) 2004 Igor Regis da Silva Simões
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package br.com.gfp.dao;
024:
025: import java.awt.Cursor;
026: import java.beans.PropertyVetoException;
027: import java.lang.ref.WeakReference;
028: import java.util.HashMap;
029: import java.util.Map;
030:
031: import javax.swing.JDesktopPane;
032: import javax.swing.JFrame;
033: import javax.swing.JInternalFrame;
034:
035: import br.com.gfp.util.SimpleLog;
036: import br.com.gfp.windows.GFPInternalFrame;
037: import br.com.gfpshare.db.DAOListener;
038:
039: /**
040: * @author Igor Regis da Silva Simões
041: */
042: public class GFPController {
043: private static GFPController controller = null;
044:
045: private JFrame telaPrincipal = null;
046:
047: private JDesktopPane desktopPane = null;
048:
049: private final Cursor cursorEspera = new Cursor(Cursor.WAIT_CURSOR);
050:
051: private final Cursor cursorPadrao = new Cursor(
052: Cursor.DEFAULT_CURSOR);
053:
054: private JInternalFrame[] frames = null;
055:
056: private Contexto contexto = new Contexto();
057:
058: private JInternalFrame telaCriada;
059:
060: /**
061: * Chave que representa o Frame Principal da aplicação no contexto
062: */
063: public static final Integer FRAME_PRINCIPAL = Integer.valueOf("0");
064:
065: /**
066: * Chave que representa o Rodapé Messager da aplicação no contexto
067: */
068: public static final Integer RODAPE_MESSAGER = Integer.valueOf("1");
069:
070: /**
071: * Chave que representa o Rodapé Messager da aplicação no contexto
072: */
073: public static final Integer DESKTOP = Integer.valueOf("2");
074:
075: /**
076: * Chave que representa o Id do usuário da aplicação no contexto
077: */
078: public static final Integer USUARIO = Integer.valueOf("3");
079:
080: /**
081: * Chave que representa a view principal usada para criar views secundárias
082: */
083: public static final Integer ROOT_WINDOW = Integer.valueOf("4");
084:
085: /**
086: * Chave que representa a view usada para o help
087: */
088: public static final Integer HELP_WINDOW = Integer.valueOf("5");
089:
090: /**
091: * Chave que representa o objeto que escreve o log
092: */
093: public static final Integer LOG = Integer.valueOf("6");
094:
095: /**
096: * Creates a new instance of GFDMediator
097: */
098: private GFPController() {
099: //Não Faz nada
100: }
101:
102: /**
103: * @return Instancia unica de GFDController
104: */
105: public static GFPController getGFPController() {
106: if (controller == null)
107: controller = new GFPController();
108: return controller;
109: }
110:
111: /**
112: * @param tipoTela
113: */
114: public void create(final Class tipoTela) {
115: create(tipoTela, null);
116: }
117:
118: /**
119: * @param tipoTela
120: * @param params
121: */
122: public void create(final Class tipoTela, final Object[] params) {
123: create(tipoTela, params, true);
124: }
125:
126: /**
127: * @param tipoTela
128: * @param show
129: */
130: public void create(final Class tipoTela, final boolean show) {
131: create(tipoTela, null, show);
132: }
133:
134: /**
135: * @param tipoTela
136: * @param params
137: * @param show
138: */
139: @SuppressWarnings("unchecked")
140: public void create(final Class tipoTela, final Object[] params,
141: final boolean show) {
142: long i = System.currentTimeMillis();
143: if (telaPrincipal == null)
144: telaPrincipal = (JFrame) getContexto().get(FRAME_PRINCIPAL);
145: if (show) {
146: telaPrincipal.setCursor(cursorEspera);
147: }
148:
149: JInternalFrame tela = null;
150: if (desktopPane == null)
151: desktopPane = (JDesktopPane) getContexto().get(DESKTOP);
152: frames = desktopPane.getAllFrames();
153:
154: if (!tipoTela.getName().equals(
155: "br.com.gfp.windows.TransactionWindow")) {
156: for (int n = 0; n < frames.length; n++) {
157: if (frames[n].getClass().getName().equals(
158: tipoTela.getName()))
159: if (desktopPane.getComponent(n) instanceof JInternalFrame)
160: tela = (JInternalFrame) desktopPane
161: .getComponent(n);
162: }
163: }
164: if (tela == null) {
165: try {
166: if (params == null) {
167: try {
168: tela = (JInternalFrame) tipoTela
169: .getConstructor(
170: new Class[] { JFrame.class })
171: .newInstance(
172: new Object[] { telaPrincipal });
173: } catch (NoSuchMethodException e) {
174: tela = (JInternalFrame) tipoTela.newInstance();
175: }
176: } else {
177: Object[] parametros = new Object[params.length + 1];
178: for (int n = 1; n < parametros.length; n++) {
179: parametros[n] = params[n - 1];
180: }
181: parametros[0] = telaPrincipal;
182:
183: boolean erro = false;
184: Class[] classParametros = new Class[parametros.length];
185: //tentamos instanciar pelas interfaces se der erro tentamos pela classe mesmo
186: while (true) {
187: int j = 1;
188: for (; j < classParametros.length; j++) {
189: if (!erro) {
190: if (parametros[j].getClass()
191: .getInterfaces() != null
192: && parametros[j].getClass()
193: .getInterfaces().length > 0)
194: classParametros[j] = parametros[j]
195: .getClass().getInterfaces()[0];
196: } else {
197: classParametros[j] = parametros[j]
198: .getClass();
199: }
200: }
201: classParametros[0] = JFrame.class;
202: try {
203: tela = (JInternalFrame) tipoTela
204: .getConstructor(classParametros)
205: .newInstance(parametros);
206: break;
207: } catch (Exception nsme) {
208: if (erro)
209: break;
210: j = 1;
211: erro = true;
212: continue;
213: }
214: }
215:
216: }
217: } catch (Exception e) {
218: ((SimpleLog) GFPController.getGFPController()
219: .getContexto().get(GFPController.LOG))
220: .log("Erro ao abrir tela " + tipoTela.getName());
221: ((SimpleLog) GFPController.getGFPController()
222: .getContexto().get(GFPController.LOG)).log(e
223: .getLocalizedMessage());
224: ((SimpleLog) GFPController.getGFPController()
225: .getContexto().get(GFPController.LOG)).log(e);
226: }
227:
228: desktopPane.add(tela);
229: } else if (params != null && params.length > 0)
230: try {
231: for (int j = 0; j < params.length; j++)
232: if (params[j] instanceof DAOListener)
233: ((GFPInternalFrame) tela)
234: .setListener((DAOListener) params[0]);
235: } catch (Exception e) {
236: ((SimpleLog) GFPController.getGFPController()
237: .getContexto().get(GFPController.LOG))
238: .log("Erro ao setar controllerListener para tela "
239: + tipoTela.getName());
240: ((SimpleLog) GFPController.getGFPController()
241: .getContexto().get(GFPController.LOG))
242: .log("Listener " + params[1]);
243: ((SimpleLog) GFPController.getGFPController()
244: .getContexto().get(GFPController.LOG)).log(e
245: .getLocalizedMessage());
246: ((SimpleLog) GFPController.getGFPController()
247: .getContexto().get(GFPController.LOG)).log(e);
248: }
249:
250: tela.setVisible(show);
251:
252: try {
253: tela.setSelected(true);
254: if (tela.isMaximizable())
255: tela.setMaximum(true);
256: } catch (PropertyVetoException e) {
257: ((SimpleLog) GFPController.getGFPController().getContexto()
258: .get(GFPController.LOG))
259: .log("Erro ao maximizar tela " + tipoTela.getName());
260: ((SimpleLog) GFPController.getGFPController().getContexto()
261: .get(GFPController.LOG)).log(e
262: .getLocalizedMessage());
263: ((SimpleLog) GFPController.getGFPController().getContexto()
264: .get(GFPController.LOG)).log(e);
265: }
266: ((SimpleLog) GFPController.getGFPController().getContexto()
267: .get(GFPController.LOG)).log(tipoTela.getName()
268: + "---------------------------------> carregada em "
269: + (System.currentTimeMillis() - i) + " milesegundos");
270:
271: telaCriada = tela;
272:
273: if (show) {
274: telaPrincipal.setCursor(cursorPadrao);
275: }
276:
277: System.gc();
278: System.runFinalization();
279:
280: (new Thread() {
281: @Override
282: public void run() {
283: try {
284: Thread.sleep(2200);
285: } catch (InterruptedException e) {
286: ((SimpleLog) GFPController.getGFPController()
287: .getContexto().get(GFPController.LOG))
288: .log("Thread sleep interrompida");
289: ((SimpleLog) GFPController.getGFPController()
290: .getContexto().get(GFPController.LOG))
291: .log(e.getLocalizedMessage());
292: ((SimpleLog) GFPController.getGFPController()
293: .getContexto().get(GFPController.LOG))
294: .log(e);
295: } finally {
296: if (telaPrincipal != null) {
297: telaPrincipal.setCursor(cursorPadrao);
298: telaPrincipal.setCursor(cursorPadrao);
299: }
300: }
301: }
302: }).start();
303: }
304:
305: /**
306: * @param tela
307: */
308: public void removeTela(JInternalFrame tela) {
309: desktopPane.remove(tela);
310: desktopPane.repaint();
311: }
312:
313: /**
314: * @return Returns the contexto.
315: */
316: public Contexto getContexto() {
317: return this .contexto;
318: }
319:
320: public JInternalFrame getUltimaTelaCriada() {
321: return telaCriada;
322: }
323:
324: /**
325: * Classe que possui o contexto da aplicação
326: * @author Igor Regis da Silva Simoes
327: */
328: public class Contexto {
329: private Map contexto = new HashMap();
330:
331: /**
332: * @param key
333: * @return True se esta chave exitir no contexto
334: */
335: public boolean containsKey(Object key) {
336: return contexto.containsKey(key);
337: }
338:
339: /**
340: * @param key
341: * @return O Objeto correspondente a esta chave do contexto
342: */
343: public Object get(Object key) {
344: return contexto.get(key);
345: }
346:
347: /**
348: * @param arg0
349: * @param arg1
350: * @return O Objeto adicionado ao contexto
351: */
352: @SuppressWarnings("unchecked")
353: public Object put(Object arg0, Object arg1) {
354: return contexto.put(arg0, arg1);
355: }
356:
357: /**
358: * @param key
359: * @return O Objeto correspondente a esta chave do contexto
360: */
361: public Object getWeak(Object key) {
362: return contexto.get(key) == null ? null
363: : ((WeakReference) contexto.get(key)).get();
364: }
365:
366: /**
367: * @param arg0
368: * @param arg1
369: * @return O Objeto adicionado ao contexto
370: */
371: @SuppressWarnings("unchecked")
372: public Object putWeak(Object arg0, Object arg1) {
373: return contexto.put(arg0, new WeakReference(arg1));
374: }
375:
376: /**
377: * @param key
378: * @return O Objeto removido do contexto
379: */
380: public Object remove(Object key) {
381: return contexto.remove(key);
382: }
383: }
384: }
|