KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > menu > MenuSCM


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

31
32
33 package org.antlr.works.menu;
34
35 import org.antlr.xjlib.appkit.utils.XJAlert;
36 import org.antlr.xjlib.appkit.utils.XJDialogProgress;
37 import org.antlr.works.components.grammar.CEditorGrammar;
38 import org.antlr.works.prefs.AWPrefs;
39 import org.antlr.works.scm.SCM;
40 import org.antlr.works.scm.SCMDelegate;
41 import org.antlr.works.scm.p4.P4;
42 import org.antlr.works.scm.p4.P4SubmitDialog;
43 import org.antlr.works.stats.StatisticsAW;
44
45 public class MenuSCM extends MenuAbstract implements SCMDelegate {
46
47     protected XJDialogProgress progress;
48     protected SCM scm;
49     protected boolean silent;
50
51     public MenuSCM(CEditorGrammar editor) {
52         super(editor);
53     }
54
55     public void awake() {
56         scm = new P4(this, editor.console);
57     }
58
59     public void setSilent(boolean silent) {
60         this.silent = silent;
61     }
62
63     public void queryFileStatus() {
64         if(getFilePath() != null && AWPrefs.getP4Enabled())
65             scm.queryFileStatus(getFilePath());
66     }
67
68     public boolean isFileWritable() {
69         return scm.isFileWritable();
70     }
71
72     public void editFile() {
73         StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_SCM_EDIT);
74         if(check()) {
75             showProgress("Open for Edit");
76             scm.editFile(getFilePath());
77         }
78     }
79
80     public void addFile() {
81         StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_SCM_ADD);
82         if(check()) {
83             showProgress("Mark for Add");
84             scm.addFile(getFilePath());
85         }
86     }
87
88     public void deleteFile() {
89         StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_SCM_DELETE);
90         if(check()) {
91             showProgress("Mark for Delete");
92             scm.deleteFile(getFilePath());
93         }
94     }
95
96     public void revertFile() {
97         StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_SCM_REVERT);
98         if(check()) {
99             if(XJAlert.displayAlertYESNO(editor.getWindowContainer(), "Revert", "Are you sure you want to discard your changes to the file ?") == XJAlert.YES) {
100                 showProgress("Revert");
101                 scm.revertFile(getFilePath());
102             }
103         }
104     }
105
106     public void submitFile() {
107         StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_SCM_SUBMIT);
108         if(check()) {
109             P4SubmitDialog dialog = new P4SubmitDialog(editor.getWindowContainer());
110             if(dialog.runModal() == P4SubmitDialog.BUTTON_OK) {
111                 showProgress("Submit");
112                 scm.submitFile(getFilePath(), dialog.getDescription(), dialog.getRemainOpen());
113             }
114         }
115     }
116
117     public void sync() {
118         StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_SCM_SYNC);
119         if(check()) {
120             showProgress("Sync");
121             scm.sync();
122         }
123     }
124
125     protected boolean check() {
126         if(getFilePath() == null) {
127             XJAlert.display(editor.getWindowContainer(), "SCM Error", "The file must be saved to the disk before any SCM command can be executed.");
128             return false;
129         }
130         return true;
131     }
132
133     protected void displayErrors() {
134         if(scm.hasErrors()) {
135             XJAlert.display(editor.getWindowContainer(), "SCM Error", scm.getErrorsDescription());
136             scm.resetErrors();
137         }
138     }
139
140     protected void showProgress(String JavaDoc title) {
141         if(progress == null)
142             progress = new XJDialogProgress(editor.getWindowContainer());
143         
144         progress.setInfo(title);
145         progress.setCancellable(false);
146         progress.setIndeterminate(true);
147         progress.display();
148         setSilent(false);
149     }
150
151     protected void hideProgress() {
152         progress.close();
153     }
154
155     protected String JavaDoc getFilePath() {
156         return editor.getFilePath();
157     }
158
159     public void scmCommandsDidComplete() {
160         if(!silent) {
161             hideProgress();
162             displayErrors();
163         }
164         editor.scmCommandsDidComplete();
165     }
166
167     public void scmFileStatusDidChange(String JavaDoc status) {
168         editor.updateSCMStatus(status);
169     }
170
171     public void scmLog(String JavaDoc log) {
172         editor.console.println(log);
173     }
174
175 }
176
Popular Tags