001: //==============================================================================
002: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
003: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
004: //=== and United Nations Environment Programme (UNEP)
005: //===
006: //=== This program 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 2 of the License, or (at
009: //=== your option) any later version.
010: //===
011: //=== This program is distributed in the hope that it will be useful, but
012: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
013: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: //=== General Public License for more details.
015: //===
016: //=== You should have received a copy of the GNU General Public License
017: //=== along with this program; if not, write to the Free Software
018: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: geonetwork@osgeo.org
022: //==============================================================================
023:
024: package org.fao.gast.gui.panels;
025:
026: import java.awt.Color;
027: import java.awt.FlowLayout;
028: import java.awt.Font;
029: import java.awt.event.ActionEvent;
030: import java.awt.event.ActionListener;
031: import java.util.List;
032: import java.util.StringTokenizer;
033: import javax.swing.BorderFactory;
034: import javax.swing.Icon;
035: import javax.swing.JButton;
036: import javax.swing.JComponent;
037: import javax.swing.JLabel;
038: import javax.swing.JPanel;
039: import javax.swing.JTextArea;
040: import org.dlib.gui.FlexLayout;
041: import org.dlib.gui.TSeparator;
042: import org.dlib.tools.Util;
043:
044: //==============================================================================
045:
046: public abstract class FormPanel extends JPanel implements
047: ActionListener {
048: //---------------------------------------------------------------------------
049: //---
050: //--- Constructor
051: //---
052: //---------------------------------------------------------------------------
053:
054: public FormPanel() {
055: FlexLayout fl = new FlexLayout(1, 6, 0, 0);
056: fl.setColProp(0, FlexLayout.EXPAND);
057: fl.setNullGaps(0, 0);
058:
059: setLayout(fl);
060: }
061:
062: //---------------------------------------------------------------------------
063: //---
064: //--- API methods
065: //---
066: //---------------------------------------------------------------------------
067:
068: public void init(String title, String descr, List<JButton> buttons,
069: Icon precon, String preconTip) {
070: labTitle.setText(title);
071: labIcon.setIcon(precon);
072: labIcon.setToolTipText(preconTip);
073: txaDescr.setText("\n" + formatLines(descr));
074:
075: JComponent innerPanel = buildInnerPanel();
076:
077: add("0,0,x", buildTitlePanel());
078: add("0,1,x", buildDescrPanel());
079: add("0,2,x", new TSeparator(TSeparator.HORIZONTAL));
080:
081: if (innerPanel != null) {
082: add("0,3,x", innerPanel);
083: add("0,4,x", new TSeparator(TSeparator.HORIZONTAL));
084: }
085:
086: add("0,5,x", buildButtons(buttons));
087: }
088:
089: //---------------------------------------------------------------------------
090: //---
091: //--- ActionListener
092: //---
093: //---------------------------------------------------------------------------
094:
095: public void actionPerformed(ActionEvent e) {
096: }
097:
098: //---------------------------------------------------------------------------
099: //---
100: //--- Protected methods
101: //---
102: //---------------------------------------------------------------------------
103:
104: protected abstract JComponent buildInnerPanel();
105:
106: //---------------------------------------------------------------------------
107: //---
108: //--- Private methods
109: //---
110: //---------------------------------------------------------------------------
111:
112: private JComponent buildTitlePanel() {
113: FlexLayout fl = new FlexLayout(2, 1);
114: fl.setColProp(0, FlexLayout.EXPAND);
115:
116: JPanel p = new JPanel();
117: p.setLayout(fl);
118: p.setBackground(Color.LIGHT_GRAY);
119: p.setBorder(BorderFactory.createLineBorder(Color.BLACK));
120:
121: p.add("0,0,x", labTitle);
122: p.add("1,0", labIcon);
123:
124: labTitle.setFont(new Font("Dialog", Font.BOLD, 20));
125:
126: return p;
127: }
128:
129: //---------------------------------------------------------------------------
130:
131: private JComponent buildDescrPanel() {
132: txaDescr.setEditable(false);
133: txaDescr.setBackground(new JPanel().getBackground());
134: txaDescr.setLineWrap(true);
135: txaDescr.setWrapStyleWord(true);
136:
137: return txaDescr;
138: }
139:
140: //---------------------------------------------------------------------------
141:
142: private String formatLines(String line) {
143: line = Util.replaceStr(line, "\r\n", "\n");
144:
145: StringTokenizer st = new StringTokenizer(line.trim(), "\n");
146: StringBuffer sb = new StringBuffer();
147:
148: while (st.hasMoreTokens())
149: sb.append(st.nextToken().trim() + " ");
150:
151: return sb.toString() + "\n";
152: }
153:
154: //---------------------------------------------------------------------------
155:
156: private JPanel buildButtons(List<JButton> buttons) {
157: this .buttons = buttons;
158:
159: JPanel panel = new JPanel();
160: panel.setLayout(new FlowLayout());
161:
162: for (JButton button : buttons)
163: panel.add(button);
164:
165: return panel;
166: }
167:
168: //---------------------------------------------------------------------------
169: //---
170: //--- Variables
171: //---
172: //---------------------------------------------------------------------------
173:
174: private JLabel labTitle = new JLabel();
175: private JLabel labIcon = new JLabel();
176: private JTextArea txaDescr = new JTextArea(1, 10);
177:
178: private List<JButton> buttons;
179: }
180:
181: //==============================================================================
|