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: LengthLimitedDockingWindowTitleProvider.java,v 1.6 2005/02/16 11:28:14 jesper Exp $
023: package net.infonode.docking.title;
024:
025: import net.infonode.docking.AbstractTabWindow;
026: import net.infonode.docking.DockingWindow;
027: import net.infonode.docking.View;
028:
029: import java.io.Serializable;
030: import java.util.ArrayList;
031:
032: /**
033: * A docking window title provider that constructs a window title from the views inside the window. It adds view
034: * titles until the window title reaches a specified length. If not all view titles fit into the window title,
035: * primarily titles from view inside selected tabs are used.
036: *
037: * @author $Author: jesper $
038: * @version $Revision: 1.6 $
039: * @since IDW 1.3.0
040: */
041: public class LengthLimitedDockingWindowTitleProvider implements
042: DockingWindowTitleProvider, Serializable {
043: private static final long serialVersionUID = 1;
044:
045: private int maxLength;
046:
047: /**
048: * Constructor.
049: *
050: * @param maxLength if the title exceeds this length no more view titles are added to it
051: */
052: public LengthLimitedDockingWindowTitleProvider(int maxLength) {
053: this .maxLength = maxLength;
054: }
055:
056: public String getTitle(DockingWindow window) {
057: ArrayList viewTitles = new ArrayList();
058: ArrayList viewPrimary = new ArrayList();
059: getViews(window, viewTitles, viewPrimary, true);
060:
061: int length = 0;
062:
063: for (int i = 0; i < viewTitles.size(); i++) {
064: if (((Boolean) viewPrimary.get(i)).booleanValue()) {
065: length += ((String) viewTitles.get(i)).length();
066: }
067: }
068:
069: StringBuffer title = new StringBuffer(40);
070: int count = 0;
071:
072: for (int i = 0; i < viewTitles.size()
073: && title.length() < maxLength; i++) {
074: boolean primary = ((Boolean) viewPrimary.get(i))
075: .booleanValue();
076:
077: if (primary || length < maxLength) {
078: if (title.length() > 0)
079: title.append(", ");
080:
081: title.append((String) viewTitles.get(i));
082: count++;
083:
084: if (!primary)
085: length += ((String) viewTitles.get(i)).length();
086: }
087: }
088:
089: if (count < viewTitles.size())
090: title.append(", ...");
091:
092: return title.toString();
093: }
094:
095: private void getViews(DockingWindow window, ArrayList viewTitles,
096: ArrayList viewPrimary, boolean primary) {
097: if (window == null)
098: return;
099: else if (window instanceof View) {
100: viewTitles.add(((View) window).getViewProperties()
101: .getTitle());
102: viewPrimary.add(Boolean.valueOf(primary));
103: } else if (window instanceof AbstractTabWindow) {
104: DockingWindow selected = ((AbstractTabWindow) window)
105: .getSelectedWindow();
106:
107: for (int i = 0; i < window.getChildWindowCount(); i++) {
108: getViews(window.getChildWindow(i), viewTitles,
109: viewPrimary, selected == window
110: .getChildWindow(i)
111: && primary);
112: }
113: } else {
114: for (int i = 0; i < window.getChildWindowCount(); i++) {
115: getViews(window.getChildWindow(i), viewTitles,
116: viewPrimary, primary);
117: }
118: }
119: }
120:
121: }
|