001/** 002 * Copyright (C) 2014 Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/ 003 * 004 * This file is part of Dicoogle/dicoogle. 005 * 006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * Dicoogle/dicoogle is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with Dicoogle. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package pt.ua.dicoogle.rGUI.client.UIHelper; 020 021import java.awt.event.ActionEvent; 022import java.io.ByteArrayInputStream; 023import java.util.ArrayList; 024import java.util.List; 025import javax.swing.JComponent; 026import javax.swing.JPanel; 027import javax.swing.JTextField; 028import org.dom4j.Document; 029import org.dom4j.DocumentException; 030import org.dom4j.Element; 031import org.dom4j.io.SAXReader; 032 033/** 034 * 035 * @author Carlos Ferreira 036 */ 037@Deprecated 038public class PanelBuiltFromXML extends JPanel 039{ 040 private List<ComponentFromXML> components; 041 042 public PanelBuiltFromXML(byte[] xml) 043 { 044 SAXReader saxReader = new SAXReader(); 045 046 Document document = null; 047 try 048 { 049 document = saxReader.read(new ByteArrayInputStream(xml)); 050 } catch (DocumentException ex) 051 { 052 ex.printStackTrace(System.out); 053 } 054 055 Element root = document.getRootElement(); 056 if (root.getName().compareTo("jpanel") != 0) 057 { 058 return; 059 } 060 List<Element> elements = root.elements("component"); 061 this.components = new ArrayList<ComponentFromXML>(); 062 063 for (Element e : elements) 064 { 065 ComponentFromXML newComponent = new ComponentFromXML(e); 066 if (newComponent.isValidComponent()) 067 { 068 this.components.add(newComponent); 069 } 070 071 } 072 } 073 074 private class ComponentFromXML extends JComponent 075 { 076 077 private String name; 078 private JComponent component; 079 private boolean valid = false; 080 081 public ComponentFromXML(Element element) 082 { 083 Element tmp = element.element("name"); 084 if (tmp.getText() == null) 085 { 086 return; 087 } 088 this.name = tmp.getText(); 089 090 tmp = element.element("type"); 091 if (tmp.getText() == null) 092 { 093 return; 094 } 095 096 if (tmp.getText().compareTo("Integer") == 0) 097 { 098 int minimum = Integer.MIN_VALUE; 099 int maximum = Integer.MAX_VALUE; 100 tmp = element.element("min"); 101 if (tmp.getText() != null) 102 { 103 minimum = Integer.parseInt(tmp.getText()); 104 } 105 tmp = element.element("max"); 106 if (tmp.getText() != null) 107 { 108 maximum = Integer.parseInt(tmp.getText()); 109 } 110 111 JTextField newcomponent = new JTextField(); 112 113 newcomponent.addActionListener(new IntegerListener(minimum, maximum, newcomponent)); 114 115 this.component = newcomponent; 116 } 117 if (tmp.getText().compareTo("Enum") == 0) 118 { 119 List<Element> tmpelems = tmp.elements("constant"); 120 if ((tmpelems == null) || tmpelems.isEmpty()) 121 { 122 return; 123 } 124 String strings[] = new String[tmpelems.size()]; 125 int i = 0; 126 for (Element el : tmpelems) 127 { 128 strings[i] = el.getText(); 129 i++; 130 } 131 } 132 133 this.valid = true; 134 } 135 136 public boolean isValidComponent() 137 { 138 return this.valid; 139 } 140 } 141 142 private class IntegerListener implements java.awt.event.ActionListener 143 { 144 145 private int minimum = Integer.MIN_VALUE; 146 private int maximum = Integer.MAX_VALUE; 147 private JTextField component; 148 149 public IntegerListener(int minimum, int maximum, JTextField component) 150 { 151 if (minimum < maximum) 152 { 153 this.minimum = minimum; 154 this.maximum = maximum; 155 } 156 this.component = component; 157 } 158 159 @Override 160 public void actionPerformed(ActionEvent e) 161 { 162 String text = this.component.getText(); 163 int i = Integer.parseInt(text); 164 165 if (i < this.minimum) 166 { 167 this.component.setText(Integer.toString(this.minimum)); 168 } 169 if (i > this.maximum) 170 { 171 this.component.setText(Integer.toString(this.maximum)); 172 } 173 this.component.setText(Integer.toString(i)); 174 } 175 } 176}