PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/mercurial/src/org/netbeans/modules/mercurial/HgProgressSupport.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 256 lines | 178 code | 31 blank | 47 comment | 23 complexity | 2a0d196c937ce0b445cc447919b6b449 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2009 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.netbeans.modules.mercurial;
  45. import java.awt.event.ActionEvent;
  46. import java.awt.event.ActionListener;
  47. import java.beans.PropertyChangeListener;
  48. import java.io.File;
  49. import java.text.DateFormat;
  50. import java.util.Date;
  51. import java.util.logging.Level;
  52. import javax.swing.Action;
  53. import javax.swing.JComponent;
  54. import javax.swing.JButton;
  55. import org.netbeans.api.progress.ProgressHandle;
  56. import org.netbeans.api.progress.ProgressHandleFactory;
  57. import org.netbeans.modules.mercurial.ui.repository.HgURL;
  58. import org.netbeans.modules.mercurial.util.HgUtils;
  59. import org.openide.util.Cancellable;
  60. import org.openide.util.NbBundle;
  61. import org.openide.util.RequestProcessor;
  62. import org.openide.util.TaskListener;
  63. /**
  64. *
  65. * @author Tomas Stupka
  66. */
  67. public abstract class HgProgressSupport implements Runnable, Cancellable {
  68. private Cancellable delegate;
  69. private volatile boolean canceled;
  70. private ProgressHandle progressHandle = null;
  71. private String displayName = ""; // NOI18N
  72. private String originalDisplayName = ""; // NOI18N
  73. private OutputLogger logger;
  74. private HgURL repositoryRoot;
  75. private RequestProcessor.Task task;
  76. public HgProgressSupport() {
  77. }
  78. public HgProgressSupport(String displayName, JButton cancel) {
  79. this.displayName = displayName;
  80. if(cancel != null) {
  81. cancel.addActionListener(new ActionListener() {
  82. public void actionPerformed(ActionEvent e) {
  83. cancel();
  84. }
  85. });
  86. }
  87. }
  88. public RequestProcessor.Task start(RequestProcessor rp, String displayName) {
  89. return start(rp, new File(""), displayName); //NOI18N
  90. }
  91. public RequestProcessor.Task start(RequestProcessor rp, File repositoryRoot, String displayName) {
  92. HgURL hgUrl = (repositoryRoot != null) ? new HgURL(repositoryRoot)
  93. : null;
  94. return start(rp, hgUrl, displayName);
  95. }
  96. public RequestProcessor.Task start(RequestProcessor rp, HgURL repositoryRoot, String displayName) {
  97. setDisplayName(displayName);
  98. this.repositoryRoot = repositoryRoot;
  99. startProgress();
  100. setProgressQueued();
  101. task = rp.post(this);
  102. task.addTaskListener(new TaskListener() {
  103. public void taskFinished(org.openide.util.Task task) {
  104. delegate = null;
  105. }
  106. });
  107. return task;
  108. }
  109. public RequestProcessor.Task start(RequestProcessor rp) {
  110. startProgress();
  111. task = rp.post(this);
  112. return task;
  113. }
  114. public JComponent getProgressComponent() {
  115. return ProgressHandleFactory.createProgressComponent(getProgressHandle());
  116. }
  117. public void setRepositoryRoot(HgURL repositoryRoot) {
  118. this.repositoryRoot = repositoryRoot;
  119. logger = null;
  120. }
  121. protected HgURL getRepositoryRoot() {
  122. return repositoryRoot;
  123. }
  124. public void run() {
  125. setProgress();
  126. performIntern();
  127. }
  128. protected void performIntern() {
  129. try {
  130. log("Start - " + displayName); // NOI18N
  131. if(!canceled) {
  132. perform();
  133. }
  134. } finally {
  135. log("End - " + displayName); // NOI18N
  136. finnishProgress();
  137. getLogger().closeLog();
  138. }
  139. }
  140. protected abstract void perform();
  141. public synchronized boolean isCanceled() {
  142. return canceled;
  143. }
  144. public synchronized boolean cancel() {
  145. if(delegate != null) {
  146. if(!delegate.cancel())
  147. return false;
  148. }
  149. if (canceled) {
  150. return false;
  151. }
  152. if(task != null) {
  153. task.cancel();
  154. }
  155. Mercurial.getInstance().clearRequestProcessor(repositoryRoot);
  156. finnishProgress();
  157. canceled = true;
  158. return true;
  159. }
  160. public void setCancellableDelegate(Cancellable cancellable) {
  161. this.delegate = cancellable;
  162. }
  163. public void setDisplayName(String displayName) {
  164. if(originalDisplayName.equals("")) { // NOI18N
  165. originalDisplayName = displayName;
  166. }
  167. logChangedDisplayName(this.displayName, displayName);
  168. this.displayName = displayName;
  169. setProgress();
  170. }
  171. private void setProgressQueued() {
  172. if(progressHandle!=null) {
  173. progressHandle.progress(NbBundle.getMessage(HgProgressSupport.class, "LBL_Queued", displayName)); // NOI18N
  174. }
  175. }
  176. private void setProgress() {
  177. if(progressHandle!=null) {
  178. progressHandle.progress(displayName);
  179. }
  180. }
  181. protected String getDisplayName() {
  182. return displayName;
  183. }
  184. protected ProgressHandle getProgressHandle() {
  185. if(progressHandle==null) {
  186. progressHandle = ProgressHandleFactory.createHandle(displayName, this, getLogger().getOpenOutputAction());
  187. }
  188. return progressHandle;
  189. }
  190. protected void startProgress() {
  191. getProgressHandle().start();
  192. }
  193. protected void finnishProgress() {
  194. getProgressHandle().finish();
  195. }
  196. public OutputLogger getLogger() {
  197. if (logger == null) {
  198. String loggerId = (repositoryRoot != null)
  199. ? repositoryRoot.toHgCommandUrlStringWithoutUserInfo()
  200. : null;
  201. logger = Mercurial.getInstance().getLogger(loggerId);
  202. }
  203. return logger;
  204. }
  205. public void annotate(HgException ex) {
  206. ExceptionHandler eh = new ExceptionHandler(ex);
  207. if(isCanceled()) {
  208. eh.notifyException(false);
  209. } else {
  210. eh.notifyException();
  211. }
  212. }
  213. private static void log(String msg) {
  214. HgUtils.logT9Y(msg);
  215. Mercurial.LOG.log(Level.FINE, msg);
  216. }
  217. private void logChangedDisplayName(String thisDisplayName, String displayName) {
  218. if(thisDisplayName != null && !thisDisplayName.equals(displayName)) {
  219. if(!thisDisplayName.equals("")) {
  220. log("End - " + thisDisplayName); // NOI18N
  221. log("Start - " + displayName); // NOI18N
  222. }
  223. }
  224. }
  225. }