KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > editors > report > ReportMultiPanel


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

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 /**
32  * Base class that components wishing to acts as report panels containing
33  * more than one query item should extend.
34  *
35  * @author Matthew Large
36  * @version $Revision: 1.2 $
37  *
38  */

39 public abstract class ReportMultiPanel extends JPanel implements LayoutManager, ActionListener {
40
41     /**
42      * Report query.
43      */

44     protected ReportQuery m_reportQuery = null;
45     
46     /**
47      * Label for panel.
48      */

49     private JLabel m_mainLabel = null;
50     
51     /**
52      * Panel for border line.
53      */

54     private JPanel m_borderPanel = null;
55     
56     /**
57      * Title for panel.
58      */

59     private String JavaDoc m_sTitle = null;
60     
61     /**
62      * List of remove {@link JButton} objects.
63      */

64     private ArrayList m_aValueComponentButtons = new ArrayList();
65     
66     /**
67      * Height of panel.
68      */

69     private int m_nHeight = 30;
70     
71     /**
72      * Maximum number of panels
73      */

74     private int m_nMaxPanels = -1;
75     
76     /**
77      * Add button.
78      */

79     private JButton m_addButton = null;
80
81     /**
82      * Constructs a new report multi panel.
83      *
84      * @param query Report query
85      * @param sTitle Title
86      */

87     public ReportMultiPanel(ReportQuery query, String JavaDoc sTitle) {
88         super();
89         this.m_reportQuery = query;
90         this.m_sTitle = sTitle;
91         this.setup();
92     }
93     
94     /**
95      * Configures this component.
96      *
97      */

98     private void setup() {
99         this.setLayout(this);
100
101         String JavaDoc 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     /* (non-Javadoc)
121      * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
122      */

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     /**
168      * Adds a value component to this multi panel.
169      *
170      * @param valueComponent Value component to add
171      */

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     /* (non-Javadoc)
186      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
187      */

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     /* (non-Javadoc)
228      * @see java.awt.Component#getPreferredSize()
229      */

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     /**
238      *
239      */

240     private ReportMultiPanel() {
241         super();
242     }
243
244     /**
245      * @param arg0
246      */

247     private ReportMultiPanel(boolean arg0) {
248         super(arg0);
249     }
250
251     /**
252      * @param arg0
253      */

254     private ReportMultiPanel(LayoutManager arg0) {
255         super(arg0);
256     }
257
258     /**
259      * @param arg0
260      * @param arg1
261      */

262     private ReportMultiPanel(LayoutManager arg0, boolean arg1) {
263         super(arg0, arg1);
264     }
265
266     /* (non-Javadoc)
267      * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
268      */

269     public void removeLayoutComponent(Component arg0) {
270     }
271
272     /* (non-Javadoc)
273      * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
274      */

275     public void addLayoutComponent(String JavaDoc arg0, Component arg1) {
276     }
277
278     /* (non-Javadoc)
279      * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
280      */

281     public Dimension minimumLayoutSize(Container arg0) {
282         return this.getPreferredSize();
283     }
284
285     /* (non-Javadoc)
286      * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
287      */

288     public Dimension preferredLayoutSize(Container arg0) {
289         return this.getPreferredSize();
290     }
291     
292     /**
293      * Returns a new value component applicable to this multi panel
294      * implementation.
295      *
296      * @return Value component
297      */

298     public abstract JPanel getNewValueComponent();
299     
300     /**
301      * Removes a value component specific to this multi panel
302      * implementation.
303      *
304      * @param valueComponent Value component
305      */

306     public abstract void removeValueComponent(JPanel valueComponent);
307     
308     /**
309      * Returns the number of components.
310      * @see java.awt.Container#getComponentCount()
311      */

312     public abstract int getValueComponentCount();
313     
314     /**
315      * Sets the number of maximum panels allowed
316      * @param max -1 for no max
317      */

318     public void setAllowedPanelMax(int max){
319         this.m_nMaxPanels = max;
320     }
321     
322     /**
323      * Wraps up a value component and a remove button.
324      *
325      * @author Matthew Large
326      * @version $Revision: 1.2 $
327      *
328      */

329     private class ComponentsWrapper {
330         
331         /**
332          * Remove button.
333          */

334         private JButton m_button = null;
335         
336         /**
337          * Value component.
338          */

339         private JPanel m_panel = null;
340         
341         /**
342          * Constructs a new components wrapper.
343          *
344          * @param valueComponent Value component
345          * @param button Remove button
346          */

347         public ComponentsWrapper(JPanel valueComponent, JButton button) {
348             super();
349             this.m_button = button;
350             this.m_panel = valueComponent;
351         }
352         
353         /**
354          * Returns the remove button.
355          *
356          * @return Remove button
357          */

358         public JButton getButton() {
359             return this.m_button;
360         }
361         
362         /**
363          * Returns the value component.
364          *
365          * @return Value component
366          */

367         public JPanel getPanel() {
368             return this.m_panel;
369         }
370     }
371
372 }
373
Popular Tags