KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > project > ProjectEditorZone


1 package org.antlr.works.project;
2
3 import org.antlr.works.components.project.CContainerProject;
4 import org.antlr.works.components.project.file.CContainerProjectGrammar;
5 import org.antlr.works.components.project.file.CContainerProjectJava;
6 import org.antlr.works.components.project.file.CContainerProjectText;
7
8 import javax.swing.*;
9 import javax.swing.event.ChangeEvent JavaDoc;
10 import javax.swing.event.ChangeListener JavaDoc;
11 import java.awt.*;
12 import java.awt.event.ActionEvent JavaDoc;
13 import java.awt.event.ActionListener JavaDoc;
14 import java.awt.event.MouseAdapter JavaDoc;
15 import java.awt.event.MouseEvent JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Map JavaDoc;
18 /*
19
20 [The "BSD licence"]
21 Copyright (c) 2005-2006 Jean Bovet
22 All rights reserved.
23
24 Redistribution and use in source and binary forms, with or without
25 modification, are permitted provided that the following conditions
26 are met:
27
28 1. Redistributions of source code must retain the above copyright
29 notice, this list of conditions and the following disclaimer.
30 2. Redistributions in binary form must reproduce the above copyright
31 notice, this list of conditions and the following disclaimer in the
32 documentation and/or other materials provided with the distribution.
33 3. The name of the author may not be used to endorse or promote products
34 derived from this software without specific prior written permission.
35
36 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
37 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
39 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
40 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
45 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46
47 */

48
49 public class ProjectEditorZone {
50
51     protected CContainerProject project;
52     protected JPanel panel;
53     protected JTabbedPane tabbedPane;
54     protected JPanel emptyPanel;
55     protected JPanel loadingPanel;
56
57     public ProjectEditorZone(CContainerProject project) {
58         this.project = project;
59
60         emptyPanel = createEmptyEditorPanel();
61         loadingPanel = createLoadingEditorPanel();
62
63         tabbedPane = new JTabbedPane();
64         tabbedPane.addChangeListener(new TabbedPaneChangeListener());
65         tabbedPane.addMouseListener(new TabbedPaneMouseListener());
66
67         panel = new JPanel(new BorderLayout());
68         openFileItem(null);
69     }
70
71     public JPanel getPanel() {
72         return panel;
73     }
74
75     public JPanel createInfoPanel(String JavaDoc info) {
76         JPanel p = new JPanel(new BorderLayout());
77         JLabel l = new JLabel(info);
78         l.setHorizontalAlignment(JLabel.CENTER);
79         l.setFont(new Font("dialog", Font.PLAIN, 36));
80         l.setForeground(Color.gray);
81         p.add(l, BorderLayout.CENTER);
82         p.setBorder(BorderFactory.createMatteBorder(0, 1, 0, 0, Color.lightGray));
83         return p;
84     }
85
86     public JPanel createLoadingEditorPanel() {
87         return createInfoPanel("Loading...");
88     }
89
90     public JPanel createEmptyEditorPanel() {
91         return createInfoPanel("No Editor");
92     }
93
94     public void setEditorZoneToEmpty() {
95         panel.remove(tabbedPane);
96         panel.add(emptyPanel, BorderLayout.CENTER);
97         panel.revalidate();
98         panel.repaint();
99
100         project.setDefaultMainMenuBar();
101     }
102
103     public void setEditorZoneToTab() {
104         panel.remove(emptyPanel);
105         panel.add(tabbedPane, BorderLayout.CENTER);
106         panel.revalidate();
107         panel.repaint();
108     }
109
110     public void setProjectFileItem(ProjectFileItem item) {
111         if(item == null)
112             return;
113                 
114         project.setMainMenuBar(item.getComponentContainer().getMainMenuBar());
115         project.setToolBar(item.getComponentContainer().getEditor().getToolbarComponent());
116         project.setStatusBar(item.getComponentContainer().getEditor().getStatusComponent());
117
118         /** Tell the editor that is has been select. Do that later in order to avoid
119          * another component to request the focus after the editor.
120          */

121
122         SwingUtilities.invokeLater(new Runnable JavaDoc() {
123             public void run() {
124                 getSelectedFileItem().getComponentContainer().getEditor().componentIsSelected();
125             }
126         });
127     }
128
129     public void openFileItem(ProjectFileItem item) {
130         if(item == null) {
131             setEditorZoneToEmpty();
132         } else {
133             if(item.getComponentContainer() == null) {
134                 new ProjectFileItemFactory(item).create();
135             } else {
136                 fileEditorItemDidLoad(item);
137             }
138         }
139     }
140
141     public void closeActiveEditor() {
142         removeFileItemFromTab(getSelectedFileItem());
143         project.refreshMainMenuBar();
144     }
145     
146     public void fileEditorItemDidLoad(ProjectFileItem item) {
147         addFileItemToTab(item);
148         setEditorZoneToTab();
149     }
150
151     public void addFileItemToTab(ProjectFileItem item) {
152         int index;
153         if(isTabbedPaneContainFileItem(item)) {
154             // The item is already in a tab. Just select the tab.
155
index = getIndexOfFileItemInTab(item);
156         } else {
157             index = tabbedPane.getSelectedIndex();
158             if(index == -1) {
159                 tabbedPane.addTab(item.getFileName(), item.getEditorPanel());
160                 index = tabbedPane.getComponentCount()-1;
161             } else {
162                 index++;
163                 tabbedPane.insertTab(item.getFileName(), null, item.getEditorPanel(), null, index);
164             }
165
166             tabbedPane.setToolTipTextAt(index, item.getFilePath());
167         }
168         tabbedPane.setSelectedIndex(index);
169         item.setOpened(true);
170         item.setTabIndex(index);
171     }
172
173     public void removeFileItemFromTab(ProjectFileItem item) {
174         int index = getIndexOfFileItemInTab(item);
175         if(index == -1)
176             return;
177         
178         tabbedPane.removeTabAt(index);
179         if(tabbedPane.getComponentCount() == 0) {
180             setEditorZoneToEmpty();
181         } else {
182             if(index > 0)
183                 index--;
184
185             if(tabbedPane.getSelectedIndex() == index)
186                 setProjectFileItem(getSelectedFileItem());
187             else
188                 tabbedPane.setSelectedIndex(index);
189         }
190         item.setOpened(false);
191     }
192
193     public boolean isTabbedPaneContainFileItem(ProjectFileItem item) {
194         return getIndexOfFileItemInTab(item) != -1;
195     }
196
197     public int getIndexOfFileItemInTab(ProjectFileItem item) {
198         for(int index=0; index<tabbedPane.getComponentCount(); index++) {
199             if(tabbedPane.getComponentAt(index) == item.getEditorPanel())
200                 return index;
201         }
202         return -1;
203     }
204
205     public ProjectFileItem getFileItemForTabComponent(Component component) {
206         if(component == null)
207             return null;
208
209         for (Object JavaDoc o : project.getFileEditorItems()) {
210             ProjectFileItem item = (ProjectFileItem) o;
211             if (item.getEditorPanel() == component)
212                 return item;
213         }
214
215         return null;
216     }
217
218     public ProjectFileItem getSelectedFileItem() {
219         return getFileItemForTabComponent(tabbedPane.getSelectedComponent());
220     }
221
222     public static final String JavaDoc KEY_SELECTED_FILE_NAME = "KEY_SELECTED_FILE_NAME";
223
224     public void setPersistentData(Map JavaDoc data) {
225         if(data == null)
226             return;
227
228         String JavaDoc fileName = (String JavaDoc) data.get(KEY_SELECTED_FILE_NAME);
229         if(fileName != null) {
230             for(int index=0; index<tabbedPane.getComponentCount(); index++) {
231                 ProjectFileItem item = getFileItemForTabComponent(tabbedPane.getComponentAt(index));
232                 if(item == null)
233                     continue;
234
235                 if(item.getFileName().equals(fileName)) {
236                     tabbedPane.setSelectedIndex(index);
237                     break;
238                 }
239             }
240         }
241     }
242
243     public Map JavaDoc<String JavaDoc,String JavaDoc> getPersistentData() {
244         Map JavaDoc<String JavaDoc,String JavaDoc> data = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
245         if(getSelectedFileItem() != null)
246             data.put(KEY_SELECTED_FILE_NAME, getSelectedFileItem().getFileName());
247         return data;
248     }
249
250     public static final int DIRECTION_LEFT = -1;
251     public static final int DIRECTION_RIGHT = 1;
252
253     public void moveActiveEditor(int direction) {
254         int index = tabbedPane.getSelectedIndex();
255         if(index == -1)
256             return;
257
258         if(direction == DIRECTION_LEFT && index <= 0)
259             return;
260
261         if(direction == DIRECTION_RIGHT && index >= tabbedPane.getTabCount()-1)
262             return;
263
264         swapTab(index, index+direction);
265     }
266
267     public void swapTab(int a, int b) {
268         String JavaDoc title = tabbedPane.getTitleAt(a);
269         Component c = tabbedPane.getComponentAt(a);
270         tabbedPane.removeTabAt(a);
271         tabbedPane.insertTab(title, null, c, null, b);
272         tabbedPane.setSelectedIndex(b);
273
274         // Update also each ProjectFileItem's tab index
275
getFileItemForTabComponent(tabbedPane.getComponentAt(a)).setTabIndex(a);
276         getFileItemForTabComponent(tabbedPane.getComponentAt(b)).setTabIndex(b);
277     }
278
279     protected class TabbedPaneChangeListener implements ChangeListener JavaDoc {
280         public void stateChanged(ChangeEvent JavaDoc e) {
281             setProjectFileItem(getSelectedFileItem());
282         }
283     }
284
285     protected class TabbedPaneMouseListener extends MouseAdapter JavaDoc {
286
287         public void displayPopUp(MouseEvent JavaDoc event) {
288             if(!event.isPopupTrigger())
289                 return;
290
291             JPopupMenu popup = new JPopupMenu();
292             JMenuItem item = new JMenuItem("Close");
293             item.addActionListener(new ActionListener JavaDoc() {
294                 public void actionPerformed(ActionEvent JavaDoc event) {
295                     closeActiveEditor();
296                 }
297             });
298             popup.add(item);
299             popup.show(event.getComponent(), event.getX(), event.getY());
300         }
301
302         public void mousePressed(MouseEvent JavaDoc event) {
303             displayPopUp(event);
304         }
305
306         public void mouseReleased(MouseEvent JavaDoc event) {
307             displayPopUp(event);
308         }
309     }
310
311     protected class ProjectFileItemFactory {
312
313         public ProjectFileItem item;
314
315         public ProjectFileItemFactory(ProjectFileItem item) {
316             this.item = item;
317         }
318
319         public void create() {
320             String JavaDoc type = ProjectFileItem.getFileType(item.getFilePath());
321             if(type.equals(ProjectFileItem.FILE_TYPE_GRAMMAR))
322                 new CContainerProjectGrammar(project, item);
323             else if(type.equals(ProjectFileItem.FILE_TYPE_JAVA))
324                 new CContainerProjectJava(project, item);
325             else
326                 new CContainerProjectText(project, item);
327
328             item.getComponentContainer().getDocument().performLoad(item.getFilePath());
329
330             if(!item.isOpened()) {
331                 // Update the layout of the item only if it is created and not loaded
332
// from disk (isOpened() will be false if it is being just created).
333
// When loaded from disk, the project will contain all data needed
334
// for the layout: that's why we don't need to layout (again) the item.
335
SwingUtilities.invokeLater(new Runnable JavaDoc() {
336                     public void run() {
337                         // @todo needs to get the size of the editor
338
//item.getComponentContainer().getEditor().componentShouldLayout();
339
}
340                 });
341             }
342
343             fileEditorItemDidLoad(item);
344         }
345
346     }
347
348 }
349
Popular Tags