View Javadoc

1   /*
2    * $Id: ProjectsView.java,v 1.17 2007/07/04 19:42:49 edankert Exp $
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  
22  package org.xmlhammer.gui;
23  
24  import java.awt.BorderLayout;
25  import java.awt.Component;
26  import java.awt.Font;
27  import java.awt.event.MouseEvent;
28  
29  import javax.swing.Icon;
30  import javax.swing.ImageIcon;
31  import javax.swing.JPopupMenu;
32  import javax.swing.JTabbedPane;
33  import javax.swing.SwingUtilities;
34  import javax.swing.event.ChangeListener;
35  
36  import org.bounce.QIcon;
37  import org.bounce.MenuUtilities;
38  import org.bounce.event.PopupListener;
39  import org.bounce.image.ImageLoader;
40  import org.xmlhammer.gui.util.SelectablePanel;
41  
42  /***
43   * Collection of views (displayed as tabs).
44   * 
45   * @version $Revision: 1.17 $, $Date: 2007/07/04 19:42:49 $
46   * @author Edwin Dankert <edankert@gmail.com>
47   */
48  public class ProjectsView extends SelectablePanel {
49  	private static final long serialVersionUID = 3257853194561337144L;
50      private static final ImageIcon ERROR_ICON = ImageLoader.get().getImage( "/org/xmlhammer/gui/icons/ovr16/error_ovr.gif");
51  	
52  	private int newProjectCounter = 1;
53  
54  	private XMLHammer parent = null;
55  	
56  	private JTabbedPane tabsPanel = null;
57  
58  	/***
59  	 * @param parent the underlying parent.
60  	 */
61  	public ProjectsView( XMLHammer parent) {
62  		super( new BorderLayout());
63  		this.parent = parent;
64          
65  		tabsPanel = new JTabbedPane();
66          tabsPanel.setTabLayoutPolicy( JTabbedPane.SCROLL_TAB_LAYOUT);
67  		tabsPanel.setFont( tabsPanel.getFont().deriveFont( Font.PLAIN));
68          tabsPanel.addMouseListener( new PopupListener() {
69              @Override
70              public void popupTriggered(MouseEvent e) {
71                  getPopup().show( (Component)e.getSource(), e.getX(), e.getY());
72              }
73          });
74  
75  		add( tabsPanel, BorderLayout.CENTER);
76  		setSelected( true);
77  	}
78  	
79  	/***
80  	 * @pre Project should not be duplicate of a previous project.
81  	 * @post Project made visible and selected.
82  	 * 
83  	 * @param view the project view to add.
84  	 */
85  	public void addView( ProjectView view ) {
86          String name = view.getName();
87          
88          if ( view.isChanged()) {
89              name = "*"+name;
90          }
91  
92          tabsPanel.addTab(name, view.getIcon(), view);
93          tabsPanel.setSelectedComponent(view);
94  	}
95  	
96  	/***
97  	 * @param view the view to remove.
98  	 */
99  	public void removeView( ProjectView view) {
100 		tabsPanel.remove( view);
101 	}
102 
103 	/***
104 	 * Returns the selected Project View.
105 	 * 
106 	 * @return the selected Project View.
107 	 */
108 	public ProjectView getSelectedView() {
109 		return (ProjectView)tabsPanel.getSelectedComponent();
110 	}
111 
112     /***
113      * Returns the selected Project View's index.
114      * 
115      * @return the selected Project View's index.
116      */
117     public int getSelectedViewIndex() {
118         return tabsPanel.getSelectedIndex();
119     }
120 
121     public void setSelectedViewIndex(int index) {
122         if (index < tabsPanel.getComponentCount()) { 
123             tabsPanel.setSelectedIndex(index);
124         }
125     }
126 
127     /***
128 	 * @param i the index of the view.
129 	 * 
130 	 * @return the view found at the index.
131 	 */
132 	public ProjectView getViewAt( int i) {
133 		return (ProjectView)tabsPanel.getComponentAt( i);
134 	}
135 
136 	/***
137 	 * @return the number of project views.
138 	 */
139 	public int getViewCount() {
140 		return tabsPanel.getTabCount();
141 	}
142 	
143 	/***
144 	 * @param listener listens to selected view changes.
145 	 */
146 	public void addChangeListener( ChangeListener listener) { 
147 		tabsPanel.addChangeListener( listener);
148 	}
149     
150     public void firePreferencesUpdated() {
151         for ( int i = 0; i < tabsPanel.getTabCount(); i++) {
152             ((ProjectView)tabsPanel.getComponentAt( i)).firePreferencesUpdated();
153         } 
154     }
155     
156 //    public void updateView(ProjectView view) {
157 //        for ( int i = 0; i < tabsPanel.getTabCount(); i++) {
158 //            if ( view == (ProjectView)tabsPanel.getComponentAt( i)) {
159 //                tabsPanel.setTitleAt(i, view.getName());
160 //            }
161 //        } 
162 //    }
163 	
164 	/***
165 	 * Marks that anything in the specific project panel has changed.
166 	 * 
167 	 * @param view the changed project panel.
168 	 */
169 	public void notifyChange(final ProjectView view) {
170         // make sure this is run after the error has been updated...
171         SwingUtilities.invokeLater(new Runnable() {
172             public void run() {
173                 parent.updateTitle();
174                 
175                 String name = view.getName();
176                 
177                 if (view.isChanged()) {
178                     name = "*"+name;
179                 }
180                 
181                 Icon icon = view.getIcon();
182                 if (view.hasError()) {
183                     icon = new QIcon(icon, ERROR_ICON, QIcon.SOUTH_WEST);
184                 }
185                 
186                 for (int i = 0; i < tabsPanel.getTabCount(); i++) {
187                     if (tabsPanel.getComponentAt(i) == view) {
188                         tabsPanel.setTitleAt(i, name);
189                         tabsPanel.setIconAt(i, icon);
190                         return;
191                     }
192                 } 
193             }
194         });
195 	}
196     
197     public AnimatedThread createAnimatedThread(ProjectView panel) {
198         return new AnimatedThread(tabsPanel, panel);
199     }
200 
201     public XMLHammer getRoot() {
202 		return parent;
203 	}
204 	
205 	/***
206 	 * Return the name for a not yet saved project.
207 	 * 
208 	 * @return the new name.
209 	 */
210 	String getNewProjectName() {
211 		String name = "New Project "+newProjectCounter;
212 		newProjectCounter++;
213 
214 		return name;
215 	}
216     
217     JPopupMenu popup = null;
218 
219     private JPopupMenu getPopup() {
220         if (popup == null) {
221             popup = new JPopupMenu();
222             popup.add( getRoot().getCloseAction());
223             popup.add( getRoot().getCloseAllAction());
224             popup.addSeparator();
225             popup.add( getRoot().getExecuteAction());
226             popup.add( getRoot().getStopAction());
227             popup.addSeparator();
228             popup.add( getRoot().getSaveAction());
229             popup.add( getRoot().getSaveAsAction());
230             
231             MenuUtilities.alignMenu( popup);
232         }
233     
234         return popup;
235     }
236 }