001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU 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., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: ContentTitleBar.java,v 1.9 2005/12/04 13:46:04 jesper Exp $
023: package net.infonode.gui;
024:
025: import net.infonode.gui.hover.panel.HoverableShapedPanel;
026: import net.infonode.util.Alignment;
027: import net.infonode.util.Direction;
028:
029: import javax.swing.*;
030: import java.awt.*;
031:
032: /**
033: * @author johan
034: */
035: public class ContentTitleBar extends HoverableShapedPanel {
036: private ComponentPaintChecker repaintChecker;
037:
038: private class ComponentData {
039: private JComponent component;
040: private Insets insets;
041:
042: public ComponentData(JComponent component, Insets insets) {
043: this .component = component;
044: this .insets = insets;
045: }
046:
047: public JComponent getComponent() {
048: return component;
049: }
050:
051: public Insets getInsets() {
052: return insets;
053: }
054: }
055:
056: private JComponent[] leftTitleComponents;
057: private JComponent[] rightTitleComponents;
058: private Insets[] leftTitleComponentsInsets;
059: private Insets[] rightTitleComponentsInsets;
060:
061: private boolean flipTitleComponents = false;
062:
063: private GridBagConstraints constraints = new GridBagConstraints();
064:
065: private Insets labelInsets = InsetsUtil.EMPTY_INSETS;
066: private Alignment labelAlignment = Alignment.LEFT;
067:
068: private RotatableLabel label = new RotatableLabel("") {
069: public Dimension getMinimumSize() {
070: Dimension d = super .getMinimumSize();
071: if (getDirection().isHorizontal())
072: return new Dimension(0, d.height);
073:
074: return new Dimension(d.width, 0);
075: }
076: };
077:
078: public ContentTitleBar() {
079: this (null);
080: }
081:
082: public ContentTitleBar(Component hoveredComponent) {
083: super (new GridBagLayout(), null, hoveredComponent);
084: repaintChecker = new ComponentPaintChecker(this );
085: add(label);
086:
087: updateLayout();
088: }
089:
090: public JLabel getLabel() {
091: return label;
092: }
093:
094: public String getText() {
095: return label.getText();
096: }
097:
098: public Icon getIcon() {
099: return label.getIcon();
100: }
101:
102: public void setIcon(Icon icon) {
103: if (label.getIcon() != icon) {
104: label.setIcon(icon);
105:
106: doUpdate();
107: }
108: }
109:
110: public Alignment getLabelAlignment() {
111: return labelAlignment;
112: }
113:
114: public void setLabelAlignment(Alignment labelAlignment) {
115: if (this .labelAlignment != labelAlignment) {
116: this .labelAlignment = labelAlignment;
117: updateLabelAlignment();
118: }
119: }
120:
121: public void setLayoutDirection(Direction direction) {
122: if (label.getDirection() != direction) {
123: label.setDirection(direction);
124: updateLayout();
125: }
126: }
127:
128: public Insets getLabelInsets() {
129: return labelInsets;
130: }
131:
132: public void setLabelInsets(Insets labelInsets) {
133: this .labelInsets = labelInsets;
134:
135: GridBagConstraints c = ((GridBagLayout) getLayout())
136: .getConstraints(label);
137: c.insets = InsetsUtil.rotate(getDirection(), labelInsets);
138: ((GridBagLayout) getLayout()).setConstraints(label, c);
139:
140: doUpdate();
141: }
142:
143: public boolean isFlipTitleComponents() {
144: return flipTitleComponents;
145: }
146:
147: public void setFlipTitleComponents(boolean flipTitleComponents) {
148: if (this .flipTitleComponents != flipTitleComponents) {
149: this .flipTitleComponents = flipTitleComponents;
150: updateLayout();
151: }
152: }
153:
154: public JComponent[] getLeftTitleComponents() {
155: return leftTitleComponents;
156: }
157:
158: public void setLeftTitleComponents(JComponent[] leftTitleComponents) {
159: setLeftTitleComponents(leftTitleComponents,
160: leftTitleComponents == null ? null
161: : createEmptyInsets(leftTitleComponents.length));
162: }
163:
164: public void setLeftTitleComponents(
165: JComponent[] leftTitleComponents,
166: Insets[] leftTitleComponentsInsets) {
167: JComponent[] oldComponents = this .leftTitleComponents;
168: this .leftTitleComponents = leftTitleComponents;
169: this .leftTitleComponentsInsets = leftTitleComponentsInsets;
170: updateTitleComponents(oldComponents, leftTitleComponents);
171: }
172:
173: public JComponent[] getRightTitleComponents() {
174: return rightTitleComponents;
175: }
176:
177: public void setRightTitleComponents(
178: JComponent[] rightTitleComponents) {
179: setRightTitleComponents(
180: rightTitleComponents,
181: rightTitleComponents == null ? null
182: : createEmptyInsets(rightTitleComponents.length));
183: }
184:
185: public void setRightTitleComponents(
186: JComponent[] rightTitleComponents,
187: Insets[] rightTitleComponentsInsets) {
188: JComponent[] oldComponents = this .rightTitleComponents;
189: this .rightTitleComponents = rightTitleComponents;
190: this .rightTitleComponentsInsets = rightTitleComponentsInsets;
191: updateTitleComponents(oldComponents, rightTitleComponents);
192: }
193:
194: private Insets[] createEmptyInsets(int num) {
195: Insets[] insets = new Insets[num];
196: for (int i = 0; i < num; i++) {
197: insets[i] = InsetsUtil.EMPTY_INSETS;
198: }
199:
200: return insets;
201: }
202:
203: private void updateLabelAlignment() {
204: label
205: .setHorizontalAlignment(labelAlignment == Alignment.LEFT ? JLabel.LEFT
206: : labelAlignment == Alignment.RIGHT ? JLabel.RIGHT
207: : JLabel.CENTER);
208: }
209:
210: private void updateTitleComponents(JComponent[] oldComponents,
211: JComponent[] newComponents) {
212: if (oldComponents != null) {
213: for (int i = 0; i < oldComponents.length; i++)
214: remove(oldComponents[i]);
215: }
216:
217: if (newComponents != null) {
218: for (int i = 0; i < newComponents.length; i++)
219: add(newComponents[i]);
220: }
221:
222: updateLayout();
223: }
224:
225: private void updateLayout() {
226: Direction direction = label.getDirection();
227: constraints = new GridBagConstraints();
228:
229: JComponent[] leftComponents = flipTitleComponents ? rightTitleComponents
230: : leftTitleComponents;
231: JComponent[] rightComponents = flipTitleComponents ? leftTitleComponents
232: : rightTitleComponents;
233:
234: Insets[] leftInsets = flipTitleComponents ? rightTitleComponentsInsets
235: : leftTitleComponentsInsets;
236: Insets[] rightInsets = flipTitleComponents ? leftTitleComponentsInsets
237: : rightTitleComponentsInsets;
238:
239: if (direction == Direction.LEFT || direction == Direction.UP) {
240: JComponent[] tmpComponents = leftComponents;
241: leftComponents = rightComponents;
242: rightComponents = tmpComponents;
243:
244: Insets[] tmpInsets = leftInsets;
245: leftInsets = rightInsets;
246: rightInsets = tmpInsets;
247: }
248:
249: if (direction.isHorizontal()) {
250: int x = 0;
251:
252: if (leftComponents != null) {
253: for (int i = 0; i < leftComponents.length; i++) {
254: int index = direction == Direction.RIGHT ? i
255: : leftComponents.length - i - 1;
256: setConstraints(leftComponents[index],
257: leftInsets[index], x++, 0, 1, 1,
258: GridBagConstraints.NONE, 0, 0,
259: GridBagConstraints.CENTER);
260: }
261: }
262:
263: setConstraints(label, labelInsets, x++, 0, 1, 1,
264: GridBagConstraints.BOTH, 1, 1,
265: GridBagConstraints.CENTER);
266:
267: if (rightComponents != null) {
268: for (int i = 0; i < rightComponents.length; i++) {
269: int index = direction == Direction.RIGHT ? i
270: : rightComponents.length - i - 1;
271: setConstraints(rightComponents[index],
272: rightInsets[index], x++, 0, 1, 1,
273: GridBagConstraints.NONE, 0, 0,
274: GridBagConstraints.CENTER);
275: }
276: }
277: } else {
278: int y = 0;
279:
280: if (leftComponents != null) {
281: for (int i = 0; i < leftComponents.length; i++) {
282: int index = direction == Direction.DOWN ? i
283: : leftComponents.length - i - 1;
284: setConstraints(leftComponents[index],
285: leftInsets[index], 0, y++, 1, 1,
286: GridBagConstraints.NONE, 0, 0,
287: GridBagConstraints.CENTER);
288: }
289: }
290:
291: setConstraints(label, labelInsets, 0, y++, 1, 1,
292: GridBagConstraints.BOTH, 1, 1,
293: GridBagConstraints.CENTER);
294:
295: if (rightComponents != null) {
296: for (int i = 0; i < rightComponents.length; i++) {
297: int index = direction == Direction.DOWN ? i
298: : rightComponents.length - i - 1;
299: setConstraints(rightComponents[index],
300: rightInsets[index], 0, y++, 1, 1,
301: GridBagConstraints.NONE, 0, 0,
302: GridBagConstraints.CENTER);
303: }
304: }
305: }
306:
307: doUpdate();
308: }
309:
310: private void setConstraints(Component c, Insets insets, int gridx,
311: int gridy, int gridWidth, int gridHeight, int fill,
312: double weightx, double weighty, int anchor) {
313: constraints.insets = InsetsUtil.rotate(getDirection(), insets);
314: constraints.gridx = gridx;
315: constraints.gridy = gridy;
316: constraints.fill = fill;
317: constraints.weightx = weightx;
318: constraints.weighty = weighty;
319: constraints.gridwidth = gridWidth;
320: constraints.gridheight = gridHeight;
321: constraints.anchor = anchor;
322:
323: ((GridBagLayout) getLayout()).setConstraints(c, constraints);
324: }
325:
326: private void doUpdate() {
327: revalidate();
328:
329: if (repaintChecker.isPaintingOk())
330: repaint();
331: }
332: }
|