View Javadoc

1   /*
2   
3   nextobjects Copyright (C) 2001-2005 Emmanuel Florent
4   
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by the
7   Free Software Foundation; either version 2 of the License, or (at your
8   option) any later version.
9   
10  This program is distributed in the hope that it will
11  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12  of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13  PURPOSE. See the GNU General Public License for more details.
14  
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc., 59
17  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  
19  */
20  package org.devaki.nextobjects.ui.main;
21  
22  import java.awt.Color;
23  import java.awt.Component;
24  import java.awt.Graphics;
25  import java.awt.Rectangle;
26  import java.awt.event.MouseEvent;
27  import java.awt.event.MouseListener;
28  import javax.swing.Icon;
29  import javax.swing.JPanel;
30  import javax.swing.JTabbedPane;
31  import org.devaki.nextobjects.util.ModelMan;
32  import org.devaki.nextobjects.util.NOFileManager;
33  import org.devaki.nextobjects.workspace.models.BaseModel;
34  import org.devaki.nextobjects.ui.menus.NOMenuBar;
35  import org.devaki.nextobjects.ui.toolbars.NOToolBar1;
36  import org.devaki.nextobjects.ui.toolbars.NOToolBar2;
37  
38  /***
39   * A JTabbedPane which has a close ('X') icon on each tab.
40   *
41   * To add a tab, use the method addTab(String, Component)
42   *
43   * To have an extra icon on each tab  use
44   * the method addTab(String, Component, Icon).
45   * Only clicking the 'X' closes the tab.
46   */
47  
48  public class NOWorkspace extends JTabbedPane implements MouseListener
49  {
50   /***
51    * constructor
52    */
53   public NOWorkspace()
54   {
55    super();
56    addMouseListener(this);
57   }
58  
59   /***
60    * Add a panel
61    * @param pJPanel the panel
62    * @param title the title
63    */
64   public void addPanel(JPanel pJPanel, String title)
65   {
66    this.add(pJPanel, title);
67   }
68  
69   /***
70    * add a tabbed pane
71    * @param component the component
72    * @param title the title
73    */
74   public void addTab(String title, Component component)
75   {
76    this.addTab(title, component, null);
77   }
78   /***
79    * add a tabbed pane
80    * @param title title
81    * @param component component
82    * @param extraIcon icon
83    */
84   public void addTab(String title, Component component, Icon extraIcon)
85   {
86    super.addTab(title, new CloseTabIcon(extraIcon), component);
87   }
88   /***
89    * What to do when mouse clicked
90    * @param e <code>MouseEvent</code>
91    */
92   public void mouseClicked(MouseEvent e)
93   {
94    int tabNumber = getUI().tabForCoordinate(this, e.getX(), e.getY());
95    ModelMan.resetCurrentObjects();
96    if (tabNumber < 0)
97     return;
98    ModelMan.setCurrentModel(
99     (BaseModel) ModelMan.getModels().elementAt(tabNumber));
100 
101   Rectangle rect = ((CloseTabIcon) getIconAt(tabNumber)).getBounds();
102   if (rect.contains(e.getX(), e.getY()))
103   {
104    if (NOFileManager.close(ModelMan.getCurrentModel()))
105    {
106     if (ModelMan.getModels().size() > 0)
107     {
108      ModelMan.setCurrentModel(
109       (BaseModel) ModelMan.getModels().elementAt(
110        this.getSelectedIndex()));
111     }
112    }
113   }
114   NOMenuBar.fixFileMenu();
115   NOMenuBar.fixEditMenu();
116   NOToolBar1.fixIcons();
117   NOToolBar2.fixIcons();
118 
119  }
120  /***
121   * What to di when mouse entered 
122   * actually do nothing
123   * @param e <code>MouseEvent</code>
124   */
125  public void mouseEntered(MouseEvent e)
126  {
127  }
128  /***
129   * What to di when mouse exited 
130   * actually do nothing
131   * @param e <code>MouseEvent</code>
132   */
133  public void mouseExited(MouseEvent e)
134  {
135  }
136  /***
137   * What to di when mouse pressed
138   * actually do nothing
139   * @param e <code>MouseEvent</code>
140   */
141  public void mousePressed(MouseEvent e)
142  {
143  }
144  /***
145   * What to di when mouse released 
146   * actually do nothing
147   * @param e <code>MouseEvent</code>
148   */
149  public void mouseReleased(MouseEvent e)
150  {
151  }
152 }
153 
154 /***
155  * The class which generates the 'X' icon for the tabs. The constructor
156  * accepts an icon which is extra to the 'X' icon, so you can have tabs
157  * like in JBuilder. This value is null if no extra icon is required.
158  */
159 class CloseTabIcon implements Icon
160 {
161  /***
162   *  X position
163   */
164  private int x_pos;
165 
166  /***
167   *  Y position
168   */
169  private int y_pos;
170 
171  /***
172   *  Width
173   */
174  private int width;
175 
176  /***
177   *  Height
178   */
179  private int height;
180  /***
181   *  Icon
182   */
183  private Icon fileIcon;
184 
185  /***
186   * CloseTabIcon
187   * @param fileIcon the file icon
188   */
189  public CloseTabIcon(Icon fileIcon)
190  {
191   this.fileIcon = fileIcon;
192   width = 16;
193   height = 16;
194  }
195 
196  /***
197   *  Paint icon
198   * @param c component
199   * @param g graphics g
200   * @param x x
201   * @param y y
202   */
203  public void paintIcon(Component c, Graphics g, int x, int y)
204  {
205   this.x_pos = x;
206   this.y_pos = y;
207 
208   Color col = g.getColor();
209 
210   g.setColor(Color.black);
211   int y_p = y + 2;
212   g.drawLine(x + 1, y_p, x + 12, y_p);
213   g.drawLine(x + 1, y_p + 13, x + 12, y_p + 13);
214   g.drawLine(x, y_p + 1, x, y_p + 12);
215   g.drawLine(x + 13, y_p + 1, x + 13, y_p + 12);
216   g.drawLine(x + 3, y_p + 3, x + 10, y_p + 10);
217   g.drawLine(x + 3, y_p + 4, x + 9, y_p + 10);
218   g.drawLine(x + 4, y_p + 3, x + 10, y_p + 9);
219   g.drawLine(x + 10, y_p + 3, x + 3, y_p + 10);
220   g.drawLine(x + 10, y_p + 4, x + 4, y_p + 10);
221   g.drawLine(x + 9, y_p + 3, x + 3, y_p + 9);
222   g.setColor(col);
223   if (fileIcon != null)
224   {
225    fileIcon.paintIcon(c, g, x + width, y_p);
226   }
227  }
228  /***
229   * get the icon width
230   * @return the width
231   */
232  public int getIconWidth()
233  {
234   return width + (fileIcon != null ? fileIcon.getIconWidth() : 0);
235  }
236  /***
237   * get icon height
238   * @return height 
239   */
240  public int getIconHeight()
241  {
242   return height;
243  }
244  /***
245   * get the boundaries
246   * @return bounds
247   */
248  public Rectangle getBounds()
249  {
250   return new Rectangle(x_pos, y_pos, width, height);
251  }
252 }