001: /*
002: * Created on 08/02/2005
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: package br.com.gfpshare.config;
023:
024: import java.beans.PropertyChangeEvent;
025: import java.beans.PropertyChangeListener;
026: import java.sql.SQLException;
027:
028: import javax.swing.JOptionPane;
029:
030: import br.com.gfpshare.controllers.PropriedadesController;
031: import br.com.gfpshare.plugin.core.DynamicClassLoader;
032:
033: import com.l2fprod.common.beans.BaseBeanInfo;
034: import com.l2fprod.common.propertysheet.Property;
035: import com.l2fprod.common.propertysheet.PropertySheet;
036: import com.l2fprod.common.propertysheet.PropertySheetPanel;
037:
038: /**
039: * @author Igor Regis da Silva Simoes
040: */
041: public abstract class ConfigPanel extends PropertySheetPanel {
042: private final PropriedadesController controller = new PropriedadesController();
043:
044: protected GrupoDePropriedades grupoDePropriedades = null;
045:
046: protected boolean dataChanged = false;
047:
048: /**
049: * @param configType
050: *
051: */
052: public ConfigPanel(Class configType) {
053: setMode(PropertySheet.VIEW_AS_CATEGORIES);
054: setDescriptionVisible(true);
055: try {
056: setProperties(((BaseBeanInfo) DynamicClassLoader
057: .getClassLoader()
058: .loadClass(configType.getName() + "BeanInfo").newInstance()).getPropertyDescriptors()); //$NON-NLS-1$
059: } catch (InstantiationException e2) {
060: // TODO Auto-generated catch block
061: e2.printStackTrace();
062: } catch (IllegalAccessException e2) {
063: // TODO Auto-generated catch block
064: e2.printStackTrace();
065: } catch (ClassNotFoundException e2) {
066: // TODO Auto-generated catch block
067: e2.printStackTrace();
068: }
069:
070: grupoDePropriedades = (GrupoDePropriedades) Propriedades
071: .loadPropiedades(configType);
072:
073: loadData();
074:
075: addPropertySheetChangeListener(new PropertyChangeListener() {
076: public void propertyChange(PropertyChangeEvent evt) {
077: if (evt.getNewValue() == evt.getOldValue()
078: || (evt.getNewValue() != null
079: && evt.getOldValue() != null && evt
080: .getNewValue()
081: .equals(evt.getOldValue()))) {
082: return;
083: }
084: ((Property) evt.getSource())
085: .writeToObject(grupoDePropriedades);
086: dataChanged = true;
087: if (!isDadosValidos())
088: JOptionPane
089: .showMessageDialog(
090: ConfigPanel.this ,
091: ConfigPanelMessages
092: .getMessages()
093: .getString("dadosInvalidos"), ConfigPanelMessages.getMessages().getString("gfp"), JOptionPane.WARNING_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$
094: ConfigPanel.this .readFromObject(grupoDePropriedades);
095: }
096: });
097: }
098:
099: /**
100: * Inicia um grupo de propriedades com os valores padrão
101: * @return GrupoDePropriedades ja com os valores inicializados
102: */
103: public abstract GrupoDePropriedades loadDefaultProperties();
104:
105: /**
106: * Valida os dados do grupo de configuração realizando
107: * as operações necessárias para a validação
108: */
109: public abstract boolean isDadosValidos();
110:
111: /**
112: * Indica se as orpriedades deste painel foram alteradas.
113: * @return boolean
114: */
115: public boolean isDataChanged() {
116: return dataChanged;
117: }
118:
119: /**
120: * Salva as aterações feitas nas propriedades deste painel
121: */
122: public void saveData() {
123: try {
124: controller.atualizar(grupoDePropriedades
125: .getPersistentObject());
126: } catch (SQLException e) {
127: // TODO Auto-generated catch block
128: e.printStackTrace();
129: }
130: dataChanged = false;
131: }
132:
133: /**
134: * Carrega os dados das propriedades editadas por este painel
135: */
136: public void loadData() {
137: try {
138: controller.getBy(grupoDePropriedades.getPersistentObject());
139: } catch (SQLException e) {
140: grupoDePropriedades = loadDefaultProperties();
141: try {
142: controller.adicionarNovo(grupoDePropriedades
143: .getPersistentObject());
144: } catch (SQLException e1) {
145: // TODO Auto-generated catch block
146: e1.printStackTrace();
147: }
148: }
149: readFromObject(grupoDePropriedades);
150: dataChanged = false;
151: }
152: }
|