1 19 package org.openharmonise.him.editors.report; 20 21 import java.awt.*; 22 import java.awt.event.*; 23 import java.util.*; 24 25 import javax.swing.*; 26 27 import org.openharmonise.him.editors.report.rqom.*; 28 import org.openharmonise.vfs.gui.*; 29 30 31 39 public abstract class ReportMultiPanel extends JPanel implements LayoutManager, ActionListener { 40 41 44 protected ReportQuery m_reportQuery = null; 45 46 49 private JLabel m_mainLabel = null; 50 51 54 private JPanel m_borderPanel = null; 55 56 59 private String m_sTitle = null; 60 61 64 private ArrayList m_aValueComponentButtons = new ArrayList(); 65 66 69 private int m_nHeight = 30; 70 71 74 private int m_nMaxPanels = -1; 75 76 79 private JButton m_addButton = null; 80 81 87 public ReportMultiPanel(ReportQuery query, String sTitle) { 88 super(); 89 this.m_reportQuery = query; 90 this.m_sTitle = sTitle; 91 this.setup(); 92 } 93 94 98 private void setup() { 99 this.setLayout(this); 100 101 String fontName = "Dialog"; 102 int fontSize = 11; 103 Font font = new Font(fontName, Font.PLAIN, fontSize); 104 105 this.m_mainLabel = new JLabel(this.m_sTitle); 106 this.m_mainLabel.setFont(font); 107 this.add(m_mainLabel); 108 109 this.m_borderPanel = new JPanel(); 110 this.m_borderPanel.setBorder( BorderFactory.createLineBorder(Color.BLACK)); 111 this.add(this.m_borderPanel); 112 113 this.m_addButton = new JButton(); 114 this.m_addButton.setIcon(IconManager.getInstance().getIcon("16-command-value-plus.gif")); 115 this.m_addButton.setActionCommand("ADD"); 116 this.m_addButton.addActionListener(this); 117 this.m_addButton.setMargin( new Insets(2,2,2,2) ); 118 } 119 120 123 public void layoutContainer(Container arg0) { 124 int nHeight = 10; 125 int nWidth = 520; 126 int nPreviousHeight = 25; 127 128 this.m_mainLabel.setSize(this.m_mainLabel.getPreferredSize()); 129 this.m_mainLabel.setLocation(10, nHeight); 130 nHeight = nHeight + this.m_mainLabel.getSize().height + 5; 131 132 this.m_borderPanel.setSize(700, 1); 133 this.m_borderPanel.setLocation(10 + this.m_mainLabel.getSize().width + 5, this.m_mainLabel.getLocation().y + this.m_mainLabel.getSize().height/2); 134 135 Iterator itor = this.m_aValueComponentButtons.iterator(); 136 while (itor.hasNext()) { 137 ComponentsWrapper wrapper = (ComponentsWrapper) itor.next(); 138 JButton removeButton = wrapper.getButton(); 139 JPanel valueComponent = wrapper.getPanel(); 140 valueComponent.setSize(valueComponent.getPreferredSize()); 141 valueComponent.setLocation(100, nHeight); 142 removeButton.setSize(20,20); 143 removeButton.setLocation(valueComponent.getLocation().x + valueComponent.getSize().width + 20, nHeight+15); 144 nWidth = valueComponent.getLocation().x + valueComponent.getSize().width + 20; 145 nPreviousHeight = nHeight; 146 nHeight += valueComponent.getPreferredSize().getHeight() + 10; 147 } 148 149 if(this.m_aValueComponentButtons.size()==0) { 150 nHeight = nHeight+5; 151 } 152 if(m_nMaxPanels<0 || m_nMaxPanels > getValueComponentCount()) { 153 this.add(this.m_addButton); 154 this.m_addButton.setSize(20, 20); 155 this.m_addButton.setLocation(nWidth + 30, nPreviousHeight+15); 156 } else { 157 this.remove(m_addButton); 158 } 159 160 if(nHeight<50) { 161 this.m_nHeight = 70; 162 } else { 163 this.m_nHeight = nHeight; 164 } 165 } 166 167 172 public void addValueComponent(JPanel valueComponent) { 173 this.add(valueComponent); 174 175 JButton removeButton = new JButton(); 176 removeButton.setIcon(IconManager.getInstance().getIcon("16-command-value-minus.gif")); 177 removeButton.setActionCommand("REMOVE"); 178 removeButton.addActionListener(this); 179 removeButton.setMargin( new Insets(2,2,2,2) ); 180 this.add(removeButton); 181 m_nHeight += valueComponent.getPreferredSize().getHeight() + 10; 182 this.m_aValueComponentButtons.add( new ComponentsWrapper(valueComponent, removeButton)); 183 } 184 185 188 public void actionPerformed(ActionEvent ae) { 189 if(ae.getActionCommand().equals("REMOVE")) { 190 ComponentsWrapper wrapper = null; 191 Iterator itor = this.m_aValueComponentButtons.iterator(); 192 while (itor.hasNext()) { 193 ComponentsWrapper wrapperTemp = (ComponentsWrapper) itor.next(); 194 if(wrapperTemp.getButton()==ae.getSource()) { 195 wrapper = wrapperTemp; 196 break; 197 } 198 } 199 if(wrapper!=null) { 200 this.m_aValueComponentButtons.remove(wrapper); 201 this.remove(wrapper.getPanel()); 202 this.remove(wrapper.getButton()); 203 this.removeValueComponent(wrapper.getPanel()); 204 this.layoutContainer(null); 205 this.revalidate(); 206 this.repaint(); 207 } 208 } else if(ae.getActionCommand().equals("ADD")) { 209 this.addValueComponent(this.getNewValueComponent()); 210 this.layoutContainer(null); 211 this.revalidate(); 212 this.repaint(); 213 } 214 215 Component comp = this.getParent(); 216 comp.validate(); 217 while (!(comp instanceof ReportPanel)) { 218 if (comp != null) { 219 comp = comp.getParent(); 220 } 221 } 222 if (comp != null) { 223 comp.validate(); 224 } 225 } 226 227 230 public Dimension getPreferredSize() { 231 if(m_nHeight<50) { 232 this.m_nHeight = 70; 233 } 234 return new Dimension(700, this.m_nHeight); 235 } 236 237 240 private ReportMultiPanel() { 241 super(); 242 } 243 244 247 private ReportMultiPanel(boolean arg0) { 248 super(arg0); 249 } 250 251 254 private ReportMultiPanel(LayoutManager arg0) { 255 super(arg0); 256 } 257 258 262 private ReportMultiPanel(LayoutManager arg0, boolean arg1) { 263 super(arg0, arg1); 264 } 265 266 269 public void removeLayoutComponent(Component arg0) { 270 } 271 272 275 public void addLayoutComponent(String arg0, Component arg1) { 276 } 277 278 281 public Dimension minimumLayoutSize(Container arg0) { 282 return this.getPreferredSize(); 283 } 284 285 288 public Dimension preferredLayoutSize(Container arg0) { 289 return this.getPreferredSize(); 290 } 291 292 298 public abstract JPanel getNewValueComponent(); 299 300 306 public abstract void removeValueComponent(JPanel valueComponent); 307 308 312 public abstract int getValueComponentCount(); 313 314 318 public void setAllowedPanelMax(int max){ 319 this.m_nMaxPanels = max; 320 } 321 322 329 private class ComponentsWrapper { 330 331 334 private JButton m_button = null; 335 336 339 private JPanel m_panel = null; 340 341 347 public ComponentsWrapper(JPanel valueComponent, JButton button) { 348 super(); 349 this.m_button = button; 350 this.m_panel = valueComponent; 351 } 352 353 358 public JButton getButton() { 359 return this.m_button; 360 } 361 362 367 public JPanel getPanel() { 368 return this.m_panel; 369 } 370 } 371 372 } 373 | Popular Tags |