KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > components > project > CContainerProject


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

54
55 public class CContainerProject extends XJWindow implements ComponentContainer, XJMenuItemDelegate {
56
57     protected XJMainMenuBar projectDefaultMainMenuBar;
58     protected JSplitPane splitPaneA;
59     protected JSplitPane splitPaneB;
60
61     protected JPanel projectPanel;
62     protected Box projectStatusBar;
63
64     protected ProjectToolbar projectToolbar;
65     protected ProjectExplorer explorer;
66     protected ProjectEditorZone editorZone;
67
68     protected ProjectBuilder builder;
69     protected ProjectConsole console;
70     protected ProjectBuildList buildList;
71
72     public CContainerProject() {
73         builder = new ProjectBuilder(this);
74         console = new ProjectConsole();
75         buildList = new ProjectBuildList();
76
77         projectPanel = new JPanel(new BorderLayout());
78
79         projectToolbar = new ProjectToolbar(this);
80         projectPanel.add(projectToolbar.getToolbar(), BorderLayout.NORTH);
81
82         projectStatusBar = new ComponentStatusBar();
83         projectStatusBar.setPreferredSize(new Dimension(0, 30));
84
85         projectPanel.add(projectStatusBar, BorderLayout.SOUTH);
86
87         explorer = new ProjectExplorer(this);
88         editorZone = new ProjectEditorZone(this);
89
90         splitPaneA = new JSplitPane();
91         splitPaneA.setBorder(null);
92         splitPaneA.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
93         splitPaneA.setLeftComponent(explorer.getPanel());
94         splitPaneA.setRightComponent(editorZone.getPanel());
95         splitPaneA.setContinuousLayout(true);
96         splitPaneA.setOneTouchExpandable(true);
97         splitPaneA.setDividerLocation(150);
98
99         splitPaneB = new JSplitPane();
100         splitPaneB.setBorder(null);
101         splitPaneB.setOrientation(JSplitPane.VERTICAL_SPLIT);
102         splitPaneB.setLeftComponent(splitPaneA);
103         splitPaneB.setRightComponent(console.getContainer());
104         splitPaneB.setContinuousLayout(true);
105         splitPaneB.setOneTouchExpandable(true);
106         splitPaneB.setDividerLocation(getRootPane().getPreferredSize().height);
107
108         projectPanel.add(splitPaneB, BorderLayout.CENTER);
109
110         getContentPane().add(projectPanel);
111
112         pack();
113     }
114
115     public void awake() {
116         super.awake();
117         getData().setProject(this);
118         projectDefaultMainMenuBar = mainMenuBar;
119     }
120
121     public void setDefaultSize() {
122         Rectangle r = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
123         r.height *= 0.75;
124         r.width *= 0.8;
125         getRootPane().setPreferredSize(r.getSize());
126     }
127
128     public void setToolBar(JComponent toolbar) {
129         projectToolbar.setCustomToolbar(toolbar);
130     }
131
132     public void setStatusBar(JComponent statusbar) {
133         if(projectStatusBar.getComponentCount() > 0)
134             projectStatusBar.remove(0);
135
136         if(statusbar != null)
137             projectStatusBar.add(statusbar, BorderLayout.CENTER);
138
139         projectStatusBar.revalidate();
140         projectStatusBar.repaint();
141     }
142
143     public void setDefaultMainMenuBar() {
144         if(getMainMenuBar() != projectDefaultMainMenuBar)
145             setMainMenuBar(projectDefaultMainMenuBar);
146     }
147
148     public void refreshMainMenuBar() {
149         if(getXJFrame().getMainMenuBar() != null)
150             getXJFrame().getMainMenuBar().refreshState();
151     }
152
153     @Override JavaDoc
154     protected String JavaDoc customizeWindowTitle(String JavaDoc title) {
155         return super.customizeWindowTitle(title)+" - [Project]";
156     }
157
158     public ProjectData getData() {
159         return ((CDocumentProject)getDocument()).data;
160     }
161
162     public ProjectBuildList getBuildList() {
163         return buildList;
164     }
165
166     public String JavaDoc getSourcePath() {
167         return getData().getSourcePath();
168     }
169
170     public void makeBottomComponentVisible() {
171         if(splitPaneB.getBottomComponent().getHeight() == 0) {
172             splitPaneB.setDividerLocation(0.6);
173         }
174     }
175
176     public XJFrameInterface getXJFrame() {
177         return this;
178     }
179
180     public void becomingVisibleForTheFirstTime() {
181         // Show the Project Settings if the project is just created
182
if(getDocument().getDocumentPath() == null) {
183             getDocument().changeDone();
184             SwingUtilities.invokeLater(new Runnable JavaDoc() {
185                 public void run() {
186                     projectSettings();
187                 }
188             });
189         }
190     }
191
192     public void windowActivated() {
193         super.windowActivated();
194
195         if(getBuildList().handleExternalModification())
196             changeDone();
197
198         explorer.windowActivated();
199     }
200
201     public void windowDeactivated() {
202         explorer.saveAll();
203     }
204
205     public void windowDocumentPathDidChange() {
206         // Called when the document associated file has changed on the disk
207
// Not used because we don't allow external modification of the project file
208
}
209
210     public void changeDone() {
211         getDocument().changeDone();
212     }
213
214     public void openFileItem(ProjectFileItem item) {
215         if(item != null) {
216             editorZone.openFileItem(item);
217             changeDone();
218         }
219     }
220
221     public void closeFileItem(ProjectFileItem fileItem) {
222         editorZone.removeFileItemFromTab(fileItem);
223         fileItem.close();
224     }
225     
226     public List JavaDoc getFileEditorItems() {
227         return explorer.getFileEditorItems();
228     }
229
230     public String JavaDoc[] getRunParameters() {
231         String JavaDoc s = getData().getRunParametersString();
232         if(s == null)
233             return null;
234         else
235             return s.split(" ");
236     }
237
238     public void projectSettings() {
239         ProjectSettingsDialog dialog = new ProjectSettingsDialog(getJavaContainer());
240         dialog.setSourcePath(getData().getSourcePath());
241         if(dialog.runModal() == XJDialog.BUTTON_OK) {
242             getData().setSourcePath(dialog.getSourcePath());
243             explorer.reload();
244             changeDone();
245         }
246     }
247
248     public void runSettings(boolean runAfterSettings) {
249         ProjectRunSettingsDialog dialog = new ProjectRunSettingsDialog(getJavaContainer());
250         dialog.setRunParametersString(getData().getRunParametersString());
251         dialog.setShowBeforeRunning(getData().getShowBeforeRunning());
252         if(dialog.runModal() == XJDialog.BUTTON_OK) {
253             changeDone();
254             getData().setRunParametersString(dialog.getRunParametersString());
255             getData().setShowBeforeRunning(dialog.isShowBeforeRunning());
256             if(runAfterSettings) {
257                 buildAndRun();
258             }
259         }
260     }
261
262     public void run() {
263         String JavaDoc[] params = getRunParameters();
264         if(params == null || params.length == 0 || getData().getShowBeforeRunning())
265             runSettings(true);
266         else
267             buildAndRun();
268     }
269
270     public void clean() {
271         builder.clean();
272     }
273
274     public void buildFile() {
275         ProjectFileItem item = editorZone.getSelectedFileItem();
276         if(item != null) {
277             explorer.saveAll();
278             clearConsole();
279
280             // Build the file
281
builder.buildFile(item);
282         }
283     }
284
285     public void buildAll() {
286         explorer.saveAll();
287         clearConsole();
288         builder.buildAll();
289     }
290
291     public void buildAndRun() {
292         explorer.saveAll();
293         clearConsole();
294         builder.run();
295     }
296
297     public void buildReportError(String JavaDoc error) {
298         printToConsole(error);
299     }
300
301     public void clearConsole() {
302         console.clear();
303     }
304
305     public void printToConsole(String JavaDoc s) {
306         console.print(s);
307         makeBottomComponentVisible();
308     }
309
310     public void printToConsole(Exception JavaDoc e) {
311         printToConsole(XJUtils.stackTrace(e));
312     }
313
314     public void close() {
315         // Automatically save the project
316
getDocument().performAutoSave();
317
318         explorer.close();
319         super.close();
320     }
321
322     public static final String JavaDoc KEY_WINDOW_LOC = "KEY_WINDOW_LOC";
323     public static final String JavaDoc KEY_WINDOW_SIZE = "KEY_WINDOW_SIZE";
324     public static final String JavaDoc KEY_SPLITPANE_A = "KEY_SPLITPANE_A";
325     public static final String JavaDoc KEY_SPLITPANE_B = "KEY_SPLITPANE_B";
326
327     public void setPersistentData(Map JavaDoc data) {
328         if(data == null)
329             return;
330
331         Point loc = (Point) data.get(KEY_WINDOW_LOC);
332         if(loc != null)
333             setLocation(loc);
334
335         Dimension size = (Dimension) data.get(KEY_WINDOW_SIZE);
336         if(size != null)
337             setSize(size);
338
339         Integer JavaDoc i = (Integer JavaDoc) data.get(KEY_SPLITPANE_A);
340         if(i != null)
341             splitPaneA.setDividerLocation(i.intValue());
342
343         i = (Integer JavaDoc) data.get(KEY_SPLITPANE_B);
344         if(i != null)
345             splitPaneB.setDividerLocation(i.intValue());
346     }
347
348     public Map JavaDoc getPersistentData() {
349         Map JavaDoc<String JavaDoc, Serializable JavaDoc> data = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>();
350         data.put(KEY_WINDOW_LOC, getLocation());
351         data.put(KEY_WINDOW_SIZE, getSize());
352         data.put(KEY_SPLITPANE_A, splitPaneA.getDividerLocation());
353         data.put(KEY_SPLITPANE_B, splitPaneB.getDividerLocation());
354         return data;
355     }
356
357     /** This method is called *very* frequently so it has to be really efficient
358      *
359      */

360
361     public void fileDidBecomeDirty(CContainerProjectFile file, ProjectFileItem item) {
362         getBuildList().setFileDirty(item, true);
363         changeDone();
364     }
365
366     public void documentDidLoad() {
367         buildList.setPersistentData((Map JavaDoc<String JavaDoc,Map JavaDoc>) getData().getBuildListData());
368         explorer.setPersistentData((Map JavaDoc) getData().getExplorerData());
369         editorZone.setPersistentData((Map JavaDoc) getData().getEditorZoneData());
370         setPersistentData((Map JavaDoc) getData().getContainerData());
371     }
372
373     public void documentWillSave() {
374         getData().setBuildListData(buildList.getPersistentData());
375         getData().setExplorerData(explorer.getPersistentData());
376         getData().setEditorZoneData(editorZone.getPersistentData());
377         getData().setContainerData(getPersistentData());
378         explorer.saveAll();
379     }
380
381     public static final int MI_CLEAN = 500;
382     public static final int MI_BUILD_FILE = 501;
383     public static final int MI_BUILD_ALL = 502;
384     public static final int MI_RUN = 503;
385     public static final int MI_RUN_SETTINGS = 504;
386     public static final int MI_SETTINGS = 505;
387     public static final int MI_CLOSE_EDITOR = 506;
388     public static final int MI_MOVE_EDITOR_LEFT = 507;
389     public static final int MI_MOVE_EDITOR_RIGHT = 508;
390
391     public void createProjectMenu(XJMainMenuBar menubar) {
392         XJMenu menu;
393         menu = new XJMenu();
394         menu.setTitle("Project");
395         menu.addItem(new XJMenuItem("Run", MI_RUN, this));
396         menu.addItem(new XJMenuItem("Edit Run Settings...", MI_RUN_SETTINGS, this));
397         menu.addSeparator();
398         menu.addItem(new XJMenuItem("Build All", MI_BUILD_ALL, this));
399         menu.addItem(new XJMenuItem("Build File", MI_BUILD_FILE, this));
400         menu.addSeparator();
401         menu.addItem(new XJMenuItem("Clean", MI_CLEAN, this));
402         menu.addSeparator();
403         menu.addItem(new XJMenuItem("Move Active Editor Left", KeyEvent.VK_LEFT, XJMenuItem.getKeyModifier() | Event.CTRL_MASK, MI_MOVE_EDITOR_LEFT, this));
404         menu.addItem(new XJMenuItem("Move Active Editor Right", KeyEvent.VK_RIGHT, XJMenuItem.getKeyModifier() | Event.CTRL_MASK, MI_MOVE_EDITOR_RIGHT, this));
405         menu.addSeparator();
406         menu.addItem(new XJMenuItem("Settings...", KeyEvent.VK_SEMICOLON, MI_SETTINGS, this));
407
408         menubar.addCustomMenu(menu);
409     }
410
411     public void customizeFileMenu(XJMenu menu) {
412         menu.insertItemAfter(new XJMenuItem("Close Active Editor", KeyEvent.VK_F4, MI_CLOSE_EDITOR, this), XJMainMenuBar.MI_CLOSE);
413     }
414
415     public void customizeMenuBar(XJMainMenuBar menubar) {
416         createProjectMenu(menubar);
417     }
418
419     public void menuItemState(XJMenuItem item) {
420         super.menuItemState(item);
421         switch(item.getTag()) {
422             case MI_CLOSE_EDITOR:
423             case MI_MOVE_EDITOR_LEFT:
424             case MI_MOVE_EDITOR_RIGHT:
425                 item.setEnabled(editorZone.getSelectedFileItem() != null);
426                 break;
427         }
428     }
429
430     public void handleMenuEvent(XJMenu menu, XJMenuItem item) {
431         super.handleMenuEvent(menu, item);
432
433         switch(item.getTag()) {
434             case MI_CLEAN:
435                 clean();
436                 break;
437
438             case MI_BUILD_FILE:
439                 buildFile();
440                 break;
441
442             case MI_BUILD_ALL:
443                 buildAll();
444                 break;
445
446             case MI_RUN:
447                 run();
448                 break;
449
450             case MI_RUN_SETTINGS:
451                 runSettings(false);
452                 break;
453
454             case MI_SETTINGS:
455                 projectSettings();
456                 break;
457
458             case MI_CLOSE_EDITOR:
459                 editorZone.closeActiveEditor();
460                 break;
461
462             case MI_MOVE_EDITOR_LEFT:
463                 editorZone.moveActiveEditor(ProjectEditorZone.DIRECTION_LEFT);
464                 break;
465
466             case MI_MOVE_EDITOR_RIGHT:
467                 editorZone.moveActiveEditor(ProjectEditorZone.DIRECTION_RIGHT);
468                 break;
469         }
470     }
471
472     public void loadText(String JavaDoc text) {
473         // Not used for project
474
}
475
476     public String JavaDoc getText() {
477         // Not used for project
478
return null;
479     }
480
481     public boolean willSaveDocument() {
482         // Not used for project
483
return true;
484     }
485
486     public ComponentEditor getEditor() {
487         // There is no specific editor for the project
488
return null;
489     }
490
491 }
492
Popular Tags