Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/jext/JextTabbedPane.java


1   /*
2    * 03/13/2003 - 17:37:39
3    *
4    * JextTabbedPane.java - Jext tabbed pane
5    * Copyright (C) 2003 Romain Guy
6    * romain.guy@jext.org
7    * www.jext.org
8    *
9    * This program is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU General Public License
11   * as published by the Free Software Foundation; either version 2
12   * of the License, or any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   *
19   * You should have received a copy of the GNU General Public License
20   * along with this program; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22   */
23  
24  package org.jext;
25  
26  import java.awt.Point;
27  import java.awt.Insets;
28  import java.awt.Dimension;
29  import java.awt.Component;
30  
31  import java.awt.event.MouseEvent;
32  import java.awt.event.MouseAdapter;
33  
34  import java.util.HashMap;
35  
36  import javax.swing.Icon;
37  import javax.swing.JPopupMenu;
38  import javax.swing.JTabbedPane;
39  import javax.swing.event.ChangeEvent;
40  import javax.swing.event.ChangeListener;
41  
42  import org.jext.event.JextEvent;
43  import org.jext.xml.XPopupReader;
44  
45  /**
46   * A tabbed pane which can display indexed titles. As a matter of fact,
47   * if a tab is added with a name which already exists, <code>JextTabbedPane</code>
48   * will add an index under the form '(x)' at the end of the name. When all tabs
49   * of the same name are closed, indexes are counted over from 0.
50   * @author Romain Guy
51   */
52  
53  public class JextTabbedPane extends JTabbedPane implements ChangeListener
54  {
55    // static fields
56    private static JPopupMenu popupMenu;
57  
58    // private
59    private JextFrame parent;
60    // hold indexes
61    private HashMap fileNames = new HashMap();
62    // mouse listener
63    private PopupMenu _mouseListener;
64  
65    // icon of a 'clean' text area
66    private static final Icon CLEAN_ICON = Utilities.getIcon("images/tab_clean.gif", Jext.class);
67    // icon of a 'dirty' text area
68    private static final Icon DIRTY_ICON = Utilities.getIcon("images/tab_dirty.gif", Jext.class);
69    
70    /**
71     * Creates a new tabbed pane and register a change listener
72     * which will be used to update Jext infos concerning text
73     * areas.
74     * @param parent The parent window
75     */
76  
77    public JextTabbedPane(JextFrame parent)
78    {
79      super();
80      this.parent = parent;
81      GUIUtilities.setScrollableTabbedPane(this);
82      addMouseListener(_mouseListener = new PopupMenu());
83      addChangeListener(this);
84    }
85  
86    /**
87     * Returns JextTabbedPane popup menu. This is needed to update look and feel when
88     * user changed it.
89     */
90  
91    public static JPopupMenu getPopupMenu()
92    {
93      return popupMenu;
94    }
95  
96    // handles mouse right clicks to display a popup
97  
98    class PopupMenu extends MouseAdapter implements Runnable
99    {
100     // when we constructs this instance, we also
101     // build a new popup menu from an external XML file
102 
103     PopupMenu()
104     {
105       if (popupMenu == null)
106       {
107         Thread t = new Thread(this);
108         t.start();
109       }
110     }
111 
112     public void run()
113     {
114       popupMenu = XPopupReader.read(Jext.class.getResourceAsStream("jext.tabbedpane.popup.xml"),
115                                     "jext.tabbedpane.popup.xml");
116       if (Jext.getFlatMenus())
117         popupMenu.setBorder(javax.swing.border.LineBorder.createBlackLineBorder());
118     }
119 
120     // shows popup on right click
121     public void mouseReleased(MouseEvent me)
122     {
123       showPopupIfNeeded(me);
124     }
125     
126     public void mousePressed(MouseEvent me)
127     {
128       showPopupIfNeeded(me);
129     }
130 
131     private void showPopupIfNeeded(MouseEvent me)
132     {
133       //if ((me.getModifiers() & me.BUTTON3_MASK) != 0 && popupMenu != null)
134       if (me.isPopupTrigger() && popupMenu != null)
135       {
136         int x = me.getX();
137         Dimension parentSize = parent.getSize();
138         Point parentLocation = parent.getLocationOnScreen();
139         Insets parentInsets  = parent.getInsets();
140 
141         Point tapLocation = JextTabbedPane.this.getLocationOnScreen();
142         Dimension popupSize  = popupMenu.getSize();
143 
144         if ((tapLocation.x + x + popupSize.width) >
145             (parentLocation.x + parentSize.width - parentInsets.right))
146         {
147           x -= popupSize.width;
148         }
149 
150         popupMenu.show(JextTabbedPane.this, x, me.getY());
151       }
152     }
153   }
154 
155   /**
156    * When a text area is saved (or simply cleaned in case
157    * of open or new), this method is used to display the 'clean'
158    * icon in the tab which holds the text area.
159    * @param textArea The text area which was cleaned
160    */
161 
162   public void setCleanIcon(JextTextArea textArea)
163   {
164     int index = indexOfComponent(textArea);
165     if (index == -1)
166       return;
167 
168     setIconAt(index, CLEAN_ICON);
169   }
170 
171   /**
172    * When a text area is modified this method is used to display
173    * the 'dirty' icon in the tab which holds the text area.
174    * @param textArea The text area which was modified
175    */
176 
177   public void setDirtyIcon(JextTextArea textArea)
178   {
179     int index = indexOfComponent(textArea);
180     if (index == -1)
181       return;
182 
183     setIconAt(index, DIRTY_ICON);
184   }
185 
186   /**
187    * Overrides default <code>addTab(String, Component)</code> method.
188    * This method register the <code>title</code> if it hasn't been
189    * displayed yet to be able to index next titles of same name. Then,
190    * <code>super.addTab(String, Component)</code> is called.
191    * @param title Title of the new tab
192    * @param component Content of the new tab
193    */
194 
195   public void addTab(String title, Component component)
196   {
197     setIndexedTitle(title);
198     super.addTab(getIndexedTitle(title), (component instanceof JextTextArea ?
199                                           (((JextTextArea) component).isDirty() ? DIRTY_ICON : CLEAN_ICON)
200                                           : null),
201                  component);
202   }
203 
204   /**
205    * Overrides default <code>removeTabAt(int)</code> method.
206    * This method checks if no other tab among the remaining ones
207    * has got the same name. In this case, the title is unregistered
208    * from the indexed titles list.
209    * @param index Removes the tab at <code>index</code>
210    */
211 
212   public void removeTabAt(int index)
213   {
214     if (index == -1)
215       return;
216 
217     removeTitle(index, getComponentAt(index).getName());
218     super.removeTabAt(index);
219 
220     // if (index == 0)
221     stateChanged(new ChangeEvent(this));
222   }
223 
224   /**
225    * Overrides default <code>setTitleAt(int, String)</code> method.
226    * Before setting the new title, the new title is registered in
227    * the indexed titles list. Then, we call <code>super.setTitleAt(int, String)</code>
228    * method but specifying an indexed title.
229    * @param index The index of tab to be re-titled
230    * @param title The new title
231    */
232 
233   public void setTitleAt(int index, String title)
234   {
235     if (index == -1)
236       return;
237 
238     removeTitle(index, getComponentAt(index).getName());
239     setIndexedTitle(title);
240     super.setTitleAt(index, getIndexedTitle(title));
241   }
242 
243   // if specified title is title of another tab, we keep it in the list,
244   // otherwise, it is removed
245 
246   private void removeTitle(int index, String title)
247   {
248     String _name;
249     boolean more = false;
250 
251     for (int i = 0; i < getTabCount(); i++)
252     {
253       if (i != index && (_name = getComponentAt(i).getName()) != null && _name.equals(title))
254       {
255         more = true;
256         break;
257       }
258     }
259 
260     if (!more)
261       fileNames.remove(title);
262   }
263 
264   // register a title in the list. If it hasn't been registered
265   // yet, it is added to list, otherwise, the corresponding index
266   // is increased by one.
267 
268   private void setIndexedTitle(String title)
269   {
270     if (title == null)
271       title = Jext.getProperty("general.unknown");
272 
273     Integer _integer = (Integer) fileNames.get(title);
274     if (_integer == null)
275     {
276       fileNames.put(title, new Integer(0));
277     } else {
278       fileNames.put(title, new Integer(_integer.intValue() + 1));
279     }
280   }
281 
282   // get a title from the indexed titles list. If the title is found in
283   // the list, and if the index is != 0, we add "(index)" at the end of
284   // the title.
285 
286   private String getIndexedTitle(String title)
287   {
288     if (title == null)
289       return Jext.getProperty("general.unknown");
290 
291     int _val;
292     Integer _integer = (Integer) fileNames.get(title);
293 
294     if (_integer != null && (_val = _integer.intValue()) != 0)
295     {
296       return (new StringBuffer(title)).append(" (").append(_val).append(')').toString();
297     }
298 
299     return title;
300   }
301 
302   /**
303    * Selects the next tab in the list. If current selected tab
304    * is the last one, then the first one is selected.
305    */
306 
307   public void nextTab()
308   {
309     int selectedIndex = getSelectedIndex();
310     if (++selectedIndex == getTabCount())
311       selectedIndex = 0;
312     setSelectedIndex(selectedIndex);
313   }
314 
315   /**
316    * Selects the previous tab in the list. If current selected tab
317    * is the first one, then the last one is selected.
318    */
319 
320   public void previousTab()
321   {
322     int selectedIndex = getSelectedIndex();
323     if (selectedIndex == 0)
324       selectedIndex = getTabCount() - 1;
325     else
326       selectedIndex--;
327     setSelectedIndex(selectedIndex);
328   }
329 
330   /**
331    * Removes any installed component from this tabbed pane.
332    * Also resets the titles list.
333    */
334 
335   public void removeAll()
336   {
337     fileNames.clear();
338     super.removeAll();
339   }
340 
341   /**
342    * When a tab is selected, and if content of the tab is a
343    * <code>JextTextArea</code>, we update infos displayed by
344    * Jext and notify Jext listeners.
345    */
346 
347   public void stateChanged(ChangeEvent evt)
348   {
349     Component c = getSelectedComponent();
350 
351     if (!(c instanceof JextTextArea))
352     {
353       if (c != null)
354       {
355         parent.setTitle("Jext - " + getTitleAt(indexOfComponent(c)) +
356                         " [" + parent.getWorkspaces().getName() + ']');
357         parent.disableSplittedTextArea();
358       }
359       return;
360     }
361 
362     JextTextArea textArea = (JextTextArea) c;
363 
364     textArea.setParentTitle();
365     parent.updateStatus(textArea);
366     parent.setStatus(textArea);
367     parent.updateSplittedTextArea(textArea);
368     parent.fireJextEvent(JextEvent.TEXT_AREA_SELECTED);
369 
370     textArea.grabFocus();
371     textArea.requestFocus();
372   }
373   
374   /***************************************************************************
375   Patch
376      -> Memory management improvements : it may help the garbage collector.
377      -> Author : Julien Ponge (julien@izforge.com)
378      -> Date : 23, May 2001
379   ***************************************************************************/
380   protected void finalize() throws Throwable
381   {
382     removeChangeListener(this);
383     removeMouseListener(_mouseListener);
384     super.finalize();
385     
386     parent = null;
387     fileNames.clear();
388     fileNames = null;
389     _mouseListener = null;
390   }
391   // End of patch
392 }
393 
394 // End of JextTabbedPane.java