001: /*
002: * SwingML Copyright (C) 2002 SwingML Team
003: *
004: * This library is free software; you can redistribute it and/or modify it under
005: * the terms of the GNU Lesser General Public License as published by the Free
006: * Software Foundation; either version 2 of the License, or (at your option) any
007: * later version.
008: *
009: * This library is distributed in the hope that it will be useful, but WITHOUT
010: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012: * details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with this library; if not, write to the Free Software Foundation, Inc.,
016: * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: * Authors: Ezequiel Cuellar <ecuellar@crosslogic.com>
019: *
020: */
021: package org.swingml.component;
022:
023: import java.awt.*;
024: import java.awt.event.*;
025:
026: import javax.swing.*;
027: import javax.swing.border.*;
028:
029: import org.swingml.*;
030: import org.swingml.event.*;
031: import org.swingml.icons.*;
032: import org.swingml.model.*;
033: import org.swingml.system.*;
034:
035: public class JPanelComponent extends JPanel {
036:
037: private JPanel bodyPanel;
038: private boolean collapsable;
039: JLabel collapseButton;
040: private boolean collapsed;
041: private EventHandler eventHandler = EventHandler.getInstance();
042:
043: public JPanelComponent(JPanelModel aModel) {
044: try {
045: // use a transparent panel as the body of this panel.
046: // if this panel is collapsable, then we hide this body panel when
047: // the user hits the "hide" button and show when they hit the "show"
048: // button.
049: setBodyPanel(new JPanel());
050: super .setLayout(new BorderLayout());
051: super .add(getBodyPanel(), BorderLayout.CENTER);
052:
053: String theName = aModel.getName();
054: String theBorder = aModel.getBorder();
055: String theLayout = aModel.getLayout();
056: String theLookAndFeel = aModel.getLookAndFeel();
057: String theTooltip = aModel.getTooltip();
058: String theTitle = aModel.getTitle();
059: int theBevelType = aModel.getBevelType();
060: int theRows = aModel.getRows();
061: int theCols = aModel.getCols();
062:
063: setName(theName);
064: getBodyPanel().setName(theName + "BodyPanel");
065: getBodyPanel().setToolTipText(theTooltip);
066:
067: if (theBorder.equalsIgnoreCase(Constants.ETCHEDBORDER)) {
068: if (theTitle == null) {
069: getBodyPanel().setBorder(new EtchedBorder());
070: } else {
071: getBodyPanel().setBorder(
072: new TitledBorder(new EtchedBorder(),
073: theTitle));
074: }
075: }
076: if (theBorder.equalsIgnoreCase(Constants.BEVELBORDER)) {
077: if (theTitle == null) {
078: getBodyPanel().setBorder(
079: new BevelBorder(theBevelType));
080: } else {
081: getBodyPanel().setBorder(
082: new TitledBorder(new BevelBorder(
083: theBevelType), theTitle));
084: }
085: }
086: if (theLayout.equalsIgnoreCase(Constants.FLOWLAYOUT)) {
087: getBodyPanel().setLayout(new FlowLayout());
088: }
089: if (theLayout.equalsIgnoreCase(Constants.GRIDLAYOUT)) {
090: getBodyPanel().setLayout(
091: new GridLayout(theRows, theCols));
092: }
093: if (theLayout.equalsIgnoreCase(Constants.BORDERLAYOUT)) {
094: getBodyPanel().setLayout(new BorderLayout());
095: }
096: if (theLayout.equalsIgnoreCase(Constants.NONE)) {
097: getBodyPanel().setLayout(null);
098: }
099: if (theLookAndFeel != null) {
100: if (theLookAndFeel.equalsIgnoreCase(Constants.METAL)) {
101: UIManager.setLookAndFeel(Constants.METAL_LAF);
102: }
103: if (theLookAndFeel.equalsIgnoreCase(Constants.WINDOWS)) {
104: UIManager.setLookAndFeel(Constants.WINDOWS_LAF);
105: }
106: if (theLookAndFeel.equalsIgnoreCase(Constants.MOTIF)) {
107: UIManager.setLookAndFeel(Constants.MOTIF_LAF);
108: }
109: if (theLookAndFeel.equalsIgnoreCase(Constants.EXTERNAL)) {
110: String theExternalLaf = aModel
111: .getExternalLookAndFeel();
112: if (theExternalLaf != null
113: && theExternalLaf.length() > 0) {
114: UIManager.setLookAndFeel(theExternalLaf);
115: }
116: }
117: }
118:
119: setCollapsable(aModel.isCollapsable());
120: if (isCollapsable()) {
121: addCollapseButton();
122: setCollapsed(aModel.isCollapsed());
123: }
124:
125: } catch (ClassNotFoundException e) {
126: SwingMLLogger.getInstance().log(e);
127: } catch (IllegalAccessException e) {
128: SwingMLLogger.getInstance().log(e);
129: } catch (UnsupportedLookAndFeelException e) {
130: SwingMLLogger.getInstance().log(e);
131: } catch (InstantiationException e) {
132: SwingMLLogger.getInstance().log(e);
133: }
134: }
135:
136: public Component add(Component comp) {
137: return getBodyPanel().add(comp);
138: }
139:
140: public Component add(Component comp, int index) {
141: return getBodyPanel().add(comp, index);
142: }
143:
144: public void add(Component comp, Object constraints) {
145: getBodyPanel().add(comp, constraints);
146: }
147:
148: public void add(Component comp, Object constraints, int index) {
149: getBodyPanel().add(comp, constraints, index);
150: }
151:
152: public synchronized void add(PopupMenu popup) {
153: getBodyPanel().add(popup);
154: }
155:
156: public Component add(String name, Component comp) {
157: return getBodyPanel().add(name, comp);
158: }
159:
160: /**
161: * Create the expand/collapse button for this collpsable panel.
162: */
163: private void addCollapseButton() {
164: setCollapseButton(new JLabel());
165: getCollapseButton()
166: .setBorder(BorderFactory.createEmptyBorder());
167: int width = 10;
168: int height = 10;
169: // set button size
170: getCollapseButton()
171: .setMinimumSize(new Dimension(width, height));
172: getCollapseButton()
173: .setMaximumSize(new Dimension(width, height));
174: getCollapseButton().setPreferredSize(
175: new Dimension(width, height));
176: getCollapseButton().addMouseListener(new MouseAdapter() {
177:
178: public void mouseClicked(MouseEvent e) {
179: setCollapsed(!isCollapsed());
180: }
181: });
182:
183: GridBagLayout layout = new GridBagLayout();
184: GridBagConstraints constraints = new GridBagConstraints();
185: JPanel buttonBar = new JPanel();
186: buttonBar.setLayout(layout);
187: constraints.fill = GridBagConstraints.BOTH;
188: constraints.weightx = .1;
189: constraints.weighty = 1.0;
190: constraints.gridx = 0;
191: constraints.gridy = 0;
192: layout.setConstraints(getCollapseButton(), constraints);
193: buttonBar.add(getCollapseButton());
194:
195: JPanel rightSide = new JPanel(new BorderLayout());
196: rightSide.add(new JLabel(" "), BorderLayout.CENTER);
197: constraints.fill = GridBagConstraints.HORIZONTAL;
198: constraints.weightx = .9;
199: constraints.weighty = 1.0;
200: constraints.gridx = 1;
201: constraints.gridy = 0;
202: layout.setConstraints(rightSide, constraints);
203: buttonBar.add(rightSide);
204:
205: super .add(buttonBar, BorderLayout.NORTH);
206: }
207:
208: public JPanel getBodyPanel() {
209: return bodyPanel;
210: }
211:
212: public JLabel getCollapseButton() {
213: return collapseButton;
214: }
215:
216: private boolean isCollapsable() {
217: return collapsable;
218: }
219:
220: private boolean isCollapsed() {
221: return collapsed;
222: }
223:
224: public void remove(Component comp) {
225: if (getBodyPanel() == comp) {
226: if (getParent() != null) {
227: getParent().remove(this );
228: }
229: } else {
230: getBodyPanel().remove(comp);
231: }
232: }
233:
234: public void remove(int index) {
235: getBodyPanel().remove(index);
236: }
237:
238: public synchronized void remove(MenuComponent popup) {
239: getBodyPanel().remove(popup);
240: }
241:
242: public void render(String aUrl, String aParent) {
243: Component theParentComponent = this .eventHandler
244: .findActionTarget(super .getTopLevelAncestor(), aParent);
245: RenderingThread theThread = new RenderingThread(aUrl,
246: (Container) theParentComponent);
247: theThread.start();
248: }
249:
250: private void setBodyPanel(JPanel bodyPanel) {
251: this .bodyPanel = bodyPanel;
252: }
253:
254: private void setCollapsable(boolean collapsable) {
255: this .collapsable = collapsable;
256: }
257:
258: public void setCollapseButton(JLabel collapseButton) {
259: this .collapseButton = collapseButton;
260: }
261:
262: private void setCollapsed(boolean collapsed) {
263: this .collapsed = collapsed;
264:
265: // update the icon and collapse/expand the panel as necessary
266: Icon icon;
267: if (isCollapsed()) {
268: icon = IconLoader.getIcon("plus.gif");
269: if (icon != null) {
270: getCollapseButton().setIcon(icon);
271: } else {
272: getCollapseButton().setIcon(null);
273: getCollapseButton().setText("+");
274: }
275: getBodyPanel().setVisible(false);
276: } else {
277: icon = IconLoader.getIcon("minus.gif");
278: if (icon != null) {
279: getCollapseButton().setIcon(icon);
280: } else {
281: getCollapseButton().setIcon(null);
282: getCollapseButton().setText("-");
283: }
284: getBodyPanel().setVisible(true);
285: }
286:
287: // NOTE: since we are updating button graphics, call updateUI to
288: // force faster refresh.
289: JPanelComponent.this .updateUI();
290: }
291:
292: public void showDocument(String aUrl, String aTarget) {
293: SwingMLRenderer theSwingmlRenderer = ((SwingMLRenderer) super
294: .getTopLevelAncestor());
295: theSwingmlRenderer.getAppletContext().showDocument(
296: URLHandler.handle(aUrl), aTarget);
297: }
298:
299: public void submit(String anAddress) {
300: SubmittingThread theThread = new SubmittingThread(anAddress,
301: this , this , true);
302: theThread.start();
303: }
304:
305: public void submit(String anAddress, String aTargetContainer,
306: boolean aRepaint) {
307: Component theTargetContainer = this .eventHandler
308: .findActionTarget(super .getTopLevelAncestor(),
309: aTargetContainer);
310: SubmittingThread theThread = new SubmittingThread(anAddress,
311: this , (Container) theTargetContainer, aRepaint);
312: theThread.start();
313: }
314:
315: }
|