001: /*
002: * Created on 30/01/2005 Swing Components - visit http://sf.net/projects/gfd Copyright (C) 2004 Igor Regis da
003: * Silva Simões This program is free software; you can redistribute it and/or modify it under the terms of the
004: * GNU General Public License as published by the Free Software Foundation; either version 2 of the License,
005: * or (at your option) any later version. This program is distributed in the hope that it will be useful, but
006: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
007: * PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU
008: * General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59
009: * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
010: */
011:
012: package br.com.igor.config;
013:
014: import java.awt.BorderLayout;
015: import java.awt.Dimension;
016: import java.awt.GridBagConstraints;
017: import java.awt.GridBagLayout;
018: import java.awt.Insets;
019: import java.awt.event.ActionEvent;
020: import java.awt.event.ActionListener;
021: import java.io.IOException;
022: import java.net.URL;
023: import java.util.Enumeration;
024: import java.util.HashMap;
025: import java.util.Map;
026:
027: import javax.swing.ButtonGroup;
028: import javax.swing.Icon;
029: import javax.swing.JButton;
030: import javax.swing.JComponent;
031: import javax.swing.JInternalFrame;
032: import javax.swing.JOptionPane;
033: import javax.swing.JPanel;
034: import javax.swing.JToggleButton;
035: import javax.xml.parsers.DocumentBuilderFactory;
036: import javax.xml.parsers.ParserConfigurationException;
037:
038: import org.w3c.dom.DOMException;
039: import org.w3c.dom.Document;
040: import org.w3c.dom.NamedNodeMap;
041: import org.w3c.dom.Node;
042: import org.w3c.dom.NodeList;
043: import org.xml.sax.SAXException;
044:
045: import br.com.igor.beans.aplicativos.IconeFactory;
046: import br.com.igor.controllers.ArquivoDeDadosAbertoContoller;
047: import br.com.igor.plugin.core.DynamicClassLoader;
048:
049: import com.l2fprod.common.swing.JButtonBar;
050: import com.l2fprod.common.swing.JOutlookBar;
051:
052: /**
053: * @author Igor Regis da Silva Simoes
054: */
055: public class TelaConfiguracao extends JInternalFrame {
056: private final JPanel configuracoesMain = new JPanel();
057:
058: private final PanelCreator mainAction = new PanelCreator();
059:
060: private final Map paineisConfigCache = new HashMap();
061:
062: private Map buttonGroup = new HashMap();
063:
064: private String xmlFile = null;
065:
066: /**
067: * This is the default constructor
068: * @param xmlFile Localizacao do arquivo xml com as configurações
069: */
070: public TelaConfiguracao(String xmlFile) {
071: super ();
072: this .xmlFile = xmlFile;
073: (new Thread() {
074: @Override
075: public void run() {
076: initialize();
077: }
078: }).start();
079: }
080:
081: /**
082: * This method initializes this
083: */
084: private void initialize() {
085: this .setResizable(true);
086: this .setClosable(true);
087: this .setMaximizable(true);
088: this .setIconifiable(true);
089: this .setSize(410, 270);
090: this .getContentPane().setLayout(new BorderLayout());
091:
092: //Painel com botões salvar, restaurar.
093: JPanel botoes = new JPanel();
094: botoes.setLayout(new GridBagLayout());
095:
096: JButton aplicar = new JButton(
097: TelaConfiguracaoMessages.getMessages().getString(
098: "aplicar"), IconeFactory.getIconeFactory().getImageIcon(IconeFactory.SALVAR_PEQUENA)); //$NON-NLS-1$
099: aplicar.setToolTipText(TelaConfiguracaoMessages.getMessages()
100: .getString("AplicarToolTip"));
101: aplicar.setMnemonic(TelaConfiguracaoMessages.getMessages()
102: .getString("AplicarMnemonic").charAt(0));
103: aplicar.addActionListener(new ActionListener() {
104: public void actionPerformed(ActionEvent event) {
105: if (configuracoesMain.getComponentCount() > 0) {
106: ((ConfigPanel) configuracoesMain.getComponent(0))
107: .saveData();
108: }
109: }
110: });
111: botoes.add(aplicar, new GridBagConstraints(0, 0, 1, 1, 1, 0,
112: GridBagConstraints.EAST, GridBagConstraints.NONE,
113: new Insets(5, 5, 5, 5), 0, 0));
114:
115: JButton restaurar = new JButton(
116: TelaConfiguracaoMessages.getMessages().getString(
117: "restaurar"), IconeFactory.getIconeFactory().getImageIcon("tamanho=pequena;/icones/32x32/undo.png")); //$NON-NLS-1$ //$NON-NLS-2$
118: restaurar.setToolTipText(TelaConfiguracaoMessages.getMessages()
119: .getString("RestaurarToolTip"));
120: restaurar.setMnemonic(TelaConfiguracaoMessages.getMessages()
121: .getString("RestaurarMnemonic").charAt(0));
122: restaurar.addActionListener(new ActionListener() {
123: public void actionPerformed(ActionEvent event) {
124: if (configuracoesMain.getComponentCount() > 0) {
125: ((ConfigPanel) configuracoesMain.getComponent(0))
126: .loadData();
127: }
128: }
129: });
130: botoes.add(restaurar, new GridBagConstraints(1, 0, 1, 1, 0, 0,
131: GridBagConstraints.EAST, GridBagConstraints.NONE,
132: new Insets(5, 5, 5, 5), 0, 0));
133:
134: this .getContentPane().add(botoes, BorderLayout.SOUTH);
135:
136: //Painel de dados
137: configuracoesMain.setLayout(new BorderLayout());
138: this .getContentPane().add(configuracoesMain,
139: BorderLayout.CENTER);
140:
141: //Coluna de opções
142: JOutlookBar outLookBar = new JOutlookBar();
143: outLookBar.setMinimumSize(new Dimension(140, 300));
144: outLookBar.setPreferredSize(new Dimension(140, 300));
145: this .getContentPane().add(outLookBar, BorderLayout.WEST);
146:
147: Enumeration enu = null;
148: try {
149: enu = DynamicClassLoader.getClassLoader().getResources(
150: xmlFile);
151: } catch (IOException e) {
152: // TODO Auto-generated catch block
153: e.printStackTrace();
154: }
155: while (enu.hasMoreElements()) //for each xml file...
156: {
157: DocumentBuilderFactory factory = DocumentBuilderFactory
158: .newInstance();
159: factory.setValidating(true);
160: factory.setNamespaceAware(true);
161:
162: URL url = (URL) enu.nextElement();
163:
164: Document document = null;
165: try {
166: document = factory.newDocumentBuilder().parse(
167: url.openStream(), url.toString());//We load/parse the file
168: } catch (ParserConfigurationException e1) {
169: // TODO Auto-generated catch block
170: e1.printStackTrace();
171: } catch (SAXException e) {
172: // TODO Auto-generated catch block
173: e.printStackTrace();
174: } catch (IOException e) {
175: // TODO Auto-generated catch block
176: e.printStackTrace();
177: }
178:
179: NodeList configBarItem = document.getLastChild()
180: .getChildNodes();//get the menus definition inside this file
181:
182: for (int i = 0; i < configBarItem.getLength(); i++)//for each config bar entry
183: {
184: Node n = null;
185: if ((n = configBarItem.item(i)).getNodeName().equals(
186: "Group"))//Se for um Grupo //$NON-NLS-1$
187: {
188: ButtonGroup grupo = null;
189: JButtonBar bbGrupo = null;
190:
191: for (int j = 0; j < outLookBar.getTabCount(); j++)//Vejamos se este grupo já existe
192: {
193: if (outLookBar
194: .getTitleAt(j)
195: .equals(
196: TelaConfiguracaoMessages
197: .getMessages()
198: .getString(
199: n
200: .getAttributes()
201: .getNamedItem(
202: "Name").getNodeValue()))) //$NON-NLS-1$
203: {//Se este grupo exite então nós vamos pegar e trabalhar com ele
204: bbGrupo = (JButtonBar) outLookBar
205: .getComponentAt(j);
206: grupo = (ButtonGroup) buttonGroup
207: .get(bbGrupo);
208: }
209: }
210: if (bbGrupo == null)//Se o grupo não existe nós vamos criá-lo
211: {
212: bbGrupo = new JButtonBar();
213: bbGrupo.setOrientation(JButtonBar.VERTICAL);
214: Node iconeNome = null;
215: if ((iconeNome = n.getAttributes()
216: .getNamedItem("Icon")) == null) {//Sem icone
217: outLookBar
218: .addTab(
219: TelaConfiguracaoMessages
220: .getMessages()
221: .getString(
222: n
223: .getAttributes()
224: .getNamedItem(
225: "Name").getNodeValue()), bbGrupo); //$NON-NLS-1$
226: } else {//Com icone
227: Icon icone = IconeFactory.getIconeFactory()
228: .getImageIcon(
229: iconeNome.getNodeValue());
230: outLookBar
231: .addTab(
232: TelaConfiguracaoMessages
233: .getMessages()
234: .getString(
235: n
236: .getAttributes()
237: .getNamedItem(
238: "Name").getNodeValue()), icone, bbGrupo); //$NON-NLS-1$
239: }
240:
241: grupo = new ButtonGroup();
242: buttonGroup.put(bbGrupo, grupo);
243: }
244:
245: NodeList toggleButtons = n.getChildNodes();//Dentro de um Grupo existem os JToggleButtons (Property)
246: for (int j = 0; j < toggleButtons.getLength(); j++) {
247: if (!toggleButtons.item(j).getNodeName()
248: .equals("Property")) //$NON-NLS-1$
249: {
250: continue;
251: }
252: //Para cada Property montamos o JToggleButton correspondente
253: NamedNodeMap atributos = toggleButtons.item(j)
254: .getAttributes();
255: JToggleButton toggleButton = new JToggleButton(
256: TelaConfiguracaoMessages
257: .getMessages()
258: .getString(
259: atributos
260: .getNamedItem(
261: "Name").getNodeValue())); //$NON-NLS-1$
262: if (atributos.getNamedItem("ToolTip") != null)
263: toggleButton
264: .setToolTipText(TelaConfiguracaoMessages
265: .getMessages()
266: .getString(
267: atributos
268: .getNamedItem(
269: "ToolTip")
270: .getNodeValue()));
271: toggleButton.setActionCommand(atributos
272: .getNamedItem("Action").getNodeValue()); //$NON-NLS-1$
273: if (ArquivoDeDadosAbertoContoller.getInstance(
274: null).getNomeArquivo() == null) {
275: try {
276: ((ConfigPanel) DynamicClassLoader
277: .getClassLoader()
278: .loadClass(
279: atributos.getNamedItem(
280: "Action")
281: .getNodeValue())
282: .newInstance()).loadData();
283: } catch (DOMException e2) {
284: // TODO Auto-generated catch block
285: e2.printStackTrace();
286: } catch (InstantiationException e2) {
287: // TODO Auto-generated catch block
288: e2.printStackTrace();
289: } catch (IllegalAccessException e2) {
290: // TODO Auto-generated catch block
291: e2.printStackTrace();
292: } catch (ClassNotFoundException e2) {
293: // TODO Auto-generated catch block
294: e2.printStackTrace();
295: }
296: }
297: toggleButton.addActionListener(mainAction);
298: grupo.add(toggleButton);
299: bbGrupo.add(toggleButton);
300: }
301: } else if ((n = configBarItem.item(i)).getNodeName()
302: .equals("Default"))//Configuramos aqui o painel que ira abrir por default //$NON-NLS-1$
303: {
304: NamedNodeMap atributos = n.getAttributes();
305: JButtonBar grupo = null;
306: for (int j = 0; j < outLookBar.getTabCount(); j++)//Selecionamos o grupo a que pertence o painel
307: {
308: if (outLookBar
309: .getTitleAt(j)
310: .equals(
311: atributos
312: .getNamedItem("Group").getNodeValue())) //$NON-NLS-1$
313: {
314: outLookBar.setSelectedIndex(j);
315: grupo = (JButtonBar) outLookBar
316: .getComponentAt(j);
317: }
318: }
319: if (grupo != null)
320: for (int j = 0; j < grupo.getComponentCount(); j++)//Selecionamos o JToggleButton a que pertence o painel
321: {
322: if (((JToggleButton) grupo.getComponent(j))
323: .getText()
324: .equals(
325: TelaConfiguracaoMessages
326: .getMessages()
327: .getString(
328: atributos
329: .getNamedItem(
330: "Property").getNodeValue()))) //$NON-NLS-1$
331: {
332: ((JToggleButton) grupo.getComponent(j))
333: .setSelected(true);
334: }
335: }
336: //Executamos a exibição do painel
337: mainAction
338: .actionPerformed(new ActionEvent(
339: this ,
340: -1,
341: atributos
342: .getNamedItem("Action").getNodeValue())); //$NON-NLS-1$
343: }
344: }
345: }
346: }
347:
348: private class PanelCreator implements ActionListener {
349: /**
350: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
351: */
352: public void actionPerformed(ActionEvent e) {
353: if (configuracoesMain.getComponentCount() > 0) {
354: if (((ConfigPanel) configuracoesMain.getComponent(0))
355: .isDataChanged()) {
356: int escolha = JOptionPane
357: .showOptionDialog(
358: TelaConfiguracao.this ,
359: TelaConfiguracaoMessages
360: .getMessages()
361: .getString(
362: "dadosAlteradosMessage"), //$NON-NLS-1$
363: TelaConfiguracaoMessages
364: .getMessages().getString(
365: "warning"), //$NON-NLS-1$
366: JOptionPane.YES_NO_CANCEL_OPTION,
367: JOptionPane.INFORMATION_MESSAGE,
368: null, new Object[] {
369: TelaConfiguracaoMessages
370: .getMessages()
371: .getString("sim"), //$NON-NLS-1$
372: TelaConfiguracaoMessages
373: .getMessages()
374: .getString("nao") //$NON-NLS-1$
375: }, TelaConfiguracaoMessages
376: .getMessages().getString(
377: "sim")); //$NON-NLS-1$
378: if (escolha == JOptionPane.YES_OPTION) {
379: //Salvamos as alterações nas propriedades deste painel
380: ((ConfigPanel) configuracoesMain
381: .getComponent(0)).saveData();
382: } else if (escolha == JOptionPane.NO_OPTION) {
383: //Carregamos devolta as propriedades previamente salvas
384: ((ConfigPanel) configuracoesMain
385: .getComponent(0)).loadData();
386: }
387: }
388: }
389:
390: if (e.getActionCommand() == null)
391: return;
392: configuracoesMain.removeAll();
393: JComponent painel = null;
394: if ((painel = (JComponent) paineisConfigCache.get(e
395: .getActionCommand())) == null) {
396: try {
397: painel = (ConfigPanel) DynamicClassLoader
398: .getClassLoader().loadClass(
399: e.getActionCommand()).newInstance();
400: } catch (InstantiationException e1) {
401: // TODO Auto-generated catch block
402: e1.printStackTrace();
403: } catch (IllegalAccessException e1) {
404: // TODO Auto-generated catch block
405: e1.printStackTrace();
406: } catch (ClassNotFoundException e1) {
407: // TODO Auto-generated catch block
408: e1.printStackTrace();
409: }
410: if (painel != null)
411: paineisConfigCache
412: .put(e.getActionCommand(), painel);
413: }
414: painel.setVisible(false);
415: configuracoesMain.add(painel);
416: configuracoesMain.repaint();
417: painel.setVisible(true);
418: }
419: }
420:
421: }
|