001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.editors.report;
020:
021: import java.awt.*;
022: import java.awt.event.*;
023: import java.util.*;
024:
025: import javax.swing.*;
026:
027: import org.openharmonise.him.editors.report.rqom.*;
028: import org.openharmonise.vfs.gui.*;
029:
030: /**
031: * Base class that components wishing to acts as report panels containing
032: * more than one query item should extend.
033: *
034: * @author Matthew Large
035: * @version $Revision: 1.2 $
036: *
037: */
038: public abstract class ReportMultiPanel extends JPanel implements
039: LayoutManager, ActionListener {
040:
041: /**
042: * Report query.
043: */
044: protected ReportQuery m_reportQuery = null;
045:
046: /**
047: * Label for panel.
048: */
049: private JLabel m_mainLabel = null;
050:
051: /**
052: * Panel for border line.
053: */
054: private JPanel m_borderPanel = null;
055:
056: /**
057: * Title for panel.
058: */
059: private String m_sTitle = null;
060:
061: /**
062: * List of remove {@link JButton} objects.
063: */
064: private ArrayList m_aValueComponentButtons = new ArrayList();
065:
066: /**
067: * Height of panel.
068: */
069: private int m_nHeight = 30;
070:
071: /**
072: * Maximum number of panels
073: */
074: private int m_nMaxPanels = -1;
075:
076: /**
077: * Add button.
078: */
079: private JButton m_addButton = null;
080:
081: /**
082: * Constructs a new report multi panel.
083: *
084: * @param query Report query
085: * @param sTitle Title
086: */
087: public ReportMultiPanel(ReportQuery query, String sTitle) {
088: super ();
089: this .m_reportQuery = query;
090: this .m_sTitle = sTitle;
091: this .setup();
092: }
093:
094: /**
095: * Configures this component.
096: *
097: */
098: private void setup() {
099: 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
111: .createLineBorder(Color.BLACK));
112: this .add(this .m_borderPanel);
113:
114: this .m_addButton = new JButton();
115: this .m_addButton.setIcon(IconManager.getInstance().getIcon(
116: "16-command-value-plus.gif"));
117: this .m_addButton.setActionCommand("ADD");
118: this .m_addButton.addActionListener(this );
119: this .m_addButton.setMargin(new Insets(2, 2, 2, 2));
120: }
121:
122: /* (non-Javadoc)
123: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
124: */
125: public void layoutContainer(Container arg0) {
126: int nHeight = 10;
127: int nWidth = 520;
128: int nPreviousHeight = 25;
129:
130: this .m_mainLabel.setSize(this .m_mainLabel.getPreferredSize());
131: this .m_mainLabel.setLocation(10, nHeight);
132: nHeight = nHeight + this .m_mainLabel.getSize().height + 5;
133:
134: this .m_borderPanel.setSize(700, 1);
135: this .m_borderPanel.setLocation(
136: 10 + this .m_mainLabel.getSize().width + 5,
137: this .m_mainLabel.getLocation().y
138: + this .m_mainLabel.getSize().height / 2);
139:
140: Iterator itor = this .m_aValueComponentButtons.iterator();
141: while (itor.hasNext()) {
142: ComponentsWrapper wrapper = (ComponentsWrapper) itor.next();
143: JButton removeButton = wrapper.getButton();
144: JPanel valueComponent = wrapper.getPanel();
145: valueComponent.setSize(valueComponent.getPreferredSize());
146: valueComponent.setLocation(100, nHeight);
147: removeButton.setSize(20, 20);
148: removeButton
149: .setLocation(valueComponent.getLocation().x
150: + valueComponent.getSize().width + 20,
151: nHeight + 15);
152: nWidth = valueComponent.getLocation().x
153: + valueComponent.getSize().width + 20;
154: nPreviousHeight = nHeight;
155: nHeight += valueComponent.getPreferredSize().getHeight() + 10;
156: }
157:
158: if (this .m_aValueComponentButtons.size() == 0) {
159: nHeight = nHeight + 5;
160: }
161: if (m_nMaxPanels < 0 || m_nMaxPanels > getValueComponentCount()) {
162: this .add(this .m_addButton);
163: this .m_addButton.setSize(20, 20);
164: this .m_addButton.setLocation(nWidth + 30,
165: nPreviousHeight + 15);
166: } else {
167: this .remove(m_addButton);
168: }
169:
170: if (nHeight < 50) {
171: this .m_nHeight = 70;
172: } else {
173: this .m_nHeight = nHeight;
174: }
175: }
176:
177: /**
178: * Adds a value component to this multi panel.
179: *
180: * @param valueComponent Value component to add
181: */
182: public void addValueComponent(JPanel valueComponent) {
183: this .add(valueComponent);
184:
185: JButton removeButton = new JButton();
186: removeButton.setIcon(IconManager.getInstance().getIcon(
187: "16-command-value-minus.gif"));
188: removeButton.setActionCommand("REMOVE");
189: removeButton.addActionListener(this );
190: removeButton.setMargin(new Insets(2, 2, 2, 2));
191: this .add(removeButton);
192: m_nHeight += valueComponent.getPreferredSize().getHeight() + 10;
193: this .m_aValueComponentButtons.add(new ComponentsWrapper(
194: valueComponent, removeButton));
195: }
196:
197: /* (non-Javadoc)
198: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
199: */
200: public void actionPerformed(ActionEvent ae) {
201: if (ae.getActionCommand().equals("REMOVE")) {
202: ComponentsWrapper wrapper = null;
203: Iterator itor = this .m_aValueComponentButtons.iterator();
204: while (itor.hasNext()) {
205: ComponentsWrapper wrapperTemp = (ComponentsWrapper) itor
206: .next();
207: if (wrapperTemp.getButton() == ae.getSource()) {
208: wrapper = wrapperTemp;
209: break;
210: }
211: }
212: if (wrapper != null) {
213: this .m_aValueComponentButtons.remove(wrapper);
214: this .remove(wrapper.getPanel());
215: this .remove(wrapper.getButton());
216: this .removeValueComponent(wrapper.getPanel());
217: this .layoutContainer(null);
218: this .revalidate();
219: this .repaint();
220: }
221: } else if (ae.getActionCommand().equals("ADD")) {
222: this .addValueComponent(this .getNewValueComponent());
223: this .layoutContainer(null);
224: this .revalidate();
225: this .repaint();
226: }
227:
228: Component comp = this .getParent();
229: comp.validate();
230: while (!(comp instanceof ReportPanel)) {
231: if (comp != null) {
232: comp = comp.getParent();
233: }
234: }
235: if (comp != null) {
236: comp.validate();
237: }
238: }
239:
240: /* (non-Javadoc)
241: * @see java.awt.Component#getPreferredSize()
242: */
243: public Dimension getPreferredSize() {
244: if (m_nHeight < 50) {
245: this .m_nHeight = 70;
246: }
247: return new Dimension(700, this .m_nHeight);
248: }
249:
250: /**
251: *
252: */
253: private ReportMultiPanel() {
254: super ();
255: }
256:
257: /**
258: * @param arg0
259: */
260: private ReportMultiPanel(boolean arg0) {
261: super (arg0);
262: }
263:
264: /**
265: * @param arg0
266: */
267: private ReportMultiPanel(LayoutManager arg0) {
268: super (arg0);
269: }
270:
271: /**
272: * @param arg0
273: * @param arg1
274: */
275: private ReportMultiPanel(LayoutManager arg0, boolean arg1) {
276: super (arg0, arg1);
277: }
278:
279: /* (non-Javadoc)
280: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
281: */
282: public void removeLayoutComponent(Component arg0) {
283: }
284:
285: /* (non-Javadoc)
286: * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
287: */
288: public void addLayoutComponent(String arg0, Component arg1) {
289: }
290:
291: /* (non-Javadoc)
292: * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
293: */
294: public Dimension minimumLayoutSize(Container arg0) {
295: return this .getPreferredSize();
296: }
297:
298: /* (non-Javadoc)
299: * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
300: */
301: public Dimension preferredLayoutSize(Container arg0) {
302: return this .getPreferredSize();
303: }
304:
305: /**
306: * Returns a new value component applicable to this multi panel
307: * implementation.
308: *
309: * @return Value component
310: */
311: public abstract JPanel getNewValueComponent();
312:
313: /**
314: * Removes a value component specific to this multi panel
315: * implementation.
316: *
317: * @param valueComponent Value component
318: */
319: public abstract void removeValueComponent(JPanel valueComponent);
320:
321: /**
322: * Returns the number of components.
323: * @see java.awt.Container#getComponentCount()
324: */
325: public abstract int getValueComponentCount();
326:
327: /**
328: * Sets the number of maximum panels allowed
329: * @param max -1 for no max
330: */
331: public void setAllowedPanelMax(int max) {
332: this .m_nMaxPanels = max;
333: }
334:
335: /**
336: * Wraps up a value component and a remove button.
337: *
338: * @author Matthew Large
339: * @version $Revision: 1.2 $
340: *
341: */
342: private class ComponentsWrapper {
343:
344: /**
345: * Remove button.
346: */
347: private JButton m_button = null;
348:
349: /**
350: * Value component.
351: */
352: private JPanel m_panel = null;
353:
354: /**
355: * Constructs a new components wrapper.
356: *
357: * @param valueComponent Value component
358: * @param button Remove button
359: */
360: public ComponentsWrapper(JPanel valueComponent, JButton button) {
361: super ();
362: this .m_button = button;
363: this .m_panel = valueComponent;
364: }
365:
366: /**
367: * Returns the remove button.
368: *
369: * @return Remove button
370: */
371: public JButton getButton() {
372: return this .m_button;
373: }
374:
375: /**
376: * Returns the value component.
377: *
378: * @return Value component
379: */
380: public JPanel getPanel() {
381: return this.m_panel;
382: }
383: }
384:
385: }
|