View Javadoc

1   /***
2    * TabbedWorkspaceComponent.java
3    *
4    * This file is part of the creme library.
5    * The creme library intends to ease the development effort of large
6    * applications by providing easy to use support classes.
7    *
8    * Copyright (C) 2003 Denis Bregeon
9    *
10   * This library is free software; you can redistribute it and/or
11   * modify it under the terms of the GNU Lesser General Public
12   * License as published by the Free Software Foundation; either
13   * version 2.1 of the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   * Lesser General Public License for more details.
19   *
20   * You should have received a copy of the GNU Lesser General Public
21   * License along with this library; if not, write to the Free Software
22   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23   *
24   * contact information: dbregeon@sourceforge.net
25   */
26  package org.jcreme.swing.workspace;
27  
28  import java.text.Format;
29  import java.util.Vector;
30  
31  import javax.swing.JPanel;
32  import javax.swing.JTabbedPane;
33  
34  /***
35   * 
36   * @author $Author: dbregeon $
37   * @version $Revision: 1.1 $
38   */
39  public class TabbedWorkspaceComponent extends JTabbedPane {
40      private Format identifierFormat = null;
41  
42      private WorkspaceFactory workspaces = null;
43  
44      private Workspace currentWorkspace = null;
45  
46      private final Vector identifiers = new Vector();
47  
48      private final WorkspaceFactoryListener workspacesListener = new WorkspaceFactoryListener() {
49          public void workspaceAdded(WorkspaceFactoryEvent e) {
50              addTab(e.getWorkspaceId());
51          }
52  
53          public void workspaceRemoved(WorkspaceFactoryEvent e) {
54              removeTab(e.getWorkspaceId());
55          }
56      };
57  
58      /***
59       * 
60       * @param f
61       */
62      public void setWorkspaces(WorkspaceFactory f) {
63          if (this.workspaces != null) {
64              this.workspaces
65                      .removeWorkspaceFactoryListener(this.workspacesListener);
66          }
67          this.workspaces = f;
68          if (this.workspaces != null) {
69              setIdentifiers(this.workspaces.getWorkspaceIdentifiers());
70              this.workspaces
71                      .addWorkspaceFactoryListener(this.workspacesListener);
72          } else {
73              setIdentifiers(new Object[0]);
74          }
75      }
76  
77      /***
78       * 
79       * @return the factory that provides all the workspaces.
80       */
81      public WorkspaceFactory getWorkspaces() {
82          return this.workspaces;
83      }
84  
85      /***
86       * 
87       * @param f
88       */
89      public void setIdentifierFormat(Format f) {
90          this.identifierFormat = f;
91      }
92  
93      /***
94       * 
95       * @return the Format to present identifiers.
96       */
97      public Format getIdentifierFormat() {
98          return this.identifierFormat;
99      }
100 
101     /***
102      * 
103      * @return the identifiers currently used.
104      */
105     public Object[] getIdentifiers() {
106         return this.identifiers.toArray();
107     }
108 
109     /***
110      * 
111      * @return the workspace currently displayed.
112      */
113     public Object getCurrentWorkspace() {
114         Object result = null;
115         if (this.currentWorkspace != null) {
116             result = this.currentWorkspace.getIdentifier();
117         }
118         return result;
119     }
120 
121     /***
122      * 
123      * @param id
124      */
125     public void setCurrentWorkspace(Object id) {
126         if (id != null) {
127             synchronized (this.identifiers) {
128                 for (int i = 0; i < this.identifiers.size(); i++) {
129                     if (id.equals(this.identifiers.elementAt(i))) {
130                         setSelectedIndex(i);
131                     }
132                 }
133             }
134         }
135     }
136 
137     /***
138      * This method can be overriden to provide icons in the tabs or a specific
139      * order to the identifiers.
140      * 
141      * @param ids
142      *            the identifiers to set in the tabbedPane.
143      */
144     protected void setIdentifiers(Object[] ids) {
145         removeAll();
146         synchronized (this.identifiers) {
147             this.identifiers.clear();
148             for (int i = 0; i < ids.length; i++) {
149                 addTab(ids[i]);
150             }
151         }
152     }
153 
154     protected void addTab(Object id) {
155         if (id != null) {
156             String title = null;
157             if (this.identifierFormat != null) {
158                 title = this.identifierFormat.format(id);
159             } else if (id != null) {
160                 title = id.toString();
161             } else {
162                 title = "";
163             }
164             synchronized (this.identifiers) {
165                 this.identifiers.add(id);
166                 addTab(title, new JPanel());
167                 setSelectedIndex(getTabCount() - 1);
168             }
169         }
170     }
171 
172     protected void removeTab(Object id) {
173         if (id != null) {
174             int index = this.identifiers.indexOf(id);
175             if (index >= 0) {
176                 synchronized (this.identifiers) {
177                     this.identifiers.removeElementAt(index);
178                     setSelectedIndex((index == this.identifiers.size() - 1) ? 0
179                             : index - 1);
180                     remove(index);
181                 }
182             }
183         }
184     }
185 
186     /***
187      * @param index
188      */
189     public void setSelectedIndex(int index) {
190         if (this.workspaces != null) {
191             if (this.currentWorkspace != null) {
192                 this.currentWorkspace.hide();
193             }
194             if ((index >= 0) && (index < this.identifiers.size())) {
195                 this.currentWorkspace = this.workspaces
196                         .getWorkspace(this.identifiers.elementAt(index));
197             }
198             if (this.currentWorkspace != null) {
199                 this.currentWorkspace.display();
200             }
201         }
202         super.setSelectedIndex(index);
203     }
204 }